Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / media / blink / buffered_data_source.h
blobb047c19a52e76da4261988d61ee04ac97f77dbad
1 // Copyright 2013 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_BLINK_BUFFERED_DATA_SOURCE_H_
6 #define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "media/base/data_source.h"
16 #include "media/base/media_export.h"
17 #include "media/base/ranges.h"
18 #include "media/blink/buffered_resource_loader.h"
19 #include "url/gurl.h"
21 namespace base {
22 class SingleThreadTaskRunner;
25 namespace media {
26 class MediaLog;
28 class MEDIA_EXPORT BufferedDataSourceHost {
29 public:
30 // Notify the host of the total size of the media file.
31 virtual void SetTotalBytes(int64 total_bytes) = 0;
33 // Notify the host that byte range [start,end] has been buffered.
34 // TODO(fischman): remove this method when demuxing is push-based instead of
35 // pull-based. http://crbug.com/131444
36 virtual void AddBufferedByteRange(int64 start, int64 end) = 0;
38 protected:
39 virtual ~BufferedDataSourceHost() {}
42 // A data source capable of loading URLs and buffering the data using an
43 // in-memory sliding window.
45 // BufferedDataSource must be created and destroyed on the thread associated
46 // with the |task_runner| passed in the constructor.
47 class MEDIA_EXPORT BufferedDataSource : public DataSource {
48 public:
49 // Used to specify video preload states. They are "hints" to the browser about
50 // how aggressively the browser should load and buffer data.
51 // Please see the HTML5 spec for the descriptions of these values:
52 // http://www.w3.org/TR/html5/video.html#attr-media-preload
54 // Enum values must match the values in blink::WebMediaPlayer::Preload and
55 // there will be assertions at compile time if they do not match.
56 enum Preload {
57 NONE,
58 METADATA,
59 AUTO,
61 typedef base::Callback<void(bool)> DownloadingCB;
63 // |url| and |cors_mode| are passed to the object. Buffered byte range changes
64 // will be reported to |host|. |downloading_cb| will be called whenever the
65 // downloading/paused state of the source changes.
66 BufferedDataSource(
67 const GURL& url,
68 BufferedResourceLoader::CORSMode cors_mode,
69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
70 blink::WebFrame* frame,
71 MediaLog* media_log,
72 BufferedDataSourceHost* host,
73 const DownloadingCB& downloading_cb);
74 ~BufferedDataSource() override;
76 // Executes |init_cb| with the result of initialization when it has completed.
78 // Method called on the render thread.
79 typedef base::Callback<void(bool)> InitializeCB;
80 void Initialize(const InitializeCB& init_cb);
82 // Adjusts the buffering algorithm based on the given preload value.
83 void SetPreload(Preload preload);
85 // Returns true if the media resource has a single origin, false otherwise.
86 // Only valid to call after Initialize() has completed.
88 // Method called on the render thread.
89 bool HasSingleOrigin();
91 // Returns true if the media resource passed a CORS access control check.
92 bool DidPassCORSAccessCheck() const;
94 // Cancels initialization, any pending loaders, and any pending read calls
95 // from the demuxer. The caller is expected to release its reference to this
96 // object and never call it again.
98 // Method called on the render thread.
99 void Abort();
101 // Notifies changes in playback state for controlling media buffering
102 // behavior.
103 void MediaPlaybackRateChanged(float playback_rate);
104 void MediaIsPlaying();
105 void MediaIsPaused();
107 // Returns true if the resource is local.
108 bool assume_fully_buffered() { return !url_.SchemeIsHTTPOrHTTPS(); }
110 // DataSource implementation.
111 // Called from demuxer thread.
112 void Stop() override;
114 void Read(int64 position,
115 int size,
116 uint8* data,
117 const DataSource::ReadCB& read_cb) override;
118 bool GetSize(int64* size_out) override;
119 bool IsStreaming() override;
120 void SetBitrate(int bitrate) override;
122 protected:
123 // A factory method to create a BufferedResourceLoader based on the read
124 // parameters. We can override this file to object a mock
125 // BufferedResourceLoader for testing.
126 virtual BufferedResourceLoader* CreateResourceLoader(
127 int64 first_byte_position, int64 last_byte_position);
129 private:
130 friend class BufferedDataSourceTest;
132 // Task posted to perform actual reading on the render thread.
133 void ReadTask();
135 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
136 // from any thread.
137 void StopInternal_Locked();
139 // Stops |loader_| if present. Used by Abort() and Stop().
140 void StopLoader();
142 // Tells |loader_| the bitrate of the media.
143 void SetBitrateTask(int bitrate);
145 // The method that performs actual read. This method can only be executed on
146 // the render thread.
147 void ReadInternal();
149 // BufferedResourceLoader::Start() callback for initial load.
150 void StartCallback(BufferedResourceLoader::Status status);
152 // BufferedResourceLoader::Start() callback for subsequent loads (i.e.,
153 // when accessing ranges that are outside initial buffered region).
154 void PartialReadStartCallback(BufferedResourceLoader::Status status);
156 // BufferedResourceLoader callbacks.
157 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read);
158 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state);
159 void ProgressCallback(int64 position);
161 // Update |loader_|'s deferring strategy in response to a play/pause, or
162 // change in playback rate.
163 void UpdateDeferStrategy(bool paused);
165 // URL of the resource requested.
166 GURL url_;
167 // crossorigin attribute on the corresponding HTML media element, if any.
168 BufferedResourceLoader::CORSMode cors_mode_;
170 // The total size of the resource. Set during StartCallback() if the size is
171 // known, otherwise it will remain kPositionNotSpecified until the size is
172 // determined by reaching EOF.
173 int64 total_bytes_;
175 // This value will be true if this data source can only support streaming.
176 // i.e. range request is not supported.
177 bool streaming_;
179 // A webframe for loading.
180 blink::WebFrame* frame_;
182 // A resource loader for the media resource.
183 scoped_ptr<BufferedResourceLoader> loader_;
185 // Callback method from the pipeline for initialization.
186 InitializeCB init_cb_;
188 // Read parameters received from the Read() method call. Must be accessed
189 // under |lock_|.
190 class ReadOperation;
191 scoped_ptr<ReadOperation> read_op_;
193 // This buffer is intermediate, we use it for BufferedResourceLoader to write
194 // to. And when read in BufferedResourceLoader is done, we copy data from
195 // this buffer to |read_buffer_|. The reason for an additional copy is that
196 // we don't own |read_buffer_|. But since the read operation is asynchronous,
197 // |read_buffer| can be destroyed at any time, so we only copy into
198 // |read_buffer| in the final step when it is safe.
199 // Memory is allocated for this member during initialization of this object
200 // because we want buffer to be passed into BufferedResourceLoader to be
201 // always non-null. And by initializing this member with a default size we can
202 // avoid creating zero-sized buffered if the first read has zero size.
203 std::vector<uint8> intermediate_read_buffer_;
205 // The task runner of the render thread.
206 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_;
208 // Protects |stop_signal_received_| and |read_op_|.
209 base::Lock lock_;
211 // Whether we've been told to stop via Abort() or Stop().
212 bool stop_signal_received_;
214 // This variable is true when the user has requested the video to play at
215 // least once.
216 bool media_has_played_;
218 // This variable holds the value of the preload attribute for the video
219 // element.
220 Preload preload_;
222 // Bitrate of the content, 0 if unknown.
223 int bitrate_;
225 // Current playback rate.
226 float playback_rate_;
228 scoped_refptr<MediaLog> media_log_;
230 // Host object to report buffered byte range changes to.
231 BufferedDataSourceHost* host_;
233 DownloadingCB downloading_cb_;
235 // Disallow rebinding WeakReference ownership to a different thread by keeping
236 // a persistent reference. This avoids problems with the thread-safety of
237 // reaching into this class from multiple threads to attain a WeakPtr.
238 base::WeakPtr<BufferedDataSource> weak_ptr_;
239 base::WeakPtrFactory<BufferedDataSource> weak_factory_;
241 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource);
244 } // namespace media
246 #endif // MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_