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