[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / media / filters / audio_clock.cc
blobb1dc4beba64eaed61a51082767f5c1be92c3bfbc
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"
10 #include "media/base/buffers.h"
12 namespace media {
14 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate)
15 : start_timestamp_(start_timestamp),
16 microseconds_per_frame_(
17 static_cast<double>(base::Time::kMicrosecondsPerSecond) /
18 sample_rate),
19 total_buffered_frames_(0),
20 front_timestamp_(start_timestamp),
21 back_timestamp_(start_timestamp) {
24 AudioClock::~AudioClock() {
27 void AudioClock::WroteAudio(int frames_written,
28 int frames_requested,
29 int delay_frames,
30 double playback_rate) {
31 DCHECK_GE(frames_written, 0);
32 DCHECK_LE(frames_written, frames_requested);
33 DCHECK_GE(delay_frames, 0);
34 DCHECK_GE(playback_rate, 0);
36 // First write: initialize buffer with silence.
37 if (start_timestamp_ == front_timestamp_ && buffered_.empty())
38 PushBufferedAudioData(delay_frames, 0.0);
40 // Move frames from |buffered_| into the computed timestamp based on
41 // |delay_frames|.
43 // The ordering of compute -> push -> pop eliminates unnecessary memory
44 // reallocations in cases where |buffered_| gets emptied.
45 int64_t frames_played =
46 std::max(INT64_C(0), total_buffered_frames_ - delay_frames);
47 PushBufferedAudioData(frames_written, playback_rate);
48 PushBufferedAudioData(frames_requested - frames_written, 0.0);
49 PopBufferedAudioData(frames_played);
51 // Update our front and back timestamps. The back timestamp is considered the
52 // authoritative source of truth, so base the front timestamp on range of data
53 // buffered. Doing so avoids accumulation errors on the front timestamp.
54 back_timestamp_ += base::TimeDelta::FromMicroseconds(
55 frames_written * playback_rate * microseconds_per_frame_);
56 front_timestamp_ = back_timestamp_ - ComputeBufferedMediaDuration();
57 DCHECK_LE(front_timestamp_, back_timestamp_);
60 void AudioClock::CompensateForSuspendedWrites(base::TimeDelta elapsed,
61 int delay_frames) {
62 const int64_t frames_elapsed =
63 elapsed.InMicroseconds() / microseconds_per_frame_ + 0.5;
65 // No need to do anything if we're within the limits of our played out audio
66 // or there are no delay frames, the next WroteAudio() call will expire
67 // everything correctly.
68 if (frames_elapsed < total_buffered_frames_ || !delay_frames)
69 return;
71 // Otherwise, flush everything and prime with the delay frames.
72 WroteAudio(0, 0, 0, 0);
73 DCHECK(buffered_.empty());
74 PushBufferedAudioData(delay_frames, 0.0);
77 base::TimeDelta AudioClock::TimeUntilPlayback(base::TimeDelta timestamp) const {
78 DCHECK_GE(timestamp, front_timestamp_);
79 DCHECK_LE(timestamp, back_timestamp_);
81 int64_t frames_until_timestamp = 0;
82 double timestamp_us = timestamp.InMicroseconds();
83 double media_time_us = front_timestamp_.InMicroseconds();
85 for (size_t i = 0; i < buffered_.size(); ++i) {
86 // Leading silence is always accounted prior to anything else.
87 if (buffered_[i].playback_rate == 0) {
88 frames_until_timestamp += buffered_[i].frames;
89 continue;
92 // Calculate upper bound on media time for current block of buffered frames.
93 double delta_us = buffered_[i].frames * buffered_[i].playback_rate *
94 microseconds_per_frame_;
95 double max_media_time_us = media_time_us + delta_us;
97 // Determine amount of media time to convert to frames for current block. If
98 // target timestamp falls within current block, scale the amount of frames
99 // based on remaining amount of media time.
100 if (timestamp_us <= max_media_time_us) {
101 frames_until_timestamp +=
102 buffered_[i].frames * (timestamp_us - media_time_us) / delta_us;
103 break;
106 media_time_us = max_media_time_us;
107 frames_until_timestamp += buffered_[i].frames;
110 return base::TimeDelta::FromMicroseconds(frames_until_timestamp *
111 microseconds_per_frame_);
114 void AudioClock::ContiguousAudioDataBufferedForTesting(
115 base::TimeDelta* total,
116 base::TimeDelta* same_rate_total) const {
117 double scaled_frames = 0;
118 double scaled_frames_at_same_rate = 0;
119 bool found_silence = false;
120 for (size_t i = 0; i < buffered_.size(); ++i) {
121 if (buffered_[i].playback_rate == 0) {
122 found_silence = true;
123 continue;
126 // Any buffered silence breaks our contiguous stretch of audio data.
127 if (found_silence)
128 break;
130 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate);
132 if (i == 0)
133 scaled_frames_at_same_rate = scaled_frames;
136 *total = base::TimeDelta::FromMicroseconds(scaled_frames *
137 microseconds_per_frame_);
138 *same_rate_total = base::TimeDelta::FromMicroseconds(
139 scaled_frames_at_same_rate * microseconds_per_frame_);
142 AudioClock::AudioData::AudioData(int64_t frames, double playback_rate)
143 : frames(frames), playback_rate(playback_rate) {
146 void AudioClock::PushBufferedAudioData(int64_t frames, double playback_rate) {
147 if (frames == 0)
148 return;
150 total_buffered_frames_ += frames;
152 // Avoid creating extra elements where possible.
153 if (!buffered_.empty() && buffered_.back().playback_rate == playback_rate) {
154 buffered_.back().frames += frames;
155 return;
158 buffered_.push_back(AudioData(frames, playback_rate));
161 void AudioClock::PopBufferedAudioData(int64_t frames) {
162 DCHECK_LE(frames, total_buffered_frames_);
164 total_buffered_frames_ -= frames;
166 while (frames > 0) {
167 int64_t frames_to_pop = std::min(buffered_.front().frames, frames);
168 buffered_.front().frames -= frames_to_pop;
169 if (buffered_.front().frames == 0)
170 buffered_.pop_front();
172 frames -= frames_to_pop;
176 base::TimeDelta AudioClock::ComputeBufferedMediaDuration() const {
177 double scaled_frames = 0;
178 for (const auto& buffer : buffered_)
179 scaled_frames += buffer.frames * buffer.playback_rate;
180 return base::TimeDelta::FromMicroseconds(scaled_frames *
181 microseconds_per_frame_);
184 } // namespace media