blob: 95a90a5bf1153ba0d51b1c669cdac0a8936d59a8 [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
13#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
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(),
26 voe_base_->audio_processing(),
27 config_.audio_mixer) {
solenberg566ef242015-11-06 15:34:49 -080028 process_thread_checker_.DetachFromThread();
29 // Only one AudioState should be created per VoiceEngine.
30 RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1);
aleloidd310712016-11-17 06:28:59 -080031
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_);
solenberg566ef242015-11-06 15:34:49 -080038}
39
40AudioState::~AudioState() {
41 RTC_DCHECK(thread_checker_.CalledOnValidThread());
42 voe_base_->DeRegisterVoiceEngineObserver();
43}
44
45VoiceEngine* AudioState::voice_engine() {
46 RTC_DCHECK(thread_checker_.CalledOnValidThread());
47 return config_.voice_engine;
48}
49
aleloidd310712016-11-17 06:28:59 -080050rtc::scoped_refptr<AudioMixer> AudioState::mixer() {
51 return config_.audio_mixer;
52}
53
solenberg566ef242015-11-06 15:34:49 -080054bool 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.
61int AudioState::AddRef() const {
62 return rtc::AtomicOps::Increment(&ref_count_);
63}
64
65// Reference count; implementation copied from rtc::RefCountedObject.
66int AudioState::Release() const {
67 int count = rtc::AtomicOps::Decrement(&ref_count_);
68 if (!count) {
69 delete this;
70 }
71 return count;
72}
73
74void 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
91rtc::scoped_refptr<AudioState> AudioState::Create(
92 const AudioState::Config& config) {
93 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
94}
95} // namespace webrtc