| 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 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 "media/base/data_source.h" | |
| 16 #include "media/base/ranges.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SingleThreadTaskRunner; | |
| 21 } | |
| 22 | |
| 23 namespace media { | |
| 24 class MediaLog; | |
| 25 } | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 class CONTENT_EXPORT BufferedDataSourceHost { | |
| 30 public: | |
| 31 // Notify the host of the total size of the media file. | |
| 32 virtual void SetTotalBytes(int64 total_bytes) = 0; | |
| 33 | |
| 34 // Notify the host that byte range [start,end] has been buffered. | |
| 35 // TODO(fischman): remove this method when demuxing is push-based instead of | |
| 36 // pull-based. http://crbug.com/131444 | |
| 37 virtual void AddBufferedByteRange(int64 start, int64 end) = 0; | |
| 38 | |
| 39 protected: | |
| 40 virtual ~BufferedDataSourceHost() {}; | |
| 41 }; | |
| 42 | |
| 43 // A data source capable of loading URLs and buffering the data using an | |
| 44 // in-memory sliding window. | |
| 45 // | |
| 46 // BufferedDataSource must be created and initialized on the render thread | |
| 47 // before being passed to other threads. It may be deleted on any thread. | |
| 48 class CONTENT_EXPORT BufferedDataSource : public media::DataSource { | |
| 49 public: | |
| 50 // Used to specify video preload states. They are "hints" to the browser about | |
| 51 // how aggressively the browser should load and buffer data. | |
| 52 // Please see the HTML5 spec for the descriptions of these values: | |
| 53 // http://www.w3.org/TR/html5/video.html#attr-media-preload | |
| 54 // | |
| 55 // Enum values must match the values in blink::WebMediaPlayer::Preload and | |
| 56 // there will be assertions at compile time if they do not match. | |
| 57 enum Preload { | |
| 58 NONE, | |
| 59 METADATA, | |
| 60 AUTO, | |
| 61 }; | |
| 62 typedef base::Callback<void(bool)> DownloadingCB; | |
| 63 | |
| 64 // |url| and |cors_mode| are passed to the object. Buffered byte range changes | |
| 65 // will be reported to |host|. |downloading_cb| will be called whenever the | |
| 66 // downloading/paused state of the source changes. | |
| 67 BufferedDataSource( | |
| 68 const GURL& url, | |
| 69 BufferedResourceLoader::CORSMode cors_mode, | |
| 70 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 71 blink::WebFrame* frame, | |
| 72 media::MediaLog* media_log, | |
| 73 BufferedDataSourceHost* host, | |
| 74 const DownloadingCB& downloading_cb); | |
| 75 virtual ~BufferedDataSource(); | |
| 76 | |
| 77 // Executes |init_cb| with the result of initialization when it has completed. | |
| 78 // | |
| 79 // Method called on the render thread. | |
| 80 typedef base::Callback<void(bool)> InitializeCB; | |
| 81 void Initialize(const InitializeCB& init_cb); | |
| 82 | |
| 83 // Adjusts the buffering algorithm based on the given preload value. | |
| 84 void SetPreload(Preload preload); | |
| 85 | |
| 86 // Returns true if the media resource has a single origin, false otherwise. | |
| 87 // Only valid to call after Initialize() has completed. | |
| 88 // | |
| 89 // Method called on the render thread. | |
| 90 bool HasSingleOrigin(); | |
| 91 | |
| 92 // Returns true if the media resource passed a CORS access control check. | |
| 93 bool DidPassCORSAccessCheck() const; | |
| 94 | |
| 95 // Cancels initialization, any pending loaders, and any pending read calls | |
| 96 // from the demuxer. The caller is expected to release its reference to this | |
| 97 // object and never call it again. | |
| 98 // | |
| 99 // Method called on the render thread. | |
| 100 void Abort(); | |
| 101 | |
| 102 // Notifies changes in playback state for controlling media buffering | |
| 103 // behavior. | |
| 104 void MediaPlaybackRateChanged(float playback_rate); | |
| 105 void MediaIsPlaying(); | |
| 106 void MediaIsPaused(); | |
| 107 | |
| 108 // Returns true if the resource is local. | |
| 109 bool assume_fully_buffered() { return !url_.SchemeIsHTTPOrHTTPS(); } | |
| 110 | |
| 111 // media::DataSource implementation. | |
| 112 // Called from demuxer thread. | |
| 113 virtual void Stop() OVERRIDE; | |
| 114 | |
| 115 virtual void Read(int64 position, int size, uint8* data, | |
| 116 const media::DataSource::ReadCB& read_cb) OVERRIDE; | |
| 117 virtual bool GetSize(int64* size_out) OVERRIDE; | |
| 118 virtual bool IsStreaming() OVERRIDE; | |
| 119 virtual void SetBitrate(int bitrate) OVERRIDE; | |
| 120 | |
| 121 protected: | |
| 122 // A factory method to create a BufferedResourceLoader based on the read | |
| 123 // parameters. We can override this file to object a mock | |
| 124 // BufferedResourceLoader for testing. | |
| 125 virtual BufferedResourceLoader* CreateResourceLoader( | |
| 126 int64 first_byte_position, int64 last_byte_position); | |
| 127 | |
| 128 private: | |
| 129 friend class BufferedDataSourceTest; | |
| 130 | |
| 131 // Task posted to perform actual reading on the render thread. | |
| 132 void ReadTask(); | |
| 133 | |
| 134 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call | |
| 135 // from any thread. | |
| 136 void StopInternal_Locked(); | |
| 137 | |
| 138 // Stops |loader_| if present. Used by Abort() and Stop(). | |
| 139 void StopLoader(); | |
| 140 | |
| 141 // Tells |loader_| the bitrate of the media. | |
| 142 void SetBitrateTask(int bitrate); | |
| 143 | |
| 144 // The method that performs actual read. This method can only be executed on | |
| 145 // the render thread. | |
| 146 void ReadInternal(); | |
| 147 | |
| 148 // BufferedResourceLoader::Start() callback for initial load. | |
| 149 void StartCallback(BufferedResourceLoader::Status status); | |
| 150 | |
| 151 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., | |
| 152 // when accessing ranges that are outside initial buffered region). | |
| 153 void PartialReadStartCallback(BufferedResourceLoader::Status status); | |
| 154 | |
| 155 // BufferedResourceLoader callbacks. | |
| 156 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); | |
| 157 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state); | |
| 158 void ProgressCallback(int64 position); | |
| 159 | |
| 160 // Update |loader_|'s deferring strategy in response to a play/pause, or | |
| 161 // change in playback rate. | |
| 162 void UpdateDeferStrategy(bool paused); | |
| 163 | |
| 164 // URL of the resource requested. | |
| 165 GURL url_; | |
| 166 // crossorigin attribute on the corresponding HTML media element, if any. | |
| 167 BufferedResourceLoader::CORSMode cors_mode_; | |
| 168 | |
| 169 // The total size of the resource. Set during StartCallback() if the size is | |
| 170 // known, otherwise it will remain kPositionNotSpecified until the size is | |
| 171 // determined by reaching EOF. | |
| 172 int64 total_bytes_; | |
| 173 | |
| 174 // This value will be true if this data source can only support streaming. | |
| 175 // i.e. range request is not supported. | |
| 176 bool streaming_; | |
| 177 | |
| 178 // A webframe for loading. | |
| 179 blink::WebFrame* frame_; | |
| 180 | |
| 181 // A resource loader for the media resource. | |
| 182 scoped_ptr<BufferedResourceLoader> loader_; | |
| 183 | |
| 184 // Callback method from the pipeline for initialization. | |
| 185 InitializeCB init_cb_; | |
| 186 | |
| 187 // Read parameters received from the Read() method call. Must be accessed | |
| 188 // under |lock_|. | |
| 189 class ReadOperation; | |
| 190 scoped_ptr<ReadOperation> read_op_; | |
| 191 | |
| 192 // This buffer is intermediate, we use it for BufferedResourceLoader to write | |
| 193 // to. And when read in BufferedResourceLoader is done, we copy data from | |
| 194 // this buffer to |read_buffer_|. The reason for an additional copy is that | |
| 195 // we don't own |read_buffer_|. But since the read operation is asynchronous, | |
| 196 // |read_buffer| can be destroyed at any time, so we only copy into | |
| 197 // |read_buffer| in the final step when it is safe. | |
| 198 // Memory is allocated for this member during initialization of this object | |
| 199 // because we want buffer to be passed into BufferedResourceLoader to be | |
| 200 // always non-null. And by initializing this member with a default size we can | |
| 201 // avoid creating zero-sized buffered if the first read has zero size. | |
| 202 scoped_ptr<uint8[]> intermediate_read_buffer_; | |
| 203 int intermediate_read_buffer_size_; | |
| 204 | |
| 205 // The task runner of the render thread. | |
| 206 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_; | |
| 207 | |
| 208 // Protects |stop_signal_received_| and |read_op_|. | |
| 209 base::Lock lock_; | |
| 210 | |
| 211 // Whether we've been told to stop via Abort() or Stop(). | |
| 212 bool stop_signal_received_; | |
| 213 | |
| 214 // This variable is true when the user has requested the video to play at | |
| 215 // least once. | |
| 216 bool media_has_played_; | |
| 217 | |
| 218 // This variable holds the value of the preload attribute for the video | |
| 219 // element. | |
| 220 Preload preload_; | |
| 221 | |
| 222 // Bitrate of the content, 0 if unknown. | |
| 223 int bitrate_; | |
| 224 | |
| 225 // Current playback rate. | |
| 226 float playback_rate_; | |
| 227 | |
| 228 scoped_refptr<media::MediaLog> media_log_; | |
| 229 | |
| 230 // Host object to report buffered byte range changes to. | |
| 231 BufferedDataSourceHost* host_; | |
| 232 | |
| 233 DownloadingCB downloading_cb_; | |
| 234 | |
| 235 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 236 base::WeakPtrFactory<BufferedDataSource> weak_factory_; | |
| 237 | |
| 238 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | |
| 239 }; | |
| 240 | |
| 241 } // namespace content | |
| 242 | |
| 243 #endif // CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ | |
| OLD | NEW |