Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ | 5 #ifndef MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_ |
| 6 #define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ | 6 #define MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "media/base/data_source.h" | 15 #include "media/base/data_source.h" |
| 16 #include "media/base/media_export.h" | 16 #include "media/base/media_export.h" |
| 17 #include "media/base/ranges.h" | 17 #include "media/base/ranges.h" |
| 18 #include "media/blink/buffered_resource_loader.h" | 18 #include "media/blink/buffered_data_source.h" |
| 19 #include "media/blink/multibuffer_resource_loader.h" | |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace media { | 26 namespace media { |
| 26 class MediaLog; | 27 class MediaLog; |
| 27 | 28 class MultiBufferReader; |
| 28 class MEDIA_EXPORT BufferedDataSourceHost { | |
| 29 public: | |
| 30 // Notify the host of the total size of the media file. | |
| 31 virtual void SetTotalBytes(int64 total_bytes) = 0; | |
| 32 | |
| 33 // Notify the host that byte range [start,end] has been buffered. | |
| 34 // TODO(fischman): remove this method when demuxing is push-based instead of | |
| 35 // pull-based. http://crbug.com/131444 | |
| 36 virtual void AddBufferedByteRange(int64 start, int64 end) = 0; | |
| 37 | |
| 38 protected: | |
| 39 virtual ~BufferedDataSourceHost() {} | |
| 40 }; | |
| 41 | 29 |
| 42 // A data source capable of loading URLs and buffering the data using an | 30 // A data source capable of loading URLs and buffering the data using an |
| 43 // in-memory sliding window. | 31 // in-memory sliding window. |
| 44 // | 32 // |
| 45 // BufferedDataSource must be created and destroyed on the thread associated | 33 // MultibufferDataSource must be created and destroyed on the thread associated |
| 46 // with the |task_runner| passed in the constructor. | 34 // with the |task_runner| passed in the constructor. |
| 47 class MEDIA_EXPORT BufferedDataSource : public DataSource { | 35 class MEDIA_EXPORT MultibufferDataSource : public BufferedDataSourceInterface { |
| 48 public: | 36 public: |
| 49 // Used to specify video preload states. They are "hints" to the browser about | |
| 50 // how aggressively the browser should load and buffer data. | |
| 51 // Please see the HTML5 spec for the descriptions of these values: | |
| 52 // http://www.w3.org/TR/html5/video.html#attr-media-preload | |
| 53 // | |
| 54 // Enum values must match the values in blink::WebMediaPlayer::Preload and | |
| 55 // there will be assertions at compile time if they do not match. | |
| 56 enum Preload { | |
| 57 NONE, | |
| 58 METADATA, | |
| 59 AUTO, | |
| 60 }; | |
| 61 typedef base::Callback<void(bool)> DownloadingCB; | 37 typedef base::Callback<void(bool)> DownloadingCB; |
| 62 | 38 |
| 63 // |url| and |cors_mode| are passed to the object. Buffered byte range changes | 39 // |url| and |cors_mode| are passed to the object. Buffered byte range changes |
| 64 // will be reported to |host|. |downloading_cb| will be called whenever the | 40 // will be reported to |host|. |downloading_cb| will be called whenever the |
| 65 // downloading/paused state of the source changes. | 41 // downloading/paused state of the source changes. |
| 66 BufferedDataSource( | 42 MultibufferDataSource( |
| 67 const GURL& url, | 43 const GURL& url, |
| 68 BufferedResourceLoader::CORSMode cors_mode, | 44 UrlData::CORSMode cors_mode, |
| 69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 46 ResourceMultiBuffer* multibuffer, | |
| 70 blink::WebFrame* frame, | 47 blink::WebFrame* frame, |
| 71 MediaLog* media_log, | 48 MediaLog* media_log, |
| 72 BufferedDataSourceHost* host, | 49 BufferedDataSourceHost* host, |
| 73 const DownloadingCB& downloading_cb); | 50 const DownloadingCB& downloading_cb); |
| 74 ~BufferedDataSource() override; | 51 ~MultibufferDataSource() override; |
| 75 | 52 |
| 76 // Executes |init_cb| with the result of initialization when it has completed. | 53 // Executes |init_cb| with the result of initialization when it has completed. |
| 77 // | 54 // |
| 78 // Method called on the render thread. | 55 // Method called on the render thread. |
| 79 typedef base::Callback<void(bool)> InitializeCB; | 56 void Initialize(const InitializeCB& init_cb) override; |
| 80 void Initialize(const InitializeCB& init_cb); | |
| 81 | 57 |
| 82 // Adjusts the buffering algorithm based on the given preload value. | 58 // Adjusts the buffering algorithm based on the given preload value. |
| 83 void SetPreload(Preload preload); | 59 void SetPreload(Preload preload) override; |
| 84 | 60 |
| 85 // Returns true if the media resource has a single origin, false otherwise. | 61 // Returns true if the media resource has a single origin, false otherwise. |
| 86 // Only valid to call after Initialize() has completed. | 62 // Only valid to call after Initialize() has completed. |
| 87 // | 63 // |
| 88 // Method called on the render thread. | 64 // Method called on the render thread. |
| 89 bool HasSingleOrigin(); | 65 bool HasSingleOrigin() override; |
| 90 | 66 |
| 91 // Returns true if the media resource passed a CORS access control check. | 67 // Returns true if the media resource passed a CORS access control check. |
| 92 bool DidPassCORSAccessCheck() const; | 68 bool DidPassCORSAccessCheck() const override; |
| 93 | 69 |
| 94 // Cancels initialization, any pending loaders, and any pending read calls | 70 // Cancels initialization, any pending loaders, and any pending read calls |
| 95 // from the demuxer. The caller is expected to release its reference to this | 71 // from the demuxer. The caller is expected to release its reference to this |
| 96 // object and never call it again. | 72 // object and never call it again. |
| 97 // | 73 // |
| 98 // Method called on the render thread. | 74 // Method called on the render thread. |
| 99 void Abort(); | 75 void Abort() override; |
| 100 | 76 |
| 101 // Notifies changes in playback state for controlling media buffering | 77 // Notifies changes in playback state for controlling media buffering |
| 102 // behavior. | 78 // behavior. |
| 103 void MediaPlaybackRateChanged(double playback_rate); | 79 void MediaPlaybackRateChanged(double playback_rate) override; |
| 104 void MediaIsPlaying(); | 80 void MediaIsPlaying() override; |
| 105 void MediaIsPaused(); | 81 void MediaIsPaused() override; |
| 106 bool media_has_played() const { return media_has_played_; } | 82 bool media_has_played() const override; |
| 107 | 83 |
| 108 // Returns true if the resource is local. | 84 // Returns true if the resource is local. |
| 109 bool assume_fully_buffered() { return !url_.SchemeIsHTTPOrHTTPS(); } | 85 bool assume_fully_buffered() override; |
| 110 | 86 |
| 111 // Cancels any open network connections once reaching the deferred state for | 87 // Cancels any open network connections once reaching the deferred state for |
| 112 // preload=metadata, non-streaming resources that have not started playback. | 88 // preload=metadata, non-streaming resources that have not started playback. |
| 113 // If already deferred, connections will be immediately closed. | 89 // If already deferred, connections will be immediately closed. |
| 114 void OnBufferingHaveEnough(); | 90 void OnBufferingHaveEnough() override; |
| 115 | 91 |
| 116 // DataSource implementation. | 92 // DataSource implementation. |
| 117 // Called from demuxer thread. | 93 // Called from demuxer thread. |
| 118 void Stop() override; | 94 void Stop() override; |
| 119 | 95 |
| 120 void Read(int64 position, | 96 void Read(int64 position, |
| 121 int size, | 97 int size, |
| 122 uint8* data, | 98 uint8* data, |
| 123 const DataSource::ReadCB& read_cb) override; | 99 const DataSource::ReadCB& read_cb) override; |
| 124 bool GetSize(int64* size_out) override; | 100 bool GetSize(int64* size_out) override; |
| 125 bool IsStreaming() override; | 101 bool IsStreaming() override; |
| 126 void SetBitrate(int bitrate) override; | 102 void SetBitrate(int bitrate) override; |
| 127 | 103 |
| 128 protected: | 104 protected: |
| 129 // A factory method to create a BufferedResourceLoader based on the read | 105 // A factory method to create a BufferedResourceLoader based on the read |
| 130 // parameters. We can override this file to object a mock | 106 // parameters. |
| 131 // BufferedResourceLoader for testing. | 107 void CreateResourceLoader(int64 first_byte_position, |
| 132 virtual BufferedResourceLoader* CreateResourceLoader( | 108 int64 last_byte_position); |
| 133 int64 first_byte_position, int64 last_byte_position); | |
| 134 | 109 |
| 135 private: | 110 friend class MultibufferDataSourceTest; |
| 136 friend class BufferedDataSourceTest; | |
| 137 | 111 |
| 138 // Task posted to perform actual reading on the render thread. | 112 // Task posted to perform actual reading on the render thread. |
| 139 void ReadTask(); | 113 void ReadTask(); |
| 140 | 114 |
| 141 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call | 115 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call |
| 142 // from any thread. | 116 // from any thread. |
| 143 void StopInternal_Locked(); | 117 void StopInternal_Locked(); |
| 144 | 118 |
| 145 // Stops |loader_| if present. Used by Abort() and Stop(). | 119 // Stops |loader_| if present. Used by Abort() and Stop(). |
| 146 void StopLoader(); | 120 void StopLoader(); |
| 147 | 121 |
| 148 // Tells |loader_| the bitrate of the media. | 122 // Tells |loader_| the bitrate of the media. |
| 149 void SetBitrateTask(int bitrate); | 123 void SetBitrateTask(int bitrate); |
| 150 | 124 |
| 151 // The method that performs actual read. This method can only be executed on | 125 // BufferedResourceLoader::Start() callback for initial load. |
| 152 // the render thread. | 126 void StartCallback(); |
| 153 void ReadInternal(); | |
| 154 | 127 |
| 155 // BufferedResourceLoader::Start() callback for initial load. | 128 // Check if we've moved to a new url and update has_signgle_origin_. |
|
liberato (no reviews please)
2015/10/16 21:50:36
s/igng/ing/
| |
| 156 void StartCallback(BufferedResourceLoader::Status status); | 129 void UpdateSingleOrigin(); |
| 157 | 130 |
| 158 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., | 131 // MultiBufferReader progress callback. |
| 159 // when accessing ranges that are outside initial buffered region). | 132 void ProgressCallback(int64 begin, int64 end); |
| 160 void PartialReadStartCallback(BufferedResourceLoader::Status status); | |
| 161 | 133 |
| 162 // Returns true if we can accept the new partial response. | 134 // call downloading_cb_ if needed. |
| 163 bool CheckPartialResponseURL(const GURL& partial_response_original_url) const; | 135 void UpdateLoadingState(); |
| 164 | 136 |
| 165 // BufferedResourceLoader callbacks. | 137 // Update |loader_|'s preload and buffer settings. |
| 166 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); | 138 void UpdateBufferSizes(); |
| 167 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state); | |
| 168 void ProgressCallback(int64 position); | |
| 169 | 139 |
| 170 // Update |loader_|'s deferring strategy in response to a play/pause, or | 140 // crossorigin attribute on the corresponding HTML media element, if any. |
| 171 // change in playback rate. | 141 UrlData::CORSMode cors_mode_; |
| 172 void UpdateDeferStrategy(bool paused); | |
| 173 | 142 |
| 174 // URL of the resource requested. | 143 // URL of the resource requested. |
| 175 GURL url_; | 144 scoped_refptr<UrlData> url_data_; |
| 176 // crossorigin attribute on the corresponding HTML media element, if any. | 145 scoped_refptr<UrlData> destination_url_data_; |
| 177 BufferedResourceLoader::CORSMode cors_mode_; | |
| 178 | 146 |
| 179 // The total size of the resource. Set during StartCallback() if the size is | 147 // The total size of the resource. Set during StartCallback() if the size is |
| 180 // known, otherwise it will remain kPositionNotSpecified until the size is | 148 // known, otherwise it will remain kPositionNotSpecified until the size is |
| 181 // determined by reaching EOF. | 149 // determined by reaching EOF. |
| 182 int64 total_bytes_; | 150 int64 total_bytes_; |
| 183 | 151 |
| 184 // This value will be true if this data source can only support streaming. | 152 // This value will be true if this data source can only support streaming. |
| 185 // i.e. range request is not supported. | 153 // i.e. range request is not supported. |
| 186 bool streaming_; | 154 bool streaming_; |
| 187 | 155 |
| 156 bool loading_; | |
| 157 | |
| 158 // The task runner of the render thread. | |
| 159 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_; | |
| 160 | |
| 161 // Shared buffer. | |
| 162 ResourceMultiBuffer* multibuffer_; | |
| 163 | |
| 188 // A webframe for loading. | 164 // A webframe for loading. |
| 189 blink::WebFrame* frame_; | 165 blink::WebFrame* frame_; |
| 190 | 166 |
| 191 // A resource loader for the media resource. | 167 // A resource reader for the media resource. |
| 192 scoped_ptr<BufferedResourceLoader> loader_; | 168 scoped_ptr<MultiBufferReader> loader_; |
|
liberato (no reviews please)
2015/10/16 21:50:36
|reader_|?
hubbe
2015/10/16 23:47:05
Done.
| |
| 193 | 169 |
| 194 // Callback method from the pipeline for initialization. | 170 // Callback method from the pipeline for initialization. |
| 195 InitializeCB init_cb_; | 171 InitializeCB init_cb_; |
| 196 | 172 |
| 197 // Read parameters received from the Read() method call. Must be accessed | 173 // Read parameters received from the Read() method call. Must be accessed |
| 198 // under |lock_|. | 174 // under |lock_|. |
| 199 class ReadOperation; | 175 class ReadOperation; |
| 200 scoped_ptr<ReadOperation> read_op_; | 176 scoped_ptr<ReadOperation> read_op_; |
| 201 | 177 |
| 202 // This buffer is intermediate, we use it for BufferedResourceLoader to write | |
| 203 // to. And when read in BufferedResourceLoader is done, we copy data from | |
| 204 // this buffer to |read_buffer_|. The reason for an additional copy is that | |
| 205 // we don't own |read_buffer_|. But since the read operation is asynchronous, | |
| 206 // |read_buffer| can be destroyed at any time, so we only copy into | |
| 207 // |read_buffer| in the final step when it is safe. | |
| 208 // Memory is allocated for this member during initialization of this object | |
| 209 // because we want buffer to be passed into BufferedResourceLoader to be | |
| 210 // always non-null. And by initializing this member with a default size we can | |
| 211 // avoid creating zero-sized buffered if the first read has zero size. | |
| 212 std::vector<uint8> intermediate_read_buffer_; | |
| 213 | |
| 214 // The task runner of the render thread. | |
| 215 const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_; | |
| 216 | |
| 217 // Protects |stop_signal_received_| and |read_op_|. | 178 // Protects |stop_signal_received_| and |read_op_|. |
| 218 base::Lock lock_; | 179 base::Lock lock_; |
| 219 | 180 |
| 220 // Whether we've been told to stop via Abort() or Stop(). | 181 // Whether we've been told to stop via Abort() or Stop(). |
| 221 bool stop_signal_received_; | 182 bool stop_signal_received_; |
| 222 | 183 |
| 223 // This variable is true when the user has requested the video to play at | 184 // This variable is true when the user has requested the video to play at |
| 224 // least once. | 185 // least once. |
| 225 bool media_has_played_; | 186 bool media_has_played_; |
| 226 | 187 |
| 188 // Are we currently paused. | |
| 189 bool paused_; | |
| 190 | |
| 191 // As we follow redirects, we set this variable to false if redirects | |
| 192 // go between different origins. | |
| 193 bool single_origin_; | |
| 194 | |
| 195 // Close the connection when we have enough data. | |
| 196 bool cancel_on_defer_; | |
| 197 | |
| 227 // This variable holds the value of the preload attribute for the video | 198 // This variable holds the value of the preload attribute for the video |
| 228 // element. | 199 // element. |
| 229 Preload preload_; | 200 Preload preload_; |
| 230 | 201 |
| 231 // Bitrate of the content, 0 if unknown. | 202 // Bitrate of the content, 0 if unknown. |
| 232 int bitrate_; | 203 int bitrate_; |
| 233 | 204 |
| 234 // Current playback rate. | 205 // Current playback rate. |
| 235 double playback_rate_; | 206 double playback_rate_; |
| 236 | 207 |
| 237 scoped_refptr<MediaLog> media_log_; | 208 scoped_refptr<MediaLog> media_log_; |
| 238 | 209 |
| 239 // Host object to report buffered byte range changes to. | 210 // Host object to report buffered byte range changes to. |
| 240 BufferedDataSourceHost* host_; | 211 BufferedDataSourceHost* host_; |
| 241 | 212 |
| 242 DownloadingCB downloading_cb_; | 213 DownloadingCB downloading_cb_; |
| 243 | 214 |
| 244 // The original URL of the first response. If the request is redirected to | 215 // The original URL of the first response. If the request is redirected to |
| 245 // another URL it is the URL after redirected. If the response is generated in | 216 // another URL it is the URL after redirected. If the response is generated in |
| 246 // a Service Worker this URL is empty. BufferedDataSource checks the original | 217 // a Service Worker this URL is empty. MultibufferDataSource checks the |
| 247 // URL of each successive response. If the origin URL of it is different from | 218 // original URL of each successive response. If the origin URL of it is |
| 248 // the original URL of the first response, it is treated as an error. | 219 // different from the original URL of the first response, it is treated |
| 220 // as an error. | |
| 249 GURL response_original_url_; | 221 GURL response_original_url_; |
| 250 | 222 |
| 251 // Disallow rebinding WeakReference ownership to a different thread by keeping | 223 // Disallow rebinding WeakReference ownership to a different thread by keeping |
| 252 // a persistent reference. This avoids problems with the thread-safety of | 224 // a persistent reference. This avoids problems with the thread-safety of |
| 253 // reaching into this class from multiple threads to attain a WeakPtr. | 225 // reaching into this class from multiple threads to attain a WeakPtr. |
| 254 base::WeakPtr<BufferedDataSource> weak_ptr_; | 226 base::WeakPtr<MultibufferDataSource> weak_ptr_; |
| 255 base::WeakPtrFactory<BufferedDataSource> weak_factory_; | 227 base::WeakPtrFactory<MultibufferDataSource> weak_factory_; |
| 256 | 228 |
| 257 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 229 DISALLOW_COPY_AND_ASSIGN(MultibufferDataSource); |
| 258 }; | 230 }; |
| 259 | 231 |
| 260 } // namespace media | 232 } // namespace media |
| 261 | 233 |
| 262 #endif // MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ | 234 #endif // MEDIA_BLINK_MULTIBUFFER_DATA_SOURCE_H_ |
| OLD | NEW |