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"
11 WallClockTimeSource::WallClockTimeSource()
12 : tick_clock_(&default_tick_clock_
), ticking_(false), playback_rate_(1.0) {
15 WallClockTimeSource::~WallClockTimeSource() {
18 void WallClockTimeSource::StartTicking() {
19 DVLOG(1) << __FUNCTION__
;
20 base::AutoLock
auto_lock(lock_
);
23 reference_wall_ticks_
= tick_clock_
->NowTicks();
26 void WallClockTimeSource::StopTicking() {
27 DVLOG(1) << __FUNCTION__
;
28 base::AutoLock
auto_lock(lock_
);
30 base_time_
= CurrentMediaTime_Locked();
32 reference_wall_ticks_
= tick_clock_
->NowTicks();
35 void WallClockTimeSource::SetPlaybackRate(double playback_rate
) {
36 DVLOG(1) << __FUNCTION__
<< "(" << playback_rate
<< ")";
37 base::AutoLock
auto_lock(lock_
);
38 // Estimate current media time using old rate to use as a new base time for
41 base_time_
= CurrentMediaTime_Locked();
42 reference_wall_ticks_
= tick_clock_
->NowTicks();
45 playback_rate_
= playback_rate
;
48 void WallClockTimeSource::SetMediaTime(base::TimeDelta time
) {
49 DVLOG(1) << __FUNCTION__
<< "(" << time
.InMicroseconds() << ")";
50 base::AutoLock
auto_lock(lock_
);
55 base::TimeDelta
WallClockTimeSource::CurrentMediaTime() {
56 base::AutoLock
auto_lock(lock_
);
57 return CurrentMediaTime_Locked();
60 bool WallClockTimeSource::GetWallClockTimes(
61 const std::vector
<base::TimeDelta
>& media_timestamps
,
62 std::vector
<base::TimeTicks
>* wall_clock_times
) {
63 base::AutoLock
auto_lock(lock_
);
64 if (!ticking_
|| !playback_rate_
)
67 DCHECK(wall_clock_times
->empty());
68 wall_clock_times
->reserve(media_timestamps
.size());
69 for (const auto& media_timestamp
: media_timestamps
) {
70 wall_clock_times
->push_back(
71 reference_wall_ticks_
+
72 base::TimeDelta::FromMicroseconds(
73 (media_timestamp
- base_time_
).InMicroseconds() / playback_rate_
));
78 base::TimeDelta
WallClockTimeSource::CurrentMediaTime_Locked() {
79 lock_
.AssertAcquired();
80 if (!ticking_
|| !playback_rate_
)
83 base::TimeTicks now
= tick_clock_
->NowTicks();
85 base::TimeDelta::FromMicroseconds(
86 (now
- reference_wall_ticks_
).InMicroseconds() * playback_rate_
);