| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/download_rate_monitor.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/time.h" | |
| 9 | |
| 10 namespace media { | |
| 11 | |
| 12 // Number of samples to use to collect and average for each measurement of | |
| 13 // download rate. | |
| 14 static const size_t kNumberOfSamples = 5; | |
| 15 | |
| 16 // Minimum number of seconds represented in a sample period. | |
| 17 static const float kSamplePeriod = 1.0; | |
| 18 | |
| 19 DownloadRateMonitor::Sample::Sample() { | |
| 20 Reset(); | |
| 21 } | |
| 22 | |
| 23 DownloadRateMonitor::Sample::~Sample() { } | |
| 24 | |
| 25 DownloadRateMonitor::Sample::Sample( | |
| 26 const BufferingPoint& start, const BufferingPoint& end) { | |
| 27 Reset(); | |
| 28 start_ = start; | |
| 29 set_end(end); | |
| 30 } | |
| 31 | |
| 32 void DownloadRateMonitor::Sample::set_end(const BufferingPoint& new_end) { | |
| 33 DCHECK(!start_.timestamp.is_null()); | |
| 34 DCHECK(new_end.buffered_bytes >= start_.buffered_bytes); | |
| 35 DCHECK(new_end.timestamp >= start_.timestamp); | |
| 36 end_ = new_end; | |
| 37 } | |
| 38 | |
| 39 float DownloadRateMonitor::Sample::bytes_per_second() const { | |
| 40 if (seconds_elapsed() > 0.0 && bytes_downloaded() >= 0) | |
| 41 return bytes_downloaded() / seconds_elapsed(); | |
| 42 return -1.0; | |
| 43 } | |
| 44 | |
| 45 float DownloadRateMonitor::Sample::seconds_elapsed() const { | |
| 46 if (start_.timestamp.is_null() || end_.timestamp.is_null()) | |
| 47 return -1.0; | |
| 48 return (end_.timestamp - start_.timestamp).InSecondsF(); | |
| 49 } | |
| 50 | |
| 51 int64 DownloadRateMonitor::Sample::bytes_downloaded() const { | |
| 52 if (start_.timestamp.is_null() || end_.timestamp.is_null()) | |
| 53 return -1.0; | |
| 54 return end_.buffered_bytes - start_.buffered_bytes; | |
| 55 } | |
| 56 | |
| 57 bool DownloadRateMonitor::Sample::is_null() const { | |
| 58 return start_.timestamp.is_null() && end_.timestamp.is_null(); | |
| 59 } | |
| 60 | |
| 61 void DownloadRateMonitor::Sample::Reset() { | |
| 62 start_ = BufferingPoint(); | |
| 63 end_ = BufferingPoint(); | |
| 64 } | |
| 65 | |
| 66 void DownloadRateMonitor::Sample::RestartAtEndBufferingPoint() { | |
| 67 start_ = end_; | |
| 68 end_ = BufferingPoint(); | |
| 69 } | |
| 70 | |
| 71 DownloadRateMonitor::DownloadRateMonitor() { | |
| 72 Reset(); | |
| 73 } | |
| 74 | |
| 75 void DownloadRateMonitor::Start( | |
| 76 const base::Closure& canplaythrough_cb, int media_bitrate, | |
| 77 bool streaming, bool local_source) { | |
| 78 canplaythrough_cb_ = canplaythrough_cb; | |
| 79 streaming_ = streaming; | |
| 80 local_source_ = local_source; | |
| 81 stopped_ = false; | |
| 82 bitrate_ = media_bitrate; | |
| 83 current_sample_.Reset(); | |
| 84 buffered_bytes_ = 0; | |
| 85 | |
| 86 NotifyCanPlayThroughIfNeeded(); | |
| 87 } | |
| 88 | |
| 89 void DownloadRateMonitor::SetBufferedBytes( | |
| 90 int64 buffered_bytes, const base::Time& timestamp) { | |
| 91 if (stopped_) | |
| 92 return; | |
| 93 | |
| 94 is_downloading_data_ = true; | |
| 95 | |
| 96 // Check monotonically nondecreasing constraint. | |
| 97 base::Time previous_time; | |
| 98 if (!current_sample_.is_null()) | |
| 99 previous_time = current_sample_.end().timestamp; | |
| 100 else if (!sample_window_.empty()) | |
| 101 previous_time = sample_window_.back().end().timestamp; | |
| 102 | |
| 103 // If we go backward in time, dismiss the sample. | |
| 104 if (!previous_time.is_null() && timestamp < previous_time) | |
| 105 return; | |
| 106 | |
| 107 // If the buffer level has dropped, invalidate current sample. | |
| 108 if (buffered_bytes < buffered_bytes_) | |
| 109 current_sample_.Reset(); | |
| 110 buffered_bytes_ = buffered_bytes; | |
| 111 | |
| 112 BufferingPoint latest_point = { buffered_bytes, timestamp }; | |
| 113 if (current_sample_.is_null()) | |
| 114 current_sample_ = Sample(latest_point, latest_point); | |
| 115 else | |
| 116 current_sample_.set_end(latest_point); | |
| 117 | |
| 118 UpdateSampleWindow(); | |
| 119 NotifyCanPlayThroughIfNeeded(); | |
| 120 } | |
| 121 | |
| 122 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 | |
| 126 // or vice versa. | |
| 127 current_sample_.Reset(); | |
| 128 is_downloading_data_ = is_downloading_data; | |
| 129 } | |
| 130 | |
| 131 void DownloadRateMonitor::Stop() { | |
| 132 stopped_ = true; | |
| 133 current_sample_.Reset(); | |
| 134 buffered_bytes_ = 0; | |
| 135 } | |
| 136 | |
| 137 void DownloadRateMonitor::Reset() { | |
| 138 canplaythrough_cb_.Reset(); | |
| 139 has_notified_can_play_through_ = false; | |
| 140 current_sample_.Reset(); | |
| 141 sample_window_.clear(); | |
| 142 is_downloading_data_ = false; | |
| 143 total_bytes_ = -1; | |
| 144 buffered_bytes_ = 0; | |
| 145 local_source_ = false; | |
| 146 bitrate_ = 0; | |
| 147 stopped_ = true; | |
| 148 streaming_ = false; | |
| 149 } | |
| 150 | |
| 151 DownloadRateMonitor::~DownloadRateMonitor() { } | |
| 152 | |
| 153 int64 DownloadRateMonitor::bytes_downloaded_in_window() const { | |
| 154 // There are max |kNumberOfSamples| so we might as well recompute each time. | |
| 155 int64 total = 0; | |
| 156 for (size_t i = 0; i < sample_window_.size(); ++i) | |
| 157 total += sample_window_[i].bytes_downloaded(); | |
| 158 return total; | |
| 159 } | |
| 160 | |
| 161 float DownloadRateMonitor::seconds_elapsed_in_window() const { | |
| 162 // There are max |kNumberOfSamples| so we might as well recompute each time. | |
| 163 float total = 0.0; | |
| 164 for (size_t i = 0; i < sample_window_.size(); ++i) | |
| 165 total += sample_window_[i].seconds_elapsed(); | |
| 166 return total; | |
| 167 } | |
| 168 | |
| 169 void DownloadRateMonitor::UpdateSampleWindow() { | |
| 170 if (current_sample_.seconds_elapsed() < kSamplePeriod) | |
| 171 return; | |
| 172 | |
| 173 // Add latest sample and remove oldest sample. | |
| 174 sample_window_.push_back(current_sample_); | |
| 175 if (sample_window_.size() > kNumberOfSamples) | |
| 176 sample_window_.pop_front(); | |
| 177 | |
| 178 // Prepare for next measurement. | |
| 179 current_sample_.RestartAtEndBufferingPoint(); | |
| 180 } | |
| 181 | |
| 182 float DownloadRateMonitor::ApproximateDownloadByteRate() const { | |
| 183 // Compute and return the average download byte rate from within the sample | |
| 184 // window. | |
| 185 // NOTE: In the unlikely case where the data is arriving really bursty-ly, | |
| 186 // say getting a big chunk of data every 5 seconds, then with this | |
| 187 // implementation it will take 25 seconds until bitrate is calculated. | |
| 188 if (sample_window_.size() >= kNumberOfSamples && | |
| 189 seconds_elapsed_in_window() > 0.0) { | |
| 190 return bytes_downloaded_in_window() / seconds_elapsed_in_window(); | |
| 191 } | |
| 192 | |
| 193 // Could not determine approximate download byte rate. | |
| 194 return -1.0; | |
| 195 } | |
| 196 | |
| 197 bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { | |
| 198 if (stopped_) | |
| 199 return false; | |
| 200 | |
| 201 // Only notify CanPlayThrough once for now. | |
| 202 if (has_notified_can_play_through_) | |
| 203 return false; | |
| 204 | |
| 205 // Fire CanPlayThrough immediately if the source is local or streaming. | |
| 206 // | |
| 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_) | |
| 211 return true; | |
| 212 | |
| 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. | |
| 220 if (bitrate_ <= 0) | |
| 221 return true; | |
| 222 | |
| 223 float bytes_needed_per_second = bitrate_ / 8; | |
| 224 float download_rate = ApproximateDownloadByteRate(); | |
| 225 | |
| 226 // 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. | |
| 228 if (download_rate > 0) | |
| 229 return download_rate >= bytes_needed_per_second; | |
| 230 | |
| 231 // If download rate is unknown, it may be because the media is being | |
| 232 // downloaded so fast that it cannot collect an adequate number of samples | |
| 233 // before the download gets deferred. | |
| 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) { | |
| 238 int64 bytes_downloaded_since_start = | |
| 239 bytes_downloaded_in_window() + current_sample_.bytes_downloaded(); | |
| 240 float seconds_elapsed_since_start = | |
| 241 seconds_elapsed_in_window() + current_sample_.seconds_elapsed(); | |
| 242 | |
| 243 // 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. | |
| 245 // This is an arbitrary metric that will likely need tweaking. | |
| 246 if (seconds_elapsed_since_start < 2.0 && | |
| 247 bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) { | |
| 248 return true; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 return false; | |
| 253 } | |
| 254 | |
| 255 void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { | |
| 256 if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { | |
| 257 canplaythrough_cb_.Run(); | |
| 258 has_notified_can_play_through_ = true; | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 } // namespace media | |
| OLD | NEW |