blob: 2a84f5c92a0b4d928fae6b098410f397fbe441a7 [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"
solenbergfc3a2e32017-09-26 09:35:01 -070017#include "voice_engine/transmit_mixer.h"
solenberg566ef242015-11-06 15:34:49 -080018
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(),
peahe67bedb2017-07-07 04:25:11 -070026 config_.audio_processing.get(),
aleloidd310712016-11-17 06:28:59 -080027 config_.audio_mixer) {
solenberg566ef242015-11-06 15:34:49 -080028 process_thread_checker_.DetachFromThread();
aleloi10111bc2016-11-17 06:48:48 -080029 RTC_DCHECK(config_.audio_mixer);
30
aleloidd310712016-11-17 06:28:59 -080031 auto* const device = voe_base_->audio_device_module();
32 RTC_DCHECK(device);
33
34 // This is needed for the Chrome implementation of RegisterAudioCallback.
35 device->RegisterAudioCallback(nullptr);
36 device->RegisterAudioCallback(&audio_transport_proxy_);
solenberg566ef242015-11-06 15:34:49 -080037}
38
39AudioState::~AudioState() {
40 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg566ef242015-11-06 15:34:49 -080041}
42
43VoiceEngine* AudioState::voice_engine() {
44 RTC_DCHECK(thread_checker_.CalledOnValidThread());
45 return config_.voice_engine;
46}
47
aleloidd310712016-11-17 06:28:59 -080048rtc::scoped_refptr<AudioMixer> AudioState::mixer() {
aleloi10111bc2016-11-17 06:48:48 -080049 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloidd310712016-11-17 06:28:59 -080050 return config_.audio_mixer;
51}
52
solenberg566ef242015-11-06 15:34:49 -080053bool AudioState::typing_noise_detected() const {
54 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergfc3a2e32017-09-26 09:35:01 -070055 // TODO(solenberg): Remove const_cast once AudioState owns transmit mixer
56 // functionality.
57 voe::TransmitMixer* transmit_mixer =
58 const_cast<AudioState*>(this)->voe_base_->transmit_mixer();
59 return transmit_mixer->typing_noise_detected();
solenberg566ef242015-11-06 15:34:49 -080060}
61
62// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +020063void AudioState::AddRef() const {
64 rtc::AtomicOps::Increment(&ref_count_);
solenberg566ef242015-11-06 15:34:49 -080065}
66
67// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +020068rtc::RefCountReleaseStatus AudioState::Release() const {
69 if (rtc::AtomicOps::Decrement(&ref_count_) == 0) {
solenberg566ef242015-11-06 15:34:49 -080070 delete this;
Niels Möller6f72f562017-10-19 13:15:17 +020071 return rtc::RefCountReleaseStatus::kDroppedLastRef;
solenberg566ef242015-11-06 15:34:49 -080072 }
Niels Möller6f72f562017-10-19 13:15:17 +020073 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
solenberg566ef242015-11-06 15:34:49 -080074}
solenberg566ef242015-11-06 15:34:49 -080075} // namespace internal
76
77rtc::scoped_refptr<AudioState> AudioState::Create(
78 const AudioState::Config& config) {
79 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
80}
81} // namespace webrtc