| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/filters/blocking_url_protocol.h" | 5 #include "media/filters/blocking_url_protocol.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "media/base/data_source.h" | 8 #include "media/base/data_source.h" |
| 9 #include "media/ffmpeg/ffmpeg_common.h" | 9 #include "media/ffmpeg/ffmpeg_common.h" |
| 10 | 10 |
| 11 namespace media { | 11 namespace media { |
| 12 | 12 |
| 13 BlockingUrlProtocol::BlockingUrlProtocol( | 13 BlockingUrlProtocol::BlockingUrlProtocol( |
| 14 const scoped_refptr<DataSource>& data_source, | 14 const scoped_refptr<DataSource>& data_source, |
| 15 const base::Closure& error_cb) | 15 const base::Closure& error_cb) |
| 16 : data_source_(data_source), | 16 : data_source_(data_source), |
| 17 error_cb_(error_cb), | 17 error_cb_(error_cb), |
| 18 read_event_(false, false), | 18 aborted_(true, false), // We never want to reset |aborted_|. |
| 19 read_has_failed_(false), | 19 read_complete_(false, false), |
| 20 last_read_bytes_(0), | 20 last_read_bytes_(0), |
| 21 read_position_(0) { | 21 read_position_(0) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 BlockingUrlProtocol::~BlockingUrlProtocol() {} | 24 BlockingUrlProtocol::~BlockingUrlProtocol() {} |
| 25 | 25 |
| 26 void BlockingUrlProtocol::Abort() { | 26 void BlockingUrlProtocol::Abort() { |
| 27 SignalReadCompleted(DataSource::kReadError); | 27 aborted_.Signal(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 int BlockingUrlProtocol::Read(int size, uint8* data) { | 30 int BlockingUrlProtocol::Read(int size, uint8* data) { |
| 31 // Read errors are unrecoverable. | 31 // Read errors are unrecoverable. |
| 32 if (read_has_failed_) | 32 if (aborted_.IsSignaled()) |
| 33 return AVERROR(EIO); | 33 return AVERROR(EIO); |
| 34 | 34 |
| 35 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O | 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. | 36 // routines. Instead return 0 for any read at or past EOF. |
| 37 int64 file_size; | 37 int64 file_size; |
| 38 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) | 38 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) |
| 39 return 0; | 39 return 0; |
| 40 | 40 |
| 41 // Blocking read from data source until |last_read_bytes_| is set and event is | 41 // Blocking read from data source until either: |
| 42 // signalled. | 42 // 1) |last_read_bytes_| is set and |read_complete_| is signalled |
| 43 // 2) |aborted_| is signalled |
| 43 data_source_->Read(read_position_, size, data, base::Bind( | 44 data_source_->Read(read_position_, size, data, base::Bind( |
| 44 &BlockingUrlProtocol::SignalReadCompleted, base::Unretained(this))); | 45 &BlockingUrlProtocol::SignalReadCompleted, base::Unretained(this))); |
| 45 read_event_.Wait(); | 46 |
| 47 base::WaitableEvent* events[] = { &aborted_, &read_complete_ }; |
| 48 size_t index = base::WaitableEvent::WaitMany(events, arraysize(events)); |
| 49 |
| 50 if (events[index] == &aborted_) |
| 51 return AVERROR(EIO); |
| 46 | 52 |
| 47 if (last_read_bytes_ == DataSource::kReadError) { | 53 if (last_read_bytes_ == DataSource::kReadError) { |
| 48 // TODO(scherkus): We shouldn't fire |error_cb_| if it was due to Abort(). | 54 aborted_.Signal(); |
| 49 error_cb_.Run(); | 55 error_cb_.Run(); |
| 50 read_has_failed_ = true; | |
| 51 return AVERROR(EIO); | 56 return AVERROR(EIO); |
| 52 } | 57 } |
| 53 | 58 |
| 54 read_position_ += last_read_bytes_; | 59 read_position_ += last_read_bytes_; |
| 55 return last_read_bytes_; | 60 return last_read_bytes_; |
| 56 } | 61 } |
| 57 | 62 |
| 58 bool BlockingUrlProtocol::GetPosition(int64* position_out) { | 63 bool BlockingUrlProtocol::GetPosition(int64* position_out) { |
| 59 *position_out = read_position_; | 64 *position_out = read_position_; |
| 60 return true; | 65 return true; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 74 bool BlockingUrlProtocol::GetSize(int64* size_out) { | 79 bool BlockingUrlProtocol::GetSize(int64* size_out) { |
| 75 return data_source_->GetSize(size_out); | 80 return data_source_->GetSize(size_out); |
| 76 } | 81 } |
| 77 | 82 |
| 78 bool BlockingUrlProtocol::IsStreaming() { | 83 bool BlockingUrlProtocol::IsStreaming() { |
| 79 return data_source_->IsStreaming(); | 84 return data_source_->IsStreaming(); |
| 80 } | 85 } |
| 81 | 86 |
| 82 void BlockingUrlProtocol::SignalReadCompleted(int size) { | 87 void BlockingUrlProtocol::SignalReadCompleted(int size) { |
| 83 last_read_bytes_ = size; | 88 last_read_bytes_ = size; |
| 84 read_event_.Signal(); | 89 read_complete_.Signal(); |
| 85 } | 90 } |
| 86 | 91 |
| 87 } // namespace media | 92 } // namespace media |
| OLD | NEW |