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

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

Issue 9155003: Fix media timeline so that thumb never exceeds buffered data (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 11 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
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 "base/logging.h" 7 #include "base/logging.h"
8 #include "media/base/buffers.h" 8 #include "media/base/buffers.h"
9 9
10 namespace media { 10 namespace media {
11 11
12 Clock::Clock(TimeProvider* time_provider) 12 Clock::Clock(TimeProvider* time_provider)
13 : time_provider_(time_provider), 13 : time_provider_(time_provider) {
14 playing_(false), 14 Reset();
15 playback_rate_(1.0f) {
16 } 15 }
17 16
18 Clock::~Clock() {} 17 Clock::~Clock() {}
19 18
20 bool Clock::IsPlaying() const { 19 bool Clock::IsPlaying() const {
21 return playing_; 20 return playing_;
22 } 21 }
23 22
24 base::TimeDelta Clock::Play() { 23 base::TimeDelta Clock::Play() {
25 DCHECK(!playing_); 24 DCHECK(!playing_);
26 reference_ = GetTimeFromProvider(); 25 UpdateReferencePoints();
27 playing_ = true; 26 playing_ = true;
28 return media_time_; 27 return media_time_;
29 } 28 }
30 29
31 base::TimeDelta Clock::Pause() { 30 base::TimeDelta Clock::Pause() {
32 DCHECK(playing_); 31 DCHECK(playing_);
33 // Save our new accumulated amount of media time. 32 UpdateReferencePoints();
34 media_time_ = Elapsed();
35 playing_ = false; 33 playing_ = false;
36 return media_time_; 34 return media_time_;
37 } 35 }
38 36
39 void Clock::SetPlaybackRate(float playback_rate) { 37 void Clock::SetPlaybackRate(float playback_rate) {
40 if (playing_) { 38 UpdateReferencePoints();
41 base::Time time = GetTimeFromProvider();
42 media_time_ = ElapsedViaProvidedTime(time);
43 reference_ = time;
44 }
45 playback_rate_ = playback_rate; 39 playback_rate_ = playback_rate;
46 } 40 }
47 41
48 void Clock::SetTime(const base::TimeDelta& time) { 42 void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) {
49 if (time == kNoTimestamp()) { 43 DCHECK(current_time <= max_time);
44 if (current_time == kNoTimestamp()) {
acolwell GONE FROM CHROMIUM 2012/01/24 18:13:59 Should we convert this into a DCHECK? Any idea why
vrk (LEFT CHROMIUM) 2012/01/25 00:09:32 Introduced in this CL: http://codereview.chromium.
50 NOTREACHED(); 45 NOTREACHED();
51 return; 46 return;
52 } 47 }
53 if (playing_) { 48 underflow_ = false;
54 reference_ = GetTimeFromProvider(); 49 UpdateReferencePoints(current_time);
55 } 50 SetMaxTime(max_time);
56 media_time_ = time;
57 } 51 }
58 52
59 base::TimeDelta Clock::Elapsed() const { 53 base::TimeDelta Clock::Elapsed() {
60 if (!playing_) { 54 if (duration_ == kNoTimestamp())
55 return base::TimeDelta();
56
57 if (!IsTimeProgressing())
61 return media_time_; 58 return media_time_;
59
60 base::TimeDelta elapsed = EstimatedElapsedTime();
61 if (max_time_ != kNoTimestamp() && elapsed > max_time_) {
62 UpdateReferencePoints(max_time_);
63 underflow_ = true;
64 elapsed = max_time_;
62 } 65 }
63 return ElapsedViaProvidedTime(GetTimeFromProvider()); 66
67 return elapsed;
68 }
69
70 void Clock::SetMaxTime(base::TimeDelta max_time) {
71 DCHECK(max_time != kNoTimestamp());
72 UpdateReferencePoints();
73 max_time_ = ClampToValidTimeRange(max_time);
74
75 DCHECK(media_time_ <= max_time_);
76 underflow_ = false;
77 }
78
79 void Clock::SetDuration(base::TimeDelta duration) {
80 DCHECK(duration_ == kNoTimestamp());
81 DCHECK(duration > base::TimeDelta());
82 duration_ = duration;
64 } 83 }
65 84
66 base::TimeDelta Clock::ElapsedViaProvidedTime(const base::Time& time) const { 85 base::TimeDelta Clock::ElapsedViaProvidedTime(const base::Time& time) const {
67 // TODO(scherkus): floating point badness scaling time by playback rate. 86 // TODO(scherkus): floating point badness scaling time by playback rate.
68 int64 now_us = (time - reference_).InMicroseconds(); 87 int64 now_us = (time - reference_).InMicroseconds();
69 now_us = static_cast<int64>(now_us * playback_rate_); 88 now_us = static_cast<int64>(now_us * playback_rate_);
70 return media_time_ + base::TimeDelta::FromMicroseconds(now_us); 89 return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
71 } 90 }
72 91
73 base::Time Clock::GetTimeFromProvider() const { 92 base::Time Clock::GetTimeFromProvider() const {
74 if (time_provider_) { 93 if (time_provider_)
75 return time_provider_(); 94 return time_provider_();
76 }
77 return base::Time(); 95 return base::Time();
78 } 96 }
79 97
98 base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const {
99 if (duration_ == kNoTimestamp())
100 return base::TimeDelta();
101 return std::max(std::min(time, duration_), base::TimeDelta());
102 }
103
104 void Clock::EndOfStream() {
105 Pause();
106 SetTime(Duration(), Duration());
107 }
108
109 base::TimeDelta Clock::Duration() const {
110 if (duration_ == kNoTimestamp())
111 return base::TimeDelta();
112 return duration_;
113 }
114
115 bool Clock::IsTimeProgressing() const {
116 return playing_ && !underflow_;
117 }
118
119 void Clock::UpdateReferencePoints() {
120 if (IsTimeProgressing())
acolwell GONE FROM CHROMIUM 2012/01/24 18:13:59 I think this function body can be replaced with Up
vrk (LEFT CHROMIUM) 2012/01/25 00:09:32 Yes, you are right! Done.
121 media_time_ = Elapsed();
122 else
123 DCHECK(media_time_ == Elapsed());
124 reference_ = GetTimeFromProvider();
125 }
126
127 void Clock::UpdateReferencePoints(base::TimeDelta current_time) {
128 media_time_ = ClampToValidTimeRange(current_time);
129 reference_ = GetTimeFromProvider();
130 }
131
132 base::TimeDelta Clock::EstimatedElapsedTime() {
133 return ClampToValidTimeRange(
134 ElapsedViaProvidedTime(GetTimeFromProvider()));
135 }
136
137 void Clock::Reset() {
138 playing_ = false;
139 playback_rate_ = 1.0f;
140 max_time_ = kNoTimestamp();
141 duration_ = kNoTimestamp();
142 media_time_ = base::TimeDelta();
143 reference_ = base::Time();
144 underflow_ = false;
145 }
146
80 } // namespace media 147 } // namespace media
OLDNEW
« no previous file with comments | « media/base/clock.h ('k') | media/base/clock_unittest.cc » ('j') | media/base/clock_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698