diff options
author | tkent@chromium.org <tkent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-11 04:14:24 +0000 |
---|---|---|
committer | tkent@chromium.org <tkent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-11 04:14:24 +0000 |
commit | e04e01b5c5fff56d4b3959e14ec7d4392b8ad3f1 (patch) | |
tree | da6b923f1e83391fd882c5fe0fa634f74a0053d1 /media | |
parent | ba30c18a957290ff02ab9872a1a9dd8c4ee2f14a (diff) | |
download | chromium_src-e04e01b5c5fff56d4b3959e14ec7d4392b8ad3f1.zip chromium_src-e04e01b5c5fff56d4b3959e14ec7d4392b8ad3f1.tar.gz chromium_src-e04e01b5c5fff56d4b3959e14ec7d4392b8ad3f1.tar.bz2 |
Revert 136486 - Delete DownloadRateMonitor since it's never worked right.
(and it complicates other things I want to do)
LayoutTests/media/video-error-does-not-exist.html crashes by r136486.
BUG=73609
Review URL: https://chromiumcodereview.appspot.com/10382109
TBR=fischman@chromium.org
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136500 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/base/download_rate_monitor.cc | 262 | ||||
-rw-r--r-- | media/base/download_rate_monitor.h | 157 | ||||
-rw-r--r-- | media/base/download_rate_monitor_unittest.cc | 165 | ||||
-rw-r--r-- | media/base/pipeline.cc | 43 | ||||
-rw-r--r-- | media/base/pipeline.h | 23 | ||||
-rw-r--r-- | media/media.gyp | 3 |
6 files changed, 650 insertions, 3 deletions
diff --git a/media/base/download_rate_monitor.cc b/media/base/download_rate_monitor.cc new file mode 100644 index 0000000..f27b2e8 --- /dev/null +++ b/media/base/download_rate_monitor.cc @@ -0,0 +1,262 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/base/download_rate_monitor.h" + +#include "base/bind.h" +#include "base/time.h" + +namespace media { + +// Number of samples to use to collect and average for each measurement of +// download rate. +static const size_t kNumberOfSamples = 5; + +// Minimum number of seconds represented in a sample period. +static const float kSamplePeriod = 1.0; + +DownloadRateMonitor::Sample::Sample() { + Reset(); +} + +DownloadRateMonitor::Sample::~Sample() { } + +DownloadRateMonitor::Sample::Sample( + const BufferingPoint& start, const BufferingPoint& end) { + Reset(); + start_ = start; + set_end(end); +} + +void DownloadRateMonitor::Sample::set_end(const BufferingPoint& new_end) { + DCHECK(!start_.timestamp.is_null()); + DCHECK(new_end.buffered_bytes >= start_.buffered_bytes); + DCHECK(new_end.timestamp >= start_.timestamp); + end_ = new_end; +} + +float DownloadRateMonitor::Sample::bytes_per_second() const { + if (seconds_elapsed() > 0.0 && bytes_downloaded() >= 0) + return bytes_downloaded() / seconds_elapsed(); + return -1.0; +} + +float DownloadRateMonitor::Sample::seconds_elapsed() const { + if (start_.timestamp.is_null() || end_.timestamp.is_null()) + return -1.0; + return (end_.timestamp - start_.timestamp).InSecondsF(); +} + +int64 DownloadRateMonitor::Sample::bytes_downloaded() const { + if (start_.timestamp.is_null() || end_.timestamp.is_null()) + return -1.0; + return end_.buffered_bytes - start_.buffered_bytes; +} + +bool DownloadRateMonitor::Sample::is_null() const { + return start_.timestamp.is_null() && end_.timestamp.is_null(); +} + +void DownloadRateMonitor::Sample::Reset() { + start_ = BufferingPoint(); + end_ = BufferingPoint(); +} + +void DownloadRateMonitor::Sample::RestartAtEndBufferingPoint() { + start_ = end_; + end_ = BufferingPoint(); +} + +DownloadRateMonitor::DownloadRateMonitor() { + Reset(); +} + +void DownloadRateMonitor::Start( + const base::Closure& canplaythrough_cb, int media_bitrate, + bool streaming, bool local_source) { + canplaythrough_cb_ = canplaythrough_cb; + streaming_ = streaming; + local_source_ = local_source; + stopped_ = false; + bitrate_ = media_bitrate; + current_sample_.Reset(); + buffered_bytes_ = 0; + + NotifyCanPlayThroughIfNeeded(); +} + +void DownloadRateMonitor::SetBufferedBytes( + int64 buffered_bytes, const base::Time& timestamp) { + if (stopped_) + return; + + is_downloading_data_ = true; + + // Check monotonically nondecreasing constraint. + base::Time previous_time; + if (!current_sample_.is_null()) + previous_time = current_sample_.end().timestamp; + else if (!sample_window_.empty()) + previous_time = sample_window_.back().end().timestamp; + + // If we go backward in time, dismiss the sample. + if (!previous_time.is_null() && timestamp < previous_time) + return; + + // If the buffer level has dropped, invalidate current sample. + if (buffered_bytes < buffered_bytes_) + current_sample_.Reset(); + buffered_bytes_ = buffered_bytes; + + BufferingPoint latest_point = { buffered_bytes, timestamp }; + if (current_sample_.is_null()) + current_sample_ = Sample(latest_point, latest_point); + else + current_sample_.set_end(latest_point); + + UpdateSampleWindow(); + NotifyCanPlayThroughIfNeeded(); +} + +void DownloadRateMonitor::SetNetworkActivity(bool is_downloading_data) { + if (is_downloading_data == is_downloading_data_) + return; + // Invalidate the current sample if downloading is going from start to stopped + // or vice versa. + current_sample_.Reset(); + is_downloading_data_ = is_downloading_data; +} + +void DownloadRateMonitor::Stop() { + stopped_ = true; + current_sample_.Reset(); + buffered_bytes_ = 0; +} + +void DownloadRateMonitor::Reset() { + canplaythrough_cb_.Reset(); + has_notified_can_play_through_ = false; + current_sample_.Reset(); + sample_window_.clear(); + is_downloading_data_ = false; + total_bytes_ = -1; + buffered_bytes_ = 0; + local_source_ = false; + bitrate_ = 0; + stopped_ = true; + streaming_ = false; +} + +DownloadRateMonitor::~DownloadRateMonitor() { } + +int64 DownloadRateMonitor::bytes_downloaded_in_window() const { + // There are max |kNumberOfSamples| so we might as well recompute each time. + int64 total = 0; + for (size_t i = 0; i < sample_window_.size(); ++i) + total += sample_window_[i].bytes_downloaded(); + return total; +} + +float DownloadRateMonitor::seconds_elapsed_in_window() const { + // There are max |kNumberOfSamples| so we might as well recompute each time. + float total = 0.0; + for (size_t i = 0; i < sample_window_.size(); ++i) + total += sample_window_[i].seconds_elapsed(); + return total; +} + +void DownloadRateMonitor::UpdateSampleWindow() { + if (current_sample_.seconds_elapsed() < kSamplePeriod) + return; + + // Add latest sample and remove oldest sample. + sample_window_.push_back(current_sample_); + if (sample_window_.size() > kNumberOfSamples) + sample_window_.pop_front(); + + // Prepare for next measurement. + current_sample_.RestartAtEndBufferingPoint(); +} + +float DownloadRateMonitor::ApproximateDownloadByteRate() const { + // Compute and return the average download byte rate from within the sample + // window. + // NOTE: In the unlikely case where the data is arriving really bursty-ly, + // say getting a big chunk of data every 5 seconds, then with this + // implementation it will take 25 seconds until bitrate is calculated. + if (sample_window_.size() >= kNumberOfSamples && + seconds_elapsed_in_window() > 0.0) { + return bytes_downloaded_in_window() / seconds_elapsed_in_window(); + } + + // Could not determine approximate download byte rate. + return -1.0; +} + +bool DownloadRateMonitor::ShouldNotifyCanPlayThrough() { + if (stopped_) + return false; + + // Only notify CanPlayThrough once for now. + if (has_notified_can_play_through_) + return false; + + // Fire CanPlayThrough immediately if the source is local or streaming. + // + // NOTE: It is a requirement for CanPlayThrough to fire immediately if the + // source is local, but the choice to optimistically fire the event for any + // streaming media element is a design decision that may need to be tweaked. + if (local_source_ || streaming_) + return true; + + // If all bytes are buffered, fire CanPlayThrough. + if (buffered_bytes_ == total_bytes_) + return true; + + // If bitrate is unknown, optimistically fire CanPlayThrough immediately. + // This is so a video with an unknown bitrate with the "autoplay" attribute + // will not wait until the entire file is downloaded before playback begins. + if (bitrate_ <= 0) + return true; + + float bytes_needed_per_second = bitrate_ / 8; + float download_rate = ApproximateDownloadByteRate(); + + // If we are downloading at or faster than the media's bitrate, then we can + // play through to the end of the media without stopping to buffer. + if (download_rate > 0) + return download_rate >= bytes_needed_per_second; + + // If download rate is unknown, it may be because the media is being + // downloaded so fast that it cannot collect an adequate number of samples + // before the download gets deferred. + // + // To catch this case, we also look at how much data is being downloaded + // immediately after the download begins. + if (sample_window_.size() < kNumberOfSamples) { + int64 bytes_downloaded_since_start = + bytes_downloaded_in_window() + current_sample_.bytes_downloaded(); + float seconds_elapsed_since_start = + seconds_elapsed_in_window() + current_sample_.seconds_elapsed(); + + // If we download 4 seconds of data in less than 2 seconds of time, we're + // probably downloading at a fast enough rate that we can play through. + // This is an arbitrary metric that will likely need tweaking. + if (seconds_elapsed_since_start < 2.0 && + bytes_downloaded_since_start > 4.0 * bytes_needed_per_second) { + return true; + } + } + + return false; +} + +void DownloadRateMonitor::NotifyCanPlayThroughIfNeeded() { + if (ShouldNotifyCanPlayThrough() && !canplaythrough_cb_.is_null()) { + canplaythrough_cb_.Run(); + has_notified_can_play_through_ = true; + } +} + +} // namespace media diff --git a/media/base/download_rate_monitor.h b/media/base/download_rate_monitor.h new file mode 100644 index 0000000..4fca328 --- /dev/null +++ b/media/base/download_rate_monitor.h @@ -0,0 +1,157 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ +#define MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ + +#include <deque> + +#include "base/callback.h" +#include "base/time.h" +#include "media/base/media_export.h" + +namespace media { + +// Measures download speed based on the rate at which a media file is buffering. +// Signals when it believes the media file can be played back without needing to +// pause to buffer. +class MEDIA_EXPORT DownloadRateMonitor { + public: + DownloadRateMonitor(); + ~DownloadRateMonitor(); + + // Begin measuring download rate. The monitor will run |canplaythrough_cb| + // when it believes the media can be played back without needing to pause to + // buffer. |media_bitrate| is the bitrate of the video. + void Start(const base::Closure& canplaythrough_cb, int media_bitrate, + bool streaming, bool local_source); + + // Notifies the monitor of the current number of bytes buffered by the media + // file at what timestamp. The monitor expects subsequent calls to + // SetBufferedBytes to have monotonically nondecreasing timestamps. + // Calls to the method are ignored if monitor has not been started or is + // stopped. + void SetBufferedBytes(int64 buffered_bytes, const base::Time& timestamp); + + // Notifies the monitor when the media file has paused or continued + // downloading data. + void SetNetworkActivity(bool is_downloading_data); + + void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } + + // Stop monitoring download rate. This does not discard previously learned + // information, but it will no longer factor incoming information into its + // canplaythrough estimation. + void Stop(); + + // Resets monitor to uninitialized state. + void Reset(); + + private: + // Represents a point in time in which the media was buffering data. + struct BufferingPoint { + // The number of bytes buffered by the media player at |timestamp|. + int64 buffered_bytes; + // Time at which buffering measurement was taken. + base::Time timestamp; + }; + + // Represents a span of time in which the media was buffering data. + class Sample { + public: + Sample(); + Sample(const BufferingPoint& start, const BufferingPoint& end); + ~Sample(); + + // Set the end point of the data sample. + void set_end(const BufferingPoint& new_end); + + const BufferingPoint& start() const { return start_; } + const BufferingPoint& end() const { return end_; } + + // Returns the bytes downloaded per second in this sample. Returns -1.0 if + // sample is invalid. + float bytes_per_second() const; + + // Returns the seconds elapsed in this sample. Returns -1.0 if the sample + // has not begun yet. + float seconds_elapsed() const; + + // Returns bytes downloaded in this sample. Returns -1.0 if the sample has + // not begun yet. + int64 bytes_downloaded() const; + + // Returns true if Sample has not been initialized. + bool is_null() const; + + // Resets the sample to an uninitialized state. + void Reset(); + + // Restarts the sample to begin its measurement at what was previously its + // end point. + void RestartAtEndBufferingPoint(); + + private: + BufferingPoint start_; + BufferingPoint end_; + }; + + int64 bytes_downloaded_in_window() const; + float seconds_elapsed_in_window() const; + + // Updates window with latest sample if it is ready. + void UpdateSampleWindow(); + + // Returns an approximation of the current download rate in bytes per second. + // Returns -1.0 if unknown. + float ApproximateDownloadByteRate() const; + + // Helper method that returns true if the monitor believes it should fire the + // |canplaythrough_cb_|. + bool ShouldNotifyCanPlayThrough(); + + // Examines the download rate and fires the |canplaythrough_cb_| callback if + // the monitor deems it the right time. + void NotifyCanPlayThroughIfNeeded(); + + // Callback to run when the monitor believes the media can play through + // without needing to pause to buffer. + base::Closure canplaythrough_cb_; + + // Indicates whether the monitor has run the |canplaythrough_cb_|. + bool has_notified_can_play_through_; + + // Measurements used to approximate download speed. + Sample current_sample_; + std::deque<Sample> sample_window_; + + // True if actively downloading bytes, false otherwise. + bool is_downloading_data_; + + // Total number of bytes in the media file, 0 if unknown or undefined. + int64 total_bytes_; + + // Amount of bytes buffered. + int64 buffered_bytes_; + + // True if the media file is from a local source, e.g. file:// protocol or a + // webcam stream. + bool local_source_; + + // Bitrate of the media file, 0 if unknown. + int bitrate_; + + // True if the monitor has not yet started or has been stopped, false + // otherwise. + bool stopped_; + + // True if the data source is a streaming source, false otherwise. + bool streaming_; + + DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitor); +}; + +} // namespace media + +#endif // MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ diff --git a/media/base/download_rate_monitor_unittest.cc b/media/base/download_rate_monitor_unittest.cc new file mode 100644 index 0000000..321ad63 --- /dev/null +++ b/media/base/download_rate_monitor_unittest.cc @@ -0,0 +1,165 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/base/download_rate_monitor.h" + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::testing::Mock; + +namespace media { + +class DownloadRateMonitorTest : public ::testing::Test { + public: + DownloadRateMonitorTest() { + monitor_.set_total_bytes(kMediaSizeInBytes); + } + + virtual ~DownloadRateMonitorTest() { } + + MOCK_METHOD0(CanPlayThrough, void()); + + protected: + static const int kMediaSizeInBytes = 20 * 1024 * 1024; + + // Simulates downloading of the media file. Packets are timed evenly in + // |ms_between_packets| intervals, starting at |starting_time|, which is + // number of seconds since unix epoch (Jan 1, 1970). + // Returns the number of bytes buffered in the media file after the + // network activity. + int SimulateNetwork(double starting_time, + int starting_bytes, + int bytes_per_packet, + int ms_between_packets, + int number_of_packets) { + int bytes_buffered = starting_bytes; + base::Time packet_time = base::Time::FromDoubleT(starting_time); + + monitor_.SetNetworkActivity(true); + // Loop executes (number_of_packets + 1) times because a packet needs a + // starting and end point. + for (int i = 0; i < number_of_packets + 1; ++i) { + monitor_.SetBufferedBytes(bytes_buffered, packet_time); + packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets); + bytes_buffered += bytes_per_packet; + } + monitor_.SetNetworkActivity(false); + return bytes_buffered; + } + + void StartMonitor(int bitrate) { + StartMonitor(bitrate, false, false); + } + + void StartMonitor(int bitrate, bool streaming, bool local_source) { + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), bitrate, streaming, local_source); + } + + DownloadRateMonitor monitor_; + + private: + DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest); +}; + +TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate downloading at double the media's bitrate. + StartMonitor(media_bitrate); + EXPECT_CALL(*this, CanPlayThrough()); + SimulateNetwork(1, 0, 2 * media_bitrate / 8, 1000, 10); +} + +// If the user pauses and the pipeline stops downloading data, make sure the +// DownloadRateMonitor understands that the download is not stalling. +TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) { + static const int media_bitrate = 1024 * 1024 * 8; + static const int download_byte_rate = 1.1 * media_bitrate / 8; + + // Start downloading faster than the media's bitrate. + StartMonitor(media_bitrate); + EXPECT_CALL(*this, CanPlayThrough()); + int buffered = SimulateNetwork(1, 0, download_byte_rate, 1000, 2); + + // Then "pause" for 3 minutes and continue downloading at same rate. + SimulateNetwork(60 * 3, buffered, download_byte_rate, 1000, 4); +} + +TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) { + static const int media_bitrate = 1024 * 1024 * 8; + static const int download_byte_rate = 1.1 * media_bitrate / 8; + + // Start downloading faster than the media's bitrate. + EXPECT_CALL(*this, CanPlayThrough()); + StartMonitor(media_bitrate); + SimulateNetwork(1, 0, download_byte_rate, 1000, 2); + + // Then seek forward mid-file and continue downloading at same rate. + SimulateNetwork(4, kMediaSizeInBytes / 2, download_byte_rate, 1000, 4); +} + +TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) { + static const int media_bitrate = 1024 * 1024 * 8; + static const int download_byte_rate = 1.1 * media_bitrate / 8; + + // Start downloading faster than the media's bitrate, in middle of file. + StartMonitor(media_bitrate); + SimulateNetwork(1, kMediaSizeInBytes / 2, download_byte_rate, 1000, 2); + + // Then seek back to beginning and continue downloading at same rate. + EXPECT_CALL(*this, CanPlayThrough()); + SimulateNetwork(4, 0, download_byte_rate, 1000, 4); +} + +TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate downloading at half the media's bitrate. + EXPECT_CALL(*this, CanPlayThrough()) + .Times(0); + StartMonitor(media_bitrate); + SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, 10); +} + +TEST_F(DownloadRateMonitorTest, MediaSourceIsLocal) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate no data downloaded. + EXPECT_CALL(*this, CanPlayThrough()); + StartMonitor(media_bitrate, false, true); +} + +TEST_F(DownloadRateMonitorTest, MediaSourceIsStreaming) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate downloading at the media's bitrate while streaming. + EXPECT_CALL(*this, CanPlayThrough()); + StartMonitor(media_bitrate, true, false); + SimulateNetwork(1, 0, media_bitrate / 8, 1000, 10); +} + +TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate downloading half the video very quickly in one chunk. + StartMonitor(media_bitrate); + EXPECT_CALL(*this, CanPlayThrough()); + SimulateNetwork(1, 0, kMediaSizeInBytes / 2, 10, 1); +} + +TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) { + static const int seconds_of_data = 20; + static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data; + + // Simulate downloading entire video at half the bitrate of the video. + StartMonitor(media_bitrate); + EXPECT_CALL(*this, CanPlayThrough()); + SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, seconds_of_data * 2); +} + +} // namespace media diff --git a/media/base/pipeline.cc b/media/base/pipeline.cc index d465e2d..29a8dd0 100644 --- a/media/base/pipeline.cc +++ b/media/base/pipeline.cc @@ -71,7 +71,8 @@ Pipeline::Pipeline(MessageLoop* message_loop, MediaLog* media_log) waiting_for_clock_update_(false), state_(kCreated), current_bytes_(0), - creation_time_(base::Time::Now()) { + creation_time_(base::Time::Now()), + is_downloading_data_(false) { media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(kCreated)); ResetState(); media_log_->AddEvent( @@ -116,6 +117,8 @@ void Pipeline::Seek(base::TimeDelta time, base::AutoLock auto_lock(lock_); CHECK(running_) << "Media pipeline isn't running"; + download_rate_monitor_.Stop(); + message_loop_->PostTask(FROM_HERE, base::Bind( &Pipeline::SeekTask, this, time, seek_cb)); } @@ -225,12 +228,12 @@ void Pipeline::GetNaturalVideoSize(gfx::Size* out_size) const { bool Pipeline::IsStreaming() const { base::AutoLock auto_lock(lock_); - return !demuxer_->IsSeekable(); + return streaming_; } bool Pipeline::IsLocalSource() const { base::AutoLock auto_lock(lock_); - return demuxer_->IsLocalSource(); + return local_source_; } PipelineStatistics Pipeline::GetStatistics() const { @@ -267,6 +270,8 @@ void Pipeline::ResetState() { playback_rate_change_pending_ = false; buffered_bytes_ = 0; buffered_time_ranges_.clear(); + streaming_ = false; + local_source_ = false; total_bytes_ = 0; natural_size_.SetSize(0, 0); volume_ = 1.0f; @@ -278,6 +283,7 @@ void Pipeline::ResetState() { waiting_for_clock_update_ = false; audio_disabled_ = false; clock_->Reset(); + download_rate_monitor_.Reset(); } void Pipeline::SetState(State next_state) { @@ -446,6 +452,7 @@ void Pipeline::SetTotalBytes(int64 total_bytes) { base::AutoLock auto_lock(lock_); total_bytes_ = total_bytes; + download_rate_monitor_.set_total_bytes(total_bytes_); } void Pipeline::SetBufferedBytes(int64 buffered_bytes) { @@ -455,6 +462,7 @@ void Pipeline::SetBufferedBytes(int64 buffered_bytes) { if (buffered_bytes < current_bytes_) current_bytes_ = buffered_bytes; buffered_bytes_ = buffered_bytes; + download_rate_monitor_.SetBufferedBytes(buffered_bytes, base::Time::Now()); UpdateBufferedTimeRanges_Locked(); } @@ -496,6 +504,11 @@ void Pipeline::SetNetworkActivity(bool is_downloading_data) { if (is_downloading_data) type = DOWNLOAD_CONTINUED; + { + base::AutoLock auto_lock(lock_); + download_rate_monitor_.SetNetworkActivity(is_downloading_data); + } + message_loop_->PostTask(FROM_HERE, base::Bind( &Pipeline::NotifyNetworkEventTask, this, type)); media_log_->AddEvent( @@ -960,6 +973,20 @@ void Pipeline::FilterStateTransitionTask() { StartClockIfWaitingForTimeUpdate_Locked(); } + // Start monitoring rate of downloading. + int bitrate = 0; + if (demuxer_) { + bitrate = demuxer_->GetBitrate(); + local_source_ = demuxer_->IsLocalSource(); + streaming_ = !demuxer_->IsSeekable(); + } + // Needs to be locked because most other calls to |download_rate_monitor_| + // occur on the renderer thread. + download_rate_monitor_.Start( + base::Bind(&Pipeline::OnCanPlayThrough, this), + bitrate, streaming_, local_source_); + download_rate_monitor_.SetBufferedBytes(buffered_bytes_, base::Time::Now()); + if (IsPipelineStopPending()) { // We had a pending stop request need to be honored right now. TearDownPipeline(); @@ -1306,6 +1333,16 @@ void Pipeline::OnAudioUnderflow() { audio_renderer_->ResumeAfterUnderflow(true); } +void Pipeline::OnCanPlayThrough() { + message_loop_->PostTask(FROM_HERE, base::Bind( + &Pipeline::NotifyCanPlayThrough, this)); +} + +void Pipeline::NotifyCanPlayThrough() { + DCHECK(message_loop_->BelongsToCurrentThread()); + NotifyNetworkEventTask(CAN_PLAY_THROUGH); +} + void Pipeline::StartClockIfWaitingForTimeUpdate_Locked() { lock_.AssertAcquired(); if (!waiting_for_clock_update_) diff --git a/media/base/pipeline.h b/media/base/pipeline.h index 699b5e2..c61ec17 100644 --- a/media/base/pipeline.h +++ b/media/base/pipeline.h @@ -11,6 +11,7 @@ #include "base/synchronization/condition_variable.h" #include "base/synchronization/lock.h" #include "media/base/demuxer.h" +#include "media/base/download_rate_monitor.h" #include "media/base/filter_host.h" #include "media/base/media_export.h" #include "media/base/pipeline_status.h" @@ -452,6 +453,14 @@ class MEDIA_EXPORT Pipeline void OnAudioUnderflow(); + // Called when |download_rate_monitor_| believes that the media can + // be played through without needing to pause to buffer. + void OnCanPlayThrough(); + + // Carries out the notification that the media can be played through without + // needing to pause to buffer. + void NotifyCanPlayThrough(); + void StartClockIfWaitingForTimeUpdate_Locked(); // Report pipeline |status| through |cb| avoiding duplicate error reporting. @@ -496,6 +505,14 @@ class MEDIA_EXPORT Pipeline // Video's natural width and height. Set by filters. gfx::Size natural_size_; + // Set by the demuxer to indicate whether the data source is a streaming + // source. + bool streaming_; + + // Indicates whether the data source is local, such as a local media file + // from disk or a local webcam stream. + bool local_source_; + // Current volume level (from 0.0f to 1.0f). This value is set immediately // via SetVolume() and a task is dispatched on the message loop to notify the // filters. @@ -589,6 +606,12 @@ class MEDIA_EXPORT Pipeline // reaches "kStarted", at which point it is used & zeroed out. base::Time creation_time_; + // Approximates the rate at which the media is being downloaded. + DownloadRateMonitor download_rate_monitor_; + + // True if the pipeline is actively downloading bytes, false otherwise. + bool is_downloading_data_; + DISALLOW_COPY_AND_ASSIGN(Pipeline); }; diff --git a/media/media.gyp b/media/media.gyp index 66b04e3..65fd702 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -148,6 +148,8 @@ 'base/demuxer_stream.h', 'base/djb2.cc', 'base/djb2.h', + 'base/download_rate_monitor.cc', + 'base/download_rate_monitor.h', 'base/filter_collection.cc', 'base/filter_collection.h', 'base/filter_host.h', @@ -647,6 +649,7 @@ 'base/composite_filter_unittest.cc', 'base/data_buffer_unittest.cc', 'base/djb2_unittest.cc', + 'base/download_rate_monitor_unittest.cc', 'base/filter_collection_unittest.cc', 'base/h264_bitstream_converter_unittest.cc', 'base/mock_reader.h', |