[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / audio_clock.cc
blob29bf14e8558ebec285c8bc3ab84bd1805870d96d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/audio_clock.h"
7 #include <algorithm>
9 #include "base/logging.h"
11 namespace media {
13 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate)
14 : start_timestamp_(start_timestamp),
15 microseconds_per_frame_(
16 static_cast<double>(base::Time::kMicrosecondsPerSecond) /
17 sample_rate),
18 total_buffered_frames_(0),
19 front_timestamp_(start_timestamp),
20 back_timestamp_(start_timestamp) {
23 AudioClock::~AudioClock() {
26 void AudioClock::WroteAudio(int frames_written,
27 int frames_requested,
28 int delay_frames,
29 double playback_rate) {
30 DCHECK_GE(frames_written, 0);
31 DCHECK_LE(frames_written, frames_requested);
32 DCHECK_GE(delay_frames, 0);
33 DCHECK_GE(playback_rate, 0);
35 // First write: initialize buffer with silence.
36 if (start_timestamp_ == front_timestamp_ && buffered_.empty())
37 PushBufferedAudioData(delay_frames, 0.0);
39 // Move frames from |buffered_| into the computed timestamp based on
40 // |delay_frames|.
42 // The ordering of compute -> push -> pop eliminates unnecessary memory
43 // reallocations in cases where |buffered_| gets emptied.
44 int64_t frames_played =
45 std::max(INT64_C(0), total_buffered_frames_ - delay_frames);
46 PushBufferedAudioData(frames_written, playback_rate);
47 PushBufferedAudioData(frames_requested - frames_written, 0.0);
48 PopBufferedAudioData(frames_played);
50 // Update our front and back timestamps. The back timestamp is considered the
51 // authoritative source of truth, so base the front timestamp on range of data
52 // buffered. Doing so avoids accumulation errors on the front timestamp.
53 back_timestamp_ += base::TimeDelta::FromMicroseconds(
54 frames_written * playback_rate * microseconds_per_frame_);
55 // Don't let front timestamp move earlier in time, as could occur due to delay
56 // frames pushed in the first write, above.
57 front_timestamp_ = std::max(front_timestamp_,
58 back_timestamp_ - ComputeBufferedMediaDuration());
59 DCHECK_GE(front_timestamp_, start_timestamp_);
60 DCHECK_LE(front_timestamp_, back_timestamp_);
63 void AudioClock::CompensateForSuspendedWrites(base::TimeDelta elapsed,
64 int delay_frames) {
65 const int64_t frames_elapsed =
66 elapsed.InMicroseconds() / microseconds_per_frame_ + 0.5;
68 // No need to do anything if we're within the limits of our played out audio
69 // or there are no delay frames, the next WroteAudio() call will expire
70 // everything correctly.
71 if (frames_elapsed < total_buffered_frames_ || !delay_frames)
72 return;
74 // Otherwise, flush everything and prime with the delay frames.
75 WroteAudio(0, 0, 0, 0);
76 DCHECK(buffered_.empty());
77 PushBufferedAudioData(delay_frames, 0.0);
80 base::TimeDelta AudioClock::TimeUntilPlayback(base::TimeDelta timestamp) const {
81 DCHECK_GE(timestamp, front_timestamp_);
82 DCHECK_LE(timestamp, back_timestamp_);
84 int64_t frames_until_timestamp = 0;
85 double timestamp_us = timestamp.InMicroseconds();
86 double media_time_us = front_timestamp_.InMicroseconds();
88 for (size_t i = 0; i < buffered_.size(); ++i) {
89 // Leading silence is always accounted prior to anything else.
90 if (buffered_[i].playback_rate == 0) {
91 frames_until_timestamp += buffered_[i].frames;
92 continue;
95 // Calculate upper bound on media time for current block of buffered frames.
96 double delta_us = buffered_[i].frames * buffered_[i].playback_rate *
97 microseconds_per_frame_;
98 double max_media_time_us = media_time_us + delta_us;
100 // Determine amount of media time to convert to frames for current block. If
101 // target timestamp falls within current block, scale the amount of frames
102 // based on remaining amount of media time.
103 if (timestamp_us <= max_media_time_us) {
104 frames_until_timestamp +=
105 buffered_[i].frames * (timestamp_us - media_time_us) / delta_us;
106 break;
109 media_time_us = max_media_time_us;
110 frames_until_timestamp += buffered_[i].frames;
113 return base::TimeDelta::FromMicroseconds(frames_until_timestamp *
114 microseconds_per_frame_);
117 void AudioClock::ContiguousAudioDataBufferedForTesting(
118 base::TimeDelta* total,
119 base::TimeDelta* same_rate_total) const {
120 double scaled_frames = 0;
121 double scaled_frames_at_same_rate = 0;
122 bool found_silence = false;
123 for (size_t i = 0; i < buffered_.size(); ++i) {
124 if (buffered_[i].playback_rate == 0) {
125 found_silence = true;
126 continue;
129 // Any buffered silence breaks our contiguous stretch of audio data.
130 if (found_silence)
131 break;
133 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate);
135 if (i == 0)
136 scaled_frames_at_same_rate = scaled_frames;
139 *total = base::TimeDelta::FromMicroseconds(scaled_frames *
140 microseconds_per_frame_);
141 *same_rate_total = base::TimeDelta::FromMicroseconds(
142 scaled_frames_at_same_rate * microseconds_per_frame_);
145 AudioClock::AudioData::AudioData(int64_t frames, double playback_rate)
146 : frames(frames), playback_rate(playback_rate) {
149 void AudioClock::PushBufferedAudioData(int64_t frames, double playback_rate) {
150 if (frames == 0)
151 return;
153 total_buffered_frames_ += frames;
155 // Avoid creating extra elements where possible.
156 if (!buffered_.empty() && buffered_.back().playback_rate == playback_rate) {
157 buffered_.back().frames += frames;
158 return;
161 buffered_.push_back(AudioData(frames, playback_rate));
164 void AudioClock::PopBufferedAudioData(int64_t frames) {
165 DCHECK_LE(frames, total_buffered_frames_);
167 total_buffered_frames_ -= frames;
169 while (frames > 0) {
170 int64_t frames_to_pop = std::min(buffered_.front().frames, frames);
171 buffered_.front().frames -= frames_to_pop;
172 if (buffered_.front().frames == 0)
173 buffered_.pop_front();
175 frames -= frames_to_pop;
179 base::TimeDelta AudioClock::ComputeBufferedMediaDuration() const {
180 double scaled_frames = 0;
181 for (const auto& buffer : buffered_)
182 scaled_frames += buffer.frames * buffer.playback_rate;
183 return base::TimeDelta::FromMicroseconds(scaled_frames *
184 microseconds_per_frame_);
187 } // namespace media