| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // 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/lock.h" | 10 #include "base/lock.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 // |last_byte_position| - Last byte to be loaded, -1 for not specified. | 37 // |last_byte_position| - Last byte to be loaded, -1 for not specified. |
| 38 BufferedResourceLoader( | 38 BufferedResourceLoader( |
| 39 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory, | 39 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory, |
| 40 const GURL& url, | 40 const GURL& url, |
| 41 int64 first_byte_position, | 41 int64 first_byte_position, |
| 42 int64 last_byte_position); | 42 int64 last_byte_position); |
| 43 virtual ~BufferedResourceLoader(); | 43 virtual ~BufferedResourceLoader(); |
| 44 | 44 |
| 45 // Start the resource loading with the specified URL and range. | 45 // Start the resource loading with the specified URL and range. |
| 46 // This method operates in asynchronous mode. Once there's a response from the | 46 // This method operates in asynchronous mode. Once there's a response from the |
| 47 // server, success or fail |start_callback| is called with the result. | 47 // server, success or fail |callback| is called with the result. |
| 48 // |callback| is called with the following values: |
| 49 // - net::OK |
| 50 // The request has started successfully. |
| 51 // - net::ERR_REQUEST_RANGE_NOT_SATISFIABLE |
| 52 // A range request was made to the server but the server doesn't support it. |
| 53 // - net::ERR_FAILED |
| 54 // The request has failed because of an error with the network. |
| 55 // - net::ERR_INVALID_RESPONSE |
| 56 // An invalid response is received from the server. |
| 57 // - (Anything else) |
| 58 // An error code that indicates the request has failed. |
| 48 virtual void Start(net::CompletionCallback* callback); | 59 virtual void Start(net::CompletionCallback* callback); |
| 49 | 60 |
| 50 // Stop this loader, cancels and request and release internal buffer. | 61 // Stop this loader, cancels and request and release internal buffer. |
| 51 virtual void Stop(); | 62 virtual void Stop(); |
| 52 | 63 |
| 53 // Reads the specified |read_size| from |position| into |buffer| and when | 64 // Reads the specified |read_size| from |position| into |buffer| and when |
| 54 // the operation is done invoke |callback| with number of bytes read or an | 65 // the operation is done invoke |callback| with number of bytes read or an |
| 55 // error code. | 66 // error code. |
| 67 // |callback| is called with the following values: |
| 68 // - (Anything greater than or equal 0) |
| 69 // Read was successful with the indicated number of bytes read. |
| 70 // - net::ERR_FAILED |
| 71 // The read has failed because of an error with the network. |
| 72 // - net::ERR_CACHE_MISS |
| 73 // The read was made too far away from the current buffered position. |
| 56 virtual void Read(int64 position, int read_size, | 74 virtual void Read(int64 position, int read_size, |
| 57 uint8* buffer, net::CompletionCallback* callback); | 75 uint8* buffer, net::CompletionCallback* callback); |
| 58 | 76 |
| 59 // Gets the content length in bytes of the instance after this loader has been | 77 // Gets the content length in bytes of the instance after this loader has been |
| 60 // started. | 78 // started. If this value is -1, then content length is unknown. |
| 61 virtual int64 content_length() { return content_length_; } | 79 virtual int64 content_length() { return content_length_; } |
| 62 | 80 |
| 81 // Gets the original size of the file requested. If this value is -1, then |
| 82 // the size is unknown. |
| 83 virtual int64 instance_size() { return instance_size_; } |
| 84 |
| 63 ///////////////////////////////////////////////////////////////////////////// | 85 ///////////////////////////////////////////////////////////////////////////// |
| 64 // webkit_glue::ResourceLoaderBridge::Peer implementations. | 86 // webkit_glue::ResourceLoaderBridge::Peer implementations. |
| 65 virtual void OnUploadProgress(uint64 position, uint64 size) {} | 87 virtual void OnUploadProgress(uint64 position, uint64 size) {} |
| 66 virtual void OnReceivedRedirect(const GURL& new_url); | 88 virtual void OnReceivedRedirect(const GURL& new_url); |
| 67 virtual void OnReceivedResponse( | 89 virtual void OnReceivedResponse( |
| 68 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info, | 90 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info, |
| 69 bool content_filtered); | 91 bool content_filtered); |
| 70 virtual void OnReceivedData(const char* data, int len); | 92 virtual void OnReceivedData(const char* data, int len); |
| 71 virtual void OnCompletedRequest(const URLRequestStatus& status, | 93 virtual void OnCompletedRequest(const URLRequestStatus& status, |
| 72 const std::string& security_info); | 94 const std::string& security_info); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 // Disable defer loading if we are under-buffered. | 106 // Disable defer loading if we are under-buffered. |
| 85 void DisableDeferIfNeeded(); | 107 void DisableDeferIfNeeded(); |
| 86 | 108 |
| 87 // Returns true if the current read request can be fulfilled by what is in | 109 // Returns true if the current read request can be fulfilled by what is in |
| 88 // the buffer. | 110 // the buffer. |
| 89 bool CanFulfillRead(); | 111 bool CanFulfillRead(); |
| 90 | 112 |
| 91 // Returns true if the current read request will be fulfilled in the future. | 113 // Returns true if the current read request will be fulfilled in the future. |
| 92 bool WillFulfillRead(); | 114 bool WillFulfillRead(); |
| 93 | 115 |
| 94 // Checks parameters and make sure they are valid. | |
| 95 bool VerifyRead(); | |
| 96 | |
| 97 // Method that does the actual read and calls the |read_callbac_|, assuming | 116 // Method that does the actual read and calls the |read_callbac_|, assuming |
| 98 // the request range is in |buffer_|. | 117 // the request range is in |buffer_|. |
| 99 void ReadInternal(); | 118 void ReadInternal(); |
| 100 | 119 |
| 120 // If we have made a range request, verify the response from the server. |
| 121 bool VerifyPartialResponse(const ResourceLoaderBridge::ResponseInfo& info); |
| 122 |
| 101 // Done with read. Invokes the read callback and reset parameters for the | 123 // Done with read. Invokes the read callback and reset parameters for the |
| 102 // read request. | 124 // read request. |
| 103 void DoneRead(int error); | 125 void DoneRead(int error); |
| 104 | 126 |
| 105 // Done with start. Invokes the start callback and reset it. | 127 // Done with start. Invokes the start callback and reset it. |
| 106 void DoneStart(int error); | 128 void DoneStart(int error); |
| 107 | 129 |
| 108 bool HasPendingRead() { return read_callback_.get() != NULL; } | 130 bool HasPendingRead() { return read_callback_.get() != NULL; } |
| 109 | 131 |
| 110 // A sliding window of buffer. | 132 // A sliding window of buffer. |
| 111 scoped_ptr<media::SeekableBuffer> buffer_; | 133 scoped_ptr<media::SeekableBuffer> buffer_; |
| 112 | 134 |
| 113 bool deferred_; | 135 bool deferred_; |
| 114 bool completed_; | 136 bool completed_; |
| 115 bool range_requested_; | 137 bool range_requested_; |
| 116 | 138 |
| 117 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; | 139 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; |
| 118 GURL url_; | 140 GURL url_; |
| 119 int64 first_byte_position_; | 141 int64 first_byte_position_; |
| 120 int64 last_byte_position_; | 142 int64 last_byte_position_; |
| 121 | 143 |
| 122 // Members used during request start. | 144 // Members used during request start. |
| 123 scoped_ptr<net::CompletionCallback> start_callback_; | 145 scoped_ptr<net::CompletionCallback> start_callback_; |
| 124 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; | 146 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; |
| 125 int64 offset_; | 147 int64 offset_; |
| 126 int64 content_length_; | 148 int64 content_length_; |
| 149 int64 instance_size_; |
| 127 | 150 |
| 128 // Members used during a read operation. They should be reset after each | 151 // Members used during a read operation. They should be reset after each |
| 129 // read has completed or failed. | 152 // read has completed or failed. |
| 130 scoped_ptr<net::CompletionCallback> read_callback_; | 153 scoped_ptr<net::CompletionCallback> read_callback_; |
| 131 int64 read_position_; | 154 int64 read_position_; |
| 132 int read_size_; | 155 int read_size_; |
| 133 uint8* read_buffer_; | 156 uint8* read_buffer_; |
| 134 | 157 |
| 135 // Offsets of the requested first byte and last byte in |buffer_|. They are | 158 // Offsets of the requested first byte and last byte in |buffer_|. They are |
| 136 // written by VerifyRead(). | 159 // written by VerifyRead(). |
| (...skipping 21 matching lines...) Expand all Loading... |
| 158 virtual void Initialize(const std::string& url, | 181 virtual void Initialize(const std::string& url, |
| 159 media::FilterCallback* callback); | 182 media::FilterCallback* callback); |
| 160 virtual void Stop(); | 183 virtual void Stop(); |
| 161 | 184 |
| 162 // media::DataSource implementation. | 185 // media::DataSource implementation. |
| 163 // Called from demuxer thread. | 186 // Called from demuxer thread. |
| 164 virtual void Read(int64 position, size_t size, | 187 virtual void Read(int64 position, size_t size, |
| 165 uint8* data, | 188 uint8* data, |
| 166 media::DataSource::ReadCallback* read_callback); | 189 media::DataSource::ReadCallback* read_callback); |
| 167 virtual bool GetSize(int64* size_out); | 190 virtual bool GetSize(int64* size_out); |
| 168 virtual bool IsSeekable(); | 191 virtual bool IsStreaming(); |
| 169 | 192 |
| 170 const media::MediaFormat& media_format() { | 193 const media::MediaFormat& media_format() { |
| 171 return media_format_; | 194 return media_format_; |
| 172 } | 195 } |
| 173 | 196 |
| 174 protected: | 197 protected: |
| 175 BufferedDataSource( | 198 BufferedDataSource( |
| 176 MessageLoop* render_loop, | 199 MessageLoop* render_loop, |
| 177 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); | 200 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); |
| 178 virtual ~BufferedDataSource(); | 201 virtual ~BufferedDataSource(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 208 // The method that performs actual read. This method can only be executed on | 231 // The method that performs actual read. This method can only be executed on |
| 209 // the render thread. | 232 // the render thread. |
| 210 void ReadInternal(); | 233 void ReadInternal(); |
| 211 | 234 |
| 212 // Calls |read_callback_| and reset all read parameters. | 235 // Calls |read_callback_| and reset all read parameters. |
| 213 void DoneRead(int error); | 236 void DoneRead(int error); |
| 214 | 237 |
| 215 // Calls |initialize_callback_| and reset it. | 238 // Calls |initialize_callback_| and reset it. |
| 216 void DoneInitialization(); | 239 void DoneInitialization(); |
| 217 | 240 |
| 218 // Callback method to perform BufferedResourceLoader::Start() during | 241 // Callback method for |loader_|. This method is called when response for |
| 219 // initialization. | 242 // initial request is received. |
| 220 void InitializeStartCallback(int error); | 243 void InitialStartCallback(int error); |
| 244 |
| 245 // Callback method for |probe_loader_|. This method is called when the |
| 246 // response for probe request is received. |
| 247 void ProbeStartCallback(int error); |
| 221 | 248 |
| 222 // Callback method to be passed to BufferedResourceLoader during range | 249 // Callback method to be passed to BufferedResourceLoader during range |
| 223 // request. Once a resource request has started, this method will be called | 250 // request. Once a resource request has started, this method will be called |
| 224 // with the error code. This method will be executed on the thread | 251 // with the error code. This method will be executed on the thread |
| 225 // BufferedResourceLoader lives, i.e. render thread. | 252 // BufferedResourceLoader lives, i.e. render thread. |
| 226 void PartialReadStartCallback(int error); | 253 void PartialReadStartCallback(int error); |
| 227 | 254 |
| 228 // Callback method for making a read request to BufferedResourceLoader. | 255 // Callback method for making a read request to BufferedResourceLoader. |
| 229 // If data arrives or the request has failed, this method is called with | 256 // If data arrives or the request has failed, this method is called with |
| 230 // the error code or the number of bytes read. | 257 // the error code or the number of bytes read. |
| 231 void ReadCallback(int error); | 258 void ReadCallback(int error); |
| 232 | 259 |
| 233 media::MediaFormat media_format_; | 260 media::MediaFormat media_format_; |
| 234 | 261 |
| 235 // URL of the resource requested. | 262 // URL of the resource requested. |
| 236 GURL url_; | 263 GURL url_; |
| 237 | 264 |
| 238 // Members for total bytes of the requested object. It is written once on | 265 // Members for total bytes of the requested object. It is written once on |
| 239 // render thread but may be read from any thread. However reading of this | 266 // render thread but may be read from any thread. However reading of this |
| 240 // member is guaranteed to happen after it is first written, so we don't | 267 // member is guaranteed to happen after it is first written, so we don't |
| 241 // need to protect it. | 268 // need to protect it. |
| 242 int64 total_bytes_; | 269 int64 total_bytes_; |
| 243 | 270 |
| 271 // This value will be true if this data source can only support streaming. |
| 272 // i.e. range request is not supported. |
| 273 bool streaming_; |
| 274 |
| 244 // A factory object to produce ResourceLoaderBridge. | 275 // A factory object to produce ResourceLoaderBridge. |
| 245 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_; | 276 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_; |
| 246 | 277 |
| 247 // A downloader object for loading the media resource. | 278 // A resource loader for the media resource. |
| 248 scoped_ptr<BufferedResourceLoader> loader_; | 279 scoped_ptr<BufferedResourceLoader> loader_; |
| 249 | 280 |
| 281 // A resource loader that probes the server's ability to serve range requests. |
| 282 scoped_ptr<BufferedResourceLoader> probe_loader_; |
| 283 |
| 284 // Callback method from the pipeline for initialization. |
| 285 scoped_ptr<media::FilterCallback> initialize_callback_; |
| 286 |
| 250 // Read parameters received from the Read() method call. | 287 // Read parameters received from the Read() method call. |
| 251 scoped_ptr<media::DataSource::ReadCallback> read_callback_; | 288 scoped_ptr<media::DataSource::ReadCallback> read_callback_; |
| 252 int64 read_position_; | 289 int64 read_position_; |
| 253 int read_size_; | 290 int read_size_; |
| 254 uint8* read_buffer_; | 291 uint8* read_buffer_; |
| 255 | 292 |
| 293 // This flag is set to true if the initial request has started. |
| 294 bool initial_response_received_; |
| 295 |
| 296 // This flag is set to true if the probe request has started. |
| 297 bool probe_response_received_; |
| 298 |
| 256 // This buffer is intermediate, we use it for BufferedResourceLoader to write | 299 // This buffer is intermediate, we use it for BufferedResourceLoader to write |
| 257 // to. And when read in BufferedResourceLoader is done, we copy data from | 300 // to. And when read in BufferedResourceLoader is done, we copy data from |
| 258 // this buffer to |read_buffer_|. The reason for an additional copy is that | 301 // this buffer to |read_buffer_|. The reason for an additional copy is that |
| 259 // we don't own |read_buffer_|. But since the read operation is asynchronous, | 302 // we don't own |read_buffer_|. But since the read operation is asynchronous, |
| 260 // |read_buffer| can be destroyed at any time, so we only copy into | 303 // |read_buffer| can be destroyed at any time, so we only copy into |
| 261 // |read_buffer| in the final step when it is safe. | 304 // |read_buffer| in the final step when it is safe. |
| 262 // Memory is allocated for this member during initialization of this object | 305 // Memory is allocated for this member during initialization of this object |
| 263 // because we want buffer to be passed into BufferedResourceLoader to be | 306 // because we want buffer to be passed into BufferedResourceLoader to be |
| 264 // always non-null. And by initializing this member with a default size we can | 307 // always non-null. And by initializing this member with a default size we can |
| 265 // avoid creating zero-sized buffered if the first read has zero size. | 308 // avoid creating zero-sized buffered if the first read has zero size. |
| 266 scoped_array<uint8> intermediate_read_buffer_; | 309 scoped_array<uint8> intermediate_read_buffer_; |
| 267 int intermediate_read_buffer_size_; | 310 int intermediate_read_buffer_size_; |
| 268 | 311 |
| 269 // The message loop of the render thread. | 312 // The message loop of the render thread. |
| 270 MessageLoop* render_loop_; | 313 MessageLoop* render_loop_; |
| 271 | 314 |
| 272 // Filter callbacks. | |
| 273 scoped_ptr<media::FilterCallback> initialize_callback_; | |
| 274 | |
| 275 // Protects |stopped_|. | 315 // Protects |stopped_|. |
| 276 Lock lock_; | 316 Lock lock_; |
| 277 | 317 |
| 278 // Stop signal to suppressing activities. | 318 // Stop signal to suppressing activities. |
| 279 bool stopped_; | 319 bool stopped_; |
| 280 | 320 |
| 281 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 321 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 282 }; | 322 }; |
| 283 | 323 |
| 284 } // namespace webkit_glue | 324 } // namespace webkit_glue |
| 285 | 325 |
| 286 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ | 326 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| OLD | NEW |