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 SingleThreadTaskRunner
;
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 // |url| and |cors_mode| are passed to the object. Buffered byte range changes
54 // will be reported to |host|. |downloading_cb| will be called whenever the
55 // downloading/paused state of the source changes.
58 BufferedResourceLoader::CORSMode cors_mode
,
59 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
60 blink::WebFrame
* frame
,
61 media::MediaLog
* media_log
,
62 BufferedDataSourceHost
* host
,
63 const DownloadingCB
& downloading_cb
);
64 virtual ~BufferedDataSource();
66 // Executes |init_cb| with the result of initialization when it has completed.
68 // Method called on the render thread.
69 typedef base::Callback
<void(bool)> InitializeCB
;
70 void Initialize(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 // Returns true if the resource is local.
98 bool assume_fully_buffered() { return !url_
.SchemeIsHTTPOrHTTPS(); }
100 // media::DataSource implementation.
101 // Called from demuxer thread.
102 virtual void Stop() OVERRIDE
;
104 virtual void Read(int64 position
, int size
, uint8
* data
,
105 const media::DataSource::ReadCB
& read_cb
) OVERRIDE
;
106 virtual bool GetSize(int64
* size_out
) OVERRIDE
;
107 virtual bool IsStreaming() OVERRIDE
;
108 virtual void SetBitrate(int bitrate
) OVERRIDE
;
111 // A factory method to create a BufferedResourceLoader based on the read
112 // parameters. We can override this file to object a mock
113 // BufferedResourceLoader for testing.
114 virtual BufferedResourceLoader
* CreateResourceLoader(
115 int64 first_byte_position
, int64 last_byte_position
);
118 friend class BufferedDataSourceTest
;
120 // Task posted to perform actual reading on the render thread.
123 // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
125 void StopInternal_Locked();
127 // Stops |loader_| if present. Used by Abort() and Stop().
130 // Tells |loader_| the bitrate of the media.
131 void SetBitrateTask(int bitrate
);
133 // The method that performs actual read. This method can only be executed on
134 // the render thread.
137 // BufferedResourceLoader::Start() callback for initial load.
138 void StartCallback(BufferedResourceLoader::Status status
);
140 // BufferedResourceLoader::Start() callback for subsequent loads (i.e.,
141 // when accessing ranges that are outside initial buffered region).
142 void PartialReadStartCallback(BufferedResourceLoader::Status status
);
144 // BufferedResourceLoader callbacks.
145 void ReadCallback(BufferedResourceLoader::Status status
, int bytes_read
);
146 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state
);
147 void ProgressCallback(int64 position
);
149 // Update |loader_|'s deferring strategy in response to a play/pause, or
150 // change in playback rate.
151 void UpdateDeferStrategy(bool paused
);
153 // URL of the resource requested.
155 // crossorigin attribute on the corresponding HTML media element, if any.
156 BufferedResourceLoader::CORSMode cors_mode_
;
158 // The total size of the resource. Set during StartCallback() if the size is
159 // known, otherwise it will remain kPositionNotSpecified until the size is
160 // determined by reaching EOF.
163 // This value will be true if this data source can only support streaming.
164 // i.e. range request is not supported.
167 // A webframe for loading.
168 blink::WebFrame
* frame_
;
170 // A resource loader for the media resource.
171 scoped_ptr
<BufferedResourceLoader
> loader_
;
173 // Callback method from the pipeline for initialization.
174 InitializeCB init_cb_
;
176 // Read parameters received from the Read() method call. Must be accessed
179 scoped_ptr
<ReadOperation
> read_op_
;
181 // This buffer is intermediate, we use it for BufferedResourceLoader to write
182 // to. And when read in BufferedResourceLoader is done, we copy data from
183 // this buffer to |read_buffer_|. The reason for an additional copy is that
184 // we don't own |read_buffer_|. But since the read operation is asynchronous,
185 // |read_buffer| can be destroyed at any time, so we only copy into
186 // |read_buffer| in the final step when it is safe.
187 // Memory is allocated for this member during initialization of this object
188 // because we want buffer to be passed into BufferedResourceLoader to be
189 // always non-null. And by initializing this member with a default size we can
190 // avoid creating zero-sized buffered if the first read has zero size.
191 scoped_ptr
<uint8
[]> intermediate_read_buffer_
;
192 int intermediate_read_buffer_size_
;
194 // The task runner of the render thread.
195 const scoped_refptr
<base::SingleThreadTaskRunner
> render_task_runner_
;
197 // Protects |stop_signal_received_| and |read_op_|.
200 // Whether we've been told to stop via Abort() or Stop().
201 bool stop_signal_received_
;
203 // This variable is true when the user has requested the video to play at
205 bool media_has_played_
;
207 // This variable holds the value of the preload attribute for the video
211 // Bitrate of the content, 0 if unknown.
214 // Current playback rate.
215 float playback_rate_
;
217 scoped_refptr
<media::MediaLog
> media_log_
;
219 // Host object to report buffered byte range changes to.
220 BufferedDataSourceHost
* host_
;
222 DownloadingCB downloading_cb_
;
224 // NOTE: Weak pointers must be invalidated before all other member variables.
225 base::WeakPtrFactory
<BufferedDataSource
> weak_factory_
;
227 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource
);
230 } // namespace content
232 #endif // CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_