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