1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/blocking_url_protocol.h"
8 #include "media/base/data_source.h"
9 #include "media/ffmpeg/ffmpeg_common.h"
13 BlockingUrlProtocol::BlockingUrlProtocol(
14 DataSource
* data_source
,
15 const base::Closure
& error_cb
)
16 : data_source_(data_source
),
18 aborted_(true, false), // We never want to reset |aborted_|.
19 read_complete_(false, false),
24 BlockingUrlProtocol::~BlockingUrlProtocol() {}
26 void BlockingUrlProtocol::Abort() {
30 int BlockingUrlProtocol::Read(int size
, uint8
* data
) {
31 // Read errors are unrecoverable.
32 if (aborted_
.IsSignaled())
35 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O
36 // routines. Instead return 0 for any read at or past EOF.
38 if (data_source_
->GetSize(&file_size
) && read_position_
>= file_size
)
41 // Blocking read from data source until either:
42 // 1) |last_read_bytes_| is set and |read_complete_| is signalled
43 // 2) |aborted_| is signalled
44 data_source_
->Read(read_position_
, size
, data
, base::Bind(
45 &BlockingUrlProtocol::SignalReadCompleted
, base::Unretained(this)));
47 base::WaitableEvent
* events
[] = { &aborted_
, &read_complete_
};
48 size_t index
= base::WaitableEvent::WaitMany(events
, arraysize(events
));
50 if (events
[index
] == &aborted_
)
53 if (last_read_bytes_
== DataSource::kReadError
) {
59 read_position_
+= last_read_bytes_
;
60 return last_read_bytes_
;
63 bool BlockingUrlProtocol::GetPosition(int64
* position_out
) {
64 *position_out
= read_position_
;
68 bool BlockingUrlProtocol::SetPosition(int64 position
) {
70 if ((data_source_
->GetSize(&file_size
) && position
> file_size
) ||
75 read_position_
= position
;
79 bool BlockingUrlProtocol::GetSize(int64
* size_out
) {
80 return data_source_
->GetSize(size_out
);
83 bool BlockingUrlProtocol::IsStreaming() {
84 return data_source_
->IsStreaming();
87 void BlockingUrlProtocol::SignalReadCompleted(int size
) {
88 last_read_bytes_
= size
;
89 read_complete_
.Signal();