blob: 9d980e8a581bd2e46bc4c1edee35c96bc87839fe [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
Fredrik Solenberg2a877972017-12-15 16:42:15 +010013#include <algorithm>
14#include <utility>
15#include <vector>
16
Fredrik Solenbergd5247512017-12-18 22:41:03 +010017#include "audio/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_device/include/audio_device.h"
19#include "rtc_base/atomicops.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
henrika5f6bf242017-11-01 11:06:56 +010022#include "rtc_base/ptr_util.h"
23#include "rtc_base/thread.h"
solenberg566ef242015-11-06 15:34:49 -080024
25namespace webrtc {
26namespace internal {
27
28AudioState::AudioState(const AudioState::Config& config)
aleloidd310712016-11-17 06:28:59 -080029 : config_(config),
30 voe_base_(config.voice_engine),
Fredrik Solenberg2a877972017-12-15 16:42:15 +010031 audio_transport_(config_.audio_mixer,
32 config_.audio_processing.get(),
33 config_.audio_device_module.get()) {
solenberg566ef242015-11-06 15:34:49 -080034 process_thread_checker_.DetachFromThread();
aleloi10111bc2016-11-17 06:48:48 -080035 RTC_DCHECK(config_.audio_mixer);
Fredrik Solenbergaaedf752017-12-18 13:09:12 +010036 RTC_DCHECK(config_.audio_device_module);
solenberg566ef242015-11-06 15:34:49 -080037}
38
39AudioState::~AudioState() {
40 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenbergd5247512017-12-18 22:41:03 +010041 RTC_DCHECK(receiving_streams_.empty());
Fredrik Solenberg2a877972017-12-15 16:42:15 +010042 RTC_DCHECK(sending_streams_.empty());
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
solenberg566ef242015-11-06 15:34:49 -080050bool AudioState::typing_noise_detected() const {
51 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +010052 return audio_transport_.typing_noise_detected();
53}
54
Fredrik Solenbergd5247512017-12-18 22:41:03 +010055void AudioState::AddReceivingStream(webrtc::AudioReceiveStream* stream) {
56 RTC_DCHECK(thread_checker_.CalledOnValidThread());
57 RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
58 receiving_streams_.insert(stream);
59 if (!config_.audio_mixer->AddSource(
60 static_cast<internal::AudioReceiveStream*>(stream))) {
61 RTC_LOG(LS_ERROR) << "Failed to add source to mixer.";
62 }
63
64 // Make sure playback is initialized; start playing if enabled.
65 auto* adm = config_.audio_device_module.get();
66 if (!adm->Playing()) {
67 if (adm->InitPlayout() == 0) {
68 if (playout_enabled_) {
69 adm->StartPlayout();
70 }
71 } else {
72 RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout.";
73 }
74 }
75}
76
77void AudioState::RemoveReceivingStream(webrtc::AudioReceiveStream* stream) {
78 RTC_DCHECK(thread_checker_.CalledOnValidThread());
79 auto count = receiving_streams_.erase(stream);
80 RTC_DCHECK_EQ(1, count);
81 config_.audio_mixer->RemoveSource(
82 static_cast<internal::AudioReceiveStream*>(stream));
83 if (receiving_streams_.empty()) {
84 config_.audio_device_module->StopPlayout();
85 }
86}
87
Fredrik Solenberg2a877972017-12-15 16:42:15 +010088void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
89 int sample_rate_hz, size_t num_channels) {
90 RTC_DCHECK(thread_checker_.CalledOnValidThread());
91 auto& properties = sending_streams_[stream];
92 properties.sample_rate_hz = sample_rate_hz;
93 properties.num_channels = num_channels;
94 UpdateAudioTransportWithSendingStreams();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +010095
96 // Make sure recording is initialized; start recording if enabled.
97 auto* adm = config_.audio_device_module.get();
98 if (!adm->Recording()) {
99 if (adm->InitRecording() == 0) {
100 if (recording_enabled_) {
101 adm->StartRecording();
102 }
103 } else {
104 RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
105 }
106 }
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100107}
108
109void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
110 RTC_DCHECK(thread_checker_.CalledOnValidThread());
111 auto count = sending_streams_.erase(stream);
112 RTC_DCHECK_EQ(1, count);
113 UpdateAudioTransportWithSendingStreams();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100114 if (sending_streams_.empty()) {
115 config_.audio_device_module->StopRecording();
116 }
solenberg566ef242015-11-06 15:34:49 -0800117}
118
henrika5f6bf242017-11-01 11:06:56 +0100119void AudioState::SetPlayout(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +0100121 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100122 if (playout_enabled_ != enabled) {
123 playout_enabled_ = enabled;
124 if (enabled) {
125 null_audio_poller_.reset();
126 if (!receiving_streams_.empty()) {
127 config_.audio_device_module->StartPlayout();
128 }
129 } else {
130 config_.audio_device_module->StopPlayout();
131 null_audio_poller_ =
132 rtc::MakeUnique<NullAudioPoller>(&audio_transport_);
133 }
henrika5f6bf242017-11-01 11:06:56 +0100134 }
135}
136
137void AudioState::SetRecording(bool enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
henrika5f6bf242017-11-01 11:06:56 +0100139 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100140 if (recording_enabled_ != enabled) {
141 recording_enabled_ = enabled;
142 if (enabled) {
143 if (!sending_streams_.empty()) {
144 config_.audio_device_module->StartRecording();
145 }
146 } else {
147 config_.audio_device_module->StopRecording();
148 }
149 }
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100150}
151
152AudioState::Stats AudioState::GetAudioInputStats() const {
153 RTC_DCHECK(thread_checker_.CalledOnValidThread());
154 const voe::AudioLevel& audio_level = audio_transport_.audio_level();
155 Stats result;
156 result.audio_level = audio_level.LevelFullRange();
157 RTC_DCHECK_LE(0, result.audio_level);
158 RTC_DCHECK_GE(32767, result.audio_level);
159 result.quantized_audio_level = audio_level.Level();
160 RTC_DCHECK_LE(0, result.quantized_audio_level);
161 RTC_DCHECK_GE(9, result.quantized_audio_level);
162 result.total_energy = audio_level.TotalEnergy();
163 result.total_duration = audio_level.TotalDuration();
164 return result;
165}
166
167void AudioState::SetStereoChannelSwapping(bool enable) {
168 RTC_DCHECK(thread_checker_.CalledOnValidThread());
169 audio_transport_.SetStereoChannelSwapping(enable);
henrika5f6bf242017-11-01 11:06:56 +0100170}
171
solenberg566ef242015-11-06 15:34:49 -0800172// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +0200173void AudioState::AddRef() const {
174 rtc::AtomicOps::Increment(&ref_count_);
solenberg566ef242015-11-06 15:34:49 -0800175}
176
177// Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller6f72f562017-10-19 13:15:17 +0200178rtc::RefCountReleaseStatus AudioState::Release() const {
179 if (rtc::AtomicOps::Decrement(&ref_count_) == 0) {
solenberg566ef242015-11-06 15:34:49 -0800180 delete this;
Niels Möller6f72f562017-10-19 13:15:17 +0200181 return rtc::RefCountReleaseStatus::kDroppedLastRef;
solenberg566ef242015-11-06 15:34:49 -0800182 }
Niels Möller6f72f562017-10-19 13:15:17 +0200183 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
solenberg566ef242015-11-06 15:34:49 -0800184}
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100185
186void AudioState::UpdateAudioTransportWithSendingStreams() {
187 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100188 std::vector<webrtc::AudioSendStream*> sending_streams;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100189 int max_sample_rate_hz = 8000;
190 size_t max_num_channels = 1;
191 for (const auto& kv : sending_streams_) {
192 sending_streams.push_back(kv.first);
193 max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz);
194 max_num_channels = std::max(max_num_channels, kv.second.num_channels);
195 }
196 audio_transport_.UpdateSendingStreams(std::move(sending_streams),
197 max_sample_rate_hz, max_num_channels);
198}
solenberg566ef242015-11-06 15:34:49 -0800199} // namespace internal
200
201rtc::scoped_refptr<AudioState> AudioState::Create(
202 const AudioState::Config& config) {
203 return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
204}
205} // namespace webrtc