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

Side by Side Diff: webkit/glue/media/buffered_data_source.h

Issue 269002: Report stalled event correctly for <video> (Closed)
Patch Set: comments Created 11 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 (c) 2009 The Chromium Authors. All rights reserved. Use of this 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the 2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file. 3 // LICENSE file.
4 4
5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
6 #define WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 6 #define WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
7 7
8 #include <algorithm>
8 #include <string> 9 #include <string>
10 #include <vector>
9 11
10 #include "base/lock.h" 12 #include "base/lock.h"
11 #include "base/scoped_ptr.h" 13 #include "base/scoped_ptr.h"
12 #include "base/timer.h" 14 #include "base/timer.h"
13 #include "base/condition_variable.h" 15 #include "base/condition_variable.h"
14 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
15 #include "media/base/factory.h" 17 #include "media/base/factory.h"
16 #include "media/base/filters.h" 18 #include "media/base/filters.h"
17 #include "media/base/media_format.h" 19 #include "media/base/media_format.h"
18 #include "media/base/pipeline.h" 20 #include "media/base/pipeline.h"
19 #include "media/base/seekable_buffer.h" 21 #include "media/base/seekable_buffer.h"
20 #include "net/base/completion_callback.h" 22 #include "net/base/completion_callback.h"
21 #include "net/base/file_stream.h" 23 #include "net/base/file_stream.h"
22 #include "webkit/glue/media/media_resource_loader_bridge_factory.h" 24 #include "webkit/glue/media/media_resource_loader_bridge_factory.h"
23 25
24 namespace webkit_glue { 26 namespace webkit_glue {
25 27
26 ///////////////////////////////////////////////////////////////////////////// 28 /////////////////////////////////////////////////////////////////////////////
27 // BufferedResourceLoader 29 // BufferedResourceLoader
28 // This class works inside demuxer thread and render thread. It contains a 30 // This class works inside demuxer thread and render thread. It contains a
29 // resource loader bridge and does the actual resource loading. This object 31 // resource loader bridge and does the actual resource loading. This object
30 // does buffering internally, it defers the resource loading if buffer is 32 // does buffering internally, it defers the resource loading if buffer is
31 // full and un-defers the resource loading if it is under buffered. 33 // full and un-defers the resource loading if it is under buffered.
32 class BufferedResourceLoader : 34 class BufferedResourceLoader :
33 public base::RefCountedThreadSafe<BufferedResourceLoader>, 35 public base::RefCountedThreadSafe<BufferedResourceLoader>,
34 public webkit_glue::ResourceLoaderBridge::Peer { 36 public webkit_glue::ResourceLoaderBridge::Peer {
35 public: 37 public:
38 typedef Callback0::Type NetworkEventCallback;
39
36 // |bridge_factory| - Factory to create a ResourceLoaderBridge. 40 // |bridge_factory| - Factory to create a ResourceLoaderBridge.
37 // |url| - URL for the resource to be loaded. 41 // |url| - URL for the resource to be loaded.
38 // |first_byte_position| - First byte to start loading from, -1 for not 42 // |first_byte_position| - First byte to start loading from, -1 for not
39 // specified. 43 // specified.
40 // |last_byte_position| - Last byte to be loaded, -1 for not specified. 44 // |last_byte_position| - Last byte to be loaded, -1 for not specified.
41 BufferedResourceLoader( 45 BufferedResourceLoader(
42 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory, 46 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory,
43 const GURL& url, 47 const GURL& url,
44 int64 first_byte_position, 48 int64 first_byte_position,
45 int64 last_byte_position); 49 int64 last_byte_position);
46 virtual ~BufferedResourceLoader(); 50 virtual ~BufferedResourceLoader();
47 51
48 // Start the resource loading with the specified URL and range. 52 // Start the resource loading with the specified URL and range.
49 // This method operates in asynchronous mode. Once there's a response from the 53 // This method operates in asynchronous mode. Once there's a response from the
50 // server, success or fail |callback| is called with the result. 54 // server, success or fail |callback| is called with the result.
51 // |callback| is called with the following values: 55 // |callback| is called with the following values:
52 // - net::OK 56 // - net::OK
53 // The request has started successfully. 57 // The request has started successfully.
54 // - net::ERR_FAILED 58 // - net::ERR_FAILED
55 // The request has failed because of an error with the network. 59 // The request has failed because of an error with the network.
56 // - net::ERR_INVALID_RESPONSE 60 // - net::ERR_INVALID_RESPONSE
57 // An invalid response is received from the server. 61 // An invalid response is received from the server.
58 // - (Anything else) 62 // - (Anything else)
59 // An error code that indicates the request has failed. 63 // An error code that indicates the request has failed.
60 virtual void Start(net::CompletionCallback* callback); 64 // |event_callback| is called when the response is completed, data is
65 // received, the request is suspended or resumed.
66 virtual void Start(net::CompletionCallback* callback,
67 NetworkEventCallback* event_callback);
61 68
62 // Stop this loader, cancels and request and release internal buffer. 69 // Stop this loader, cancels and request and release internal buffer.
63 virtual void Stop(); 70 virtual void Stop();
64 71
65 // Reads the specified |read_size| from |position| into |buffer| and when 72 // Reads the specified |read_size| from |position| into |buffer| and when
66 // the operation is done invoke |callback| with number of bytes read or an 73 // the operation is done invoke |callback| with number of bytes read or an
67 // error code. 74 // error code.
68 // |callback| is called with the following values: 75 // |callback| is called with the following values:
69 // - (Anything greater than or equal 0) 76 // - (Anything greater than or equal 0)
70 // Read was successful with the indicated number of bytes read. 77 // Read was successful with the indicated number of bytes read.
71 // - net::ERR_FAILED 78 // - net::ERR_FAILED
72 // The read has failed because of an error with the network. 79 // The read has failed because of an error with the network.
73 // - net::ERR_CACHE_MISS 80 // - net::ERR_CACHE_MISS
74 // The read was made too far away from the current buffered position. 81 // The read was made too far away from the current buffered position.
75 virtual void Read(int64 position, int read_size, 82 virtual void Read(int64 position, int read_size,
76 uint8* buffer, net::CompletionCallback* callback); 83 uint8* buffer, net::CompletionCallback* callback);
77 84
85 // Returns the position of the first byte buffered. Returns -1 if such value
86 // is not available.
87 virtual int64 GetBufferedFirstBytePosition();
88
89 // Returns the position of the last byte buffered. Returns -1 if such value
90 // is not available.
91 virtual int64 GetBufferedLastBytePosition();
92
78 // Gets the content length in bytes of the instance after this loader has been 93 // Gets the content length in bytes of the instance after this loader has been
79 // started. If this value is -1, then content length is unknown. 94 // started. If this value is -1, then content length is unknown.
80 virtual int64 content_length() { return content_length_; } 95 virtual int64 content_length() { return content_length_; }
81 96
82 // Gets the original size of the file requested. If this value is -1, then 97 // Gets the original size of the file requested. If this value is -1, then
83 // the size is unknown. 98 // the size is unknown.
84 virtual int64 instance_size() { return instance_size_; } 99 virtual int64 instance_size() { return instance_size_; }
85 100
86 // Returns true if the response for this loader is a partial response. 101 // Returns true if the response for this loader is a partial response.
87 // It means a 206 response in HTTP/HTTPS protocol. 102 // It means a 206 response in HTTP/HTTPS protocol.
88 virtual bool partial_response() { return partial_response_; } 103 virtual bool partial_response() { return partial_response_; }
89 104
105 // Returns true if network is currently active.
106 virtual bool network_activity() { return !completed_ && !deferred_; }
107
90 ///////////////////////////////////////////////////////////////////////////// 108 /////////////////////////////////////////////////////////////////////////////
91 // webkit_glue::ResourceLoaderBridge::Peer implementations. 109 // webkit_glue::ResourceLoaderBridge::Peer implementations.
92 virtual void OnUploadProgress(uint64 position, uint64 size) {} 110 virtual void OnUploadProgress(uint64 position, uint64 size) {}
93 virtual bool OnReceivedRedirect( 111 virtual bool OnReceivedRedirect(
94 const GURL& new_url, 112 const GURL& new_url,
95 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info); 113 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info);
96 virtual void OnReceivedResponse( 114 virtual void OnReceivedResponse(
97 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info, 115 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info,
98 bool content_filtered); 116 bool content_filtered);
99 virtual void OnReceivedData(const char* data, int len); 117 virtual void OnReceivedData(const char* data, int len);
(...skipping 27 matching lines...) Expand all
127 // If we have made a range request, verify the response from the server. 145 // If we have made a range request, verify the response from the server.
128 bool VerifyPartialResponse(const ResourceLoaderBridge::ResponseInfo& info); 146 bool VerifyPartialResponse(const ResourceLoaderBridge::ResponseInfo& info);
129 147
130 // Done with read. Invokes the read callback and reset parameters for the 148 // Done with read. Invokes the read callback and reset parameters for the
131 // read request. 149 // read request.
132 void DoneRead(int error); 150 void DoneRead(int error);
133 151
134 // Done with start. Invokes the start callback and reset it. 152 // Done with start. Invokes the start callback and reset it.
135 void DoneStart(int error); 153 void DoneStart(int error);
136 154
155 // Calls |event_callback_| in terms of a network event.
156 void NotifyNetworkEvent();
157
137 bool HasPendingRead() { return read_callback_.get() != NULL; } 158 bool HasPendingRead() { return read_callback_.get() != NULL; }
138 159
139 // A sliding window of buffer. 160 // A sliding window of buffer.
140 scoped_ptr<media::SeekableBuffer> buffer_; 161 scoped_ptr<media::SeekableBuffer> buffer_;
141 162
142 // True if resource loading was deferred. 163 // True if resource loading was deferred.
143 bool deferred_; 164 bool deferred_;
144 165
145 // True if resource loading has completed. 166 // True if resource loading has completed.
146 bool completed_; 167 bool completed_;
147 168
148 // True if a range request was made. 169 // True if a range request was made.
149 bool range_requested_; 170 bool range_requested_;
150 171
151 // True if response data received is a partial range. 172 // True if response data received is a partial range.
152 bool partial_response_; 173 bool partial_response_;
153 174
154 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; 175 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_;
155 GURL url_; 176 GURL url_;
156 int64 first_byte_position_; 177 int64 first_byte_position_;
157 int64 last_byte_position_; 178 int64 last_byte_position_;
158 179
180 // Callback method that listens to network events.
181 scoped_ptr<NetworkEventCallback> event_callback_;
182
159 // Members used during request start. 183 // Members used during request start.
160 scoped_ptr<net::CompletionCallback> start_callback_; 184 scoped_ptr<net::CompletionCallback> start_callback_;
161 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; 185 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_;
162 int64 offset_; 186 int64 offset_;
163 int64 content_length_; 187 int64 content_length_;
164 int64 instance_size_; 188 int64 instance_size_;
165 189
166 // Members used during a read operation. They should be reset after each 190 // Members used during a read operation. They should be reset after each
167 // read has completed or failed. 191 // read has completed or failed.
168 scoped_ptr<net::CompletionCallback> read_callback_; 192 scoped_ptr<net::CompletionCallback> read_callback_;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 235
212 protected: 236 protected:
213 BufferedDataSource( 237 BufferedDataSource(
214 MessageLoop* render_loop, 238 MessageLoop* render_loop,
215 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); 239 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory);
216 virtual ~BufferedDataSource(); 240 virtual ~BufferedDataSource();
217 241
218 // A factory method to create a BufferedResourceLoader based on the read 242 // A factory method to create a BufferedResourceLoader based on the read
219 // parameters. We can override this file to object a mock 243 // parameters. We can override this file to object a mock
220 // BufferedResourceLoader for testing. 244 // BufferedResourceLoader for testing.
221 virtual BufferedResourceLoader* CreateLoader(int64 first_byte_position, 245 virtual BufferedResourceLoader* CreateResourceLoader(
222 int64 last_byte_position); 246 int64 first_byte_position, int64 last_byte_position);
223 247
224 // Gets the number of milliseconds to declare a request timeout since 248 // Gets the number of milliseconds to declare a request timeout since
225 // the request was made. This method is made virtual so as to inject a 249 // the request was made. This method is made virtual so as to inject a
226 // different number for testing purpose. 250 // different number for testing purpose.
227 virtual base::TimeDelta GetTimeoutMilliseconds(); 251 virtual base::TimeDelta GetTimeoutMilliseconds();
228 252
229 private: 253 private:
230 friend class media::FilterFactoryImpl2< 254 friend class media::FilterFactoryImpl2<
231 BufferedDataSource, 255 BufferedDataSource,
232 MessageLoop*, 256 MessageLoop*,
233 webkit_glue::MediaResourceLoaderBridgeFactory*>; 257 webkit_glue::MediaResourceLoaderBridgeFactory*>;
234 258
235 // Posted to perform initialization on render thread. 259 // Posted to perform initialization on render thread and start resource
260 // loading.
236 void InitializeTask(); 261 void InitializeTask();
237 262
238 // Task posted to perform resource loading and actual reading on the render 263 // Task posted to perform actual reading on the render thread.
239 // thread. 264 void ReadTask(int64 position, int read_size, uint8* read_buffer,
240 void ReadTask(int64 position, int read_size,
241 uint8* read_buffer,
242 media::DataSource::ReadCallback* read_callback); 265 media::DataSource::ReadCallback* read_callback);
243 266
244 // Task posted when Stop() is called. 267 // Task posted when Stop() is called.
245 void StopTask(); 268 void StopTask();
246 269
247 // Reset |loader_| with |loader| and starts it. This task is posted from 270 // Restart resource loading on render thread.
248 // callback method from the current buffered resource loader. 271 void RestartLoadingTask();
249 void SwapLoaderTask(scoped_refptr<BufferedResourceLoader> loader);
250 272
251 // This task monitors the current active read request. If the current read 273 // This task monitors the current active read request. If the current read
252 // request has timed out, this task will destroy the current loader and 274 // request has timed out, this task will destroy the current loader and
253 // creates a new to accomodate the read request. 275 // creates a new one to accomodate the read request.
254 void WatchDogTask(); 276 void WatchDogTask();
255 277
256 // The method that performs actual read. This method can only be executed on 278 // The method that performs actual read. This method can only be executed on
257 // the render thread. 279 // the render thread.
258 void ReadInternal(); 280 void ReadInternal();
259 281
260 // Calls |read_callback_| and reset all read parameters. 282 // Calls |read_callback_| and reset all read parameters.
261 void DoneRead(int error); 283 void DoneRead_Locked(int error);
262 284
263 // Calls |initialize_callback_| and reset it. 285 // Calls |initialize_callback_| and reset it.
264 void DoneInitialization(); 286 void DoneInitialization_Locked();
265 287
266 // Callback method for |loader_| if URL for the resource requested is using 288 // Callback method for |loader_| if URL for the resource requested is using
267 // HTTP protocol. This method is called when response for initial request is 289 // HTTP protocol. This method is called when response for initial request is
268 // received. 290 // received.
269 void HttpInitialStartCallback(int error); 291 void HttpInitialStartCallback(int error);
270 292
271 // Callback method for |loader_| if URL for the resource requested is using 293 // Callback method for |loader_| if URL for the resource requested is using
272 // a non-HTTP protocol, e.g. local files. This method is called when response 294 // a non-HTTP protocol, e.g. local files. This method is called when response
273 // for initial request is received. 295 // for initial request is received.
274 void NonHttpInitialStartCallback(int error); 296 void NonHttpInitialStartCallback(int error);
275 297
276 // Callback method to be passed to BufferedResourceLoader during range 298 // Callback method to be passed to BufferedResourceLoader during range
277 // request. Once a resource request has started, this method will be called 299 // request. Once a resource request has started, this method will be called
278 // with the error code. This method will be executed on the thread 300 // with the error code. This method will be executed on the thread
279 // BufferedResourceLoader lives, i.e. render thread. 301 // BufferedResourceLoader lives, i.e. render thread.
280 void PartialReadStartCallback(int error); 302 void PartialReadStartCallback(int error);
281 303
282 // Callback method for making a read request to BufferedResourceLoader. 304 // Callback method for making a read request to BufferedResourceLoader.
283 // If data arrives or the request has failed, this method is called with 305 // If data arrives or the request has failed, this method is called with
284 // the error code or the number of bytes read. 306 // the error code or the number of bytes read.
285 void ReadCallback(int error); 307 void ReadCallback(int error);
286 308
309 // Callback method when a network event is received.
310 void NetworkEventCallback();
311
287 media::MediaFormat media_format_; 312 media::MediaFormat media_format_;
288 313
289 // URL of the resource requested. 314 // URL of the resource requested.
290 GURL url_; 315 GURL url_;
291 316
292 // Members for total bytes of the requested object. It is written once on 317 // Members for total bytes of the requested object. It is written once on
293 // render thread but may be read from any thread. However reading of this 318 // render thread but may be read from any thread. However reading of this
294 // member is guaranteed to happen after it is first written, so we don't 319 // member is guaranteed to happen after it is first written, so we don't
295 // need to protect it. 320 // need to protect it.
296 int64 total_bytes_; 321 int64 total_bytes_;
297 322
323 // True if this data source is considered loaded.
324 bool loaded_;
325
298 // This value will be true if this data source can only support streaming. 326 // This value will be true if this data source can only support streaming.
299 // i.e. range request is not supported. 327 // i.e. range request is not supported.
300 bool streaming_; 328 bool streaming_;
301 329
302 // A factory object to produce ResourceLoaderBridge. 330 // A factory object to produce ResourceLoaderBridge.
303 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_; 331 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_;
304 332
305 // A resource loader for the media resource. 333 // A resource loader for the media resource.
306 scoped_refptr<BufferedResourceLoader> loader_; 334 scoped_refptr<BufferedResourceLoader> loader_;
307 335
336 // True if network is active.
337 bool network_activity_;
338
308 // Callback method from the pipeline for initialization. 339 // Callback method from the pipeline for initialization.
309 scoped_ptr<media::FilterCallback> initialize_callback_; 340 scoped_ptr<media::FilterCallback> initialize_callback_;
310 341
311 // Read parameters received from the Read() method call. 342 // Read parameters received from the Read() method call.
312 scoped_ptr<media::DataSource::ReadCallback> read_callback_; 343 scoped_ptr<media::DataSource::ReadCallback> read_callback_;
313 int64 read_position_; 344 int64 read_position_;
314 int read_size_; 345 int read_size_;
315 uint8* read_buffer_; 346 uint8* read_buffer_;
316 base::Time read_submitted_time_; 347 base::Time read_submitted_time_;
317 int read_attempts_; 348 int read_attempts_;
(...skipping 12 matching lines...) Expand all
330 int intermediate_read_buffer_size_; 361 int intermediate_read_buffer_size_;
331 362
332 // The message loop of the render thread. 363 // The message loop of the render thread.
333 MessageLoop* render_loop_; 364 MessageLoop* render_loop_;
334 365
335 // Protects |stopped_|. 366 // Protects |stopped_|.
336 Lock lock_; 367 Lock lock_;
337 368
338 // Stop signal to suppressing activities. This variable is set on the pipeline 369 // Stop signal to suppressing activities. This variable is set on the pipeline
339 // thread and read from the render thread. 370 // thread and read from the render thread.
340 bool stopped_; 371 bool stop_signal_received_;
341 372
342 // This variable is set by StopTask() and read from ReadTask(). It is used to 373 // This variable is set by StopTask() that indicates this object is stopped
343 // prevent ReadTask() from doing anything after StopTask() is executed. 374 // on the render thread.
344 bool stop_task_finished_; 375 bool stopped_on_render_loop_;
345 376
346 // This timer is to run the WatchDogTask repeatedly. We use a timer instead 377 // This timer is to run the WatchDogTask repeatedly. We use a timer instead
347 // of doing PostDelayedTask() reduce the extra reference held by the message 378 // of doing PostDelayedTask() reduce the extra reference held by the message
348 // loop. The RepeatingTimer does PostDelayedTask() internally, by using it 379 // loop. The RepeatingTimer does PostDelayedTask() internally, by using it
349 // the message loop doesn't hold a reference for the watch dog task. 380 // the message loop doesn't hold a reference for the watch dog task.
350 base::RepeatingTimer<BufferedDataSource> watch_dog_timer_; 381 base::RepeatingTimer<BufferedDataSource> watch_dog_timer_;
351 382
352 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); 383 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource);
353 }; 384 };
354 385
355 } // namespace webkit_glue 386 } // namespace webkit_glue
356 387
357 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 388 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698