| OLD | NEW |
| 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 "content/public/common/content_switches.h" | 9 #include "content/public/common/content_switches.h" |
| 10 #include "content/renderer/media/media_stream_audio_processor_options.h" | 10 #include "content/renderer/media/media_stream_audio_processor_options.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 const media::AudioParameters sink_params_; | 133 const media::AudioParameters sink_params_; |
| 134 | 134 |
| 135 // TODO(xians): consider using SincResampler to save some memcpy. | 135 // TODO(xians): consider using SincResampler to save some memcpy. |
| 136 // Handles mixing and resampling between input and output parameters. | 136 // Handles mixing and resampling between input and output parameters. |
| 137 media::AudioConverter audio_converter_; | 137 media::AudioConverter audio_converter_; |
| 138 scoped_ptr<media::AudioBus> audio_wrapper_; | 138 scoped_ptr<media::AudioBus> audio_wrapper_; |
| 139 scoped_ptr<media::AudioFifo> fifo_; | 139 scoped_ptr<media::AudioFifo> fifo_; |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 MediaStreamAudioProcessor::MediaStreamAudioProcessor( | 142 MediaStreamAudioProcessor::MediaStreamAudioProcessor( |
| 143 const media::AudioParameters& source_params, | |
| 144 const blink::WebMediaConstraints& constraints, | 143 const blink::WebMediaConstraints& constraints, |
| 145 int effects, | 144 int effects, |
| 146 WebRtcPlayoutDataSource* playout_data_source) | 145 WebRtcPlayoutDataSource* playout_data_source) |
| 147 : render_delay_ms_(0), | 146 : render_delay_ms_(0), |
| 148 playout_data_source_(playout_data_source), | 147 playout_data_source_(playout_data_source), |
| 149 audio_mirroring_(false), | 148 audio_mirroring_(false), |
| 150 typing_detected_(false) { | 149 typing_detected_(false) { |
| 151 capture_thread_checker_.DetachFromThread(); | 150 capture_thread_checker_.DetachFromThread(); |
| 152 render_thread_checker_.DetachFromThread(); | 151 render_thread_checker_.DetachFromThread(); |
| 153 InitializeAudioProcessingModule(constraints, effects); | 152 InitializeAudioProcessingModule(constraints, effects); |
| 154 InitializeCaptureConverter(source_params); | |
| 155 } | 153 } |
| 156 | 154 |
| 157 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() { | 155 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() { |
| 158 DCHECK(main_thread_checker_.CalledOnValidThread()); | 156 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 159 StopAudioProcessing(); | 157 StopAudioProcessing(); |
| 160 } | 158 } |
| 161 | 159 |
| 160 void MediaStreamAudioProcessor::OnCaptureFormatChanged( |
| 161 const media::AudioParameters& source_params) { |
| 162 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 163 // There is no need to hold a lock here since the caller guarantees that |
| 164 // there is no more PushCaptureData() and ProcessAndConsumeData() callbacks |
| 165 // on the capture thread. |
| 166 InitializeCaptureConverter(source_params); |
| 167 |
| 168 // Reset the |capture_thread_checker_| since the capture data will come from |
| 169 // a new capture thread. |
| 170 capture_thread_checker_.DetachFromThread(); |
| 171 } |
| 172 |
| 162 void MediaStreamAudioProcessor::PushCaptureData(media::AudioBus* audio_source) { | 173 void MediaStreamAudioProcessor::PushCaptureData(media::AudioBus* audio_source) { |
| 163 DCHECK(capture_thread_checker_.CalledOnValidThread()); | 174 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 175 DCHECK_EQ(audio_source->channels(), |
| 176 capture_converter_->source_parameters().channels()); |
| 177 DCHECK_EQ(audio_source->frames(), |
| 178 capture_converter_->source_parameters().frames_per_buffer()); |
| 179 |
| 164 if (audio_mirroring_ && | 180 if (audio_mirroring_ && |
| 165 capture_converter_->source_parameters().channel_layout() == | 181 capture_converter_->source_parameters().channel_layout() == |
| 166 media::CHANNEL_LAYOUT_STEREO) { | 182 media::CHANNEL_LAYOUT_STEREO) { |
| 167 // Swap the first and second channels. | 183 // Swap the first and second channels. |
| 168 audio_source->SwapChannels(0, 1); | 184 audio_source->SwapChannels(0, 1); |
| 169 } | 185 } |
| 170 | 186 |
| 171 capture_converter_->Push(audio_source); | 187 capture_converter_->Push(audio_source); |
| 172 } | 188 } |
| 173 | 189 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 188 } | 204 } |
| 189 | 205 |
| 190 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const { | 206 const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const { |
| 191 return capture_converter_->source_parameters(); | 207 return capture_converter_->source_parameters(); |
| 192 } | 208 } |
| 193 | 209 |
| 194 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const { | 210 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const { |
| 195 return capture_converter_->sink_parameters(); | 211 return capture_converter_->sink_parameters(); |
| 196 } | 212 } |
| 197 | 213 |
| 214 void MediaStreamAudioProcessor::StartAecDump( |
| 215 const base::PlatformFile& aec_dump_file) { |
| 216 if (audio_processing_) |
| 217 StartEchoCancellationDump(audio_processing_.get(), aec_dump_file); |
| 218 } |
| 219 |
| 220 void MediaStreamAudioProcessor::StopAecDump() { |
| 221 if (audio_processing_) |
| 222 StopEchoCancellationDump(audio_processing_.get()); |
| 223 } |
| 224 |
| 198 void MediaStreamAudioProcessor::OnPlayoutData(media::AudioBus* audio_bus, | 225 void MediaStreamAudioProcessor::OnPlayoutData(media::AudioBus* audio_bus, |
| 199 int sample_rate, | 226 int sample_rate, |
| 200 int audio_delay_milliseconds) { | 227 int audio_delay_milliseconds) { |
| 201 DCHECK(render_thread_checker_.CalledOnValidThread()); | 228 DCHECK(render_thread_checker_.CalledOnValidThread()); |
| 202 #if defined(OS_ANDROID) || defined(OS_IOS) | 229 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 203 DCHECK(audio_processing_->echo_control_mobile()->is_enabled()); | 230 DCHECK(audio_processing_->echo_control_mobile()->is_enabled()); |
| 204 #else | 231 #else |
| 205 DCHECK(audio_processing_->echo_cancellation()->is_enabled()); | 232 DCHECK(audio_processing_->echo_cancellation()->is_enabled()); |
| 206 #endif | 233 #endif |
| 207 | 234 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 // has to be done after all the needed components are enabled. | 352 // has to be done after all the needed components are enabled. |
| 326 CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate), | 353 CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate), |
| 327 0); | 354 0); |
| 328 CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, | 355 CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, |
| 329 kAudioProcessingNumberOfChannel), | 356 kAudioProcessingNumberOfChannel), |
| 330 0); | 357 0); |
| 331 } | 358 } |
| 332 | 359 |
| 333 void MediaStreamAudioProcessor::InitializeCaptureConverter( | 360 void MediaStreamAudioProcessor::InitializeCaptureConverter( |
| 334 const media::AudioParameters& source_params) { | 361 const media::AudioParameters& source_params) { |
| 362 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 335 DCHECK(source_params.IsValid()); | 363 DCHECK(source_params.IsValid()); |
| 336 | 364 |
| 337 // Create and initialize audio converter for the source data. | 365 // Create and initialize audio converter for the source data. |
| 338 // When the webrtc AudioProcessing is enabled, the sink format of the | 366 // When the webrtc AudioProcessing is enabled, the sink format of the |
| 339 // converter will be the same as the post-processed data format, which is | 367 // converter will be the same as the post-processed data format, which is |
| 340 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing | 368 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing |
| 341 // is disabled, the sink format will be the same as the source format. | 369 // is disabled, the sink format will be the same as the source format. |
| 342 const int sink_sample_rate = audio_processing_ ? | 370 const int sink_sample_rate = audio_processing_ ? |
| 343 kAudioProcessingSampleRate : source_params.sample_rate(); | 371 kAudioProcessingSampleRate : source_params.sample_rate(); |
| 344 const media::ChannelLayout sink_channel_layout = audio_processing_ ? | 372 const media::ChannelLayout sink_channel_layout = audio_processing_ ? |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 // Return 0 if the volume has not been changed, otherwise return the new | 466 // Return 0 if the volume has not been changed, otherwise return the new |
| 439 // volume. | 467 // volume. |
| 440 return (agc->stream_analog_level() == volume) ? | 468 return (agc->stream_analog_level() == volume) ? |
| 441 0 : agc->stream_analog_level(); | 469 0 : agc->stream_analog_level(); |
| 442 } | 470 } |
| 443 | 471 |
| 444 void MediaStreamAudioProcessor::StopAudioProcessing() { | 472 void MediaStreamAudioProcessor::StopAudioProcessing() { |
| 445 if (!audio_processing_.get()) | 473 if (!audio_processing_.get()) |
| 446 return; | 474 return; |
| 447 | 475 |
| 476 StopAecDump(); |
| 477 |
| 448 if (playout_data_source_) | 478 if (playout_data_source_) |
| 449 playout_data_source_->RemovePlayoutSink(this); | 479 playout_data_source_->RemovePlayoutSink(this); |
| 450 | 480 |
| 451 audio_processing_.reset(); | 481 audio_processing_.reset(); |
| 452 } | 482 } |
| 453 | 483 |
| 454 } // namespace content | 484 } // namespace content |
| OLD | NEW |