Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Unified Diff: media/base/clock_impl.cc

Issue 159517: Implemented a proper clock for audio/video synchronization. (Closed)
Patch Set: Now with convenience! Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/clock_impl.h ('k') | media/base/clock_impl_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/clock_impl.cc
diff --git a/media/base/clock_impl.cc b/media/base/clock_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cc465680e4abfc7a819f7ee818742177b0d0591e
--- /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
« no previous file with comments | « media/base/clock_impl.h ('k') | media/base/clock_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698