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 "audio/audio_state.h"
12 
13 #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 "rtc_base/ptr_util.h"
18 #include "rtc_base/thread.h"
19 #include "voice_engine/transmit_mixer.h"
20 
21 namespace webrtc {
22 namespace internal {
23 
AudioState(const AudioState::Config & config)24 AudioState::AudioState(const AudioState::Config& config)
25     : config_(config),
26       voe_base_(config.voice_engine),
27       audio_transport_proxy_(voe_base_->audio_transport(),
28                              config_.audio_processing.get(),
29                              config_.audio_mixer) {
30   process_thread_checker_.DetachFromThread();
31   RTC_DCHECK(config_.audio_mixer);
32 }
33 
~AudioState()34 AudioState::~AudioState() {
35   RTC_DCHECK(thread_checker_.CalledOnValidThread());
36 }
37 
voice_engine()38 VoiceEngine* AudioState::voice_engine() {
39   RTC_DCHECK(thread_checker_.CalledOnValidThread());
40   return config_.voice_engine;
41 }
42 
mixer()43 rtc::scoped_refptr<AudioMixer> AudioState::mixer() {
44   RTC_DCHECK(thread_checker_.CalledOnValidThread());
45   return config_.audio_mixer;
46 }
47 
typing_noise_detected() const48 bool AudioState::typing_noise_detected() const {
49   RTC_DCHECK(thread_checker_.CalledOnValidThread());
50   // TODO(solenberg): Remove const_cast once AudioState owns transmit mixer
51   //                  functionality.
52   voe::TransmitMixer* transmit_mixer =
53       const_cast<AudioState*>(this)->voe_base_->transmit_mixer();
54   return transmit_mixer->typing_noise_detected();
55 }
56 
SetPlayout(bool enabled)57 void AudioState::SetPlayout(bool enabled) {
58   RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
59   RTC_DCHECK(thread_checker_.CalledOnValidThread());
60   const bool currently_enabled = (null_audio_poller_ == nullptr);
61   if (enabled == currently_enabled) {
62     return;
63   }
64   VoEBase* const voe = VoEBase::GetInterface(voice_engine());
65   RTC_DCHECK(voe);
66   if (enabled) {
67     null_audio_poller_.reset();
68   }
69   // Will stop/start playout of the underlying device, if necessary, and
70   // remember the setting for when it receives subsequent calls of
71   // StartPlayout.
72   voe->SetPlayout(enabled);
73   if (!enabled) {
74     null_audio_poller_ =
75         rtc::MakeUnique<NullAudioPoller>(&audio_transport_proxy_);
76   }
77   voe->Release();
78 }
79 
SetRecording(bool enabled)80 void AudioState::SetRecording(bool enabled) {
81   RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
82   RTC_DCHECK(thread_checker_.CalledOnValidThread());
83   // TODO(henrika): keep track of state as in SetPlayout().
84   VoEBase* const voe = VoEBase::GetInterface(voice_engine());
85   RTC_DCHECK(voe);
86   // Will stop/start recording of the underlying device, if necessary, and
87   // remember the setting for when it receives subsequent calls of
88   // StartPlayout.
89   voe->SetRecording(enabled);
90   voe->Release();
91 }
92 
93 // Reference count; implementation copied from rtc::RefCountedObject.
AddRef() const94 void AudioState::AddRef() const {
95   rtc::AtomicOps::Increment(&ref_count_);
96 }
97 
98 // Reference count; implementation copied from rtc::RefCountedObject.
Release() const99 rtc::RefCountReleaseStatus AudioState::Release() const {
100   if (rtc::AtomicOps::Decrement(&ref_count_) == 0) {
101     delete this;
102     return rtc::RefCountReleaseStatus::kDroppedLastRef;
103   }
104   return rtc::RefCountReleaseStatus::kOtherRefsRemained;
105 }
106 }  // namespace internal
107 
Create(const AudioState::Config & config)108 rtc::scoped_refptr<AudioState> AudioState::Create(
109     const AudioState::Config& config) {
110   return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
111 }
112 }  // namespace webrtc
113