1 // Copyright 2012 The Chromium Authors
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_
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/synchronization/lock.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "media/filters/ffmpeg_glue.h"
20 // An implementation of FFmpegURLProtocol that blocks until the underlying
21 // asynchronous DataSource::Read() operation completes. Generally constructed on
22 // the media thread and used by ffmpeg through the AVIO interface from a
23 // sequenced blocking pool.
24 class MEDIA_EXPORT BlockingUrlProtocol : public FFmpegURLProtocol {
26 BlockingUrlProtocol() = delete;
28 // Implements FFmpegURLProtocol using the given |data_source|. |error_cb| is
29 // fired any time DataSource::Read() returns an error.
30 BlockingUrlProtocol(DataSource* data_source,
31 const base::RepeatingClosure& error_cb);
33 BlockingUrlProtocol(const BlockingUrlProtocol&) = delete;
34 BlockingUrlProtocol& operator=(const BlockingUrlProtocol&) = delete;
36 virtual ~BlockingUrlProtocol();
38 // Aborts any pending reads by returning a read error. After this method
39 // returns all subsequent calls to Read() will immediately fail. May be called
40 // from any thread and upon return ensures no further use of |data_source_|.
43 // FFmpegURLProtocol implementation.
44 int Read(int size, uint8_t* data) override;
45 bool GetPosition(int64_t* position_out) override;
46 bool SetPosition(int64_t position) override;
47 bool GetSize(int64_t* size_out) override;
48 bool IsStreaming() override;
51 // Sets |last_read_bytes_| and signals the blocked thread that the read
53 void SignalReadCompleted(int size);
55 // |data_source_lock_| allows Abort() to be called from any thread and stop
56 // all outstanding access to |data_source_|. Typically Abort() is called from
57 // the media thread while ffmpeg is operating on another thread.
58 base::Lock data_source_lock_;
59 raw_ptr<DataSource> data_source_;
61 base::RepeatingClosure error_cb_;
62 const bool is_streaming_;
64 // Used to unblock the thread during shutdown and when reads complete.
65 base::WaitableEvent aborted_;
66 base::WaitableEvent read_complete_;
68 // Cached number of bytes last read from the data source.
71 // Cached position within the data source.
72 int64_t read_position_;
77 #endif // MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_