OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/base/download_rate_monitor.h" | 5 #include "media/base/download_rate_monitor.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 buffered_bytes_ = 0; | 84 buffered_bytes_ = 0; |
85 | 85 |
86 NotifyCanPlayThroughIfNeeded(); | 86 NotifyCanPlayThroughIfNeeded(); |
87 } | 87 } |
88 | 88 |
89 void DownloadRateMonitor::SetBufferedBytes( | 89 void DownloadRateMonitor::SetBufferedBytes( |
90 int64 buffered_bytes, const base::Time& timestamp) { | 90 int64 buffered_bytes, const base::Time& timestamp) { |
91 if (stopped_) | 91 if (stopped_) |
92 return; | 92 return; |
93 | 93 |
| 94 is_downloading_data_ = true; |
| 95 |
94 // Check monotonically nondecreasing constraint. | 96 // Check monotonically nondecreasing constraint. |
95 base::Time previous_time; | 97 base::Time previous_time; |
96 if (!current_sample_.is_null()) | 98 if (!current_sample_.is_null()) |
97 previous_time = current_sample_.end().timestamp; | 99 previous_time = current_sample_.end().timestamp; |
98 else if (!sample_window_.empty()) | 100 else if (!sample_window_.empty()) |
99 previous_time = sample_window_.back().end().timestamp; | 101 previous_time = sample_window_.back().end().timestamp; |
100 | 102 |
101 // If we go backward in time, dismiss the sample. | 103 // If we go backward in time, dismiss the sample. |
102 if (!previous_time.is_null() && timestamp < previous_time) | 104 if (!previous_time.is_null() && timestamp < previous_time) |
103 return; | 105 return; |
104 | 106 |
105 // If the buffer level has dropped, invalidate current sample. | 107 // If the buffer level has dropped, invalidate current sample. |
106 if (buffered_bytes < buffered_bytes_) | 108 if (buffered_bytes < buffered_bytes_) |
107 current_sample_.Reset(); | 109 current_sample_.Reset(); |
108 buffered_bytes_ = buffered_bytes; | 110 buffered_bytes_ = buffered_bytes; |
109 | 111 |
110 BufferingPoint latest_point = { buffered_bytes, timestamp }; | 112 BufferingPoint latest_point = { buffered_bytes, timestamp }; |
111 if (current_sample_.is_null()) | 113 if (current_sample_.is_null()) |
112 current_sample_ = Sample(latest_point, latest_point); | 114 current_sample_ = Sample(latest_point, latest_point); |
113 else | 115 else |
114 current_sample_.set_end(latest_point); | 116 current_sample_.set_end(latest_point); |
115 | 117 |
116 UpdateSampleWindow(); | 118 UpdateSampleWindow(); |
117 NotifyCanPlayThroughIfNeeded(); | 119 NotifyCanPlayThroughIfNeeded(); |
118 } | 120 } |
119 | 121 |
120 void DownloadRateMonitor::SetNetworkActivity(bool is_downloading_data) { | 122 void DownloadRateMonitor::SetNetworkActivity(bool is_downloading_data) { |
121 // Record when download defers for the first time. | 123 if (is_downloading_data == is_downloading_data_) |
122 if (!is_downloading_data && !has_deferred_) { | 124 return; |
123 has_deferred_ = true; | 125 // Invalidate the current sample if downloading is going from start to stopped |
124 NotifyCanPlayThroughIfNeeded(); | 126 // or vice versa. |
125 } | 127 current_sample_.Reset(); |
| 128 is_downloading_data_ = is_downloading_data; |
126 } | 129 } |
127 | 130 |
128 void DownloadRateMonitor::Stop() { | 131 void DownloadRateMonitor::Stop() { |
129 stopped_ = true; | 132 stopped_ = true; |
130 current_sample_.Reset(); | 133 current_sample_.Reset(); |
131 buffered_bytes_ = 0; | 134 buffered_bytes_ = 0; |
132 } | 135 } |
133 | 136 |
134 void DownloadRateMonitor::Reset() { | 137 void DownloadRateMonitor::Reset() { |
135 canplaythrough_cb_.Reset(); | 138 canplaythrough_cb_.Reset(); |
136 has_notified_can_play_through_ = false; | 139 has_notified_can_play_through_ = false; |
137 current_sample_.Reset(); | 140 current_sample_.Reset(); |
138 sample_window_.clear(); | 141 sample_window_.clear(); |
| 142 is_downloading_data_ = false; |
139 total_bytes_ = -1; | 143 total_bytes_ = -1; |
140 buffered_bytes_ = 0; | 144 buffered_bytes_ = 0; |
141 local_source_ = false; | 145 local_source_ = false; |
142 bitrate_ = 0; | 146 bitrate_ = 0; |
143 stopped_ = true; | 147 stopped_ = true; |
144 streaming_ = false; | 148 streaming_ = false; |
145 has_deferred_ = false; | |
146 } | 149 } |
147 | 150 |
148 DownloadRateMonitor::~DownloadRateMonitor() { } | 151 DownloadRateMonitor::~DownloadRateMonitor() { } |
149 | 152 |
150 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { | 153 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { |
151 // There are max |kNumberOfSamples| so we might as well recompute each time. | 154 // There are max |kNumberOfSamples| so we might as well recompute each time. |
152 int64 total = 0; | 155 int64 total = 0; |
153 for (size_t i = 0; i < sample_window_.size(); ++i) | 156 for (size_t i = 0; i < sample_window_.size(); ++i) |
154 total += sample_window_[i].bytes_downloaded(); | 157 total += sample_window_[i].bytes_downloaded(); |
155 return total; | 158 return total; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 return false; | 203 return false; |
201 | 204 |
202 // Fire CanPlayThrough immediately if the source is local or streaming. | 205 // Fire CanPlayThrough immediately if the source is local or streaming. |
203 // | 206 // |
204 // NOTE: It is a requirement for CanPlayThrough to fire immediately if the | 207 // NOTE: It is a requirement for CanPlayThrough to fire immediately if the |
205 // source is local, but the choice to optimistically fire the event for any | 208 // source is local, but the choice to optimistically fire the event for any |
206 // streaming media element is a design decision that may need to be tweaked. | 209 // streaming media element is a design decision that may need to be tweaked. |
207 if (local_source_ || streaming_) | 210 if (local_source_ || streaming_) |
208 return true; | 211 return true; |
209 | 212 |
210 // If all bytes are buffered or if enough bytes were buffered such that | 213 // If all bytes are buffered, fire CanPlayThrough. |
211 // downloading has deferred, fire CanPlayThrough. | 214 if (buffered_bytes_ == total_bytes_) |
212 if (buffered_bytes_ == total_bytes_ || has_deferred_) | |
213 return true; | 215 return true; |
214 | 216 |
215 // If bitrate is unknown, optimistically fire CanPlayThrough immediately. | 217 // If bitrate is unknown, optimistically fire CanPlayThrough immediately. |
216 // This is so a video with an unknown bitrate with the "autoplay" attribute | 218 // This is so a video with an unknown bitrate with the "autoplay" attribute |
217 // will not wait until the entire file is downloaded before playback begins. | 219 // will not wait until the entire file is downloaded before playback begins. |
218 if (bitrate_ <= 0) | 220 if (bitrate_ <= 0) |
219 return true; | 221 return true; |
220 | 222 |
221 float bytes_needed_per_second = bitrate_ / 8; | 223 float bytes_needed_per_second = bitrate_ / 8; |
222 float download_rate = ApproximateDownloadByteRate(); | 224 float download_rate = ApproximateDownloadByteRate(); |
223 | 225 |
224 // If we are downloading at or faster than the media's bitrate, then we can | 226 // If we are downloading at or faster than the media's bitrate, then we can |
225 // play through to the end of the media without stopping to buffer. | 227 // play through to the end of the media without stopping to buffer. |
226 if (download_rate > 0) | 228 if (download_rate > 0) |
227 return download_rate >= bytes_needed_per_second; | 229 return download_rate >= bytes_needed_per_second; |
228 | 230 |
229 // With very fast connections, we may want to fire CanPlayThrough before | 231 // If download rate is unknown, it may be because the media is being |
230 // waiting for the sample window size to reach |kNumberOfSamples|. Check for | 232 // downloaded so fast that it cannot collect an adequate number of samples |
231 // this scenario. | 233 // before the download gets deferred. |
| 234 // |
| 235 // To catch this case, we also look at how much data is being downloaded |
| 236 // immediately after the download begins. |
232 if (sample_window_.size() < kNumberOfSamples) { | 237 if (sample_window_.size() < kNumberOfSamples) { |
233 int64 bytes_downloaded_since_start = | 238 int64 bytes_downloaded_since_start = |
234 bytes_downloaded_in_window() + current_sample_.bytes_downloaded(); | 239 bytes_downloaded_in_window() + current_sample_.bytes_downloaded(); |
235 float seconds_elapsed_since_start = | 240 float seconds_elapsed_since_start = |
236 seconds_elapsed_in_window() + current_sample_.seconds_elapsed(); | 241 seconds_elapsed_in_window() + current_sample_.seconds_elapsed(); |
237 | 242 |
238 // If we download 4 seconds of data in less than 2 seconds of time, we're | 243 // If we download 4 seconds of data in less than 2 seconds of time, we're |
239 // probably downloading at a fast enough rate that we can play through. | 244 // probably downloading at a fast enough rate that we can play through. |
240 // This is an arbitrary metric that will likely need tweaking. | 245 // This is an arbitrary metric that will likely need tweaking. |
241 if (seconds_elapsed_since_start < 2.0 && | 246 if (seconds_elapsed_since_start < 2.0 && |
242 bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) { | 247 bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) { |
243 return true; | 248 return true; |
244 } | 249 } |
245 } | 250 } |
246 | 251 |
247 return false; | 252 return false; |
248 } | 253 } |
249 | 254 |
250 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { | 255 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { |
251 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { | 256 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { |
252 canplaythrough_cb_.Run(); | 257 canplaythrough_cb_.Run(); |
253 has_notified_can_play_through_ = true; | 258 has_notified_can_play_through_ = true; |
254 } | 259 } |
255 } | 260 } |
256 | 261 |
257 } // namespace media | 262 } // namespace media |
OLD | NEW |