OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | |
6 #define WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/timer.h" | |
13 #include "googleurl/src/gurl.h" | |
14 #include "media/base/seekable_buffer.h" | |
15 #include "net/base/file_stream.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | |
20 #include "webkit/glue/media/web_data_source.h" | |
21 #include "webkit/glue/webmediaplayer_impl.h" | |
22 | |
23 namespace media { | |
24 class MediaLog; | |
25 } | |
26 | |
27 namespace webkit_glue { | |
28 | |
29 const int64 kPositionNotSpecified = -1; | |
30 | |
31 const char kHttpScheme[] = "http"; | |
32 const char kHttpsScheme[] = "https"; | |
33 const char kDataScheme[] = "data"; | |
34 | |
35 // This class works inside demuxer thread and render thread. It contains a | |
36 // WebURLLoader and does the actual resource loading. This object does | |
37 // buffering internally, it defers the resource loading if buffer is full | |
38 // and un-defers the resource loading if it is under buffered. | |
39 class BufferedResourceLoader | |
40 : public base::RefCountedThreadSafe<BufferedResourceLoader>, | |
41 public WebKit::WebURLLoaderClient { | |
42 public: | |
43 // kNeverDefer - Aggresively buffer; never defer loading while paused. | |
44 // kReadThenDefer - Request only enough data to fulfill read requests. | |
45 // kThresholdDefer - Try to keep amount of buffered data at a threshold. | |
46 enum DeferStrategy { | |
47 kNeverDefer, | |
48 kReadThenDefer, | |
49 kThresholdDefer, | |
50 }; | |
51 | |
52 // |url| - URL for the resource to be loaded. | |
53 // |first_byte_position| - First byte to start loading from, | |
54 // |kPositionNotSpecified| for not specified. | |
55 // |last_byte_position| - Last byte to be loaded, | |
56 // |kPositionNotSpecified| for not specified. | |
57 // |strategy| is the initial loading strategy to use. | |
58 // |bitrate| is the bitrate of the media, 0 if unknown. | |
59 // |playback_rate| is the current playback rate of the media. | |
60 BufferedResourceLoader(const GURL& url, | |
61 int64 first_byte_position, | |
62 int64 last_byte_position, | |
63 DeferStrategy strategy, | |
64 int bitrate, | |
65 float playback_rate, | |
66 media::MediaLog* media_log); | |
67 | |
68 // Start the resource loading with the specified URL and range. | |
69 // This method operates in asynchronous mode. Once there's a response from the | |
70 // server, success or fail |callback| is called with the result. | |
71 // |callback| is called with the following values: | |
72 // - net::OK | |
73 // The request has started successfully. | |
74 // - net::ERR_FAILED | |
75 // The request has failed because of an error with the network. | |
76 // - net::ERR_INVALID_RESPONSE | |
77 // An invalid response is received from the server. | |
78 // - (Anything else) | |
79 // An error code that indicates the request has failed. | |
80 // |event_callback| is called when the response is completed, data is | |
81 // received, the request is suspended or resumed. | |
82 virtual void Start(net::OldCompletionCallback* callback, | |
83 const base::Closure& event_callback, | |
84 WebKit::WebFrame* frame); | |
85 | |
86 // Stop this loader, cancels and request and release internal buffer. | |
87 virtual void Stop(); | |
88 | |
89 // 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 | |
91 // error code. If necessary, will temporarily increase forward capacity of | |
92 // buffer to accomodate an unusually large read. | |
93 // |callback| is called with the following values: | |
94 // - (Anything greater than or equal 0) | |
95 // Read was successful with the indicated number of bytes read. | |
96 // - net::ERR_FAILED | |
97 // The read has failed because of an error with the network. | |
98 // - net::ERR_CACHE_MISS | |
99 // The read was made too far away from the current buffered position. | |
100 virtual void Read(int64 position, int read_size, | |
101 uint8* buffer, net::OldCompletionCallback* callback); | |
102 | |
103 // Returns the position of the last byte buffered. Returns | |
104 // |kPositionNotSpecified| if such value is not available. | |
105 virtual int64 GetBufferedPosition(); | |
106 | |
107 // Gets the content length in bytes of the instance after this loader has been | |
108 // started. If this value is |kPositionNotSpecified|, then content length is | |
109 // unknown. | |
110 virtual int64 content_length(); | |
111 | |
112 // Gets the original size of the file requested. If this value is | |
113 // |kPositionNotSpecified|, then the size is unknown. | |
114 virtual int64 instance_size(); | |
115 | |
116 // Returns true if the server supports byte range requests. | |
117 virtual bool range_supported(); | |
118 | |
119 // Returns true if the resource loader is currently downloading data. | |
120 virtual bool is_downloading_data(); | |
121 | |
122 // Returns resulting URL. | |
123 virtual const GURL& url(); | |
124 | |
125 // Used to inject a mock used for unittests. | |
126 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); | |
127 | |
128 // WebKit::WebURLLoaderClient implementation. | |
129 virtual void willSendRequest( | |
130 WebKit::WebURLLoader* loader, | |
131 WebKit::WebURLRequest& newRequest, | |
132 const WebKit::WebURLResponse& redirectResponse); | |
133 virtual void didSendData( | |
134 WebKit::WebURLLoader* loader, | |
135 unsigned long long bytesSent, | |
136 unsigned long long totalBytesToBeSent); | |
137 virtual void didReceiveResponse( | |
138 WebKit::WebURLLoader* loader, | |
139 const WebKit::WebURLResponse& response); | |
140 virtual void didDownloadData( | |
141 WebKit::WebURLLoader* loader, | |
142 int data_length); | |
143 virtual void didReceiveData( | |
144 WebKit::WebURLLoader* loader, | |
145 const char* data, | |
146 int data_length, | |
147 int encoded_data_length); | |
148 virtual void didReceiveCachedMetadata( | |
149 WebKit::WebURLLoader* loader, | |
150 const char* data, int dataLength); | |
151 virtual void didFinishLoading( | |
152 WebKit::WebURLLoader* loader, | |
153 double finishTime); | |
154 virtual void didFail( | |
155 WebKit::WebURLLoader* loader, | |
156 const WebKit::WebURLError&); | |
157 | |
158 bool HasSingleOrigin() const; | |
159 | |
160 // Sets the defer strategy to the given value. | |
161 void UpdateDeferStrategy(DeferStrategy strategy); | |
162 | |
163 // Sets the playback rate to the given value and updates buffer window | |
164 // accordingly. | |
165 void SetPlaybackRate(float playback_rate); | |
166 | |
167 // Sets the bitrate to the given value and updates buffer window | |
168 // accordingly. | |
169 void SetBitrate(int bitrate); | |
170 | |
171 protected: | |
172 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | |
173 virtual ~BufferedResourceLoader(); | |
174 | |
175 private: | |
176 friend class BufferedDataSourceTest2; | |
177 friend class BufferedResourceLoaderTest; | |
178 | |
179 // Updates the |buffer_|'s forward and backward capacities. | |
180 void UpdateBufferWindow(); | |
181 | |
182 // Returns true if we should defer resource loading, based | |
183 // on current buffering scheme. | |
184 bool ShouldEnableDefer(); | |
185 | |
186 // Returns true if we should enable resource loading, based | |
187 // on current buffering scheme. | |
188 bool ShouldDisableDefer(); | |
189 | |
190 // Updates deferring behavior based on current buffering scheme. | |
191 void UpdateDeferBehavior(); | |
192 | |
193 // Set defer state to |deferred| and cease/continue downloading data | |
194 // accordingly. | |
195 void SetDeferred(bool deferred); | |
196 | |
197 // Returns true if the current read request can be fulfilled by what is in | |
198 // the buffer. | |
199 bool CanFulfillRead(); | |
200 | |
201 // Returns true if the current read request will be fulfilled in the future. | |
202 bool WillFulfillRead(); | |
203 | |
204 // Method that does the actual read and calls the |read_callback_|, assuming | |
205 // the request range is in |buffer_|. | |
206 void ReadInternal(); | |
207 | |
208 // If we have made a range request, verify the response from the server. | |
209 bool VerifyPartialResponse(const WebKit::WebURLResponse& response); | |
210 | |
211 // Returns the value for a range request header using parameters | |
212 // |first_byte_position| and |last_byte_position|. Negative numbers other | |
213 // than |kPositionNotSpecified| are not allowed for |first_byte_position| and | |
214 // |last_byte_position|. |first_byte_position| should always be less than or | |
215 // equal to |last_byte_position| if they are both not |kPositionNotSpecified|. | |
216 // Empty string is returned on invalid parameters. | |
217 std::string GenerateHeaders(int64 first_byte_position, | |
218 int64 last_byte_position); | |
219 | |
220 // Done with read. Invokes the read callback and reset parameters for the | |
221 // read request. | |
222 void DoneRead(int error); | |
223 | |
224 // Done with start. Invokes the start callback and reset it. | |
225 void DoneStart(int error); | |
226 | |
227 // Calls |event_callback_| in terms of a network event. | |
228 void NotifyNetworkEvent(); | |
229 | |
230 bool HasPendingRead() { return read_callback_.get() != NULL; } | |
231 | |
232 // Helper function that returns true if a range request was specified. | |
233 bool IsRangeRequest() const; | |
234 | |
235 // Log everything interesting to |media_log_|. | |
236 void Log(); | |
237 | |
238 // A sliding window of buffer. | |
239 scoped_ptr<media::SeekableBuffer> buffer_; | |
240 | |
241 // True if resource loading was deferred. | |
242 bool deferred_; | |
243 | |
244 // Current buffering algorithm in place for resource loading. | |
245 DeferStrategy defer_strategy_; | |
246 | |
247 // True if resource loading has completed. | |
248 bool completed_; | |
249 | |
250 // True if a range request was made. | |
251 bool range_requested_; | |
252 | |
253 // True if Range header is supported. | |
254 bool range_supported_; | |
255 | |
256 // Forward capacity to reset to after an extension. | |
257 size_t saved_forward_capacity_; | |
258 | |
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 int64 first_byte_position_; | |
264 int64 last_byte_position_; | |
265 bool single_origin_; | |
266 | |
267 // Callback method that listens to network events. | |
268 base::Closure event_callback_; | |
269 | |
270 // Members used during request start. | |
271 scoped_ptr<net::OldCompletionCallback> start_callback_; | |
272 int64 offset_; | |
273 int64 content_length_; | |
274 int64 instance_size_; | |
275 | |
276 // Members used during a read operation. They should be reset after each | |
277 // read has completed or failed. | |
278 scoped_ptr<net::OldCompletionCallback> read_callback_; | |
279 int64 read_position_; | |
280 size_t read_size_; | |
281 uint8* read_buffer_; | |
282 | |
283 // Offsets of the requested first byte and last byte in |buffer_|. They are | |
284 // written by Read(). | |
285 int first_offset_; | |
286 int last_offset_; | |
287 | |
288 // Used to ensure mocks for unittests are used instead of reset in Start(). | |
289 bool keep_test_loader_; | |
290 | |
291 // Bitrate of the media. Set to 0 if unknown. | |
292 int bitrate_; | |
293 | |
294 // Playback rate of the media. | |
295 float playback_rate_; | |
296 | |
297 scoped_refptr<media::MediaLog> media_log_; | |
298 | |
299 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | |
300 }; | |
301 | |
302 } // namespace webkit_glue | |
303 | |
304 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | |
OLD | NEW |