Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1103)

Side by Side Diff: media/base/audio_buffer_converter.cc

Issue 177333003: Add support for midstream audio configuration changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ABS
Patch Set: address comments Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/sinc_resampler.h"
17 #include "media/base/vector_math.h"
18
19 namespace media {
20
21 // Is the config presented by |buffer| a config change from |params|?
22 static bool IsConfigChange(const AudioParameters& params,
23 const scoped_refptr<AudioBuffer>& buffer) {
24 return buffer->sample_rate() != params.sample_rate() ||
25 buffer->channel_count() != params.channels() ||
26 buffer->channel_layout() != params.channel_layout();
27 }
28
29 AudioBufferConverter::AudioBufferConverter(const AudioParameters& output_params)
30 : output_params_(output_params),
31 input_params_(output_params),
32 offset_into_queue_(0),
33 input_frames_(0),
34 buffered_input_frames_(0.0),
35 sample_rate_ratio_(1.0),
36 timestamp_helper_(output_params_.sample_rate()),
37 is_flushing_(false) {}
38
39 AudioBufferConverter::~AudioBufferConverter() {}
40
41 void AudioBufferConverter::AddInput(
42 const scoped_refptr<AudioBuffer>& buffer) {
43
44 // On EOS flush any remaining buffered data.
45 if (buffer->end_of_stream()) {
46 Flush();
47 queued_outputs_.push_back(buffer);
48 return;
49 }
50
51 // We'll need a new |audio_converter_| if there was a config change.
52 if (IsConfigChange(input_params_, buffer))
53 ResetConverter(buffer);
54
55 // Pass straight through if there's no work to be done.
56 if (!audio_converter_) {
57 queued_outputs_.push_back(buffer);
58 return;
59 }
60
61 if (timestamp_helper_.base_timestamp() == kNoTimestamp())
62 timestamp_helper_.SetBaseTimestamp(buffer->timestamp());
63
64 queued_inputs_.push_back(buffer);
65 input_frames_ += buffer->frame_count();
66
67 ConvertIfPossible();
68 }
69
70 bool AudioBufferConverter::HasNextBuffer() {
71 return !queued_outputs_.empty();
72 }
73
74 scoped_refptr<AudioBuffer> AudioBufferConverter::GetNextBuffer() {
75 DCHECK(!queued_outputs_.empty());
76 scoped_refptr<AudioBuffer> out = queued_outputs_.front();
77 queued_outputs_.pop_front();
78 return out;
79 }
80
81 void AudioBufferConverter::Reset() {
82 audio_converter_.reset(NULL);
DaleCurtis 2014/03/21 23:18:56 shouldn't need NULL.
rileya (GONE FROM CHROMIUM) 2014/03/24 18:07:05 Done.
83 queued_inputs_.clear();
84 queued_outputs_.clear();
85 timestamp_helper_.SetBaseTimestamp(kNoTimestamp());
86 input_params_ = output_params_;
87 input_frames_ = 0;
88 buffered_input_frames_ = 0.0;
89 offset_into_queue_ = 0;
90 }
91
92 double AudioBufferConverter::ProvideInput(AudioBus* audio_bus,
93 base::TimeDelta buffer_delay) {
94 DCHECK(is_flushing_ || input_frames_ >= audio_bus->frames());
95
96 int requested_frames_left = audio_bus->frames();
97 int dest_index = 0;
98
99 while (requested_frames_left > 0 && !queued_inputs_.empty()) {
100 scoped_refptr<AudioBuffer> input_buffer = queued_inputs_.front();
101
102 int frames_to_read =
103 std::min(requested_frames_left,
104 input_buffer->frame_count() - offset_into_queue_);
105 input_buffer->ReadFrames(
106 frames_to_read, offset_into_queue_, dest_index, audio_bus);
107 offset_into_queue_ += frames_to_read;
108
109 if (offset_into_queue_ == input_buffer->frame_count()) {
110 // We've consumed all the frames in |input_buffer|.
111 queued_inputs_.pop_front();
112 offset_into_queue_ = 0;
113 }
114
115 requested_frames_left -= frames_to_read;
116 dest_index += frames_to_read;
117 }
118
119 // If we're flushing, zero any extra space, otherwise we should always have
120 // enough data to completely fulfill the request.
121 if (is_flushing_ && requested_frames_left > 0)
122 audio_bus->ZeroFramesPartial(audio_bus->frames() - requested_frames_left,
123 requested_frames_left);
DaleCurtis 2014/03/21 23:18:56 alignment is off and multiline if's require {}
rileya (GONE FROM CHROMIUM) 2014/03/24 18:07:05 Done.
124 else
125 DCHECK_EQ(requested_frames_left, 0);
126
127 input_frames_ -= (audio_bus->frames() - requested_frames_left);
128 DCHECK_GE(input_frames_, 0);
129
130 buffered_input_frames_ += audio_bus->frames() - requested_frames_left;
131
132 // Full volume.
133 return 1.0;
134 }
135
136 void AudioBufferConverter::ResetConverter(
137 const scoped_refptr<AudioBuffer>& buffer) {
138 Flush();
139 audio_converter_.reset(NULL);
140 input_params_.Reset(
141 input_params_.format(),
142 buffer->channel_layout(),
143 buffer->channel_count(),
144 0,
145 buffer->sample_rate(),
146 input_params_.bits_per_sample(),
147 // This is arbitrary, but small buffer sizes result in a lot of tiny
148 // ProvideInput calls, so we'll use at least the SincResampler's default
149 // request size.
150 std::max(buffer->frame_count(),
DaleCurtis 2014/03/21 23:18:56 Probably this should round to the nearest multiple
151 static_cast<int>(SincResampler::kDefaultRequestSize)));
152
153 sample_rate_ratio_ = static_cast<double>(input_params_.sample_rate()) /
DaleCurtis 2014/03/21 23:18:56 alignment is off. git cl format is your friend :)
rileya (GONE FROM CHROMIUM) 2014/03/24 18:07:05 Fixed.
154 output_params_.sample_rate();
155
156 // If |buffer| matches |output_params_| we don't need an AudioConverter at
157 // all, and can early-out here.
158 if (!IsConfigChange(output_params_, buffer))
159 return;
160
161 audio_converter_.reset(
162 new AudioConverter(input_params_, output_params_, false));
163 audio_converter_->AddInput(this);
164 }
165
166 void AudioBufferConverter::ConvertIfPossible() {
167 DCHECK(audio_converter_);
168
169 int request_frames = 0;
170
171 if (is_flushing_) {
172 // If we're flushing we want to convert *everything* even if this means
173 // we'll have to pad some silence in ProvideInput().
174 request_frames =
175 ceil((buffered_input_frames_ + input_frames_) / sample_rate_ratio_);
176 } else {
177 // How many calls to ProvideInput() we can satisfy completely.
178 int chunks = input_frames_ / input_params_.frames_per_buffer();
179
180 // How many output frames that corresponds to:
181 request_frames = chunks * audio_converter_->ChunkSize();
182 }
183
184 if (!request_frames)
185 return;
186
187 scoped_refptr<AudioBuffer> output_buffer =
188 AudioBuffer::CreateBuffer(kSampleFormatPlanarF32,
189 output_params_.channel_layout(),
190 output_params_.sample_rate(),
191 request_frames);
192 scoped_ptr<AudioBus> output_bus =
193 AudioBus::CreateWrapper(output_buffer->channel_count());
194
195 int frames_remaining = request_frames;
196
197 // The AudioConverter wants requests of a fixed size, so we'll slide an
198 // AudioBus of that size across the |output_buffer|.
199 while (frames_remaining != 0) {
200 int frames_this_iteration =
201 std::min(output_params_.frames_per_buffer(), frames_remaining);
202
203 int offset_into_buffer = output_buffer->frame_count() - frames_remaining;
204
205 // Wrap the portion of the AudioBuffer in an AudioBus so the AudioConverter
206 // can fill it.
207 output_bus->set_frames(frames_this_iteration);
208 for (int ch = 0; ch < output_buffer->channel_count(); ++ch) {
209 output_bus->SetChannelData(
210 ch,
211 reinterpret_cast<float*>(output_buffer->channel_data()[ch]) +
212 offset_into_buffer);
213 }
214
215 // Do the actual conversion.
216 audio_converter_->Convert(output_bus.get());
217 frames_remaining -= frames_this_iteration;
218 buffered_input_frames_ -= frames_this_iteration * sample_rate_ratio_;
219 }
220
221 // Compute the timestamp.
222 output_buffer->set_timestamp(timestamp_helper_.GetTimestamp());
223 output_buffer->set_duration(
224 timestamp_helper_.GetFrameDuration(request_frames));
225 timestamp_helper_.AddFrames(request_frames);
226
227 queued_outputs_.push_back(output_buffer);
228 }
229
230 void AudioBufferConverter::Flush() {
231 if (!audio_converter_)
232 return;
233 is_flushing_ = true;
234 ConvertIfPossible();
235 is_flushing_ = false;
236 DCHECK_EQ(input_frames_, 0);
237 DCHECK_EQ(offset_into_queue_, 0);
238 DCHECK_LT(buffered_input_frames_, 1.0);
239 DCHECK(queued_inputs_.empty());
240 buffered_input_frames_ = 0.0;
241 }
242
243 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698