Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/lock.h" | 11 #include "base/lock.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/timer.h" | 13 #include "base/timer.h" |
| 14 #include "base/condition_variable.h" | 14 #include "base/condition_variable.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "media/base/filters.h" | 16 #include "media/base/filters.h" |
| 17 #include "media/base/media_format.h" | 17 #include "media/base/media_format.h" |
| 18 #include "media/base/pipeline.h" | 18 #include "media/base/pipeline.h" |
| 19 #include "media/base/seekable_buffer.h" | 19 #include "media/base/seekable_buffer.h" |
| 20 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
| 21 #include "net/base/file_stream.h" | 21 #include "net/base/file_stream.h" |
| 22 #include "webkit/glue/media/media_resource_loader_bridge_factory.h" | 22 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
|
scherkus (not reviewing)
2010/11/22 21:27:29
I'd also try to remove as many of these headers as
annacc
2010/11/24 21:34:44
Done for WebURLResponse. All others are explicitl
| |
| 23 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" | |
| 24 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoaderClient.h" | |
| 25 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | |
| 26 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" | |
| 23 #include "webkit/glue/media/web_data_source.h" | 27 #include "webkit/glue/media/web_data_source.h" |
| 24 #include "webkit/glue/webmediaplayer_impl.h" | 28 #include "webkit/glue/webmediaplayer_impl.h" |
| 25 | 29 |
| 30 using WebKit::WebFrame; | |
|
scherkus (not reviewing)
2010/11/22 21:27:29
I don't think you need this as it seems you always
annacc
2010/11/24 21:34:44
Done.
| |
| 31 | |
| 26 namespace webkit_glue { | 32 namespace webkit_glue { |
| 33 | |
| 27 ///////////////////////////////////////////////////////////////////////////// | 34 ///////////////////////////////////////////////////////////////////////////// |
| 28 // BufferedResourceLoader | 35 // BufferedResourceLoader |
| 29 // This class works inside demuxer thread and render thread. It contains a | 36 // This class works inside demuxer thread and render thread. It contains a |
| 30 // resource loader bridge and does the actual resource loading. This object | 37 // WebUrlLoader and does the actual resource loading. This object does |
| 31 // does buffering internally, it defers the resource loading if buffer is | 38 // buffering internally, it defers the resource loading if buffer is full |
| 32 // full and un-defers the resource loading if it is under buffered. | 39 // and un-defers the resource loading if it is under buffered. |
| 33 class BufferedResourceLoader : | 40 class BufferedResourceLoader : |
| 34 public base::RefCountedThreadSafe<BufferedResourceLoader>, | 41 public base::RefCountedThreadSafe<BufferedResourceLoader>, |
| 35 public webkit_glue::ResourceLoaderBridge::Peer { | 42 public WebKit::WebURLLoaderClient { |
| 36 public: | 43 public: |
| 37 typedef Callback0::Type NetworkEventCallback; | 44 typedef Callback0::Type NetworkEventCallback; |
| 38 | 45 |
| 39 // |bridge_factory| - Factory to create a ResourceLoaderBridge. | |
| 40 // |url| - URL for the resource to be loaded. | 46 // |url| - URL for the resource to be loaded. |
| 41 // |first_byte_position| - First byte to start loading from, -1 for not | 47 // |first_byte_position| - First byte to start loading from, -1 for not |
| 42 // specified. | 48 // specified. |
| 43 // |last_byte_position| - Last byte to be loaded, -1 for not specified. | 49 // |last_byte_position| - Last byte to be loaded, -1 for not specified. |
| 44 BufferedResourceLoader( | 50 BufferedResourceLoader( |
| 45 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory, | 51 const GURL& url, |
|
scherkus (not reviewing)
2010/11/22 21:27:29
the first param should be on same line as function
annacc
2010/11/24 21:34:44
Done.
| |
| 46 const GURL& url, | 52 int64 first_byte_position, |
| 47 int64 first_byte_position, | 53 int64 last_byte_position); |
| 48 int64 last_byte_position); | |
| 49 | 54 |
| 50 // Start the resource loading with the specified URL and range. | 55 // Start the resource loading with the specified URL and range. |
| 51 // This method operates in asynchronous mode. Once there's a response from the | 56 // This method operates in asynchronous mode. Once there's a response from the |
| 52 // server, success or fail |callback| is called with the result. | 57 // server, success or fail |callback| is called with the result. |
| 53 // |callback| is called with the following values: | 58 // |callback| is called with the following values: |
| 54 // - net::OK | 59 // - net::OK |
| 55 // The request has started successfully. | 60 // The request has started successfully. |
| 56 // - net::ERR_FAILED | 61 // - net::ERR_FAILED |
| 57 // The request has failed because of an error with the network. | 62 // The request has failed because of an error with the network. |
| 58 // - net::ERR_INVALID_RESPONSE | 63 // - net::ERR_INVALID_RESPONSE |
| 59 // An invalid response is received from the server. | 64 // An invalid response is received from the server. |
| 60 // - (Anything else) | 65 // - (Anything else) |
| 61 // An error code that indicates the request has failed. | 66 // An error code that indicates the request has failed. |
| 62 // |event_callback| is called when the response is completed, data is | 67 // |event_callback| is called when the response is completed, data is |
| 63 // received, the request is suspended or resumed. | 68 // received, the request is suspended or resumed. |
| 64 virtual void Start(net::CompletionCallback* callback, | 69 virtual void Start(net::CompletionCallback* callback, |
| 65 NetworkEventCallback* event_callback); | 70 NetworkEventCallback* event_callback, |
| 71 WebKit::WebFrame* frame); | |
| 66 | 72 |
| 67 // Stop this loader, cancels and request and release internal buffer. | 73 // Stop this loader, cancels and request and release internal buffer. |
| 68 virtual void Stop(); | 74 virtual void Stop(); |
| 69 | 75 |
| 70 // Reads the specified |read_size| from |position| into |buffer| and when | 76 // Reads the specified |read_size| from |position| into |buffer| and when |
| 71 // the operation is done invoke |callback| with number of bytes read or an | 77 // the operation is done invoke |callback| with number of bytes read or an |
| 72 // error code. | 78 // error code. |
| 73 // |callback| is called with the following values: | 79 // |callback| is called with the following values: |
| 74 // - (Anything greater than or equal 0) | 80 // - (Anything greater than or equal 0) |
| 75 // Read was successful with the indicated number of bytes read. | 81 // Read was successful with the indicated number of bytes read. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 102 // Returns true if the response for this loader is a partial response. | 108 // Returns true if the response for this loader is a partial response. |
| 103 // It means a 206 response in HTTP/HTTPS protocol. | 109 // It means a 206 response in HTTP/HTTPS protocol. |
| 104 virtual bool partial_response() { return partial_response_; } | 110 virtual bool partial_response() { return partial_response_; } |
| 105 | 111 |
| 106 // Returns true if network is currently active. | 112 // Returns true if network is currently active. |
| 107 virtual bool network_activity() { return !completed_ && !deferred_; } | 113 virtual bool network_activity() { return !completed_ && !deferred_; } |
| 108 | 114 |
| 109 // Returns resulting URL. | 115 // Returns resulting URL. |
| 110 virtual const GURL& url() { return url_; } | 116 virtual const GURL& url() { return url_; } |
| 111 | 117 |
| 118 // Used to inject a mock used for unittests. | |
| 119 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); | |
| 120 | |
| 112 ///////////////////////////////////////////////////////////////////////////// | 121 ///////////////////////////////////////////////////////////////////////////// |
| 113 // webkit_glue::ResourceLoaderBridge::Peer implementations. | 122 // WebKit::WebURLLoaderClient implementations. |
| 114 virtual void OnUploadProgress(uint64 position, uint64 size) {} | 123 virtual void willSendRequest( |
| 115 virtual bool OnReceivedRedirect( | 124 WebKit::WebURLLoader* loader, |
| 116 const GURL& new_url, | 125 WebKit::WebURLRequest& newRequest, |
| 117 const webkit_glue::ResourceResponseInfo& info, | 126 const WebKit::WebURLResponse& redirectResponse); |
| 118 bool* has_new_first_party_for_cookies, | 127 virtual void didSendData( |
| 119 GURL* new_first_party_for_cookies); | 128 WebKit::WebURLLoader* loader, |
| 120 virtual void OnReceivedResponse( | 129 unsigned long long bytesSent, |
| 121 const webkit_glue::ResourceResponseInfo& info, | 130 unsigned long long totalBytesToBeSent); |
| 122 bool content_filtered); | 131 virtual void didReceiveResponse( |
| 123 virtual void OnDownloadedData(int len) {} | 132 WebKit::WebURLLoader* loader, |
| 124 virtual void OnReceivedData(const char* data, int len); | 133 const WebKit::WebURLResponse& response); |
| 125 virtual void OnCompletedRequest( | 134 virtual void didDownloadData( |
| 126 const URLRequestStatus& status, | 135 WebKit::WebURLLoader* loader, |
| 127 const std::string& security_info, | 136 int dataLength); |
| 128 const base::Time& completion_time); | 137 virtual void didReceiveData( |
| 138 WebKit::WebURLLoader* loader, | |
| 139 const char* data, | |
| 140 int dataLength); | |
| 141 virtual void didReceiveCachedMetadata( | |
| 142 WebKit::WebURLLoader* loader, | |
| 143 const char* data, int dataLength); | |
| 144 virtual void didFinishLoading( | |
| 145 WebKit::WebURLLoader* loader, | |
| 146 double finishTime); | |
| 147 virtual void didFail( | |
| 148 WebKit::WebURLLoader* loader, | |
| 149 const WebKit::WebURLError&); | |
| 129 | 150 |
| 130 protected: | 151 protected: |
| 131 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | 152 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; |
| 132 | 153 |
| 133 virtual ~BufferedResourceLoader(); | 154 virtual ~BufferedResourceLoader(); |
| 134 | 155 |
| 135 private: | 156 private: |
| 136 friend class BufferedResourceLoaderTest; | 157 friend class BufferedResourceLoaderTest; |
| 137 | 158 |
| 138 // Defer the resource loading if the buffer is full. | 159 // Defer the resource loading if the buffer is full. |
| 139 void EnableDeferIfNeeded(); | 160 void EnableDeferIfNeeded(); |
| 140 | 161 |
| 141 // Disable defer loading if we are under-buffered. | 162 // Disable defer loading if we are under-buffered. |
| 142 void DisableDeferIfNeeded(); | 163 void DisableDeferIfNeeded(); |
| 143 | 164 |
| 144 // Returns true if the current read request can be fulfilled by what is in | 165 // Returns true if the current read request can be fulfilled by what is in |
| 145 // the buffer. | 166 // the buffer. |
| 146 bool CanFulfillRead(); | 167 bool CanFulfillRead(); |
| 147 | 168 |
| 148 // Returns true if the current read request will be fulfilled in the future. | 169 // Returns true if the current read request will be fulfilled in the future. |
| 149 bool WillFulfillRead(); | 170 bool WillFulfillRead(); |
| 150 | 171 |
| 151 // Method that does the actual read and calls the |read_callbac_|, assuming | 172 // Method that does the actual read and calls the |read_callbac_|, assuming |
| 152 // the request range is in |buffer_|. | 173 // the request range is in |buffer_|. |
| 153 void ReadInternal(); | 174 void ReadInternal(); |
| 154 | 175 |
| 155 // If we have made a range request, verify the response from the server. | 176 // If we have made a range request, verify the response from the server. |
| 156 bool VerifyPartialResponse(const ResourceResponseInfo& info); | 177 bool VerifyPartialResponse(const WebKit::WebURLResponse& response); |
| 178 | |
| 179 // Returns the value for a range request header using parameters | |
| 180 // |first_byte_position| and |last_byte_position|. Negative numbers other | |
| 181 // than -1 are not allowed for |first_byte_position| and |last_byte_position|. | |
| 182 // |first_byte_position| should always be less than or equal to | |
| 183 // |last_byte_position| if they are both not -1. | |
| 184 // Empty string is returned on invalid parameters. | |
| 185 std::string GenerateHeaders(int64 first_byte_position, | |
| 186 int64 last_byte_position); | |
| 157 | 187 |
| 158 // Done with read. Invokes the read callback and reset parameters for the | 188 // Done with read. Invokes the read callback and reset parameters for the |
| 159 // read request. | 189 // read request. |
| 160 void DoneRead(int error); | 190 void DoneRead(int error); |
| 161 | 191 |
| 162 // Done with start. Invokes the start callback and reset it. | 192 // Done with start. Invokes the start callback and reset it. |
| 163 void DoneStart(int error); | 193 void DoneStart(int error); |
| 164 | 194 |
| 165 // Calls |event_callback_| in terms of a network event. | 195 // Calls |event_callback_| in terms of a network event. |
| 166 void NotifyNetworkEvent(); | 196 void NotifyNetworkEvent(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 178 | 208 |
| 179 // True if resource loading has completed. | 209 // True if resource loading has completed. |
| 180 bool completed_; | 210 bool completed_; |
| 181 | 211 |
| 182 // True if a range request was made. | 212 // True if a range request was made. |
| 183 bool range_requested_; | 213 bool range_requested_; |
| 184 | 214 |
| 185 // True if response data received is a partial range. | 215 // True if response data received is a partial range. |
| 186 bool partial_response_; | 216 bool partial_response_; |
| 187 | 217 |
| 188 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; | 218 // Does the work of loading and sends data back to this client. |
| 219 scoped_ptr<WebKit::WebURLLoader> url_loader_; | |
| 220 | |
| 189 GURL url_; | 221 GURL url_; |
| 190 int64 first_byte_position_; | 222 int64 first_byte_position_; |
| 191 int64 last_byte_position_; | 223 int64 last_byte_position_; |
| 192 | 224 |
| 193 // Callback method that listens to network events. | 225 // Callback method that listens to network events. |
| 194 scoped_ptr<NetworkEventCallback> event_callback_; | 226 scoped_ptr<NetworkEventCallback> event_callback_; |
| 195 | 227 |
| 196 // Members used during request start. | 228 // Members used during request start. |
| 197 scoped_ptr<net::CompletionCallback> start_callback_; | 229 scoped_ptr<net::CompletionCallback> start_callback_; |
| 198 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; | |
| 199 int64 offset_; | 230 int64 offset_; |
| 200 int64 content_length_; | 231 int64 content_length_; |
| 201 int64 instance_size_; | 232 int64 instance_size_; |
| 202 | 233 |
| 203 // Members used during a read operation. They should be reset after each | 234 // Members used during a read operation. They should be reset after each |
| 204 // read has completed or failed. | 235 // read has completed or failed. |
| 205 scoped_ptr<net::CompletionCallback> read_callback_; | 236 scoped_ptr<net::CompletionCallback> read_callback_; |
| 206 int64 read_position_; | 237 int64 read_position_; |
| 207 int read_size_; | 238 int read_size_; |
| 208 uint8* read_buffer_; | 239 uint8* read_buffer_; |
| 209 | 240 |
| 210 // Offsets of the requested first byte and last byte in |buffer_|. They are | 241 // Offsets of the requested first byte and last byte in |buffer_|. They are |
| 211 // written by VerifyRead(). | 242 // written by VerifyRead(). |
| 212 int first_offset_; | 243 int first_offset_; |
| 213 int last_offset_; | 244 int last_offset_; |
| 214 | 245 |
| 246 // Used to ensure mocks for unittests are used instead of reset in Start(). | |
| 247 bool keep_test_loader; | |
|
scherkus (not reviewing)
2010/11/22 21:27:29
member variables should end with _
annacc
2010/11/24 21:34:44
Done.
| |
| 248 | |
| 215 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | 249 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
| 216 }; | 250 }; |
| 217 | 251 |
| 218 class BufferedDataSource : public WebDataSource { | 252 class BufferedDataSource : public WebDataSource { |
| 219 public: | 253 public: |
| 220 BufferedDataSource( | 254 BufferedDataSource( |
| 221 MessageLoop* render_loop, | 255 MessageLoop* render_loop, |
|
scherkus (not reviewing)
2010/11/22 21:27:29
ditto on argument style here
annacc
2010/11/24 21:34:44
Done.
| |
| 222 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); | 256 WebKit::WebFrame* frame); |
| 223 | 257 |
| 224 virtual ~BufferedDataSource(); | 258 virtual ~BufferedDataSource(); |
| 225 | 259 |
| 226 // media::MediaFilter implementation. | 260 // media::MediaFilter implementation. |
| 227 virtual void Initialize(const std::string& url, | 261 virtual void Initialize(const std::string& url, |
| 228 media::FilterCallback* callback); | 262 media::FilterCallback* callback); |
| 229 virtual bool IsUrlSupported(const std::string& url); | 263 virtual bool IsUrlSupported(const std::string& url); |
| 230 virtual void Stop(media::FilterCallback* callback); | 264 virtual void Stop(media::FilterCallback* callback); |
| 231 virtual void SetPlaybackRate(float playback_rate); | 265 virtual void SetPlaybackRate(float playback_rate); |
| 232 | 266 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 // need to protect it. | 365 // need to protect it. |
| 332 int64 total_bytes_; | 366 int64 total_bytes_; |
| 333 | 367 |
| 334 // True if this data source is considered loaded. | 368 // True if this data source is considered loaded. |
| 335 bool loaded_; | 369 bool loaded_; |
| 336 | 370 |
| 337 // This value will be true if this data source can only support streaming. | 371 // This value will be true if this data source can only support streaming. |
| 338 // i.e. range request is not supported. | 372 // i.e. range request is not supported. |
| 339 bool streaming_; | 373 bool streaming_; |
| 340 | 374 |
| 375 // A webframe for loading. | |
| 376 WebKit::WebFrame* frame_; | |
| 377 | |
| 341 // True if the media resource has a single origin. | 378 // True if the media resource has a single origin. |
| 342 bool single_origin_; | 379 bool single_origin_; |
| 343 | 380 |
| 344 // A factory object to produce ResourceLoaderBridge. | |
| 345 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_; | |
| 346 | |
| 347 // A resource loader for the media resource. | 381 // A resource loader for the media resource. |
| 348 scoped_refptr<BufferedResourceLoader> loader_; | 382 scoped_refptr<BufferedResourceLoader> loader_; |
| 349 | 383 |
| 350 // True if network is active. | 384 // True if network is active. |
| 351 bool network_activity_; | 385 bool network_activity_; |
| 352 | 386 |
| 353 // Callback method from the pipeline for initialization. | 387 // Callback method from the pipeline for initialization. |
| 354 scoped_ptr<media::FilterCallback> initialize_callback_; | 388 scoped_ptr<media::FilterCallback> initialize_callback_; |
| 355 | 389 |
| 356 // Read parameters received from the Read() method call. | 390 // Read parameters received from the Read() method call. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 // Keeps track of whether we used a Range header in the initialization | 435 // Keeps track of whether we used a Range header in the initialization |
| 402 // request. | 436 // request. |
| 403 bool using_range_request_; | 437 bool using_range_request_; |
| 404 | 438 |
| 405 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 439 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 406 }; | 440 }; |
| 407 | 441 |
| 408 } // namespace webkit_glue | 442 } // namespace webkit_glue |
| 409 | 443 |
| 410 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ | 444 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| OLD | NEW |