Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(651)

Side by Side Diff: media/base/clock.cc

Issue 11607003: Add a Clock and TickClock interface for mocking out time (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix warnings Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/clock.h ('k') | media/base/clock_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/clock.h"
10 #include "media/base/buffers.h" 11 #include "media/base/buffers.h"
11 12
12 namespace media { 13 namespace media {
13 14
14 Clock::Clock(TimeProvider* time_provider) 15 Clock::Clock(base::Clock* clock) : clock_(clock) {
15 : time_provider_(time_provider) { 16 DCHECK(clock_);
16 Reset(); 17 Reset();
17 } 18 }
18 19
19 Clock::~Clock() {} 20 Clock::~Clock() {}
20 21
21 bool Clock::IsPlaying() const { 22 bool Clock::IsPlaying() const {
22 return playing_; 23 return playing_;
23 } 24 }
24 25
25 base::TimeDelta Clock::Play() { 26 base::TimeDelta Clock::Play() {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 max_time_ = ClampToValidTimeRange(max_time_); 89 max_time_ = ClampToValidTimeRange(max_time_);
89 } 90 }
90 91
91 base::TimeDelta Clock::ElapsedViaProvidedTime(const base::Time& time) const { 92 base::TimeDelta Clock::ElapsedViaProvidedTime(const base::Time& time) const {
92 // TODO(scherkus): floating point badness scaling time by playback rate. 93 // TODO(scherkus): floating point badness scaling time by playback rate.
93 int64 now_us = (time - reference_).InMicroseconds(); 94 int64 now_us = (time - reference_).InMicroseconds();
94 now_us = static_cast<int64>(now_us * playback_rate_); 95 now_us = static_cast<int64>(now_us * playback_rate_);
95 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); 96 return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
96 } 97 }
97 98
98 base::Time Clock::GetTimeFromProvider() const {
99 if (time_provider_)
100 return time_provider_();
101 return base::Time();
102 }
103
104 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const { 99 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const {
105 if (duration_ == kNoTimestamp()) 100 if (duration_ == kNoTimestamp())
106 return base::TimeDelta(); 101 return base::TimeDelta();
107 return std::max(std::min(time, duration_), base::TimeDelta()); 102 return std::max(std::min(time, duration_), base::TimeDelta());
108 } 103 }
109 104
110 void Clock::EndOfStream() { 105 void Clock::EndOfStream() {
111 Pause(); 106 Pause();
112 SetTime(Duration(), Duration()); 107 SetTime(Duration(), Duration());
113 } 108 }
114 109
115 base::TimeDelta Clock::Duration() const { 110 base::TimeDelta Clock::Duration() const {
116 if (duration_ == kNoTimestamp()) 111 if (duration_ == kNoTimestamp())
117 return base::TimeDelta(); 112 return base::TimeDelta();
118 return duration_; 113 return duration_;
119 } 114 }
120 115
121 void Clock::UpdateReferencePoints() { 116 void Clock::UpdateReferencePoints() {
122 UpdateReferencePoints(Elapsed()); 117 UpdateReferencePoints(Elapsed());
123 } 118 }
124 119
125 void Clock::UpdateReferencePoints(base::TimeDelta current_time) { 120 void Clock::UpdateReferencePoints(base::TimeDelta current_time) {
126 media_time_ = ClampToValidTimeRange(current_time); 121 media_time_ = ClampToValidTimeRange(current_time);
127 reference_ = GetTimeFromProvider(); 122 reference_ = clock_->Now();
128 } 123 }
129 124
130 base::TimeDelta Clock::EstimatedElapsedTime() { 125 base::TimeDelta Clock::EstimatedElapsedTime() {
131 return ClampToValidTimeRange( 126 return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->Now()));
132 ElapsedViaProvidedTime(GetTimeFromProvider()));
133 } 127 }
134 128
135 void Clock::Reset() { 129 void Clock::Reset() {
136 playing_ = false; 130 playing_ = false;
137 playback_rate_ = 1.0f; 131 playback_rate_ = 1.0f;
138 max_time_ = kNoTimestamp(); 132 max_time_ = kNoTimestamp();
139 duration_ = kNoTimestamp(); 133 duration_ = kNoTimestamp();
140 media_time_ = base::TimeDelta(); 134 media_time_ = base::TimeDelta();
141 reference_ = base::Time(); 135 reference_ = base::Time();
142 underflow_ = false; 136 underflow_ = false;
143 } 137 }
144 138
145 } // namespace media 139 } // namespace media
OLDNEW
« no previous file with comments | « media/base/clock.h ('k') | media/base/clock_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698