| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "media/base/data_source.h" | 11 #include "media/base/data_source.h" |
| 12 #include "media/ffmpeg/ffmpeg_common.h" | 12 #include "media/ffmpeg/ffmpeg_common.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 | 15 |
| 16 BlockingUrlProtocol::BlockingUrlProtocol(DataSource* data_source, | 16 BlockingUrlProtocol::BlockingUrlProtocol(DataSource* data_source, |
| 17 const base::Closure& error_cb) | 17 const base::Closure& error_cb) |
| 18 : data_source_(data_source), | 18 : data_source_(data_source), |
| 19 error_cb_(error_cb), | 19 error_cb_(error_cb), |
| 20 is_streaming_(data_source_->IsStreaming()), | |
| 21 aborted_(base::WaitableEvent::ResetPolicy::MANUAL, | 20 aborted_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 22 base::WaitableEvent::InitialState::NOT_SIGNALED), // We never | 21 base::WaitableEvent::InitialState::NOT_SIGNALED), // We never |
| 23 // want to | 22 // want to |
| 24 // reset | 23 // reset |
| 25 // |aborted_|. | 24 // |aborted_|. |
| 26 read_complete_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 25 read_complete_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 27 base::WaitableEvent::InitialState::NOT_SIGNALED), | 26 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 28 last_read_bytes_(0), | 27 last_read_bytes_(0), |
| 29 read_position_(0) {} | 28 read_position_(0) {} |
| 30 | 29 |
| 31 BlockingUrlProtocol::~BlockingUrlProtocol() {} | 30 BlockingUrlProtocol::~BlockingUrlProtocol() {} |
| 32 | 31 |
| 33 void BlockingUrlProtocol::Abort() { | 32 void BlockingUrlProtocol::Abort() { |
| 34 aborted_.Signal(); | 33 aborted_.Signal(); |
| 35 base::AutoLock lock(data_source_lock_); | |
| 36 data_source_ = nullptr; | |
| 37 } | 34 } |
| 38 | 35 |
| 39 int BlockingUrlProtocol::Read(int size, uint8_t* data) { | 36 int BlockingUrlProtocol::Read(int size, uint8_t* data) { |
| 40 { | 37 // Read errors are unrecoverable. |
| 41 // Read errors are unrecoverable. | 38 if (aborted_.IsSignaled()) |
| 42 base::AutoLock lock(data_source_lock_); | 39 return AVERROR(EIO); |
| 43 if (!data_source_) { | |
| 44 DCHECK(aborted_.IsSignaled()); | |
| 45 return AVERROR(EIO); | |
| 46 } | |
| 47 | 40 |
| 48 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O | 41 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O |
| 49 // routines. Instead return 0 for any read at or past EOF. | 42 // routines. Instead return 0 for any read at or past EOF. |
| 50 int64_t file_size; | 43 int64_t file_size; |
| 51 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) | 44 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) |
| 52 return 0; | 45 return 0; |
| 53 | 46 |
| 54 // Blocking read from data source until either: | 47 // Blocking read from data source until either: |
| 55 // 1) |last_read_bytes_| is set and |read_complete_| is signalled | 48 // 1) |last_read_bytes_| is set and |read_complete_| is signalled |
| 56 // 2) |aborted_| is signalled | 49 // 2) |aborted_| is signalled |
| 57 data_source_->Read(read_position_, size, data, | 50 data_source_->Read(read_position_, size, data, base::Bind( |
| 58 base::Bind(&BlockingUrlProtocol::SignalReadCompleted, | 51 &BlockingUrlProtocol::SignalReadCompleted, base::Unretained(this))); |
| 59 base::Unretained(this))); | |
| 60 } | |
| 61 | 52 |
| 62 base::WaitableEvent* events[] = { &aborted_, &read_complete_ }; | 53 base::WaitableEvent* events[] = { &aborted_, &read_complete_ }; |
| 63 size_t index = base::WaitableEvent::WaitMany(events, arraysize(events)); | 54 size_t index = base::WaitableEvent::WaitMany(events, arraysize(events)); |
| 64 | 55 |
| 65 if (events[index] == &aborted_) | 56 if (events[index] == &aborted_) |
| 66 return AVERROR(EIO); | 57 return AVERROR(EIO); |
| 67 | 58 |
| 68 if (last_read_bytes_ == DataSource::kReadError) { | 59 if (last_read_bytes_ == DataSource::kReadError) { |
| 69 aborted_.Signal(); | 60 aborted_.Signal(); |
| 70 error_cb_.Run(); | 61 error_cb_.Run(); |
| 71 return AVERROR(EIO); | 62 return AVERROR(EIO); |
| 72 } | 63 } |
| 73 | 64 |
| 74 if (last_read_bytes_ == DataSource::kAborted) | 65 if (last_read_bytes_ == DataSource::kAborted) |
| 75 return AVERROR(EIO); | 66 return AVERROR(EIO); |
| 76 | 67 |
| 77 read_position_ += last_read_bytes_; | 68 read_position_ += last_read_bytes_; |
| 78 return last_read_bytes_; | 69 return last_read_bytes_; |
| 79 } | 70 } |
| 80 | 71 |
| 81 bool BlockingUrlProtocol::GetPosition(int64_t* position_out) { | 72 bool BlockingUrlProtocol::GetPosition(int64_t* position_out) { |
| 82 *position_out = read_position_; | 73 *position_out = read_position_; |
| 83 return true; | 74 return true; |
| 84 } | 75 } |
| 85 | 76 |
| 86 bool BlockingUrlProtocol::SetPosition(int64_t position) { | 77 bool BlockingUrlProtocol::SetPosition(int64_t position) { |
| 87 base::AutoLock lock(data_source_lock_); | |
| 88 int64_t file_size; | 78 int64_t file_size; |
| 89 if (!data_source_ || | 79 if ((data_source_->GetSize(&file_size) && position > file_size) || |
| 90 (data_source_->GetSize(&file_size) && position > file_size) || | |
| 91 position < 0) { | 80 position < 0) { |
| 92 return false; | 81 return false; |
| 93 } | 82 } |
| 94 | 83 |
| 95 read_position_ = position; | 84 read_position_ = position; |
| 96 return true; | 85 return true; |
| 97 } | 86 } |
| 98 | 87 |
| 99 bool BlockingUrlProtocol::GetSize(int64_t* size_out) { | 88 bool BlockingUrlProtocol::GetSize(int64_t* size_out) { |
| 100 base::AutoLock lock(data_source_lock_); | 89 return data_source_->GetSize(size_out); |
| 101 return data_source_ ? data_source_->GetSize(size_out) : 0; | |
| 102 } | 90 } |
| 103 | 91 |
| 104 bool BlockingUrlProtocol::IsStreaming() { | 92 bool BlockingUrlProtocol::IsStreaming() { |
| 105 return is_streaming_; | 93 return data_source_->IsStreaming(); |
| 106 } | 94 } |
| 107 | 95 |
| 108 void BlockingUrlProtocol::SignalReadCompleted(int size) { | 96 void BlockingUrlProtocol::SignalReadCompleted(int size) { |
| 109 last_read_bytes_ = size; | 97 last_read_bytes_ = size; |
| 110 read_complete_.Signal(); | 98 read_complete_.Signal(); |
| 111 } | 99 } |
| 112 | 100 |
| 113 } // namespace media | 101 } // namespace media |
| OLD | NEW |