| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/clock.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/time/tick_clock.h" | |
| 11 #include "media/base/buffers.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 Clock::Clock(base::TickClock* clock) | |
| 16 : clock_(clock), | |
| 17 playing_(false), | |
| 18 underflow_(false), | |
| 19 playback_rate_(1.0f), | |
| 20 max_time_(kNoTimestamp()) { | |
| 21 DCHECK(clock_); | |
| 22 } | |
| 23 | |
| 24 Clock::~Clock() {} | |
| 25 | |
| 26 bool Clock::IsPlaying() const { | |
| 27 return playing_; | |
| 28 } | |
| 29 | |
| 30 base::TimeDelta Clock::Play() { | |
| 31 DCHECK(!playing_); | |
| 32 UpdateReferencePoints(); | |
| 33 playing_ = true; | |
| 34 return media_time_; | |
| 35 } | |
| 36 | |
| 37 base::TimeDelta Clock::Pause() { | |
| 38 DCHECK(playing_); | |
| 39 UpdateReferencePoints(); | |
| 40 playing_ = false; | |
| 41 return media_time_; | |
| 42 } | |
| 43 | |
| 44 void Clock::SetPlaybackRate(float playback_rate) { | |
| 45 UpdateReferencePoints(); | |
| 46 playback_rate_ = playback_rate; | |
| 47 } | |
| 48 | |
| 49 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) { | |
| 50 DCHECK(current_time <= max_time); | |
| 51 DCHECK(current_time != kNoTimestamp()); | |
| 52 | |
| 53 UpdateReferencePoints(current_time); | |
| 54 max_time_ = ClampToValidTimeRange(max_time); | |
| 55 underflow_ = false; | |
| 56 } | |
| 57 | |
| 58 base::TimeDelta Clock::Elapsed() { | |
| 59 // The clock is not advancing, so return the last recorded time. | |
| 60 if (!playing_ || underflow_) | |
| 61 return media_time_; | |
| 62 | |
| 63 base::TimeDelta elapsed = EstimatedElapsedTime(); | |
| 64 if (max_time_ != kNoTimestamp() && elapsed > max_time_) { | |
| 65 UpdateReferencePoints(max_time_); | |
| 66 underflow_ = true; | |
| 67 elapsed = max_time_; | |
| 68 } | |
| 69 | |
| 70 return elapsed; | |
| 71 } | |
| 72 | |
| 73 void Clock::SetMaxTime(base::TimeDelta max_time) { | |
| 74 DCHECK(max_time != kNoTimestamp()); | |
| 75 | |
| 76 UpdateReferencePoints(); | |
| 77 max_time_ = ClampToValidTimeRange(max_time); | |
| 78 | |
| 79 underflow_ = media_time_ > max_time_; | |
| 80 if (underflow_) | |
| 81 media_time_ = max_time_; | |
| 82 } | |
| 83 | |
| 84 base::TimeDelta Clock::ElapsedViaProvidedTime( | |
| 85 const base::TimeTicks& time) const { | |
| 86 // TODO(scherkus): floating point badness scaling time by playback rate. | |
| 87 int64 now_us = (time - reference_).InMicroseconds(); | |
| 88 now_us = static_cast<int64>(now_us * playback_rate_); | |
| 89 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); | |
| 90 } | |
| 91 | |
| 92 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const { | |
| 93 return std::max(base::TimeDelta(), time); | |
| 94 } | |
| 95 | |
| 96 void Clock::UpdateReferencePoints() { | |
| 97 UpdateReferencePoints(Elapsed()); | |
| 98 } | |
| 99 | |
| 100 void Clock::UpdateReferencePoints(base::TimeDelta current_time) { | |
| 101 media_time_ = ClampToValidTimeRange(current_time); | |
| 102 reference_ = clock_->NowTicks(); | |
| 103 } | |
| 104 | |
| 105 base::TimeDelta Clock::EstimatedElapsedTime() { | |
| 106 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks())); | |
| 107 } | |
| 108 | |
| 109 } // namespace media | |
| OLD | NEW |