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

Unified Diff: media/base/wall_clock_time_source.cc

Issue 379343005: Introduce media::TimeSource. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 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
Index: media/base/wall_clock_time_source.cc
diff --git a/media/base/wall_clock_time_source.cc b/media/base/wall_clock_time_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a8f484463c05352ec813f61255789428cb18a7fc
--- /dev/null
+++ b/media/base/wall_clock_time_source.cc
@@ -0,0 +1,62 @@
+// Copyright 2014 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/wall_clock_time_source.h"
+
+#include "base/logging.h"
+
+namespace media {
+
+WallClockTimeSource::WallClockTimeSource()
+ : ticking_(false), playback_rate_(1.0f) {
+}
+
+WallClockTimeSource::~WallClockTimeSource() {
+}
+
+void WallClockTimeSource::StartTicking() {
+ DCHECK(!ticking_);
+ ticking_ = true;
+ reference_wall_ticks_ = base::TimeTicks::Now();
+}
+
+void WallClockTimeSource::StopTicking() {
+ DCHECK(ticking_);
+ ticking_ = false;
+ base::TimeTicks now = base::TimeTicks::Now();
+ base_timestamp_ += base::TimeDelta::FromMicroseconds(
+ (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
+ reference_wall_ticks_ = now;
+}
+
+void WallClockTimeSource::SetPlaybackRate(float playback_rate) {
+ base::TimeTicks now = base::TimeTicks::Now();
+
+ // Estimate current media time using old rate to use as a new base timestamp
+ // for the new rate.
+ if (ticking_) {
+ base_timestamp_ += base::TimeDelta::FromMicroseconds(
+ (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
+ reference_wall_ticks_ = now;
+ }
+
+ playback_rate_ = playback_rate;
+}
+
+void WallClockTimeSource::SetMediaTimestamp(base::TimeDelta timestamp) {
+ CHECK(!ticking_);
+ base_timestamp_ = timestamp;
+}
+
+base::TimeDelta WallClockTimeSource::CurrentMediaTimestamp() {
+ if (!ticking_)
+ return base_timestamp_;
+
+ base::TimeTicks now = base::TimeTicks::Now();
+ return base_timestamp_ +
+ base::TimeDelta::FromMicroseconds(
+ (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
xhwang 2014/07/12 06:36:44 nit: This appears 3 times and deserve a helper fun
scherkus (not reviewing) 2014/07/17 23:52:56 Done.
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698