Chromium Code Reviews| Index: media/base/audio_buffer_converter.cc |
| diff --git a/media/base/audio_buffer_converter.cc b/media/base/audio_buffer_converter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c67851d4e9e99fc06a22e2fb9ca704a604965ab |
| --- /dev/null |
| +++ b/media/base/audio_buffer_converter.cc |
| @@ -0,0 +1,199 @@ |
| +// Copyright 2014 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_buffer_converter.h" |
| + |
| +#include <cstdlib> |
| +#include <list> |
| + |
| +#include "base/logging.h" |
| +#include "media/base/audio_buffer.h" |
| +#include "media/base/audio_bus.h" |
| +#include "media/base/audio_decoder_config.h" |
| +#include "media/base/audio_timestamp_helper.h" |
| +#include "media/base/buffers.h" |
| +#include "media/base/vector_math.h" |
| + |
| +namespace media { |
| + |
| +AudioBufferConverter::AudioBufferConverter(const AudioParameters& output_params) |
| + : output_params_(output_params), |
| + offset_into_queue_(0), |
| + output_frames_(0), |
| + timestamp_helper_(output_params_.sample_rate()) { |
| + ResetConverter(output_params_); |
| +} |
| + |
| +AudioBufferConverter::~AudioBufferConverter() {} |
| + |
| +void AudioBufferConverter::AddInput( |
| + const scoped_refptr<AudioBuffer>& buffer) { |
| + |
| + if (buffer->end_of_stream()) { |
| + Flush(); |
| + queued_outputs_.push_back(buffer); |
| + return; |
| + } |
| + |
| + AudioParameters buffer_params = AudioBufferToAudioParameters(buffer); |
| + if (RequiresConverterReset(buffer_params)) |
|
DaleCurtis
2014/03/07 02:00:10
It'd be more readable to use names related to "Con
|
| + ResetConverter(buffer_params); |
| + |
| + if (timestamp_helper_.base_timestamp() == kNoTimestamp()) { |
| + timestamp_helper_.SetBaseTimestamp(buffer->timestamp()); |
| + } |
| + |
| + queued_inputs_.push_back(buffer); |
| + output_frames_ += floor(sample_rate_ratio_ * buffer->frame_count()); |
| + |
| + // Only proceed if we have enough data to produce a full output buffer. |
| + while(output_frames_ >= output_params_.frames_per_buffer() * 2) { |
|
rileya (GONE FROM CHROMIUM)
2014/03/07 01:19:29
I'm not quite sure here. Since the SincResampler d
DaleCurtis
2014/03/07 02:00:10
As discussed offline, I think instead you want to
|
| + scoped_refptr<AudioBuffer> output_buffer = Convert(); |
| + DCHECK(output_buffer); |
| + queued_outputs_.push_back(output_buffer); |
| + } |
| +} |
| + |
| +bool AudioBufferConverter::HasNextBuffer() { |
| + return !queued_outputs_.empty(); |
| +} |
| + |
| +scoped_refptr<AudioBuffer> AudioBufferConverter::GetNextBuffer() { |
| + DCHECK(!queued_outputs_.empty()); |
| + scoped_refptr<AudioBuffer> out = queued_outputs_.front(); |
| + queued_outputs_.pop_front(); |
| + return out; |
| +} |
| + |
| +double AudioBufferConverter::ProvideInput(AudioBus* audio_bus, |
| + base::TimeDelta buffer_delay) { |
| + DCHECK(is_flushing_ || output_frames_ >= audio_bus->frames()); |
| + |
| + int requested_frames_left = audio_bus->frames(); |
| + int dest_index = 0; |
| + |
| + while (requested_frames_left > 0 && !queued_inputs_.empty()) { |
| + scoped_refptr<AudioBuffer> input_buffer = queued_inputs_.front(); |
| + int frames_to_read = requested_frames_left; |
| + |
| + if (input_buffer->frame_count() - offset_into_queue_ <= |
| + requested_frames_left) { |
| + frames_to_read = input_buffer->frame_count() - offset_into_queue_; |
| + queued_inputs_.pop_front(); |
| + input_buffer->ReadFrames( |
| + frames_to_read, offset_into_queue_, dest_index, audio_bus); |
| + offset_into_queue_ = 0; |
| + } else { |
| + input_buffer->ReadFrames( |
| + frames_to_read, offset_into_queue_, dest_index, audio_bus); |
| + offset_into_queue_ += frames_to_read; |
| + } |
| + |
| + requested_frames_left -= frames_to_read; |
| + dest_index += frames_to_read; |
| + } |
| + |
| + // Unless we're flushing we should always have enough data to satsify the |
| + // request. |
| + if (!is_flushing_) |
| + DCHECK_EQ(requested_frames_left, 0); |
| + |
| + // Assume full volume (is this correct?) |
| + return 1.0; |
| +} |
| + |
| +void AudioBufferConverter::ResetConverter(const AudioParameters& input_params) { |
| + Flush(); |
| + output_frames_ = 0; |
| + offset_into_queue_ = 0; |
| + queued_inputs_.clear(); |
| + input_params_ = input_params; |
| + audio_converter_.reset( |
| + new AudioConverter(input_params_, output_params_, true)); |
| + sample_rate_ratio_ = static_cast<double>(output_params_.sample_rate()) / |
| + input_params_.sample_rate(); |
| + audio_converter_->AddInput(this); |
| +} |
| + |
| +AudioParameters AudioBufferConverter::AudioBufferToAudioParameters( |
| + const scoped_refptr<AudioBuffer>& buffer) { |
| + return AudioParameters( |
| + AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| + buffer->channel_layout(), |
| + buffer->sample_rate(), |
| + SampleFormatToBytesPerChannel(buffer->sample_format()) * 8, |
| + buffer->frame_count()); |
| +} |
| + |
| +scoped_refptr<AudioBuffer> AudioBufferConverter::Convert() { |
| + if (!output_frames_) |
| + return NULL; |
| + |
| + scoped_refptr<AudioBuffer> output_buffer = |
|
DaleCurtis
2014/03/07 02:00:10
You should avoid conversion if possible.
|
| + AudioBuffer::CreateBuffer(kSampleFormatPlanarF32, |
| + output_params_.channel_layout(), |
| + output_params_.sample_rate(), |
| + output_params_.frames_per_buffer()); |
| + |
| + // If there's not enough data in the Converter for a full buffer, we need to |
| + // know how much of the output we actually want. |
| + int frames = output_params_.frames_per_buffer() > output_frames_ |
| + ? output_frames_ |
| + : output_params_.frames_per_buffer(); |
| + |
| + // Wrap it in an AudioBus so the AudioConverter can fill it. |
| + scoped_ptr<AudioBus> output_bus = |
| + AudioBus::CreateWrapper(output_buffer->channel_count()); |
| + output_bus->set_frames(output_buffer->frame_count()); |
| + for (int ch = 0; ch < output_buffer->channel_count(); ++ch) { |
| + output_bus->SetChannelData( |
| + ch, reinterpret_cast<float*>(output_buffer->channel_data()[ch])); |
| + } |
| + |
| + // Do the actual conversion. |
| + audio_converter_->Convert(output_bus.get()); |
| + output_frames_ -= output_params_.frames_per_buffer(); |
| + |
| + // If we have a partial buffer, copy only the frames we want into a new |
| + // buffer of the appropriate size. |
| + if (frames < output_params_.frames_per_buffer()) { |
|
rileya (GONE FROM CHROMIUM)
2014/03/07 01:19:29
This is kinda ugly...
|
| + output_buffer = |
| + AudioBuffer::CopyFrom(kSampleFormatPlanarF32, |
| + output_params_.channel_layout(), |
| + output_params_.sample_rate(), |
| + frames, |
| + &output_buffer->channel_data()[0], |
| + kNoTimestamp(), |
| + kNoTimestamp()); |
| + } |
| + |
| + // Compute the timestamp. |
| + output_buffer->set_timestamp(timestamp_helper_.GetTimestamp()); |
| + output_buffer->set_duration( |
| + timestamp_helper_.GetFrameDuration(output_params_.frames_per_buffer())); |
| + timestamp_helper_.AddFrames(frames); |
| + |
| + return output_buffer; |
| +} |
| + |
| +void AudioBufferConverter::Flush() { |
| + while (output_frames_ > 0) { |
| + scoped_refptr<AudioBuffer> output_buffer = Convert(); |
| + DCHECK(output_buffer); |
| + queued_outputs_.push_back(output_buffer); |
| + } |
| +} |
| + |
| +bool AudioBufferConverter::RequiresConverterReset( |
| + const AudioParameters& new_params) { |
| + // If frames_per_buffer() varies, there's no need to reset. |
| + return new_params.format() != input_params_.format() || |
| + new_params.sample_rate() != input_params_.sample_rate() || |
| + new_params.bits_per_sample() != input_params_.bits_per_sample() || |
| + new_params.channels() != input_params_.channels() || |
| + new_params.channel_layout() != input_params_.channels() || |
| + new_params.effects() != input_params_.effects(); |
| +} |
| + |
| +} // namespace media |