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 #include "webkit/media/buffered_resource_loader.h" | 5 #include "webkit/media/buffered_resource_loader.h" |
6 | 6 |
7 #include "base/bits.h" | 7 #include "base/bits.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 // capacity. | 292 // capacity. |
293 // | 293 // |
294 // This can happen when reading in a large seek index or when the | 294 // This can happen when reading in a large seek index or when the |
295 // first byte of a read request falls within kForwardWaitThreshold. | 295 // first byte of a read request falls within kForwardWaitThreshold. |
296 if (last_offset_ > buffer_.forward_capacity()) { | 296 if (last_offset_ > buffer_.forward_capacity()) { |
297 saved_forward_capacity_ = buffer_.forward_capacity(); | 297 saved_forward_capacity_ = buffer_.forward_capacity(); |
298 buffer_.set_forward_capacity(last_offset_); | 298 buffer_.set_forward_capacity(last_offset_); |
299 } | 299 } |
300 | 300 |
301 // Make sure we stop deferring now that there's additional capacity. | 301 // Make sure we stop deferring now that there's additional capacity. |
302 if (active_loader_->deferred()) | 302 DCHECK(!ShouldDefer()) |
303 SetDeferred(false); | |
304 | |
305 DCHECK(!ShouldEnableDefer()) | |
306 << "Capacity was not adjusted properly to prevent deferring."; | 303 << "Capacity was not adjusted properly to prevent deferring."; |
304 UpdateDeferBehavior(); | |
307 | 305 |
308 return; | 306 return; |
309 } | 307 } |
310 | 308 |
311 // Make a callback to report failure. | 309 // Make a callback to report failure. |
312 DoneRead(kCacheMiss, 0); | 310 DoneRead(kCacheMiss, 0); |
313 } | 311 } |
314 | 312 |
315 int64 BufferedResourceLoader::content_length() { | 313 int64 BufferedResourceLoader::content_length() { |
316 return content_length_; | 314 return content_length_; |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); | 598 playback_rate_, bitrate_, &backward_capacity, &forward_capacity); |
601 | 599 |
602 // This does not evict data from the buffer if the new capacities are less | 600 // This does not evict data from the buffer if the new capacities are less |
603 // than the current capacities; the new limits will be enforced after the | 601 // than the current capacities; the new limits will be enforced after the |
604 // existing excess buffered data is consumed. | 602 // existing excess buffered data is consumed. |
605 buffer_.set_backward_capacity(backward_capacity); | 603 buffer_.set_backward_capacity(backward_capacity); |
606 buffer_.set_forward_capacity(forward_capacity); | 604 buffer_.set_forward_capacity(forward_capacity); |
607 } | 605 } |
608 | 606 |
609 void BufferedResourceLoader::UpdateDeferBehavior() { | 607 void BufferedResourceLoader::UpdateDeferBehavior() { |
610 if (!active_loader_.get()) | 608 if (!active_loader_.get()) |
scherkus (not reviewing)
2012/07/11 18:11:51
I don't know how useful this method is or whether
| |
611 return; | 609 return; |
612 | 610 |
613 // If necessary, toggle defer state and continue/pause downloading data | 611 SetDeferred(ShouldDefer()); |
614 // accordingly. | |
615 if (ShouldEnableDefer() || ShouldDisableDefer()) | |
616 SetDeferred(!active_loader_->deferred()); | |
617 } | 612 } |
618 | 613 |
619 void BufferedResourceLoader::SetDeferred(bool deferred) { | 614 void BufferedResourceLoader::SetDeferred(bool deferred) { |
615 if (active_loader_->deferred() == deferred) | |
616 return; | |
617 | |
620 active_loader_->SetDeferred(deferred); | 618 active_loader_->SetDeferred(deferred); |
621 loading_cb_.Run(deferred ? kLoadingDeferred : kLoading); | 619 loading_cb_.Run(deferred ? kLoadingDeferred : kLoading); |
622 } | 620 } |
623 | 621 |
624 bool BufferedResourceLoader::ShouldEnableDefer() const { | 622 bool BufferedResourceLoader::ShouldDefer() const { |
625 // If we're already deferring, then enabling makes no sense. | |
626 if (active_loader_->deferred()) | |
627 return false; | |
628 | |
629 switch(defer_strategy_) { | 623 switch(defer_strategy_) { |
630 // Never defer at all, so never enable defer. | 624 // Never defer at all. |
Ami GONE FROM CHROMIUM
2012/07/11 18:36:09
This (and the comments for the other two cases) do
scherkus (not reviewing)
2012/07/12 00:21:03
what is this -- webkit!? :P
| |
631 case kNeverDefer: | 625 case kNeverDefer: |
632 return false; | 626 return false; |
633 | 627 |
634 // Defer if nothing is being requested. | 628 // Defer if there are no pending reads. |
635 case kReadThenDefer: | 629 case kReadThenDefer: |
636 return read_cb_.is_null(); | 630 return read_cb_.is_null(); |
637 | 631 |
638 // Defer if we've reached max capacity. | 632 // Defer if we've reached max capacity. |
639 case kCapacityDefer: | 633 case kCapacityDefer: |
640 return buffer_.forward_bytes() >= buffer_.forward_capacity(); | 634 return buffer_.forward_bytes() >= buffer_.forward_capacity(); |
641 } | 635 } |
642 // Otherwise don't enable defer. | |
643 return false; | 636 return false; |
Ami GONE FROM CHROMIUM
2012/07/11 18:36:09
NOTREACHED?
scherkus (not reviewing)
2012/07/12 00:21:03
Done.
| |
644 } | 637 } |
645 | 638 |
646 bool BufferedResourceLoader::ShouldDisableDefer() const { | |
647 // If we're not deferring, then disabling makes no sense. | |
648 if (!active_loader_->deferred()) | |
649 return false; | |
650 | |
651 switch(defer_strategy_) { | |
652 // Always disable deferring. | |
653 case kNeverDefer: | |
654 return true; | |
655 | |
656 // We have an outstanding read request, and we have not buffered enough | |
657 // yet to fulfill the request; disable defer to get more data. | |
658 case kReadThenDefer: | |
659 return !read_cb_.is_null() && last_offset_ > buffer_.forward_bytes(); | |
scherkus (not reviewing)
2012/07/11 18:11:51
Need some scrutiny here!!!
The last half of this
Ami GONE FROM CHROMIUM
2012/07/11 18:36:09
ISTM the thing to do is simply not call UpdateDefe
| |
660 | |
661 // Disable deferring whenever our forward-buffered amount falls beneath our | |
662 // capacity. | |
663 case kCapacityDefer: | |
664 return buffer_.forward_bytes() < buffer_.forward_capacity(); | |
665 } | |
666 | |
667 // Otherwise keep deferring. | |
668 return false; | |
669 } | |
670 | |
671 bool BufferedResourceLoader::CanFulfillRead() const { | 639 bool BufferedResourceLoader::CanFulfillRead() const { |
672 // If we are reading too far in the backward direction. | 640 // If we are reading too far in the backward direction. |
673 if (first_offset_ < 0 && (first_offset_ + buffer_.backward_bytes()) < 0) | 641 if (first_offset_ < 0 && (first_offset_ + buffer_.backward_bytes()) < 0) |
674 return false; | 642 return false; |
675 | 643 |
676 // If the start offset is too far ahead. | 644 // If the start offset is too far ahead. |
677 if (first_offset_ >= buffer_.forward_bytes()) | 645 if (first_offset_ >= buffer_.forward_bytes()) |
678 return false; | 646 return false; |
679 | 647 |
680 // At the point, we verified that first byte requested is within the buffer. | 648 // At the point, we verified that first byte requested is within the buffer. |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
835 | 803 |
836 void BufferedResourceLoader::Log() { | 804 void BufferedResourceLoader::Log() { |
837 media_log_->AddEvent( | 805 media_log_->AddEvent( |
838 media_log_->CreateBufferedExtentsChangedEvent( | 806 media_log_->CreateBufferedExtentsChangedEvent( |
839 offset_ - buffer_.backward_bytes(), | 807 offset_ - buffer_.backward_bytes(), |
840 offset_, | 808 offset_, |
841 offset_ + buffer_.forward_bytes())); | 809 offset_ + buffer_.forward_bytes())); |
842 } | 810 } |
843 | 811 |
844 } // namespace webkit_media | 812 } // namespace webkit_media |
OLD | NEW |