Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / blink / buffered_data_source.h
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 MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_
6 #define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "media/base/data_source.h"
16 #include "media/base/media_export.h"
17 #include "media/base/ranges.h"
18 #include "media/blink/buffered_resource_loader.h"
19 #include "url/gurl.h"
20
21 namespace base {
22 class SingleThreadTaskRunner;
23 }
24
25 namespace media {
26 class MediaLog;
27
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
42 // A data source capable of loading URLs and buffering the data using an
43 // in-memory sliding window.
44 //
45 // BufferedDataSource must be created and initialized on the render thread
46 // before being passed to other threads. It may be deleted on any thread.
47 class MEDIA_EXPORT BufferedDataSource : public DataSource {
48  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;
62
63   // |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
65   // downloading/paused state of the source changes.
66   BufferedDataSource(
67       const GURL& url,
68       BufferedResourceLoader::CORSMode cors_mode,
69       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
70       blink::WebFrame* frame,
71       MediaLog* media_log,
72       BufferedDataSourceHost* host,
73       const DownloadingCB& downloading_cb);
74   ~BufferedDataSource() override;
75
76   // Executes |init_cb| with the result of initialization when it has completed.
77   //
78   // Method called on the render thread.
79   typedef base::Callback<void(bool)> InitializeCB;
80   void Initialize(const InitializeCB& init_cb);
81
82   // Adjusts the buffering algorithm based on the given preload value.
83   void SetPreload(Preload preload);
84
85   // Returns true if the media resource has a single origin, false otherwise.
86   // Only valid to call after Initialize() has completed.
87   //
88   // Method called on the render thread.
89   bool HasSingleOrigin();
90
91   // Returns true if the media resource passed a CORS access control check.
92   bool DidPassCORSAccessCheck() const;
93
94   // Cancels initialization, any pending loaders, and any pending read calls
95   // from the demuxer. The caller is expected to release its reference to this
96   // object and never call it again.
97   //
98   // Method called on the render thread.
99   void Abort();
100
101   // Notifies changes in playback state for controlling media buffering
102   // behavior.
103   void MediaPlaybackRateChanged(float playback_rate);
104   void MediaIsPlaying();
105   void MediaIsPaused();
106
107   // Returns true if the resource is local.
108   bool assume_fully_buffered() { return !url_.SchemeIsHTTPOrHTTPS(); }
109
110   // DataSource implementation.
111   // Called from demuxer thread.
112   void Stop() override;
113
114   void Read(int64 position,
115             int size,
116             uint8* data,
117             const DataSource::ReadCB& read_cb) override;
118   bool GetSize(int64* size_out) override;
119   bool IsStreaming() override;
120   void SetBitrate(int bitrate) override;
121
122  protected:
123   // A factory method to create a BufferedResourceLoader based on the read
124   // parameters. We can override this file to object a mock
125   // BufferedResourceLoader for testing.
126   virtual BufferedResourceLoader* CreateResourceLoader(
127       int64 first_byte_position, int64 last_byte_position);
128
129  private:
130   friend class BufferedDataSourceTest;
131
132   // Task posted to perform actual reading on the render thread.
133   void ReadTask();
134
135   // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
136   // from any thread.
137   void StopInternal_Locked();
138
139   // Stops |loader_| if present. Used by Abort() and Stop().
140   void StopLoader();
141
142   // Tells |loader_| the bitrate of the media.
143   void SetBitrateTask(int bitrate);
144
145   // The method that performs actual read. This method can only be executed on
146   // the render thread.
147   void ReadInternal();
148
149   // BufferedResourceLoader::Start() callback for initial load.
150   void StartCallback(BufferedResourceLoader::Status status);
151
152   // BufferedResourceLoader::Start() callback for subsequent loads (i.e.,
153   // when accessing ranges that are outside initial buffered region).
154   void PartialReadStartCallback(BufferedResourceLoader::Status status);
155
156   // BufferedResourceLoader callbacks.
157   void ReadCallback(BufferedResourceLoader::Status status, int bytes_read);
158   void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state);
159   void ProgressCallback(int64 position);
160
161   // Update |loader_|'s deferring strategy in response to a play/pause, or
162   // change in playback rate.
163   void UpdateDeferStrategy(bool paused);
164
165   // URL of the resource requested.
166   GURL url_;
167   // crossorigin attribute on the corresponding HTML media element, if any.
168   BufferedResourceLoader::CORSMode cors_mode_;
169
170   // The total size of the resource. Set during StartCallback() if the size is
171   // known, otherwise it will remain kPositionNotSpecified until the size is
172   // determined by reaching EOF.
173   int64 total_bytes_;
174
175   // This value will be true if this data source can only support streaming.
176   // i.e. range request is not supported.
177   bool streaming_;
178
179   // A webframe for loading.
180   blink::WebFrame* frame_;
181
182   // A resource loader for the media resource.
183   scoped_ptr<BufferedResourceLoader> loader_;
184
185   // Callback method from the pipeline for initialization.
186   InitializeCB init_cb_;
187
188   // Read parameters received from the Read() method call. Must be accessed
189   // under |lock_|.
190   class ReadOperation;
191   scoped_ptr<ReadOperation> read_op_;
192
193   // This buffer is intermediate, we use it for BufferedResourceLoader to write
194   // to. And when read in BufferedResourceLoader is done, we copy data from
195   // this buffer to |read_buffer_|. The reason for an additional copy is that
196   // we don't own |read_buffer_|. But since the read operation is asynchronous,
197   // |read_buffer| can be destroyed at any time, so we only copy into
198   // |read_buffer| in the final step when it is safe.
199   // Memory is allocated for this member during initialization of this object
200   // because we want buffer to be passed into BufferedResourceLoader to be
201   // always non-null. And by initializing this member with a default size we can
202   // avoid creating zero-sized buffered if the first read has zero size.
203   std::vector<uint8> intermediate_read_buffer_;
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<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 media
242
243 #endif  // MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_