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