OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 5 #ifndef WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
6 #define WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 6 #define WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/timer.h" | 12 #include "base/timer.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "media/base/seekable_buffer.h" | 14 #include "media/base/seekable_buffer.h" |
15 #include "net/base/file_stream.h" | 15 #include "net/base/file_stream.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 20 #include "webkit/media/active_loader.h" |
20 #include "webkit/media/web_data_source.h" | 21 #include "webkit/media/web_data_source.h" |
21 #include "webkit/media/webmediaplayer_impl.h" | 22 #include "webkit/media/webmediaplayer_impl.h" |
22 | 23 |
23 namespace media { | 24 namespace media { |
24 class MediaLog; | 25 class MediaLog; |
25 } | 26 } |
26 | 27 |
27 namespace webkit_media { | 28 namespace webkit_media { |
28 | 29 |
29 const int64 kPositionNotSpecified = -1; | 30 const int64 kPositionNotSpecified = -1; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // - net::ERR_INVALID_RESPONSE | 77 // - net::ERR_INVALID_RESPONSE |
77 // An invalid response is received from the server. | 78 // An invalid response is received from the server. |
78 // - (Anything else) | 79 // - (Anything else) |
79 // An error code that indicates the request has failed. | 80 // An error code that indicates the request has failed. |
80 // |event_callback| is called when the response is completed, data is | 81 // |event_callback| is called when the response is completed, data is |
81 // received, the request is suspended or resumed. | 82 // received, the request is suspended or resumed. |
82 virtual void Start(net::OldCompletionCallback* callback, | 83 virtual void Start(net::OldCompletionCallback* callback, |
83 const base::Closure& event_callback, | 84 const base::Closure& event_callback, |
84 WebKit::WebFrame* frame); | 85 WebKit::WebFrame* frame); |
85 | 86 |
86 // Stop this loader, cancels and request and release internal buffer. | 87 // Stops everything associated with this loader, including active URL loads |
| 88 // and pending callbacks. |
| 89 // |
| 90 // It is safe to delete a BufferedResourceLoader after calling Stop(). |
87 virtual void Stop(); | 91 virtual void Stop(); |
88 | 92 |
89 // Reads the specified |read_size| from |position| into |buffer| and when | 93 // Reads the specified |read_size| from |position| into |buffer| and when |
90 // the operation is done invoke |callback| with number of bytes read or an | 94 // the operation is done invoke |callback| with number of bytes read or an |
91 // error code. If necessary, will temporarily increase forward capacity of | 95 // error code. If necessary, will temporarily increase forward capacity of |
92 // buffer to accomodate an unusually large read. | 96 // buffer to accomodate an unusually large read. |
93 // |callback| is called with the following values: | 97 // |callback| is called with the following values: |
94 // - (Anything greater than or equal 0) | 98 // - (Anything greater than or equal 0) |
95 // Read was successful with the indicated number of bytes read. | 99 // Read was successful with the indicated number of bytes read. |
96 // - net::ERR_FAILED | 100 // - net::ERR_FAILED |
(...skipping 18 matching lines...) Expand all Loading... |
115 | 119 |
116 // Returns true if the server supports byte range requests. | 120 // Returns true if the server supports byte range requests. |
117 virtual bool range_supported(); | 121 virtual bool range_supported(); |
118 | 122 |
119 // Returns true if the resource loader is currently downloading data. | 123 // Returns true if the resource loader is currently downloading data. |
120 virtual bool is_downloading_data(); | 124 virtual bool is_downloading_data(); |
121 | 125 |
122 // Returns resulting URL. | 126 // Returns resulting URL. |
123 virtual const GURL& url(); | 127 virtual const GURL& url(); |
124 | 128 |
125 // Used to inject a mock used for unittests. | 129 // Transfer ownership of an existing WebURLLoader instance for |
126 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); | 130 // testing purposes. |
| 131 // |
| 132 // |test_loader| will get used the next time Start() is called. |
| 133 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* test_loader); |
127 | 134 |
128 // WebKit::WebURLLoaderClient implementation. | 135 // WebKit::WebURLLoaderClient implementation. |
129 virtual void willSendRequest( | 136 virtual void willSendRequest( |
130 WebKit::WebURLLoader* loader, | 137 WebKit::WebURLLoader* loader, |
131 WebKit::WebURLRequest& newRequest, | 138 WebKit::WebURLRequest& newRequest, |
132 const WebKit::WebURLResponse& redirectResponse); | 139 const WebKit::WebURLResponse& redirectResponse); |
133 virtual void didSendData( | 140 virtual void didSendData( |
134 WebKit::WebURLLoader* loader, | 141 WebKit::WebURLLoader* loader, |
135 unsigned long long bytesSent, | 142 unsigned long long bytesSent, |
136 unsigned long long totalBytesToBeSent); | 143 unsigned long long totalBytesToBeSent); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 181 |
175 private: | 182 private: |
176 friend class BufferedDataSourceTest; | 183 friend class BufferedDataSourceTest; |
177 friend class BufferedResourceLoaderTest; | 184 friend class BufferedResourceLoaderTest; |
178 | 185 |
179 // Updates the |buffer_|'s forward and backward capacities. | 186 // Updates the |buffer_|'s forward and backward capacities. |
180 void UpdateBufferWindow(); | 187 void UpdateBufferWindow(); |
181 | 188 |
182 // Returns true if we should defer resource loading, based | 189 // Returns true if we should defer resource loading, based |
183 // on current buffering scheme. | 190 // on current buffering scheme. |
184 bool ShouldEnableDefer(); | 191 bool ShouldEnableDefer() const; |
185 | 192 |
186 // Returns true if we should enable resource loading, based | 193 // Returns true if we should enable resource loading, based |
187 // on current buffering scheme. | 194 // on current buffering scheme. |
188 bool ShouldDisableDefer(); | 195 bool ShouldDisableDefer() const; |
189 | 196 |
190 // Updates deferring behavior based on current buffering scheme. | 197 // Updates deferring behavior based on current buffering scheme. |
191 void UpdateDeferBehavior(); | 198 void UpdateDeferBehavior(); |
192 | 199 |
193 // Set defer state to |deferred| and cease/continue downloading data | 200 // Set defer state to |deferred| and cease/continue downloading data |
194 // accordingly. | 201 // accordingly. |
195 void SetDeferred(bool deferred); | 202 void SetDeferred(bool deferred); |
196 | 203 |
197 // Returns true if the current read request can be fulfilled by what is in | 204 // Returns true if the current read request can be fulfilled by what is in |
198 // the buffer. | 205 // the buffer. |
199 bool CanFulfillRead(); | 206 bool CanFulfillRead() const; |
200 | 207 |
201 // Returns true if the current read request will be fulfilled in the future. | 208 // Returns true if the current read request will be fulfilled in the future. |
202 bool WillFulfillRead(); | 209 bool WillFulfillRead() const; |
203 | 210 |
204 // Method that does the actual read and calls the |read_callback_|, assuming | 211 // Method that does the actual read and calls the |read_callback_|, assuming |
205 // the request range is in |buffer_|. | 212 // the request range is in |buffer_|. |
206 void ReadInternal(); | 213 void ReadInternal(); |
207 | 214 |
208 // If we have made a range request, verify the response from the server. | 215 // If we have made a range request, verify the response from the server. |
209 bool VerifyPartialResponse(const WebKit::WebURLResponse& response); | 216 bool VerifyPartialResponse(const WebKit::WebURLResponse& response); |
210 | 217 |
211 // Returns the value for a range request header using parameters | 218 // Returns the value for a range request header using parameters |
212 // |first_byte_position| and |last_byte_position|. Negative numbers other | 219 // |first_byte_position| and |last_byte_position|. Negative numbers other |
(...skipping 18 matching lines...) Expand all Loading... |
231 | 238 |
232 // Helper function that returns true if a range request was specified. | 239 // Helper function that returns true if a range request was specified. |
233 bool IsRangeRequest() const; | 240 bool IsRangeRequest() const; |
234 | 241 |
235 // Log everything interesting to |media_log_|. | 242 // Log everything interesting to |media_log_|. |
236 void Log(); | 243 void Log(); |
237 | 244 |
238 // A sliding window of buffer. | 245 // A sliding window of buffer. |
239 scoped_ptr<media::SeekableBuffer> buffer_; | 246 scoped_ptr<media::SeekableBuffer> buffer_; |
240 | 247 |
241 // True if resource loading was deferred. | 248 // Keeps track of an active WebURLLoader and associated state. |
242 bool deferred_; | 249 scoped_ptr<ActiveLoader> active_loader_; |
243 | 250 |
244 // Current buffering algorithm in place for resource loading. | 251 // Current buffering algorithm in place for resource loading. |
245 DeferStrategy defer_strategy_; | 252 DeferStrategy defer_strategy_; |
246 | 253 |
247 // True if resource loading has completed. | |
248 bool completed_; | |
249 | |
250 // True if a range request was made. | 254 // True if a range request was made. |
251 bool range_requested_; | 255 bool range_requested_; |
252 | 256 |
253 // True if Range header is supported. | 257 // True if Range header is supported. |
254 bool range_supported_; | 258 bool range_supported_; |
255 | 259 |
256 // Forward capacity to reset to after an extension. | 260 // Forward capacity to reset to after an extension. |
257 size_t saved_forward_capacity_; | 261 size_t saved_forward_capacity_; |
258 | 262 |
259 // Does the work of loading and sends data back to this client. | |
260 scoped_ptr<WebKit::WebURLLoader> url_loader_; | |
261 | |
262 GURL url_; | 263 GURL url_; |
263 int64 first_byte_position_; | 264 int64 first_byte_position_; |
264 int64 last_byte_position_; | 265 int64 last_byte_position_; |
265 bool single_origin_; | 266 bool single_origin_; |
266 | 267 |
267 // Callback method that listens to network events. | 268 // Callback method that listens to network events. |
268 base::Closure event_callback_; | 269 base::Closure event_callback_; |
269 | 270 |
270 // Members used during request start. | 271 // Members used during request start. |
271 scoped_ptr<net::OldCompletionCallback> start_callback_; | 272 scoped_ptr<net::OldCompletionCallback> start_callback_; |
272 int64 offset_; | 273 int64 offset_; |
273 int64 content_length_; | 274 int64 content_length_; |
274 int64 instance_size_; | 275 int64 instance_size_; |
275 | 276 |
276 // Members used during a read operation. They should be reset after each | 277 // Members used during a read operation. They should be reset after each |
277 // read has completed or failed. | 278 // read has completed or failed. |
278 scoped_ptr<net::OldCompletionCallback> read_callback_; | 279 scoped_ptr<net::OldCompletionCallback> read_callback_; |
279 int64 read_position_; | 280 int64 read_position_; |
280 size_t read_size_; | 281 size_t read_size_; |
281 uint8* read_buffer_; | 282 uint8* read_buffer_; |
282 | 283 |
283 // Offsets of the requested first byte and last byte in |buffer_|. They are | 284 // Offsets of the requested first byte and last byte in |buffer_|. They are |
284 // written by Read(). | 285 // written by Read(). |
285 int first_offset_; | 286 int first_offset_; |
286 int last_offset_; | 287 int last_offset_; |
287 | 288 |
288 // Used to ensure mocks for unittests are used instead of reset in Start(). | 289 // Injected WebURLLoader instance for testing purposes. |
289 bool keep_test_loader_; | 290 scoped_ptr<WebKit::WebURLLoader> test_loader_; |
290 | 291 |
291 // Bitrate of the media. Set to 0 if unknown. | 292 // Bitrate of the media. Set to 0 if unknown. |
292 int bitrate_; | 293 int bitrate_; |
293 | 294 |
294 // Playback rate of the media. | 295 // Playback rate of the media. |
295 float playback_rate_; | 296 float playback_rate_; |
296 | 297 |
297 scoped_refptr<media::MediaLog> media_log_; | 298 scoped_refptr<media::MediaLog> media_log_; |
298 | 299 |
299 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | 300 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
300 }; | 301 }; |
301 | 302 |
302 } // namespace webkit_media | 303 } // namespace webkit_media |
303 | 304 |
304 #endif // WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 305 #endif // WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
OLD | NEW |