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/sinc_resampler.h" | |
17 #include "media/base/vector_math.h" | |
18 | |
19 namespace media { | |
20 | |
21 AudioBufferConverter::AudioBufferConverter(const AudioParameters& output_params) | |
22 : output_params_(output_params), | |
23 offset_into_queue_(0), | |
24 input_frames_(0), | |
25 timestamp_helper_(output_params_.sample_rate()), | |
26 is_flushing_(false) { | |
27 ResetConverter(output_params_); | |
28 } | |
29 | |
30 AudioBufferConverter::~AudioBufferConverter() {} | |
31 | |
32 void AudioBufferConverter::AddInput( | |
33 const scoped_refptr<AudioBuffer>& buffer) { | |
34 | |
35 // On EOS flush any remaining buffered data. | |
36 if (buffer->end_of_stream()) { | |
37 Flush(); | |
38 queued_outputs_.push_back(buffer); | |
39 return; | |
40 } | |
41 | |
42 // We'll need a new |audio_converter_| if there was a config change. | |
43 AudioParameters buffer_params = AudioBufferToAudioParameters(buffer); | |
DaleCurtis
2014/03/20 01:36:26
I'm loathe to create a new AudioParameters object
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Good point, I removed this and instead check direc
| |
44 if (IsConfigChange(buffer_params)) | |
45 ResetConverter(buffer_params); | |
46 | |
47 // Pass straight through if there's no work to be done. | |
48 if (!audio_converter_) { | |
49 queued_outputs_.push_back(buffer); | |
50 return; | |
51 } | |
52 | |
53 if (timestamp_helper_.base_timestamp() == kNoTimestamp()) { | |
rileya (GONE FROM CHROMIUM)
2014/03/20 00:55:11
I'm not very familiar with how timestamps are hand
DaleCurtis
2014/03/20 01:36:26
Yeah, this is fine, this is what we do in the Audi
| |
54 timestamp_helper_.SetBaseTimestamp(buffer->timestamp()); | |
55 } | |
56 | |
57 queued_inputs_.push_back(buffer); | |
58 input_frames_ += buffer->frame_count(); | |
59 | |
60 scoped_refptr<AudioBuffer> output_buffer = Convert(); | |
DaleCurtis
2014/03/20 01:36:26
Every Convert() call seems to do the same thing, s
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Sounds good. Done.
| |
61 if (output_buffer) | |
62 queued_outputs_.push_back(output_buffer); | |
63 } | |
64 | |
65 bool AudioBufferConverter::HasNextBuffer() { | |
66 return !queued_outputs_.empty(); | |
67 } | |
68 | |
69 scoped_refptr<AudioBuffer> AudioBufferConverter::GetNextBuffer() { | |
70 DCHECK(!queued_outputs_.empty()); | |
71 scoped_refptr<AudioBuffer> out = queued_outputs_.front(); | |
72 queued_outputs_.pop_front(); | |
73 return out; | |
74 } | |
75 | |
76 double AudioBufferConverter::ProvideInput(AudioBus* audio_bus, | |
77 base::TimeDelta buffer_delay) { | |
78 DCHECK(is_flushing_ || input_frames_ >= audio_bus->frames()); | |
79 | |
80 int requested_frames_left = audio_bus->frames(); | |
81 int dest_index = 0; | |
82 | |
83 while (requested_frames_left > 0 && !queued_inputs_.empty()) { | |
84 scoped_refptr<AudioBuffer> input_buffer = queued_inputs_.front(); | |
85 int frames_to_read = requested_frames_left; | |
DaleCurtis
2014/03/20 01:36:26
I think the logic below can be simplified a bit if
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Sounds good. A conditional is still necessary for
| |
86 | |
87 if (input_buffer->frame_count() - offset_into_queue_ <= | |
88 requested_frames_left) { | |
89 // Consume all remaining frames in the buffer at the front of | |
90 // |queued_inputs_| and pop it. | |
91 frames_to_read = input_buffer->frame_count() - offset_into_queue_; | |
92 queued_inputs_.pop_front(); | |
93 input_buffer->ReadFrames( | |
94 frames_to_read, offset_into_queue_, dest_index, audio_bus); | |
95 offset_into_queue_ = 0; | |
96 } else { | |
97 // Consume part of the front buffer in |queued_inputs_| and store our | |
98 // progress into that buffer in |offset_into_queue_|. | |
99 input_buffer->ReadFrames( | |
100 frames_to_read, offset_into_queue_, dest_index, audio_bus); | |
101 offset_into_queue_ += frames_to_read; | |
102 } | |
103 | |
104 requested_frames_left -= frames_to_read; | |
105 dest_index += frames_to_read; | |
106 } | |
107 | |
108 // Unless we're flushing we should always have enough data to satsify the | |
109 // request. | |
110 if (!is_flushing_) | |
111 DCHECK_EQ(requested_frames_left, 0); | |
112 | |
113 input_frames_ -= (audio_bus->frames() - requested_frames_left); | |
DaleCurtis
2014/03/20 01:36:26
You'll want to Zero out any unfilled frames. See Z
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Done.
| |
114 DCHECK_GE(input_frames_, 0); | |
115 | |
116 // Assume full volume (is this correct?) | |
DaleCurtis
2014/03/20 19:29:01
Yes. Volume is applied at the output device. If
| |
117 return 1.0; | |
118 } | |
119 | |
120 void AudioBufferConverter::ResetConverter(const AudioParameters& input_params) { | |
121 Flush(); | |
122 DCHECK_EQ(input_frames_, 0); | |
123 DCHECK(queued_inputs_.empty()); | |
124 | |
125 offset_into_queue_ = 0; | |
126 audio_converter_.reset(NULL); | |
127 input_params_ = input_params; | |
128 | |
129 if (!IsConfigChange(output_params_)) | |
DaleCurtis
2014/03/20 01:36:26
Hmm, maybe make this a bool that's passed in?
| |
130 return; | |
131 | |
132 sample_rate_ratio_ = static_cast<double>(output_params_.sample_rate()) / | |
rileya (GONE FROM CHROMIUM)
2014/03/20 00:55:11
I just realized this is only used in one place and
| |
133 input_params_.sample_rate(); | |
134 | |
135 audio_converter_.reset( | |
136 new AudioConverter(input_params_, output_params_, true)); | |
137 audio_converter_->AddInput(this); | |
138 } | |
139 | |
140 AudioParameters AudioBufferConverter::AudioBufferToAudioParameters( | |
DaleCurtis
2014/03/20 19:29:01
This can be a static .cc only method. Prefer that
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Removed entirely. (IsConfigChange is now static th
| |
141 const scoped_refptr<AudioBuffer>& buffer) { | |
142 return AudioParameters( | |
143 AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
144 buffer->channel_layout(), | |
145 buffer->sample_rate(), | |
146 32, // This is a dummy value. | |
rileya (GONE FROM CHROMIUM)
2014/03/20 00:55:11
bits_per_channel is unused by the AudioConverter.
| |
147 buffer->frame_count()); | |
148 } | |
149 | |
150 scoped_refptr<AudioBuffer> AudioBufferConverter::Convert() { | |
151 DCHECK(audio_converter_); | |
152 | |
153 // How many calls to ProvideInput() we can satisfy completely. | |
154 int chunks = input_frames_ / audio_converter_->RequestSize(); | |
DaleCurtis
2014/03/20 19:29:01
ChunkSize?
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
ChunkSize is in output frames, this is to determin
| |
155 | |
156 // If we're flushing we want to convert *everything* even if this means we'll | |
157 // have to pad some silence in ProvideInput(). Otherwise we want a multiple of | |
158 // |audio_converter_|'s ChunkSize(). | |
159 int request_frames = | |
160 is_flushing_ ? floor(sample_rate_ratio_ * input_frames_) | |
161 : chunks * audio_converter_->ChunkSize(); | |
162 | |
163 if (!request_frames) | |
rileya (GONE FROM CHROMIUM)
2014/03/20 00:55:11
General style question: is "== 0" preferable in ca
| |
164 return NULL; | |
165 | |
166 scoped_refptr<AudioBuffer> output_buffer = | |
167 AudioBuffer::CreateBuffer(kSampleFormatPlanarF32, | |
168 output_params_.channel_layout(), | |
169 output_params_.sample_rate(), | |
170 request_frames); | |
171 | |
172 int frames_remaining = request_frames; | |
173 | |
174 // The AudioConverter wants requests of a fixed size, so we'll slide an | |
175 // AudioBus of that size across the |output_buffer|. | |
rileya (GONE FROM CHROMIUM)
2014/03/20 00:55:11
Since AudioConverter now supports arbitrarily size
| |
176 while (frames_remaining != 0) { | |
177 | |
DaleCurtis
2014/03/20 19:29:01
No extra line.
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Fixed.
| |
178 int frames_this_iteration = output_params_.frames_per_buffer(); | |
DaleCurtis
2014/03/20 19:29:01
use std::min() to simplify this + conditional?
| |
179 if (frames_remaining < frames_this_iteration) | |
180 frames_this_iteration = frames_remaining; | |
181 | |
182 int offset_into_buffer = output_buffer->frame_count() - frames_remaining; | |
183 | |
184 // Wrap the portion of the AudioBuffer in an AudioBus so the AudioConverter | |
185 // can fill it. | |
186 scoped_ptr<AudioBus> output_bus = | |
DaleCurtis
2014/03/20 19:29:01
Create this outside of the while() loop and just s
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Done.
| |
187 AudioBus::CreateWrapper(output_buffer->channel_count()); | |
188 output_bus->set_frames(frames_this_iteration); | |
189 for (int ch = 0; ch < output_buffer->channel_count(); ++ch) { | |
190 output_bus->SetChannelData( | |
191 ch, | |
192 reinterpret_cast<float*>(output_buffer->channel_data()[ch]) + | |
193 offset_into_buffer); | |
194 } | |
195 | |
196 // Do the actual conversion. | |
197 audio_converter_->Convert(output_bus.get()); | |
198 frames_remaining -= frames_this_iteration; | |
199 } | |
200 | |
DaleCurtis
2014/03/20 19:29:01
You need to now trim off silence from the output u
rileya (GONE FROM CHROMIUM)
2014/03/21 20:58:49
Is this actually necessary here? The |output_buffe
DaleCurtis
2014/03/21 23:18:56
Hmm, yeah you're right, this should be correct.
| |
201 // Compute the timestamp. | |
202 output_buffer->set_timestamp(timestamp_helper_.GetTimestamp()); | |
203 output_buffer->set_duration( | |
204 timestamp_helper_.GetFrameDuration(request_frames)); | |
205 timestamp_helper_.AddFrames(request_frames); | |
206 | |
207 return output_buffer; | |
208 } | |
209 | |
210 void AudioBufferConverter::Flush() { | |
211 if (!audio_converter_) | |
212 return; | |
213 is_flushing_ = true; | |
214 scoped_refptr<AudioBuffer> output_buffer = Convert(); | |
215 if (output_buffer) | |
216 queued_outputs_.push_back(output_buffer); | |
217 is_flushing_ = false; | |
218 } | |
219 | |
220 bool AudioBufferConverter::IsConfigChange( | |
221 const AudioParameters& new_params) { | |
222 return new_params.format() != input_params_.format() || | |
223 new_params.sample_rate() != input_params_.sample_rate() || | |
224 new_params.channels() != input_params_.channels() || | |
225 new_params.channel_layout() != input_params_.channel_layout(); | |
226 } | |
227 | |
228 } // namespace media | |
OLD | NEW |