| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "media/blink/multibuffer_data_source.h" | 5 #include "media/blink/multibuffer_data_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 169 |
| 170 // We're not allowed to call Wait() if data is already available. | 170 // We're not allowed to call Wait() if data is already available. |
| 171 if (reader_->Available()) { | 171 if (reader_->Available()) { |
| 172 render_task_runner_->PostTask( | 172 render_task_runner_->PostTask( |
| 173 FROM_HERE, | 173 FROM_HERE, |
| 174 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); | 174 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); |
| 175 } else { | 175 } else { |
| 176 reader_->Wait(1, | 176 reader_->Wait(1, |
| 177 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); | 177 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); |
| 178 } | 178 } |
| 179 UpdateLoadingState(); | |
| 180 } | 179 } |
| 181 | 180 |
| 182 void MultibufferDataSource::OnRedirect( | 181 void MultibufferDataSource::OnRedirect( |
| 183 const scoped_refptr<UrlData>& destination) { | 182 const scoped_refptr<UrlData>& destination) { |
| 184 if (!destination) { | 183 if (!destination) { |
| 185 // A failure occured. | 184 // A failure occured. |
| 186 if (!init_cb_.is_null()) { | 185 if (!init_cb_.is_null()) { |
| 187 render_task_runner_->PostTask( | 186 render_task_runner_->PostTask( |
| 188 FROM_HERE, | 187 FROM_HERE, |
| 189 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); | 188 base::Bind(&MultibufferDataSource::StartCallback, weak_ptr_)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 FROM_HERE, base::Bind(&MultibufferDataSource::ReadTask, weak_ptr_)); | 222 FROM_HERE, base::Bind(&MultibufferDataSource::ReadTask, weak_ptr_)); |
| 224 } else { | 223 } else { |
| 225 reader_->Wait(1, | 224 reader_->Wait(1, |
| 226 base::Bind(&MultibufferDataSource::ReadTask, weak_ptr_)); | 225 base::Bind(&MultibufferDataSource::ReadTask, weak_ptr_)); |
| 227 } | 226 } |
| 228 } | 227 } |
| 229 } | 228 } |
| 230 } | 229 } |
| 231 | 230 |
| 232 void MultibufferDataSource::SetPreload(Preload preload) { | 231 void MultibufferDataSource::SetPreload(Preload preload) { |
| 232 DVLOG(1) << __FUNCTION__ << "(" << preload << ")"; |
| 233 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 233 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
| 234 preload_ = preload; | 234 preload_ = preload; |
| 235 UpdateBufferSizes(); | 235 UpdateBufferSizes(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void MultibufferDataSource::SetBufferingStrategy( | 238 void MultibufferDataSource::SetBufferingStrategy( |
| 239 BufferingStrategy buffering_strategy) { | 239 BufferingStrategy buffering_strategy) { |
| 240 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 240 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
| 241 buffering_strategy_ = buffering_strategy; | 241 buffering_strategy_ = buffering_strategy; |
| 242 UpdateBufferSizes(); | 242 UpdateBufferSizes(); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 return; | 386 return; |
| 387 } | 387 } |
| 388 if (available) { | 388 if (available) { |
| 389 bytes_read = | 389 bytes_read = |
| 390 static_cast<int>(std::min<int64_t>(available, read_op_->size())); | 390 static_cast<int>(std::min<int64_t>(available, read_op_->size())); |
| 391 bytes_read = reader_->TryRead(read_op_->data(), bytes_read); | 391 bytes_read = reader_->TryRead(read_op_->data(), bytes_read); |
| 392 ReadOperation::Run(read_op_.Pass(), bytes_read); | 392 ReadOperation::Run(read_op_.Pass(), bytes_read); |
| 393 } else { | 393 } else { |
| 394 reader_->Wait(1, base::Bind(&MultibufferDataSource::ReadTask, | 394 reader_->Wait(1, base::Bind(&MultibufferDataSource::ReadTask, |
| 395 weak_factory_.GetWeakPtr())); | 395 weak_factory_.GetWeakPtr())); |
| 396 UpdateLoadingState(); | 396 UpdateLoadingState(false); |
| 397 } | 397 } |
| 398 } | 398 } |
| 399 | 399 |
| 400 void MultibufferDataSource::StopInternal_Locked() { | 400 void MultibufferDataSource::StopInternal_Locked() { |
| 401 lock_.AssertAcquired(); | 401 lock_.AssertAcquired(); |
| 402 if (stop_signal_received_) | 402 if (stop_signal_received_) |
| 403 return; | 403 return; |
| 404 | 404 |
| 405 stop_signal_received_ = true; | 405 stop_signal_received_ = true; |
| 406 | 406 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 | 469 |
| 470 // Progress callback might be called after the start callback, | 470 // Progress callback might be called after the start callback, |
| 471 // make sure that we update single_origin_ now. | 471 // make sure that we update single_origin_ now. |
| 472 media_log_->SetBooleanProperty("single_origin", single_origin_); | 472 media_log_->SetBooleanProperty("single_origin", single_origin_); |
| 473 media_log_->SetBooleanProperty("passed_cors_access_check", | 473 media_log_->SetBooleanProperty("passed_cors_access_check", |
| 474 DidPassCORSAccessCheck()); | 474 DidPassCORSAccessCheck()); |
| 475 media_log_->SetBooleanProperty("range_header_supported", | 475 media_log_->SetBooleanProperty("range_header_supported", |
| 476 url_data_->range_supported()); | 476 url_data_->range_supported()); |
| 477 } | 477 } |
| 478 | 478 |
| 479 UpdateLoadingState(); | |
| 480 render_task_runner_->PostTask( | 479 render_task_runner_->PostTask( |
| 481 FROM_HERE, base::Bind(base::ResetAndReturn(&init_cb_), success)); | 480 FROM_HERE, base::Bind(base::ResetAndReturn(&init_cb_), success)); |
| 481 |
| 482 // Even if data is cached, say that we're loading at this point for |
| 483 // compatibility. |
| 484 UpdateLoadingState(true); |
| 482 } | 485 } |
| 483 | 486 |
| 484 void MultibufferDataSource::ProgressCallback(int64 begin, int64 end) { | 487 void MultibufferDataSource::ProgressCallback(int64 begin, int64 end) { |
| 488 DVLOG(1) << __FUNCTION__ << "(" << begin << ", " << end << ")"; |
| 485 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 489 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
| 486 | 490 |
| 487 if (assume_fully_buffered()) | 491 if (assume_fully_buffered()) |
| 488 return; | 492 return; |
| 489 | 493 |
| 490 if (end > begin) { | 494 if (end > begin) { |
| 491 // TODO(scherkus): we shouldn't have to lock to signal host(), see | 495 // TODO(scherkus): we shouldn't have to lock to signal host(), see |
| 492 // http://crbug.com/113712 for details. | 496 // http://crbug.com/113712 for details. |
| 493 base::AutoLock auto_lock(lock_); | 497 base::AutoLock auto_lock(lock_); |
| 494 if (stop_signal_received_) | 498 if (stop_signal_received_) |
| 495 return; | 499 return; |
| 496 | 500 |
| 497 host_->AddBufferedByteRange(begin, end); | 501 host_->AddBufferedByteRange(begin, end); |
| 498 } | 502 } |
| 499 | 503 |
| 500 UpdateLoadingState(); | 504 UpdateLoadingState(false); |
| 501 } | 505 } |
| 502 | 506 |
| 503 void MultibufferDataSource::UpdateLoadingState() { | 507 void MultibufferDataSource::UpdateLoadingState(bool force_loading) { |
| 508 DVLOG(1) << __FUNCTION__; |
| 509 if (assume_fully_buffered()) |
| 510 return; |
| 504 // Update loading state. | 511 // Update loading state. |
| 505 if ((!!reader_ && reader_->IsLoading()) != loading_) { | 512 bool is_loading = !!reader_ && reader_->IsLoading(); |
| 506 loading_ = !loading_; | 513 if (force_loading || is_loading != loading_) { |
| 514 loading_ = is_loading || force_loading; |
| 507 | 515 |
| 508 if (!loading_ && cancel_on_defer_) { | 516 if (!loading_ && cancel_on_defer_) { |
| 509 reader_.reset(nullptr); | 517 reader_.reset(nullptr); |
| 510 } | 518 } |
| 511 | 519 |
| 512 // Callback could kill us, be sure to call it last. | 520 // Callback could kill us, be sure to call it last. |
| 513 downloading_cb_.Run(loading_); | 521 downloading_cb_.Run(loading_); |
| 514 } | 522 } |
| 515 } | 523 } |
| 516 | 524 |
| 517 void MultibufferDataSource::UpdateBufferSizes() { | 525 void MultibufferDataSource::UpdateBufferSizes() { |
| 526 DVLOG(1) << __FUNCTION__; |
| 518 if (!reader_) | 527 if (!reader_) |
| 519 return; | 528 return; |
| 520 | 529 |
| 521 if (!assume_fully_buffered()) { | 530 if (!assume_fully_buffered()) { |
| 522 // If the playback has started and the strategy is aggressive, then try to | 531 // If the playback has started and the strategy is aggressive, then try to |
| 523 // load as much as possible, assuming that the file is cacheable. (If not, | 532 // load as much as possible, assuming that the file is cacheable. (If not, |
| 524 // why bother?) | 533 // why bother?) |
| 525 bool aggressive = (buffering_strategy_ == BUFFERING_STRATEGY_AGGRESSIVE); | 534 bool aggressive = (buffering_strategy_ == BUFFERING_STRATEGY_AGGRESSIVE); |
| 526 if (media_has_played_ && aggressive && url_data_ && | 535 if (media_has_played_ && aggressive && url_data_ && |
| 527 url_data_->range_supported() && url_data_->cacheable()) { | 536 url_data_->range_supported() && url_data_->cacheable()) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 553 reader_->SetMaxBuffer(back_buffer, pin_forwards); | 562 reader_->SetMaxBuffer(back_buffer, pin_forwards); |
| 554 | 563 |
| 555 if (preload_ == METADATA) { | 564 if (preload_ == METADATA) { |
| 556 reader_->SetPreload(0, 0); | 565 reader_->SetPreload(0, 0); |
| 557 } else { | 566 } else { |
| 558 reader_->SetPreload(preload + kPreloadHighExtra, preload); | 567 reader_->SetPreload(preload + kPreloadHighExtra, preload); |
| 559 } | 568 } |
| 560 } | 569 } |
| 561 | 570 |
| 562 } // namespace media | 571 } // namespace media |
| OLD | NEW |