Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/base/audio_buffer_converter.h" | |
| 6 | |
| 7 #include <cstdlib> | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "media/base/audio_buffer.h" | |
| 12 #include "media/base/audio_bus.h" | |
| 13 #include "media/base/audio_decoder_config.h" | |
| 14 #include "media/base/audio_timestamp_helper.h" | |
| 15 #include "media/base/buffers.h" | |
| 16 #include "media/base/vector_math.h" | |
| 17 | |
| 18 namespace media { | |
| 19 | |
| 20 AudioBufferConverter::AudioBufferConverter(const AudioParameters& output_params) | |
| 21 : output_params_(output_params), | |
| 22 offset_into_queue_(0), | |
| 23 output_frames_(0), | |
| 24 timestamp_helper_(output_params_.sample_rate()) { | |
| 25 ResetConverter(output_params_); | |
| 26 } | |
| 27 | |
| 28 AudioBufferConverter::~AudioBufferConverter() {} | |
| 29 | |
| 30 void AudioBufferConverter::AddInput( | |
| 31 const scoped_refptr<AudioBuffer>& buffer) { | |
| 32 | |
| 33 if (buffer->end_of_stream()) { | |
| 34 Flush(); | |
| 35 queued_outputs_.push_back(buffer); | |
| 36 return; | |
| 37 } | |
| 38 | |
| 39 AudioParameters buffer_params = AudioBufferToAudioParameters(buffer); | |
| 40 if (RequiresConverterReset(buffer_params)) | |
|
DaleCurtis
2014/03/07 02:00:10
It'd be more readable to use names related to "Con
| |
| 41 ResetConverter(buffer_params); | |
| 42 | |
| 43 if (timestamp_helper_.base_timestamp() == kNoTimestamp()) { | |
| 44 timestamp_helper_.SetBaseTimestamp(buffer->timestamp()); | |
| 45 } | |
| 46 | |
| 47 queued_inputs_.push_back(buffer); | |
| 48 output_frames_ += floor(sample_rate_ratio_ * buffer->frame_count()); | |
| 49 | |
| 50 // Only proceed if we have enough data to produce a full output buffer. | |
| 51 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
| |
| 52 scoped_refptr<AudioBuffer> output_buffer = Convert(); | |
| 53 DCHECK(output_buffer); | |
| 54 queued_outputs_.push_back(output_buffer); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 bool AudioBufferConverter::HasNextBuffer() { | |
| 59 return !queued_outputs_.empty(); | |
| 60 } | |
| 61 | |
| 62 scoped_refptr<AudioBuffer> AudioBufferConverter::GetNextBuffer() { | |
| 63 DCHECK(!queued_outputs_.empty()); | |
| 64 scoped_refptr<AudioBuffer> out = queued_outputs_.front(); | |
| 65 queued_outputs_.pop_front(); | |
| 66 return out; | |
| 67 } | |
| 68 | |
| 69 double AudioBufferConverter::ProvideInput(AudioBus* audio_bus, | |
| 70 base::TimeDelta buffer_delay) { | |
| 71 DCHECK(is_flushing_ || output_frames_ >= audio_bus->frames()); | |
| 72 | |
| 73 int requested_frames_left = audio_bus->frames(); | |
| 74 int dest_index = 0; | |
| 75 | |
| 76 while (requested_frames_left > 0 && !queued_inputs_.empty()) { | |
| 77 scoped_refptr<AudioBuffer> input_buffer = queued_inputs_.front(); | |
| 78 int frames_to_read = requested_frames_left; | |
| 79 | |
| 80 if (input_buffer->frame_count() - offset_into_queue_ <= | |
| 81 requested_frames_left) { | |
| 82 frames_to_read = input_buffer->frame_count() - offset_into_queue_; | |
| 83 queued_inputs_.pop_front(); | |
| 84 input_buffer->ReadFrames( | |
| 85 frames_to_read, offset_into_queue_, dest_index, audio_bus); | |
| 86 offset_into_queue_ = 0; | |
| 87 } else { | |
| 88 input_buffer->ReadFrames( | |
| 89 frames_to_read, offset_into_queue_, dest_index, audio_bus); | |
| 90 offset_into_queue_ += frames_to_read; | |
| 91 } | |
| 92 | |
| 93 requested_frames_left -= frames_to_read; | |
| 94 dest_index += frames_to_read; | |
| 95 } | |
| 96 | |
| 97 // Unless we're flushing we should always have enough data to satsify the | |
| 98 // request. | |
| 99 if (!is_flushing_) | |
| 100 DCHECK_EQ(requested_frames_left, 0); | |
| 101 | |
| 102 // Assume full volume (is this correct?) | |
| 103 return 1.0; | |
| 104 } | |
| 105 | |
| 106 void AudioBufferConverter::ResetConverter(const AudioParameters& input_params) { | |
| 107 Flush(); | |
| 108 output_frames_ = 0; | |
| 109 offset_into_queue_ = 0; | |
| 110 queued_inputs_.clear(); | |
| 111 input_params_ = input_params; | |
| 112 audio_converter_.reset( | |
| 113 new AudioConverter(input_params_, output_params_, true)); | |
| 114 sample_rate_ratio_ = static_cast<double>(output_params_.sample_rate()) / | |
| 115 input_params_.sample_rate(); | |
| 116 audio_converter_->AddInput(this); | |
| 117 } | |
| 118 | |
| 119 AudioParameters AudioBufferConverter::AudioBufferToAudioParameters( | |
| 120 const scoped_refptr<AudioBuffer>& buffer) { | |
| 121 return AudioParameters( | |
| 122 AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 123 buffer->channel_layout(), | |
| 124 buffer->sample_rate(), | |
| 125 SampleFormatToBytesPerChannel(buffer->sample_format()) * 8, | |
| 126 buffer->frame_count()); | |
| 127 } | |
| 128 | |
| 129 scoped_refptr<AudioBuffer> AudioBufferConverter::Convert() { | |
| 130 if (!output_frames_) | |
| 131 return NULL; | |
| 132 | |
| 133 scoped_refptr<AudioBuffer> output_buffer = | |
|
DaleCurtis
2014/03/07 02:00:10
You should avoid conversion if possible.
| |
| 134 AudioBuffer::CreateBuffer(kSampleFormatPlanarF32, | |
| 135 output_params_.channel_layout(), | |
| 136 output_params_.sample_rate(), | |
| 137 output_params_.frames_per_buffer()); | |
| 138 | |
| 139 // If there's not enough data in the Converter for a full buffer, we need to | |
| 140 // know how much of the output we actually want. | |
| 141 int frames = output_params_.frames_per_buffer() > output_frames_ | |
| 142 ? output_frames_ | |
| 143 : output_params_.frames_per_buffer(); | |
| 144 | |
| 145 // Wrap it in an AudioBus so the AudioConverter can fill it. | |
| 146 scoped_ptr<AudioBus> output_bus = | |
| 147 AudioBus::CreateWrapper(output_buffer->channel_count()); | |
| 148 output_bus->set_frames(output_buffer->frame_count()); | |
| 149 for (int ch = 0; ch < output_buffer->channel_count(); ++ch) { | |
| 150 output_bus->SetChannelData( | |
| 151 ch, reinterpret_cast<float*>(output_buffer->channel_data()[ch])); | |
| 152 } | |
| 153 | |
| 154 // Do the actual conversion. | |
| 155 audio_converter_->Convert(output_bus.get()); | |
| 156 output_frames_ -= output_params_.frames_per_buffer(); | |
| 157 | |
| 158 // If we have a partial buffer, copy only the frames we want into a new | |
| 159 // buffer of the appropriate size. | |
| 160 if (frames < output_params_.frames_per_buffer()) { | |
|
rileya (GONE FROM CHROMIUM)
2014/03/07 01:19:29
This is kinda ugly...
| |
| 161 output_buffer = | |
| 162 AudioBuffer::CopyFrom(kSampleFormatPlanarF32, | |
| 163 output_params_.channel_layout(), | |
| 164 output_params_.sample_rate(), | |
| 165 frames, | |
| 166 &output_buffer->channel_data()[0], | |
| 167 kNoTimestamp(), | |
| 168 kNoTimestamp()); | |
| 169 } | |
| 170 | |
| 171 // Compute the timestamp. | |
| 172 output_buffer->set_timestamp(timestamp_helper_.GetTimestamp()); | |
| 173 output_buffer->set_duration( | |
| 174 timestamp_helper_.GetFrameDuration(output_params_.frames_per_buffer())); | |
| 175 timestamp_helper_.AddFrames(frames); | |
| 176 | |
| 177 return output_buffer; | |
| 178 } | |
| 179 | |
| 180 void AudioBufferConverter::Flush() { | |
| 181 while (output_frames_ > 0) { | |
| 182 scoped_refptr<AudioBuffer> output_buffer = Convert(); | |
| 183 DCHECK(output_buffer); | |
| 184 queued_outputs_.push_back(output_buffer); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 bool AudioBufferConverter::RequiresConverterReset( | |
| 189 const AudioParameters& new_params) { | |
| 190 // If frames_per_buffer() varies, there's no need to reset. | |
| 191 return new_params.format() != input_params_.format() || | |
| 192 new_params.sample_rate() != input_params_.sample_rate() || | |
| 193 new_params.bits_per_sample() != input_params_.bits_per_sample() || | |
| 194 new_params.channels() != input_params_.channels() || | |
| 195 new_params.channel_layout() != input_params_.channels() || | |
| 196 new_params.effects() != input_params_.effects(); | |
| 197 } | |
| 198 | |
| 199 } // namespace media | |
| OLD | NEW |