Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/media/webrtc_audio_processor.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/debug/trace_event.h" | |
| 9 #include "content/public/common/content_switches.h" | |
| 10 #include "content/renderer/media/webrtc_audio_processor_util.h" | |
| 11 #include "media/audio/audio_parameters.h" | |
| 12 #include "media/base/audio_converter.h" | |
| 13 #include "media/base/audio_fifo.h" | |
| 14 #include "media/base/channel_layout.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 using webrtc::AudioProcessing; | |
| 21 using webrtc::MediaConstraintsInterface; | |
| 22 | |
| 23 #if defined(ANDROID) | |
| 24 const int kAudioProcessingSampleRate = 16000; | |
| 25 #else | |
| 26 const int kAudioProcessingSampleRate = 32000; | |
| 27 #endif | |
| 28 const int kAudioProcessingNumberOfChannel = 1; | |
| 29 | |
| 30 const int kMaxNumberOfBuffersInFifo = 2; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 class WebRtcAudioProcessor::WebRtcAudioConverter | |
| 35 : public media::AudioConverter::InputCallback { | |
| 36 public: | |
| 37 WebRtcAudioConverter(const media::AudioParameters& source_params, | |
| 38 const media::AudioParameters& sink_params) | |
| 39 : source_params_(source_params), | |
| 40 sink_params_(sink_params), | |
| 41 audio_converter_(source_params, sink_params_, false) { | |
| 42 audio_converter_.AddInput(this); | |
| 43 // Create and initialize audio fifo and audio bus wrapper. | |
| 44 // The size of the FIFO should be at least twice of the source buffer size | |
| 45 // or twice of the sink buffer size. | |
| 46 int buffer_size = std::max( | |
| 47 kMaxNumberOfBuffersInFifo * source_params_.frames_per_buffer(), | |
| 48 kMaxNumberOfBuffersInFifo * sink_params_.frames_per_buffer()); | |
| 49 fifo_.reset(new media::AudioFifo(source_params_.channels(), buffer_size)); | |
|
DaleCurtis
2013/11/07 01:36:00
This could also be stack allocated if you want. Ju
no longer working on chromium
2013/11/07 14:43:12
I forgot to explain why I would like to keep it as
| |
| 50 // TODO(xians): Use CreateWrapper to save one memcpy. | |
| 51 audio_wrapper_ = media::AudioBus::Create(sink_params_.channels(), | |
| 52 sink_params_.frames_per_buffer()); | |
| 53 } | |
| 54 | |
| 55 virtual ~WebRtcAudioConverter() { | |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 57 audio_converter_.RemoveInput(this); | |
| 58 } | |
| 59 | |
| 60 void Push(media::AudioBus* audio_source) { | |
| 61 fifo_->Push(audio_source); | |
| 62 } | |
| 63 | |
| 64 bool Convert() { | |
|
DaleCurtis
2013/11/07 01:36:00
Comment on what thread this runs on? Ditto for Pus
no longer working on chromium
2013/11/07 14:43:12
Done.
| |
| 65 // Return false if there is no 10ms data in the FIFO. | |
| 66 if (fifo_->frames() < (source_params_.sample_rate() / 100)) | |
| 67 return false; | |
| 68 | |
| 69 // Convert 10ms data to the output format, this will trigger ProvideInput(). | |
| 70 audio_converter_.Convert(audio_wrapper_.get()); | |
| 71 | |
| 72 // TODO(xians): Figure out a better way to handle the interleaved and | |
| 73 // deinterleaved format switching. | |
| 74 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(), | |
| 75 sink_params_.bits_per_sample() / 8, | |
| 76 audio_frame_.data_); | |
| 77 | |
| 78 audio_frame_.samples_per_channel_ = sink_params_.frames_per_buffer(); | |
| 79 audio_frame_.sample_rate_hz_ = sink_params_.sample_rate(); | |
| 80 audio_frame_.speech_type_ = webrtc::AudioFrame::kNormalSpeech; | |
| 81 audio_frame_.vad_activity_ = webrtc::AudioFrame::kVadUnknown; | |
| 82 audio_frame_.num_channels_ = sink_params_.channels(); | |
| 83 | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 webrtc::AudioFrame* audio_frame() { return &audio_frame_; } | |
| 88 const media::AudioParameters& source_parameters() const { | |
| 89 return source_params_; | |
| 90 } | |
| 91 const media::AudioParameters& sink_parameters() const { | |
| 92 return sink_params_; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 // AudioConverter::InputCallback implementation. | |
| 97 virtual double ProvideInput(media::AudioBus* audio_bus, | |
| 98 base::TimeDelta buffer_delay) { | |
| 99 // TODO(xians): Figure out why the first Convert() triggers ProvideInput | |
|
DaleCurtis
2013/11/07 01:36:00
Probably because output frames > input_frames * in
no longer working on chromium
2013/11/07 14:43:12
Acknowledged.
| |
| 100 // two times. | |
| 101 if (fifo_->frames() < audio_bus->frames()) | |
| 102 return 0; | |
| 103 | |
| 104 fifo_->Consume(audio_bus, 0, audio_bus->frames()); | |
| 105 return 1.0; | |
| 106 } | |
| 107 | |
| 108 base::ThreadChecker thread_checker_; | |
| 109 media::AudioParameters source_params_; | |
| 110 media::AudioParameters sink_params_; | |
| 111 webrtc::AudioFrame audio_frame_; | |
| 112 | |
| 113 // TODO(xians): consider using SincResampler to save some memcpy. | |
| 114 // Handles mixing and resampling between input and output parameters. | |
| 115 media::AudioConverter audio_converter_; | |
| 116 scoped_ptr<media::AudioBus> audio_wrapper_; | |
| 117 scoped_ptr<media::AudioFifo> fifo_; | |
| 118 }; | |
| 119 | |
| 120 WebRtcAudioProcessor::WebRtcAudioProcessor( | |
| 121 const webrtc::MediaConstraintsInterface* constraints) | |
| 122 : render_delay_ms_(0) { | |
| 123 InitializeAudioProcessingModule(constraints); | |
| 124 } | |
| 125 | |
| 126 WebRtcAudioProcessor::~WebRtcAudioProcessor() { | |
| 127 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 128 StopAudioProcessing(); | |
| 129 } | |
| 130 | |
| 131 void WebRtcAudioProcessor::SetCaptureFormat( | |
| 132 const media::AudioParameters& source_params) { | |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 134 DCHECK(source_params.IsValid()); | |
| 135 | |
| 136 // Create and initialize audio converter for the source data. | |
| 137 // When the webrtc AudioProcessing is enabled, the sink format of the | |
| 138 // converter will be the same as the post-processed data format, which is | |
| 139 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing | |
| 140 // is disabled, the sink format will be the same as the source format. | |
| 141 int sink_sample_rate = audio_processing_ ? | |
|
DaleCurtis
2013/11/07 01:36:00
const these two.
no longer working on chromium
2013/11/07 14:43:12
Done.
| |
| 142 kAudioProcessingSampleRate : source_params.sample_rate(); | |
| 143 media::ChannelLayout sink_channel_layout = audio_processing_ ? | |
| 144 media::CHANNEL_LAYOUT_MONO : source_params.channel_layout(); | |
| 145 | |
| 146 // WebRtc is using 10ms data as its native packet size. | |
| 147 media::AudioParameters sink_params( | |
| 148 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, sink_channel_layout, | |
| 149 sink_sample_rate, 16, sink_sample_rate / 100); | |
| 150 capture_converter_.reset( | |
| 151 new WebRtcAudioConverter(source_params, sink_params)); | |
| 152 } | |
| 153 | |
| 154 void WebRtcAudioProcessor::PushCaptureData(media::AudioBus* audio_source) { | |
| 155 capture_converter_->Push(audio_source); | |
| 156 } | |
| 157 | |
| 158 bool WebRtcAudioProcessor::ProcessAndConsumeData( | |
| 159 int capture_audio_delay_ms, int volume, bool key_pressed, | |
| 160 int16** out) { | |
| 161 TRACE_EVENT0("audio", | |
| 162 "WebRtcAudioProcessor::ProcessAndConsumeData"); | |
| 163 | |
| 164 if (!capture_converter_->Convert()) | |
| 165 return false; | |
| 166 | |
| 167 ProcessData(capture_audio_delay_ms, volume, key_pressed); | |
| 168 *out = capture_converter_->audio_frame()->data_; | |
| 169 | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 173 const media::AudioParameters& WebRtcAudioProcessor::OutputFormat() const { | |
| 174 return capture_converter_->sink_parameters(); | |
| 175 } | |
| 176 | |
| 177 void WebRtcAudioProcessor::ProcessData(int capture_audio_delay_ms, | |
| 178 int volume, | |
| 179 bool key_pressed) { | |
| 180 if (!audio_processing_) | |
| 181 return; | |
| 182 | |
| 183 TRACE_EVENT0("audio", "WebRtcAudioProcessor::Process10MsData"); | |
| 184 DCHECK_EQ(audio_processing_->sample_rate_hz(), | |
| 185 capture_converter_->sink_parameters().sample_rate()); | |
| 186 DCHECK_EQ(audio_processing_->num_input_channels(), | |
| 187 capture_converter_->sink_parameters().channels()); | |
| 188 DCHECK_EQ(audio_processing_->num_output_channels(), | |
| 189 capture_converter_->sink_parameters().channels()); | |
| 190 | |
| 191 int total_delay_ms = 0; | |
| 192 { | |
| 193 base::AutoLock auto_lock(lock_); | |
| 194 total_delay_ms = capture_audio_delay_ms + render_delay_ms_; | |
| 195 } | |
| 196 | |
| 197 audio_processing_->set_stream_delay_ms(total_delay_ms); | |
| 198 webrtc::GainControl* agc = audio_processing_->gain_control(); | |
| 199 if (agc->set_stream_analog_level(volume)) | |
| 200 NOTREACHED(); | |
| 201 int err = audio_processing_->ProcessStream( | |
| 202 capture_converter_->audio_frame()); | |
| 203 if (err) { | |
| 204 NOTREACHED() << "ProcessStream() error: " << err; | |
| 205 } | |
| 206 | |
| 207 // TODO(xians): Add support for AGC, typing detectin, audio level calculation, | |
| 208 // stereo swapping. | |
| 209 } | |
| 210 | |
| 211 void WebRtcAudioProcessor::PushRenderData( | |
| 212 const int16* render_audio, int sample_rate, int number_of_channels, | |
| 213 int number_of_frames, int render_delay_ms) { | |
| 214 // Return immediately if the echo cancellation is off. | |
| 215 if (!audio_processing_ || | |
| 216 !audio_processing_->echo_cancellation()->is_enabled()) | |
| 217 return; | |
| 218 | |
| 219 TRACE_EVENT0("audio", | |
| 220 "WebRtcAudioProcessor::FeedRenderDataToAudioProcessing"); | |
| 221 { | |
| 222 base::AutoLock auto_lock(lock_); | |
| 223 render_delay_ms_ = render_delay_ms; | |
| 224 } | |
| 225 | |
| 226 InitializeRenderConverterIfNeeded(sample_rate, number_of_channels, | |
| 227 number_of_frames); | |
| 228 | |
| 229 // TODO(xians): Avoid this extra interleave/deinterleave. | |
| 230 render_data_bus_->FromInterleaved(render_audio, | |
| 231 render_data_bus_->frames(), | |
| 232 sizeof(render_audio[0])); | |
| 233 render_converter_->Push(render_data_bus_.get()); | |
| 234 while (render_converter_->Convert()) { | |
| 235 audio_processing_->AnalyzeReverseStream(render_converter_->audio_frame()); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void WebRtcAudioProcessor::InitializeAudioProcessingModule( | |
| 240 const webrtc::MediaConstraintsInterface* constraints) { | |
| 241 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 242 switches::kEnableAudioTrackProcessing)) { | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 if (!constraints) | |
| 247 return; | |
| 248 | |
| 249 const bool enable_aec = GetPropertyFromConstraints( | |
| 250 constraints, MediaConstraintsInterface::kEchoCancellation); | |
| 251 const bool enable_ns = GetPropertyFromConstraints( | |
| 252 constraints, MediaConstraintsInterface::kNoiseSuppression); | |
| 253 const bool enable_high_pass_filter = GetPropertyFromConstraints( | |
| 254 constraints, MediaConstraintsInterface::kHighpassFilter); | |
| 255 const bool start_aec_dump = GetPropertyFromConstraints( | |
| 256 constraints, MediaConstraintsInterface::kInternalAecDump); | |
| 257 #if defined(IOS) || defined(ANDROID) | |
| 258 const bool enable_experimental_aec = false; | |
| 259 const bool enable_typing_detection = false; | |
| 260 #else | |
| 261 const bool enable_experimental_aec = GetPropertyFromConstraints( | |
| 262 constraints, MediaConstraintsInterface::kExperimentalEchoCancellation); | |
| 263 const bool enable_typing_detection = GetPropertyFromConstraints( | |
| 264 constraints, MediaConstraintsInterface::kTypingNoiseDetection); | |
| 265 #endif | |
| 266 | |
| 267 // Reset the audio processing to NULL if no audio processing component is | |
| 268 // enabled. | |
| 269 if (!enable_aec && !enable_experimental_aec && !enable_ns && | |
| 270 !enable_high_pass_filter && !enable_typing_detection) { | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 // Create and configure the audio processing if it does not exist. | |
| 275 if (!audio_processing_) | |
| 276 audio_processing_.reset(webrtc::AudioProcessing::Create(0)); | |
| 277 | |
| 278 // Enable the audio processing components. | |
| 279 if (enable_aec) { | |
| 280 EnableEchoCancellation(audio_processing_.get()); | |
| 281 | |
| 282 if (enable_experimental_aec) | |
| 283 EnableExperimentalEchoCancellation(audio_processing_.get()); | |
| 284 } | |
| 285 | |
| 286 if (enable_ns) | |
| 287 EnableNoiseSuppression(audio_processing_.get()); | |
| 288 | |
| 289 if (enable_high_pass_filter) | |
| 290 EnableHighPassFilter(audio_processing_.get()); | |
| 291 | |
| 292 if (enable_typing_detection) | |
| 293 EnableTypingDetection(audio_processing_.get()); | |
| 294 | |
| 295 if (enable_aec && start_aec_dump) | |
| 296 StartAecDump(audio_processing_.get()); | |
| 297 | |
| 298 // Configure the audio format the audio processing is running on. This | |
| 299 // has to be done after all the needed components are enabled. | |
| 300 if (audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate)) | |
| 301 NOTREACHED(); | |
| 302 if (audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, | |
| 303 kAudioProcessingNumberOfChannel)) | |
| 304 NOTREACHED(); | |
| 305 } | |
| 306 | |
| 307 void WebRtcAudioProcessor::InitializeRenderConverterIfNeeded( | |
| 308 int sample_rate, int number_of_channels, int frames_per_buffer) { | |
| 309 // TODO, figure out if we need to handle the buffer size change. | |
| 310 if (render_converter_.get() && | |
| 311 render_converter_->source_parameters().sample_rate() == sample_rate && | |
| 312 render_converter_->source_parameters().channels() == number_of_channels) { | |
| 313 // Do nothing if the |render_converter_| has been setup properly. | |
| 314 return; | |
| 315 } | |
| 316 | |
| 317 media::AudioParameters source_params( | |
| 318 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 319 media::GuessChannelLayout(number_of_channels), sample_rate, 16, | |
| 320 frames_per_buffer); | |
| 321 media::AudioParameters sink_params( | |
| 322 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 323 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16, | |
| 324 kAudioProcessingSampleRate / 100); | |
| 325 render_converter_.reset(new WebRtcAudioConverter(source_params, sink_params)); | |
| 326 render_data_bus_ = media::AudioBus::Create(number_of_channels, | |
| 327 frames_per_buffer); | |
| 328 } | |
| 329 | |
| 330 void WebRtcAudioProcessor::StopAudioProcessing() { | |
| 331 if (!audio_processing_.get()) | |
| 332 return; | |
| 333 | |
| 334 // It is safe to stop the AEC dump even it is not started. | |
| 335 StopAecDump(audio_processing_.get()); | |
| 336 | |
| 337 audio_processing_.reset(); | |
| 338 } | |
| 339 | |
| 340 } // namespace content | |
| OLD | NEW |