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/media_stream_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/media_stream_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 MediaStreamAudioProcessor::MediaStreamAudioConverter |
| 35 : public media::AudioConverter::InputCallback { |
| 36 public: |
| 37 MediaStreamAudioConverter(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 ~MediaStreamAudioConverter() { |
| 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 // |MediaStreamAudioProcessor::capture_converter_|, and render audio thread |
| 63 // for |MediaStreamAudioProcessor::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 // |MediaStreamAudioProcessor::capture_converter_|, and render audio thread |
| 72 // for |MediaStreamAudioProcessor::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 |
| 115 // Return 1.0 to indicate no volume scaling on the data. |
| 116 return 1.0; |
| 117 } |
| 118 |
| 119 base::ThreadChecker thread_checker_; |
| 120 const media::AudioParameters source_params_; |
| 121 const media::AudioParameters sink_params_; |
| 122 |
| 123 // TODO(xians): consider using SincResampler to save some memcpy. |
| 124 // Handles mixing and resampling between input and output parameters. |
| 125 media::AudioConverter audio_converter_; |
| 126 scoped_ptr<media::AudioBus> audio_wrapper_; |
| 127 scoped_ptr<media::AudioFifo> fifo_; |
| 128 }; |
| 129 |
| 130 MediaStreamAudioProcessor::MediaStreamAudioProcessor( |
| 131 const webrtc::MediaConstraintsInterface* constraints) |
| 132 : render_delay_ms_(0) { |
| 133 capture_thread_checker_.DetachFromThread(); |
| 134 render_thread_checker_.DetachFromThread(); |
| 135 InitializeAudioProcessingModule(constraints); |
| 136 } |
| 137 |
| 138 MediaStreamAudioProcessor::~MediaStreamAudioProcessor() { |
| 139 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 140 StopAudioProcessing(); |
| 141 } |
| 142 |
| 143 void MediaStreamAudioProcessor::PushCaptureData(media::AudioBus* audio_source) { |
| 144 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 145 capture_converter_->Push(audio_source); |
| 146 } |
| 147 |
| 148 void MediaStreamAudioProcessor::PushRenderData( |
| 149 const int16* render_audio, int sample_rate, int number_of_channels, |
| 150 int number_of_frames, base::TimeDelta render_delay) { |
| 151 DCHECK(render_thread_checker_.CalledOnValidThread()); |
| 152 |
| 153 // Return immediately if the echo cancellation is off. |
| 154 if (!audio_processing_ || |
| 155 !audio_processing_->echo_cancellation()->is_enabled()) { |
| 156 return; |
| 157 } |
| 158 |
| 159 TRACE_EVENT0("audio", |
| 160 "MediaStreamAudioProcessor::FeedRenderDataToAudioProcessing"); |
| 161 int64 new_render_delay_ms = render_delay.InMilliseconds(); |
| 162 DCHECK_LT(new_render_delay_ms, |
| 163 std::numeric_limits<base::subtle::Atomic32>::max()); |
| 164 base::subtle::Release_Store(&render_delay_ms_, new_render_delay_ms); |
| 165 |
| 166 InitializeRenderConverterIfNeeded(sample_rate, number_of_channels, |
| 167 number_of_frames); |
| 168 |
| 169 // TODO(xians): Avoid this extra interleave/deinterleave. |
| 170 render_data_bus_->FromInterleaved(render_audio, |
| 171 render_data_bus_->frames(), |
| 172 sizeof(render_audio[0])); |
| 173 render_converter_->Push(render_data_bus_.get()); |
| 174 while (render_converter_->Convert(&render_frame_)) |
| 175 audio_processing_->AnalyzeReverseStream(&render_frame_); |
| 176 } |
| 177 |
| 178 bool MediaStreamAudioProcessor::ProcessAndConsumeData( |
| 179 base::TimeDelta capture_delay, int volume, bool key_pressed, |
| 180 int16** out) { |
| 181 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 182 TRACE_EVENT0("audio", |
| 183 "MediaStreamAudioProcessor::ProcessAndConsumeData"); |
| 184 |
| 185 if (!capture_converter_->Convert(&capture_frame_)) |
| 186 return false; |
| 187 |
| 188 ProcessData(&capture_frame_, capture_delay, volume, key_pressed); |
| 189 *out = capture_frame_.data_; |
| 190 |
| 191 return true; |
| 192 } |
| 193 |
| 194 void MediaStreamAudioProcessor::SetCaptureFormat( |
| 195 const media::AudioParameters& source_params) { |
| 196 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 197 DCHECK(source_params.IsValid()); |
| 198 |
| 199 // Create and initialize audio converter for the source data. |
| 200 // When the webrtc AudioProcessing is enabled, the sink format of the |
| 201 // converter will be the same as the post-processed data format, which is |
| 202 // 32k mono for desktops and 16k mono for Android. When the AudioProcessing |
| 203 // is disabled, the sink format will be the same as the source format. |
| 204 const int sink_sample_rate = audio_processing_ ? |
| 205 kAudioProcessingSampleRate : source_params.sample_rate(); |
| 206 const media::ChannelLayout sink_channel_layout = audio_processing_ ? |
| 207 media::CHANNEL_LAYOUT_MONO : source_params.channel_layout(); |
| 208 |
| 209 // WebRtc is using 10ms data as its native packet size. |
| 210 media::AudioParameters sink_params( |
| 211 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, sink_channel_layout, |
| 212 sink_sample_rate, 16, sink_sample_rate / 100); |
| 213 capture_converter_.reset( |
| 214 new MediaStreamAudioConverter(source_params, sink_params)); |
| 215 } |
| 216 |
| 217 const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const { |
| 218 return capture_converter_->sink_parameters(); |
| 219 } |
| 220 |
| 221 void MediaStreamAudioProcessor::InitializeAudioProcessingModule( |
| 222 const webrtc::MediaConstraintsInterface* constraints) { |
| 223 DCHECK(!audio_processing_); |
| 224 DCHECK(constraints); |
| 225 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 226 switches::kEnableAudioTrackProcessing)) { |
| 227 return; |
| 228 } |
| 229 |
| 230 const bool enable_aec = GetPropertyFromConstraints( |
| 231 constraints, MediaConstraintsInterface::kEchoCancellation); |
| 232 const bool enable_ns = GetPropertyFromConstraints( |
| 233 constraints, MediaConstraintsInterface::kNoiseSuppression); |
| 234 const bool enable_high_pass_filter = GetPropertyFromConstraints( |
| 235 constraints, MediaConstraintsInterface::kHighpassFilter); |
| 236 const bool start_aec_dump = GetPropertyFromConstraints( |
| 237 constraints, MediaConstraintsInterface::kInternalAecDump); |
| 238 #if defined(IOS) || defined(ANDROID) |
| 239 const bool enable_experimental_aec = false; |
| 240 const bool enable_typing_detection = false; |
| 241 #else |
| 242 const bool enable_experimental_aec = GetPropertyFromConstraints( |
| 243 constraints, MediaConstraintsInterface::kExperimentalEchoCancellation); |
| 244 const bool enable_typing_detection = GetPropertyFromConstraints( |
| 245 constraints, MediaConstraintsInterface::kTypingNoiseDetection); |
| 246 #endif |
| 247 |
| 248 // Return immediately if no audio processing component is enabled. |
| 249 if (!enable_aec && !enable_experimental_aec && !enable_ns && |
| 250 !enable_high_pass_filter && !enable_typing_detection) { |
| 251 return; |
| 252 } |
| 253 |
| 254 // Create and configure the webrtc::AudioProcessing. |
| 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 CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate), |
| 279 0); |
| 280 CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel, |
| 281 kAudioProcessingNumberOfChannel), |
| 282 0); |
| 283 } |
| 284 |
| 285 void MediaStreamAudioProcessor::InitializeRenderConverterIfNeeded( |
| 286 int sample_rate, int number_of_channels, int frames_per_buffer) { |
| 287 DCHECK(render_thread_checker_.CalledOnValidThread()); |
| 288 // TODO(xians): Figure out if we need to handle the buffer size change. |
| 289 if (render_converter_.get() && |
| 290 render_converter_->source_parameters().sample_rate() == sample_rate && |
| 291 render_converter_->source_parameters().channels() == number_of_channels) { |
| 292 // Do nothing if the |render_converter_| has been setup properly. |
| 293 return; |
| 294 } |
| 295 |
| 296 // Create and initialize audio converter for the render data. |
| 297 // webrtc::AudioProcessing accepts the same format as what it uses to process |
| 298 // capture data, which is 32k mono for desktops and 16k mono for Android. |
| 299 media::AudioParameters source_params( |
| 300 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 301 media::GuessChannelLayout(number_of_channels), sample_rate, 16, |
| 302 frames_per_buffer); |
| 303 media::AudioParameters sink_params( |
| 304 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 305 media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16, |
| 306 kAudioProcessingSampleRate / 100); |
| 307 render_converter_.reset( |
| 308 new MediaStreamAudioConverter(source_params, sink_params)); |
| 309 render_data_bus_ = media::AudioBus::Create(number_of_channels, |
| 310 frames_per_buffer); |
| 311 } |
| 312 |
| 313 void MediaStreamAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame, |
| 314 base::TimeDelta capture_delay, |
| 315 int volume, |
| 316 bool key_pressed) { |
| 317 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 318 if (!audio_processing_) |
| 319 return; |
| 320 |
| 321 TRACE_EVENT0("audio", "MediaStreamAudioProcessor::Process10MsData"); |
| 322 DCHECK_EQ(audio_processing_->sample_rate_hz(), |
| 323 capture_converter_->sink_parameters().sample_rate()); |
| 324 DCHECK_EQ(audio_processing_->num_input_channels(), |
| 325 capture_converter_->sink_parameters().channels()); |
| 326 DCHECK_EQ(audio_processing_->num_output_channels(), |
| 327 capture_converter_->sink_parameters().channels()); |
| 328 |
| 329 base::subtle::Atomic32 render_delay_ms = |
| 330 base::subtle::Acquire_Load(&render_delay_ms_); |
| 331 int64 capture_delay_ms = capture_delay.InMilliseconds(); |
| 332 DCHECK_LT(capture_delay_ms, |
| 333 std::numeric_limits<base::subtle::Atomic32>::max()); |
| 334 int total_delay_ms = capture_delay_ms + render_delay_ms; |
| 335 if (total_delay_ms > 1000) { |
| 336 LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms |
| 337 << "ms; render delay: " << render_delay_ms << "ms"; |
| 338 } |
| 339 |
| 340 audio_processing_->set_stream_delay_ms(total_delay_ms); |
| 341 webrtc::GainControl* agc = audio_processing_->gain_control(); |
| 342 int err = agc->set_stream_analog_level(volume); |
| 343 DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err; |
| 344 err = audio_processing_->ProcessStream(audio_frame); |
| 345 DCHECK_EQ(err, 0) << "ProcessStream() error: " << err; |
| 346 |
| 347 // TODO(xians): Add support for AGC, typing detection, audio level |
| 348 // calculation, stereo swapping. |
| 349 } |
| 350 |
| 351 void MediaStreamAudioProcessor::StopAudioProcessing() { |
| 352 if (!audio_processing_.get()) |
| 353 return; |
| 354 |
| 355 // It is safe to stop the AEC dump even it is not started. |
| 356 StopAecDump(audio_processing_.get()); |
| 357 |
| 358 audio_processing_.reset(); |
| 359 } |
| 360 |
| 361 } // namespace content |
OLD | NEW |