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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/base/wall_clock_time_source.h"
6
7 #include "base/logging.h"
8
9 namespace media {
10
11 WallClockTimeSource::WallClockTimeSource()
12 : ticking_(false), playback_rate_(1.0f) {
13 }
14
15 WallClockTimeSource::~WallClockTimeSource() {
16 }
17
18 void WallClockTimeSource::StartTicking() {
19 DCHECK(!ticking_);
20 ticking_ = true;
21 reference_wall_ticks_ = base::TimeTicks::Now();
22 }
23
24 void WallClockTimeSource::StopTicking() {
25 DCHECK(ticking_);
26 ticking_ = false;
27 base::TimeTicks now = base::TimeTicks::Now();
28 base_timestamp_ += base::TimeDelta::FromMicroseconds(
29 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
30 reference_wall_ticks_ = now;
31 }
32
33 void WallClockTimeSource::SetPlaybackRate(float playback_rate) {
34 base::TimeTicks now = base::TimeTicks::Now();
35
36 // Estimate current media time using old rate to use as a new base timestamp
37 // for the new rate.
38 if (ticking_) {
39 base_timestamp_ += base::TimeDelta::FromMicroseconds(
40 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
41 reference_wall_ticks_ = now;
42 }
43
44 playback_rate_ = playback_rate;
45 }
46
47 void WallClockTimeSource::SetMediaTimestamp(base::TimeDelta timestamp) {
48 CHECK(!ticking_);
49 base_timestamp_ = timestamp;
50 }
51
52 base::TimeDelta WallClockTimeSource::CurrentMediaTimestamp() {
53 if (!ticking_)
54 return base_timestamp_;
55
56 base::TimeTicks now = base::TimeTicks::Now();
57 return base_timestamp_ +
58 base::TimeDelta::FromMicroseconds(
59 (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.
60 }
61
62 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698