| 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 #ifndef MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ | 5 #ifndef MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ |
| 6 #define MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ | 6 #define MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "media/filters/ffmpeg_glue.h" | 11 #include "media/filters/ffmpeg_glue.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 class DataSource; | 15 class DataSource; |
| 16 | 16 |
| 17 // An implementation of FFmpegURLProtocol that blocks until the underlying | 17 // An implementation of FFmpegURLProtocol that blocks until the underlying |
| 18 // asynchronous DataSource::Read() operation completes. | 18 // asynchronous DataSource::Read() operation completes. |
| 19 // | |
| 20 // TODO(scherkus): Should be more thread-safe as |last_read_bytes_| is updated | |
| 21 // from multiple threads without any protection. | |
| 22 class MEDIA_EXPORT BlockingUrlProtocol : public FFmpegURLProtocol { | 19 class MEDIA_EXPORT BlockingUrlProtocol : public FFmpegURLProtocol { |
| 23 public: | 20 public: |
| 24 // Implements FFmpegURLProtocol using the given |data_source|. |error_cb| is | 21 // Implements FFmpegURLProtocol using the given |data_source|. |error_cb| is |
| 25 // fired any time DataSource::Read() returns an error. | 22 // fired any time DataSource::Read() returns an error. |
| 26 // | 23 // |
| 27 // TODO(scherkus): After all blocking operations are isolated on a separate | 24 // TODO(scherkus): After all blocking operations are isolated on a separate |
| 28 // thread we should be able to eliminate |error_cb|. | 25 // thread we should be able to eliminate |error_cb|. |
| 29 BlockingUrlProtocol(const scoped_refptr<DataSource>& data_source, | 26 BlockingUrlProtocol(const scoped_refptr<DataSource>& data_source, |
| 30 const base::Closure& error_cb); | 27 const base::Closure& error_cb); |
| 31 virtual ~BlockingUrlProtocol(); | 28 virtual ~BlockingUrlProtocol(); |
| 32 | 29 |
| 33 // Aborts any pending reads by returning a read error. After this method | 30 // Aborts any pending reads by returning a read error. After this method |
| 34 // returns all subsequent calls to Read() will immediately fail. | 31 // returns all subsequent calls to Read() will immediately fail. |
| 35 // | |
| 36 // TODO(scherkus): Currently this will cause |error_cb| to fire. Fix. | |
| 37 void Abort(); | 32 void Abort(); |
| 38 | 33 |
| 39 // FFmpegURLProtocol implementation. | 34 // FFmpegURLProtocol implementation. |
| 40 virtual int Read(int size, uint8* data) OVERRIDE; | 35 virtual int Read(int size, uint8* data) OVERRIDE; |
| 41 virtual bool GetPosition(int64* position_out) OVERRIDE; | 36 virtual bool GetPosition(int64* position_out) OVERRIDE; |
| 42 virtual bool SetPosition(int64 position) OVERRIDE; | 37 virtual bool SetPosition(int64 position) OVERRIDE; |
| 43 virtual bool GetSize(int64* size_out) OVERRIDE; | 38 virtual bool GetSize(int64* size_out) OVERRIDE; |
| 44 virtual bool IsStreaming() OVERRIDE; | 39 virtual bool IsStreaming() OVERRIDE; |
| 45 | 40 |
| 46 private: | 41 private: |
| 47 // Sets |last_read_bytes_| and signals the blocked thread that the read | 42 // Sets |last_read_bytes_| and signals the blocked thread that the read |
| 48 // has completed. | 43 // has completed. |
| 49 void SignalReadCompleted(int size); | 44 void SignalReadCompleted(int size); |
| 50 | 45 |
| 51 scoped_refptr<DataSource> data_source_; | 46 scoped_refptr<DataSource> data_source_; |
| 52 base::Closure error_cb_; | 47 base::Closure error_cb_; |
| 53 | 48 |
| 54 // Used to convert an asynchronous DataSource::Read() into a blocking | 49 // Used to unblock the thread during shutdown and when reads complete. |
| 55 // FFmpegUrlProtocol::Read(). | 50 base::WaitableEvent aborted_; |
| 56 base::WaitableEvent read_event_; | 51 base::WaitableEvent read_complete_; |
| 57 | |
| 58 // Read errors and aborts are unrecoverable. Any subsequent reads will | |
| 59 // immediately fail when this is set to true. | |
| 60 bool read_has_failed_; | |
| 61 | 52 |
| 62 // Cached number of bytes last read from the data source. | 53 // Cached number of bytes last read from the data source. |
| 63 int last_read_bytes_; | 54 int last_read_bytes_; |
| 64 | 55 |
| 65 // Cached position within the data source. | 56 // Cached position within the data source. |
| 66 int64 read_position_; | 57 int64 read_position_; |
| 67 | 58 |
| 68 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockingUrlProtocol); | 59 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockingUrlProtocol); |
| 69 }; | 60 }; |
| 70 | 61 |
| 71 } // namespace media | 62 } // namespace media |
| 72 | 63 |
| 73 #endif // MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ | 64 #endif // MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_ |
| OLD | NEW |