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.
5 #ifndef CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_
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"
21 class MessageLoopProxy;
30 class CONTENT_EXPORT BufferedDataSourceHost {
32 // Notify the host of the total size of the media file.
33 virtual void SetTotalBytes(int64 total_bytes) = 0;
35 // Notify the host that byte range [start,end] has been buffered.
36 // TODO(fischman): remove this method when demuxing is push-based instead of
37 // pull-based. http://crbug.com/131444
38 virtual void AddBufferedByteRange(int64 start, int64 end) = 0;
41 virtual ~BufferedDataSourceHost() {};
44 // A data source capable of loading URLs and buffering the data using an
45 // in-memory sliding window.
47 // BufferedDataSource must be created and initialized on the render thread
48 // before being passed to other threads. It may be deleted on any thread.
49 class CONTENT_EXPORT BufferedDataSource : public media::DataSource {
51 typedef base::Callback<void(bool)> DownloadingCB;
53 // Buffered byte range changes will be reported to |host|. |downloading_cb|
54 // will be called whenever the downloading/paused state of the source changes.
55 BufferedDataSource(const scoped_refptr<base::MessageLoopProxy>& render_loop,
56 blink::WebFrame* frame,
57 media::MediaLog* media_log,
58 BufferedDataSourceHost* host,
59 const DownloadingCB& downloading_cb);
60 virtual ~BufferedDataSource();
62 // Initialize this object using |url| and |cors_mode|, executing |init_cb|
63 // with the result of initialization when it has completed.
65 // Method called on the render thread.
66 typedef base::Callback<void(bool)> InitializeCB;
69 BufferedResourceLoader::CORSMode cors_mode,
70 const InitializeCB& init_cb);
72 // Adjusts the buffering algorithm based on the given preload value.
73 void SetPreload(Preload preload);
75 // Returns true if the media resource has a single origin, false otherwise.
76 // Only valid to call after Initialize() has completed.
78 // Method called on the render thread.
79 bool HasSingleOrigin();
81 // Returns true if the media resource passed a CORS access control check.
82 bool DidPassCORSAccessCheck() const;
84 // Cancels initialization, any pending loaders, and any pending read calls
85 // from the demuxer. The caller is expected to release its reference to this
86 // object and never call it again.
88 // Method called on the render thread.
91 // Notifies changes in playback state for controlling media buffering
93 void MediaPlaybackRateChanged(float playback_rate);
94 void MediaIsPlaying();
97 // media::DataSource implementation.
98 // Called from demuxer thread.
99 virtual void Stop(const base::Closure& closure) OVERRIDE;
101 virtual void Read(int64 position, int size, uint8* data,
102 const media::DataSource::ReadCB& read_cb) OVERRIDE;
103 virtual bool GetSize(int64* size_out) OVERRIDE;
104 virtual bool IsStreaming() OVERRIDE;
105 virtual void SetBitrate(int bitrate) OVERRIDE;
108 // A factory method to create a BufferedResourceLoader based on the read
109 // parameters. We can override this file to object a mock
110 // BufferedResourceLoader for testing.
111 virtual BufferedResourceLoader* CreateResourceLoader(
112 int64 first_byte_position, int64 last_byte_position);
115 friend class BufferedDataSourceTest;
117 // Task posted to perform actual reading on the render thread.
120 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
122 void StopInternal_Locked();
124 // Stops |loader_| if present. Used by Abort() and Stop().
127 // Tells |loader_| the bitrate of the media.
128 void SetBitrateTask(int bitrate);
130 // The method that performs actual read. This method can only be executed on
131 // the render thread.
134 // BufferedResourceLoader::Start() callback for initial load.
135 void StartCallback(BufferedResourceLoader::Status status);
137 // BufferedResourceLoader::Start() callback for subsequent loads (i.e.,
138 // when accessing ranges that are outside initial buffered region).
139 void PartialReadStartCallback(BufferedResourceLoader::Status status);
141 // BufferedResourceLoader callbacks.
142 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read);
143 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state);
144 void ProgressCallback(int64 position);
146 // Update |loader_|'s deferring strategy in response to a play/pause, or
147 // change in playback rate.
148 void UpdateDeferStrategy(bool paused);
150 // URL of the resource requested.
152 // crossorigin attribute on the corresponding HTML media element, if any.
153 BufferedResourceLoader::CORSMode cors_mode_;
155 // The total size of the resource. Set during StartCallback() if the size is
156 // known, otherwise it will remain kPositionNotSpecified until the size is
157 // determined by reaching EOF.
160 // Some resources are assumed to be fully buffered (i.e., file://) so we don't
161 // need to report what |loader_| has buffered.
162 bool assume_fully_buffered_;
164 // This value will be true if this data source can only support streaming.
165 // i.e. range request is not supported.
168 // A webframe for loading.
169 blink::WebFrame* frame_;
171 // A resource loader for the media resource.
172 scoped_ptr<BufferedResourceLoader> loader_;
174 // Callback method from the pipeline for initialization.
175 InitializeCB init_cb_;
177 // Read parameters received from the Read() method call. Must be accessed
180 scoped_ptr<ReadOperation> read_op_;
182 // This buffer is intermediate, we use it for BufferedResourceLoader to write
183 // to. And when read in BufferedResourceLoader is done, we copy data from
184 // this buffer to |read_buffer_|. The reason for an additional copy is that
185 // we don't own |read_buffer_|. But since the read operation is asynchronous,
186 // |read_buffer| can be destroyed at any time, so we only copy into
187 // |read_buffer| in the final step when it is safe.
188 // Memory is allocated for this member during initialization of this object
189 // because we want buffer to be passed into BufferedResourceLoader to be
190 // always non-null. And by initializing this member with a default size we can
191 // avoid creating zero-sized buffered if the first read has zero size.
192 scoped_ptr<uint8[]> intermediate_read_buffer_;
193 int intermediate_read_buffer_size_;
195 // The message loop of the render thread.
196 const scoped_refptr<base::MessageLoopProxy> render_loop_;
198 // Protects |stop_signal_received_| and |read_op_|.
201 // Whether we've been told to stop via Abort() or Stop().
202 bool stop_signal_received_;
204 // This variable is true when the user has requested the video to play at
206 bool media_has_played_;
208 // This variable holds the value of the preload attribute for the video
212 // Bitrate of the content, 0 if unknown.
215 // Current playback rate.
216 float playback_rate_;
218 scoped_refptr<media::MediaLog> media_log_;
220 // Host object to report buffered byte range changes to.
221 BufferedDataSourceHost* host_;
223 DownloadingCB downloading_cb_;
225 // NOTE: Weak pointers must be invalidated before all other member variables.
226 base::WeakPtrFactory<BufferedDataSource> weak_factory_;
228 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource);
231 } // namespace content
233 #endif // CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_