| 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 <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/lock.h" | 13 #include "base/lock.h" |
| 14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "base/condition_variable.h" | 16 #include "base/condition_variable.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 #include "media/base/factory.h" | 18 #include "media/base/factory.h" |
| 19 #include "media/base/filters.h" | 19 #include "media/base/filters.h" |
| 20 #include "media/base/media_format.h" | 20 #include "media/base/media_format.h" |
| 21 #include "media/base/pipeline.h" | 21 #include "media/base/pipeline.h" |
| 22 #include "media/base/seekable_buffer.h" | 22 #include "media/base/seekable_buffer.h" |
| 23 #include "net/base/completion_callback.h" | 23 #include "net/base/completion_callback.h" |
| 24 #include "net/base/file_stream.h" | 24 #include "net/base/file_stream.h" |
| 25 #include "webkit/glue/media/media_resource_loader_bridge_factory.h" | 25 #include "webkit/glue/media/media_resource_loader_bridge_factory.h" |
| 26 | 26 |
| 27 namespace webkit_glue { | 27 namespace webkit_glue { |
| 28 | |
| 29 ///////////////////////////////////////////////////////////////////////////// | 28 ///////////////////////////////////////////////////////////////////////////// |
| 30 // BufferedResourceLoader | 29 // BufferedResourceLoader |
| 31 // This class works inside demuxer thread and render thread. It contains a | 30 // This class works inside demuxer thread and render thread. It contains a |
| 32 // resource loader bridge and does the actual resource loading. This object | 31 // resource loader bridge and does the actual resource loading. This object |
| 33 // does buffering internally, it defers the resource loading if buffer is | 32 // does buffering internally, it defers the resource loading if buffer is |
| 34 // full and un-defers the resource loading if it is under buffered. | 33 // full and un-defers the resource loading if it is under buffered. |
| 35 class BufferedResourceLoader : | 34 class BufferedResourceLoader : |
| 36 public base::RefCountedThreadSafe<BufferedResourceLoader>, | 35 public base::RefCountedThreadSafe<BufferedResourceLoader>, |
| 37 public webkit_glue::ResourceLoaderBridge::Peer { | 36 public webkit_glue::ResourceLoaderBridge::Peer { |
| 38 public: | 37 public: |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 uint8* buffer, net::CompletionCallback* callback); | 82 uint8* buffer, net::CompletionCallback* callback); |
| 84 | 83 |
| 85 // Returns the position of the first byte buffered. Returns -1 if such value | 84 // Returns the position of the first byte buffered. Returns -1 if such value |
| 86 // is not available. | 85 // is not available. |
| 87 virtual int64 GetBufferedFirstBytePosition(); | 86 virtual int64 GetBufferedFirstBytePosition(); |
| 88 | 87 |
| 89 // Returns the position of the last byte buffered. Returns -1 if such value | 88 // Returns the position of the last byte buffered. Returns -1 if such value |
| 90 // is not available. | 89 // is not available. |
| 91 virtual int64 GetBufferedLastBytePosition(); | 90 virtual int64 GetBufferedLastBytePosition(); |
| 92 | 91 |
| 92 // Sets whether deferring data is allowed or disallowed. |
| 93 virtual void SetAllowDefer(bool is_allowed); |
| 94 |
| 93 // Gets the content length in bytes of the instance after this loader has been | 95 // Gets the content length in bytes of the instance after this loader has been |
| 94 // started. If this value is -1, then content length is unknown. | 96 // started. If this value is -1, then content length is unknown. |
| 95 virtual int64 content_length() { return content_length_; } | 97 virtual int64 content_length() { return content_length_; } |
| 96 | 98 |
| 97 // Gets the original size of the file requested. If this value is -1, then | 99 // Gets the original size of the file requested. If this value is -1, then |
| 98 // the size is unknown. | 100 // the size is unknown. |
| 99 virtual int64 instance_size() { return instance_size_; } | 101 virtual int64 instance_size() { return instance_size_; } |
| 100 | 102 |
| 101 // Returns true if the response for this loader is a partial response. | 103 // Returns true if the response for this loader is a partial response. |
| 102 // It means a 206 response in HTTP/HTTPS protocol. | 104 // It means a 206 response in HTTP/HTTPS protocol. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 124 protected: | 126 protected: |
| 125 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | 127 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; |
| 126 | 128 |
| 127 // An empty constructor so mock classes can be constructed. | 129 // An empty constructor so mock classes can be constructed. |
| 128 BufferedResourceLoader() { | 130 BufferedResourceLoader() { |
| 129 } | 131 } |
| 130 | 132 |
| 131 virtual ~BufferedResourceLoader(); | 133 virtual ~BufferedResourceLoader(); |
| 132 | 134 |
| 133 private: | 135 private: |
| 136 friend class BufferedResourceLoaderTest; |
| 134 | 137 |
| 135 // Defer the resource loading if the buffer is full. | 138 // Defer the resource loading if the buffer is full. |
| 136 void EnableDeferIfNeeded(); | 139 void EnableDeferIfNeeded(); |
| 137 | 140 |
| 138 // Disable defer loading if we are under-buffered. | 141 // Disable defer loading if we are under-buffered. |
| 139 void DisableDeferIfNeeded(); | 142 void DisableDeferIfNeeded(); |
| 140 | 143 |
| 141 // Returns true if the current read request can be fulfilled by what is in | 144 // Returns true if the current read request can be fulfilled by what is in |
| 142 // the buffer. | 145 // the buffer. |
| 143 bool CanFulfillRead(); | 146 bool CanFulfillRead(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 163 void NotifyNetworkEvent(); | 166 void NotifyNetworkEvent(); |
| 164 | 167 |
| 165 bool HasPendingRead() { return read_callback_.get() != NULL; } | 168 bool HasPendingRead() { return read_callback_.get() != NULL; } |
| 166 | 169 |
| 167 // A sliding window of buffer. | 170 // A sliding window of buffer. |
| 168 scoped_ptr<media::SeekableBuffer> buffer_; | 171 scoped_ptr<media::SeekableBuffer> buffer_; |
| 169 | 172 |
| 170 // True if resource loading was deferred. | 173 // True if resource loading was deferred. |
| 171 bool deferred_; | 174 bool deferred_; |
| 172 | 175 |
| 176 // True if resource loader is allowed to defer, false otherwise. |
| 177 bool defer_allowed_; |
| 178 |
| 173 // True if resource loading has completed. | 179 // True if resource loading has completed. |
| 174 bool completed_; | 180 bool completed_; |
| 175 | 181 |
| 176 // True if a range request was made. | 182 // True if a range request was made. |
| 177 bool range_requested_; | 183 bool range_requested_; |
| 178 | 184 |
| 179 // True if response data received is a partial range. | 185 // True if response data received is a partial range. |
| 180 bool partial_response_; | 186 bool partial_response_; |
| 181 | 187 |
| 182 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; | 188 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 } | 230 } |
| 225 | 231 |
| 226 // media::FilterFactoryImpl2 implementation. | 232 // media::FilterFactoryImpl2 implementation. |
| 227 static bool IsMediaFormatSupported( | 233 static bool IsMediaFormatSupported( |
| 228 const media::MediaFormat& media_format); | 234 const media::MediaFormat& media_format); |
| 229 | 235 |
| 230 // media::MediaFilter implementation. | 236 // media::MediaFilter implementation. |
| 231 virtual void Initialize(const std::string& url, | 237 virtual void Initialize(const std::string& url, |
| 232 media::FilterCallback* callback); | 238 media::FilterCallback* callback); |
| 233 virtual void Stop(media::FilterCallback* callback); | 239 virtual void Stop(media::FilterCallback* callback); |
| 240 virtual void SetPlaybackRate(float playback_rate); |
| 234 | 241 |
| 235 // media::DataSource implementation. | 242 // media::DataSource implementation. |
| 236 // Called from demuxer thread. | 243 // Called from demuxer thread. |
| 237 virtual void Read(int64 position, size_t size, | 244 virtual void Read(int64 position, size_t size, |
| 238 uint8* data, | 245 uint8* data, |
| 239 media::DataSource::ReadCallback* read_callback); | 246 media::DataSource::ReadCallback* read_callback); |
| 240 virtual bool GetSize(int64* size_out); | 247 virtual bool GetSize(int64* size_out); |
| 241 virtual bool IsStreaming(); | 248 virtual bool IsStreaming(); |
| 242 | 249 |
| 243 const media::MediaFormat& media_format() { | 250 const media::MediaFormat& media_format() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 void CleanupTask(); | 288 void CleanupTask(); |
| 282 | 289 |
| 283 // Restart resource loading on render thread. | 290 // Restart resource loading on render thread. |
| 284 void RestartLoadingTask(); | 291 void RestartLoadingTask(); |
| 285 | 292 |
| 286 // This task monitors the current active read request. If the current read | 293 // This task monitors the current active read request. If the current read |
| 287 // request has timed out, this task will destroy the current loader and | 294 // request has timed out, this task will destroy the current loader and |
| 288 // creates a new one to accommodate the read request. | 295 // creates a new one to accommodate the read request. |
| 289 void WatchDogTask(); | 296 void WatchDogTask(); |
| 290 | 297 |
| 298 // This task uses the current playback rate with the previous playback rate |
| 299 // to determine whether we are going from pause to play and play to pause, |
| 300 // and signals the buffered resource loader accordingly. |
| 301 void SetPlaybackRateTask(float playback_rate); |
| 302 |
| 291 // The method that performs actual read. This method can only be executed on | 303 // The method that performs actual read. This method can only be executed on |
| 292 // the render thread. | 304 // the render thread. |
| 293 void ReadInternal(); | 305 void ReadInternal(); |
| 294 | 306 |
| 295 // Calls |read_callback_| and reset all read parameters. | 307 // Calls |read_callback_| and reset all read parameters. |
| 296 void DoneRead_Locked(int error); | 308 void DoneRead_Locked(int error); |
| 297 | 309 |
| 298 // Calls |initialize_callback_| and reset it. | 310 // Calls |initialize_callback_| and reset it. |
| 299 void DoneInitialization_Locked(); | 311 void DoneInitialization_Locked(); |
| 300 | 312 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 Lock lock_; | 392 Lock lock_; |
| 381 | 393 |
| 382 // Stop signal to suppressing activities. This variable is set on the pipeline | 394 // Stop signal to suppressing activities. This variable is set on the pipeline |
| 383 // thread and read from the render thread. | 395 // thread and read from the render thread. |
| 384 bool stop_signal_received_; | 396 bool stop_signal_received_; |
| 385 | 397 |
| 386 // This variable is set by CleanupTask() that indicates this object is stopped | 398 // This variable is set by CleanupTask() that indicates this object is stopped |
| 387 // on the render thread. | 399 // on the render thread. |
| 388 bool stopped_on_render_loop_; | 400 bool stopped_on_render_loop_; |
| 389 | 401 |
| 402 // This variable is true when we are in a paused state and false when we |
| 403 // are in a playing state. |
| 404 bool media_is_paused_; |
| 405 |
| 390 // This timer is to run the WatchDogTask repeatedly. We use a timer instead | 406 // This timer is to run the WatchDogTask repeatedly. We use a timer instead |
| 391 // of doing PostDelayedTask() reduce the extra reference held by the message | 407 // of doing PostDelayedTask() reduce the extra reference held by the message |
| 392 // loop. The RepeatingTimer does PostDelayedTask() internally, by using it | 408 // loop. The RepeatingTimer does PostDelayedTask() internally, by using it |
| 393 // the message loop doesn't hold a reference for the watch dog task. | 409 // the message loop doesn't hold a reference for the watch dog task. |
| 394 base::RepeatingTimer<BufferedDataSource> watch_dog_timer_; | 410 base::RepeatingTimer<BufferedDataSource> watch_dog_timer_; |
| 395 | 411 |
| 396 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 412 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 397 }; | 413 }; |
| 398 | 414 |
| 399 } // namespace webkit_glue | 415 } // namespace webkit_glue |
| 400 | 416 |
| 401 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ | 417 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| OLD | NEW |