blob: 36d9d476a37c77f5307bab7ae18ce452cf938d37 [file] [log] [blame]
solenberg566ef242015-11-06 15:34:49 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "audio/audio_state.h"
solenberg566ef242015-11-06 15:34:49 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#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"
solenberg566ef242015-11-06 15:34:49 -080018
19namespace webrtc {
20namespace internal {
21
22AudioState::AudioState(const AudioState::Config& config)
aleloidd310712016-11-17 06:28:59 -080023 : config_(config),
24 voe_base_(config.voice_engine),
25 audio_transport_proxy_(voe_base_->audio_transport(),
peahe67bedb2017-07-07 04:25:11 -070026 config_.audio_processing.get(),
aleloidd310712016-11-17 06:28:59 -080027 config_.audio_mixer) {
solenberg566ef242015-11-06 15:34:49 -080028 process_thread_checker_.DetachFromThread();
aleloi10111bc2016-11-17 06:48:48 -080029 RTC_DCHECK(config_.audio_mixer);
30
solenberg566ef242015-11-06 15:34:49 -080031 // Only one AudioState should be created per VoiceEngine.
32 RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1);
aleloidd310712016-11-17 06:28:59 -080033
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_);
solenberg566ef242015-11-06 15:34:49 -080040}
41
42AudioState::~AudioState() {
43 RTC_DCHECK(thread_checker_.CalledOnValidThread());
44 voe_base_->DeRegisterVoiceEngineObserver();
45}
46
47VoiceEngine* AudioState::voice_engine() {
48 RTC_DCHECK(thread_checker_.CalledOnValidThread());
49 return config_.voice_engine;
50}
51
aleloidd310712016-11-17 06:28:59 -080052rtc::scoped_refptr<AudioMixer> AudioState::mixer() {
aleloi10111bc2016-11-17 06:48:48 -080053 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloidd310712016-11-17 06:28:59 -080054 return config_.audio_mixer;
55}
56
solenberg566ef242015-11-06 15:34:49 -080057bool 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.
64int AudioState::AddRef() const {
65 return rtc::AtomicOps::Increment(&ref_count_);
66}
67
68// Reference count; implementation copied from rtc::RefCountedObject.
69int AudioState::Release() const {
70 int count = rtc::AtomicOps::Decrement(&ref_count_);
71 if (!count) {
72 delete this;
73 }
74 return count;
75}
76
77void 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
94rtc::scoped_refptr<AudioState> AudioState::Create(
95 const AudioState::Config& config) {
96 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
97}
98} // namespace webrtc