• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
16 #include "webrtc/voice_engine/include/voe_errors.h"
17 
18 namespace webrtc {
19 namespace internal {
20 
AudioState(const AudioState::Config & config)21 AudioState::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 
~AudioState()28 AudioState::~AudioState() {
29   RTC_DCHECK(thread_checker_.CalledOnValidThread());
30   voe_base_->DeRegisterVoiceEngineObserver();
31 }
32 
voice_engine()33 VoiceEngine* AudioState::voice_engine() {
34   RTC_DCHECK(thread_checker_.CalledOnValidThread());
35   return config_.voice_engine;
36 }
37 
typing_noise_detected() const38 bool 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.
AddRef() const45 int AudioState::AddRef() const {
46   return rtc::AtomicOps::Increment(&ref_count_);
47 }
48 
49 // Reference count; implementation copied from rtc::RefCountedObject.
Release() const50 int AudioState::Release() const {
51   int count = rtc::AtomicOps::Decrement(&ref_count_);
52   if (!count) {
53     delete this;
54   }
55   return count;
56 }
57 
CallbackOnError(int channel_id,int err_code)58 void 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 
Create(const AudioState::Config & config)75 rtc::scoped_refptr<AudioState> AudioState::Create(
76     const AudioState::Config& config) {
77   return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
78 }
79 }  // namespace webrtc
80