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 "webkit/glue/media/buffered_resource_loader.h" |
|
scherkus (not reviewing)
2010/12/14 18:48:19
nit: I don't think you actually need the header (I
annacc
2010/12/14 21:10:17
Yes, it seems like that should be true, but the co
scherkus (not reviewing)
2010/12/15 16:00:36
should be fine but let's not worry about it for no
| |
| 14 #include "base/condition_variable.h" | |
| 15 #include "googleurl/src/gurl.h" | |
| 16 #include "media/base/filters.h" | |
| 17 #include "media/base/media_format.h" | |
| 18 #include "media/base/pipeline.h" | |
| 19 #include "media/base/seekable_buffer.h" | |
| 20 #include "net/base/completion_callback.h" | |
| 21 #include "net/base/file_stream.h" | |
| 22 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.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" | |
| 27 #include "webkit/glue/webmediaplayer_impl.h" | |
| 28 | |
| 29 namespace WebKit { | |
| 30 class WebURLResponse; | |
| 31 } | |
| 32 | 14 |
| 33 namespace webkit_glue { | 15 namespace webkit_glue { |
| 34 | 16 |
| 35 ///////////////////////////////////////////////////////////////////////////// | |
| 36 // BufferedResourceLoader | |
| 37 // This class works inside demuxer thread and render thread. It contains a | |
| 38 // WebURLLoader and does the actual resource loading. This object does | |
| 39 // buffering internally, it defers the resource loading if buffer is full | |
| 40 // and un-defers the resource loading if it is under buffered. | |
| 41 class BufferedResourceLoader : | |
| 42 public base::RefCountedThreadSafe<BufferedResourceLoader>, | |
| 43 public WebKit::WebURLLoaderClient { | |
| 44 public: | |
| 45 typedef Callback0::Type NetworkEventCallback; | |
| 46 | |
| 47 // |url| - URL for the resource to be loaded. | |
| 48 // |first_byte_position| - First byte to start loading from, -1 for not | |
| 49 // specified. | |
| 50 // |last_byte_position| - Last byte to be loaded, -1 for not specified. | |
| 51 BufferedResourceLoader(const GURL& url, | |
| 52 int64 first_byte_position, | |
| 53 int64 last_byte_position); | |
| 54 | |
| 55 // Start the resource loading with the specified URL and range. | |
| 56 // This method operates in asynchronous mode. Once there's a response from the | |
| 57 // server, success or fail |callback| is called with the result. | |
| 58 // |callback| is called with the following values: | |
| 59 // - net::OK | |
| 60 // The request has started successfully. | |
| 61 // - net::ERR_FAILED | |
| 62 // The request has failed because of an error with the network. | |
| 63 // - net::ERR_INVALID_RESPONSE | |
| 64 // An invalid response is received from the server. | |
| 65 // - (Anything else) | |
| 66 // An error code that indicates the request has failed. | |
| 67 // |event_callback| is called when the response is completed, data is | |
| 68 // received, the request is suspended or resumed. | |
| 69 virtual void Start(net::CompletionCallback* callback, | |
| 70 NetworkEventCallback* event_callback, | |
| 71 WebKit::WebFrame* frame); | |
| 72 | |
| 73 // Stop this loader, cancels and request and release internal buffer. | |
| 74 virtual void Stop(); | |
| 75 | |
| 76 // 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 | |
| 78 // error code. | |
| 79 // |callback| is called with the following values: | |
| 80 // - (Anything greater than or equal 0) | |
| 81 // Read was successful with the indicated number of bytes read. | |
| 82 // - net::ERR_FAILED | |
| 83 // The read has failed because of an error with the network. | |
| 84 // - net::ERR_CACHE_MISS | |
| 85 // The read was made too far away from the current buffered position. | |
| 86 virtual void Read(int64 position, int read_size, | |
| 87 uint8* buffer, net::CompletionCallback* callback); | |
| 88 | |
| 89 // Returns the position of the first byte buffered. Returns -1 if such value | |
| 90 // is not available. | |
| 91 virtual int64 GetBufferedFirstBytePosition(); | |
| 92 | |
| 93 // Returns the position of the last byte buffered. Returns -1 if such value | |
| 94 // is not available. | |
| 95 virtual int64 GetBufferedLastBytePosition(); | |
| 96 | |
| 97 // Sets whether deferring data is allowed or disallowed. | |
| 98 virtual void SetAllowDefer(bool is_allowed); | |
| 99 | |
| 100 // Gets the content length in bytes of the instance after this loader has been | |
| 101 // started. If this value is -1, then content length is unknown. | |
| 102 virtual int64 content_length() { return content_length_; } | |
| 103 | |
| 104 // Gets the original size of the file requested. If this value is -1, then | |
| 105 // the size is unknown. | |
| 106 virtual int64 instance_size() { return instance_size_; } | |
| 107 | |
| 108 // Returns true if the response for this loader is a partial response. | |
| 109 // It means a 206 response in HTTP/HTTPS protocol. | |
| 110 virtual bool partial_response() { return partial_response_; } | |
| 111 | |
| 112 // Returns true if network is currently active. | |
| 113 virtual bool network_activity() { return !completed_ && !deferred_; } | |
| 114 | |
| 115 // Returns resulting URL. | |
| 116 virtual const GURL& url() { return url_; } | |
| 117 | |
| 118 // Used to inject a mock used for unittests. | |
| 119 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); | |
| 120 | |
| 121 ///////////////////////////////////////////////////////////////////////////// | |
| 122 // WebKit::WebURLLoaderClient implementations. | |
| 123 virtual void willSendRequest( | |
| 124 WebKit::WebURLLoader* loader, | |
| 125 WebKit::WebURLRequest& newRequest, | |
| 126 const WebKit::WebURLResponse& redirectResponse); | |
| 127 virtual void didSendData( | |
| 128 WebKit::WebURLLoader* loader, | |
| 129 unsigned long long bytesSent, | |
| 130 unsigned long long totalBytesToBeSent); | |
| 131 virtual void didReceiveResponse( | |
| 132 WebKit::WebURLLoader* loader, | |
| 133 const WebKit::WebURLResponse& response); | |
| 134 virtual void didDownloadData( | |
| 135 WebKit::WebURLLoader* loader, | |
| 136 int dataLength); | |
| 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&); | |
| 150 | |
| 151 protected: | |
| 152 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | |
| 153 | |
| 154 virtual ~BufferedResourceLoader(); | |
| 155 | |
| 156 private: | |
| 157 friend class BufferedResourceLoaderTest; | |
| 158 | |
| 159 // Defer the resource loading if the buffer is full. | |
| 160 void EnableDeferIfNeeded(); | |
| 161 | |
| 162 // Disable defer loading if we are under-buffered. | |
| 163 void DisableDeferIfNeeded(); | |
| 164 | |
| 165 // Returns true if the current read request can be fulfilled by what is in | |
| 166 // the buffer. | |
| 167 bool CanFulfillRead(); | |
| 168 | |
| 169 // Returns true if the current read request will be fulfilled in the future. | |
| 170 bool WillFulfillRead(); | |
| 171 | |
| 172 // Method that does the actual read and calls the |read_callbac_|, assuming | |
| 173 // the request range is in |buffer_|. | |
| 174 void ReadInternal(); | |
| 175 | |
| 176 // If we have made a range request, verify the response from the server. | |
| 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); | |
| 187 | |
| 188 // Done with read. Invokes the read callback and reset parameters for the | |
| 189 // read request. | |
| 190 void DoneRead(int error); | |
| 191 | |
| 192 // Done with start. Invokes the start callback and reset it. | |
| 193 void DoneStart(int error); | |
| 194 | |
| 195 // Calls |event_callback_| in terms of a network event. | |
| 196 void NotifyNetworkEvent(); | |
| 197 | |
| 198 bool HasPendingRead() { return read_callback_.get() != NULL; } | |
| 199 | |
| 200 // A sliding window of buffer. | |
| 201 scoped_ptr<media::SeekableBuffer> buffer_; | |
| 202 | |
| 203 // True if resource loading was deferred. | |
| 204 bool deferred_; | |
| 205 | |
| 206 // True if resource loader is allowed to defer, false otherwise. | |
| 207 bool defer_allowed_; | |
| 208 | |
| 209 // True if resource loading has completed. | |
| 210 bool completed_; | |
| 211 | |
| 212 // True if a range request was made. | |
| 213 bool range_requested_; | |
| 214 | |
| 215 // True if response data received is a partial range. | |
| 216 bool partial_response_; | |
| 217 | |
| 218 // Does the work of loading and sends data back to this client. | |
| 219 scoped_ptr<WebKit::WebURLLoader> url_loader_; | |
| 220 | |
| 221 GURL url_; | |
| 222 int64 first_byte_position_; | |
| 223 int64 last_byte_position_; | |
| 224 | |
| 225 // Callback method that listens to network events. | |
| 226 scoped_ptr<NetworkEventCallback> event_callback_; | |
| 227 | |
| 228 // Members used during request start. | |
| 229 scoped_ptr<net::CompletionCallback> start_callback_; | |
| 230 int64 offset_; | |
| 231 int64 content_length_; | |
| 232 int64 instance_size_; | |
| 233 | |
| 234 // Members used during a read operation. They should be reset after each | |
| 235 // read has completed or failed. | |
| 236 scoped_ptr<net::CompletionCallback> read_callback_; | |
| 237 int64 read_position_; | |
| 238 int read_size_; | |
| 239 uint8* read_buffer_; | |
| 240 | |
| 241 // Offsets of the requested first byte and last byte in |buffer_|. They are | |
| 242 // written by VerifyRead(). | |
| 243 int first_offset_; | |
| 244 int last_offset_; | |
| 245 | |
| 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); | |
| 250 }; | |
| 251 | |
| 252 class BufferedDataSource : public WebDataSource { | 17 class BufferedDataSource : public WebDataSource { |
| 253 public: | 18 public: |
| 254 BufferedDataSource(MessageLoop* render_loop, | 19 BufferedDataSource(MessageLoop* render_loop, |
| 255 WebKit::WebFrame* frame); | 20 WebKit::WebFrame* frame); |
| 256 | 21 |
| 257 virtual ~BufferedDataSource(); | 22 virtual ~BufferedDataSource(); |
| 258 | 23 |
| 259 // media::Filter implementation. | 24 // media::Filter implementation. |
| 260 virtual void Initialize(const std::string& url, | 25 virtual void Initialize(const std::string& url, |
| 261 media::FilterCallback* callback); | 26 media::FilterCallback* callback); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 // Keeps track of whether we used a Range header in the initialization | 198 // Keeps track of whether we used a Range header in the initialization |
| 434 // request. | 199 // request. |
| 435 bool using_range_request_; | 200 bool using_range_request_; |
| 436 | 201 |
| 437 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 202 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 438 }; | 203 }; |
| 439 | 204 |
| 440 } // namespace webkit_glue | 205 } // namespace webkit_glue |
| 441 | 206 |
| 442 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ | 207 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| OLD | NEW |