solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "audio/audio_state.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_device/include/audio_device.h" |
| 14 | #include "rtc_base/atomicops.h" |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "voice_engine/include/voe_errors.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace internal { |
| 21 | |
| 22 | AudioState::AudioState(const AudioState::Config& config) |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 23 | : config_(config), |
| 24 | voe_base_(config.voice_engine), |
| 25 | audio_transport_proxy_(voe_base_->audio_transport(), |
peah | e67bedb | 2017-07-07 04:25:11 -0700 | [diff] [blame] | 26 | config_.audio_processing.get(), |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 27 | config_.audio_mixer) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 28 | process_thread_checker_.DetachFromThread(); |
aleloi | 10111bc | 2016-11-17 06:48:48 -0800 | [diff] [blame] | 29 | RTC_DCHECK(config_.audio_mixer); |
| 30 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 31 | // Only one AudioState should be created per VoiceEngine. |
| 32 | RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 33 | |
| 34 | auto* const device = voe_base_->audio_device_module(); |
| 35 | RTC_DCHECK(device); |
| 36 | |
| 37 | // This is needed for the Chrome implementation of RegisterAudioCallback. |
| 38 | device->RegisterAudioCallback(nullptr); |
| 39 | device->RegisterAudioCallback(&audio_transport_proxy_); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | AudioState::~AudioState() { |
| 43 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 | voe_base_->DeRegisterVoiceEngineObserver(); |
| 45 | } |
| 46 | |
| 47 | VoiceEngine* AudioState::voice_engine() { |
| 48 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 | return config_.voice_engine; |
| 50 | } |
| 51 | |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 52 | rtc::scoped_refptr<AudioMixer> AudioState::mixer() { |
aleloi | 10111bc | 2016-11-17 06:48:48 -0800 | [diff] [blame] | 53 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 54 | return config_.audio_mixer; |
| 55 | } |
| 56 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 57 | bool AudioState::typing_noise_detected() const { |
| 58 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 | rtc::CritScope lock(&crit_sect_); |
| 60 | return typing_noise_detected_; |
| 61 | } |
| 62 | |
| 63 | // Reference count; implementation copied from rtc::RefCountedObject. |
| 64 | int AudioState::AddRef() const { |
| 65 | return rtc::AtomicOps::Increment(&ref_count_); |
| 66 | } |
| 67 | |
| 68 | // Reference count; implementation copied from rtc::RefCountedObject. |
| 69 | int AudioState::Release() const { |
| 70 | int count = rtc::AtomicOps::Decrement(&ref_count_); |
| 71 | if (!count) { |
| 72 | delete this; |
| 73 | } |
| 74 | return count; |
| 75 | } |
| 76 | |
| 77 | void AudioState::CallbackOnError(int channel_id, int err_code) { |
| 78 | RTC_DCHECK(process_thread_checker_.CalledOnValidThread()); |
| 79 | |
| 80 | // All call sites in VoE, as of this writing, specify -1 as channel_id. |
| 81 | RTC_DCHECK(channel_id == -1); |
| 82 | LOG(LS_INFO) << "VoiceEngine error " << err_code << " reported on channel " |
| 83 | << channel_id << "."; |
| 84 | if (err_code == VE_TYPING_NOISE_WARNING) { |
| 85 | rtc::CritScope lock(&crit_sect_); |
| 86 | typing_noise_detected_ = true; |
| 87 | } else if (err_code == VE_TYPING_NOISE_OFF_WARNING) { |
| 88 | rtc::CritScope lock(&crit_sect_); |
| 89 | typing_noise_detected_ = false; |
| 90 | } |
| 91 | } |
| 92 | } // namespace internal |
| 93 | |
| 94 | rtc::scoped_refptr<AudioState> AudioState::Create( |
| 95 | const AudioState::Config& config) { |
| 96 | return rtc::scoped_refptr<AudioState>(new internal::AudioState(config)); |
| 97 | } |
| 98 | } // namespace webrtc |