OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/clock.h" | 5 #include "media/base/clock.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time/tick_clock.h" | 10 #include "base/time/tick_clock.h" |
11 #include "media/base/buffers.h" | 11 #include "media/base/buffers.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 | 14 |
15 Clock::Clock(base::TickClock* clock) | 15 Clock::Clock(base::TickClock* clock) |
16 : clock_(clock), | 16 : clock_(clock), |
17 playing_(false), | 17 playing_(false), |
18 underflow_(false), | 18 underflow_(false), |
19 playback_rate_(1.0f), | 19 playback_rate_(1.0f), |
20 max_time_(kNoTimestamp()), | 20 max_time_(kNoTimestamp()) { |
21 duration_(kNoTimestamp()) { | |
22 DCHECK(clock_); | 21 DCHECK(clock_); |
23 } | 22 } |
24 | 23 |
25 Clock::~Clock() {} | 24 Clock::~Clock() {} |
26 | 25 |
27 bool Clock::IsPlaying() const { | 26 bool Clock::IsPlaying() const { |
28 return playing_; | 27 return playing_; |
29 } | 28 } |
30 | 29 |
31 base::TimeDelta Clock::Play() { | 30 base::TimeDelta Clock::Play() { |
(...skipping 18 matching lines...) Expand all Loading... |
50 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) { | 49 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) { |
51 DCHECK(current_time <= max_time); | 50 DCHECK(current_time <= max_time); |
52 DCHECK(current_time != kNoTimestamp()); | 51 DCHECK(current_time != kNoTimestamp()); |
53 | 52 |
54 UpdateReferencePoints(current_time); | 53 UpdateReferencePoints(current_time); |
55 max_time_ = ClampToValidTimeRange(max_time); | 54 max_time_ = ClampToValidTimeRange(max_time); |
56 underflow_ = false; | 55 underflow_ = false; |
57 } | 56 } |
58 | 57 |
59 base::TimeDelta Clock::Elapsed() { | 58 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. | 59 // The clock is not advancing, so return the last recorded time. |
64 if (!playing_ || underflow_) | 60 if (!playing_ || underflow_) |
65 return media_time_; | 61 return media_time_; |
66 | 62 |
67 base::TimeDelta elapsed = EstimatedElapsedTime(); | 63 base::TimeDelta elapsed = EstimatedElapsedTime(); |
68 if (max_time_ != kNoTimestamp() && elapsed > max_time_) { | 64 if (max_time_ != kNoTimestamp() && elapsed > max_time_) { |
69 UpdateReferencePoints(max_time_); | 65 UpdateReferencePoints(max_time_); |
70 underflow_ = true; | 66 underflow_ = true; |
71 elapsed = max_time_; | 67 elapsed = max_time_; |
72 } | 68 } |
73 | 69 |
74 return elapsed; | 70 return elapsed; |
75 } | 71 } |
76 | 72 |
77 void Clock::SetMaxTime(base::TimeDelta max_time) { | 73 void Clock::SetMaxTime(base::TimeDelta max_time) { |
78 DCHECK(max_time != kNoTimestamp()); | 74 DCHECK(max_time != kNoTimestamp()); |
79 | 75 |
80 UpdateReferencePoints(); | 76 UpdateReferencePoints(); |
81 max_time_ = ClampToValidTimeRange(max_time); | 77 max_time_ = ClampToValidTimeRange(max_time); |
82 | 78 |
83 underflow_ = media_time_ > max_time_; | 79 underflow_ = media_time_ > max_time_; |
84 if (underflow_) | 80 if (underflow_) |
85 media_time_ = max_time_; | 81 media_time_ = max_time_; |
86 } | 82 } |
87 | 83 |
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 | |
97 base::TimeDelta Clock::ElapsedViaProvidedTime( | 84 base::TimeDelta Clock::ElapsedViaProvidedTime( |
98 const base::TimeTicks& time) const { | 85 const base::TimeTicks& time) const { |
99 // TODO(scherkus): floating point badness scaling time by playback rate. | 86 // TODO(scherkus): floating point badness scaling time by playback rate. |
100 int64 now_us = (time - reference_).InMicroseconds(); | 87 int64 now_us = (time - reference_).InMicroseconds(); |
101 now_us = static_cast<int64>(now_us * playback_rate_); | 88 now_us = static_cast<int64>(now_us * playback_rate_); |
102 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); | 89 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); |
103 } | 90 } |
104 | 91 |
105 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const { | 92 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const { |
106 if (duration_ == kNoTimestamp()) | 93 return std::max(base::TimeDelta(), time); |
107 return base::TimeDelta(); | |
108 return std::max(std::min(time, duration_), base::TimeDelta()); | |
109 } | |
110 | |
111 base::TimeDelta Clock::Duration() const { | |
112 if (duration_ == kNoTimestamp()) | |
113 return base::TimeDelta(); | |
114 return duration_; | |
115 } | 94 } |
116 | 95 |
117 void Clock::UpdateReferencePoints() { | 96 void Clock::UpdateReferencePoints() { |
118 UpdateReferencePoints(Elapsed()); | 97 UpdateReferencePoints(Elapsed()); |
119 } | 98 } |
120 | 99 |
121 void Clock::UpdateReferencePoints(base::TimeDelta current_time) { | 100 void Clock::UpdateReferencePoints(base::TimeDelta current_time) { |
122 media_time_ = ClampToValidTimeRange(current_time); | 101 media_time_ = ClampToValidTimeRange(current_time); |
123 reference_ = clock_->NowTicks(); | 102 reference_ = clock_->NowTicks(); |
124 } | 103 } |
125 | 104 |
126 base::TimeDelta Clock::EstimatedElapsedTime() { | 105 base::TimeDelta Clock::EstimatedElapsedTime() { |
127 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks())); | 106 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks())); |
128 } | 107 } |
129 | 108 |
130 } // namespace media | 109 } // namespace media |
OLD | NEW |