Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/filters/blocking_url_protocol.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "media/base/data_source.h" | |
| 9 #include "media/ffmpeg/ffmpeg_common.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 BlockingUrlProtocol::BlockingUrlProtocol( | |
| 14 const scoped_refptr<DataSource>& data_source, | |
| 15 const base::Closure& error_cb) | |
| 16 : data_source_(data_source), | |
| 17 error_cb_(error_cb), | |
| 18 read_event_(false, false), | |
| 19 read_has_failed_(false), | |
| 20 last_read_bytes_(0), | |
| 21 read_position_(0) { | |
| 22 } | |
| 23 | |
| 24 BlockingUrlProtocol::~BlockingUrlProtocol() {} | |
| 25 | |
| 26 void BlockingUrlProtocol::Abort() { | |
| 27 SignalReadCompleted(DataSource::kReadError); | |
| 28 } | |
| 29 | |
| 30 int BlockingUrlProtocol::Read(int size, uint8* data) { | |
| 31 // Read errors are unrecoverable. | |
| 32 if (read_has_failed_) | |
| 33 return AVERROR(EIO); | |
| 34 | |
| 35 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O | |
|
DaleCurtis
2012/11/13 03:42:28
+rbultje; I wonder if this is still true, Ronald?
| |
| 36 // routines. Instead return 0 for any read at or past EOF. | |
| 37 int64 file_size; | |
| 38 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) | |
| 39 return 0; | |
| 40 | |
| 41 // Blocking read from data source until |last_read_bytes_| is set and event is | |
| 42 // signalled. | |
| 43 data_source_->Read(read_position_, size, data, base::Bind( | |
| 44 &BlockingUrlProtocol::SignalReadCompleted, base::Unretained(this))); | |
| 45 read_event_.Wait(); | |
| 46 | |
| 47 if (last_read_bytes_ == DataSource::kReadError) { | |
| 48 // TODO(scherkus): We shouldn't fire |error_cb_| if it was due to Abort(). | |
| 49 error_cb_.Run(); | |
| 50 read_has_failed_ = true; | |
| 51 return AVERROR(EIO); | |
| 52 } | |
| 53 | |
| 54 read_position_ += last_read_bytes_; | |
| 55 return last_read_bytes_; | |
| 56 } | |
| 57 | |
| 58 bool BlockingUrlProtocol::GetPosition(int64* position_out) { | |
| 59 *position_out = read_position_; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 bool BlockingUrlProtocol::SetPosition(int64 position) { | |
| 64 int64 file_size; | |
| 65 if ((data_source_->GetSize(&file_size) && position >= file_size) || | |
| 66 position < 0) { | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 read_position_ = position; | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 bool BlockingUrlProtocol::GetSize(int64* size_out) { | |
| 75 return data_source_->GetSize(size_out); | |
| 76 } | |
| 77 | |
| 78 bool BlockingUrlProtocol::IsStreaming() { | |
| 79 return data_source_->IsStreaming(); | |
| 80 } | |
| 81 | |
| 82 void BlockingUrlProtocol::SignalReadCompleted(int size) { | |
| 83 last_read_bytes_ = size; | |
| 84 read_event_.Signal(); | |
| 85 } | |
| 86 | |
| 87 } // namespace media | |
| OLD | NEW |