1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "audio/audio_state.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker #include <vector>
17*d9f75844SAndroid Build Coastguard Worker
18*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
21*d9f75844SAndroid Build Coastguard Worker #include "audio/audio_receive_stream.h"
22*d9f75844SAndroid Build Coastguard Worker #include "audio/audio_send_stream.h"
23*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_device/include/audio_device.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
25*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
26*d9f75844SAndroid Build Coastguard Worker
27*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
28*d9f75844SAndroid Build Coastguard Worker namespace internal {
29*d9f75844SAndroid Build Coastguard Worker
AudioState(const AudioState::Config & config)30*d9f75844SAndroid Build Coastguard Worker AudioState::AudioState(const AudioState::Config& config)
31*d9f75844SAndroid Build Coastguard Worker : config_(config),
32*d9f75844SAndroid Build Coastguard Worker audio_transport_(config_.audio_mixer.get(),
33*d9f75844SAndroid Build Coastguard Worker config_.audio_processing.get(),
34*d9f75844SAndroid Build Coastguard Worker config_.async_audio_processing_factory.get()) {
35*d9f75844SAndroid Build Coastguard Worker process_thread_checker_.Detach();
36*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(config_.audio_mixer);
37*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(config_.audio_device_module);
38*d9f75844SAndroid Build Coastguard Worker }
39*d9f75844SAndroid Build Coastguard Worker
~AudioState()40*d9f75844SAndroid Build Coastguard Worker AudioState::~AudioState() {
41*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
42*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(receiving_streams_.empty());
43*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(sending_streams_.empty());
44*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!null_audio_poller_.Running());
45*d9f75844SAndroid Build Coastguard Worker }
46*d9f75844SAndroid Build Coastguard Worker
audio_processing()47*d9f75844SAndroid Build Coastguard Worker AudioProcessing* AudioState::audio_processing() {
48*d9f75844SAndroid Build Coastguard Worker return config_.audio_processing.get();
49*d9f75844SAndroid Build Coastguard Worker }
50*d9f75844SAndroid Build Coastguard Worker
audio_transport()51*d9f75844SAndroid Build Coastguard Worker AudioTransport* AudioState::audio_transport() {
52*d9f75844SAndroid Build Coastguard Worker return &audio_transport_;
53*d9f75844SAndroid Build Coastguard Worker }
54*d9f75844SAndroid Build Coastguard Worker
AddReceivingStream(webrtc::AudioReceiveStreamInterface * stream)55*d9f75844SAndroid Build Coastguard Worker void AudioState::AddReceivingStream(
56*d9f75844SAndroid Build Coastguard Worker webrtc::AudioReceiveStreamInterface* stream) {
57*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
58*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
59*d9f75844SAndroid Build Coastguard Worker receiving_streams_.insert(stream);
60*d9f75844SAndroid Build Coastguard Worker if (!config_.audio_mixer->AddSource(
61*d9f75844SAndroid Build Coastguard Worker static_cast<AudioReceiveStreamImpl*>(stream))) {
62*d9f75844SAndroid Build Coastguard Worker RTC_DLOG(LS_ERROR) << "Failed to add source to mixer.";
63*d9f75844SAndroid Build Coastguard Worker }
64*d9f75844SAndroid Build Coastguard Worker
65*d9f75844SAndroid Build Coastguard Worker // Make sure playback is initialized; start playing if enabled.
66*d9f75844SAndroid Build Coastguard Worker UpdateNullAudioPollerState();
67*d9f75844SAndroid Build Coastguard Worker auto* adm = config_.audio_device_module.get();
68*d9f75844SAndroid Build Coastguard Worker if (!adm->Playing()) {
69*d9f75844SAndroid Build Coastguard Worker if (adm->InitPlayout() == 0) {
70*d9f75844SAndroid Build Coastguard Worker if (playout_enabled_) {
71*d9f75844SAndroid Build Coastguard Worker adm->StartPlayout();
72*d9f75844SAndroid Build Coastguard Worker }
73*d9f75844SAndroid Build Coastguard Worker } else {
74*d9f75844SAndroid Build Coastguard Worker RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout.";
75*d9f75844SAndroid Build Coastguard Worker }
76*d9f75844SAndroid Build Coastguard Worker }
77*d9f75844SAndroid Build Coastguard Worker }
78*d9f75844SAndroid Build Coastguard Worker
RemoveReceivingStream(webrtc::AudioReceiveStreamInterface * stream)79*d9f75844SAndroid Build Coastguard Worker void AudioState::RemoveReceivingStream(
80*d9f75844SAndroid Build Coastguard Worker webrtc::AudioReceiveStreamInterface* stream) {
81*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
82*d9f75844SAndroid Build Coastguard Worker auto count = receiving_streams_.erase(stream);
83*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(1, count);
84*d9f75844SAndroid Build Coastguard Worker config_.audio_mixer->RemoveSource(
85*d9f75844SAndroid Build Coastguard Worker static_cast<AudioReceiveStreamImpl*>(stream));
86*d9f75844SAndroid Build Coastguard Worker UpdateNullAudioPollerState();
87*d9f75844SAndroid Build Coastguard Worker if (receiving_streams_.empty()) {
88*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StopPlayout();
89*d9f75844SAndroid Build Coastguard Worker }
90*d9f75844SAndroid Build Coastguard Worker }
91*d9f75844SAndroid Build Coastguard Worker
AddSendingStream(webrtc::AudioSendStream * stream,int sample_rate_hz,size_t num_channels)92*d9f75844SAndroid Build Coastguard Worker void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
93*d9f75844SAndroid Build Coastguard Worker int sample_rate_hz,
94*d9f75844SAndroid Build Coastguard Worker size_t num_channels) {
95*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
96*d9f75844SAndroid Build Coastguard Worker auto& properties = sending_streams_[stream];
97*d9f75844SAndroid Build Coastguard Worker properties.sample_rate_hz = sample_rate_hz;
98*d9f75844SAndroid Build Coastguard Worker properties.num_channels = num_channels;
99*d9f75844SAndroid Build Coastguard Worker UpdateAudioTransportWithSendingStreams();
100*d9f75844SAndroid Build Coastguard Worker
101*d9f75844SAndroid Build Coastguard Worker // Make sure recording is initialized; start recording if enabled.
102*d9f75844SAndroid Build Coastguard Worker auto* adm = config_.audio_device_module.get();
103*d9f75844SAndroid Build Coastguard Worker if (!adm->Recording()) {
104*d9f75844SAndroid Build Coastguard Worker if (adm->InitRecording() == 0) {
105*d9f75844SAndroid Build Coastguard Worker if (recording_enabled_) {
106*d9f75844SAndroid Build Coastguard Worker adm->StartRecording();
107*d9f75844SAndroid Build Coastguard Worker }
108*d9f75844SAndroid Build Coastguard Worker } else {
109*d9f75844SAndroid Build Coastguard Worker RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
110*d9f75844SAndroid Build Coastguard Worker }
111*d9f75844SAndroid Build Coastguard Worker }
112*d9f75844SAndroid Build Coastguard Worker }
113*d9f75844SAndroid Build Coastguard Worker
RemoveSendingStream(webrtc::AudioSendStream * stream)114*d9f75844SAndroid Build Coastguard Worker void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
115*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
116*d9f75844SAndroid Build Coastguard Worker auto count = sending_streams_.erase(stream);
117*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(1, count);
118*d9f75844SAndroid Build Coastguard Worker UpdateAudioTransportWithSendingStreams();
119*d9f75844SAndroid Build Coastguard Worker if (sending_streams_.empty()) {
120*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StopRecording();
121*d9f75844SAndroid Build Coastguard Worker }
122*d9f75844SAndroid Build Coastguard Worker }
123*d9f75844SAndroid Build Coastguard Worker
SetPlayout(bool enabled)124*d9f75844SAndroid Build Coastguard Worker void AudioState::SetPlayout(bool enabled) {
125*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << "SetPlayout(" << enabled << ")";
126*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
127*d9f75844SAndroid Build Coastguard Worker if (playout_enabled_ != enabled) {
128*d9f75844SAndroid Build Coastguard Worker playout_enabled_ = enabled;
129*d9f75844SAndroid Build Coastguard Worker if (enabled) {
130*d9f75844SAndroid Build Coastguard Worker UpdateNullAudioPollerState();
131*d9f75844SAndroid Build Coastguard Worker if (!receiving_streams_.empty()) {
132*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StartPlayout();
133*d9f75844SAndroid Build Coastguard Worker }
134*d9f75844SAndroid Build Coastguard Worker } else {
135*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StopPlayout();
136*d9f75844SAndroid Build Coastguard Worker UpdateNullAudioPollerState();
137*d9f75844SAndroid Build Coastguard Worker }
138*d9f75844SAndroid Build Coastguard Worker }
139*d9f75844SAndroid Build Coastguard Worker }
140*d9f75844SAndroid Build Coastguard Worker
SetRecording(bool enabled)141*d9f75844SAndroid Build Coastguard Worker void AudioState::SetRecording(bool enabled) {
142*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
143*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&thread_checker_);
144*d9f75844SAndroid Build Coastguard Worker if (recording_enabled_ != enabled) {
145*d9f75844SAndroid Build Coastguard Worker recording_enabled_ = enabled;
146*d9f75844SAndroid Build Coastguard Worker if (enabled) {
147*d9f75844SAndroid Build Coastguard Worker if (!sending_streams_.empty()) {
148*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StartRecording();
149*d9f75844SAndroid Build Coastguard Worker }
150*d9f75844SAndroid Build Coastguard Worker } else {
151*d9f75844SAndroid Build Coastguard Worker config_.audio_device_module->StopRecording();
152*d9f75844SAndroid Build Coastguard Worker }
153*d9f75844SAndroid Build Coastguard Worker }
154*d9f75844SAndroid Build Coastguard Worker }
155*d9f75844SAndroid Build Coastguard Worker
SetStereoChannelSwapping(bool enable)156*d9f75844SAndroid Build Coastguard Worker void AudioState::SetStereoChannelSwapping(bool enable) {
157*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(thread_checker_.IsCurrent());
158*d9f75844SAndroid Build Coastguard Worker audio_transport_.SetStereoChannelSwapping(enable);
159*d9f75844SAndroid Build Coastguard Worker }
160*d9f75844SAndroid Build Coastguard Worker
UpdateAudioTransportWithSendingStreams()161*d9f75844SAndroid Build Coastguard Worker void AudioState::UpdateAudioTransportWithSendingStreams() {
162*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(thread_checker_.IsCurrent());
163*d9f75844SAndroid Build Coastguard Worker std::vector<AudioSender*> audio_senders;
164*d9f75844SAndroid Build Coastguard Worker int max_sample_rate_hz = 8000;
165*d9f75844SAndroid Build Coastguard Worker size_t max_num_channels = 1;
166*d9f75844SAndroid Build Coastguard Worker for (const auto& kv : sending_streams_) {
167*d9f75844SAndroid Build Coastguard Worker audio_senders.push_back(kv.first);
168*d9f75844SAndroid Build Coastguard Worker max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz);
169*d9f75844SAndroid Build Coastguard Worker max_num_channels = std::max(max_num_channels, kv.second.num_channels);
170*d9f75844SAndroid Build Coastguard Worker }
171*d9f75844SAndroid Build Coastguard Worker audio_transport_.UpdateAudioSenders(std::move(audio_senders),
172*d9f75844SAndroid Build Coastguard Worker max_sample_rate_hz, max_num_channels);
173*d9f75844SAndroid Build Coastguard Worker }
174*d9f75844SAndroid Build Coastguard Worker
UpdateNullAudioPollerState()175*d9f75844SAndroid Build Coastguard Worker void AudioState::UpdateNullAudioPollerState() {
176*d9f75844SAndroid Build Coastguard Worker // Run NullAudioPoller when there are receiving streams and playout is
177*d9f75844SAndroid Build Coastguard Worker // disabled.
178*d9f75844SAndroid Build Coastguard Worker if (!receiving_streams_.empty() && !playout_enabled_) {
179*d9f75844SAndroid Build Coastguard Worker if (!null_audio_poller_.Running()) {
180*d9f75844SAndroid Build Coastguard Worker AudioTransport* audio_transport = &audio_transport_;
181*d9f75844SAndroid Build Coastguard Worker null_audio_poller_ = RepeatingTaskHandle::Start(
182*d9f75844SAndroid Build Coastguard Worker TaskQueueBase::Current(), [audio_transport] {
183*d9f75844SAndroid Build Coastguard Worker static constexpr size_t kNumChannels = 1;
184*d9f75844SAndroid Build Coastguard Worker static constexpr uint32_t kSamplesPerSecond = 48'000;
185*d9f75844SAndroid Build Coastguard Worker // 10ms of samples
186*d9f75844SAndroid Build Coastguard Worker static constexpr size_t kNumSamples = kSamplesPerSecond / 100;
187*d9f75844SAndroid Build Coastguard Worker
188*d9f75844SAndroid Build Coastguard Worker // Buffer to hold the audio samples.
189*d9f75844SAndroid Build Coastguard Worker int16_t buffer[kNumSamples * kNumChannels];
190*d9f75844SAndroid Build Coastguard Worker
191*d9f75844SAndroid Build Coastguard Worker // Output variables from `NeedMorePlayData`.
192*d9f75844SAndroid Build Coastguard Worker size_t n_samples;
193*d9f75844SAndroid Build Coastguard Worker int64_t elapsed_time_ms;
194*d9f75844SAndroid Build Coastguard Worker int64_t ntp_time_ms;
195*d9f75844SAndroid Build Coastguard Worker audio_transport->NeedMorePlayData(
196*d9f75844SAndroid Build Coastguard Worker kNumSamples, sizeof(int16_t), kNumChannels, kSamplesPerSecond,
197*d9f75844SAndroid Build Coastguard Worker buffer, n_samples, &elapsed_time_ms, &ntp_time_ms);
198*d9f75844SAndroid Build Coastguard Worker
199*d9f75844SAndroid Build Coastguard Worker // Reschedule the next poll iteration.
200*d9f75844SAndroid Build Coastguard Worker return TimeDelta::Millis(10);
201*d9f75844SAndroid Build Coastguard Worker });
202*d9f75844SAndroid Build Coastguard Worker }
203*d9f75844SAndroid Build Coastguard Worker } else {
204*d9f75844SAndroid Build Coastguard Worker null_audio_poller_.Stop();
205*d9f75844SAndroid Build Coastguard Worker }
206*d9f75844SAndroid Build Coastguard Worker }
207*d9f75844SAndroid Build Coastguard Worker } // namespace internal
208*d9f75844SAndroid Build Coastguard Worker
Create(const AudioState::Config & config)209*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioState> AudioState::Create(
210*d9f75844SAndroid Build Coastguard Worker const AudioState::Config& config) {
211*d9f75844SAndroid Build Coastguard Worker return rtc::make_ref_counted<internal::AudioState>(config);
212*d9f75844SAndroid Build Coastguard Worker }
213*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc
214