• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "audio/audio_state.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <utility>
16 #include <vector>
17 
18 #include "audio/audio_receive_stream.h"
19 #include "audio/audio_send_stream.h"
20 #include "modules/audio_device/include/audio_device.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/ref_counted_object.h"
24 #include "rtc_base/thread.h"
25 
26 namespace webrtc {
27 namespace internal {
28 
AudioState(const AudioState::Config & config)29 AudioState::AudioState(const AudioState::Config& config)
30     : config_(config),
31       audio_transport_(config_.audio_mixer, config_.audio_processing.get()) {
32   process_thread_checker_.Detach();
33   RTC_DCHECK(config_.audio_mixer);
34   RTC_DCHECK(config_.audio_device_module);
35 }
36 
~AudioState()37 AudioState::~AudioState() {
38   RTC_DCHECK(thread_checker_.IsCurrent());
39   RTC_DCHECK(receiving_streams_.empty());
40   RTC_DCHECK(sending_streams_.empty());
41 }
42 
audio_processing()43 AudioProcessing* AudioState::audio_processing() {
44   return config_.audio_processing.get();
45 }
46 
audio_transport()47 AudioTransport* AudioState::audio_transport() {
48   return &audio_transport_;
49 }
50 
typing_noise_detected() const51 bool AudioState::typing_noise_detected() const {
52   RTC_DCHECK(thread_checker_.IsCurrent());
53   return audio_transport_.typing_noise_detected();
54 }
55 
AddReceivingStream(webrtc::AudioReceiveStream * stream)56 void AudioState::AddReceivingStream(webrtc::AudioReceiveStream* stream) {
57   RTC_DCHECK(thread_checker_.IsCurrent());
58   RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
59   receiving_streams_.insert(stream);
60   if (!config_.audio_mixer->AddSource(
61           static_cast<internal::AudioReceiveStream*>(stream))) {
62     RTC_DLOG(LS_ERROR) << "Failed to add source to mixer.";
63   }
64 
65   // Make sure playback is initialized; start playing if enabled.
66   UpdateNullAudioPollerState();
67   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 
RemoveReceivingStream(webrtc::AudioReceiveStream * stream)79 void AudioState::RemoveReceivingStream(webrtc::AudioReceiveStream* stream) {
80   RTC_DCHECK(thread_checker_.IsCurrent());
81   auto count = receiving_streams_.erase(stream);
82   RTC_DCHECK_EQ(1, count);
83   config_.audio_mixer->RemoveSource(
84       static_cast<internal::AudioReceiveStream*>(stream));
85   UpdateNullAudioPollerState();
86   if (receiving_streams_.empty()) {
87     config_.audio_device_module->StopPlayout();
88   }
89 }
90 
AddSendingStream(webrtc::AudioSendStream * stream,int sample_rate_hz,size_t num_channels)91 void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
92                                   int sample_rate_hz,
93                                   size_t num_channels) {
94   RTC_DCHECK(thread_checker_.IsCurrent());
95   auto& properties = sending_streams_[stream];
96   properties.sample_rate_hz = sample_rate_hz;
97   properties.num_channels = num_channels;
98   UpdateAudioTransportWithSendingStreams();
99 
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   }
111 }
112 
RemoveSendingStream(webrtc::AudioSendStream * stream)113 void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
114   RTC_DCHECK(thread_checker_.IsCurrent());
115   auto count = sending_streams_.erase(stream);
116   RTC_DCHECK_EQ(1, count);
117   UpdateAudioTransportWithSendingStreams();
118   if (sending_streams_.empty()) {
119     config_.audio_device_module->StopRecording();
120   }
121 }
122 
SetPlayout(bool enabled)123 void AudioState::SetPlayout(bool enabled) {
124   RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
125   RTC_DCHECK(thread_checker_.IsCurrent());
126   if (playout_enabled_ != enabled) {
127     playout_enabled_ = enabled;
128     if (enabled) {
129       UpdateNullAudioPollerState();
130       if (!receiving_streams_.empty()) {
131         config_.audio_device_module->StartPlayout();
132       }
133     } else {
134       config_.audio_device_module->StopPlayout();
135       UpdateNullAudioPollerState();
136     }
137   }
138 }
139 
SetRecording(bool enabled)140 void AudioState::SetRecording(bool enabled) {
141   RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
142   RTC_DCHECK(thread_checker_.IsCurrent());
143   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   }
153 }
154 
SetStereoChannelSwapping(bool enable)155 void AudioState::SetStereoChannelSwapping(bool enable) {
156   RTC_DCHECK(thread_checker_.IsCurrent());
157   audio_transport_.SetStereoChannelSwapping(enable);
158 }
159 
UpdateAudioTransportWithSendingStreams()160 void AudioState::UpdateAudioTransportWithSendingStreams() {
161   RTC_DCHECK(thread_checker_.IsCurrent());
162   std::vector<AudioSender*> audio_senders;
163   int max_sample_rate_hz = 8000;
164   size_t max_num_channels = 1;
165   for (const auto& kv : sending_streams_) {
166     audio_senders.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_.UpdateAudioSenders(std::move(audio_senders),
171                                       max_sample_rate_hz, max_num_channels);
172 }
173 
UpdateNullAudioPollerState()174 void 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 }
184 }  // namespace internal
185 
Create(const AudioState::Config & config)186 rtc::scoped_refptr<AudioState> AudioState::Create(
187     const AudioState::Config& config) {
188   return new rtc::RefCountedObject<internal::AudioState>(config);
189 }
190 }  // namespace webrtc
191