OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/wall_clock_time_source.h" | 5 #include "media/base/wall_clock_time_source.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/time/default_tick_clock.h" | 8 #include "base/time/default_tick_clock.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 WallClockTimeSource::WallClockTimeSource() | 12 WallClockTimeSource::WallClockTimeSource() |
13 : tick_clock_(new base::DefaultTickClock()), | 13 : tick_clock_(new base::DefaultTickClock()), |
14 ticking_(false), | 14 ticking_(false), |
15 playback_rate_(1.0f) { | 15 playback_rate_(1.0f) { |
16 } | 16 } |
17 | 17 |
18 WallClockTimeSource::~WallClockTimeSource() { | 18 WallClockTimeSource::~WallClockTimeSource() { |
19 } | 19 } |
20 | 20 |
21 void WallClockTimeSource::StartTicking() { | 21 void WallClockTimeSource::StartTicking() { |
| 22 base::AutoLock auto_lock(lock_); |
22 DCHECK(!ticking_); | 23 DCHECK(!ticking_); |
23 ticking_ = true; | 24 ticking_ = true; |
24 reference_wall_ticks_ = tick_clock_->NowTicks(); | 25 reference_wall_ticks_ = tick_clock_->NowTicks(); |
25 } | 26 } |
26 | 27 |
27 void WallClockTimeSource::StopTicking() { | 28 void WallClockTimeSource::StopTicking() { |
| 29 base::AutoLock auto_lock(lock_); |
28 DCHECK(ticking_); | 30 DCHECK(ticking_); |
29 base_time_ = CurrentMediaTime(); | 31 base_time_ = CurrentMediaTime_Locked(); |
30 ticking_ = false; | 32 ticking_ = false; |
31 reference_wall_ticks_ = tick_clock_->NowTicks(); | 33 reference_wall_ticks_ = tick_clock_->NowTicks(); |
32 } | 34 } |
33 | 35 |
34 void WallClockTimeSource::SetPlaybackRate(float playback_rate) { | 36 void WallClockTimeSource::SetPlaybackRate(float playback_rate) { |
| 37 base::AutoLock auto_lock(lock_); |
35 // Estimate current media time using old rate to use as a new base time for | 38 // Estimate current media time using old rate to use as a new base time for |
36 // the new rate. | 39 // the new rate. |
37 if (ticking_) { | 40 if (ticking_) { |
38 base_time_ = CurrentMediaTime(); | 41 base_time_ = CurrentMediaTime_Locked(); |
39 reference_wall_ticks_ = tick_clock_->NowTicks(); | 42 reference_wall_ticks_ = tick_clock_->NowTicks(); |
40 } | 43 } |
41 | 44 |
42 playback_rate_ = playback_rate; | 45 playback_rate_ = playback_rate; |
43 } | 46 } |
44 | 47 |
45 void WallClockTimeSource::SetMediaTime(base::TimeDelta time) { | 48 void WallClockTimeSource::SetMediaTime(base::TimeDelta time) { |
| 49 base::AutoLock auto_lock(lock_); |
46 CHECK(!ticking_); | 50 CHECK(!ticking_); |
47 base_time_ = time; | 51 base_time_ = time; |
48 } | 52 } |
49 | 53 |
50 base::TimeDelta WallClockTimeSource::CurrentMediaTime() { | 54 base::TimeDelta WallClockTimeSource::CurrentMediaTime() { |
51 if (!ticking_) | 55 base::AutoLock auto_lock(lock_); |
52 return base_time_; | 56 return CurrentMediaTime_Locked(); |
53 | |
54 base::TimeTicks now = tick_clock_->NowTicks(); | |
55 return base_time_ + | |
56 base::TimeDelta::FromMicroseconds( | |
57 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_); | |
58 } | 57 } |
59 | 58 |
60 base::TimeDelta WallClockTimeSource::CurrentMediaTimeForSyncingVideo() { | 59 base::TimeDelta WallClockTimeSource::CurrentMediaTimeForSyncingVideo() { |
61 return CurrentMediaTime(); | 60 return CurrentMediaTime(); |
62 } | 61 } |
63 | 62 |
64 void WallClockTimeSource::SetTickClockForTesting( | 63 void WallClockTimeSource::SetTickClockForTesting( |
65 scoped_ptr<base::TickClock> tick_clock) { | 64 scoped_ptr<base::TickClock> tick_clock) { |
66 tick_clock_.swap(tick_clock); | 65 tick_clock_.swap(tick_clock); |
67 } | 66 } |
68 | 67 |
| 68 base::TimeDelta WallClockTimeSource::CurrentMediaTime_Locked() { |
| 69 lock_.AssertAcquired(); |
| 70 if (!ticking_) |
| 71 return base_time_; |
| 72 |
| 73 base::TimeTicks now = tick_clock_->NowTicks(); |
| 74 return base_time_ + |
| 75 base::TimeDelta::FromMicroseconds( |
| 76 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_); |
| 77 } |
| 78 |
69 } // namespace media | 79 } // namespace media |
OLD | NEW |