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

Side by Side Diff: webrtc/modules/audio_processing/audio_processing_impl.cc

Issue 1422013002: Preparational work for an upcoming addition of a threadchecking scheme for APM (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@bundling_of_state_CL
Patch Set: Created 5 years, 1 month 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume), 215 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
216 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) 216 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
217 transient_suppressor_enabled_(false), 217 transient_suppressor_enabled_(false),
218 #else 218 #else
219 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled), 219 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
220 #endif 220 #endif
221 beamformer_enabled_(config.Get<Beamforming>().enabled), 221 beamformer_enabled_(config.Get<Beamforming>().enabled),
222 beamformer_(beamformer), 222 beamformer_(beamformer),
223 array_geometry_(config.Get<Beamforming>().array_geometry), 223 array_geometry_(config.Get<Beamforming>().array_geometry),
224 intelligibility_enabled_(config.Get<Intelligibility>().enabled) { 224 intelligibility_enabled_(config.Get<Intelligibility>().enabled) {
225 echo_cancellation_ = new EchoCancellationImpl(this, crit_); 225 echo_cancellation_ =
226 new EchoCancellationImpl(this, crit_, &render_thread_, &capture_thread_);
226 component_list_.push_back(echo_cancellation_); 227 component_list_.push_back(echo_cancellation_);
227 228
228 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_); 229 echo_control_mobile_ =
230 new EchoControlMobileImpl(this, crit_, &render_thread_, &capture_thread_);
229 component_list_.push_back(echo_control_mobile_); 231 component_list_.push_back(echo_control_mobile_);
230 232
231 gain_control_ = new GainControlImpl(this, crit_); 233 gain_control_ =
234 new GainControlImpl(this, crit_, &render_thread_, &capture_thread_);
232 component_list_.push_back(gain_control_); 235 component_list_.push_back(gain_control_);
233 236
234 high_pass_filter_ = new HighPassFilterImpl(this, crit_); 237 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
235 component_list_.push_back(high_pass_filter_); 238 component_list_.push_back(high_pass_filter_);
236 239
237 level_estimator_ = new LevelEstimatorImpl(this, crit_); 240 level_estimator_ = new LevelEstimatorImpl(this, crit_);
238 component_list_.push_back(level_estimator_); 241 component_list_.push_back(level_estimator_);
239 242
240 noise_suppression_ = new NoiseSuppressionImpl(this, crit_); 243 noise_suppression_ = new NoiseSuppressionImpl(this, crit_, &capture_thread_);
241 component_list_.push_back(noise_suppression_); 244 component_list_.push_back(noise_suppression_);
242 245
243 voice_detection_ = new VoiceDetectionImpl(this, crit_); 246 voice_detection_ = new VoiceDetectionImpl(this, crit_, &capture_thread_);
244 component_list_.push_back(voice_detection_); 247 component_list_.push_back(voice_detection_);
245 248
246 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_)); 249 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
247 250
248 SetExtraOptions(config); 251 SetExtraOptions(config);
252
253 render_thread_.DetachFromThread();
254 capture_thread_.DetachFromThread();
kwiberg-webrtc 2015/10/26 13:57:56 Move these as high up as possible, so that you nev
peah-webrtc 2015/11/05 11:47:33 I'm not convinced this is the right thing to do. I
kwiberg-webrtc 2015/11/08 09:50:51 Yes, that's exactly the sort of behavior you prote
249 } 255 }
250 256
251 AudioProcessingImpl::~AudioProcessingImpl() { 257 AudioProcessingImpl::~AudioProcessingImpl() {
252 { 258 {
253 CriticalSectionScoped crit_scoped(crit_); 259 CriticalSectionScoped crit_scoped(crit_);
254 // Depends on gain_control_ and gain_control_for_new_agc_. 260 // Depends on gain_control_ and gain_control_for_new_agc_.
255 agc_manager_.reset(); 261 agc_manager_.reset();
256 // Depends on gain_control_. 262 // Depends on gain_control_.
257 gain_control_for_new_agc_.reset(); 263 gain_control_for_new_agc_.reset();
258 while (!component_list_.empty()) { 264 while (!component_list_.empty()) {
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz || 451 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
446 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) { 452 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
447 split_rate_ = kSampleRate16kHz; 453 split_rate_ = kSampleRate16kHz;
448 } else { 454 } else {
449 split_rate_ = fwd_proc_format_.sample_rate_hz(); 455 split_rate_ = fwd_proc_format_.sample_rate_hz();
450 } 456 }
451 457
452 return InitializeLocked(); 458 return InitializeLocked();
453 } 459 }
454 460
455
456 void AudioProcessingImpl::SetExtraOptions(const Config& config) { 461 void AudioProcessingImpl::SetExtraOptions(const Config& config) {
457 CriticalSectionScoped crit_scoped(crit_); 462 CriticalSectionScoped crit_scoped(crit_);
458 for (auto item : component_list_) { 463 for (auto item : component_list_) {
459 item->SetExtraOptions(config); 464 item->SetExtraOptions(config);
460 } 465 }
461 466
462 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) { 467 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
463 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled; 468 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
464 InitializeTransient(); 469 InitializeTransient();
465 } 470 }
(...skipping 15 matching lines...) Expand all
481 int AudioProcessingImpl::num_input_channels() const { 486 int AudioProcessingImpl::num_input_channels() const {
482 return shared_state_.api_format_.input_stream().num_channels(); 487 return shared_state_.api_format_.input_stream().num_channels();
483 } 488 }
484 489
485 int AudioProcessingImpl::num_output_channels() const { 490 int AudioProcessingImpl::num_output_channels() const {
486 return shared_state_.api_format_.output_stream().num_channels(); 491 return shared_state_.api_format_.output_stream().num_channels();
487 } 492 }
488 493
489 void AudioProcessingImpl::set_output_will_be_muted(bool muted) { 494 void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
490 CriticalSectionScoped lock(crit_); 495 CriticalSectionScoped lock(crit_);
496 RTC_DCHECK(capture_thread_.CalledOnValidThread());
491 output_will_be_muted_ = muted; 497 output_will_be_muted_ = muted;
492 if (agc_manager_.get()) { 498 if (agc_manager_.get()) {
493 agc_manager_->SetCaptureMuted(output_will_be_muted_); 499 agc_manager_->SetCaptureMuted(output_will_be_muted_);
494 } 500 }
495 } 501 }
496 502
497 503
498 int AudioProcessingImpl::ProcessStream(const float* const* src, 504 int AudioProcessingImpl::ProcessStream(const float* const* src,
499 size_t samples_per_channel, 505 size_t samples_per_channel,
500 int input_sample_rate_hz, 506 int input_sample_rate_hz,
501 ChannelLayout input_layout, 507 ChannelLayout input_layout,
502 int output_sample_rate_hz, 508 int output_sample_rate_hz,
503 ChannelLayout output_layout, 509 ChannelLayout output_layout,
504 float* const* dest) { 510 float* const* dest) {
505 CriticalSectionScoped crit_scoped(crit_); 511 CriticalSectionScoped crit_scoped(crit_);
512 RTC_DCHECK(capture_thread_.CalledOnValidThread());
506 StreamConfig input_stream = shared_state_.api_format_.input_stream(); 513 StreamConfig input_stream = shared_state_.api_format_.input_stream();
507 input_stream.set_sample_rate_hz(input_sample_rate_hz); 514 input_stream.set_sample_rate_hz(input_sample_rate_hz);
508 input_stream.set_num_channels(ChannelsFromLayout(input_layout)); 515 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
509 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout)); 516 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
510 517
511 StreamConfig output_stream = shared_state_.api_format_.output_stream(); 518 StreamConfig output_stream = shared_state_.api_format_.output_stream();
512 output_stream.set_sample_rate_hz(output_sample_rate_hz); 519 output_stream.set_sample_rate_hz(output_sample_rate_hz);
513 output_stream.set_num_channels(ChannelsFromLayout(output_layout)); 520 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
514 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout)); 521 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
515 522
516 if (samples_per_channel != input_stream.num_frames()) { 523 if (samples_per_channel != input_stream.num_frames()) {
517 return kBadDataLengthError; 524 return kBadDataLengthError;
518 } 525 }
519 return ProcessStream(src, input_stream, output_stream, dest); 526 return ProcessStream(src, input_stream, output_stream, dest);
520 } 527 }
521 528
522 int AudioProcessingImpl::ProcessStream(const float* const* src, 529 int AudioProcessingImpl::ProcessStream(const float* const* src,
523 const StreamConfig& input_config, 530 const StreamConfig& input_config,
524 const StreamConfig& output_config, 531 const StreamConfig& output_config,
525 float* const* dest) { 532 float* const* dest) {
526 CriticalSectionScoped crit_scoped(crit_); 533 CriticalSectionScoped crit_scoped(crit_);
534 RTC_DCHECK(capture_thread_.CalledOnValidThread());
527 if (!src || !dest) { 535 if (!src || !dest) {
528 return kNullPointerError; 536 return kNullPointerError;
529 } 537 }
530 538
531 echo_cancellation_->ReadQueuedRenderData(); 539 echo_cancellation_->ReadQueuedRenderData();
532 echo_control_mobile_->ReadQueuedRenderData(); 540 echo_control_mobile_->ReadQueuedRenderData();
533 gain_control_->ReadQueuedRenderData(); 541 gain_control_->ReadQueuedRenderData();
534 542
535 ProcessingConfig processing_config = shared_state_.api_format_; 543 ProcessingConfig processing_config = shared_state_.api_format_;
536 processing_config.input_stream() = input_config; 544 processing_config.input_stream() = input_config;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 msg->add_output_channel(dest[i], channel_size); 576 msg->add_output_channel(dest[i], channel_size);
569 RETURN_ON_ERR(WriteMessageToDebugFile()); 577 RETURN_ON_ERR(WriteMessageToDebugFile());
570 } 578 }
571 #endif 579 #endif
572 580
573 return kNoError; 581 return kNoError;
574 } 582 }
575 583
576 int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { 584 int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
577 CriticalSectionScoped crit_scoped(crit_); 585 CriticalSectionScoped crit_scoped(crit_);
586 RTC_DCHECK(capture_thread_.CalledOnValidThread());
578 echo_cancellation_->ReadQueuedRenderData(); 587 echo_cancellation_->ReadQueuedRenderData();
579 echo_control_mobile_->ReadQueuedRenderData(); 588 echo_control_mobile_->ReadQueuedRenderData();
580 gain_control_->ReadQueuedRenderData(); 589 gain_control_->ReadQueuedRenderData();
581 590
582 if (!frame) { 591 if (!frame) {
583 return kNullPointerError; 592 return kNullPointerError;
584 } 593 }
585 // Must be a native rate. 594 // Must be a native rate.
586 if (frame->sample_rate_hz_ != kSampleRate8kHz && 595 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
587 frame->sample_rate_hz_ != kSampleRate16kHz && 596 frame->sample_rate_hz_ != kSampleRate16kHz &&
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 RETURN_ON_ERR(level_estimator_->ProcessStream(ca)); 721 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
713 722
714 was_stream_delay_set_ = false; 723 was_stream_delay_set_ = false;
715 return kNoError; 724 return kNoError;
716 } 725 }
717 726
718 int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, 727 int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
719 size_t samples_per_channel, 728 size_t samples_per_channel,
720 int rev_sample_rate_hz, 729 int rev_sample_rate_hz,
721 ChannelLayout layout) { 730 ChannelLayout layout) {
731 RTC_DCHECK(render_thread_.CalledOnValidThread());
722 const StreamConfig reverse_config = { 732 const StreamConfig reverse_config = {
723 rev_sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout), 733 rev_sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
724 }; 734 };
725 if (samples_per_channel != reverse_config.num_frames()) { 735 if (samples_per_channel != reverse_config.num_frames()) {
726 return kBadDataLengthError; 736 return kBadDataLengthError;
727 } 737 }
728 return AnalyzeReverseStream(data, reverse_config, reverse_config); 738 return AnalyzeReverseStream(data, reverse_config, reverse_config);
729 } 739 }
730 740
731 int AudioProcessingImpl::ProcessReverseStream( 741 int AudioProcessingImpl::ProcessReverseStream(
732 const float* const* src, 742 const float* const* src,
733 const StreamConfig& reverse_input_config, 743 const StreamConfig& reverse_input_config,
734 const StreamConfig& reverse_output_config, 744 const StreamConfig& reverse_output_config,
735 float* const* dest) { 745 float* const* dest) {
746 RTC_DCHECK(render_thread_.CalledOnValidThread());
736 RETURN_ON_ERR( 747 RETURN_ON_ERR(
737 AnalyzeReverseStream(src, reverse_input_config, reverse_output_config)); 748 AnalyzeReverseStream(src, reverse_input_config, reverse_output_config));
738 if (is_rev_processed()) { 749 if (is_rev_processed()) {
739 render_audio_->CopyTo(shared_state_.api_format_.reverse_output_stream(), 750 render_audio_->CopyTo(shared_state_.api_format_.reverse_output_stream(),
740 dest); 751 dest);
741 } else if (rev_conversion_needed()) { 752 } else if (rev_conversion_needed()) {
742 render_converter_->Convert(src, reverse_input_config.num_samples(), dest, 753 render_converter_->Convert(src, reverse_input_config.num_samples(), dest,
743 reverse_output_config.num_samples()); 754 reverse_output_config.num_samples());
744 } else { 755 } else {
745 CopyAudioIfNeeded(src, reverse_input_config.num_frames(), 756 CopyAudioIfNeeded(src, reverse_input_config.num_frames(),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 RETURN_ON_ERR(WriteMessageToDebugFile()); 795 RETURN_ON_ERR(WriteMessageToDebugFile());
785 } 796 }
786 #endif 797 #endif
787 798
788 render_audio_->CopyFrom(src, 799 render_audio_->CopyFrom(src,
789 shared_state_.api_format_.reverse_input_stream()); 800 shared_state_.api_format_.reverse_input_stream());
790 return ProcessReverseStreamLocked(); 801 return ProcessReverseStreamLocked();
791 } 802 }
792 803
793 int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) { 804 int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
805 RTC_DCHECK(render_thread_.CalledOnValidThread());
794 RETURN_ON_ERR(AnalyzeReverseStream(frame)); 806 RETURN_ON_ERR(AnalyzeReverseStream(frame));
795 if (is_rev_processed()) { 807 if (is_rev_processed()) {
796 render_audio_->InterleaveTo(frame, true); 808 render_audio_->InterleaveTo(frame, true);
797 } 809 }
798 810
799 return kNoError; 811 return kNoError;
800 } 812 }
801 813
802 int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { 814 int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
815 RTC_DCHECK(render_thread_.CalledOnValidThread());
803 CriticalSectionScoped crit_scoped(crit_); 816 CriticalSectionScoped crit_scoped(crit_);
804 if (frame == NULL) { 817 if (frame == NULL) {
805 return kNullPointerError; 818 return kNullPointerError;
806 } 819 }
807 // Must be a native rate. 820 // Must be a native rate.
808 if (frame->sample_rate_hz_ != kSampleRate8kHz && 821 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
809 frame->sample_rate_hz_ != kSampleRate16kHz && 822 frame->sample_rate_hz_ != kSampleRate16kHz &&
810 frame->sample_rate_hz_ != kSampleRate32kHz && 823 frame->sample_rate_hz_ != kSampleRate32kHz &&
811 frame->sample_rate_hz_ != kSampleRate48kHz) { 824 frame->sample_rate_hz_ != kSampleRate48kHz) {
812 return kBadSampleRateError; 825 return kBadSampleRateError;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 883
871 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz && 884 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz &&
872 is_rev_processed()) { 885 is_rev_processed()) {
873 ra->MergeFrequencyBands(); 886 ra->MergeFrequencyBands();
874 } 887 }
875 888
876 return kNoError; 889 return kNoError;
877 } 890 }
878 891
879 int AudioProcessingImpl::set_stream_delay_ms(int delay) { 892 int AudioProcessingImpl::set_stream_delay_ms(int delay) {
893 RTC_DCHECK(capture_thread_.CalledOnValidThread());
880 Error retval = kNoError; 894 Error retval = kNoError;
881 was_stream_delay_set_ = true; 895 was_stream_delay_set_ = true;
882 delay += delay_offset_ms_; 896 delay += delay_offset_ms_;
883 897
884 if (delay < 0) { 898 if (delay < 0) {
885 delay = 0; 899 delay = 0;
886 retval = kBadStreamParameterWarning; 900 retval = kBadStreamParameterWarning;
887 } 901 }
888 902
889 // TODO(ajm): the max is rather arbitrarily chosen; investigate. 903 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
890 if (delay > 500) { 904 if (delay > 500) {
891 delay = 500; 905 delay = 500;
892 retval = kBadStreamParameterWarning; 906 retval = kBadStreamParameterWarning;
893 } 907 }
894 908
895 stream_delay_ms_ = delay; 909 stream_delay_ms_ = delay;
896 return retval; 910 return retval;
897 } 911 }
898 912
899 int AudioProcessingImpl::stream_delay_ms() const { 913 int AudioProcessingImpl::stream_delay_ms() const {
914 RTC_DCHECK(capture_thread_.CalledOnValidThread());
900 return stream_delay_ms_; 915 return stream_delay_ms_;
901 } 916 }
902 917
903 bool AudioProcessingImpl::was_stream_delay_set() const { 918 bool AudioProcessingImpl::was_stream_delay_set() const {
904 return was_stream_delay_set_; 919 return was_stream_delay_set_;
905 } 920 }
906 921
907 void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { 922 void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
923 RTC_DCHECK(capture_thread_.CalledOnValidThread());
908 key_pressed_ = key_pressed; 924 key_pressed_ = key_pressed;
909 } 925 }
910 926
911 void AudioProcessingImpl::set_delay_offset_ms(int offset) { 927 void AudioProcessingImpl::set_delay_offset_ms(int offset) {
928 RTC_DCHECK(capture_thread_.CalledOnValidThread());
912 CriticalSectionScoped crit_scoped(crit_); 929 CriticalSectionScoped crit_scoped(crit_);
913 delay_offset_ms_ = offset; 930 delay_offset_ms_ = offset;
914 } 931 }
915 932
916 int AudioProcessingImpl::delay_offset_ms() const { 933 int AudioProcessingImpl::delay_offset_ms() const {
934 RTC_DCHECK(capture_thread_.CalledOnValidThread());
917 return delay_offset_ms_; 935 return delay_offset_ms_;
918 } 936 }
919 937
920 int AudioProcessingImpl::StartDebugRecording( 938 int AudioProcessingImpl::StartDebugRecording(
921 const char filename[AudioProcessing::kMaxFilenameSize]) { 939 const char filename[AudioProcessing::kMaxFilenameSize]) {
922 CriticalSectionScoped crit_scoped(crit_); 940 CriticalSectionScoped crit_scoped(crit_);
923 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, ""); 941 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
924 942
925 if (filename == NULL) { 943 if (filename == NULL) {
926 return kNullPointerError; 944 return kNullPointerError;
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 1303
1286 event_msg_->set_type(audioproc::Event::CONFIG); 1304 event_msg_->set_type(audioproc::Event::CONFIG);
1287 event_msg_->mutable_config()->CopyFrom(config); 1305 event_msg_->mutable_config()->CopyFrom(config);
1288 1306
1289 RETURN_ON_ERR(WriteMessageToDebugFile()); 1307 RETURN_ON_ERR(WriteMessageToDebugFile());
1290 return kNoError; 1308 return kNoError;
1291 } 1309 }
1292 #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP 1310 #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1293 1311
1294 } // namespace webrtc 1312 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698