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::TimeDelta
WallClockTimeSource::CurrentMediaTimeForSyncingVideo() {
64 return CurrentMediaTime();
67 void WallClockTimeSource::SetTickClockForTesting(
68 scoped_ptr
<base::TickClock
> tick_clock
) {
69 tick_clock_
.swap(tick_clock
);
72 base::TimeDelta
WallClockTimeSource::CurrentMediaTime_Locked() {
73 lock_
.AssertAcquired();
77 base::TimeTicks now
= tick_clock_
->NowTicks();
79 base::TimeDelta::FromMicroseconds(
80 (now
- reference_wall_ticks_
).InMicroseconds() * playback_rate_
);