@@ -98,14 +98,26 @@ void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
9898 UpdateAudioTransportWithSendingStreams ();
9999
100100 // 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 ();
101+ if (ShouldRecord ()) {
102+ auto * adm = config_.audio_device_module .get ();
103+ if (!adm->Recording ()) {
104+ if (adm->InitRecording () == 0 ) {
105+ if (recording_enabled_) {
106+
107+ // TODO: Verify if the following windows only logic is still required.
108+ #if defined(WEBRTC_WIN)
109+ if (adm->BuiltInAECIsAvailable () && !adm->Playing ()) {
110+ if (!adm->PlayoutIsInitialized ()) {
111+ adm->InitPlayout ();
112+ }
113+ adm->StartPlayout ();
114+ }
115+ #endif
116+ adm->StartRecording ();
117+ }
118+ } else {
119+ RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording." ;
106120 }
107- } else {
108- RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording." ;
109121 }
110122 }
111123}
@@ -115,7 +127,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
115127 auto count = sending_streams_.erase (stream);
116128 RTC_DCHECK_EQ (1 , count);
117129 UpdateAudioTransportWithSendingStreams ();
118- if (sending_streams_.empty ()) {
130+
131+ if (!ShouldRecord ()) {
119132 config_.audio_device_module ->StopRecording ();
120133 }
121134}
@@ -143,7 +156,7 @@ void AudioState::SetRecording(bool enabled) {
143156 if (recording_enabled_ != enabled) {
144157 recording_enabled_ = enabled;
145158 if (enabled) {
146- if (!sending_streams_. empty ()) {
159+ if (ShouldRecord ()) {
147160 config_.audio_device_module ->StartRecording ();
148161 }
149162 } else {
@@ -203,6 +216,39 @@ void AudioState::UpdateNullAudioPollerState() {
203216 null_audio_poller_.Stop ();
204217 }
205218}
219+
220+ void AudioState::OnMuteStreamChanged () {
221+
222+ auto * adm = config_.audio_device_module .get ();
223+ bool should_record = ShouldRecord ();
224+
225+ if (should_record && !adm->Recording ()) {
226+ if (adm->InitRecording () == 0 ) {
227+ adm->StartRecording ();
228+ }
229+ } else if (!should_record && adm->Recording ()) {
230+ adm->StopRecording ();
231+ }
232+ }
233+
234+ bool AudioState::ShouldRecord () {
235+ // no streams to send
236+ if (sending_streams_.empty ()) {
237+ return false ;
238+ }
239+
240+ int stream_count = sending_streams_.size ();
241+
242+ int muted_count = 0 ;
243+ for (const auto & kv : sending_streams_) {
244+ if (kv.first ->GetMuted ()) {
245+ muted_count++;
246+ }
247+ }
248+
249+ return muted_count != stream_count;
250+ }
251+
206252} // namespace internal
207253
208254rtc::scoped_refptr<AudioState> AudioState::Create (
0 commit comments