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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 loaded_ = false; |
143 bitrate_ = 0; | 143 bitrate_ = 0; |
144 stopped_ = true; | 144 stopped_ = true; |
145 media_stream_ = false; | |
146 streaming_ = false; | |
145 } | 147 } |
146 | 148 |
147 DownloadRateMonitor::~DownloadRateMonitor() { } | 149 DownloadRateMonitor::~DownloadRateMonitor() { } |
148 | 150 |
149 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { | 151 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { |
150 // There are max |kNumberOfSamples| so we might as well recompute each time. | 152 // There are max |kNumberOfSamples| so we might as well recompute each time. |
151 int64 total = 0; | 153 int64 total = 0; |
152 for (size_t i = 0; i < sample_window_.size(); ++i) | 154 for (size_t i = 0; i < sample_window_.size(); ++i) |
153 total += sample_window_[i].bytes_downloaded(); | 155 total += sample_window_[i].bytes_downloaded(); |
154 return total; | 156 return total; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 } | 193 } |
192 | 194 |
193 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { | 195 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { |
194 if (stopped_) | 196 if (stopped_) |
195 return false; | 197 return false; |
196 | 198 |
197 // Only notify CanPlayThrough once for now. | 199 // Only notify CanPlayThrough once for now. |
198 if (has_notified_can_play_through_) | 200 if (has_notified_can_play_through_) |
199 return false; | 201 return false; |
200 | 202 |
201 // If the media is from a local file (|loaded_|) or if all bytes are | 203 // Fire CanPlayThrough immediately if the source is loaded or streaming. |
202 // buffered, fire CanPlayThrough. | 204 // |
203 if (loaded_ || buffered_bytes_ == total_bytes_) | 205 // NOTE: It is a requirement for CanPlayThrough to fire immediately if the |
206 // source is |loaded_|, but the choice to fire the event for any streaming | |
207 // element is an optimistic design decision that may need to be tweaked. The | |
208 // only streaming element that *must* fire CanPlayThrough immediately is a | |
209 // media_stream_ whose source is local, such as from a webcam. | |
210 if (loaded_ || streaming_ || media_stream_) | |
wjia(left Chromium)
2011/11/22 23:01:17
should it be: if (loaded_ || !streaming_ || media_
vrk (LEFT CHROMIUM)
2011/11/23 21:29:45
So to be clear, the DownloadRateMonitor does not d
| |
204 return true; | 211 return true; |
205 | 212 |
206 // Cannot approximate when the media can play through if bitrate is unknown. | 213 // If all bytes are buffered, fire CanPlayThrough. |
214 if (buffered_bytes_ == total_bytes_) | |
215 return true; | |
216 | |
217 // If bitrate is unknown, optimistically fire CanPlayThrough immediately. | |
218 // 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. | |
207 if (bitrate_ <= 0) | 220 if (bitrate_ <= 0) |
208 return false; | 221 return true; |
209 | 222 |
210 float bytes_needed_per_second = bitrate_ / 8; | 223 float bytes_needed_per_second = bitrate_ / 8; |
211 float download_rate = ApproximateDownloadByteRate(); | 224 float download_rate = ApproximateDownloadByteRate(); |
212 | 225 |
213 // 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 |
214 // 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. |
215 if (download_rate > 0) | 228 if (download_rate > 0) |
216 return download_rate >= bytes_needed_per_second; | 229 return download_rate >= bytes_needed_per_second; |
217 | 230 |
218 // If download rate is unknown, it may be because the media is being | 231 // If download rate is unknown, it may be because the media is being |
(...skipping 21 matching lines...) Expand all Loading... | |
240 } | 253 } |
241 | 254 |
242 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { | 255 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { |
243 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { | 256 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { |
244 canplaythrough_cb_.Run(); | 257 canplaythrough_cb_.Run(); |
245 has_notified_can_play_through_ = true; | 258 has_notified_can_play_through_ = true; |
246 } | 259 } |
247 } | 260 } |
248 | 261 |
249 } // namespace media | 262 } // namespace media |
OLD | NEW |