Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / 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 CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_
7
8 #include <string>
9
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"
18 #include "url/gurl.h"
19
20 namespace base {
21 class MessageLoopProxy;
22 }
23
24 namespace media {
25 class MediaLog;
26 }
27
28 namespace content {
29
30 class CONTENT_EXPORT BufferedDataSourceHost {
31  public:
32   // Notify the host of the total size of the media file.
33   virtual void SetTotalBytes(int64 total_bytes) = 0;
34
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;
39
40  protected:
41   virtual ~BufferedDataSourceHost() {};
42 };
43
44 // A data source capable of loading URLs and buffering the data using an
45 // in-memory sliding window.
46 //
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 {
50  public:
51   typedef base::Callback<void(bool)> DownloadingCB;
52
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();
61
62   // Initialize this object using |url| and |cors_mode|, executing |init_cb|
63   // with the result of initialization when it has completed.
64   //
65   // Method called on the render thread.
66   typedef base::Callback<void(bool)> InitializeCB;
67   void Initialize(
68       const GURL& url,
69       BufferedResourceLoader::CORSMode cors_mode,
70       const InitializeCB& init_cb);
71
72   // Adjusts the buffering algorithm based on the given preload value.
73   void SetPreload(Preload preload);
74
75   // Returns true if the media resource has a single origin, false otherwise.
76   // Only valid to call after Initialize() has completed.
77   //
78   // Method called on the render thread.
79   bool HasSingleOrigin();
80
81   // Returns true if the media resource passed a CORS access control check.
82   bool DidPassCORSAccessCheck() const;
83
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.
87   //
88   // Method called on the render thread.
89   void Abort();
90
91   // Notifies changes in playback state for controlling media buffering
92   // behavior.
93   void MediaPlaybackRateChanged(float playback_rate);
94   void MediaIsPlaying();
95   void MediaIsPaused();
96
97   // media::DataSource implementation.
98   // Called from demuxer thread.
99   virtual void Stop(const base::Closure& closure) OVERRIDE;
100
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;
106
107  protected:
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);
113
114  private:
115   friend class BufferedDataSourceTest;
116
117   // Task posted to perform actual reading on the render thread.
118   void ReadTask();
119
120   // Cancels oustanding callbacks and sets |stop_signal_received_|. Safe to call
121   // from any thread.
122   void StopInternal_Locked();
123
124   // Stops |loader_| if present. Used by Abort() and Stop().
125   void StopLoader();
126
127   // Tells |loader_| the bitrate of the media.
128   void SetBitrateTask(int bitrate);
129
130   // The method that performs actual read. This method can only be executed on
131   // the render thread.
132   void ReadInternal();
133
134   // BufferedResourceLoader::Start() callback for initial load.
135   void StartCallback(BufferedResourceLoader::Status status);
136
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);
140
141   // BufferedResourceLoader callbacks.
142   void ReadCallback(BufferedResourceLoader::Status status, int bytes_read);
143   void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state);
144   void ProgressCallback(int64 position);
145
146   // Update |loader_|'s deferring strategy in response to a play/pause, or
147   // change in playback rate.
148   void UpdateDeferStrategy(bool paused);
149
150   // URL of the resource requested.
151   GURL url_;
152   // crossorigin attribute on the corresponding HTML media element, if any.
153   BufferedResourceLoader::CORSMode cors_mode_;
154
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.
158   int64 total_bytes_;
159
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_;
163
164   // This value will be true if this data source can only support streaming.
165   // i.e. range request is not supported.
166   bool streaming_;
167
168   // A webframe for loading.
169   blink::WebFrame* frame_;
170
171   // A resource loader for the media resource.
172   scoped_ptr<BufferedResourceLoader> loader_;
173
174   // Callback method from the pipeline for initialization.
175   InitializeCB init_cb_;
176
177   // Read parameters received from the Read() method call. Must be accessed
178   // under |lock_|.
179   class ReadOperation;
180   scoped_ptr<ReadOperation> read_op_;
181
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_;
194
195   // The message loop of the render thread.
196   const scoped_refptr<base::MessageLoopProxy> render_loop_;
197
198   // Protects |stop_signal_received_| and |read_op_|.
199   base::Lock lock_;
200
201   // Whether we've been told to stop via Abort() or Stop().
202   bool stop_signal_received_;
203
204   // This variable is true when the user has requested the video to play at
205   // least once.
206   bool media_has_played_;
207
208   // This variable holds the value of the preload attribute for the video
209   // element.
210   Preload preload_;
211
212   // Bitrate of the content, 0 if unknown.
213   int bitrate_;
214
215   // Current playback rate.
216   float playback_rate_;
217
218   scoped_refptr<media::MediaLog> media_log_;
219
220   // Host object to report buffered byte range changes to.
221   BufferedDataSourceHost* host_;
222
223   DownloadingCB downloading_cb_;
224
225   // NOTE: Weak pointers must be invalidated before all other member variables.
226   base::WeakPtrFactory<BufferedDataSource> weak_factory_;
227
228   DISALLOW_COPY_AND_ASSIGN(BufferedDataSource);
229 };
230
231 }  // namespace content
232
233 #endif  // CONTENT_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_