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_
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"
22 class SingleThreadTaskRunner
;
28 class MEDIA_EXPORT BufferedDataSourceHost
{
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;
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
{
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.
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.
68 BufferedResourceLoader::CORSMode cors_mode
,
69 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
70 blink::WebFrame
* frame
,
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.
101 // Notifies changes in playback state for controlling media buffering
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
,
117 const DataSource::ReadCB
& read_cb
) override
;
118 bool GetSize(int64
* size_out
) override
;
119 bool IsStreaming() override
;
120 void SetBitrate(int bitrate
) override
;
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
);
130 friend class BufferedDataSourceTest
;
132 // Task posted to perform actual reading on the render thread.
135 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
137 void StopInternal_Locked();
139 // Stops |loader_| if present. Used by Abort() and Stop().
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.
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.
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.
175 // This value will be true if this data source can only support streaming.
176 // i.e. range request is not supported.
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
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_|.
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
216 bool media_has_played_
;
218 // This variable holds the value of the preload attribute for the video
222 // Bitrate of the content, 0 if unknown.
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
);
246 #endif // MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_