Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / media / base / wall_clock_time_source.cc
blob1c59be948bbf6f22b2fe8472d93d3ac78f29e5c5
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"
9 namespace media {
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_);
21 DCHECK(!ticking_);
22 ticking_ = true;
23 reference_wall_ticks_ = tick_clock_->NowTicks();
26 void WallClockTimeSource::StopTicking() {
27 DVLOG(1) << __FUNCTION__;
28 base::AutoLock auto_lock(lock_);
29 DCHECK(ticking_);
30 base_time_ = CurrentMediaTime_Locked();
31 ticking_ = false;
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
39 // the new rate.
40 if (ticking_) {
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_);
51 CHECK(!ticking_);
52 base_time_ = time;
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_)
65 return false;
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_));
75 return true;
78 base::TimeDelta WallClockTimeSource::CurrentMediaTime_Locked() {
79 lock_.AssertAcquired();
80 if (!ticking_ || !playback_rate_)
81 return base_time_;
83 base::TimeTicks now = tick_clock_->NowTicks();
84 return base_time_ +
85 base::TimeDelta::FromMicroseconds(
86 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
89 } // namespace media