[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / blocking_url_protocol.h
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.
4
5 #ifndef MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_
6 #define MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_
7
8 #include <stdint.h>
9
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"
15
16 namespace media {
17
18 class DataSource;
19
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 {
25  public:
26   BlockingUrlProtocol() = delete;
27
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);
32
33   BlockingUrlProtocol(const BlockingUrlProtocol&) = delete;
34   BlockingUrlProtocol& operator=(const BlockingUrlProtocol&) = delete;
35
36   virtual ~BlockingUrlProtocol();
37
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_|.
41   void Abort();
42
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;
49
50  private:
51   // Sets |last_read_bytes_| and signals the blocked thread that the read
52   // has completed.
53   void SignalReadCompleted(int size);
54
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_;
60
61   base::RepeatingClosure error_cb_;
62   const bool is_streaming_;
63
64   // Used to unblock the thread during shutdown and when reads complete.
65   base::WaitableEvent aborted_;
66   base::WaitableEvent read_complete_;
67
68   // Cached number of bytes last read from the data source.
69   int last_read_bytes_;
70
71   // Cached position within the data source.
72   int64_t read_position_;
73 };
74
75 }  // namespace media
76
77 #endif  // MEDIA_FILTERS_BLOCKING_URL_PROTOCOL_H_