BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / base / wall_clock_time_source.cc
blob33ec372e5008ea732c1f7a600c15fbdc8d832841
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_time_ = tick_clock_->NowTicks();
26 void WallClockTimeSource::StopTicking() {
27 DVLOG(1) << __FUNCTION__;
28 base::AutoLock auto_lock(lock_);
29 DCHECK(ticking_);
30 base_timestamp_ = CurrentMediaTime_Locked();
31 ticking_ = false;
32 reference_time_ = 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_timestamp_ = CurrentMediaTime_Locked();
42 reference_time_ = 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_timestamp_ = 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 DCHECK(wall_clock_times->empty());
66 if (media_timestamps.empty()) {
67 wall_clock_times->push_back(reference_time_);
68 } else {
69 // When playback is paused (rate is zero), assume a rate of 1.0.
70 const double playback_rate = playback_rate_ ? playback_rate_ : 1.0;
72 wall_clock_times->reserve(media_timestamps.size());
73 for (const auto& media_timestamp : media_timestamps) {
74 wall_clock_times->push_back(reference_time_ +
75 (media_timestamp - base_timestamp_) /
76 playback_rate);
80 return playback_rate_ && ticking_;
83 base::TimeDelta WallClockTimeSource::CurrentMediaTime_Locked() {
84 lock_.AssertAcquired();
85 if (!ticking_ || !playback_rate_)
86 return base_timestamp_;
88 base::TimeTicks now = tick_clock_->NowTicks();
89 return base_timestamp_ +
90 base::TimeDelta::FromMicroseconds(
91 (now - reference_time_).InMicroseconds() * playback_rate_);
94 } // namespace media