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

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

Issue 8661002: Fire CanPlayThrough immediately for local and streaming media files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase ToT Created 9 years 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 void DownloadRateMonitor::Sample::RestartAtEndBufferingPoint() { 66 void DownloadRateMonitor::Sample::RestartAtEndBufferingPoint() {
67 start_ = end_; 67 start_ = end_;
68 end_ = BufferingPoint(); 68 end_ = BufferingPoint();
69 } 69 }
70 70
71 DownloadRateMonitor::DownloadRateMonitor() { 71 DownloadRateMonitor::DownloadRateMonitor() {
72 Reset(); 72 Reset();
73 } 73 }
74 74
75 void DownloadRateMonitor::Start( 75 void DownloadRateMonitor::Start(
76 const base::Closure& canplaythrough_cb, int media_bitrate) { 76 const base::Closure& canplaythrough_cb, int media_bitrate,
77 bool streaming, bool local_source) {
77 canplaythrough_cb_ = canplaythrough_cb; 78 canplaythrough_cb_ = canplaythrough_cb;
79 streaming_ = streaming;
80 local_source_ = local_source;
78 stopped_ = false; 81 stopped_ = false;
79 bitrate_ = media_bitrate; 82 bitrate_ = media_bitrate;
80 current_sample_.Reset(); 83 current_sample_.Reset();
81 buffered_bytes_ = 0; 84 buffered_bytes_ = 0;
82 85
83 NotifyCanPlayThroughIfNeeded(); 86 NotifyCanPlayThroughIfNeeded();
84 } 87 }
85 88
86 void DownloadRateMonitor::SetBufferedBytes( 89 void DownloadRateMonitor::SetBufferedBytes(
87 int64 buffered_bytes, const base::Time& timestamp) { 90 int64 buffered_bytes, const base::Time& timestamp) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 135 }
133 136
134 void DownloadRateMonitor::Reset() { 137 void DownloadRateMonitor::Reset() {
135 canplaythrough_cb_.Reset(); 138 canplaythrough_cb_.Reset();
136 has_notified_can_play_through_ = false; 139 has_notified_can_play_through_ = false;
137 current_sample_.Reset(); 140 current_sample_.Reset();
138 sample_window_.clear(); 141 sample_window_.clear();
139 is_downloading_data_ = false; 142 is_downloading_data_ = false;
140 total_bytes_ = -1; 143 total_bytes_ = -1;
141 buffered_bytes_ = 0; 144 buffered_bytes_ = 0;
142 loaded_ = false; 145 local_source_ = false;
143 bitrate_ = 0; 146 bitrate_ = 0;
144 stopped_ = true; 147 stopped_ = true;
148 streaming_ = false;
145 } 149 }
146 150
147 DownloadRateMonitor::~DownloadRateMonitor() { } 151 DownloadRateMonitor::~DownloadRateMonitor() { }
148 152
149 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { 153 int64 DownloadRateMonitor::bytes_downloaded_in_window() const {
150 // There are max |kNumberOfSamples| so we might as well recompute each time. 154 // There are max |kNumberOfSamples| so we might as well recompute each time.
151 int64 total = 0; 155 int64 total = 0;
152 for (size_t i = 0; i < sample_window_.size(); ++i) 156 for (size_t i = 0; i < sample_window_.size(); ++i)
153 total += sample_window_[i].bytes_downloaded(); 157 total += sample_window_[i].bytes_downloaded();
154 return total; 158 return total;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 195 }
192 196
193 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { 197 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() {
194 if (stopped_) 198 if (stopped_)
195 return false; 199 return false;
196 200
197 // Only notify CanPlayThrough once for now. 201 // Only notify CanPlayThrough once for now.
198 if (has_notified_can_play_through_) 202 if (has_notified_can_play_through_)
199 return false; 203 return false;
200 204
201 // If the media is from a local file (|loaded_|) or if all bytes are 205 // Fire CanPlayThrough immediately if the source is local or streaming.
202 // buffered, fire CanPlayThrough. 206 //
203 if (loaded_ || buffered_bytes_ == total_bytes_) 207 // 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
209 // streaming media element is a design decision that may need to be tweaked.
210 if (local_source_ || streaming_)
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698