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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 // effectively the largest single read the code path can handle. | 46 // effectively the largest single read the code path can handle. |
47 // 20MB is an arbitrary limit; it just seems to be "good enough" in practice. | 47 // 20MB is an arbitrary limit; it just seems to be "good enough" in practice. |
48 static const int kMaxBufferCapacity = 20 * kMegabyte; | 48 static const int kMaxBufferCapacity = 20 * kMegabyte; |
49 | 49 |
50 // Maximum number of bytes outside the buffer we will wait for in order to | 50 // Maximum number of bytes outside the buffer we will wait for in order to |
51 // fulfill a read. If a read starts more than 2MB away from the data we | 51 // fulfill a read. If a read starts more than 2MB away from the data we |
52 // currently have in the buffer, we will not wait for buffer to reach the read's | 52 // currently have in the buffer, we will not wait for buffer to reach the read's |
53 // location and will instead reset the request. | 53 // location and will instead reset the request. |
54 static const int kForwardWaitThreshold = 2 * kMegabyte; | 54 static const int kForwardWaitThreshold = 2 * kMegabyte; |
55 | 55 |
56 // The lower bound on our buffer (expressed as a fraction of the buffer size) | |
57 // where we'll disable deferring and continue downloading data. | |
58 // | |
59 // TODO(scherkus): refer to http://crbug.com/124719 for more discussion on | |
60 // how we could improve our buffering logic. | |
61 static const double kDisableDeferThreshold = 0.9; | |
62 | |
63 // Computes the suggested backward and forward capacity for the buffer | 56 // Computes the suggested backward and forward capacity for the buffer |
64 // if one wants to play at |playback_rate| * the natural playback speed. | 57 // if one wants to play at |playback_rate| * the natural playback speed. |
65 // Use a value of 0 for |bitrate| if it is unknown. | 58 // Use a value of 0 for |bitrate| if it is unknown. |
66 static void ComputeTargetBufferWindow(float playback_rate, int bitrate, | 59 static void ComputeTargetBufferWindow(float playback_rate, int bitrate, |
67 int* out_backward_capacity, | 60 int* out_backward_capacity, |
68 int* out_forward_capacity) { | 61 int* out_forward_capacity) { |
69 static const int kDefaultBitrate = 200 * 1024 * 8; // 200 Kbps. | 62 static const int kDefaultBitrate = 200 * 1024 * 8; // 200 Kbps. |
70 static const int kMaxBitrate = 20 * kMegabyte * 8; // 20 Mbps. | 63 static const int kMaxBitrate = 20 * kMegabyte * 8; // 20 Mbps. |
71 static const float kMaxPlaybackRate = 25.0; | 64 static const float kMaxPlaybackRate = 25.0; |
72 static const int kTargetSecondsBufferedAhead = 10; | 65 static const int kTargetSecondsBufferedAhead = 10; |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 | 611 |
619 switch(defer_strategy_) { | 612 switch(defer_strategy_) { |
620 // Never defer at all, so never enable defer. | 613 // Never defer at all, so never enable defer. |
621 case kNeverDefer: | 614 case kNeverDefer: |
622 return false; | 615 return false; |
623 | 616 |
624 // Defer if nothing is being requested. | 617 // Defer if nothing is being requested. |
625 case kReadThenDefer: | 618 case kReadThenDefer: |
626 return read_cb_.is_null(); | 619 return read_cb_.is_null(); |
627 | 620 |
628 // Defer if we've reached the max capacity of the threshold. | 621 // Defer if we've reached max capacity. |
629 case kThresholdDefer: | 622 case kThresholdDefer: |
630 return buffer_.forward_bytes() >= buffer_.forward_capacity(); | 623 return buffer_.forward_bytes() >= buffer_.forward_capacity(); |
631 } | 624 } |
632 // Otherwise don't enable defer. | 625 // Otherwise don't enable defer. |
633 return false; | 626 return false; |
634 } | 627 } |
635 | 628 |
636 bool BufferedResourceLoader::ShouldDisableDefer() const { | 629 bool BufferedResourceLoader::ShouldDisableDefer() const { |
637 // If we're not deferring, then disabling makes no sense. | 630 // If we're not deferring, then disabling makes no sense. |
638 if (!active_loader_->deferred()) | 631 if (!active_loader_->deferred()) |
639 return false; | 632 return false; |
640 | 633 |
641 switch(defer_strategy_) { | 634 switch(defer_strategy_) { |
642 // Always disable deferring. | 635 // Always disable deferring. |
643 case kNeverDefer: | 636 case kNeverDefer: |
644 return true; | 637 return true; |
645 | 638 |
646 // We have an outstanding read request, and we have not buffered enough | 639 // We have an outstanding read request, and we have not buffered enough |
647 // yet to fulfill the request; disable defer to get more data. | 640 // yet to fulfill the request; disable defer to get more data. |
648 case kReadThenDefer: | 641 case kReadThenDefer: |
649 return !read_cb_.is_null() && last_offset_ > buffer_.forward_bytes(); | 642 return !read_cb_.is_null() && last_offset_ > buffer_.forward_bytes(); |
650 | 643 |
651 // Disable deferring whenever our forward-buffered amount falls beneath our | 644 // Disable deferring whenever our forward-buffered amount falls beneath our |
652 // threshold. | 645 // capacity. |
653 // | 646 // |
654 // TODO(scherkus): refer to http://crbug.com/124719 for more discussion on | 647 // TODO(scherkus): refer to http://crbug.com/124719 for more discussion on |
Ami GONE FROM CHROMIUM
2012/07/09 17:27:41
still needed?
scherkus (not reviewing)
2012/07/11 00:49:37
Done.
| |
655 // how we could improve our buffering logic. | 648 // how we could improve our buffering logic. |
656 case kThresholdDefer: { | 649 case kThresholdDefer: { |
Ami GONE FROM CHROMIUM
2012/07/09 17:27:41
braces no longer necessary
scherkus (not reviewing)
2012/07/11 00:49:37
Done.
| |
657 int buffered = buffer_.forward_bytes(); | 650 return buffer_.forward_bytes() < buffer_.forward_capacity(); |
658 int threshold = buffer_.forward_capacity() * kDisableDeferThreshold; | |
659 return buffered < threshold; | |
660 } | 651 } |
661 } | 652 } |
662 | 653 |
663 // Otherwise keep deferring. | 654 // Otherwise keep deferring. |
664 return false; | 655 return false; |
665 } | 656 } |
666 | 657 |
667 bool BufferedResourceLoader::CanFulfillRead() const { | 658 bool BufferedResourceLoader::CanFulfillRead() const { |
668 // If we are reading too far in the backward direction. | 659 // If we are reading too far in the backward direction. |
669 if (first_offset_ < 0 && (first_offset_ + buffer_.backward_bytes()) < 0) | 660 if (first_offset_ < 0 && (first_offset_ + buffer_.backward_bytes()) < 0) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
832 | 823 |
833 void BufferedResourceLoader::Log() { | 824 void BufferedResourceLoader::Log() { |
834 media_log_->AddEvent( | 825 media_log_->AddEvent( |
835 media_log_->CreateBufferedExtentsChangedEvent( | 826 media_log_->CreateBufferedExtentsChangedEvent( |
836 offset_ - buffer_.backward_bytes(), | 827 offset_ - buffer_.backward_bytes(), |
837 offset_, | 828 offset_, |
838 offset_ + buffer_.forward_bytes())); | 829 offset_ + buffer_.forward_bytes())); |
839 } | 830 } |
840 | 831 |
841 } // namespace webkit_media | 832 } // namespace webkit_media |
OLD | NEW |