Chromium Code Reviews| Index: media/filters/audio_clock.cc |
| diff --git a/media/filters/audio_clock.cc b/media/filters/audio_clock.cc |
| index e315fa31e2d7084218136c5266461fa23ec92f90..65015ed41fbd389ec91236ddc9c6dcad8d8f314c 100644 |
| --- a/media/filters/audio_clock.cc |
| +++ b/media/filters/audio_clock.cc |
| @@ -4,144 +4,136 @@ |
| #include "media/filters/audio_clock.h" |
| +#include <algorithm> |
| + |
| #include "base/logging.h" |
| #include "media/base/buffers.h" |
| namespace media { |
| -AudioClock::AudioClock(int sample_rate) |
| - : sample_rate_(sample_rate), last_endpoint_timestamp_(kNoTimestamp()) { |
| +AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
| + : start_timestamp_(start_timestamp), |
| + sample_rate_(sample_rate), |
| + total_buffered_frames_(0), |
| + audio_data_buffered_(0), |
| + current_media_timestamp_(start_timestamp) { |
| } |
| AudioClock::~AudioClock() { |
| } |
| -void AudioClock::WroteAudio(int frames, |
| +void AudioClock::WroteAudio(int frames_written, |
| + int frames_requested, |
| int delay_frames, |
| - float playback_rate, |
| - base::TimeDelta timestamp) { |
| - CHECK_GT(playback_rate, 0); |
| - CHECK(timestamp != kNoTimestamp()); |
| - DCHECK_GE(frames, 0); |
| + float playback_rate) { |
| + DCHECK_GE(frames_written, 0); |
| + DCHECK_LE(frames_written, frames_requested); |
| DCHECK_GE(delay_frames, 0); |
| + DCHECK_GE(playback_rate, 0); |
| - if (last_endpoint_timestamp_ == kNoTimestamp()) |
| - PushBufferedAudio(delay_frames, 0, kNoTimestamp()); |
| + // First write: initialize buffer with silence. |
| + if (buffered_.empty()) { |
| + PushAudioData(&buffered_, delay_frames, 0.0f); |
| + total_buffered_frames_ = delay_frames; |
| + } |
| - TrimBufferedAudioToMatchDelay(delay_frames); |
| - PushBufferedAudio(frames, playback_rate, timestamp); |
| + // Move frames from |buffered_| to |played_| based on |delay_frames|. |
| + int64_t played_frames = |
| + std::max(INT64_C(0), total_buffered_frames_ - delay_frames); |
| + while (played_frames > 0) { |
| + int64_t frames_to_move = std::min(buffered_.front().frames, played_frames); |
| - last_endpoint_timestamp_ = timestamp; |
| -} |
| + // No need to keep track of silent audio. |
| + if (buffered_.front().playback_rate > 0.0f) |
| + PushAudioData(&played_, frames_to_move, buffered_.front().playback_rate); |
| -void AudioClock::WroteSilence(int frames, int delay_frames) { |
| - DCHECK_GE(frames, 0); |
| - DCHECK_GE(delay_frames, 0); |
| + buffered_.front().frames -= frames_to_move; |
| + if (buffered_.front().frames == 0) |
| + buffered_.pop_front(); |
| - if (last_endpoint_timestamp_ == kNoTimestamp()) |
| - PushBufferedAudio(delay_frames, 0, kNoTimestamp()); |
| + played_frames -= frames_to_move; |
| + } |
| - TrimBufferedAudioToMatchDelay(delay_frames); |
| - PushBufferedAudio(frames, 0, kNoTimestamp()); |
| -} |
| + // Push in newly buffered data. |
| + PushAudioData(&buffered_, frames_written, playback_rate); |
| + PushAudioData(&buffered_, frames_requested - frames_written, 0.0f); |
| + |
| + // Update cached values. |
|
DaleCurtis
2014/08/04 18:55:19
Hmm, I didn't strictly mean a cache. I.e. the tot
scherkus (not reviewing)
2014/08/05 00:55:00
Reorganized code and helper methods.
|
| + total_buffered_frames_ = 0; |
| + audio_data_buffered_ = false; |
| + for (size_t i = 0; i < buffered_.size(); ++i) { |
| + total_buffered_frames_ += buffered_[i].frames; |
| + if (buffered_[i].playback_rate > 0.0f) |
| + audio_data_buffered_ = true; |
| + } |
| -base::TimeDelta AudioClock::CurrentMediaTimestamp( |
| - base::TimeDelta time_since_writing) const { |
| - int frames_to_skip = |
| - static_cast<int>(time_since_writing.InSecondsF() * sample_rate_); |
| - int silence_frames = 0; |
| - for (size_t i = 0; i < buffered_audio_.size(); ++i) { |
| - int frames = buffered_audio_[i].frames; |
| - if (frames_to_skip > 0) { |
| - if (frames <= frames_to_skip) { |
| - frames_to_skip -= frames; |
| - continue; |
| - } |
| - frames -= frames_to_skip; |
| - frames_to_skip = 0; |
| - } |
| - |
| - // Account for silence ahead of the buffer closest to being played. |
| - if (buffered_audio_[i].playback_rate == 0) { |
| - silence_frames += frames; |
| - continue; |
| - } |
| - |
| - // Multiply by playback rate as frames represent time-scaled audio. |
| - return buffered_audio_[i].endpoint_timestamp - |
| - base::TimeDelta::FromMicroseconds( |
| - ((frames * buffered_audio_[i].playback_rate) + silence_frames) / |
| - sample_rate_ * base::Time::kMicrosecondsPerSecond); |
| + double scaled_frames = 0; |
| + for (size_t i = 0; i < played_.size(); ++i) { |
| + DCHECK_NE(played_[i].playback_rate, 0.0f) |
| + << "Silent audio doesn't need to be tracked in |played_|."; |
| + scaled_frames += played_[i].frames * played_[i].playback_rate; |
| + } |
| + current_media_timestamp_ = |
|
DaleCurtis
2014/08/04 18:55:19
Again, I didn't mean as a strict cache, but a roll
scherkus (not reviewing)
2014/08/05 00:55:00
Ditto.
|
| + start_timestamp_ + |
| + base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); |
| + |
| + scaled_frames = 0; |
| + for (size_t i = 0; i < buffered_.size(); ++i) { |
| + // Any buffered silence breaks our contiguous stretch of audio data. |
| + if (buffered_[i].playback_rate == 0) |
| + break; |
| + scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); |
| } |
| + contiguous_audio_data_buffered_ = |
| + base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); |
| - // Either: |
| - // 1) AudioClock is uninitialziated and we'll return kNoTimestamp() |
| - // 2) All previously buffered audio has been replaced by silence, |
| - // meaning media time is now at the last endpoint |
| - return last_endpoint_timestamp_; |
| + scaled_frames = 0; |
|
DaleCurtis
2014/08/04 18:55:19
If you calculate this first you can avoid the for
scherkus (not reviewing)
2014/08/05 00:55:00
Ditto.
|
| + if (buffered_.front().playback_rate > 0.0f) { |
| + scaled_frames = buffered_.front().frames * buffered_.front().playback_rate; |
| + } |
| + contiguous_audio_data_buffered_at_same_rate_ = |
| + base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); |
| } |
| -void AudioClock::TrimBufferedAudioToMatchDelay(int delay_frames) { |
| - if (buffered_audio_.empty()) |
| - return; |
| - |
| - size_t i = buffered_audio_.size() - 1; |
| - while (true) { |
| - if (buffered_audio_[i].frames <= delay_frames) { |
| - // Reached the end before accounting for all of |delay_frames|. This |
| - // means we haven't written enough audio data yet to account for hardware |
| - // delay. In this case, do nothing. |
| - if (i == 0) |
| - return; |
| - |
| - // Keep accounting for |delay_frames|. |
| - delay_frames -= buffered_audio_[i].frames; |
| - --i; |
| - continue; |
| - } |
| - |
| - // All of |delay_frames| has been accounted for: adjust amount of frames |
| - // left in current buffer. All preceeding elements with index < |i| should |
| - // be considered played out and hence discarded. |
| - buffered_audio_[i].frames = delay_frames; |
| - break; |
| +base::TimeDelta AudioClock::CurrentMediaTimestampSinceWriting( |
| + base::TimeDelta time_since_writing) const { |
| + base::TimeDelta computed_timestamp = current_media_timestamp_; |
| + |
| + // Count up all |buffered_| audio based on |time_since_writing|. |
| + int64_t frames_played_since_writing = |
| + static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_); |
| + for (size_t i = 0; i < buffered_.size() && frames_played_since_writing > 0; |
| + ++i) { |
| + int64_t frames_played = |
| + std::min(buffered_[i].frames, frames_played_since_writing); |
| + computed_timestamp += base::TimeDelta::FromMicroseconds( |
| + (frames_played * buffered_[i].playback_rate) / sample_rate_ * |
| + base::Time::kMicrosecondsPerSecond); |
| + frames_played_since_writing -= frames_played; |
| } |
| - // At this point |i| points at what will be the new head of |buffered_audio_| |
| - // however if it contains no audio it should be removed as well. |
| - if (buffered_audio_[i].frames == 0) |
| - ++i; |
| - |
| - buffered_audio_.erase(buffered_audio_.begin(), buffered_audio_.begin() + i); |
| + return computed_timestamp; |
| } |
| -void AudioClock::PushBufferedAudio(int frames, |
| - float playback_rate, |
| - base::TimeDelta endpoint_timestamp) { |
| - if (playback_rate == 0) |
| - DCHECK(endpoint_timestamp == kNoTimestamp()); |
| +AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) |
| + : frames(frames), playback_rate(playback_rate) { |
| +} |
| +// static |
| +void AudioClock::PushAudioData(std::deque<AudioData>* audio_data, |
| + int64_t frames, |
| + float playback_rate) { |
| if (frames == 0) |
| return; |
| // Avoid creating extra elements where possible. |
| - if (!buffered_audio_.empty() && |
| - buffered_audio_.back().playback_rate == playback_rate) { |
| - buffered_audio_.back().frames += frames; |
| - buffered_audio_.back().endpoint_timestamp = endpoint_timestamp; |
| + if (!audio_data->empty() && |
| + audio_data->back().playback_rate == playback_rate) { |
| + audio_data->back().frames += frames; |
| return; |
| } |
| - buffered_audio_.push_back( |
| - BufferedAudio(frames, playback_rate, endpoint_timestamp)); |
| -} |
| - |
| -AudioClock::BufferedAudio::BufferedAudio(int frames, |
| - float playback_rate, |
| - base::TimeDelta endpoint_timestamp) |
| - : frames(frames), |
| - playback_rate(playback_rate), |
| - endpoint_timestamp(endpoint_timestamp) { |
| + audio_data->push_back(AudioData(frames, playback_rate)); |
| } |
| } // namespace media |