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_options.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)); | |
| 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 // Called on the audio thread, which is the capture audio thread for | |
| 62 // |WebRtcAudioProcessor::capture_converter_|, and render audio thread for | |
| 63 // |WebRtcAudioProcessor::render_converter_|. | |
| 64 // And it must be the same thread as calling Convert(). | |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 66 fifo_->Push(audio_source); | |
| 67 } | |
| 68 | |
| 69 bool Convert(webrtc::AudioFrame* out) { | |
| 70 // Called on the audio thread, which is the capture audio thread for | |
| 71 // |WebRtcAudioProcessor::capture_converter_|, and render audio thread for | |
| 72 // |WebRtcAudioProcessor::render_converter_|. | |
| 73 // Return false if there is no 10ms data in the FIFO. | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 75 if (fifo_->frames() < (source_params_.sample_rate() / 100)) | |
| 76 return false; | |
| 77 | |
| 78 // Convert 10ms data to the output format, this will trigger ProvideInput(). | |
| 79 audio_converter_.Convert(audio_wrapper_.get()); | |
| 80 | |
| 81 // TODO(xians): Figure out a better way to handle the interleaved and | |
| 82 // deinterleaved format switching. | |
| 83 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(), | |
| 84 sink_params_.bits_per_sample() / 8, | |
| 85 out->data_); | |
| 86 | |
| 87 out->samples_per_channel_ = sink_params_.frames_per_buffer(); | |
| 88 out->sample_rate_hz_ = sink_params_.sample_rate(); | |
| 89 out->speech_type_ = webrtc::AudioFrame::kNormalSpeech; | |
| 90 out->vad_activity_ = webrtc::AudioFrame::kVadUnknown; | |
| 91 out->num_channels_ = sink_params_.channels(); | |
| 92 | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 const media::AudioParameters& source_parameters() const { | |
| 97 return source_params_; | |
| 98 } | |
| 99 const media::AudioParameters& sink_parameters() const { | |
| 100 return sink_params_; | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 // AudioConverter::InputCallback implementation. | |
| 105 virtual double ProvideInput(media::AudioBus* audio_bus, | |
| 106 base::TimeDelta buffer_delay) OVERRIDE { | |
| 107 // Called on realtime audio thread. | |
| 108 // TODO(xians): Figure out why the first Convert() triggers ProvideInput | |
| 109 // two times. | |
| 110 if (fifo_->frames() < audio_bus->frames()) | |
| 111 return 0; | |
| 112 | |
| 113 fifo_->Consume(audio_bus, 0, audio_bus->frames()); | |
| 114 return 1.0; | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
add a note that explains this return value?
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 115 } | |
| 116 | |
| 117 base::ThreadChecker thread_checker_; | |
| 118 media::AudioParameters source_params_; | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
if these parameters (sink and source) do not chang
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 119 media::AudioParameters sink_params_; | |
| 120 | |
| 121 // TODO(xians): consider using SincResampler to save some memcpy. | |
| 122 // Handles mixing and resampling between input and output parameters. | |
| 123 media::AudioConverter audio_converter_; | |
| 124 scoped_ptr<media::AudioBus> audio_wrapper_; | |
| 125 scoped_ptr<media::AudioFifo> fifo_; | |
| 126 }; | |
| 127 | |
| 128 WebRtcAudioProcessor::WebRtcAudioProcessor( | |
| 129 const webrtc::MediaConstraintsInterface* constraints) | |
| 130 : render_delay_ms_(0) { | |
| 131 capture_thread_checker_.DetachFromThread(); | |
| 132 render_thread_checker_.DetachFromThread(); | |
| 133 InitializeAudioProcessingModule(constraints); | |
| 134 } | |
| 135 | |
| 136 WebRtcAudioProcessor::~WebRtcAudioProcessor() { | |
| 137 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 138 StopAudioProcessing(); | |
| 139 } | |
| 140 | |
| 141 void WebRtcAudioProcessor::PushCaptureData(media::AudioBus* audio_source) { | |
| 142 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
| 143 capture_converter_->Push(audio_source); | |
| 144 } | |
| 145 | |
| 146 void WebRtcAudioProcessor::PushRenderData( | |
| 147 const int16* render_audio, int sample_rate, int number_of_channels, | |
| 148 int number_of_frames, base::TimeDelta render_delay) { | |
| 149 DCHECK(render_thread_checker_.CalledOnValidThread()); | |
| 150 | |
| 151 // Return immediately if the echo cancellation is off. | |
| 152 if (!audio_processing_ || | |
| 153 !audio_processing_->echo_cancellation()->is_enabled()) | |
| 154 return; | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
{}
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 155 | |
| 156 TRACE_EVENT0("audio", | |
| 157 "WebRtcAudioProcessor::FeedRenderDataToAudioProcessing"); | |
| 158 int64 new_render_delay_ms = render_delay.InMilliseconds(); | |
| 159 DCHECK_LT(new_render_delay_ms, | |
| 160 std::numeric_limits<base::subtle::Atomic32>::max()); | |
| 161 base::subtle::Release_Store(&render_delay_ms_, new_render_delay_ms); | |
| 162 | |
| 163 InitializeRenderConverterIfNeeded(sample_rate, number_of_channels, | |
| 164 number_of_frames); | |
| 165 | |
| 166 // TODO(xians): Avoid this extra interleave/deinterleave. | |
| 167 render_data_bus_->FromInterleaved(render_audio, | |
| 168 render_data_bus_->frames(), | |
| 169 sizeof(render_audio[0])); | |
| 170 render_converter_->Push(render_data_bus_.get()); | |
| 171 while (render_converter_->Convert(&render_frame_)) { | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
no {}
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 172 audio_processing_->AnalyzeReverseStream(&render_frame_); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 bool WebRtcAudioProcessor::ProcessAndConsumeData( | |
| 177 base::TimeDelta capture_delay, int volume, bool key_pressed, | |
| 178 int16** out) { | |
| 179 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
| 180 TRACE_EVENT0("audio", | |
| 181 "WebRtcAudioProcessor::ProcessAndConsumeData"); | |
| 182 | |
| 183 if (!capture_converter_->Convert(&capture_frame_)) | |
| 184 return false; | |
| 185 | |
| 186 ProcessData(&capture_frame_, capture_delay, volume, key_pressed); | |
| 187 *out = capture_frame_.data_; | |
| 188 | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 void WebRtcAudioProcessor::SetCaptureFormat( | |
| 193 const media::AudioParameters& source_params) { | |
| 194 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
| 195 DCHECK(source_params.IsValid()); | |
| 196 | |
| 197 // Create and initialize audio converter for the source data. | |
| 198 // When the webrtc AudioProcessing is enabled, the sink format of the | |
| 199 // converter will be the same as the post-processed data format, which is | |
| 200 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing | |
| 201 // is disabled, the sink format will be the same as the source format. | |
| 202 const int sink_sample_rate = audio_processing_ ? | |
| 203 kAudioProcessingSampleRate : source_params.sample_rate(); | |
| 204 const media::ChannelLayout sink_channel_layout = audio_processing_ ? | |
| 205 media::CHANNEL_LAYOUT_MONO : source_params.channel_layout(); | |
| 206 | |
| 207 // WebRtc is using 10ms data as its native packet size. | |
| 208 media::AudioParameters sink_params( | |
| 209 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, sink_channel_layout, | |
| 210 sink_sample_rate, 16, sink_sample_rate / 100); | |
| 211 capture_converter_.reset( | |
| 212 new WebRtcAudioConverter(source_params, sink_params)); | |
| 213 } | |
| 214 | |
| 215 const media::AudioParameters& WebRtcAudioProcessor::OutputFormat() const { | |
| 216 return capture_converter_->sink_parameters(); | |
| 217 } | |
| 218 | |
| 219 void WebRtcAudioProcessor::InitializeAudioProcessingModule( | |
| 220 const webrtc::MediaConstraintsInterface* constraints) { | |
| 221 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 222 switches::kEnableAudioTrackProcessing)) { | |
| 223 return; | |
| 224 } | |
| 225 | |
| 226 if (!constraints) | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
when will this happen? if it's a supported use ca
no longer working on chromium
2013/11/22 13:27:53
Right, in some of the unittests which do not care
tommi (sloooow) - chröme
2013/11/22 14:32:42
Can we change those tests to send in empty constra
no longer working on chromium
2013/11/25 16:36:26
OK, I will make sure the constraints won't be NULL
| |
| 227 return; | |
| 228 | |
| 229 const bool enable_aec = GetPropertyFromConstraints( | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
should agc be included as well or is that coming l
no longer working on chromium
2013/11/22 13:27:53
It will be in the future.
| |
| 230 constraints, MediaConstraintsInterface::kEchoCancellation); | |
| 231 const bool enable_ns = GetPropertyFromConstraints( | |
| 232 constraints, MediaConstraintsInterface::kNoiseSuppression); | |
| 233 const bool enable_high_pass_filter = GetPropertyFromConstraints( | |
| 234 constraints, MediaConstraintsInterface::kHighpassFilter); | |
| 235 const bool start_aec_dump = GetPropertyFromConstraints( | |
| 236 constraints, MediaConstraintsInterface::kInternalAecDump); | |
| 237 #if defined(IOS) || defined(ANDROID) | |
| 238 const bool enable_experimental_aec = false; | |
| 239 const bool enable_typing_detection = false; | |
| 240 #else | |
| 241 const bool enable_experimental_aec = GetPropertyFromConstraints( | |
| 242 constraints, MediaConstraintsInterface::kExperimentalEchoCancellation); | |
| 243 const bool enable_typing_detection = GetPropertyFromConstraints( | |
| 244 constraints, MediaConstraintsInterface::kTypingNoiseDetection); | |
| 245 #endif | |
| 246 | |
| 247 // Return immediately if no audio processing component is enabled. | |
| 248 if (!enable_aec && !enable_experimental_aec && !enable_ns && | |
| 249 !enable_high_pass_filter && !enable_typing_detection) { | |
| 250 return; | |
| 251 } | |
| 252 | |
| 253 // Create and configure the audio processing if it does not exist. | |
| 254 if (!audio_processing_) | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
is there a case where this pointer is not NULL whe
no longer working on chromium
2013/11/22 13:27:53
Added a DCHECK instead.
| |
| 255 audio_processing_.reset(webrtc::AudioProcessing::Create(0)); | |
| 256 | |
| 257 // Enable the audio processing components. | |
| 258 if (enable_aec) { | |
| 259 EnableEchoCancellation(audio_processing_.get()); | |
| 260 if (enable_experimental_aec) | |
| 261 EnableExperimentalEchoCancellation(audio_processing_.get()); | |
| 262 } | |
| 263 | |
| 264 if (enable_ns) | |
| 265 EnableNoiseSuppression(audio_processing_.get()); | |
| 266 | |
| 267 if (enable_high_pass_filter) | |
| 268 EnableHighPassFilter(audio_processing_.get()); | |
| 269 | |
| 270 if (enable_typing_detection) | |
| 271 EnableTypingDetection(audio_processing_.get()); | |
| 272 | |
| 273 if (enable_aec && start_aec_dump) | |
| 274 StartAecDump(audio_processing_.get()); | |
| 275 | |
| 276 // Configure the audio format the audio processing is running on. This | |
| 277 // has to be done after all the needed components are enabled. | |
| 278 if (audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate)) | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
should this and the next conditional be |if (!...)
Henrik Grunell
2013/11/22 09:03:36
I think this returns an int. Maybe check != 0.
no longer working on chromium
2013/11/22 13:27:53
Use CHECK_EQ
no longer working on chromium
2013/11/22 13:27:53
Use CHECK_EQ
| |
| 279 NOTREACHED(); | |
| 280 if (audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, | |
| 281 kAudioProcessingNumberOfChannel)) | |
| 282 NOTREACHED(); | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
How you're using NOTREACHED is a bit confusing. s
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 283 } | |
| 284 | |
| 285 void WebRtcAudioProcessor::InitializeRenderConverterIfNeeded( | |
| 286 int sample_rate, int number_of_channels, int frames_per_buffer) { | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
thread check?
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 287 // TODO(xians): Figure out if we need to handle the buffer size change. | |
| 288 if (render_converter_.get() && | |
| 289 render_converter_->source_parameters().sample_rate() == sample_rate && | |
| 290 render_converter_->source_parameters().channels() == number_of_channels) { | |
| 291 // Do nothing if the |render_converter_| has been setup properly. | |
| 292 return; | |
| 293 } | |
| 294 | |
| 295 media::AudioParameters source_params( | |
| 296 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 297 media::GuessChannelLayout(number_of_channels), sample_rate, 16, | |
| 298 frames_per_buffer); | |
| 299 media::AudioParameters sink_params( | |
| 300 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 301 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16, | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
It's probably worth adding some comments here that
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 302 kAudioProcessingSampleRate / 100); | |
| 303 render_converter_.reset(new WebRtcAudioConverter(source_params, sink_params)); | |
| 304 render_data_bus_ = media::AudioBus::Create(number_of_channels, | |
| 305 frames_per_buffer); | |
| 306 } | |
| 307 | |
| 308 void WebRtcAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame, | |
| 309 base::TimeDelta capture_delay, | |
| 310 int volume, | |
| 311 bool key_pressed) { | |
| 312 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
| 313 if (!audio_processing_) | |
| 314 return; | |
| 315 | |
| 316 TRACE_EVENT0("audio", "WebRtcAudioProcessor::Process10MsData"); | |
| 317 DCHECK_EQ(audio_processing_->sample_rate_hz(), | |
| 318 capture_converter_->sink_parameters().sample_rate()); | |
| 319 DCHECK_EQ(audio_processing_->num_input_channels(), | |
| 320 capture_converter_->sink_parameters().channels()); | |
| 321 DCHECK_EQ(audio_processing_->num_output_channels(), | |
| 322 capture_converter_->sink_parameters().channels()); | |
| 323 | |
| 324 base::subtle::Atomic32 render_delay_ms = | |
| 325 base::subtle::Acquire_Load(&render_delay_ms_); | |
| 326 int64 capture_delay_ms = capture_delay.InMilliseconds(); | |
| 327 DCHECK_LT(capture_delay_ms, | |
| 328 std::numeric_limits<base::subtle::Atomic32>::max()); | |
| 329 int total_delay_ms = capture_delay_ms + render_delay_ms; | |
| 330 if (total_delay_ms > 1000) { | |
| 331 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms | |
| 332 << "ms; render delay: " << render_delay_ms << "ms"; | |
| 333 } | |
| 334 | |
| 335 audio_processing_->set_stream_delay_ms(total_delay_ms); | |
| 336 webrtc::GainControl* agc = audio_processing_->gain_control(); | |
| 337 if (agc->set_stream_analog_level(volume)) | |
| 338 NOTREACHED(); | |
|
tommi (sloooow) - chröme
2013/11/21 19:52:03
same questions as above about NOTREACHED... does s
no longer working on chromium
2013/11/22 13:27:53
Done.
| |
| 339 int err = audio_processing_->ProcessStream(audio_frame); | |
| 340 DCHECK(!err) << "ProcessStream() error: " << err; | |
| 341 | |
| 342 // TODO(xians): Add support for AGC, typing detection, audio level | |
| 343 // calculation, stereo swapping. | |
| 344 } | |
| 345 | |
| 346 void WebRtcAudioProcessor::StopAudioProcessing() { | |
| 347 if (!audio_processing_.get()) | |
| 348 return; | |
| 349 | |
| 350 // It is safe to stop the AEC dump even it is not started. | |
| 351 StopAecDump(audio_processing_.get()); | |
| 352 | |
| 353 audio_processing_.reset(); | |
| 354 } | |
| 355 | |
| 356 } // namespace content | |
| OLD | NEW |