OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ | 5 #ifndef WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ |
6 #define WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ | 6 #define WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "media/base/data_source.h" | 14 #include "media/base/data_source.h" |
15 #include "media/base/pipeline_status.h" | |
16 #include "webkit/media/buffered_resource_loader.h" | 15 #include "webkit/media/buffered_resource_loader.h" |
17 #include "webkit/media/preload.h" | 16 #include "webkit/media/preload.h" |
18 | 17 |
19 class MessageLoop; | 18 class MessageLoop; |
20 | 19 |
21 namespace media { | 20 namespace media { |
22 class MediaLog; | 21 class MediaLog; |
23 } | 22 } |
24 | 23 |
25 namespace webkit_media { | 24 namespace webkit_media { |
26 | 25 |
27 // A data source capable of loading URLs and buffering the data using an | 26 // A data source capable of loading URLs and buffering the data using an |
28 // in-memory sliding window. | 27 // in-memory sliding window. |
29 // | 28 // |
30 // BufferedDataSource must be created and initialized on the render thread | 29 // BufferedDataSource must be created and initialized on the render thread |
31 // before being passed to other threads. It may be deleted on any thread. | 30 // before being passed to other threads. It may be deleted on any thread. |
32 class BufferedDataSource : public media::DataSource { | 31 class BufferedDataSource : public media::DataSource { |
33 public: | 32 public: |
34 typedef base::Callback<void(bool)> DownloadingCB; | 33 typedef base::Callback<void(bool)> DownloadingCB; |
35 | 34 |
36 // |downloading_cb| will be called whenever the downloading/paused state of | 35 // |downloading_cb| will be called whenever the downloading/paused state of |
37 // the source changes. | 36 // the source changes. |
38 BufferedDataSource(MessageLoop* render_loop, | 37 BufferedDataSource(MessageLoop* render_loop, |
39 WebKit::WebFrame* frame, | 38 WebKit::WebFrame* frame, |
40 media::MediaLog* media_log, | 39 media::MediaLog* media_log, |
41 const DownloadingCB& downloading_cb); | 40 const DownloadingCB& downloading_cb); |
42 | 41 |
43 // Initialize this object using |url| and |cors_mode|, and call |status_cb| | 42 // Initialize this object using |url| and |cors_mode|, executing |init_cb| |
44 // when initialization has completed. | 43 // with the result of initialization when it has completed. |
45 // | 44 // |
46 // Method called on the render thread. | 45 // Method called on the render thread. |
| 46 typedef base::Callback<void(bool)> InitializeCB; |
47 void Initialize( | 47 void Initialize( |
48 const GURL& url, | 48 const GURL& url, |
49 BufferedResourceLoader::CORSMode cors_mode, | 49 BufferedResourceLoader::CORSMode cors_mode, |
50 const media::PipelineStatusCB& status_cb); | 50 const InitializeCB& init_cb); |
51 | 51 |
52 // Adjusts the buffering algorithm based on the given preload value. | 52 // Adjusts the buffering algorithm based on the given preload value. |
53 void SetPreload(Preload preload); | 53 void SetPreload(Preload preload); |
54 | 54 |
55 // Returns true if the media resource has a single origin, false otherwise. | 55 // Returns true if the media resource has a single origin, false otherwise. |
56 // Only valid to call after Initialize() has completed. | 56 // Only valid to call after Initialize() has completed. |
57 // | 57 // |
58 // Method called on the render thread. | 58 // Method called on the render thread. |
59 bool HasSingleOrigin(); | 59 bool HasSingleOrigin(); |
60 | 60 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 // The method that performs actual read. This method can only be executed on | 111 // The method that performs actual read. This method can only be executed on |
112 // the render thread. | 112 // the render thread. |
113 void ReadInternal(); | 113 void ReadInternal(); |
114 | 114 |
115 // Calls |read_cb_| and reset all read parameters. Non-negative |bytes_read| | 115 // Calls |read_cb_| and reset all read parameters. Non-negative |bytes_read| |
116 // values represent successful reads, otherwise |bytes_read| should be | 116 // values represent successful reads, otherwise |bytes_read| should be |
117 // kReadError. | 117 // kReadError. |
118 void DoneRead_Locked(int bytes_read); | 118 void DoneRead_Locked(int bytes_read); |
119 | 119 |
120 // Calls |initialize_cb_| and reset it. | |
121 void DoneInitialization_Locked(media::PipelineStatus status); | |
122 | |
123 // BufferedResourceLoader::Start() callback for initial load. | 120 // BufferedResourceLoader::Start() callback for initial load. |
124 void StartCallback(BufferedResourceLoader::Status status); | 121 void StartCallback(BufferedResourceLoader::Status status); |
125 | 122 |
126 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., | 123 // BufferedResourceLoader::Start() callback for subsequent loads (i.e., |
127 // when accessing ranges that are outside initial buffered region). | 124 // when accessing ranges that are outside initial buffered region). |
128 void PartialReadStartCallback(BufferedResourceLoader::Status status); | 125 void PartialReadStartCallback(BufferedResourceLoader::Status status); |
129 | 126 |
130 // BufferedResourceLoader callbacks. | 127 // BufferedResourceLoader callbacks. |
131 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); | 128 void ReadCallback(BufferedResourceLoader::Status status, int bytes_read); |
132 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state); | 129 void LoadingStateChangedCallback(BufferedResourceLoader::LoadingState state); |
(...skipping 19 matching lines...) Expand all Loading... |
152 // i.e. range request is not supported. | 149 // i.e. range request is not supported. |
153 bool streaming_; | 150 bool streaming_; |
154 | 151 |
155 // A webframe for loading. | 152 // A webframe for loading. |
156 WebKit::WebFrame* frame_; | 153 WebKit::WebFrame* frame_; |
157 | 154 |
158 // A resource loader for the media resource. | 155 // A resource loader for the media resource. |
159 scoped_ptr<BufferedResourceLoader> loader_; | 156 scoped_ptr<BufferedResourceLoader> loader_; |
160 | 157 |
161 // Callback method from the pipeline for initialization. | 158 // Callback method from the pipeline for initialization. |
162 media::PipelineStatusCB initialize_cb_; | 159 InitializeCB init_cb_; |
163 | 160 |
164 // Read parameters received from the Read() method call. | 161 // Read parameters received from the Read() method call. |
165 media::DataSource::ReadCB read_cb_; | 162 media::DataSource::ReadCB read_cb_; |
166 int read_size_; | 163 int read_size_; |
167 uint8* read_buffer_; | 164 uint8* read_buffer_; |
168 // Retained between reads to make sense of buffering information. | 165 // Retained between reads to make sense of buffering information. |
169 int64 last_read_start_; | 166 int64 last_read_start_; |
170 | 167 |
171 // This buffer is intermediate, we use it for BufferedResourceLoader to write | 168 // This buffer is intermediate, we use it for BufferedResourceLoader to write |
172 // to. And when read in BufferedResourceLoader is done, we copy data from | 169 // to. And when read in BufferedResourceLoader is done, we copy data from |
173 // this buffer to |read_buffer_|. The reason for an additional copy is that | 170 // this buffer to |read_buffer_|. The reason for an additional copy is that |
174 // we don't own |read_buffer_|. But since the read operation is asynchronous, | 171 // we don't own |read_buffer_|. But since the read operation is asynchronous, |
175 // |read_buffer| can be destroyed at any time, so we only copy into | 172 // |read_buffer| can be destroyed at any time, so we only copy into |
176 // |read_buffer| in the final step when it is safe. | 173 // |read_buffer| in the final step when it is safe. |
177 // Memory is allocated for this member during initialization of this object | 174 // Memory is allocated for this member during initialization of this object |
178 // because we want buffer to be passed into BufferedResourceLoader to be | 175 // because we want buffer to be passed into BufferedResourceLoader to be |
179 // always non-null. And by initializing this member with a default size we can | 176 // always non-null. And by initializing this member with a default size we can |
180 // avoid creating zero-sized buffered if the first read has zero size. | 177 // avoid creating zero-sized buffered if the first read has zero size. |
181 scoped_array<uint8> intermediate_read_buffer_; | 178 scoped_array<uint8> intermediate_read_buffer_; |
182 int intermediate_read_buffer_size_; | 179 int intermediate_read_buffer_size_; |
183 | 180 |
184 // The message loop of the render thread. | 181 // The message loop of the render thread. |
185 MessageLoop* render_loop_; | 182 MessageLoop* render_loop_; |
186 | 183 |
187 // Protects |stop_signal_received_|, |stopped_on_render_loop_| and | 184 // Protects |stop_signal_received_|, |stopped_on_render_loop_| and |
188 // |initialize_cb_|. | 185 // |init_cb_|. |
189 base::Lock lock_; | 186 base::Lock lock_; |
190 | 187 |
191 // Stop signal to suppressing activities. This variable is set on the pipeline | 188 // Stop signal to suppressing activities. This variable is set on the pipeline |
192 // thread and read from the render thread. | 189 // thread and read from the render thread. |
193 bool stop_signal_received_; | 190 bool stop_signal_received_; |
194 | 191 |
195 // This variable is set by CleanupTask() that indicates this object is stopped | 192 // This variable is set by CleanupTask() that indicates this object is stopped |
196 // on the render thread and work should no longer progress. | 193 // on the render thread and work should no longer progress. |
197 bool stopped_on_render_loop_; | 194 bool stopped_on_render_loop_; |
198 | 195 |
(...skipping 17 matching lines...) Expand all Loading... |
216 scoped_refptr<media::MediaLog> media_log_; | 213 scoped_refptr<media::MediaLog> media_log_; |
217 | 214 |
218 DownloadingCB downloading_cb_; | 215 DownloadingCB downloading_cb_; |
219 | 216 |
220 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); | 217 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
221 }; | 218 }; |
222 | 219 |
223 } // namespace webkit_media | 220 } // namespace webkit_media |
224 | 221 |
225 #endif // WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ | 222 #endif // WEBKIT_MEDIA_BUFFERED_DATA_SOURCE_H_ |
OLD | NEW |