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

Side by Side Diff: content/renderer/media/media_stream_audio_processor.cc

Issue 420603004: Use the AudioProcessing float interface in MediaStreamAudioProcessor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add MediaStreamAudioBus. Created 6 years, 4 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
« no previous file with comments | « content/renderer/media/media_stream_audio_processor.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/media/media_stream_audio_processor.h" 5 #include "content/renderer/media/media_stream_audio_processor.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "content/public/common/content_switches.h" 10 #include "content/public/common/content_switches.h"
(...skipping 13 matching lines...) Expand all
24 namespace { 24 namespace {
25 25
26 using webrtc::AudioProcessing; 26 using webrtc::AudioProcessing;
27 27
28 #if defined(OS_ANDROID) 28 #if defined(OS_ANDROID)
29 const int kAudioProcessingSampleRate = 16000; 29 const int kAudioProcessingSampleRate = 16000;
30 #else 30 #else
31 const int kAudioProcessingSampleRate = 32000; 31 const int kAudioProcessingSampleRate = 32000;
32 #endif 32 #endif
33 const int kAudioProcessingNumberOfChannels = 1; 33 const int kAudioProcessingNumberOfChannels = 1;
34 const AudioProcessing::ChannelLayout kAudioProcessingChannelLayout =
35 AudioProcessing::kMono;
36 34
37 const int kMaxNumberOfBuffersInFifo = 2; 35 AudioProcessing::ChannelLayout MapLayout(media::ChannelLayout media_layout) {
36 switch (media_layout) {
37 case media::CHANNEL_LAYOUT_MONO:
38 return AudioProcessing::kMono;
39 case media::CHANNEL_LAYOUT_STEREO:
40 return AudioProcessing::kStereo;
41 case media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC:
42 return AudioProcessing::kStereoAndKeyboard;
43 default:
44 NOTREACHED() << "Layout not supported: " << media_layout;
45 return AudioProcessing::kMono;
46 }
47 }
48
49 AudioProcessing::ChannelLayout ChannelsToLayout(int num_channels) {
50 switch (num_channels) {
51 case 1:
52 return AudioProcessing::kMono;
53 case 2:
54 return AudioProcessing::kStereo;
55 default:
56 NOTREACHED() << "Channels not supported: " << num_channels;
57 return AudioProcessing::kMono;
58 }
59 }
38 60
39 // Used by UMA histograms and entries shouldn't be re-ordered or removed. 61 // Used by UMA histograms and entries shouldn't be re-ordered or removed.
40 enum AudioTrackProcessingStates { 62 enum AudioTrackProcessingStates {
41 AUDIO_PROCESSING_ENABLED = 0, 63 AUDIO_PROCESSING_ENABLED = 0,
42 AUDIO_PROCESSING_DISABLED, 64 AUDIO_PROCESSING_DISABLED,
43 AUDIO_PROCESSING_IN_WEBRTC, 65 AUDIO_PROCESSING_IN_WEBRTC,
44 AUDIO_PROCESSING_MAX 66 AUDIO_PROCESSING_MAX
45 }; 67 };
46 68
47 void RecordProcessingState(AudioTrackProcessingStates state) { 69 void RecordProcessingState(AudioTrackProcessingStates state) {
48 UMA_HISTOGRAM_ENUMERATION("Media.AudioTrackProcessingStates", 70 UMA_HISTOGRAM_ENUMERATION("Media.AudioTrackProcessingStates",
49 state, AUDIO_PROCESSING_MAX); 71 state, AUDIO_PROCESSING_MAX);
50 } 72 }
51 73
52 } // namespace 74 } // namespace
53 75
54 class MediaStreamAudioProcessor::MediaStreamAudioConverter 76 // Wraps AudioBus to provide access to the array of channel pointers, since this
55 : public media::AudioConverter::InputCallback { 77 // is the type webrtc::AudioProcessing deals in. The array is refreshed on every
78 // channel_ptrs() call, and will be valid until the underlying AudioBus pointers
79 // are changed, e.g. through calls to SetChannelData() or SwapChannels().
80 //
81 // All methods are called on one of the capture or render audio threads
82 // exclusively.
83 class MediaStreamAudioBus {
56 public: 84 public:
57 MediaStreamAudioConverter(const media::AudioParameters& source_params, 85 MediaStreamAudioBus(int channels, int frames)
58 const media::AudioParameters& sink_params) 86 : bus_(media::AudioBus::Create(channels, frames)),
59 : source_params_(source_params), 87 channel_ptrs_(new float*[channels]) {
60 sink_params_(sink_params), 88 // May be created in the main render thread and used in the audio threads.
61 audio_converter_(source_params, sink_params_, false) {
62 // An instance of MediaStreamAudioConverter may be created in the main
63 // render thread and used in the audio thread, for example, the
64 // |MediaStreamAudioProcessor::capture_converter_|.
65 thread_checker_.DetachFromThread(); 89 thread_checker_.DetachFromThread();
66 audio_converter_.AddInput(this);
67
68 // Create and initialize audio fifo and audio bus wrapper.
69 // The size of the FIFO should be at least twice of the source buffer size
70 // or twice of the sink buffer size. Also, FIFO needs to have enough space
71 // to store pre-processed data before passing the data to
72 // webrtc::AudioProcessing, which requires 10ms as packet size.
73 int max_frame_size = std::max(source_params_.frames_per_buffer(),
74 sink_params_.frames_per_buffer());
75 int buffer_size = std::max(
76 kMaxNumberOfBuffersInFifo * max_frame_size,
77 kMaxNumberOfBuffersInFifo * source_params_.sample_rate() / 100);
78 fifo_.reset(new media::AudioFifo(source_params_.channels(), buffer_size));
79
80 // TODO(xians): Use CreateWrapper to save one memcpy.
81 audio_wrapper_ = media::AudioBus::Create(sink_params_.channels(),
82 sink_params_.frames_per_buffer());
83 } 90 }
84 91
85 virtual ~MediaStreamAudioConverter() { 92 media::AudioBus* bus() {
86 audio_converter_.RemoveInput(this); 93 DCHECK(thread_checker_.CalledOnValidThread());
94 return bus_.get();
95 }
96 const media::AudioBus* bus() const {
97 DCHECK(thread_checker_.CalledOnValidThread());
98 return bus_.get();
87 } 99 }
88 100
89 void Push(const media::AudioBus* audio_source) { 101 float* const* channel_ptrs() {
90 // Called on the audio thread, which is the capture audio thread for 102 const MediaStreamAudioBus* t = this;
91 // |MediaStreamAudioProcessor::capture_converter_|, and render audio thread 103 return const_cast<float* const*>(t->channel_ptrs());
92 // for |MediaStreamAudioProcessor::render_converter_|. 104 }
93 // And it must be the same thread as calling Convert(). 105 const float* const* channel_ptrs() const {
94 DCHECK(thread_checker_.CalledOnValidThread()); 106 DCHECK(thread_checker_.CalledOnValidThread());
95 fifo_->Push(audio_source); 107 for (int i = 0; i < bus_->channels(); ++i) {
108 channel_ptrs_[i] = bus_->channel(i);
109 }
110 return channel_ptrs_.get();
96 } 111 }
97 112
98 bool Convert(webrtc::AudioFrame* out, bool audio_mirroring) { 113 private:
99 // Called on the audio thread, which is the capture audio thread for 114 base::ThreadChecker thread_checker_;
100 // |MediaStreamAudioProcessor::capture_converter_|, and render audio thread 115 scoped_ptr<media::AudioBus> bus_;
101 // for |MediaStreamAudioProcessor::render_converter_|. 116 // Mutable to allow updating before a const version is returned.
DaleCurtis 2014/08/11 20:53:13 I don't understand why this is necessary. Can you
ajm 2014/08/12 01:47:34 Yeah, I was definitely hesitant to add it. The tro
102 DCHECK(thread_checker_.CalledOnValidThread()); 117 mutable scoped_ptr<float*[]> channel_ptrs_;
103 // Return false if there is not enough data in the FIFO, this happens when 118 };
104 // fifo_->frames() / source_params_.sample_rate() is less than 119
105 // sink_params.frames_per_buffer() / sink_params.sample_rate(). 120 // Wraps AudioFifo to provide a cleaner interface to MediaStreamAudioProcessor.
106 if (fifo_->frames() * sink_params_.sample_rate() < 121 // It avoids the FIFO when the source and destination frames match. All methods
107 sink_params_.frames_per_buffer() * source_params_.sample_rate()) { 122 // are called on one of the capture or render audio threads exclusively.
108 return false; 123 class MediaStreamAudioFifo {
DaleCurtis 2014/08/11 20:53:13 Long term you may consider switching to a block fi
ajm 2014/08/12 01:47:34 Thanks for the tips. We still want deinterleaved a
124 public:
125 MediaStreamAudioFifo(int channels, int source_frames,
126 int destination_frames)
127 : source_frames_(source_frames),
128 destination_(new MediaStreamAudioBus(channels, destination_frames)),
129 data_available_(false) {
130 if (source_frames != destination_frames) {
131 // Since we require every Push to be followed by as many Consumes as
132 // possible, twice the larger of the two is a (probably) loose upper bound
133 // on the FIFO size.
134 const int fifo_frames = 2 * std::max(source_frames, destination_frames);
135 fifo_.reset(new media::AudioFifo(channels, fifo_frames));
109 } 136 }
110 137
111 // Convert data to the output format, this will trigger ProvideInput(). 138 // May be created in the main render thread and used in the audio threads.
112 audio_converter_.Convert(audio_wrapper_.get()); 139 thread_checker_.DetachFromThread();
113 DCHECK_EQ(audio_wrapper_->frames(), sink_params_.frames_per_buffer()); 140 }
114 141
115 // Swap channels before interleaving the data if |audio_mirroring| is 142 void Push(const media::AudioBus* source) {
116 // set to true. 143 DCHECK(thread_checker_.CalledOnValidThread());
117 if (audio_mirroring && 144 DCHECK_EQ(source->channels(), destination_->bus()->channels());
118 sink_params_.channel_layout() == media::CHANNEL_LAYOUT_STEREO) { 145 DCHECK_EQ(source->frames(), source_frames_);
119 // Swap the first and second channels. 146
120 audio_wrapper_->SwapChannels(0, 1); 147 if (fifo_) {
148 fifo_->Push(source);
149 } else {
150 source->CopyTo(destination_->bus());
151 data_available_ = true;
152 }
153 }
154
155 // Returns true if there are destination_frames() of data available to be
156 // consumed, and otherwise false.
157 bool Consume(MediaStreamAudioBus** destination) {
158 DCHECK(thread_checker_.CalledOnValidThread());
159
160 if (fifo_) {
161 if (fifo_->frames() < destination_->bus()->frames())
162 return false;
163
164 fifo_->Consume(destination_->bus(), 0, destination_->bus()->frames());
165 } else {
166 if (!data_available_)
167 return false;
168
169 // The data was already copied to |destination_| in this case.
170 data_available_ = false;
121 } 171 }
122 172
123 // TODO(xians): Figure out a better way to handle the interleaved and 173 *destination = destination_.get();
124 // deinterleaved format switching.
125 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(),
126 sink_params_.bits_per_sample() / 8,
127 out->data_);
128
129 out->samples_per_channel_ = sink_params_.frames_per_buffer();
130 out->sample_rate_hz_ = sink_params_.sample_rate();
131 out->speech_type_ = webrtc::AudioFrame::kNormalSpeech;
132 out->vad_activity_ = webrtc::AudioFrame::kVadUnknown;
133 out->num_channels_ = sink_params_.channels();
134
135 return true; 174 return true;
136 } 175 }
137 176
138 const media::AudioParameters& source_parameters() const {
139 return source_params_;
140 }
141 const media::AudioParameters& sink_parameters() const {
142 return sink_params_;
143 }
144
145 private: 177 private:
146 // AudioConverter::InputCallback implementation.
147 virtual double ProvideInput(media::AudioBus* audio_bus,
148 base::TimeDelta buffer_delay) OVERRIDE {
149 // Called on realtime audio thread.
150 // TODO(xians): Figure out why the first Convert() triggers ProvideInput
151 // two times.
152 if (fifo_->frames() < audio_bus->frames())
153 return 0;
154
155 fifo_->Consume(audio_bus, 0, audio_bus->frames());
156
157 // Return 1.0 to indicate no volume scaling on the data.
158 return 1.0;
159 }
160
161 base::ThreadChecker thread_checker_; 178 base::ThreadChecker thread_checker_;
162 const media::AudioParameters source_params_; 179 const int source_frames_; // For a DCHECK.
163 const media::AudioParameters sink_params_; 180 scoped_ptr<MediaStreamAudioBus> destination_;
164
165 // TODO(xians): consider using SincResampler to save some memcpy.
166 // Handles mixing and resampling between input and output parameters.
167 media::AudioConverter audio_converter_;
168 scoped_ptr<media::AudioBus> audio_wrapper_;
169 scoped_ptr<media::AudioFifo> fifo_; 181 scoped_ptr<media::AudioFifo> fifo_;
182 // Only used when the FIFO is disabled;
183 bool data_available_;
170 }; 184 };
171 185
172 bool MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled() { 186 bool MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled() {
173 return !CommandLine::ForCurrentProcess()->HasSwitch( 187 return !CommandLine::ForCurrentProcess()->HasSwitch(
174 switches::kDisableAudioTrackProcessing); 188 switches::kDisableAudioTrackProcessing);
175 } 189 }
176 190
177 MediaStreamAudioProcessor::MediaStreamAudioProcessor( 191 MediaStreamAudioProcessor::MediaStreamAudioProcessor(
178 const blink::WebMediaConstraints& constraints, 192 const blink::WebMediaConstraints& constraints,
179 int effects, 193 int effects,
(...skipping 15 matching lines...) Expand all
195 aec_dump_message_filter_->AddDelegate(this); 209 aec_dump_message_filter_->AddDelegate(this);
196 } 210 }
197 } 211 }
198 212
199 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() { 213 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() {
200 DCHECK(main_thread_checker_.CalledOnValidThread()); 214 DCHECK(main_thread_checker_.CalledOnValidThread());
201 Stop(); 215 Stop();
202 } 216 }
203 217
204 void MediaStreamAudioProcessor::OnCaptureFormatChanged( 218 void MediaStreamAudioProcessor::OnCaptureFormatChanged(
205 const media::AudioParameters& source_params) { 219 const media::AudioParameters& input_format) {
206 DCHECK(main_thread_checker_.CalledOnValidThread()); 220 DCHECK(main_thread_checker_.CalledOnValidThread());
207 // There is no need to hold a lock here since the caller guarantees that 221 // There is no need to hold a lock here since the caller guarantees that
208 // there is no more PushCaptureData() and ProcessAndConsumeData() callbacks 222 // there is no more PushCaptureData() and ProcessAndConsumeData() callbacks
209 // on the capture thread. 223 // on the capture thread.
210 InitializeCaptureConverter(source_params); 224 InitializeCaptureFifo(input_format);
211 225
212 // Reset the |capture_thread_checker_| since the capture data will come from 226 // Reset the |capture_thread_checker_| since the capture data will come from
213 // a new capture thread. 227 // a new capture thread.
214 capture_thread_checker_.DetachFromThread(); 228 capture_thread_checker_.DetachFromThread();
215 } 229 }
216 230
217 void MediaStreamAudioProcessor::PushCaptureData( 231 void MediaStreamAudioProcessor::PushCaptureData(
218 const media::AudioBus* audio_source) { 232 const media::AudioBus* audio_source) {
219 DCHECK(capture_thread_checker_.CalledOnValidThread()); 233 DCHECK(capture_thread_checker_.CalledOnValidThread());
220 DCHECK_EQ(audio_source->channels(),
221 capture_converter_->source_parameters().channels());
222 DCHECK_EQ(audio_source->frames(),
223 capture_converter_->source_parameters().frames_per_buffer());
224 234
225 capture_converter_->Push(audio_source); 235 capture_fifo_->Push(audio_source);
226 } 236 }
227 237
228 bool MediaStreamAudioProcessor::ProcessAndConsumeData( 238 bool MediaStreamAudioProcessor::ProcessAndConsumeData(
229 base::TimeDelta capture_delay, int volume, bool key_pressed, 239 base::TimeDelta capture_delay, int volume, bool key_pressed,
230 int* new_volume, int16** out) { 240 int* new_volume, int16** out) {
231 DCHECK(capture_thread_checker_.CalledOnValidThread()); 241 DCHECK(capture_thread_checker_.CalledOnValidThread());
232 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessAndConsumeData"); 242 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessAndConsumeData");
233 243
234 if (!capture_converter_->Convert(&capture_frame_, audio_mirroring_)) 244 MediaStreamAudioBus* input_bus;
245 if (!capture_fifo_->Consume(&input_bus))
235 return false; 246 return false;
236 247
237 *new_volume = ProcessData(&capture_frame_, capture_delay, volume, 248 // Use the input bus directly if audio processing is disabled.
238 key_pressed); 249 MediaStreamAudioBus* output_bus = input_bus;
239 *out = capture_frame_.data_; 250 *new_volume = 0;
251 if (audio_processing_) {
252 output_bus = output_bus_.get();
253 *new_volume = ProcessData(input_bus, capture_delay, volume,
254 key_pressed, output_bus);
255 }
256
257 // Swap channels before interleaving the data.
258 if (audio_mirroring_ &&
259 output_format_.channel_layout() == media::CHANNEL_LAYOUT_STEREO) {
260 // Swap the first and second channels.
261 output_bus->bus()->SwapChannels(0, 1);
262 }
263
264 output_bus->bus()->ToInterleaved(output_bus->bus()->frames(),
265 sizeof(int16),
266 output_data_.get());
267 *out = output_data_.get();
240 268
241 return true; 269 return true;
242 } 270 }
243 271
244 void MediaStreamAudioProcessor::Stop() { 272 void MediaStreamAudioProcessor::Stop() {
245 DCHECK(main_thread_checker_.CalledOnValidThread()); 273 DCHECK(main_thread_checker_.CalledOnValidThread());
246 if (stopped_) 274 if (stopped_)
247 return; 275 return;
248 276
249 stopped_ = true; 277 stopped_ = true;
250 278
251 if (aec_dump_message_filter_) { 279 if (aec_dump_message_filter_) {
252 aec_dump_message_filter_->RemoveDelegate(this); 280 aec_dump_message_filter_->RemoveDelegate(this);
253 aec_dump_message_filter_ = NULL; 281 aec_dump_message_filter_ = NULL;
254 } 282 }
255 283
256 if (!audio_processing_.get()) 284 if (!audio_processing_.get())
257 return; 285 return;
258 286
259 StopEchoCancellationDump(audio_processing_.get()); 287 StopEchoCancellationDump(audio_processing_.get());
260 288
261 if (playout_data_source_) { 289 if (playout_data_source_) {
262 playout_data_source_->RemovePlayoutSink(this); 290 playout_data_source_->RemovePlayoutSink(this);
263 playout_data_source_ = NULL; 291 playout_data_source_ = NULL;
264 } 292 }
265 } 293 }
266 294
267 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const { 295 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const {
268 return capture_converter_->source_parameters(); 296 return input_format_;
269 } 297 }
270 298
271 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const { 299 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const {
272 return capture_converter_->sink_parameters(); 300 return output_format_;
273 } 301 }
274 302
275 void MediaStreamAudioProcessor::OnAecDumpFile( 303 void MediaStreamAudioProcessor::OnAecDumpFile(
276 const IPC::PlatformFileForTransit& file_handle) { 304 const IPC::PlatformFileForTransit& file_handle) {
277 DCHECK(main_thread_checker_.CalledOnValidThread()); 305 DCHECK(main_thread_checker_.CalledOnValidThread());
278 306
279 base::File file = IPC::PlatformFileForTransitToFile(file_handle); 307 base::File file = IPC::PlatformFileForTransitToFile(file_handle);
280 DCHECK(file.IsValid()); 308 DCHECK(file.IsValid());
281 309
282 if (audio_processing_) 310 if (audio_processing_)
(...skipping 18 matching lines...) Expand all
301 int audio_delay_milliseconds) { 329 int audio_delay_milliseconds) {
302 DCHECK(render_thread_checker_.CalledOnValidThread()); 330 DCHECK(render_thread_checker_.CalledOnValidThread());
303 DCHECK(audio_processing_->echo_control_mobile()->is_enabled() ^ 331 DCHECK(audio_processing_->echo_control_mobile()->is_enabled() ^
304 audio_processing_->echo_cancellation()->is_enabled()); 332 audio_processing_->echo_cancellation()->is_enabled());
305 333
306 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::OnPlayoutData"); 334 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::OnPlayoutData");
307 DCHECK_LT(audio_delay_milliseconds, 335 DCHECK_LT(audio_delay_milliseconds,
308 std::numeric_limits<base::subtle::Atomic32>::max()); 336 std::numeric_limits<base::subtle::Atomic32>::max());
309 base::subtle::Release_Store(&render_delay_ms_, audio_delay_milliseconds); 337 base::subtle::Release_Store(&render_delay_ms_, audio_delay_milliseconds);
310 338
311 InitializeRenderConverterIfNeeded(sample_rate, audio_bus->channels(), 339 InitializeRenderFifoIfNeeded(sample_rate, audio_bus->channels(),
312 audio_bus->frames()); 340 audio_bus->frames());
313 341
314 render_converter_->Push(audio_bus); 342 render_fifo_->Push(audio_bus);
315 while (render_converter_->Convert(&render_frame_, false)) 343 MediaStreamAudioBus* analysis_bus;
316 audio_processing_->AnalyzeReverseStream(&render_frame_); 344 while (render_fifo_->Consume(&analysis_bus)) {
345 audio_processing_->AnalyzeReverseStream(
346 analysis_bus->channel_ptrs(),
347 analysis_bus->bus()->frames(),
348 sample_rate,
349 ChannelsToLayout(audio_bus->channels()));
350 }
317 } 351 }
318 352
319 void MediaStreamAudioProcessor::OnPlayoutDataSourceChanged() { 353 void MediaStreamAudioProcessor::OnPlayoutDataSourceChanged() {
320 DCHECK(main_thread_checker_.CalledOnValidThread()); 354 DCHECK(main_thread_checker_.CalledOnValidThread());
321 // There is no need to hold a lock here since the caller guarantees that 355 // There is no need to hold a lock here since the caller guarantees that
322 // there is no more OnPlayoutData() callback on the render thread. 356 // there is no more OnPlayoutData() callback on the render thread.
323 render_thread_checker_.DetachFromThread(); 357 render_thread_checker_.DetachFromThread();
324 render_converter_.reset(); 358 render_fifo_.reset();
325 } 359 }
326 360
327 void MediaStreamAudioProcessor::GetStats(AudioProcessorStats* stats) { 361 void MediaStreamAudioProcessor::GetStats(AudioProcessorStats* stats) {
328 stats->typing_noise_detected = 362 stats->typing_noise_detected =
329 (base::subtle::Acquire_Load(&typing_detected_) != false); 363 (base::subtle::Acquire_Load(&typing_detected_) != false);
330 GetAecStats(audio_processing_.get(), stats); 364 GetAecStats(audio_processing_.get(), stats);
331 } 365 }
332 366
333 void MediaStreamAudioProcessor::InitializeAudioProcessingModule( 367 void MediaStreamAudioProcessor::InitializeAudioProcessingModule(
334 const blink::WebMediaConstraints& constraints, int effects) { 368 const blink::WebMediaConstraints& constraints, int effects) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 // Return immediately if no goog constraint is enabled. 411 // Return immediately if no goog constraint is enabled.
378 if (!echo_cancellation && !goog_experimental_aec && !goog_ns && 412 if (!echo_cancellation && !goog_experimental_aec && !goog_ns &&
379 !goog_high_pass_filter && !goog_typing_detection && 413 !goog_high_pass_filter && !goog_typing_detection &&
380 !goog_agc && !goog_experimental_ns) { 414 !goog_agc && !goog_experimental_ns) {
381 RecordProcessingState(AUDIO_PROCESSING_DISABLED); 415 RecordProcessingState(AUDIO_PROCESSING_DISABLED);
382 return; 416 return;
383 } 417 }
384 418
385 // Create and configure the webrtc::AudioProcessing. 419 // Create and configure the webrtc::AudioProcessing.
386 audio_processing_.reset(webrtc::AudioProcessing::Create()); 420 audio_processing_.reset(webrtc::AudioProcessing::Create());
387 CHECK_EQ(0, audio_processing_->Initialize(kAudioProcessingSampleRate,
388 kAudioProcessingSampleRate,
389 kAudioProcessingSampleRate,
390 kAudioProcessingChannelLayout,
391 kAudioProcessingChannelLayout,
392 kAudioProcessingChannelLayout));
393 421
394 // Enable the audio processing components. 422 // Enable the audio processing components.
395 if (echo_cancellation) { 423 if (echo_cancellation) {
396 EnableEchoCancellation(audio_processing_.get()); 424 EnableEchoCancellation(audio_processing_.get());
397 425
398 if (goog_experimental_aec) 426 if (goog_experimental_aec)
399 EnableExperimentalEchoCancellation(audio_processing_.get()); 427 EnableExperimentalEchoCancellation(audio_processing_.get());
400 428
401 if (playout_data_source_) 429 if (playout_data_source_)
402 playout_data_source_->AddPlayoutSink(this); 430 playout_data_source_->AddPlayoutSink(this);
(...skipping 14 matching lines...) Expand all
417 typing_detector_.reset(new webrtc::TypingDetection()); 445 typing_detector_.reset(new webrtc::TypingDetection());
418 EnableTypingDetection(audio_processing_.get(), typing_detector_.get()); 446 EnableTypingDetection(audio_processing_.get(), typing_detector_.get());
419 } 447 }
420 448
421 if (goog_agc) 449 if (goog_agc)
422 EnableAutomaticGainControl(audio_processing_.get()); 450 EnableAutomaticGainControl(audio_processing_.get());
423 451
424 RecordProcessingState(AUDIO_PROCESSING_ENABLED); 452 RecordProcessingState(AUDIO_PROCESSING_ENABLED);
425 } 453 }
426 454
427 void MediaStreamAudioProcessor::InitializeCaptureConverter( 455 void MediaStreamAudioProcessor::InitializeCaptureFifo(
428 const media::AudioParameters& source_params) { 456 const media::AudioParameters& input_format) {
429 DCHECK(main_thread_checker_.CalledOnValidThread()); 457 DCHECK(main_thread_checker_.CalledOnValidThread());
430 DCHECK(source_params.IsValid()); 458 DCHECK(input_format.IsValid());
459 input_format_ = input_format;
431 460
432 // Create and initialize audio converter for the source data. 461 // TODO(ajm): For now, we assume fixed parameters for the output when audio
433 // When the webrtc AudioProcessing is enabled, the sink format of the 462 // processing is enabled, to match the previous behavior. We should either
434 // converter will be the same as the post-processed data format, which is 463 // use the input parameters (in which case, audio processing will convert
435 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing 464 // at output) or ideally, have a backchannel from the sink to know what
436 // is disabled, the sink format will be the same as the source format. 465 // format it would prefer.
437 const int sink_sample_rate = audio_processing_ ? 466 const int output_sample_rate = audio_processing_ ?
438 kAudioProcessingSampleRate : source_params.sample_rate(); 467 kAudioProcessingSampleRate : input_format.sample_rate();
439 const media::ChannelLayout sink_channel_layout = audio_processing_ ? 468 const media::ChannelLayout output_channel_layout = audio_processing_ ?
440 media::GuessChannelLayout(kAudioProcessingNumberOfChannels) : 469 media::GuessChannelLayout(kAudioProcessingNumberOfChannels) :
441 source_params.channel_layout(); 470 input_format.channel_layout();
442 471
443 // WebRtc AudioProcessing requires 10ms as its packet size. We use this 472 // webrtc::AudioProcessing requires a 10 ms chunk size. We use this native
444 // native size when processing is enabled. While processing is disabled, and 473 // size when processing is enabled. When disabled we use the same size as
445 // the source is running with a buffer size smaller than 10ms buffer, we use 474 // the source if less than 10 ms.
446 // same buffer size as the incoming format to avoid extra FIFO for WebAudio. 475 //
447 int sink_buffer_size = sink_sample_rate / 100; 476 // TODO(ajm): This conditional buffer size appears to be assuming knowledge of
448 if (!audio_processing_ && 477 // the sink based on the source parameters. PeerConnection sinks seem to want
449 source_params.frames_per_buffer() < sink_buffer_size) { 478 // 10 ms chunks regardless, while WebAudio sinks want less, and we're assuming
450 sink_buffer_size = source_params.frames_per_buffer(); 479 // we can identify WebAudio sinks by the input chunk size. Less fragile would
480 // be to have the sink actually tell us how much it wants (as in the above
481 // TODO).
482 const int processing_frames = audio_processing_ ?
483 input_format.sample_rate() / 100 : input_format.frames_per_buffer();
484 const int output_frames = audio_processing_ ?
485 output_sample_rate / 100 : input_format.frames_per_buffer();
486
487 output_format_ = media::AudioParameters(
488 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
489 output_channel_layout,
490 output_sample_rate,
491 16,
492 output_frames);
493
494 capture_fifo_.reset(
495 new MediaStreamAudioFifo(input_format.channels(),
496 input_format.frames_per_buffer(),
497 processing_frames));
498
499 if (audio_processing_) {
500 output_bus_.reset(new MediaStreamAudioBus(output_format_.channels(),
501 output_frames));
451 } 502 }
452 503 output_data_.reset(new int16[output_format_.GetBytesPerBuffer() /
453 media::AudioParameters sink_params( 504 sizeof(int16)]);
454 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, sink_channel_layout,
455 sink_sample_rate, 16, sink_buffer_size);
456 capture_converter_.reset(
457 new MediaStreamAudioConverter(source_params, sink_params));
458 } 505 }
459 506
460 void MediaStreamAudioProcessor::InitializeRenderConverterIfNeeded( 507 void MediaStreamAudioProcessor::InitializeRenderFifoIfNeeded(
461 int sample_rate, int number_of_channels, int frames_per_buffer) { 508 int sample_rate, int number_of_channels, int frames_per_buffer) {
462 DCHECK(render_thread_checker_.CalledOnValidThread()); 509 DCHECK(render_thread_checker_.CalledOnValidThread());
463 // TODO(xians): Figure out if we need to handle the buffer size change. 510 if (render_fifo_.get() &&
464 if (render_converter_.get() && 511 render_format_.sample_rate() == sample_rate &&
465 render_converter_->source_parameters().sample_rate() == sample_rate && 512 render_format_.channels() == number_of_channels &&
466 render_converter_->source_parameters().channels() == number_of_channels) { 513 render_format_.frames_per_buffer() == frames_per_buffer) {
467 // Do nothing if the |render_converter_| has been setup properly. 514 // Do nothing if the |render_fifo_| has been setup properly.
468 return; 515 return;
469 } 516 }
470 517
471 // Create and initialize audio converter for the render data. 518 render_format_ = media::AudioParameters(
472 // webrtc::AudioProcessing accepts the same format as what it uses to process
473 // capture data, which is 32k mono for desktops and 16k mono for Android.
474 media::AudioParameters source_params(
475 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 519 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
476 media::GuessChannelLayout(number_of_channels), sample_rate, 16, 520 media::GuessChannelLayout(number_of_channels),
521 sample_rate,
522 16,
477 frames_per_buffer); 523 frames_per_buffer);
478 media::AudioParameters sink_params( 524
479 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 525 const int analysis_frames = sample_rate / 100; // 10 ms chunks.
480 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16, 526 render_fifo_.reset(
481 kAudioProcessingSampleRate / 100); 527 new MediaStreamAudioFifo(number_of_channels,
482 render_converter_.reset( 528 frames_per_buffer,
483 new MediaStreamAudioConverter(source_params, sink_params)); 529 analysis_frames));
484 render_data_bus_ = media::AudioBus::Create(number_of_channels,
485 frames_per_buffer);
486 } 530 }
487 531
488 int MediaStreamAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame, 532 int MediaStreamAudioProcessor::ProcessData(const MediaStreamAudioBus* input,
489 base::TimeDelta capture_delay, 533 base::TimeDelta capture_delay,
490 int volume, 534 int volume,
491 bool key_pressed) { 535 bool key_pressed,
536 MediaStreamAudioBus* output) {
537 DCHECK(audio_processing_);
492 DCHECK(capture_thread_checker_.CalledOnValidThread()); 538 DCHECK(capture_thread_checker_.CalledOnValidThread());
493 if (!audio_processing_)
494 return 0;
495 539
496 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessData"); 540 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::ProcessData");
497 DCHECK_EQ(audio_processing_->input_sample_rate_hz(),
498 capture_converter_->sink_parameters().sample_rate());
499 DCHECK_EQ(audio_processing_->num_input_channels(),
500 capture_converter_->sink_parameters().channels());
501 DCHECK_EQ(audio_processing_->num_output_channels(),
502 capture_converter_->sink_parameters().channels());
503 541
504 base::subtle::Atomic32 render_delay_ms = 542 base::subtle::Atomic32 render_delay_ms =
505 base::subtle::Acquire_Load(&render_delay_ms_); 543 base::subtle::Acquire_Load(&render_delay_ms_);
506 int64 capture_delay_ms = capture_delay.InMilliseconds(); 544 int64 capture_delay_ms = capture_delay.InMilliseconds();
507 DCHECK_LT(capture_delay_ms, 545 DCHECK_LT(capture_delay_ms,
508 std::numeric_limits<base::subtle::Atomic32>::max()); 546 std::numeric_limits<base::subtle::Atomic32>::max());
509 int total_delay_ms = capture_delay_ms + render_delay_ms; 547 int total_delay_ms = capture_delay_ms + render_delay_ms;
510 if (total_delay_ms > 300) { 548 if (total_delay_ms > 300) {
511 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms 549 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms
512 << "ms; render delay: " << render_delay_ms << "ms"; 550 << "ms; render delay: " << render_delay_ms << "ms";
513 } 551 }
514 552
515 audio_processing_->set_stream_delay_ms(total_delay_ms); 553 webrtc::AudioProcessing* ap = audio_processing_.get();
554 ap->set_stream_delay_ms(total_delay_ms);
516 555
517 DCHECK_LE(volume, WebRtcAudioDeviceImpl::kMaxVolumeLevel); 556 DCHECK_LE(volume, WebRtcAudioDeviceImpl::kMaxVolumeLevel);
518 webrtc::GainControl* agc = audio_processing_->gain_control(); 557 webrtc::GainControl* agc = ap->gain_control();
519 int err = agc->set_stream_analog_level(volume); 558 int err = agc->set_stream_analog_level(volume);
520 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err; 559 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err;
521 560
522 audio_processing_->set_stream_key_pressed(key_pressed); 561 ap->set_stream_key_pressed(key_pressed);
523 562
524 err = audio_processing_->ProcessStream(audio_frame); 563 err = ap->ProcessStream(input->channel_ptrs(),
564 input->bus()->frames(),
565 input_format_.sample_rate(),
566 MapLayout(input_format_.channel_layout()),
567 output_format_.sample_rate(),
568 MapLayout(output_format_.channel_layout()),
569 output->channel_ptrs());
525 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err; 570 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err;
526 571
527 if (typing_detector_ && 572 if (typing_detector_) {
528 audio_frame->vad_activity_ != webrtc::AudioFrame::kVadUnknown) { 573 webrtc::VoiceDetection* vad = ap->voice_detection();
529 bool vad_active = 574 DCHECK(vad->is_enabled());
530 (audio_frame->vad_activity_ == webrtc::AudioFrame::kVadActive); 575 bool detected = typing_detector_->Process(key_pressed,
531 bool typing_detected = typing_detector_->Process(key_pressed, vad_active); 576 vad->stream_has_voice());
532 base::subtle::Release_Store(&typing_detected_, typing_detected); 577 base::subtle::Release_Store(&typing_detected_, detected);
533 } 578 }
534 579
535 // Return 0 if the volume has not been changed, otherwise return the new 580 // Return 0 if the volume hasn't been changed, and otherwise the new volume.
536 // volume.
537 return (agc->stream_analog_level() == volume) ? 581 return (agc->stream_analog_level() == volume) ?
538 0 : agc->stream_analog_level(); 582 0 : agc->stream_analog_level();
539 } 583 }
540 584
541 } // namespace content 585 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/media_stream_audio_processor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698