blob: 961e772a9de3dcb5887bdcaa9159c0eebab6e713 [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
11#include "webrtc/audio/audio_state.h"
12
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020013#include "webrtc/base/atomicops.h"
14#include "webrtc/base/checks.h"
15#include "webrtc/base/logging.h"
aleloidd310712016-11-17 06:28:59 -080016#include "webrtc/modules/audio_device/include/audio_device.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/voice_engine/include/voe_errors.h"
18
19namespace webrtc {
20namespace internal {
21
peaha9cc40b2017-06-29 08:32:09 -070022// TODO(peah): Remove the conditional in the audio_transport_proxy_ constructor
23// call when upstream dependencies have properly been resolved.
solenberg566ef242015-11-06 15:34:49 -080024AudioState::AudioState(const AudioState::Config& config)
aleloidd310712016-11-17 06:28:59 -080025 : config_(config),
26 voe_base_(config.voice_engine),
27 audio_transport_proxy_(voe_base_->audio_transport(),
peaha9cc40b2017-06-29 08:32:09 -070028 config_.audio_processing
29 ? config_.audio_processing.get()
30 : voe_base_->audio_processing(),
aleloidd310712016-11-17 06:28:59 -080031 config_.audio_mixer) {
solenberg566ef242015-11-06 15:34:49 -080032 process_thread_checker_.DetachFromThread();
aleloi10111bc2016-11-17 06:48:48 -080033 RTC_DCHECK(config_.audio_mixer);
34
solenberg566ef242015-11-06 15:34:49 -080035 // Only one AudioState should be created per VoiceEngine.
36 RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1);
aleloidd310712016-11-17 06:28:59 -080037
38 auto* const device = voe_base_->audio_device_module();
39 RTC_DCHECK(device);
40
41 // This is needed for the Chrome implementation of RegisterAudioCallback.
42 device->RegisterAudioCallback(nullptr);
43 device->RegisterAudioCallback(&audio_transport_proxy_);
solenberg566ef242015-11-06 15:34:49 -080044}
45
46AudioState::~AudioState() {
47 RTC_DCHECK(thread_checker_.CalledOnValidThread());
48 voe_base_->DeRegisterVoiceEngineObserver();
49}
50
51VoiceEngine* AudioState::voice_engine() {
52 RTC_DCHECK(thread_checker_.CalledOnValidThread());
53 return config_.voice_engine;
54}
55
aleloidd310712016-11-17 06:28:59 -080056rtc::scoped_refptr<AudioMixer> AudioState::mixer() {
aleloi10111bc2016-11-17 06:48:48 -080057 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloidd310712016-11-17 06:28:59 -080058 return config_.audio_mixer;
59}
60
solenberg566ef242015-11-06 15:34:49 -080061bool AudioState::typing_noise_detected() const {
62 RTC_DCHECK(thread_checker_.CalledOnValidThread());
63 rtc::CritScope lock(&crit_sect_);
64 return typing_noise_detected_;
65}
66
67// Reference count; implementation copied from rtc::RefCountedObject.
68int AudioState::AddRef() const {
69 return rtc::AtomicOps::Increment(&ref_count_);
70}
71
72// Reference count; implementation copied from rtc::RefCountedObject.
73int AudioState::Release() const {
74 int count = rtc::AtomicOps::Decrement(&ref_count_);
75 if (!count) {
76 delete this;
77 }
78 return count;
79}
80
81void AudioState::CallbackOnError(int channel_id, int err_code) {
82 RTC_DCHECK(process_thread_checker_.CalledOnValidThread());
83
84 // All call sites in VoE, as of this writing, specify -1 as channel_id.
85 RTC_DCHECK(channel_id == -1);
86 LOG(LS_INFO) << "VoiceEngine error " << err_code << " reported on channel "
87 << channel_id << ".";
88 if (err_code == VE_TYPING_NOISE_WARNING) {
89 rtc::CritScope lock(&crit_sect_);
90 typing_noise_detected_ = true;
91 } else if (err_code == VE_TYPING_NOISE_OFF_WARNING) {
92 rtc::CritScope lock(&crit_sect_);
93 typing_noise_detected_ = false;
94 }
95}
96} // namespace internal
97
98rtc::scoped_refptr<AudioState> AudioState::Create(
99 const AudioState::Config& config) {
100 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
101}
102} // namespace webrtc