| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MEDIA_BUFFERED_DATA_SOURCE_H_ | 5 #ifndef WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| 6 #define WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ | 6 #define WEBKIT_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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "media/base/data_source.h" | 14 #include "media/base/data_source.h" |
| 15 #include "media/base/pipeline_status.h" | 15 #include "media/base/pipeline_status.h" |
| 16 #include "webkit/media/buffered_resource_loader.h" | 16 #include "webkit/media/buffered_resource_loader.h" |
| 17 #include "webkit/media/preload.h" |
| 17 | 18 |
| 18 class MessageLoop; | 19 class MessageLoop; |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 class MediaLog; | 22 class MediaLog; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace webkit_media { | 25 namespace webkit_media { |
| 25 | 26 |
| 26 // A data source capable of loading URLs and buffering the data using an | 27 // A data source capable of loading URLs and buffering the data using an |
| 27 // in-memory sliding window. | 28 // in-memory sliding window. |
| 28 // | 29 // |
| 29 // BufferedDataSource must be created and initialized on the render thread | 30 // BufferedDataSource must be created and initialized on the render thread |
| 30 // before being passed to other threads. It may be deleted on any thread. | 31 // before being passed to other threads. It may be deleted on any thread. |
| 31 class BufferedDataSource : public media::DataSource { | 32 class BufferedDataSource : public media::DataSource { |
| 32 public: | 33 public: |
| 33 BufferedDataSource(MessageLoop* render_loop, | 34 BufferedDataSource(MessageLoop* render_loop, |
| 34 WebKit::WebFrame* frame, | 35 WebKit::WebFrame* frame, |
| 35 media::MediaLog* media_log); | 36 media::MediaLog* media_log); |
| 36 | 37 |
| 37 virtual ~BufferedDataSource(); | 38 virtual ~BufferedDataSource(); |
| 38 | 39 |
| 39 // Initialize this object using |url|. This object calls |status_cb| when | 40 // Initialize this object using |url|. This object calls |status_cb| when |
| 40 // initialization has completed. | 41 // initialization has completed. |
| 41 // | 42 // |
| 42 // Method called on the render thread. | 43 // Method called on the render thread. |
| 43 void Initialize(const GURL& url, | 44 void Initialize(const GURL& url, |
| 44 const media::PipelineStatusCB& status_cb); | 45 const media::PipelineStatusCB& status_cb); |
| 45 | 46 |
| 47 // Adjusts the buffering algorithm based on the given preload value. |
| 48 void SetPreload(Preload preload); |
| 49 |
| 46 // Returns true if the media resource has a single origin, false otherwise. | 50 // Returns true if the media resource has a single origin, false otherwise. |
| 47 // Only valid to call after Initialize() has completed. | 51 // Only valid to call after Initialize() has completed. |
| 48 // | 52 // |
| 49 // Method called on the render thread. | 53 // Method called on the render thread. |
| 50 bool HasSingleOrigin(); | 54 bool HasSingleOrigin(); |
| 51 | 55 |
| 52 // Cancels initialization, any pending loaders, and any pending read calls | 56 // Cancels initialization, any pending loaders, and any pending read calls |
| 53 // from the demuxer. The caller is expected to release its reference to this | 57 // from the demuxer. The caller is expected to release its reference to this |
| 54 // object and never call it again. | 58 // object and never call it again. |
| 55 // | 59 // |
| 56 // Method called on the render thread. | 60 // Method called on the render thread. |
| 57 void Abort(); | 61 void Abort(); |
| 58 | 62 |
| 59 // media::DataSource implementation. | 63 // media::DataSource implementation. |
| 60 // Called from demuxer thread. | 64 // Called from demuxer thread. |
| 61 virtual void set_host(media::DataSourceHost* host) OVERRIDE; | 65 virtual void set_host(media::DataSourceHost* host) OVERRIDE; |
| 62 virtual void Stop(const base::Closure& closure) OVERRIDE; | 66 virtual void Stop(const base::Closure& closure) OVERRIDE; |
| 63 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 67 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
| 64 | 68 |
| 65 virtual void Read(int64 position, int size, uint8* data, | 69 virtual void Read(int64 position, int size, uint8* data, |
| 66 const media::DataSource::ReadCB& read_cb) OVERRIDE; | 70 const media::DataSource::ReadCB& read_cb) OVERRIDE; |
| 67 virtual bool GetSize(int64* size_out) OVERRIDE; | 71 virtual bool GetSize(int64* size_out) OVERRIDE; |
| 68 virtual bool IsStreaming() OVERRIDE; | 72 virtual bool IsStreaming() OVERRIDE; |
| 69 virtual void SetPreload(media::Preload preload) OVERRIDE; | |
| 70 virtual void SetBitrate(int bitrate) OVERRIDE; | 73 virtual void SetBitrate(int bitrate) OVERRIDE; |
| 71 | 74 |
| 72 protected: | 75 protected: |
| 73 // A factory method to create a BufferedResourceLoader based on the read | 76 // A factory method to create a BufferedResourceLoader based on the read |
| 74 // parameters. We can override this file to object a mock | 77 // parameters. We can override this file to object a mock |
| 75 // BufferedResourceLoader for testing. | 78 // BufferedResourceLoader for testing. |
| 76 virtual BufferedResourceLoader* CreateResourceLoader( | 79 virtual BufferedResourceLoader* CreateResourceLoader( |
| 77 int64 first_byte_position, int64 last_byte_position); | 80 int64 first_byte_position, int64 last_byte_position); |
| 78 | 81 |
| 79 private: | 82 private: |
| 80 friend class BufferedDataSourceTest; | 83 friend class BufferedDataSourceTest; |
| 81 | 84 |
| 82 // Task posted to perform actual reading on the render thread. | 85 // Task posted to perform actual reading on the render thread. |
| 83 void ReadTask(int64 position, int read_size, uint8* read_buffer); | 86 void ReadTask(int64 position, int read_size, uint8* read_buffer); |
| 84 | 87 |
| 85 // Task posted when Stop() is called. Stops |watch_dog_timer_| and | 88 // Task posted when Stop() is called. Stops |watch_dog_timer_| and |
| 86 // |loader_|, reset Read() variables, and set |stopped_on_render_loop_| | 89 // |loader_|, reset Read() variables, and set |stopped_on_render_loop_| |
| 87 // to signal any remaining tasks to stop. | 90 // to signal any remaining tasks to stop. |
| 88 void CleanupTask(); | 91 void CleanupTask(); |
| 89 | 92 |
| 90 // Restart resource loading on render thread. | 93 // Restart resource loading on render thread. |
| 91 void RestartLoadingTask(); | 94 void RestartLoadingTask(); |
| 92 | 95 |
| 93 // This task uses the current playback rate with the previous playback rate | 96 // This task uses the current playback rate with the previous playback rate |
| 94 // to determine whether we are going from pause to play and play to pause, | 97 // to determine whether we are going from pause to play and play to pause, |
| 95 // and signals the buffered resource loader accordingly. | 98 // and signals the buffered resource loader accordingly. |
| 96 void SetPlaybackRateTask(float playback_rate); | 99 void SetPlaybackRateTask(float playback_rate); |
| 97 | 100 |
| 98 // This task saves the preload value for the media. | |
| 99 void SetPreloadTask(media::Preload preload); | |
| 100 | |
| 101 // Tells |loader_| the bitrate of the media. | 101 // Tells |loader_| the bitrate of the media. |
| 102 void SetBitrateTask(int bitrate); | 102 void SetBitrateTask(int bitrate); |
| 103 | 103 |
| 104 // Decides which DeferStrategy to used based on the current state of the | 104 // Decides which DeferStrategy to used based on the current state of the |
| 105 // BufferedDataSource. | 105 // BufferedDataSource. |
| 106 BufferedResourceLoader::DeferStrategy ChooseDeferStrategy(); | 106 BufferedResourceLoader::DeferStrategy ChooseDeferStrategy(); |
| 107 | 107 |
| 108 // The method that performs actual read. This method can only be executed on | 108 // The method that performs actual read. This method can only be executed on |
| 109 // the render thread. | 109 // the render thread. |
| 110 void ReadInternal(); | 110 void ReadInternal(); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 // This variable is true when we are in a paused state and false when we | 204 // This variable is true when we are in a paused state and false when we |
| 205 // are in a playing state. | 205 // are in a playing state. |
| 206 bool media_is_paused_; | 206 bool media_is_paused_; |
| 207 | 207 |
| 208 // This variable is true when the user has requested the video to play at | 208 // This variable is true when the user has requested the video to play at |
| 209 // least once. | 209 // least once. |
| 210 bool media_has_played_; | 210 bool media_has_played_; |
| 211 | 211 |
| 212 // This variable holds the value of the preload attribute for the video | 212 // This variable holds the value of the preload attribute for the video |
| 213 // element. | 213 // element. |
| 214 media::Preload preload_; | 214 Preload preload_; |
| 215 | 215 |
| 216 // Number of cache miss retries left. | 216 // Number of cache miss retries left. |
| 217 int cache_miss_retries_left_; | 217 int cache_miss_retries_left_; |
| 218 | 218 |
| 219 // Bitrate of the content, 0 if unknown. | 219 // Bitrate of the content, 0 if unknown. |
| 220 int bitrate_; | 220 int bitrate_; |
| 221 | 221 |
| 222 // Current playback rate. | 222 // Current playback rate. |
| 223 float playback_rate_; | 223 float playback_rate_; |
| 224 | 224 |
| 225 scoped_refptr<media::MediaLog> media_log_; | 225 scoped_refptr<media::MediaLog> media_log_; |
| 226 | 226 |
| 227 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 227 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 } // namespace webkit_media | 230 } // namespace webkit_media |
| 231 | 231 |
| 232 #endif // WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ | 232 #endif // WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| OLD | NEW |