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.
5 #include "media/base/wall_clock_time_source.h"
7 #include "base/logging.h"
8 #include "base/time/default_tick_clock.h"
12 WallClockTimeSource::WallClockTimeSource()
13 : tick_clock_(new base::DefaultTickClock()),
15 playback_rate_(1.0f
) {
18 WallClockTimeSource::~WallClockTimeSource() {
21 void WallClockTimeSource::StartTicking() {
22 DVLOG(1) << __FUNCTION__
;
23 base::AutoLock
auto_lock(lock_
);
26 reference_wall_ticks_
= tick_clock_
->NowTicks();
29 void WallClockTimeSource::StopTicking() {
30 DVLOG(1) << __FUNCTION__
;
31 base::AutoLock
auto_lock(lock_
);
33 base_time_
= CurrentMediaTime_Locked();
35 reference_wall_ticks_
= tick_clock_
->NowTicks();
38 void WallClockTimeSource::SetPlaybackRate(float playback_rate
) {
39 DVLOG(1) << __FUNCTION__
<< "(" << playback_rate
<< ")";
40 base::AutoLock
auto_lock(lock_
);
41 // Estimate current media time using old rate to use as a new base time for
44 base_time_
= CurrentMediaTime_Locked();
45 reference_wall_ticks_
= tick_clock_
->NowTicks();
48 playback_rate_
= playback_rate
;
51 void WallClockTimeSource::SetMediaTime(base::TimeDelta time
) {
52 DVLOG(1) << __FUNCTION__
<< "(" << time
.InMicroseconds() << ")";
53 base::AutoLock
auto_lock(lock_
);
58 base::TimeDelta
WallClockTimeSource::CurrentMediaTime() {
59 base::AutoLock
auto_lock(lock_
);
60 return CurrentMediaTime_Locked();
63 base::TimeTicks
WallClockTimeSource::GetWallClockTime(base::TimeDelta time
) {
64 base::AutoLock
auto_lock(lock_
);
65 if (!ticking_
|| playback_rate_
== 0.0)
66 return base::TimeTicks();
68 // See notes about |time| values less than |base_time_| in TimeSource header.
69 return reference_wall_ticks_
+
70 base::TimeDelta::FromMicroseconds(
71 (time
- base_time_
).InMicroseconds() * playback_rate_
);
74 void WallClockTimeSource::SetTickClockForTesting(
75 scoped_ptr
<base::TickClock
> tick_clock
) {
76 tick_clock_
.swap(tick_clock
);
79 base::TimeDelta
WallClockTimeSource::CurrentMediaTime_Locked() {
80 lock_
.AssertAcquired();
81 if (!ticking_
|| playback_rate_
== 0.0)
84 base::TimeTicks now
= tick_clock_
->NowTicks();
86 base::TimeDelta::FromMicroseconds(
87 (now
- reference_wall_ticks_
).InMicroseconds() * playback_rate_
);