OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include "webkit/glue/media/buffered_data_source.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "media/base/filter_host.h" | |
9 #include "media/base/media_log.h" | |
10 #include "net/base/net_errors.h" | |
11 #include "webkit/glue/media/web_data_source_factory.h" | |
12 #include "webkit/glue/webkit_glue.h" | |
13 | |
14 using WebKit::WebFrame; | |
15 | |
16 namespace webkit_glue { | |
17 | |
18 // BufferedDataSource has an intermediate buffer, this value governs the initial | |
19 // size of that buffer. It is set to 32KB because this is a typical read size | |
20 // of FFmpeg. | |
21 static const int kInitialReadBufferSize = 32768; | |
22 | |
23 // Number of cache misses we allow for a single Read() before signalling an | |
24 // error. | |
25 static const int kNumCacheMissRetries = 3; | |
26 | |
27 static WebDataSource* NewBufferedDataSource(MessageLoop* render_loop, | |
28 WebKit::WebFrame* frame, | |
29 media::MediaLog* media_log) { | |
30 return new BufferedDataSource(render_loop, frame, media_log); | |
31 } | |
32 | |
33 // static | |
34 media::DataSourceFactory* BufferedDataSource::CreateFactory( | |
35 MessageLoop* render_loop, | |
36 WebKit::WebFrame* frame, | |
37 media::MediaLog* media_log, | |
38 const WebDataSourceBuildObserverHack& build_observer) { | |
39 return new WebDataSourceFactory(render_loop, frame, media_log, | |
40 &NewBufferedDataSource, build_observer); | |
41 } | |
42 | |
43 BufferedDataSource::BufferedDataSource( | |
44 MessageLoop* render_loop, | |
45 WebFrame* frame, | |
46 media::MediaLog* media_log) | |
47 : total_bytes_(kPositionNotSpecified), | |
48 buffered_bytes_(0), | |
49 loaded_(false), | |
50 streaming_(false), | |
51 frame_(frame), | |
52 loader_(NULL), | |
53 is_downloading_data_(false), | |
54 read_position_(0), | |
55 read_size_(0), | |
56 read_buffer_(NULL), | |
57 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]), | |
58 intermediate_read_buffer_size_(kInitialReadBufferSize), | |
59 render_loop_(render_loop), | |
60 stop_signal_received_(false), | |
61 stopped_on_render_loop_(false), | |
62 media_is_paused_(true), | |
63 media_has_played_(false), | |
64 preload_(media::AUTO), | |
65 using_range_request_(true), | |
66 cache_miss_retries_left_(kNumCacheMissRetries), | |
67 bitrate_(0), | |
68 playback_rate_(0.0), | |
69 media_log_(media_log) { | |
70 } | |
71 | |
72 BufferedDataSource::~BufferedDataSource() {} | |
73 | |
74 // A factory method to create BufferedResourceLoader using the read parameters. | |
75 // This method can be overrided to inject mock BufferedResourceLoader object | |
76 // for testing purpose. | |
77 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( | |
78 int64 first_byte_position, int64 last_byte_position) { | |
79 DCHECK(MessageLoop::current() == render_loop_); | |
80 | |
81 return new BufferedResourceLoader(url_, | |
82 first_byte_position, | |
83 last_byte_position, | |
84 ChooseDeferStrategy(), | |
85 bitrate_, | |
86 playback_rate_, | |
87 media_log_); | |
88 } | |
89 | |
90 void BufferedDataSource::set_host(media::FilterHost* host) { | |
91 DataSource::set_host(host); | |
92 | |
93 if (loader_.get()) { | |
94 base::AutoLock auto_lock(lock_); | |
95 UpdateHostState_Locked(); | |
96 } | |
97 } | |
98 | |
99 void BufferedDataSource::Initialize(const std::string& url, | |
100 const media::PipelineStatusCB& callback) { | |
101 // Saves the url. | |
102 url_ = GURL(url); | |
103 | |
104 // This data source doesn't support data:// protocol so reject it. | |
105 if (url_.SchemeIs(kDataScheme)) { | |
106 callback.Run(media::DATASOURCE_ERROR_URL_NOT_SUPPORTED); | |
107 return; | |
108 } else if (!IsProtocolSupportedForMedia(url_)) { | |
109 callback.Run(media::PIPELINE_ERROR_NETWORK); | |
110 return; | |
111 } | |
112 | |
113 DCHECK(!callback.is_null()); | |
114 { | |
115 base::AutoLock auto_lock(lock_); | |
116 initialize_cb_ = callback; | |
117 } | |
118 | |
119 // Post a task to complete the initialization task. | |
120 render_loop_->PostTask(FROM_HERE, | |
121 base::Bind(&BufferedDataSource::InitializeTask, this)); | |
122 } | |
123 | |
124 void BufferedDataSource::CancelInitialize() { | |
125 base::AutoLock auto_lock(lock_); | |
126 DCHECK(!initialize_cb_.is_null()); | |
127 | |
128 initialize_cb_.Reset(); | |
129 | |
130 render_loop_->PostTask( | |
131 FROM_HERE, base::Bind(&BufferedDataSource::CleanupTask, this)); | |
132 } | |
133 | |
134 ///////////////////////////////////////////////////////////////////////////// | |
135 // media::Filter implementation. | |
136 void BufferedDataSource::Stop(const base::Closure& callback) { | |
137 { | |
138 base::AutoLock auto_lock(lock_); | |
139 stop_signal_received_ = true; | |
140 } | |
141 if (!callback.is_null()) | |
142 callback.Run(); | |
143 | |
144 render_loop_->PostTask(FROM_HERE, | |
145 base::Bind(&BufferedDataSource::CleanupTask, this)); | |
146 } | |
147 | |
148 void BufferedDataSource::SetPlaybackRate(float playback_rate) { | |
149 render_loop_->PostTask(FROM_HERE, base::Bind( | |
150 &BufferedDataSource::SetPlaybackRateTask, this, playback_rate)); | |
151 } | |
152 | |
153 void BufferedDataSource::SetPreload(media::Preload preload) { | |
154 render_loop_->PostTask(FROM_HERE, base::Bind( | |
155 &BufferedDataSource::SetPreloadTask, this, preload)); | |
156 } | |
157 | |
158 void BufferedDataSource::SetBitrate(int bitrate) { | |
159 render_loop_->PostTask(FROM_HERE, base::Bind( | |
160 &BufferedDataSource::SetBitrateTask, this, bitrate)); | |
161 } | |
162 | |
163 ///////////////////////////////////////////////////////////////////////////// | |
164 // media::DataSource implementation. | |
165 void BufferedDataSource::Read( | |
166 int64 position, size_t size, uint8* data, | |
167 const media::DataSource::ReadCallback& read_callback) { | |
168 VLOG(1) << "Read: " << position << " offset, " << size << " bytes"; | |
169 DCHECK(!read_callback.is_null()); | |
170 | |
171 { | |
172 base::AutoLock auto_lock(lock_); | |
173 DCHECK(read_callback_.is_null()); | |
174 | |
175 if (stop_signal_received_ || stopped_on_render_loop_) { | |
176 read_callback.Run(kReadError); | |
177 return; | |
178 } | |
179 | |
180 read_callback_ = read_callback; | |
181 } | |
182 | |
183 render_loop_->PostTask(FROM_HERE, base::Bind( | |
184 &BufferedDataSource::ReadTask, this, | |
185 position, static_cast<int>(size), data)); | |
186 } | |
187 | |
188 bool BufferedDataSource::GetSize(int64* size_out) { | |
189 if (total_bytes_ != kPositionNotSpecified) { | |
190 *size_out = total_bytes_; | |
191 return true; | |
192 } | |
193 *size_out = 0; | |
194 return false; | |
195 } | |
196 | |
197 bool BufferedDataSource::IsStreaming() { | |
198 return streaming_; | |
199 } | |
200 | |
201 bool BufferedDataSource::HasSingleOrigin() { | |
202 DCHECK(MessageLoop::current() == render_loop_); | |
203 return loader_.get() ? loader_->HasSingleOrigin() : true; | |
204 } | |
205 | |
206 void BufferedDataSource::Abort() { | |
207 DCHECK(MessageLoop::current() == render_loop_); | |
208 | |
209 CleanupTask(); | |
210 frame_ = NULL; | |
211 } | |
212 | |
213 ///////////////////////////////////////////////////////////////////////////// | |
214 // Render thread tasks. | |
215 void BufferedDataSource::InitializeTask() { | |
216 DCHECK(MessageLoop::current() == render_loop_); | |
217 DCHECK(!loader_.get()); | |
218 | |
219 { | |
220 base::AutoLock auto_lock(lock_); | |
221 if (stopped_on_render_loop_ || initialize_cb_.is_null() || | |
222 stop_signal_received_) { | |
223 return; | |
224 } | |
225 } | |
226 | |
227 if (url_.SchemeIs(kHttpScheme) || url_.SchemeIs(kHttpsScheme)) { | |
228 // Do an unbounded range request starting at the beginning. If the server | |
229 // responds with 200 instead of 206 we'll fall back into a streaming mode. | |
230 loader_ = CreateResourceLoader(0, kPositionNotSpecified); | |
231 loader_->Start( | |
232 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), | |
233 base::Bind(&BufferedDataSource::NetworkEventCallback, this), | |
234 frame_); | |
235 } else { | |
236 // For all other protocols, assume they support range request. We fetch | |
237 // the full range of the resource to obtain the instance size because | |
238 // we won't be served HTTP headers. | |
239 loader_ = CreateResourceLoader(kPositionNotSpecified, | |
240 kPositionNotSpecified); | |
241 loader_->Start( | |
242 NewCallback(this, &BufferedDataSource::NonHttpInitialStartCallback), | |
243 base::Bind(&BufferedDataSource::NetworkEventCallback, this), | |
244 frame_); | |
245 } | |
246 } | |
247 | |
248 void BufferedDataSource::ReadTask( | |
249 int64 position, | |
250 int read_size, | |
251 uint8* buffer) { | |
252 DCHECK(MessageLoop::current() == render_loop_); | |
253 { | |
254 base::AutoLock auto_lock(lock_); | |
255 if (stopped_on_render_loop_) | |
256 return; | |
257 | |
258 DCHECK(!read_callback_.is_null()); | |
259 } | |
260 | |
261 // Saves the read parameters. | |
262 read_position_ = position; | |
263 read_size_ = read_size; | |
264 read_buffer_ = buffer; | |
265 cache_miss_retries_left_ = kNumCacheMissRetries; | |
266 | |
267 // Call to read internal to perform the actual read. | |
268 ReadInternal(); | |
269 } | |
270 | |
271 void BufferedDataSource::CleanupTask() { | |
272 DCHECK(MessageLoop::current() == render_loop_); | |
273 | |
274 { | |
275 base::AutoLock auto_lock(lock_); | |
276 initialize_cb_.Reset(); | |
277 if (stopped_on_render_loop_) | |
278 return; | |
279 | |
280 // Signal that stop task has finished execution. | |
281 // NOTE: it's vital that this be set under lock, as that's how Read() tests | |
282 // before registering a new |read_callback_| (which is cleared below). | |
283 stopped_on_render_loop_ = true; | |
284 | |
285 if (!read_callback_.is_null()) | |
286 DoneRead_Locked(net::ERR_FAILED); | |
287 } | |
288 | |
289 // We just need to stop the loader, so it stops activity. | |
290 if (loader_.get()) | |
291 loader_->Stop(); | |
292 | |
293 // Reset the parameters of the current read request. | |
294 read_position_ = 0; | |
295 read_size_ = 0; | |
296 read_buffer_ = 0; | |
297 } | |
298 | |
299 void BufferedDataSource::RestartLoadingTask() { | |
300 DCHECK(MessageLoop::current() == render_loop_); | |
301 if (stopped_on_render_loop_) | |
302 return; | |
303 | |
304 { | |
305 // If there's no outstanding read then return early. | |
306 base::AutoLock auto_lock(lock_); | |
307 if (read_callback_.is_null()) | |
308 return; | |
309 } | |
310 | |
311 loader_ = CreateResourceLoader(read_position_, kPositionNotSpecified); | |
312 loader_->Start( | |
313 NewCallback(this, &BufferedDataSource::PartialReadStartCallback), | |
314 base::Bind(&BufferedDataSource::NetworkEventCallback, this), | |
315 frame_); | |
316 } | |
317 | |
318 void BufferedDataSource::SetPlaybackRateTask(float playback_rate) { | |
319 DCHECK(MessageLoop::current() == render_loop_); | |
320 DCHECK(loader_.get()); | |
321 | |
322 playback_rate_ = playback_rate; | |
323 loader_->SetPlaybackRate(playback_rate); | |
324 | |
325 bool previously_paused = media_is_paused_; | |
326 media_is_paused_ = (playback_rate == 0.0); | |
327 | |
328 if (!media_has_played_ && previously_paused && !media_is_paused_) | |
329 media_has_played_ = true; | |
330 | |
331 BufferedResourceLoader::DeferStrategy strategy = ChooseDeferStrategy(); | |
332 loader_->UpdateDeferStrategy(strategy); | |
333 } | |
334 | |
335 void BufferedDataSource::SetPreloadTask(media::Preload preload) { | |
336 DCHECK(MessageLoop::current() == render_loop_); | |
337 preload_ = preload; | |
338 } | |
339 | |
340 void BufferedDataSource::SetBitrateTask(int bitrate) { | |
341 DCHECK(MessageLoop::current() == render_loop_); | |
342 DCHECK(loader_.get()); | |
343 | |
344 bitrate_ = bitrate; | |
345 loader_->SetBitrate(bitrate); | |
346 } | |
347 | |
348 BufferedResourceLoader::DeferStrategy | |
349 BufferedDataSource::ChooseDeferStrategy() { | |
350 DCHECK(MessageLoop::current() == render_loop_); | |
351 // If the page indicated preload=metadata, then load exactly what is needed | |
352 // needed for starting playback. | |
353 if (!media_has_played_ && preload_ == media::METADATA) | |
354 return BufferedResourceLoader::kReadThenDefer; | |
355 | |
356 // If the playback has started (at which point the preload value is ignored) | |
357 // and we're paused, then try to load as much as possible. | |
358 if (media_has_played_ && media_is_paused_) | |
359 return BufferedResourceLoader::kNeverDefer; | |
360 | |
361 // If media is currently playing or the page indicated preload=auto, | |
362 // use threshold strategy to enable/disable deferring when the buffer | |
363 // is full/depleted. | |
364 return BufferedResourceLoader::kThresholdDefer; | |
365 } | |
366 | |
367 // This method is the place where actual read happens, |loader_| must be valid | |
368 // prior to make this method call. | |
369 void BufferedDataSource::ReadInternal() { | |
370 DCHECK(MessageLoop::current() == render_loop_); | |
371 DCHECK(loader_); | |
372 | |
373 // First we prepare the intermediate read buffer for BufferedResourceLoader | |
374 // to write to. | |
375 if (read_size_ > intermediate_read_buffer_size_) { | |
376 intermediate_read_buffer_.reset(new uint8[read_size_]); | |
377 } | |
378 | |
379 // Perform the actual read with BufferedResourceLoader. | |
380 loader_->Read(read_position_, read_size_, intermediate_read_buffer_.get(), | |
381 NewCallback(this, &BufferedDataSource::ReadCallback)); | |
382 } | |
383 | |
384 // Method to report the results of the current read request. Also reset all | |
385 // the read parameters. | |
386 void BufferedDataSource::DoneRead_Locked(int error) { | |
387 VLOG(1) << "DoneRead: " << error << " bytes"; | |
388 | |
389 DCHECK(MessageLoop::current() == render_loop_); | |
390 DCHECK(!read_callback_.is_null()); | |
391 lock_.AssertAcquired(); | |
392 | |
393 if (error >= 0) { | |
394 read_callback_.Run(static_cast<size_t>(error)); | |
395 } else { | |
396 read_callback_.Run(kReadError); | |
397 } | |
398 | |
399 read_callback_.Reset(); | |
400 read_position_ = 0; | |
401 read_size_ = 0; | |
402 read_buffer_ = 0; | |
403 } | |
404 | |
405 void BufferedDataSource::DoneInitialization_Locked( | |
406 media::PipelineStatus status) { | |
407 DCHECK(MessageLoop::current() == render_loop_); | |
408 DCHECK(!initialize_cb_.is_null()); | |
409 lock_.AssertAcquired(); | |
410 | |
411 initialize_cb_.Run(status); | |
412 initialize_cb_.Reset(); | |
413 } | |
414 | |
415 ///////////////////////////////////////////////////////////////////////////// | |
416 // BufferedResourceLoader callback methods. | |
417 void BufferedDataSource::HttpInitialStartCallback(int error) { | |
418 DCHECK(MessageLoop::current() == render_loop_); | |
419 DCHECK(loader_.get()); | |
420 | |
421 int64 instance_size = loader_->instance_size(); | |
422 bool success = error == net::OK; | |
423 | |
424 bool initialize_cb_is_null = false; | |
425 { | |
426 base::AutoLock auto_lock(lock_); | |
427 initialize_cb_is_null = initialize_cb_.is_null(); | |
428 } | |
429 if (initialize_cb_is_null) { | |
430 loader_->Stop(); | |
431 return; | |
432 } | |
433 | |
434 if (success) { | |
435 // TODO(hclam): Needs more thinking about supporting servers without range | |
436 // request or their partial response is not complete. | |
437 total_bytes_ = instance_size; | |
438 loaded_ = false; | |
439 streaming_ = (instance_size == kPositionNotSpecified) || | |
440 !loader_->range_supported(); | |
441 } else { | |
442 // TODO(hclam): In case of failure, we can retry several times. | |
443 loader_->Stop(); | |
444 } | |
445 | |
446 if (error == net::ERR_INVALID_RESPONSE && using_range_request_) { | |
447 // Assuming that the Range header was causing the problem. Retry without | |
448 // the Range header. | |
449 using_range_request_ = false; | |
450 loader_ = CreateResourceLoader(kPositionNotSpecified, | |
451 kPositionNotSpecified); | |
452 loader_->Start( | |
453 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), | |
454 base::Bind(&BufferedDataSource::NetworkEventCallback, this), | |
455 frame_); | |
456 return; | |
457 } | |
458 | |
459 // Reference to prevent destruction while inside the |initialize_cb_| | |
460 // call. This is a temporary fix to prevent crashes caused by holding the | |
461 // lock and running the destructor. | |
462 // TODO: Review locking in this class and figure out a way to run the callback | |
463 // w/o the lock. | |
464 scoped_refptr<BufferedDataSource> destruction_guard(this); | |
465 { | |
466 // We need to prevent calling to filter host and running the callback if | |
467 // we have received the stop signal. We need to lock down the whole callback | |
468 // method to prevent bad things from happening. The reason behind this is | |
469 // that we cannot guarantee tasks on render thread have completely stopped | |
470 // when we receive the Stop() method call. The only way to solve this is to | |
471 // let tasks on render thread to run but make sure they don't call outside | |
472 // this object when Stop() method is ever called. Locking this method is | |
473 // safe because |lock_| is only acquired in tasks on render thread. | |
474 base::AutoLock auto_lock(lock_); | |
475 if (stop_signal_received_) | |
476 return; | |
477 | |
478 if (!success) { | |
479 DoneInitialization_Locked(media::PIPELINE_ERROR_NETWORK); | |
480 return; | |
481 } | |
482 | |
483 UpdateHostState_Locked(); | |
484 DoneInitialization_Locked(media::PIPELINE_OK); | |
485 } | |
486 } | |
487 | |
488 void BufferedDataSource::NonHttpInitialStartCallback(int error) { | |
489 DCHECK(MessageLoop::current() == render_loop_); | |
490 DCHECK(loader_.get()); | |
491 | |
492 bool initialize_cb_is_null = false; | |
493 { | |
494 base::AutoLock auto_lock(lock_); | |
495 initialize_cb_is_null = initialize_cb_.is_null(); | |
496 } | |
497 if (initialize_cb_is_null) { | |
498 loader_->Stop(); | |
499 return; | |
500 } | |
501 | |
502 int64 instance_size = loader_->instance_size(); | |
503 bool success = error == net::OK && instance_size != kPositionNotSpecified; | |
504 | |
505 if (success) { | |
506 total_bytes_ = instance_size; | |
507 buffered_bytes_ = total_bytes_; | |
508 loaded_ = true; | |
509 } else { | |
510 loader_->Stop(); | |
511 } | |
512 | |
513 // Reference to prevent destruction while inside the |initialize_cb_| | |
514 // call. This is a temporary fix to prevent crashes caused by holding the | |
515 // lock and running the destructor. | |
516 // TODO: Review locking in this class and figure out a way to run the callback | |
517 // w/o the lock. | |
518 scoped_refptr<BufferedDataSource> destruction_guard(this); | |
519 { | |
520 // We need to prevent calling to filter host and running the callback if | |
521 // we have received the stop signal. We need to lock down the whole callback | |
522 // method to prevent bad things from happening. The reason behind this is | |
523 // that we cannot guarantee tasks on render thread have completely stopped | |
524 // when we receive the Stop() method call. The only way to solve this is to | |
525 // let tasks on render thread to run but make sure they don't call outside | |
526 // this object when Stop() method is ever called. Locking this method is | |
527 // safe because |lock_| is only acquired in tasks on render thread. | |
528 base::AutoLock auto_lock(lock_); | |
529 if (stop_signal_received_ || initialize_cb_.is_null()) | |
530 return; | |
531 | |
532 if (!success) { | |
533 DoneInitialization_Locked(media::PIPELINE_ERROR_NETWORK); | |
534 return; | |
535 } | |
536 | |
537 UpdateHostState_Locked(); | |
538 DoneInitialization_Locked(media::PIPELINE_OK); | |
539 } | |
540 } | |
541 | |
542 void BufferedDataSource::PartialReadStartCallback(int error) { | |
543 DCHECK(MessageLoop::current() == render_loop_); | |
544 DCHECK(loader_.get()); | |
545 | |
546 if (error == net::OK) { | |
547 // Once the request has started successfully, we can proceed with | |
548 // reading from it. | |
549 ReadInternal(); | |
550 return; | |
551 } | |
552 | |
553 // Stop the resource loader since we have received an error. | |
554 loader_->Stop(); | |
555 | |
556 // We need to prevent calling to filter host and running the callback if | |
557 // we have received the stop signal. We need to lock down the whole callback | |
558 // method to prevent bad things from happening. The reason behind this is | |
559 // that we cannot guarantee tasks on render thread have completely stopped | |
560 // when we receive the Stop() method call. So only way to solve this is to | |
561 // let tasks on render thread to run but make sure they don't call outside | |
562 // this object when Stop() method is ever called. Locking this method is | |
563 // safe because |lock_| is only acquired in tasks on render thread. | |
564 base::AutoLock auto_lock(lock_); | |
565 if (stop_signal_received_) | |
566 return; | |
567 DoneRead_Locked(net::ERR_INVALID_RESPONSE); | |
568 } | |
569 | |
570 void BufferedDataSource::ReadCallback(int error) { | |
571 DCHECK(MessageLoop::current() == render_loop_); | |
572 | |
573 if (error < 0) { | |
574 DCHECK(loader_.get()); | |
575 | |
576 // Stop the resource load if it failed. | |
577 loader_->Stop(); | |
578 | |
579 if (error == net::ERR_CACHE_MISS && cache_miss_retries_left_ > 0) { | |
580 cache_miss_retries_left_--; | |
581 render_loop_->PostTask(FROM_HERE, | |
582 base::Bind(&BufferedDataSource::RestartLoadingTask, this)); | |
583 return; | |
584 } | |
585 } | |
586 | |
587 // We need to prevent calling to filter host and running the callback if | |
588 // we have received the stop signal. We need to lock down the whole callback | |
589 // method to prevent bad things from happening. The reason behind this is | |
590 // that we cannot guarantee tasks on render thread have completely stopped | |
591 // when we receive the Stop() method call. So only way to solve this is to | |
592 // let tasks on render thread to run but make sure they don't call outside | |
593 // this object when Stop() method is ever called. Locking this method is safe | |
594 // because |lock_| is only acquired in tasks on render thread. | |
595 base::AutoLock auto_lock(lock_); | |
596 if (stop_signal_received_) | |
597 return; | |
598 | |
599 if (error > 0) { | |
600 // If a position error code is received, read was successful. So copy | |
601 // from intermediate read buffer to the target read buffer. | |
602 memcpy(read_buffer_, intermediate_read_buffer_.get(), error); | |
603 } else if (error == 0 && total_bytes_ == kPositionNotSpecified) { | |
604 // We've reached the end of the file and we didn't know the total size | |
605 // before. Update the total size so Read()s past the end of the file will | |
606 // fail like they would if we had known the file size at the beginning. | |
607 total_bytes_ = loader_->instance_size(); | |
608 | |
609 if (host() && total_bytes_ != kPositionNotSpecified) | |
610 host()->SetTotalBytes(total_bytes_); | |
611 } | |
612 DoneRead_Locked(error); | |
613 } | |
614 | |
615 void BufferedDataSource::NetworkEventCallback() { | |
616 DCHECK(MessageLoop::current() == render_loop_); | |
617 DCHECK(loader_.get()); | |
618 | |
619 // In case of non-HTTP request we don't need to report network events, | |
620 // so return immediately. | |
621 if (loaded_) | |
622 return; | |
623 | |
624 bool is_downloading_data = loader_->is_downloading_data(); | |
625 int64 buffered_position = loader_->GetBufferedPosition(); | |
626 | |
627 // If we get an unspecified value, return immediately. | |
628 if (buffered_position == kPositionNotSpecified) | |
629 return; | |
630 | |
631 // We need to prevent calling to filter host and running the callback if | |
632 // we have received the stop signal. We need to lock down the whole callback | |
633 // method to prevent bad things from happening. The reason behind this is | |
634 // that we cannot guarantee tasks on render thread have completely stopped | |
635 // when we receive the Stop() method call. So only way to solve this is to | |
636 // let tasks on render thread to run but make sure they don't call outside | |
637 // this object when Stop() method is ever called. Locking this method is safe | |
638 // because |lock_| is only acquired in tasks on render thread. | |
639 base::AutoLock auto_lock(lock_); | |
640 if (stop_signal_received_) | |
641 return; | |
642 | |
643 if (is_downloading_data != is_downloading_data_) { | |
644 is_downloading_data_ = is_downloading_data; | |
645 if (host()) | |
646 host()->SetNetworkActivity(is_downloading_data); | |
647 } | |
648 | |
649 buffered_bytes_ = buffered_position + 1; | |
650 if (host()) | |
651 host()->SetBufferedBytes(buffered_bytes_); | |
652 } | |
653 | |
654 void BufferedDataSource::UpdateHostState_Locked() { | |
655 // Called from various threads, under lock. | |
656 lock_.AssertAcquired(); | |
657 | |
658 media::FilterHost* filter_host = host(); | |
659 if (!filter_host) | |
660 return; | |
661 | |
662 filter_host->SetLoaded(loaded_); | |
663 | |
664 if (streaming_) { | |
665 filter_host->SetStreaming(true); | |
666 } else { | |
667 filter_host->SetTotalBytes(total_bytes_); | |
668 filter_host->SetBufferedBytes(buffered_bytes_); | |
669 } | |
670 } | |
671 | |
672 } // namespace webkit_glue | |
OLD | NEW |