diff options
author | vrk@google.com <vrk@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-18 19:52:08 +0000 |
---|---|---|
committer | vrk@google.com <vrk@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-18 19:52:08 +0000 |
commit | 7b268b7a549561344b59da31d58cbe3c6ca71dd8 (patch) | |
tree | b93d5a298c4e40b896026ed02a28efb999701d35 /media | |
parent | 49d633905b2467796aa6a4794f64001664358334 (diff) | |
download | chromium_src-7b268b7a549561344b59da31d58cbe3c6ca71dd8.zip chromium_src-7b268b7a549561344b59da31d58cbe3c6ca71dd8.tar.gz chromium_src-7b268b7a549561344b59da31d58cbe3c6ca71dd8.tar.bz2 |
Fire canplaythrough event at the proper time for audio/video
In this CL, the browser fires the canplaythrough event based on an
approximation of the download speed of the media instead of firing the
event right away.
BUG=73609
TEST=NONE
Review URL: http://codereview.chromium.org/8399023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110733 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/base/demuxer.h | 4 | ||||
-rw-r--r-- | media/base/download_rate_monitor.cc | 250 | ||||
-rw-r--r-- | media/base/download_rate_monitor.h | 155 | ||||
-rw-r--r-- | media/base/download_rate_monitor_unittest.cc | 156 | ||||
-rw-r--r-- | media/base/mock_filters.h | 1 | ||||
-rw-r--r-- | media/base/pipeline.h | 12 | ||||
-rw-r--r-- | media/base/pipeline_impl.cc | 54 | ||||
-rw-r--r-- | media/base/pipeline_impl.h | 17 | ||||
-rw-r--r-- | media/filters/chunk_demuxer.cc | 5 | ||||
-rw-r--r-- | media/filters/chunk_demuxer.h | 1 | ||||
-rw-r--r-- | media/filters/dummy_demuxer.cc | 4 | ||||
-rw-r--r-- | media/filters/dummy_demuxer.h | 1 | ||||
-rw-r--r-- | media/filters/ffmpeg_demuxer.h | 5 | ||||
-rw-r--r-- | media/media.gyp | 3 |
14 files changed, 648 insertions, 20 deletions
diff --git a/media/base/demuxer.h b/media/base/demuxer.h index d6013ff..a75f248 100644 --- a/media/base/demuxer.h +++ b/media/base/demuxer.h @@ -57,6 +57,10 @@ class MEDIA_EXPORT Demuxer // Returns the starting time for the media file. virtual base::TimeDelta GetStartTime() const = 0; + // Returns the content bitrate. May be obtained from container or + // approximated. Returns 0 if it is unknown. + virtual int GetBitrate() = 0; + protected: Demuxer(); FilterHost* host() { return host_; } diff --git a/media/base/download_rate_monitor.cc b/media/base/download_rate_monitor.cc new file mode 100644 index 0000000..863967c --- /dev/null +++ b/media/base/download_rate_monitor.cc @@ -0,0 +1,250 @@ +// 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) { + DCHECK(stopped_); + canplaythrough_cb_ = canplaythrough_cb; + 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; + loaded_ = false; + bitrate_ = 0; + stopped_ = true; +} + +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; + + // If the media is from a local file (|loaded_|) or if all bytes are + // buffered, fire CanPlayThrough. + if (loaded_ || buffered_bytes_ == total_bytes_) + return true; + + // Cannot approximate when the media can play through if bitrate is unknown. + if (bitrate_ <= 0) + return false; + + 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..ac88f7f --- /dev/null +++ b/media/base/download_rate_monitor.h @@ -0,0 +1,155 @@ +// 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/bind.h" +#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); + + // 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; } + + void set_loaded(bool loaded) { loaded_ = loaded; } + + // 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 a fully loaded source, e.g. file:// protocol. + bool loaded_; + + // 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_; + + 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..787a5b4 --- /dev/null +++ b/media/base/download_rate_monitor_unittest.cc @@ -0,0 +1,156 @@ +// 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 "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; + } + + 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. + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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. + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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()); + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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. + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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); + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), media_bitrate); + SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, 10); +} + +TEST_F(DownloadRateMonitorTest, MediaIsLoaded) { + static const int media_bitrate = 1024 * 1024 * 8; + + monitor_.set_loaded(true); + + // Simulate no data downloaded (source is already loaded). + EXPECT_CALL(*this, CanPlayThrough()); + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), media_bitrate); + SimulateNetwork(1, 0, 0, 1000, 10); +} + +TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) { + static const int media_bitrate = 1024 * 1024 * 8; + + // Simulate downloading half the video very quickly in one chunk. + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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. + monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, + base::Unretained(this)), 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/mock_filters.h b/media/base/mock_filters.h index 841629d..f54a114 100644 --- a/media/base/mock_filters.h +++ b/media/base/mock_filters.h @@ -112,6 +112,7 @@ class MockDemuxer : public Demuxer { MOCK_METHOD1(SetPreload, void(Preload preload)); MOCK_METHOD2(Seek, void(base::TimeDelta time, const FilterStatusCB& cb)); MOCK_METHOD0(OnAudioRendererDisabled, void()); + MOCK_METHOD0(GetBitrate, int()); // Demuxer implementation. MOCK_METHOD2(Initialize, void(DataSource* data_source, diff --git a/media/base/pipeline.h b/media/base/pipeline.h index 7cf5e73..1c91f80 100644 --- a/media/base/pipeline.h +++ b/media/base/pipeline.h @@ -34,15 +34,19 @@ struct PipelineStatistics { uint32 video_frames_dropped; }; +enum NetworkEvent { + DOWNLOAD_CONTINUED, + DOWNLOAD_PAUSED, + CAN_PLAY_THROUGH +}; + class FilterCollection; class MEDIA_EXPORT Pipeline : public base::RefCountedThreadSafe<Pipeline> { public: // Callback that executes when a network event occurrs. - // The parameter represents the state of network activity: true if the network - // is downloading data, false if it is not. If the callback never runs, it is - // assumed the network is not downloading data. - typedef base::Callback<void(bool)> NetworkEventCB; + // The parameter specifies the type of event that is being signaled. + typedef base::Callback<void(NetworkEvent)> NetworkEventCB; // Initializes pipeline. Pipeline takes ownership of all callbacks passed // into this method. diff --git a/media/base/pipeline_impl.cc b/media/base/pipeline_impl.cc index e0d8270..386b064 100644 --- a/media/base/pipeline_impl.cc +++ b/media/base/pipeline_impl.cc @@ -143,6 +143,8 @@ void PipelineImpl::Seek(base::TimeDelta time, return; } + download_rate_monitor_.Stop(); + message_loop_->PostTask(FROM_HERE, base::Bind(&PipelineImpl::SeekTask, this, time, seek_callback)); } @@ -186,9 +188,8 @@ float PipelineImpl::GetPlaybackRate() const { } void PipelineImpl::SetPlaybackRate(float playback_rate) { - if (playback_rate < 0.0f) { + if (playback_rate < 0.0f) return; - } base::AutoLock auto_lock(lock_); playback_rate_ = playback_rate; @@ -204,9 +205,8 @@ float PipelineImpl::GetVolume() const { } void PipelineImpl::SetVolume(float volume) { - if (volume < 0.0f || volume > 1.0f) { + if (volume < 0.0f || volume > 1.0f) return; - } base::AutoLock auto_lock(lock_); volume_ = volume; @@ -239,6 +239,7 @@ base::TimeDelta PipelineImpl::GetCurrentTime() const { } base::TimeDelta PipelineImpl::GetCurrentTime_Locked() const { + lock_.AssertAcquired(); base::TimeDelta elapsed = clock_->Elapsed(); if (state_ == kEnded || elapsed > duration_) { return duration_; @@ -258,9 +259,8 @@ base::TimeDelta PipelineImpl::GetBufferedTime() { base::TimeDelta current_time = GetCurrentTime_Locked(); // If buffered time was set, we report that value directly. - if (buffered_time_.ToInternalValue() > 0) { + if (buffered_time_.ToInternalValue() > 0) return std::max(buffered_time_, current_time); - } if (total_bytes_ == 0) return base::TimeDelta(); @@ -371,6 +371,7 @@ void PipelineImpl::ResetState() { waiting_for_clock_update_ = false; audio_disabled_ = false; clock_->SetTime(kZero); + download_rate_monitor_.Reset(); } void PipelineImpl::SetState(State next_state) { @@ -524,16 +525,17 @@ void PipelineImpl::SetTotalBytes(int64 total_bytes) { base::AutoLock auto_lock(lock_); total_bytes_ = total_bytes; + download_rate_monitor_.set_total_bytes(total_bytes_); } void PipelineImpl::SetBufferedBytes(int64 buffered_bytes) { DCHECK(IsRunning()); base::AutoLock auto_lock(lock_); - // See comments in SetCurrentReadPosition() about capping. if (buffered_bytes < current_bytes_) current_bytes_ = buffered_bytes; buffered_bytes_ = buffered_bytes; + download_rate_monitor_.SetBufferedBytes(buffered_bytes, base::Time::Now()); } void PipelineImpl::SetNaturalVideoSize(const gfx::Size& size) { @@ -570,13 +572,24 @@ void PipelineImpl::SetLoaded(bool loaded) { base::AutoLock auto_lock(lock_); loaded_ = loaded; + download_rate_monitor_.set_loaded(loaded_); } void PipelineImpl::SetNetworkActivity(bool is_downloading_data) { DCHECK(IsRunning()); + + NetworkEvent type = DOWNLOAD_PAUSED; + 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( - &PipelineImpl::NotifyNetworkEventTask, this, is_downloading_data)); + &PipelineImpl::NotifyNetworkEventTask, this, type)); media_log_->AddEvent( media_log_->CreateBooleanEvent( MediaLogEvent::NETWORK_ACTIVITY_SET, @@ -682,7 +695,6 @@ void PipelineImpl::InitializeTask() { state_ == kInitVideoDecoder || state_ == kInitVideoRenderer); - // Demuxer created, create audio decoder. if (state_ == kInitDemuxer) { SetState(kInitAudioDecoder); @@ -940,10 +952,10 @@ void PipelineImpl::NotifyEndedTask() { } } -void PipelineImpl::NotifyNetworkEventTask(bool is_downloading_data) { +void PipelineImpl::NotifyNetworkEventTask(NetworkEvent type) { DCHECK_EQ(MessageLoop::current(), message_loop_); if (!network_callback_.is_null()) - network_callback_.Run(is_downloading_data); + network_callback_.Run(type); } void PipelineImpl::DisableAudioRendererTask() { @@ -1030,6 +1042,16 @@ void PipelineImpl::FilterStateTransitionTask() { if (!waiting_for_clock_update_) clock_->Play(); + // Start monitoring rate of downloading. + int bitrate = 0; + if (demuxer_.get()) + bitrate = demuxer_->GetBitrate(); + // Needs to be locked because most other calls to |download_rate_monitor_| + // occur on the renderer thread. + download_rate_monitor_.Start( + base::Bind(&PipelineImpl::OnCanPlayThrough, this), bitrate); + download_rate_monitor_.SetBufferedBytes(buffered_bytes_, base::Time::Now()); + if (IsPipelineStopPending()) { // We had a pending stop request need to be honored right now. TearDownPipeline(); @@ -1405,4 +1427,14 @@ void PipelineImpl::OnAudioUnderflow() { audio_renderer_->ResumeAfterUnderflow(true); } +void PipelineImpl::OnCanPlayThrough() { + message_loop_->PostTask(FROM_HERE, + base::Bind(&PipelineImpl::NotifyCanPlayThrough, this)); +} + +void PipelineImpl::NotifyCanPlayThrough() { + DCHECK_EQ(MessageLoop::current(), message_loop_); + NotifyNetworkEventTask(CAN_PLAY_THROUGH); +} + } // namespace media diff --git a/media/base/pipeline_impl.h b/media/base/pipeline_impl.h index 084e1cd..57f8aebd 100644 --- a/media/base/pipeline_impl.h +++ b/media/base/pipeline_impl.h @@ -23,6 +23,7 @@ #include "media/base/clock.h" #include "media/base/composite_filter.h" #include "media/base/demuxer.h" +#include "media/base/download_rate_monitor.h" #include "media/base/filter_host.h" #include "media/base/pipeline.h" #include "ui/gfx/size.h" @@ -261,7 +262,7 @@ class MEDIA_EXPORT PipelineImpl : public Pipeline, public FilterHost { void NotifyEndedTask(); // Carries out handling a notification of network event. - void NotifyNetworkEventTask(bool is_downloading_data); + void NotifyNetworkEventTask(NetworkEvent type); // Carries out disabling the audio renderer. void DisableAudioRendererTask(); @@ -334,6 +335,14 @@ class MEDIA_EXPORT PipelineImpl : public Pipeline, public FilterHost { 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(); + // Message loop used to execute pipeline tasks. MessageLoop* message_loop_; @@ -479,6 +488,12 @@ class MEDIA_EXPORT PipelineImpl : public Pipeline, public FilterHost { // 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_; + FRIEND_TEST_ALL_PREFIXES(PipelineImplTest, GetBufferedTime); DISALLOW_COPY_AND_ASSIGN(PipelineImpl); diff --git a/media/filters/chunk_demuxer.cc b/media/filters/chunk_demuxer.cc index 0df22a5..c27c490 100644 --- a/media/filters/chunk_demuxer.cc +++ b/media/filters/chunk_demuxer.cc @@ -364,6 +364,11 @@ void ChunkDemuxer::OnAudioRendererDisabled() { void ChunkDemuxer::SetPreload(Preload preload) {} +int ChunkDemuxer::GetBitrate() { + // TODO(acolwell): Implement bitrate reporting. + return 0; +} + // Demuxer implementation. scoped_refptr<DemuxerStream> ChunkDemuxer::GetStream( DemuxerStream::Type type) { diff --git a/media/filters/chunk_demuxer.h b/media/filters/chunk_demuxer.h index 63dd7b1..a940100 100644 --- a/media/filters/chunk_demuxer.h +++ b/media/filters/chunk_demuxer.h @@ -38,6 +38,7 @@ class MEDIA_EXPORT ChunkDemuxer : public Demuxer { DemuxerStream::Type type) OVERRIDE; virtual void SetPreload(Preload preload) OVERRIDE; virtual base::TimeDelta GetStartTime() const OVERRIDE; + virtual int GetBitrate() OVERRIDE; // Methods used by an external object to control this demuxer. void FlushData(); diff --git a/media/filters/dummy_demuxer.cc b/media/filters/dummy_demuxer.cc index 24307b9..095ffee 100644 --- a/media/filters/dummy_demuxer.cc +++ b/media/filters/dummy_demuxer.cc @@ -48,6 +48,10 @@ DummyDemuxer::~DummyDemuxer() {} void DummyDemuxer::SetPreload(Preload preload) {} +int DummyDemuxer::GetBitrate() { + return 0; +} + scoped_refptr<DemuxerStream> DummyDemuxer::GetStream(DemuxerStream::Type type) { return streams_[type]; } diff --git a/media/filters/dummy_demuxer.h b/media/filters/dummy_demuxer.h index b029aa5..309dded 100644 --- a/media/filters/dummy_demuxer.h +++ b/media/filters/dummy_demuxer.h @@ -48,6 +48,7 @@ class DummyDemuxer : public Demuxer { DemuxerStream::Type type) OVERRIDE; virtual void SetPreload(Preload preload) OVERRIDE; virtual base::TimeDelta GetStartTime() const OVERRIDE; + virtual int GetBitrate() OVERRIDE; private: bool has_video_; diff --git a/media/filters/ffmpeg_demuxer.h b/media/filters/ffmpeg_demuxer.h index ddfe51e..0cff00f 100644 --- a/media/filters/ffmpeg_demuxer.h +++ b/media/filters/ffmpeg_demuxer.h @@ -147,6 +147,7 @@ class MEDIA_EXPORT FFmpegDemuxer : public Demuxer, public FFmpegURLProtocol { DemuxerStream::Type type) OVERRIDE; virtual void SetPreload(Preload preload) OVERRIDE; virtual base::TimeDelta GetStartTime() const OVERRIDE; + virtual int GetBitrate() OVERRIDE; // FFmpegURLProtocol implementation. virtual size_t Read(size_t size, uint8* data) OVERRIDE; @@ -161,10 +162,6 @@ class MEDIA_EXPORT FFmpegDemuxer : public Demuxer, public FFmpegURLProtocol { // For testing purposes. void disable_first_seek_hack_for_testing() { first_seek_hack_ = false; } - // Returns the bitrate of media file. May be obtained from container or - // approximated. Returns 0 if it is unknown. - int GetBitrate(); - private: // Only allow a factory to create this class. friend class MockFFmpegDemuxer; diff --git a/media/media.gyp b/media/media.gyp index e99dd28..b47be2c 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -116,6 +116,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_factories.cc', @@ -573,6 +575,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', |