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

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

Powered by Google App Engine
This is Rietveld 408576698