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