diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 20:36:37 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 20:36:37 +0000 |
commit | 46564213965397d01fb87975f3bddd597985fcbf (patch) | |
tree | 346f9cf4005fcb9865196622f13462065f52a83f /media/base/clock_impl.cc | |
parent | c2972193bd0d0c2354d0862444b7ce4662d52d77 (diff) | |
download | chromium_src-46564213965397d01fb87975f3bddd597985fcbf.zip chromium_src-46564213965397d01fb87975f3bddd597985fcbf.tar.gz chromium_src-46564213965397d01fb87975f3bddd597985fcbf.tar.bz2 |
Implemented a proper clock for audio/video synchronization.
More or less a change to pull out time management from PipelineImpl into a new class ClockImpl. Biggest difference is ClockImpl will use the system clock + linear interpolation to provide a more "precise" representation of the current playback position.
BUG=16508
TEST=a/v sync should remain the same, currentTime should report more precise values
Review URL: http://codereview.chromium.org/159517
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21882 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base/clock_impl.cc')
-rw-r--r-- | media/base/clock_impl.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/media/base/clock_impl.cc b/media/base/clock_impl.cc new file mode 100644 index 0000000..cc46568 --- /dev/null +++ b/media/base/clock_impl.cc @@ -0,0 +1,65 @@ +// 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. + +#include "base/logging.h" +#include "media/base/clock_impl.h" + +namespace media { + +ClockImpl::ClockImpl(TimeProvider* time_provider) + : time_provider_(time_provider), + playing_(false), + playback_rate_(1.0f) { +} + +ClockImpl::~ClockImpl() { +} + +base::TimeDelta ClockImpl::Play() { + DCHECK(!playing_); + reference_ = time_provider_(); + playing_ = true; + return media_time_; +} + +base::TimeDelta ClockImpl::Pause() { + DCHECK(playing_); + // Save our new accumulated amount of media time. + media_time_ = Elapsed(); + playing_ = false; + return media_time_; +} + +void ClockImpl::SetTime(const base::TimeDelta& time) { + if (playing_) { + reference_ = time_provider_(); + } + media_time_ = time; +} + +void ClockImpl::SetPlaybackRate(float playback_rate) { + if (playing_) { + base::Time time = time_provider_(); + media_time_ = ElapsedViaProvidedTime(time); + reference_ = time; + } + playback_rate_ = playback_rate; +} + +base::TimeDelta ClockImpl::Elapsed() const { + if (!playing_) { + return media_time_; + } + return ElapsedViaProvidedTime(time_provider_()); +} + +base::TimeDelta ClockImpl::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); +} + +} // namespace media |