blob: 3ca1dd70b54b3f3e5aaa6f9abbe19a8965e8a88f [file] [log] [blame]
solenberg566ef242015-11-06 23:34:491/*
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 04:47:3111#include "audio/audio_state.h"
solenberg566ef242015-11-06 23:34:4912
Fredrik Solenberg2a877972017-12-15 15:42:1513#include <algorithm>
Mirko Bonadei317a1f02019-09-17 15:06:1814#include <memory>
Fredrik Solenberg2a877972017-12-15 15:42:1515#include <utility>
16#include <vector>
17
Fredrik Solenbergd5247512017-12-18 21:41:0318#include "audio/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/ref_counted_object.h"
henrika5f6bf242017-11-01 10:06:5623#include "rtc_base/thread.h"
solenberg566ef242015-11-06 23:34:4924
25namespace webrtc {
26namespace internal {
27
28AudioState::AudioState(const AudioState::Config& config)
aleloidd310712016-11-17 14:28:5929 : config_(config),
Yves Gerey665174f2018-06-19 13:03:0530 audio_transport_(config_.audio_mixer, config_.audio_processing.get()) {
Sebastian Janssonc01367d2019-04-08 13:20:4431 process_thread_checker_.Detach();
aleloi10111bc2016-11-17 14:48:4832 RTC_DCHECK(config_.audio_mixer);
Fredrik Solenbergaaedf752017-12-18 12:09:1233 RTC_DCHECK(config_.audio_device_module);
solenberg566ef242015-11-06 23:34:4934}
35
36AudioState::~AudioState() {
Sebastian Janssonc01367d2019-04-08 13:20:4437 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergd5247512017-12-18 21:41:0338 RTC_DCHECK(receiving_streams_.empty());
Fredrik Solenberg2a877972017-12-15 15:42:1539 RTC_DCHECK(sending_streams_.empty());
solenberg566ef242015-11-06 23:34:4940}
41
Mirko Bonadei8fdcac32018-08-28 14:30:1842AudioProcessing* AudioState::audio_processing() {
43 RTC_DCHECK(config_.audio_processing);
44 return config_.audio_processing.get();
45}
46
47AudioTransport* AudioState::audio_transport() {
48 return &audio_transport_;
49}
50
solenberg566ef242015-11-06 23:34:4951bool AudioState::typing_noise_detected() const {
Sebastian Janssonc01367d2019-04-08 13:20:4452 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:1553 return audio_transport_.typing_noise_detected();
54}
55
Fredrik Solenbergd5247512017-12-18 21:41:0356void AudioState::AddReceivingStream(webrtc::AudioReceiveStream* stream) {
Sebastian Janssonc01367d2019-04-08 13:20:4457 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergd5247512017-12-18 21:41:0358 RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
59 receiving_streams_.insert(stream);
60 if (!config_.audio_mixer->AddSource(
Yves Gerey665174f2018-06-19 13:03:0561 static_cast<internal::AudioReceiveStream*>(stream))) {
Jonas Olsson24ea8222018-01-25 09:14:2962 RTC_DLOG(LS_ERROR) << "Failed to add source to mixer.";
Fredrik Solenbergd5247512017-12-18 21:41:0363 }
64
65 // Make sure playback is initialized; start playing if enabled.
Gustaf Ullbergdabdde62019-10-17 09:32:1066 UpdateNullAudioPollerState();
Fredrik Solenbergd5247512017-12-18 21:41:0367 auto* adm = config_.audio_device_module.get();
68 if (!adm->Playing()) {
69 if (adm->InitPlayout() == 0) {
70 if (playout_enabled_) {
71 adm->StartPlayout();
72 }
73 } else {
74 RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout.";
75 }
76 }
77}
78
79void AudioState::RemoveReceivingStream(webrtc::AudioReceiveStream* stream) {
Sebastian Janssonc01367d2019-04-08 13:20:4480 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergd5247512017-12-18 21:41:0381 auto count = receiving_streams_.erase(stream);
82 RTC_DCHECK_EQ(1, count);
83 config_.audio_mixer->RemoveSource(
84 static_cast<internal::AudioReceiveStream*>(stream));
Gustaf Ullbergdabdde62019-10-17 09:32:1085 UpdateNullAudioPollerState();
Fredrik Solenbergd5247512017-12-18 21:41:0386 if (receiving_streams_.empty()) {
87 config_.audio_device_module->StopPlayout();
88 }
89}
90
Fredrik Solenberg2a877972017-12-15 15:42:1591void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
Yves Gerey665174f2018-06-19 13:03:0592 int sample_rate_hz,
93 size_t num_channels) {
Sebastian Janssonc01367d2019-04-08 13:20:4494 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:1595 auto& properties = sending_streams_[stream];
96 properties.sample_rate_hz = sample_rate_hz;
97 properties.num_channels = num_channels;
98 UpdateAudioTransportWithSendingStreams();
Fredrik Solenbergaaedf752017-12-18 12:09:1299
100 // Make sure recording is initialized; start recording if enabled.
101 auto* adm = config_.audio_device_module.get();
102 if (!adm->Recording()) {
103 if (adm->InitRecording() == 0) {
104 if (recording_enabled_) {
105 adm->StartRecording();
106 }
107 } else {
108 RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
109 }
110 }
Fredrik Solenberg2a877972017-12-15 15:42:15111}
112
113void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
Sebastian Janssonc01367d2019-04-08 13:20:44114 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15115 auto count = sending_streams_.erase(stream);
116 RTC_DCHECK_EQ(1, count);
117 UpdateAudioTransportWithSendingStreams();
Fredrik Solenbergaaedf752017-12-18 12:09:12118 if (sending_streams_.empty()) {
119 config_.audio_device_module->StopRecording();
120 }
solenberg566ef242015-11-06 23:34:49121}
122
henrika5f6bf242017-11-01 10:06:56123void AudioState::SetPlayout(bool enabled) {
Mirko Bonadei675513b2017-11-09 10:09:25124 RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
Sebastian Janssonc01367d2019-04-08 13:20:44125 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergd5247512017-12-18 21:41:03126 if (playout_enabled_ != enabled) {
127 playout_enabled_ = enabled;
128 if (enabled) {
Gustaf Ullbergdabdde62019-10-17 09:32:10129 UpdateNullAudioPollerState();
Fredrik Solenbergd5247512017-12-18 21:41:03130 if (!receiving_streams_.empty()) {
131 config_.audio_device_module->StartPlayout();
132 }
133 } else {
134 config_.audio_device_module->StopPlayout();
Gustaf Ullbergdabdde62019-10-17 09:32:10135 UpdateNullAudioPollerState();
Fredrik Solenbergd5247512017-12-18 21:41:03136 }
henrika5f6bf242017-11-01 10:06:56137 }
138}
139
140void AudioState::SetRecording(bool enabled) {
Mirko Bonadei675513b2017-11-09 10:09:25141 RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
Sebastian Janssonc01367d2019-04-08 13:20:44142 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergaaedf752017-12-18 12:09:12143 if (recording_enabled_ != enabled) {
144 recording_enabled_ = enabled;
145 if (enabled) {
146 if (!sending_streams_.empty()) {
147 config_.audio_device_module->StartRecording();
148 }
149 } else {
150 config_.audio_device_module->StopRecording();
151 }
152 }
Fredrik Solenberg2a877972017-12-15 15:42:15153}
154
Fredrik Solenberg2a877972017-12-15 15:42:15155void AudioState::SetStereoChannelSwapping(bool enable) {
Sebastian Janssonc01367d2019-04-08 13:20:44156 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15157 audio_transport_.SetStereoChannelSwapping(enable);
henrika5f6bf242017-11-01 10:06:56158}
159
Fredrik Solenberg2a877972017-12-15 15:42:15160void AudioState::UpdateAudioTransportWithSendingStreams() {
Sebastian Janssonc01367d2019-04-08 13:20:44161 RTC_DCHECK(thread_checker_.IsCurrent());
Fredrik Solenbergd5247512017-12-18 21:41:03162 std::vector<webrtc::AudioSendStream*> sending_streams;
Fredrik Solenberg2a877972017-12-15 15:42:15163 int max_sample_rate_hz = 8000;
164 size_t max_num_channels = 1;
165 for (const auto& kv : sending_streams_) {
166 sending_streams.push_back(kv.first);
167 max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz);
168 max_num_channels = std::max(max_num_channels, kv.second.num_channels);
169 }
170 audio_transport_.UpdateSendingStreams(std::move(sending_streams),
171 max_sample_rate_hz, max_num_channels);
172}
Gustaf Ullbergdabdde62019-10-17 09:32:10173
174void AudioState::UpdateNullAudioPollerState() {
175 // Run NullAudioPoller when there are receiving streams and playout is
176 // disabled.
177 if (!receiving_streams_.empty() && !playout_enabled_) {
178 if (!null_audio_poller_)
179 null_audio_poller_ = std::make_unique<NullAudioPoller>(&audio_transport_);
180 } else {
181 null_audio_poller_.reset();
182 }
183}
solenberg566ef242015-11-06 23:34:49184} // namespace internal
185
186rtc::scoped_refptr<AudioState> AudioState::Create(
187 const AudioState::Config& config) {
Niels Möllerac63ac72019-01-08 12:47:12188 return new rtc::RefCountedObject<internal::AudioState>(config);
solenberg566ef242015-11-06 23:34:49189}
190} // namespace webrtc