[Extensions Toolbar] Make the ExtensionToolbarModel icon count more stable
[chromium-blink-merge.git] / media / base / wall_clock_time_source.cc
blob408de93e05e830867278d3e667bc3ff4ad59abb2
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"
10 namespace media {
12 WallClockTimeSource::WallClockTimeSource()
13 : tick_clock_(new base::DefaultTickClock()),
14 ticking_(false),
15 playback_rate_(1.0f) {
18 WallClockTimeSource::~WallClockTimeSource() {
21 void WallClockTimeSource::StartTicking() {
22 DVLOG(1) << __FUNCTION__;
23 base::AutoLock auto_lock(lock_);
24 DCHECK(!ticking_);
25 ticking_ = true;
26 reference_wall_ticks_ = tick_clock_->NowTicks();
29 void WallClockTimeSource::StopTicking() {
30 DVLOG(1) << __FUNCTION__;
31 base::AutoLock auto_lock(lock_);
32 DCHECK(ticking_);
33 base_time_ = CurrentMediaTime_Locked();
34 ticking_ = false;
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
42 // the new rate.
43 if (ticking_) {
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_);
54 CHECK(!ticking_);
55 base_time_ = time;
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();
74 if (!ticking_)
75 return base_time_;
77 base::TimeTicks now = tick_clock_->NowTicks();
78 return base_time_ +
79 base::TimeDelta::FromMicroseconds(
80 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_);
83 } // namespace media