| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 132 } |
| 133 | 133 |
| 134 void DownloadRateMonitor::Reset() { | 134 void DownloadRateMonitor::Reset() { |
| 135 canplaythrough_cb_.Reset(); | 135 canplaythrough_cb_.Reset(); |
| 136 has_notified_can_play_through_ = false; | 136 has_notified_can_play_through_ = false; |
| 137 current_sample_.Reset(); | 137 current_sample_.Reset(); |
| 138 sample_window_.clear(); | 138 sample_window_.clear(); |
| 139 is_downloading_data_ = false; | 139 is_downloading_data_ = false; |
| 140 total_bytes_ = -1; | 140 total_bytes_ = -1; |
| 141 buffered_bytes_ = 0; | 141 buffered_bytes_ = 0; |
| 142 loaded_ = false; | 142 local_source_ = false; |
| 143 bitrate_ = 0; | 143 bitrate_ = 0; |
| 144 stopped_ = true; | 144 stopped_ = true; |
| 145 streaming_ = false; |
| 145 } | 146 } |
| 146 | 147 |
| 147 DownloadRateMonitor::~DownloadRateMonitor() { } | 148 DownloadRateMonitor::~DownloadRateMonitor() { } |
| 148 | 149 |
| 149 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { | 150 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { |
| 150 // There are max |kNumberOfSamples| so we might as well recompute each time. | 151 // There are max |kNumberOfSamples| so we might as well recompute each time. |
| 151 int64 total = 0; | 152 int64 total = 0; |
| 152 for (size_t i = 0; i < sample_window_.size(); ++i) | 153 for (size_t i = 0; i < sample_window_.size(); ++i) |
| 153 total += sample_window_[i].bytes_downloaded(); | 154 total += sample_window_[i].bytes_downloaded(); |
| 154 return total; | 155 return total; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 192 } |
| 192 | 193 |
| 193 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { | 194 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { |
| 194 if (stopped_) | 195 if (stopped_) |
| 195 return false; | 196 return false; |
| 196 | 197 |
| 197 // Only notify CanPlayThrough once for now. | 198 // Only notify CanPlayThrough once for now. |
| 198 if (has_notified_can_play_through_) | 199 if (has_notified_can_play_through_) |
| 199 return false; | 200 return false; |
| 200 | 201 |
| 201 // If the media is from a local file (|loaded_|) or if all bytes are | 202 // Fire CanPlayThrough immediately if the source is local or streaming. |
| 202 // buffered, fire CanPlayThrough. | 203 // |
| 203 if (loaded_ || buffered_bytes_ == total_bytes_) | 204 // 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 |
| 206 // streaming media element is a design decision that may need to be tweaked. |
| 207 if (local_source_ || streaming_) |
| 204 return true; | 208 return true; |
| 205 | 209 |
| 206 // Cannot approximate when the media can play through if bitrate is unknown. | 210 // If all bytes are buffered, fire CanPlayThrough. |
| 211 if (buffered_bytes_ == total_bytes_) |
| 212 return true; |
| 213 |
| 214 // If bitrate is unknown, optimistically fire CanPlayThrough immediately. |
| 215 // This is so a video with an unknown bitrate with the "autoplay" attribute |
| 216 // will not wait until the entire file is downloaded before playback begins. |
| 207 if (bitrate_ <= 0) | 217 if (bitrate_ <= 0) |
| 208 return false; | 218 return true; |
| 209 | 219 |
| 210 float bytes_needed_per_second = bitrate_ / 8; | 220 float bytes_needed_per_second = bitrate_ / 8; |
| 211 float download_rate = ApproximateDownloadByteRate(); | 221 float download_rate = ApproximateDownloadByteRate(); |
| 212 | 222 |
| 213 // If we are downloading at or faster than the media's bitrate, then we can | 223 // If we are downloading at or faster than the media's bitrate, then we can |
| 214 // play through to the end of the media without stopping to buffer. | 224 // play through to the end of the media without stopping to buffer. |
| 215 if (download_rate > 0) | 225 if (download_rate > 0) |
| 216 return download_rate >= bytes_needed_per_second; | 226 return download_rate >= bytes_needed_per_second; |
| 217 | 227 |
| 218 // If download rate is unknown, it may be because the media is being | 228 // If download rate is unknown, it may be because the media is being |
| (...skipping 21 matching lines...) Expand all Loading... |
| 240 } | 250 } |
| 241 | 251 |
| 242 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { | 252 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { |
| 243 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { | 253 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { |
| 244 canplaythrough_cb_.Run(); | 254 canplaythrough_cb_.Run(); |
| 245 has_notified_can_play_through_ = true; | 255 has_notified_can_play_through_ = true; |
| 246 } | 256 } |
| 247 } | 257 } |
| 248 | 258 |
| 249 } // namespace media | 259 } // namespace media |
| OLD | NEW |