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
Clock(base::TickClock * clock)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 duration_(kNoTimestamp()) {
22 DCHECK(clock_);
23 }
24
~Clock()25 Clock::~Clock() {}
26
IsPlaying() const27 bool Clock::IsPlaying() const {
28 return playing_;
29 }
30
Play()31 base::TimeDelta Clock::Play() {
32 DCHECK(!playing_);
33 UpdateReferencePoints();
34 playing_ = true;
35 return media_time_;
36 }
37
Pause()38 base::TimeDelta Clock::Pause() {
39 DCHECK(playing_);
40 UpdateReferencePoints();
41 playing_ = false;
42 return media_time_;
43 }
44
SetPlaybackRate(float playback_rate)45 void Clock::SetPlaybackRate(float playback_rate) {
46 UpdateReferencePoints();
47 playback_rate_ = playback_rate;
48 }
49
SetTime(base::TimeDelta current_time,base::TimeDelta max_time)50 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) {
51 DCHECK(current_time <= max_time);
52 DCHECK(current_time != kNoTimestamp());
53
54 UpdateReferencePoints(current_time);
55 max_time_ = ClampToValidTimeRange(max_time);
56 underflow_ = false;
57 }
58
Elapsed()59 base::TimeDelta Clock::Elapsed() {
60 if (duration_ == kNoTimestamp())
61 return base::TimeDelta();
62
63 // The clock is not advancing, so return the last recorded time.
64 if (!playing_ || underflow_)
65 return media_time_;
66
67 base::TimeDelta elapsed = EstimatedElapsedTime();
68 if (max_time_ != kNoTimestamp() && elapsed > max_time_) {
69 UpdateReferencePoints(max_time_);
70 underflow_ = true;
71 elapsed = max_time_;
72 }
73
74 return elapsed;
75 }
76
SetMaxTime(base::TimeDelta max_time)77 void Clock::SetMaxTime(base::TimeDelta max_time) {
78 DCHECK(max_time != kNoTimestamp());
79
80 UpdateReferencePoints();
81 max_time_ = ClampToValidTimeRange(max_time);
82
83 underflow_ = media_time_ > max_time_;
84 if (underflow_)
85 media_time_ = max_time_;
86 }
87
SetDuration(base::TimeDelta duration)88 void Clock::SetDuration(base::TimeDelta duration) {
89 DCHECK(duration > base::TimeDelta());
90 duration_ = duration;
91
92 media_time_ = ClampToValidTimeRange(media_time_);
93 if (max_time_ != kNoTimestamp())
94 max_time_ = ClampToValidTimeRange(max_time_);
95 }
96
ElapsedViaProvidedTime(const base::TimeTicks & time) const97 base::TimeDelta Clock::ElapsedViaProvidedTime(
98 const base::TimeTicks& time) const {
99 // TODO(scherkus): floating point badness scaling time by playback rate.
100 int64 now_us = (time - reference_).InMicroseconds();
101 now_us = static_cast<int64>(now_us * playback_rate_);
102 return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
103 }
104
ClampToValidTimeRange(base::TimeDelta time) const105 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const {
106 if (duration_ == kNoTimestamp())
107 return base::TimeDelta();
108 return std::max(std::min(time, duration_), base::TimeDelta());
109 }
110
Duration() const111 base::TimeDelta Clock::Duration() const {
112 if (duration_ == kNoTimestamp())
113 return base::TimeDelta();
114 return duration_;
115 }
116
UpdateReferencePoints()117 void Clock::UpdateReferencePoints() {
118 UpdateReferencePoints(Elapsed());
119 }
120
UpdateReferencePoints(base::TimeDelta current_time)121 void Clock::UpdateReferencePoints(base::TimeDelta current_time) {
122 media_time_ = ClampToValidTimeRange(current_time);
123 reference_ = clock_->NowTicks();
124 }
125
EstimatedElapsedTime()126 base::TimeDelta Clock::EstimatedElapsedTime() {
127 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks()));
128 }
129
130 } // namespace media
131