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/time_delta_interpolator.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 TimeDeltaInterpolator::TimeDeltaInterpolator(base::TickClock* tick_clock) |
16 : clock_(clock), | 16 : tick_clock_(tick_clock), |
17 playing_(false), | 17 interpolating_(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 DCHECK(clock_); | 21 DCHECK(tick_clock_); |
22 } | 22 } |
23 | 23 |
24 Clock::~Clock() {} | 24 TimeDeltaInterpolator::~TimeDeltaInterpolator() {} |
25 | 25 |
26 bool Clock::IsPlaying() const { | 26 base::TimeDelta TimeDeltaInterpolator::StartInterpolating() { |
27 return playing_; | 27 DCHECK(!interpolating_); |
28 } | |
29 | |
30 base::TimeDelta Clock::Play() { | |
31 DCHECK(!playing_); | |
32 UpdateReferencePoints(); | 28 UpdateReferencePoints(); |
33 playing_ = true; | 29 interpolating_ = true; |
34 return media_time_; | 30 return media_time_; |
35 } | 31 } |
36 | 32 |
37 base::TimeDelta Clock::Pause() { | 33 base::TimeDelta TimeDeltaInterpolator::StopInterpolating() { |
38 DCHECK(playing_); | 34 DCHECK(interpolating_); |
39 UpdateReferencePoints(); | 35 UpdateReferencePoints(); |
40 playing_ = false; | 36 interpolating_ = false; |
41 return media_time_; | 37 return media_time_; |
42 } | 38 } |
43 | 39 |
44 void Clock::SetPlaybackRate(float playback_rate) { | 40 void TimeDeltaInterpolator::SetPlaybackRate(float playback_rate) { |
45 UpdateReferencePoints(); | 41 UpdateReferencePoints(); |
46 playback_rate_ = playback_rate; | 42 playback_rate_ = playback_rate; |
47 } | 43 } |
48 | 44 |
49 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) { | 45 void TimeDeltaInterpolator::SetTime(base::TimeDelta current_time, |
46 base::TimeDelta max_time) { | |
50 DCHECK(current_time <= max_time); | 47 DCHECK(current_time <= max_time); |
51 DCHECK(current_time != kNoTimestamp()); | 48 DCHECK(current_time != kNoTimestamp()); |
52 | 49 |
53 UpdateReferencePoints(current_time); | 50 UpdateReferencePoints(current_time); |
54 max_time_ = ClampToValidTimeRange(max_time); | 51 max_time_ = ClampToValidTimeRange(max_time); |
55 underflow_ = false; | 52 underflow_ = false; |
56 } | 53 } |
57 | 54 |
58 base::TimeDelta Clock::Elapsed() { | 55 base::TimeDelta TimeDeltaInterpolator::GetInterpolatedTime() { |
59 // The clock is not advancing, so return the last recorded time. | 56 // The clock is not advancing, so return the last recorded time. |
60 if (!playing_ || underflow_) | 57 if (!interpolating_ || underflow_) |
acolwell GONE FROM CHROMIUM
2014/07/09 00:42:46
hmm.. seems like we should merge these into a sing
scherkus (not reviewing)
2014/07/09 01:54:32
I noticed the same thing but wanted to keep this C
| |
61 return media_time_; | 58 return media_time_; |
62 | 59 |
63 base::TimeDelta elapsed = EstimatedElapsedTime(); | 60 base::TimeDelta elapsed = EstimatedElapsedTime(); |
64 if (max_time_ != kNoTimestamp() && elapsed > max_time_) { | 61 if (max_time_ != kNoTimestamp() && elapsed > max_time_) { |
65 UpdateReferencePoints(max_time_); | 62 UpdateReferencePoints(max_time_); |
66 underflow_ = true; | 63 underflow_ = true; |
67 elapsed = max_time_; | 64 elapsed = max_time_; |
68 } | 65 } |
69 | 66 |
70 return elapsed; | 67 return elapsed; |
71 } | 68 } |
72 | 69 |
73 void Clock::SetMaxTime(base::TimeDelta max_time) { | 70 void TimeDeltaInterpolator::SetMaxTime(base::TimeDelta max_time) { |
74 DCHECK(max_time != kNoTimestamp()); | 71 DCHECK(max_time != kNoTimestamp()); |
75 | 72 |
76 UpdateReferencePoints(); | 73 UpdateReferencePoints(); |
77 max_time_ = ClampToValidTimeRange(max_time); | 74 max_time_ = ClampToValidTimeRange(max_time); |
78 | 75 |
79 underflow_ = media_time_ > max_time_; | 76 underflow_ = media_time_ > max_time_; |
80 if (underflow_) | 77 if (underflow_) |
81 media_time_ = max_time_; | 78 media_time_ = max_time_; |
82 } | 79 } |
83 | 80 |
84 base::TimeDelta Clock::ElapsedViaProvidedTime( | 81 base::TimeDelta TimeDeltaInterpolator::ElapsedViaProvidedTime( |
85 const base::TimeTicks& time) const { | 82 const base::TimeTicks& time) const { |
86 // TODO(scherkus): floating point badness scaling time by playback rate. | 83 // TODO(scherkus): floating point badness scaling time by playback rate. |
87 int64 now_us = (time - reference_).InMicroseconds(); | 84 int64 now_us = (time - reference_).InMicroseconds(); |
88 now_us = static_cast<int64>(now_us * playback_rate_); | 85 now_us = static_cast<int64>(now_us * playback_rate_); |
89 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); | 86 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); |
90 } | 87 } |
91 | 88 |
92 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const { | 89 base::TimeDelta TimeDeltaInterpolator::ClampToValidTimeRange( |
90 base::TimeDelta time) const { | |
93 return std::max(base::TimeDelta(), time); | 91 return std::max(base::TimeDelta(), time); |
94 } | 92 } |
95 | 93 |
96 void Clock::UpdateReferencePoints() { | 94 void TimeDeltaInterpolator::UpdateReferencePoints() { |
97 UpdateReferencePoints(Elapsed()); | 95 UpdateReferencePoints(GetInterpolatedTime()); |
98 } | 96 } |
99 | 97 |
100 void Clock::UpdateReferencePoints(base::TimeDelta current_time) { | 98 void TimeDeltaInterpolator::UpdateReferencePoints( |
99 base::TimeDelta current_time) { | |
101 media_time_ = ClampToValidTimeRange(current_time); | 100 media_time_ = ClampToValidTimeRange(current_time); |
102 reference_ = clock_->NowTicks(); | 101 reference_ = tick_clock_->NowTicks(); |
103 } | 102 } |
104 | 103 |
105 base::TimeDelta Clock::EstimatedElapsedTime() { | 104 base::TimeDelta TimeDeltaInterpolator::EstimatedElapsedTime() { |
106 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks())); | 105 return ClampToValidTimeRange(ElapsedViaProvidedTime(tick_clock_->NowTicks())); |
107 } | 106 } |
108 | 107 |
109 } // namespace media | 108 } // namespace media |
OLD | NEW |