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