diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 02:07:45 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-04 02:07:45 +0000 |
commit | 026b594cf4d3fc0bc22b0fa0ca5db2b6eae43c26 (patch) | |
tree | eea8e730b698b450092279a92b6c4c90e978f460 /media | |
parent | 0276b3bce9fe48c2e27890baa4524a9e38710593 (diff) | |
download | chromium_src-026b594cf4d3fc0bc22b0fa0ca5db2b6eae43c26.zip chromium_src-026b594cf4d3fc0bc22b0fa0ca5db2b6eae43c26.tar.gz chromium_src-026b594cf4d3fc0bc22b0fa0ca5db2b6eae43c26.tar.bz2 |
Fold ClockImpl into Clock and update corresponding clients and tests.
BUG=none
TEST=media_unittests --gtest_filter=ClockTest.*
Review URL: http://codereview.chromium.org/6609035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76863 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/base/clock.cc (renamed from media/base/clock_impl.cc) | 26 | ||||
-rw-r--r-- | media/base/clock.h | 72 | ||||
-rw-r--r-- | media/base/clock_impl.h | 62 | ||||
-rw-r--r-- | media/base/clock_unittest.cc (renamed from media/base/clock_impl_unittest.cc) | 22 | ||||
-rw-r--r-- | media/base/pipeline_impl.cc | 10 | ||||
-rw-r--r-- | media/base/pipeline_impl.h | 5 | ||||
-rw-r--r-- | media/base/pipeline_impl_unittest.cc | 8 | ||||
-rw-r--r-- | media/media.gyp | 5 |
8 files changed, 90 insertions, 120 deletions
diff --git a/media/base/clock_impl.cc b/media/base/clock.cc index 4b38eb81..aa0dbfb 100644 --- a/media/base/clock_impl.cc +++ b/media/base/clock.cc @@ -1,30 +1,30 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// 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/clock.h" + #include "base/logging.h" #include "media/base/buffers.h" -#include "media/base/clock_impl.h" namespace media { -ClockImpl::ClockImpl(TimeProvider* time_provider) +Clock::Clock(TimeProvider* time_provider) : time_provider_(time_provider), playing_(false), playback_rate_(1.0f) { } -ClockImpl::~ClockImpl() { -} +Clock::~Clock() {} -base::TimeDelta ClockImpl::Play() { +base::TimeDelta Clock::Play() { DCHECK(!playing_); reference_ = GetTimeFromProvider(); playing_ = true; return media_time_; } -base::TimeDelta ClockImpl::Pause() { +base::TimeDelta Clock::Pause() { DCHECK(playing_); // Save our new accumulated amount of media time. media_time_ = Elapsed(); @@ -32,7 +32,7 @@ base::TimeDelta ClockImpl::Pause() { return media_time_; } -void ClockImpl::SetTime(const base::TimeDelta& time) { +void Clock::SetTime(const base::TimeDelta& time) { if (time == kNoTimestamp) { NOTREACHED(); return; @@ -43,7 +43,7 @@ void ClockImpl::SetTime(const base::TimeDelta& time) { media_time_ = time; } -void ClockImpl::SetPlaybackRate(float playback_rate) { +void Clock::SetPlaybackRate(float playback_rate) { if (playing_) { base::Time time = GetTimeFromProvider(); media_time_ = ElapsedViaProvidedTime(time); @@ -52,25 +52,25 @@ void ClockImpl::SetPlaybackRate(float playback_rate) { playback_rate_ = playback_rate; } -base::TimeDelta ClockImpl::Elapsed() const { +base::TimeDelta Clock::Elapsed() const { if (!playing_) { return media_time_; } return ElapsedViaProvidedTime(GetTimeFromProvider()); } -base::TimeDelta ClockImpl::ElapsedViaProvidedTime( - const base::Time& time) const { +base::TimeDelta Clock::ElapsedViaProvidedTime(const base::Time& time) const { // TODO(scherkus): floating point badness scaling time by playback rate. int64 now_us = (time - reference_).InMicroseconds(); now_us = static_cast<int64>(now_us * playback_rate_); return media_time_ + base::TimeDelta::FromMicroseconds(now_us); } -base::Time ClockImpl::GetTimeFromProvider() const { +base::Time Clock::GetTimeFromProvider() const { if (time_provider_) { return time_provider_(); } return base::Time(); } + } // namespace media diff --git a/media/base/clock.h b/media/base/clock.h index 52f3d8a..df94cad 100644 --- a/media/base/clock.h +++ b/media/base/clock.h @@ -1,51 +1,81 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. -// -// A clock represent a single source of time to allow audio and video streams -// to synchronize with each other. Clocks essentially track the media time with -// respect to some other source of time, whether that may be the system clock, -// audio hardware or some other OS-level API. -// -// Clocks start off paused with a playback rate of 1.0f and a media time of 0. -// -// TODO(scherkus): Clocks will some day be responsible for executing callbacks -// given a media time. This will be used primarily by video renderers. For now -// we'll keep using a poll-and-sleep solution. #ifndef MEDIA_BASE_CLOCK_H_ #define MEDIA_BASE_CLOCK_H_ +#include "base/gtest_prod_util.h" #include "base/scoped_ptr.h" #include "base/time.h" namespace media { +// A clock represents a single source of time to allow audio and video streams +// to synchronize with each other. Clock essentially tracks the media time with +// respect to some other source of time, whether that may be the system clock or +// updates via SetTime(). Clock uses linear interpolation to calculate the +// current media time since the last time SetTime() was called. +// +// Clocks start off paused with a playback rate of 1.0f and a media time of 0. +// +// Clock is not thread-safe and must be externally locked. +// +// TODO(scherkus): Clock will some day be responsible for executing callbacks +// given a media time. This will be used primarily by video renderers. For now +// we'll keep using a poll-and-sleep solution. class Clock { public: + // Type for a static function pointer that acts as a time source. + typedef base::Time(TimeProvider)(); + + Clock(TimeProvider* time_provider); + ~Clock(); + // Starts the clock and returns the current media time, which will increase // with respect to the current playback rate. - virtual base::TimeDelta Play() = 0; + base::TimeDelta Play(); // Stops the clock and returns the current media time, which will remain // constant until Play() is called. - virtual base::TimeDelta Pause() = 0; + base::TimeDelta Pause(); // Sets a new playback rate. The rate at which the media time will increase // will now change. - virtual void SetPlaybackRate(float playback_rate) = 0; + void SetPlaybackRate(float playback_rate); // Forcefully sets the media time to the given time. This should only be used // where a discontinuity in the media is found (i.e., seeking). - virtual void SetTime(const base::TimeDelta& time) = 0; + void SetTime(const base::TimeDelta& time); // Returns the current elapsed media time. - virtual base::TimeDelta Elapsed() const = 0; + base::TimeDelta Elapsed() const; + + private: + // Returns the current media time treating the given time as the latest + // value as returned by |time_provider_|. + base::TimeDelta ElapsedViaProvidedTime(const base::Time& time) const; + + base::Time GetTimeFromProvider() const; + + // Function returning current time in base::Time units. + TimeProvider* time_provider_; + + // Whether the clock is running. + bool playing_; + + // The system clock time when this clock last starting playing or had its + // time set via SetTime(). + base::Time reference_; + + // Current accumulated amount of media time. The remaining portion must be + // calculated by comparing the system time to the reference time. + base::TimeDelta media_time_; + + // Current playback rate. + float playback_rate_; - protected: - // Only allow scoped_ptr<> to delete clocks. - friend class scoped_ptr<Clock>; - virtual ~Clock() {} + DISALLOW_COPY_AND_ASSIGN(Clock); }; } // namespace media diff --git a/media/base/clock_impl.h b/media/base/clock_impl.h deleted file mode 100644 index ac44b2b..0000000 --- a/media/base/clock_impl.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2009 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. -// -// An implementation of Clock based on the system clock. ClockImpl uses linear -// interpolation to calculate the current media time since the last time -// SetTime() was called. -// -// ClockImpl is not thread-safe and must be externally locked. - -#ifndef MEDIA_BASE_CLOCK_IMPL_H_ -#define MEDIA_BASE_CLOCK_IMPL_H_ - -#include "media/base/clock.h" - -namespace media { - -// Type for a static function pointer that acts as a time source. -typedef base::Time(TimeProvider)(); - -class ClockImpl : public Clock { - public: - ClockImpl(TimeProvider* time_provider); - virtual ~ClockImpl(); - - // Clock implementation. - virtual base::TimeDelta Play(); - virtual base::TimeDelta Pause(); - virtual void SetPlaybackRate(float playback_rate); - virtual void SetTime(const base::TimeDelta& time); - virtual base::TimeDelta Elapsed() const; - - private: - // Returns the current media time treating the given time as the latest - // value as returned by |time_provider_|. - base::TimeDelta ElapsedViaProvidedTime(const base::Time& time) const; - - virtual base::Time GetTimeFromProvider() const; - - // Function returning current time in base::Time units. - TimeProvider* time_provider_; - - // Whether the clock is running. - bool playing_; - - // The system clock time when this clock last starting playing or had its - // time set via SetTime(). - base::Time reference_; - - // Current accumulated amount of media time. The remaining portion must be - // calculated by comparing the system time to the reference time. - base::TimeDelta media_time_; - - // Current playback rate. - float playback_rate_; - - DISALLOW_COPY_AND_ASSIGN(ClockImpl); -}; - -} // namespace media - -#endif // MEDIA_BASE_CLOCK_IMPL_H_ diff --git a/media/base/clock_impl_unittest.cc b/media/base/clock_unittest.cc index 87f3a5e..1fe7416 100644 --- a/media/base/clock_impl_unittest.cc +++ b/media/base/clock_unittest.cc @@ -1,9 +1,9 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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 "base/logging.h" -#include "media/base/clock_impl.h" +#include "media/base/clock.h" #include "testing/gmock/include/gmock/gmock.h" using ::testing::DefaultValue; @@ -55,7 +55,7 @@ TEST(ClockTest, Created) { StrictMock<MockTimeProvider> mock_time; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(0); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); EXPECT_EQ(kExpected, clock.Elapsed()); } @@ -69,7 +69,7 @@ TEST(ClockTest, Play_NormalSpeed) { const base::TimeDelta kZero; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(2); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); EXPECT_EQ(kZero, clock.Play()); EXPECT_EQ(kExpected, clock.Elapsed()); } @@ -84,7 +84,7 @@ TEST(ClockTest, Play_DoubleSpeed) { const base::TimeDelta kZero; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(10); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); clock.SetPlaybackRate(2.0f); EXPECT_EQ(kZero, clock.Play()); EXPECT_EQ(kExpected, clock.Elapsed()); @@ -100,7 +100,7 @@ TEST(ClockTest, Play_HalfSpeed) { const base::TimeDelta kZero; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(2); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); clock.SetPlaybackRate(0.5f); EXPECT_EQ(kZero, clock.Play()); EXPECT_EQ(kExpected, clock.Elapsed()); @@ -123,7 +123,7 @@ TEST(ClockTest, Play_ZeroSpeed) { const base::TimeDelta kZero; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(10); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); EXPECT_EQ(kZero, clock.Play()); clock.SetPlaybackRate(0.0f); clock.SetPlaybackRate(1.0f); @@ -147,7 +147,7 @@ TEST(ClockTest, Play_MultiSpeed) { const base::TimeDelta kZero; const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(21); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); clock.SetPlaybackRate(0.5f); EXPECT_EQ(kZero, clock.Play()); clock.SetPlaybackRate(1.0f); @@ -170,7 +170,7 @@ TEST(ClockTest, Pause) { const base::TimeDelta kFirstPause = base::TimeDelta::FromSeconds(4); const base::TimeDelta kSecondPause = base::TimeDelta::FromSeconds(8); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); EXPECT_EQ(kZero, clock.Play()); EXPECT_EQ(kFirstPause, clock.Pause()); EXPECT_EQ(kFirstPause, clock.Elapsed()); @@ -187,7 +187,7 @@ TEST(ClockTest, SetTime_Paused) { const base::TimeDelta kFirstTime = base::TimeDelta::FromSeconds(4); const base::TimeDelta kSecondTime = base::TimeDelta::FromSeconds(16); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); clock.SetTime(kFirstTime); EXPECT_EQ(kFirstTime, clock.Elapsed()); clock.SetTime(kSecondTime); @@ -208,7 +208,7 @@ TEST(ClockTest, SetTime_Playing) { const base::TimeDelta kZero; const base::TimeDelta kExepected = base::TimeDelta::FromSeconds(16); - ClockImpl clock(&MockTimeProvider::StaticNow); + Clock clock(&MockTimeProvider::StaticNow); EXPECT_EQ(kZero, clock.Play()); clock.SetTime(base::TimeDelta::FromSeconds(12)); EXPECT_EQ(kExepected, clock.Elapsed()); diff --git a/media/base/pipeline_impl.cc b/media/base/pipeline_impl.cc index 76a07cd..9bce38c 100644 --- a/media/base/pipeline_impl.cc +++ b/media/base/pipeline_impl.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// 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. // @@ -9,7 +9,7 @@ #include "base/compiler_specific.h" #include "base/stl_util-inl.h" #include "base/synchronization/condition_variable.h" -#include "media/base/clock_impl.h" +#include "media/base/clock.h" #include "media/base/filter_collection.h" #include "media/base/media_format.h" #include "media/base/pipeline_impl.h" @@ -27,7 +27,7 @@ class PipelineImpl::PipelineInitState { PipelineImpl::PipelineImpl(MessageLoop* message_loop) : message_loop_(message_loop), - clock_(new ClockImpl(&base::Time::Now)), + clock_(new Clock(&base::Time::Now)), waiting_for_clock_update_(false), state_(kCreated), current_bytes_(0) { @@ -279,6 +279,10 @@ PipelineStatistics PipelineImpl::GetStatistics() const { return statistics_; } +void PipelineImpl::SetClockForTesting(Clock* clock) { + clock_.reset(clock); +} + void PipelineImpl::SetCurrentReadPosition(int64 offset) { base::AutoLock auto_lock(lock_); diff --git a/media/base/pipeline_impl.h b/media/base/pipeline_impl.h index 128e350..e817eed 100644 --- a/media/base/pipeline_impl.h +++ b/media/base/pipeline_impl.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// 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. @@ -94,6 +94,8 @@ class PipelineImpl : public Pipeline, public FilterHost { virtual PipelineError GetError() const; virtual PipelineStatistics GetStatistics() const; + void SetClockForTesting(Clock* clock); + private: // Pipeline states, as described above. enum State { @@ -413,7 +415,6 @@ class PipelineImpl : public Pipeline, public FilterHost { PipelineStatistics statistics_; FRIEND_TEST_ALL_PREFIXES(PipelineImplTest, GetBufferedTime); - FRIEND_TEST_ALL_PREFIXES(PipelineImplTest, AudioStreamShorterThanVideo); DISALLOW_COPY_AND_ASSIGN(PipelineImpl); }; diff --git a/media/base/pipeline_impl_unittest.cc b/media/base/pipeline_impl_unittest.cc index 5aa9b16..cc6de37 100644 --- a/media/base/pipeline_impl_unittest.cc +++ b/media/base/pipeline_impl_unittest.cc @@ -1,5 +1,4 @@ - -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// 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. @@ -7,7 +6,6 @@ #include "base/callback.h" #include "base/stl_util-inl.h" -#include "media/base/clock_impl.h" #include "media/base/pipeline_impl.h" #include "media/base/media_format.h" #include "media/base/filters.h" @@ -758,8 +756,8 @@ TEST_F(PipelineImplTest, AudioStreamShorterThanVideo) { FilterHost* host = pipeline_; // Replace the clock so we can simulate wallclock time advancing w/o using - // Sleep. - pipeline_->clock_.reset(new ClockImpl(&StaticClockFunction)); + // Sleep(). + pipeline_->SetClockForTesting(new Clock(&StaticClockFunction)); EXPECT_EQ(0, host->GetTime().ToInternalValue()); diff --git a/media/media.gyp b/media/media.gyp index 70a45a8..23307e0 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -76,9 +76,8 @@ 'base/buffers.h', 'base/callback.cc', 'base/callback.h', + 'base/clock.cc', 'base/clock.h', - 'base/clock_impl.cc', - 'base/clock_impl.h', 'base/composite_filter.cc', 'base/composite_filter.h', 'base/data_buffer.cc', @@ -347,8 +346,8 @@ 'audio/mac/audio_output_mac_unittest.cc', 'audio/simple_sources_unittest.cc', 'audio/win/audio_output_win_unittest.cc', + 'base/clock_unittest.cc', 'base/composite_filter_unittest.cc', - 'base/clock_impl_unittest.cc', 'base/data_buffer_unittest.cc', 'base/djb2_unittest.cc', 'base/filter_collection_unittest.cc', |