| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef WEBKIT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ | |
| 6 #define WEBKIT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "googleurl/src/gurl.h" | |
| 14 #include "media/base/data_source.h" | |
| 15 #include "media/base/ranges.h" | |
| 16 #include "webkit/renderer/media/buffered_resource_loader.h" | |
| 17 #include "webkit/renderer/media/preload.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class MessageLoopProxy; | |
| 21 } | |
| 22 | |
| 23 namespace media { | |
| 24 class MediaLog; | |
| 25 } | |
| 26 | |
| 27 namespace webkit_media { | |
| 28 | |
| 29 // A data source capable of loading URLs and buffering the data using an | |
| 30 // in-memory sliding window. | |
| 31 // | |
| 32 // BufferedDataSource must be created and initialized on the render thread | |
| 33 // before being passed to other threads. It may be deleted on any thread. | |
| 34 class BufferedDataSource : public media::DataSource { | |
| 35 public: | |
| 36 typedef base::Callback<void(bool)> DownloadingCB; | |
| 37 | |
| 38 // |downloading_cb| will be called whenever the downloading/paused state of | |
| 39 // the source changes. | |
| 40 BufferedDataSource(const scoped_refptr<base::MessageLoopProxy>& render_loop, | |
| 41 WebKit::WebFrame* frame, | |
| 42 media::MediaLog* media_log, | |
| 43 const DownloadingCB& downloading_cb); | |
| 44 virtual ~BufferedDataSource(); | |
| 45 | |
| 46 // Initialize this object using |url| and |cors_mode|, executing |init_cb| | |
| 47 // with the result of initialization when it has completed. | |
| 48 // | |
| 49 // Method called on the render thread. | |
| 50 typedef base::Callback<void(bool)> InitializeCB; | |
| 51 void Initialize( | |
| 52 const GURL& url, | |
| 53 BufferedResourceLoader::CORSMode cors_mode, | |
| 54 const InitializeCB& init_cb); | |
| 55 | |
| 56 // Adjusts the buffering algorithm based on the given preload value. | |
| 57 void SetPreload(Preload preload); | |
| 58 | |
| 59 // Returns true if the media resource has a single origin, false otherwise. | |
| 60 // Only valid to call after Initialize() has completed. | |
| 61 // | |
| 62 // Method called on the render thread. | |
| 63 bool HasSingleOrigin(); | |
| 64 | |
| 65 // Returns true if the media resource passed a CORS access control check. | |
| 66 bool DidPassCORSAccessCheck() const; | |
| 67 | |
| 68 // Cancels initialization, any pending loaders, and any pending read calls | |
| 69 // from the demuxer. The caller is expected to release its reference to this | |
| 70 // object and never call it again. | |
| 71 // | |
| 72 // Method called on the render thread. | |
| 73 void Abort(); | |
| 74 | |
| 75 // media::DataSource implementation. | |
| 76 // Called from demuxer thread. | |
| 77 virtual void set_host(media::DataSourceHost* host) OVERRIDE; | |
| 78 virtual void Stop(const base::Closure& closure) OVERRIDE; | |
| 79 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | |
| 80 | |
| 81 virtual void Read(int64 position, int size, uint8* data, | |
| 82 const media::DataSource::ReadCB& read_cb) OVERRIDE; | |
| 83 virtual bool GetSize(int64* size_out) OVERRIDE; | |
| 84 virtual bool IsStreaming() OVERRIDE; | |
| 85 virtual void SetBitrate(int bitrate) OVERRIDE; | |
| 86 | |
| 87 protected: | |
| 88 // A factory method to create a BufferedResourceLoader based on the read | |
| 89 // parameters. We can override this file to object a mock | |
| 90 // BufferedResourceLoader for testing. | |
| 91 virtual BufferedResourceLoader* CreateResourceLoader( | |
| 92 int64 first_byte_position, int64 last_byte_position); | |
| 93 | |
| 94 private: | |
| 95 friend class BufferedDataSourceTest; | |
| 96 | |
| 97 // Task posted to perform actual reading on the render thread. | |
| 98 void ReadTask(); | |
| 99 | |
| 100 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call | |
| 101 // from any thread. | |
| 102 void StopInternal_Locked(); | |
| 103 | |
| 104 // Stops |loader_| if present. Used by Abort() and Stop(). | |
| 105 void StopLoader(); | |
| 106 | |
| 107 // This task uses the current playback rate with the previous playback rate | |
| 108 // to determine whether we are going from pause to play and play to pause, | |
| 109 // and signals the buffered resource loader accordingly. | |
| 110 void SetPlaybackRateTask(float playback_rate); | |
| 111 | |
| 112 // Tells |loader_| the bitrate of the media. | |
| 113 void SetBitrateTask(int bitrate); | |
| 114 | |
| 115 // The method that performs actual read. This method can only be executed on | |
| 116 // the render thread. | |
| 117 void ReadInternal(); | |
| 118 | |
| 119 // BufferedResourceLoader::Start() callback for initial load. | |
| 120 void StartCallback(BufferedResourceLoader::Status status); | |
| 121 | |
| 122 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., | |
| 123 // when accessing ranges that are outside initial buffered region). | |
| 124 void PartialReadStartCallback(BufferedResourceLoader::Status status); | |
| 125 | |
| 126 // BufferedResourceLoader callbacks. | |
| 127 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); | |
| 128 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state); | |
| 129 void ProgressCallback(int64 position); | |
| 130 | |
| 131 // Report a buffered byte range [start,end] or queue it for later | |
| 132 // reporting if set_host() hasn't been called yet. | |
| 133 void ReportOrQueueBufferedBytes(int64 start, int64 end); | |
| 134 | |
| 135 void UpdateHostState_Locked(); | |
| 136 | |
| 137 base::WeakPtrFactory<BufferedDataSource> weak_factory_; | |
| 138 base::WeakPtr<BufferedDataSource> weak_this_; | |
| 139 | |
| 140 // URL of the resource requested. | |
| 141 GURL url_; | |
| 142 // crossorigin attribute on the corresponding HTML media element, if any. | |
| 143 BufferedResourceLoader::CORSMode cors_mode_; | |
| 144 | |
| 145 // The total size of the resource. Set during StartCallback() if the size is | |
| 146 // known, otherwise it will remain kPositionNotSpecified until the size is | |
| 147 // determined by reaching EOF. | |
| 148 int64 total_bytes_; | |
| 149 | |
| 150 // Some resources are assumed to be fully buffered (i.e., file://) so we don't | |
| 151 // need to report what |loader_| has buffered. | |
| 152 bool assume_fully_buffered_; | |
| 153 | |
| 154 // This value will be true if this data source can only support streaming. | |
| 155 // i.e. range request is not supported. | |
| 156 bool streaming_; | |
| 157 | |
| 158 // A webframe for loading. | |
| 159 WebKit::WebFrame* frame_; | |
| 160 | |
| 161 // A resource loader for the media resource. | |
| 162 scoped_ptr<BufferedResourceLoader> loader_; | |
| 163 | |
| 164 // Callback method from the pipeline for initialization. | |
| 165 InitializeCB init_cb_; | |
| 166 | |
| 167 // Read parameters received from the Read() method call. Must be accessed | |
| 168 // under |lock_|. | |
| 169 class ReadOperation; | |
| 170 scoped_ptr<ReadOperation> read_op_; | |
| 171 | |
| 172 // This buffer is intermediate, we use it for BufferedResourceLoader to write | |
| 173 // to. And when read in BufferedResourceLoader is done, we copy data from | |
| 174 // this buffer to |read_buffer_|. The reason for an additional copy is that | |
| 175 // we don't own |read_buffer_|. But since the read operation is asynchronous, | |
| 176 // |read_buffer| can be destroyed at any time, so we only copy into | |
| 177 // |read_buffer| in the final step when it is safe. | |
| 178 // Memory is allocated for this member during initialization of this object | |
| 179 // because we want buffer to be passed into BufferedResourceLoader to be | |
| 180 // always non-null. And by initializing this member with a default size we can | |
| 181 // avoid creating zero-sized buffered if the first read has zero size. | |
| 182 scoped_ptr<uint8[]> intermediate_read_buffer_; | |
| 183 int intermediate_read_buffer_size_; | |
| 184 | |
| 185 // The message loop of the render thread. | |
| 186 const scoped_refptr<base::MessageLoopProxy> render_loop_; | |
| 187 | |
| 188 // Protects |stop_signal_received_| and |read_op_|. | |
| 189 base::Lock lock_; | |
| 190 | |
| 191 // Whether we've been told to stop via Abort() or Stop(). | |
| 192 bool stop_signal_received_; | |
| 193 | |
| 194 // This variable is true when the user has requested the video to play at | |
| 195 // least once. | |
| 196 bool media_has_played_; | |
| 197 | |
| 198 // This variable holds the value of the preload attribute for the video | |
| 199 // element. | |
| 200 Preload preload_; | |
| 201 | |
| 202 // Bitrate of the content, 0 if unknown. | |
| 203 int bitrate_; | |
| 204 | |
| 205 // Current playback rate. | |
| 206 float playback_rate_; | |
| 207 | |
| 208 // Buffered byte ranges awaiting set_host() being called to report to host(). | |
| 209 media::Ranges<int64> queued_buffered_byte_ranges_; | |
| 210 | |
| 211 scoped_refptr<media::MediaLog> media_log_; | |
| 212 | |
| 213 DownloadingCB downloading_cb_; | |
| 214 | |
| 215 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | |
| 216 }; | |
| 217 | |
| 218 } // namespace webkit_media | |
| 219 | |
| 220 #endif // WEBKIT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ | |
| OLD | NEW |