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