blob: a1dd956a24430a6031416354abbc9f6faf2914d0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "audio/audio_state.h"
solenberg566ef242015-11-06 15:34:49 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#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"
henrika5f6bf242017-11-01 11:06:56 +010017#include "rtc_base/ptr_util.h"
18#include "rtc_base/thread.h"
solenbergfc3a2e32017-09-26 09:35:01 -070019#include "voice_engine/transmit_mixer.h"
solenberg566ef242015-11-06 15:34:49 -080020
21namespace webrtc {
22namespace internal {
23
24AudioState::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(),
peahe67bedb2017-07-07 04:25:11 -070028 config_.audio_processing.get(),
aleloidd310712016-11-17 06:28:59 -080029 config_.audio_mixer) {
solenberg566ef242015-11-06 15:34:49 -080030 process_thread_checker_.DetachFromThread();
aleloi10111bc2016-11-17 06:48:48 -080031 RTC_DCHECK(config_.audio_mixer);
32
aleloidd310712016-11-17 06:28:59 -080033 auto* const device = voe_base_->audio_device_module();
34 RTC_DCHECK(device);
35
36 // This is needed for the Chrome implementation of RegisterAudioCallback.
37 device->RegisterAudioCallback(nullptr);
38 device->RegisterAudioCallback(&audio_transport_proxy_);
solenberg566ef242015-11-06 15:34:49 -080039}
40
41AudioState::~AudioState() {
42 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg566ef242015-11-06 15:34:49 -080043}
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() {
aleloi10111bc2016-11-17 06:48:48 -080051 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloidd310712016-11-17 06:28:59 -080052 return config_.audio_mixer;
53}
54
solenberg566ef242015-11-06 15:34:49 -080055bool AudioState::typing_noise_detected() const {
56 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergfc3a2e32017-09-26 09:35:01 -070057 // TODO(solenberg): Remove const_cast once AudioState owns transmit mixer
58 // functionality.
59 voe::TransmitMixer* transmit_mixer =
60 const_cast<AudioState*>(this)->voe_base_->transmit_mixer();
61 return transmit_mixer->typing_noise_detected();
solenberg566ef242015-11-06 15:34:49 -080062}
63
henrika5f6bf242017-11-01 11:06:56 +010064void AudioState::SetPlayout(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +010066 RTC_DCHECK(thread_checker_.CalledOnValidThread());
67 const bool currently_enabled = (null_audio_poller_ == nullptr);
68 if (enabled == currently_enabled) {
69 return;
70 }
71 VoEBase* const voe = VoEBase::GetInterface(voice_engine());
72 RTC_DCHECK(voe);
73 if (enabled) {
74 null_audio_poller_.reset();
75 }
76 // Will stop/start playout of the underlying device, if necessary, and
77 // remember the setting for when it receives subsequent calls of
78 // StartPlayout.
79 voe->SetPlayout(enabled);
80 if (!enabled) {
81 null_audio_poller_ =
82 rtc::MakeUnique<NullAudioPoller>(&audio_transport_proxy_);
83 }
henrika6d852522017-11-09 16:37:41 +010084 voe->Release();
henrika5f6bf242017-11-01 11:06:56 +010085}
86
87void AudioState::SetRecording(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +010089 RTC_DCHECK(thread_checker_.CalledOnValidThread());
90 // TODO(henrika): keep track of state as in SetPlayout().
91 VoEBase* const voe = VoEBase::GetInterface(voice_engine());
92 RTC_DCHECK(voe);
93 // Will stop/start recording of the underlying device, if necessary, and
94 // remember the setting for when it receives subsequent calls of
95 // StartPlayout.
96 voe->SetRecording(enabled);
henrika6d852522017-11-09 16:37:41 +010097 voe->Release();
henrika5f6bf242017-11-01 11:06:56 +010098}
99
solenberg566ef242015-11-06 15:34:49 -0800100// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +0200101void AudioState::AddRef() const {
102 rtc::AtomicOps::Increment(&ref_count_);
solenberg566ef242015-11-06 15:34:49 -0800103}
104
105// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +0200106rtc::RefCountReleaseStatus AudioState::Release() const {
107 if (rtc::AtomicOps::Decrement(&ref_count_) == 0) {
solenberg566ef242015-11-06 15:34:49 -0800108 delete this;
Niels Möller6f72f562017-10-19 13:15:17 +0200109 return rtc::RefCountReleaseStatus::kDroppedLastRef;
solenberg566ef242015-11-06 15:34:49 -0800110 }
Niels Möller6f72f562017-10-19 13:15:17 +0200111 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
solenberg566ef242015-11-06 15:34:49 -0800112}
solenberg566ef242015-11-06 15:34:49 -0800113} // namespace internal
114
115rtc::scoped_refptr<AudioState> AudioState::Create(
116 const AudioState::Config& config) {
117 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
118}
119} // namespace webrtc