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.
5 #ifndef MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_
6 #define MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "media/filters/ffmpeg_glue.h"
17 // An implementation of FFmpegURLProtocol that blocks until the underlying
18 // asynchronous DataSource::Read() operation completes.
19 class MEDIA_EXPORT BlockingUrlProtocol
: public FFmpegURLProtocol
{
21 // Implements FFmpegURLProtocol using the given |data_source|. |error_cb| is
22 // fired any time DataSource::Read() returns an error.
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();
29 // Aborts any pending reads by returning a read error. After this method
30 // returns all subsequent calls to Read() will immediately fail.
33 // FFmpegURLProtocol implementation.
34 int Read(int size
, uint8
* data
) override
;
35 bool GetPosition(int64
* position_out
) override
;
36 bool SetPosition(int64 position
) override
;
37 bool GetSize(int64
* size_out
) override
;
38 bool IsStreaming() override
;
41 // Sets |last_read_bytes_| and signals the blocked thread that the read
43 void SignalReadCompleted(int size
);
45 DataSource
* data_source_
;
46 base::Closure error_cb_
;
48 // Used to unblock the thread during shutdown and when reads complete.
49 base::WaitableEvent aborted_
;
50 base::WaitableEvent read_complete_
;
52 // Cached number of bytes last read from the data source.
55 // Cached position within the data source.
58 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockingUrlProtocol
);
63 #endif // MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_