Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: media/base/download_rate_monitor.cc

Issue 9113023: Fire canplaythrough as soon as download defers to fix autoplay (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Response to CR Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_)
124 return;
125 // Invalidate the current sample if downloading is going from start to stopped 121 // Invalidate the current sample if downloading is going from start to stopped
126 // or vice versa. 122 // or vice versa.
127 current_sample_.Reset(); 123 if (is_downloading_data != is_downloading_data_) {
128 is_downloading_data_ = is_downloading_data; 124 current_sample_.Reset();
125 is_downloading_data_ = is_downloading_data;
126 }
127
128 // Record when download defers for the first time.
129 if (!is_downloading_data && !has_deferred_) {
acolwell GONE FROM CHROMIUM 2012/01/11 18:51:01 Is it possible for this method to get called with
vrk (LEFT CHROMIUM) 2012/01/13 01:22:49 Yes, on very short files or very fast downloads, w
130 has_deferred_ = true;
131 NotifyCanPlayThroughIfNeeded();
132 }
129 } 133 }
130 134
131 void DownloadRateMonitor::Stop() { 135 void DownloadRateMonitor::Stop() {
132 stopped_ = true; 136 stopped_ = true;
133 current_sample_.Reset(); 137 current_sample_.Reset();
134 buffered_bytes_ = 0; 138 buffered_bytes_ = 0;
135 } 139 }
136 140
137 void DownloadRateMonitor::Reset() { 141 void DownloadRateMonitor::Reset() {
138 canplaythrough_cb_.Reset(); 142 canplaythrough_cb_.Reset();
139 has_notified_can_play_through_ = false; 143 has_notified_can_play_through_ = false;
140 current_sample_.Reset(); 144 current_sample_.Reset();
141 sample_window_.clear(); 145 sample_window_.clear();
142 is_downloading_data_ = false; 146 is_downloading_data_ = false;
143 total_bytes_ = -1; 147 total_bytes_ = -1;
144 buffered_bytes_ = 0; 148 buffered_bytes_ = 0;
145 local_source_ = false; 149 local_source_ = false;
146 bitrate_ = 0; 150 bitrate_ = 0;
147 stopped_ = true; 151 stopped_ = true;
148 streaming_ = false; 152 streaming_ = false;
153 has_deferred_ = false;
149 } 154 }
150 155
151 DownloadRateMonitor::~DownloadRateMonitor() { } 156 DownloadRateMonitor::~DownloadRateMonitor() { }
152 157
153 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { 158 int64 DownloadRateMonitor::bytes_downloaded_in_window() const {
154 // There are max |kNumberOfSamples| so we might as well recompute each time. 159 // There are max |kNumberOfSamples| so we might as well recompute each time.
155 int64 total = 0; 160 int64 total = 0;
156 for (size_t i = 0; i < sample_window_.size(); ++i) 161 for (size_t i = 0; i < sample_window_.size(); ++i)
157 total += sample_window_[i].bytes_downloaded(); 162 total += sample_window_[i].bytes_downloaded();
158 return total; 163 return total;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 return false; 208 return false;
204 209
205 // Fire CanPlayThrough immediately if the source is local or streaming. 210 // Fire CanPlayThrough immediately if the source is local or streaming.
206 // 211 //
207 // NOTE: It is a requirement for CanPlayThrough to fire immediately if the 212 // 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 213 // 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. 214 // streaming media element is a design decision that may need to be tweaked.
210 if (local_source_ || streaming_) 215 if (local_source_ || streaming_)
211 return true; 216 return true;
212 217
213 // If all bytes are buffered, fire CanPlayThrough. 218 // If all bytes are buffered or if enough bytes were buffered such that
214 if (buffered_bytes_ == total_bytes_) 219 // downloading has deferred, fire CanPlayThrough.
220 if (buffered_bytes_ == total_bytes_ || has_deferred_)
215 return true; 221 return true;
216 222
217 // If bitrate is unknown, optimistically fire CanPlayThrough immediately. 223 // If bitrate is unknown, optimistically fire CanPlayThrough immediately.
218 // This is so a video with an unknown bitrate with the "autoplay" attribute 224 // 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. 225 // will not wait until the entire file is downloaded before playback begins.
220 if (bitrate_ <= 0) 226 if (bitrate_ <= 0)
221 return true; 227 return true;
222 228
223 float bytes_needed_per_second = bitrate_ / 8; 229 float bytes_needed_per_second = bitrate_ / 8;
224 float download_rate = ApproximateDownloadByteRate(); 230 float download_rate = ApproximateDownloadByteRate();
225 231
226 // If we are downloading at or faster than the media's bitrate, then we can 232 // 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. 233 // play through to the end of the media without stopping to buffer.
228 if (download_rate > 0) 234 if (download_rate > 0)
229 return download_rate >= bytes_needed_per_second; 235 return download_rate >= bytes_needed_per_second;
230 236
231 // If download rate is unknown, it may be because the media is being 237 // With very fast connections, we may want to fire CanPlayThrough before
232 // downloaded so fast that it cannot collect an adequate number of samples 238 // waiting for the sample window size to reach |kNumberOfSamples|. Check for
233 // before the download gets deferred. 239 // 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) { 240 if (sample_window_.size() < kNumberOfSamples) {
238 int64 bytes_downloaded_since_start = 241 int64 bytes_downloaded_since_start =
239 bytes_downloaded_in_window() + current_sample_.bytes_downloaded(); 242 bytes_downloaded_in_window() + current_sample_.bytes_downloaded();
240 float seconds_elapsed_since_start = 243 float seconds_elapsed_since_start =
241 seconds_elapsed_in_window() + current_sample_.seconds_elapsed(); 244 seconds_elapsed_in_window() + current_sample_.seconds_elapsed();
242 245
243 // If we download 4 seconds of data in less than 2 seconds of time, we're 246 // 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. 247 // probably downloading at a fast enough rate that we can play through.
245 // This is an arbitrary metric that will likely need tweaking. 248 // This is an arbitrary metric that will likely need tweaking.
246 if (seconds_elapsed_since_start < 2.0 && 249 if (seconds_elapsed_since_start < 2.0 &&
247 bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) { 250 bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) {
248 return true; 251 return true;
249 } 252 }
250 } 253 }
251 254
252 return false; 255 return false;
253 } 256 }
254 257
255 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { 258 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() {
256 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { 259 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) {
257 canplaythrough_cb_.Run(); 260 canplaythrough_cb_.Run();
258 has_notified_can_play_through_ = true; 261 has_notified_can_play_through_ = true;
259 } 262 }
260 } 263 }
261 264
262 } // namespace media 265 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698