Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: media/blink/multibuffer_data_source.h

Issue 1399603003: Tie multibuffers to URLs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@media_cache
Patch Set: added MEDIA_BLINK_EXPORT Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ 5 #ifndef MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_
6 #define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ 6 #define MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
15 #include "media/base/data_source.h" 16 #include "media/base/data_source.h"
16 #include "media/base/media_export.h"
17 #include "media/base/ranges.h" 17 #include "media/base/ranges.h"
18 #include "media/blink/buffered_resource_loader.h" 18 #include "media/blink/buffered_data_source.h"
19 #include "media/blink/media_blink_export.h"
20 #include "media/blink/resource_multibuffer.h"
19 #include "url/gurl.h" 21 #include "url/gurl.h"
20 22
21 namespace base { 23 namespace base {
22 class SingleThreadTaskRunner; 24 class SingleThreadTaskRunner;
23 } 25 }
24 26
25 namespace media { 27 namespace media {
26 class MediaLog; 28 class MediaLog;
27 29 class MultiBufferReader;
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;
32
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;
37
38 protected:
39 virtual ~BufferedDataSourceHost() {}
40 };
41 30
42 // A data source capable of loading URLs and buffering the data using an 31 // A data source capable of loading URLs and buffering the data using an
43 // in-memory sliding window. 32 // in-memory sliding window.
44 // 33 //
45 // BufferedDataSource must be created and destroyed on the thread associated 34 // MultibufferDataSource must be created and destroyed on the thread associated
46 // with the |task_runner| passed in the constructor. 35 // with the |task_runner| passed in the constructor.
47 class MEDIA_EXPORT BufferedDataSource : public DataSource { 36 class MEDIA_BLINK_EXPORT MultibufferDataSource
37 : NON_EXPORTED_BASE(public BufferedDataSourceInterface) {
48 public: 38 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
53 //
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,
60 };
61 typedef base::Callback<void(bool)> DownloadingCB; 39 typedef base::Callback<void(bool)> DownloadingCB;
62 40
63 // |url| and |cors_mode| are passed to the object. Buffered byte range changes 41 // |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 42 // will be reported to |host|. |downloading_cb| will be called whenever the
65 // downloading/paused state of the source changes. 43 // downloading/paused state of the source changes.
66 BufferedDataSource( 44 MultibufferDataSource(
67 const GURL& url, 45 const GURL& url,
68 BufferedResourceLoader::CORSMode cors_mode, 46 UrlData::CORSMode cors_mode,
69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
48 linked_ptr<ResourceMultiBuffer> multibuffer,
70 blink::WebFrame* frame, 49 blink::WebFrame* frame,
71 MediaLog* media_log, 50 MediaLog* media_log,
72 BufferedDataSourceHost* host, 51 BufferedDataSourceHost* host,
73 const DownloadingCB& downloading_cb); 52 const DownloadingCB& downloading_cb);
74 ~BufferedDataSource() override; 53 ~MultibufferDataSource() override;
75 54
76 // Executes |init_cb| with the result of initialization when it has completed. 55 // Executes |init_cb| with the result of initialization when it has completed.
77 // 56 //
78 // Method called on the render thread. 57 // Method called on the render thread.
79 typedef base::Callback<void(bool)> InitializeCB; 58 void Initialize(const InitializeCB& init_cb) override;
80 void Initialize(const InitializeCB& init_cb);
81 59
82 // Adjusts the buffering algorithm based on the given preload value. 60 // Adjusts the buffering algorithm based on the given preload value.
83 void SetPreload(Preload preload); 61 void SetPreload(Preload preload) override;
84 62
85 // Returns true if the media resource has a single origin, false otherwise. 63 // Returns true if the media resource has a single origin, false otherwise.
86 // Only valid to call after Initialize() has completed. 64 // Only valid to call after Initialize() has completed.
87 // 65 //
88 // Method called on the render thread. 66 // Method called on the render thread.
89 bool HasSingleOrigin(); 67 bool HasSingleOrigin() override;
90 68
91 // Returns true if the media resource passed a CORS access control check. 69 // Returns true if the media resource passed a CORS access control check.
92 bool DidPassCORSAccessCheck() const; 70 bool DidPassCORSAccessCheck() const override;
93 71
94 // Cancels initialization, any pending loaders, and any pending read calls 72 // Cancels initialization, any pending loaders, and any pending read calls
95 // from the demuxer. The caller is expected to release its reference to this 73 // from the demuxer. The caller is expected to release its reference to this
96 // object and never call it again. 74 // object and never call it again.
97 // 75 //
98 // Method called on the render thread. 76 // Method called on the render thread.
99 void Abort(); 77 void Abort() override;
100 78
101 // Notifies changes in playback state for controlling media buffering 79 // Notifies changes in playback state for controlling media buffering
102 // behavior. 80 // behavior.
103 void MediaPlaybackRateChanged(double playback_rate); 81 void MediaPlaybackRateChanged(double playback_rate) override;
104 void MediaIsPlaying(); 82 void MediaIsPlaying() override;
105 void MediaIsPaused(); 83 void MediaIsPaused() override;
106 bool media_has_played() const { return media_has_played_; } 84 bool media_has_played() const override;
107 85
108 // Returns true if the resource is local. 86 // Returns true if the resource is local.
109 bool assume_fully_buffered() { return !url_.SchemeIsHTTPOrHTTPS(); } 87 bool assume_fully_buffered() override;
110 88
111 // Cancels any open network connections once reaching the deferred state for 89 // Cancels any open network connections once reaching the deferred state for
112 // preload=metadata, non-streaming resources that have not started playback. 90 // preload=metadata, non-streaming resources that have not started playback.
113 // If already deferred, connections will be immediately closed. 91 // If already deferred, connections will be immediately closed.
114 void OnBufferingHaveEnough(); 92 void OnBufferingHaveEnough() override;
115 93
116 // DataSource implementation. 94 // DataSource implementation.
117 // Called from demuxer thread. 95 // Called from demuxer thread.
118 void Stop() override; 96 void Stop() override;
119 97
120 void Read(int64 position, 98 void Read(int64 position,
121 int size, 99 int size,
122 uint8* data, 100 uint8* data,
123 const DataSource::ReadCB& read_cb) override; 101 const DataSource::ReadCB& read_cb) override;
124 bool GetSize(int64* size_out) override; 102 bool GetSize(int64* size_out) override;
125 bool IsStreaming() override; 103 bool IsStreaming() override;
126 void SetBitrate(int bitrate) override; 104 void SetBitrate(int bitrate) override;
127 105
128 protected: 106 protected:
129 // A factory method to create a BufferedResourceLoader based on the read 107 // A factory method to create a BufferedResourceLoader based on the read
130 // parameters. We can override this file to object a mock 108 // parameters.
131 // BufferedResourceLoader for testing. 109 void CreateResourceLoader(int64 first_byte_position,
132 virtual BufferedResourceLoader* CreateResourceLoader( 110 int64 last_byte_position);
133 int64 first_byte_position, int64 last_byte_position);
134 111
135 private: 112 friend class MultibufferDataSourceTest;
136 friend class BufferedDataSourceTest;
137 113
138 // Task posted to perform actual reading on the render thread. 114 // Task posted to perform actual reading on the render thread.
139 void ReadTask(); 115 void ReadTask();
140 116
141 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call 117 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
142 // from any thread. 118 // from any thread.
143 void StopInternal_Locked(); 119 void StopInternal_Locked();
144 120
145 // Stops |loader_| if present. Used by Abort() and Stop(). 121 // Stops |reader_| if present. Used by Abort() and Stop().
146 void StopLoader(); 122 void StopLoader();
147 123
148 // Tells |loader_| the bitrate of the media. 124 // Tells |reader_| the bitrate of the media.
149 void SetBitrateTask(int bitrate); 125 void SetBitrateTask(int bitrate);
150 126
151 // The method that performs actual read. This method can only be executed on 127 // BufferedResourceLoader::Start() callback for initial load.
152 // the render thread. 128 void StartCallback();
153 void ReadInternal();
154 129
155 // BufferedResourceLoader::Start() callback for initial load. 130 // Check if we've moved to a new url and update has_signgle_origin_.
156 void StartCallback(BufferedResourceLoader::Status status); 131 void UpdateSingleOrigin();
157 132
158 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., 133 // MultiBufferReader progress callback.
159 // when accessing ranges that are outside initial buffered region). 134 void ProgressCallback(int64 begin, int64 end);
160 void PartialReadStartCallback(BufferedResourceLoader::Status status);
161 135
162 // Returns true if we can accept the new partial response. 136 // call downloading_cb_ if needed.
163 bool CheckPartialResponseURL(const GURL& partial_response_original_url) const; 137 void UpdateLoadingState();
164 138
165 // BufferedResourceLoader callbacks. 139 // Update |reader_|'s preload and buffer settings.
166 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); 140 void UpdateBufferSizes();
167 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state);
168 void ProgressCallback(int64 position);
169 141
170 // Update |loader_|'s deferring strategy in response to a play/pause, or 142 // crossorigin attribute on the corresponding HTML media element, if any.
171 // change in playback rate. 143 UrlData::CORSMode cors_mode_;
172 void UpdateDeferStrategy(bool paused);
173 144
174 // URL of the resource requested. 145 // URL of the resource requested.
175 GURL url_; 146 scoped_refptr<UrlData> url_data_;
176 // crossorigin attribute on the corresponding HTML media element, if any. 147 scoped_refptr<UrlData> destination_url_data_;
177 BufferedResourceLoader::CORSMode cors_mode_;
178 148
179 // The total size of the resource. Set during StartCallback() if the size is 149 // The total size of the resource. Set during StartCallback() if the size is
180 // known, otherwise it will remain kPositionNotSpecified until the size is 150 // known, otherwise it will remain kPositionNotSpecified until the size is
181 // determined by reaching EOF. 151 // determined by reaching EOF.
182 int64 total_bytes_; 152 int64 total_bytes_;
183 153
184 // This value will be true if this data source can only support streaming. 154 // This value will be true if this data source can only support streaming.
185 // i.e. range request is not supported. 155 // i.e. range request is not supported.
186 bool streaming_; 156 bool streaming_;
187 157
158 bool loading_;
159
160 // The task runner of the render thread.
161 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_;
162
163 // Shared buffer.
164 linked_ptr<ResourceMultiBuffer> multibuffer_;
165
188 // A webframe for loading. 166 // A webframe for loading.
189 blink::WebFrame* frame_; 167 blink::WebFrame* frame_;
190 168
191 // A resource loader for the media resource. 169 // A resource reader for the media resource.
192 scoped_ptr<BufferedResourceLoader> loader_; 170 scoped_ptr<MultiBufferReader> reader_;
193 171
194 // Callback method from the pipeline for initialization. 172 // Callback method from the pipeline for initialization.
195 InitializeCB init_cb_; 173 InitializeCB init_cb_;
196 174
197 // Read parameters received from the Read() method call. Must be accessed 175 // Read parameters received from the Read() method call. Must be accessed
198 // under |lock_|. 176 // under |lock_|.
199 class ReadOperation; 177 class ReadOperation;
200 scoped_ptr<ReadOperation> read_op_; 178 scoped_ptr<ReadOperation> read_op_;
201 179
202 // This buffer is intermediate, we use it for BufferedResourceLoader to write
203 // to. And when read in BufferedResourceLoader is done, we copy data from
204 // this buffer to |read_buffer_|. The reason for an additional copy is that
205 // we don't own |read_buffer_|. But since the read operation is asynchronous,
206 // |read_buffer| can be destroyed at any time, so we only copy into
207 // |read_buffer| in the final step when it is safe.
208 // Memory is allocated for this member during initialization of this object
209 // because we want buffer to be passed into BufferedResourceLoader to be
210 // always non-null. And by initializing this member with a default size we can
211 // avoid creating zero-sized buffered if the first read has zero size.
212 std::vector<uint8> intermediate_read_buffer_;
213
214 // The task runner of the render thread.
215 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_;
216
217 // Protects |stop_signal_received_| and |read_op_|. 180 // Protects |stop_signal_received_| and |read_op_|.
218 base::Lock lock_; 181 base::Lock lock_;
219 182
220 // Whether we've been told to stop via Abort() or Stop(). 183 // Whether we've been told to stop via Abort() or Stop().
221 bool stop_signal_received_; 184 bool stop_signal_received_;
222 185
223 // This variable is true when the user has requested the video to play at 186 // This variable is true when the user has requested the video to play at
224 // least once. 187 // least once.
225 bool media_has_played_; 188 bool media_has_played_;
226 189
190 // Are we currently paused.
191 bool paused_;
192
193 // As we follow redirects, we set this variable to false if redirects
194 // go between different origins.
195 bool single_origin_;
196
197 // Close the connection when we have enough data.
198 bool cancel_on_defer_;
199
227 // This variable holds the value of the preload attribute for the video 200 // This variable holds the value of the preload attribute for the video
228 // element. 201 // element.
229 Preload preload_; 202 Preload preload_;
230 203
231 // Bitrate of the content, 0 if unknown. 204 // Bitrate of the content, 0 if unknown.
232 int bitrate_; 205 int bitrate_;
233 206
234 // Current playback rate. 207 // Current playback rate.
235 double playback_rate_; 208 double playback_rate_;
236 209
237 scoped_refptr<MediaLog> media_log_; 210 scoped_refptr<MediaLog> media_log_;
238 211
239 // Host object to report buffered byte range changes to. 212 // Host object to report buffered byte range changes to.
240 BufferedDataSourceHost* host_; 213 BufferedDataSourceHost* host_;
241 214
242 DownloadingCB downloading_cb_; 215 DownloadingCB downloading_cb_;
243 216
244 // The original URL of the first response. If the request is redirected to 217 // The original URL of the first response. If the request is redirected to
245 // another URL it is the URL after redirected. If the response is generated in 218 // another URL it is the URL after redirected. If the response is generated in
246 // a Service Worker this URL is empty. BufferedDataSource checks the original 219 // a Service Worker this URL is empty. MultibufferDataSource checks the
247 // URL of each successive response. If the origin URL of it is different from 220 // original URL of each successive response. If the origin URL of it is
248 // the original URL of the first response, it is treated as an error. 221 // different from the original URL of the first response, it is treated
222 // as an error.
249 GURL response_original_url_; 223 GURL response_original_url_;
250 224
251 // Disallow rebinding WeakReference ownership to a different thread by keeping 225 // Disallow rebinding WeakReference ownership to a different thread by keeping
252 // a persistent reference. This avoids problems with the thread-safety of 226 // a persistent reference. This avoids problems with the thread-safety of
253 // reaching into this class from multiple threads to attain a WeakPtr. 227 // reaching into this class from multiple threads to attain a WeakPtr.
254 base::WeakPtr<BufferedDataSource> weak_ptr_; 228 base::WeakPtr<MultibufferDataSource> weak_ptr_;
255 base::WeakPtrFactory<BufferedDataSource> weak_factory_; 229 base::WeakPtrFactory<MultibufferDataSource> weak_factory_;
256 230
257 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); 231 DISALLOW_COPY_AND_ASSIGN(MultibufferDataSource);
258 }; 232 };
259 233
260 } // namespace media 234 } // namespace media
261 235
262 #endif // MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ 236 #endif // MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698