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 | |
| 11 | #include "webrtc/audio/audio_state.h" |
| 12 | |
| 13 | #include "webrtc/base/atomicops.h" |
| 14 | #include "webrtc/base/checks.h" |
| 15 | #include "webrtc/base/logging.h" |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame^] | 16 | #include "webrtc/modules/audio_device/include/audio_device.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 17 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 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(), |
| 26 | voe_base_->audio_processing(), |
| 27 | config_.audio_mixer) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 28 | process_thread_checker_.DetachFromThread(); |
| 29 | // Only one AudioState should be created per VoiceEngine. |
| 30 | RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame^] | 31 | |
| 32 | auto* const device = voe_base_->audio_device_module(); |
| 33 | RTC_DCHECK(device); |
| 34 | |
| 35 | // This is needed for the Chrome implementation of RegisterAudioCallback. |
| 36 | device->RegisterAudioCallback(nullptr); |
| 37 | device->RegisterAudioCallback(&audio_transport_proxy_); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | AudioState::~AudioState() { |
| 41 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 | voe_base_->DeRegisterVoiceEngineObserver(); |
| 43 | } |
| 44 | |
| 45 | VoiceEngine* AudioState::voice_engine() { |
| 46 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 | return config_.voice_engine; |
| 48 | } |
| 49 | |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame^] | 50 | rtc::scoped_refptr<AudioMixer> AudioState::mixer() { |
| 51 | return config_.audio_mixer; |
| 52 | } |
| 53 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 54 | bool AudioState::typing_noise_detected() const { |
| 55 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 | rtc::CritScope lock(&crit_sect_); |
| 57 | return typing_noise_detected_; |
| 58 | } |
| 59 | |
| 60 | // Reference count; implementation copied from rtc::RefCountedObject. |
| 61 | int AudioState::AddRef() const { |
| 62 | return rtc::AtomicOps::Increment(&ref_count_); |
| 63 | } |
| 64 | |
| 65 | // Reference count; implementation copied from rtc::RefCountedObject. |
| 66 | int AudioState::Release() const { |
| 67 | int count = rtc::AtomicOps::Decrement(&ref_count_); |
| 68 | if (!count) { |
| 69 | delete this; |
| 70 | } |
| 71 | return count; |
| 72 | } |
| 73 | |
| 74 | void AudioState::CallbackOnError(int channel_id, int err_code) { |
| 75 | RTC_DCHECK(process_thread_checker_.CalledOnValidThread()); |
| 76 | |
| 77 | // All call sites in VoE, as of this writing, specify -1 as channel_id. |
| 78 | RTC_DCHECK(channel_id == -1); |
| 79 | LOG(LS_INFO) << "VoiceEngine error " << err_code << " reported on channel " |
| 80 | << channel_id << "."; |
| 81 | if (err_code == VE_TYPING_NOISE_WARNING) { |
| 82 | rtc::CritScope lock(&crit_sect_); |
| 83 | typing_noise_detected_ = true; |
| 84 | } else if (err_code == VE_TYPING_NOISE_OFF_WARNING) { |
| 85 | rtc::CritScope lock(&crit_sect_); |
| 86 | typing_noise_detected_ = false; |
| 87 | } |
| 88 | } |
| 89 | } // namespace internal |
| 90 | |
| 91 | rtc::scoped_refptr<AudioState> AudioState::Create( |
| 92 | const AudioState::Config& config) { |
| 93 | return rtc::scoped_refptr<AudioState>(new internal::AudioState(config)); |
| 94 | } |
| 95 | } // namespace webrtc |