blob: ea954834e94d3c4138313bc922123e71981eb1a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/clock.h"
#include <algorithm>
#include "base/logging.h"
#include "base/time/tick_clock.h"
#include "media/base/buffers.h"
namespace media {
Clock::Clock(base::TickClock* clock) : clock_(clock) {
DCHECK(clock_);
Reset();
}
Clock::~Clock() {}
bool Clock::IsPlaying() const {
return playing_;
}
base::TimeDelta Clock::Play() {
DCHECK(!playing_);
UpdateReferencePoints();
playing_ = true;
return media_time_;
}
base::TimeDelta Clock::Pause() {
DCHECK(playing_);
UpdateReferencePoints();
playing_ = false;
return media_time_;
}
void Clock::SetPlaybackRate(float playback_rate) {
UpdateReferencePoints();
playback_rate_ = playback_rate;
}
void Clock::SetTime(base::TimeDelta current_time, base::TimeDelta max_time) {
DCHECK(current_time <= max_time);
DCHECK(current_time != kNoTimestamp());
UpdateReferencePoints(current_time);
max_time_ = ClampToValidTimeRange(max_time);
underflow_ = false;
}
base::TimeDelta Clock::Elapsed() {
if (duration_ == kNoTimestamp())
return base::TimeDelta();
// The clock is not advancing, so return the last recorded time.
if (!playing_ || underflow_)
return media_time_;
base::TimeDelta elapsed = EstimatedElapsedTime();
if (max_time_ != kNoTimestamp() && elapsed > max_time_) {
UpdateReferencePoints(max_time_);
underflow_ = true;
elapsed = max_time_;
}
return elapsed;
}
void Clock::SetMaxTime(base::TimeDelta max_time) {
DCHECK(max_time != kNoTimestamp());
UpdateReferencePoints();
max_time_ = ClampToValidTimeRange(max_time);
underflow_ = media_time_ > max_time_;
if (underflow_)
media_time_ = max_time_;
}
void Clock::SetDuration(base::TimeDelta duration) {
DCHECK(duration > base::TimeDelta());
duration_ = duration;
media_time_ = ClampToValidTimeRange(media_time_);
if (max_time_ != kNoTimestamp())
max_time_ = ClampToValidTimeRange(max_time_);
}
base::TimeDelta Clock::ElapsedViaProvidedTime(
const base::TimeTicks& time) const {
// TODO(scherkus): floating point badness scaling time by playback rate.
int64 now_us = (time - reference_).InMicroseconds();
now_us = static_cast<int64>(now_us * playback_rate_);
return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
}
base::TimeDelta Clock::ClampToValidTimeRange(base::TimeDelta time) const {
if (duration_ == kNoTimestamp())
return base::TimeDelta();
return std::max(std::min(time, duration_), base::TimeDelta());
}
void Clock::EndOfStream() {
Pause();
SetTime(Duration(), Duration());
}
base::TimeDelta Clock::Duration() const {
if (duration_ == kNoTimestamp())
return base::TimeDelta();
return duration_;
}
void Clock::UpdateReferencePoints() {
UpdateReferencePoints(Elapsed());
}
void Clock::UpdateReferencePoints(base::TimeDelta current_time) {
media_time_ = ClampToValidTimeRange(current_time);
reference_ = clock_->NowTicks();
}
base::TimeDelta Clock::EstimatedElapsedTime() {
return ClampToValidTimeRange(ElapsedViaProvidedTime(clock_->NowTicks()));
}
void Clock::Reset() {
playing_ = false;
playback_rate_ = 1.0f;
max_time_ = kNoTimestamp();
duration_ = kNoTimestamp();
media_time_ = base::TimeDelta();
reference_ = base::TimeTicks();
underflow_ = false;
}
} // namespace media
|