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