blob: e63f97af2d4fb9fd3123f37532f0553382715984 [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"
16#include "webrtc/voice_engine/include/voe_errors.h"
17
18namespace webrtc {
19namespace internal {
20
21AudioState::AudioState(const AudioState::Config& config)
22 : config_(config), voe_base_(config.voice_engine) {
23 process_thread_checker_.DetachFromThread();
24 // Only one AudioState should be created per VoiceEngine.
25 RTC_CHECK(voe_base_->RegisterVoiceEngineObserver(*this) != -1);
26}
27
28AudioState::~AudioState() {
29 RTC_DCHECK(thread_checker_.CalledOnValidThread());
30 voe_base_->DeRegisterVoiceEngineObserver();
31}
32
33VoiceEngine* AudioState::voice_engine() {
34 RTC_DCHECK(thread_checker_.CalledOnValidThread());
35 return config_.voice_engine;
36}
37
38bool AudioState::typing_noise_detected() const {
39 RTC_DCHECK(thread_checker_.CalledOnValidThread());
40 rtc::CritScope lock(&crit_sect_);
41 return typing_noise_detected_;
42}
43
44// Reference count; implementation copied from rtc::RefCountedObject.
45int AudioState::AddRef() const {
46 return rtc::AtomicOps::Increment(&ref_count_);
47}
48
49// Reference count; implementation copied from rtc::RefCountedObject.
50int AudioState::Release() const {
51 int count = rtc::AtomicOps::Decrement(&ref_count_);
52 if (!count) {
53 delete this;
54 }
55 return count;
56}
57
58void AudioState::CallbackOnError(int channel_id, int err_code) {
59 RTC_DCHECK(process_thread_checker_.CalledOnValidThread());
60
61 // All call sites in VoE, as of this writing, specify -1 as channel_id.
62 RTC_DCHECK(channel_id == -1);
63 LOG(LS_INFO) << "VoiceEngine error " << err_code << " reported on channel "
64 << channel_id << ".";
65 if (err_code == VE_TYPING_NOISE_WARNING) {
66 rtc::CritScope lock(&crit_sect_);
67 typing_noise_detected_ = true;
68 } else if (err_code == VE_TYPING_NOISE_OFF_WARNING) {
69 rtc::CritScope lock(&crit_sect_);
70 typing_noise_detected_ = false;
71 }
72}
73} // namespace internal
74
75rtc::scoped_refptr<AudioState> AudioState::Create(
76 const AudioState::Config& config) {
77 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
78}
79} // namespace webrtc