| Index: media/base/audio_rechunker.cc
|
| diff --git a/media/base/audio_rechunker.cc b/media/base/audio_rechunker.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..72326028db69535f299b1dc16c0c55aefe3b585d
|
| --- /dev/null
|
| +++ b/media/base/audio_rechunker.cc
|
| @@ -0,0 +1,80 @@
|
| +// Copyright 2016 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/audio_rechunker.h"
|
| +
|
| +namespace media {
|
| +
|
| +AudioRechunker::AudioRechunker(base::TimeDelta output_duration,
|
| + const RechunkedAudioCallback& callback)
|
| + : output_duration_(output_duration), callback_(callback),
|
| + sample_rate_(0), output_frames_(0) {
|
| + DCHECK_GT(output_duration_, base::TimeDelta());
|
| + DCHECK(!callback_.is_null());
|
| +}
|
| +
|
| +AudioRechunker::~AudioRechunker() {}
|
| +
|
| +bool AudioRechunker::SetSampleRate(int sample_rate) {
|
| + DCHECK_GT(sample_rate, 0);
|
| +
|
| + sample_rate_ = sample_rate;
|
| + audio_queue_.reset();
|
| + pending_frames_= 0;
|
| +
|
| + const int64_t numerator = sample_rate * output_duration_.InMicroseconds();
|
| + output_frames_ = numerator / base::Time::kMicrosecondsPerSecond;
|
| + DCHECK_GT(output_frames_, 0);
|
| + return ((numerator % base::Time::kMicrosecondsPerSecond) == 0);
|
| +}
|
| +
|
| +void AudioRechunker::Push(const AudioBus& input_bus,
|
| + base::TimeDelta reference_timestamp) {
|
| + DCHECK_GT(output_frames_, 0);
|
| +
|
| + // Fast path: No buffering required.
|
| + if ((pending_frames_ == 0) && (input_bus.frames() == output_frames_)) {
|
| + callback_.Run(input_bus, reference_timestamp);
|
| + return;
|
| + }
|
| +
|
| + // Lazy-create the |audio_queue_| if needed.
|
| + if (!audio_queue_ || audio_queue_->channels() != input_bus.channels())
|
| + audio_queue_ = AudioBus::Create(input_bus.channels(), output_frames_);
|
| +
|
| + // Adjust |reference_timestamp| to be that of the first sample in
|
| + // |audio_queue_| since that will be the first sample delivered via the
|
| + // |callback_|.
|
| + reference_timestamp -= base::TimeDelta::FromMicroseconds(
|
| + pending_frames_ * base::Time::kMicrosecondsPerSecond / sample_rate_);
|
| +
|
| + // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus|
|
| + // and deliver batches until all sample frames in |input_bus| have been
|
| + // consumed.
|
| + int source_pos = 0;
|
| + do {
|
| + // Attempt to fill |audio_queue_| completely.
|
| + const int frames_to_push =
|
| + std::min(static_cast<int>(input_bus.frames() - source_pos),
|
| + output_frames_ - pending_frames_);
|
| + if (frames_to_push > 0) {
|
| + DVLOG(2) << "Enqueuing " << frames_to_push << " frames.";
|
| + input_bus.CopyPartialFramesTo(source_pos, frames_to_push, pending_frames_,
|
| + audio_queue_.get());
|
| + pending_frames_ += frames_to_push;
|
| + source_pos += frames_to_push;
|
| + }
|
| +
|
| + // If |audio_queue_| has been filled completely, deliver the re-chunked
|
| + // audio to the consumer.
|
| + if (pending_frames_ == output_frames_) {
|
| + DVLOG(2) << "Delivering another " << pending_frames_ << " frames.";
|
| + callback_.Run(*audio_queue_, reference_timestamp);
|
| + reference_timestamp += output_duration_;
|
| + pending_frames_ = 0;
|
| + }
|
| + } while (source_pos < input_bus.frames());
|
| +}
|
| +
|
| +} // namespace media
|
|
|