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/buffered_resource_loader.h" | 5 #include "media/blink/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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 // Maximum number of bytes outside the buffer we will wait for in order to | 48 // Maximum number of bytes outside the buffer we will wait for in order to |
49 // fulfill a read. If a read starts more than 2MB away from the data we | 49 // fulfill a read. If a read starts more than 2MB away from the data we |
50 // currently have in the buffer, we will not wait for buffer to reach the read's | 50 // currently have in the buffer, we will not wait for buffer to reach the read's |
51 // location and will instead reset the request. | 51 // location and will instead reset the request. |
52 static const int kForwardWaitThreshold = 2 * kMegabyte; | 52 static const int kForwardWaitThreshold = 2 * kMegabyte; |
53 | 53 |
54 // Computes the suggested backward and forward capacity for the buffer | 54 // Computes the suggested backward and forward capacity for the buffer |
55 // if one wants to play at |playback_rate| * the natural playback speed. | 55 // if one wants to play at |playback_rate| * the natural playback speed. |
56 // Use a value of 0 for |bitrate| if it is unknown. | 56 // Use a value of 0 for |bitrate| if it is unknown. |
57 static void ComputeTargetBufferWindow(float playback_rate, int bitrate, | 57 static void ComputeTargetBufferWindow(double playback_rate, int bitrate, |
58 int* out_backward_capacity, | 58 int* out_backward_capacity, |
59 int* out_forward_capacity) { | 59 int* out_forward_capacity) { |
60 static const int kDefaultBitrate = 200 * 1024 * 8; // 200 Kbps. | 60 static const int kDefaultBitrate = 200 * 1024 * 8; // 200 Kbps. |
61 static const int kMaxBitrate = 20 * kMegabyte * 8; // 20 Mbps. | 61 static const int kMaxBitrate = 20 * kMegabyte * 8; // 20 Mbps. |
62 static const float kMaxPlaybackRate = 25.0; | 62 static const double kMaxPlaybackRate = 25.0; |
63 static const int kTargetSecondsBufferedAhead = 10; | 63 static const int kTargetSecondsBufferedAhead = 10; |
64 static const int kTargetSecondsBufferedBehind = 2; | 64 static const int kTargetSecondsBufferedBehind = 2; |
65 | 65 |
66 // Use a default bit rate if unknown and clamp to prevent overflow. | 66 // Use a default bit rate if unknown and clamp to prevent overflow. |
67 if (bitrate <= 0) | 67 if (bitrate <= 0) |
68 bitrate = kDefaultBitrate; | 68 bitrate = kDefaultBitrate; |
69 bitrate = std::min(bitrate, kMaxBitrate); | 69 bitrate = std::min(bitrate, kMaxBitrate); |
70 | 70 |
71 // Only scale the buffer window for playback rates greater than 1.0 in | 71 // Only scale the buffer window for playback rates greater than 1.0 in |
72 // magnitude and clamp to prevent overflow. | 72 // magnitude and clamp to prevent overflow. |
73 bool backward_playback = false; | 73 bool backward_playback = false; |
74 if (playback_rate < 0.0f) { | 74 if (playback_rate < 0.0) { |
75 backward_playback = true; | 75 backward_playback = true; |
76 playback_rate *= -1.0f; | 76 playback_rate *= -1.0; |
77 } | 77 } |
78 | 78 |
79 playback_rate = std::max(playback_rate, 1.0f); | 79 playback_rate = std::max(playback_rate, 1.0); |
80 playback_rate = std::min(playback_rate, kMaxPlaybackRate); | 80 playback_rate = std::min(playback_rate, kMaxPlaybackRate); |
81 | 81 |
82 int bytes_per_second = (bitrate / 8.0) * playback_rate; | 82 int bytes_per_second = (bitrate / 8.0) * playback_rate; |
83 | 83 |
84 // Clamp between kMinBufferCapacity and kMaxBufferCapacity. | 84 // Clamp between kMinBufferCapacity and kMaxBufferCapacity. |
85 *out_forward_capacity = std::max( | 85 *out_forward_capacity = std::max( |
86 kTargetSecondsBufferedAhead * bytes_per_second, kMinBufferCapacity); | 86 kTargetSecondsBufferedAhead * bytes_per_second, kMinBufferCapacity); |
87 *out_backward_capacity = std::max( | 87 *out_backward_capacity = std::max( |
88 kTargetSecondsBufferedBehind * bytes_per_second, kMinBufferCapacity); | 88 kTargetSecondsBufferedBehind * bytes_per_second, kMinBufferCapacity); |
89 | 89 |
90 *out_forward_capacity = std::min(*out_forward_capacity, kMaxBufferCapacity); | 90 *out_forward_capacity = std::min(*out_forward_capacity, kMaxBufferCapacity); |
91 *out_backward_capacity = std::min(*out_backward_capacity, kMaxBufferCapacity); | 91 *out_backward_capacity = std::min(*out_backward_capacity, kMaxBufferCapacity); |
92 | 92 |
93 if (backward_playback) | 93 if (backward_playback) |
94 std::swap(*out_forward_capacity, *out_backward_capacity); | 94 std::swap(*out_forward_capacity, *out_backward_capacity); |
95 } | 95 } |
96 | 96 |
97 BufferedResourceLoader::BufferedResourceLoader( | 97 BufferedResourceLoader::BufferedResourceLoader( |
98 const GURL& url, | 98 const GURL& url, |
99 CORSMode cors_mode, | 99 CORSMode cors_mode, |
100 int64 first_byte_position, | 100 int64 first_byte_position, |
101 int64 last_byte_position, | 101 int64 last_byte_position, |
102 DeferStrategy strategy, | 102 DeferStrategy strategy, |
103 int bitrate, | 103 int bitrate, |
104 float playback_rate, | 104 double playback_rate, |
105 MediaLog* media_log) | 105 MediaLog* media_log) |
106 : buffer_(kMinBufferCapacity, kMinBufferCapacity), | 106 : buffer_(kMinBufferCapacity, kMinBufferCapacity), |
107 loader_failed_(false), | 107 loader_failed_(false), |
108 defer_strategy_(strategy), | 108 defer_strategy_(strategy), |
109 might_be_reused_from_cache_in_future_(true), | 109 might_be_reused_from_cache_in_future_(true), |
110 range_supported_(false), | 110 range_supported_(false), |
111 saved_forward_capacity_(0), | 111 saved_forward_capacity_(0), |
112 url_(url), | 112 url_(url), |
113 cors_mode_(cors_mode), | 113 cors_mode_(cors_mode), |
114 first_byte_position_(first_byte_position), | 114 first_byte_position_(first_byte_position), |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 return !loader_failed_ && cors_mode_ != kUnspecified; | 564 return !loader_failed_ && cors_mode_ != kUnspecified; |
565 } | 565 } |
566 | 566 |
567 void BufferedResourceLoader::UpdateDeferStrategy(DeferStrategy strategy) { | 567 void BufferedResourceLoader::UpdateDeferStrategy(DeferStrategy strategy) { |
568 if (!might_be_reused_from_cache_in_future_ && strategy == kNeverDefer) | 568 if (!might_be_reused_from_cache_in_future_ && strategy == kNeverDefer) |
569 strategy = kCapacityDefer; | 569 strategy = kCapacityDefer; |
570 defer_strategy_ = strategy; | 570 defer_strategy_ = strategy; |
571 UpdateDeferBehavior(); | 571 UpdateDeferBehavior(); |
572 } | 572 } |
573 | 573 |
574 void BufferedResourceLoader::SetPlaybackRate(float playback_rate) { | 574 void BufferedResourceLoader::SetPlaybackRate(double playback_rate) { |
575 playback_rate_ = playback_rate; | 575 playback_rate_ = playback_rate; |
576 | 576 |
577 // This is a pause so don't bother updating the buffer window as we'll likely | 577 // This is a pause so don't bother updating the buffer window as we'll likely |
578 // get unpaused in the future. | 578 // get unpaused in the future. |
579 if (playback_rate_ == 0.0) | 579 if (playback_rate_ == 0.0) |
580 return; | 580 return; |
581 | 581 |
582 // Abort any cancellations in progress if playback starts. | 582 // Abort any cancellations in progress if playback starts. |
583 if (playback_rate_ > 0 && cancel_upon_deferral_) | 583 if (playback_rate_ > 0 && cancel_upon_deferral_) |
584 cancel_upon_deferral_ = false; | 584 cancel_upon_deferral_ = false; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 | 795 |
796 void BufferedResourceLoader::Log() { | 796 void BufferedResourceLoader::Log() { |
797 media_log_->AddEvent( | 797 media_log_->AddEvent( |
798 media_log_->CreateBufferedExtentsChangedEvent( | 798 media_log_->CreateBufferedExtentsChangedEvent( |
799 offset_ - buffer_.backward_bytes(), | 799 offset_ - buffer_.backward_bytes(), |
800 offset_, | 800 offset_, |
801 offset_ + buffer_.forward_bytes())); | 801 offset_ + buffer_.forward_bytes())); |
802 } | 802 } |
803 | 803 |
804 } // namespace media | 804 } // namespace media |
OLD | NEW |