@@ -135,11 +135,23 @@ void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
135135 UpdateAudioTransportWithSendingStreams ();
136136
137137 // Make sure recording is initialized; start recording if enabled.
138- auto * adm = config_. audio_device_module . get ();
139- if (recording_enabled_) {
138+ if ( ShouldRecord ()) {
139+ auto * adm = config_. audio_device_module . get ();
140140 if (!adm->Recording ()) {
141141 if (adm->InitRecording () == 0 ) {
142- adm->StartRecording ();
142+ if (recording_enabled_) {
143+
144+ // TODO: Verify if the following windows only logic is still required.
145+ #if defined(WEBRTC_WIN)
146+ if (adm->BuiltInAECIsAvailable () && !adm->Playing ()) {
147+ if (!adm->PlayoutIsInitialized ()) {
148+ adm->InitPlayout ();
149+ }
150+ adm->StartPlayout ();
151+ }
152+ #endif
153+ adm->StartRecording ();
154+ }
143155 } else {
144156 RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording." ;
145157 }
@@ -152,7 +164,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
152164 auto count = sending_streams_.erase (stream);
153165 RTC_DCHECK_EQ (1 , count);
154166 UpdateAudioTransportWithSendingStreams ();
155- if (sending_streams_.empty ()) {
167+
168+ if (!ShouldRecord ()) {
156169 config_.audio_device_module ->StopRecording ();
157170 }
158171}
@@ -208,6 +221,39 @@ void AudioState::UpdateNullAudioPollerState() {
208221 null_audio_poller_.Stop ();
209222 }
210223}
224+
225+ void AudioState::OnMuteStreamChanged () {
226+
227+ auto * adm = config_.audio_device_module .get ();
228+ bool should_record = ShouldRecord ();
229+
230+ if (should_record && !adm->Recording ()) {
231+ if (adm->InitRecording () == 0 ) {
232+ adm->StartRecording ();
233+ }
234+ } else if (!should_record && adm->Recording ()) {
235+ adm->StopRecording ();
236+ }
237+ }
238+
239+ bool AudioState::ShouldRecord () {
240+ // no streams to send
241+ if (sending_streams_.empty ()) {
242+ return false ;
243+ }
244+
245+ int stream_count = sending_streams_.size ();
246+
247+ int muted_count = 0 ;
248+ for (const auto & kv : sending_streams_) {
249+ if (kv.first ->GetMuted ()) {
250+ muted_count++;
251+ }
252+ }
253+
254+ return muted_count != stream_count;
255+ }
256+
211257} // namespace internal
212258
213259scoped_refptr<AudioState> AudioState::Create (const AudioState::Config& config) {
0 commit comments