Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/base/wall_clock_time_source.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace media { | |
| 10 | |
| 11 WallClockTimeSource::WallClockTimeSource() | |
| 12 : ticking_(false), playback_rate_(1.0f) { | |
| 13 } | |
| 14 | |
| 15 WallClockTimeSource::~WallClockTimeSource() { | |
| 16 } | |
| 17 | |
| 18 void WallClockTimeSource::StartTicking() { | |
| 19 DCHECK(!ticking_); | |
| 20 ticking_ = true; | |
| 21 reference_wall_ticks_ = base::TimeTicks::Now(); | |
| 22 } | |
| 23 | |
| 24 void WallClockTimeSource::StopTicking() { | |
| 25 DCHECK(ticking_); | |
| 26 ticking_ = false; | |
| 27 base::TimeTicks now = base::TimeTicks::Now(); | |
| 28 base_timestamp_ += base::TimeDelta::FromMicroseconds( | |
| 29 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_); | |
| 30 reference_wall_ticks_ = now; | |
| 31 } | |
| 32 | |
| 33 void WallClockTimeSource::SetPlaybackRate(float playback_rate) { | |
| 34 base::TimeTicks now = base::TimeTicks::Now(); | |
| 35 | |
| 36 // Estimate current media time using old rate to use as a new base timestamp | |
| 37 // for the new rate. | |
| 38 if (ticking_) { | |
| 39 base_timestamp_ += base::TimeDelta::FromMicroseconds( | |
| 40 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_); | |
| 41 reference_wall_ticks_ = now; | |
| 42 } | |
| 43 | |
| 44 playback_rate_ = playback_rate; | |
| 45 } | |
| 46 | |
| 47 void WallClockTimeSource::SetMediaTimestamp(base::TimeDelta timestamp) { | |
| 48 CHECK(!ticking_); | |
| 49 base_timestamp_ = timestamp; | |
| 50 } | |
| 51 | |
| 52 base::TimeDelta WallClockTimeSource::CurrentMediaTimestamp() { | |
| 53 if (!ticking_) | |
| 54 return base_timestamp_; | |
| 55 | |
| 56 base::TimeTicks now = base::TimeTicks::Now(); | |
| 57 return base_timestamp_ + | |
| 58 base::TimeDelta::FromMicroseconds( | |
| 59 (now - reference_wall_ticks_).InMicroseconds() * playback_rate_); | |
|
xhwang
2014/07/12 06:36:44
nit: This appears 3 times and deserve a helper fun
scherkus (not reviewing)
2014/07/17 23:52:56
Done.
| |
| 60 } | |
| 61 | |
| 62 } // namespace media | |
| OLD | NEW |