@@ -98,22 +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_) {
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.
105108#if defined(WEBRTC_WIN)
106- if (adm->BuiltInAECIsAvailable () && !adm->Playing ()) {
107- if (!adm->PlayoutIsInitialized ()) {
108- adm->InitPlayout ();
109+ if (adm->BuiltInAECIsAvailable () && !adm->Playing ()) {
110+ if (!adm->PlayoutIsInitialized ()) {
111+ adm->InitPlayout ();
112+ }
113+ adm->StartPlayout ();
109114 }
110- adm->StartPlayout ();
111- }
112115#endif
113- adm->StartRecording ();
116+ adm->StartRecording ();
117+ }
118+ } else {
119+ RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording." ;
114120 }
115- } else {
116- RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording." ;
117121 }
118122 }
119123}
@@ -123,7 +127,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
123127 auto count = sending_streams_.erase (stream);
124128 RTC_DCHECK_EQ (1 , count);
125129 UpdateAudioTransportWithSendingStreams ();
126- if (sending_streams_.empty ()) {
130+
131+ if (!ShouldRecord ()) {
127132 config_.audio_device_module ->StopRecording ();
128133 }
129134}
@@ -151,7 +156,7 @@ void AudioState::SetRecording(bool enabled) {
151156 if (recording_enabled_ != enabled) {
152157 recording_enabled_ = enabled;
153158 if (enabled) {
154- if (!sending_streams_. empty ()) {
159+ if (ShouldRecord ()) {
155160 config_.audio_device_module ->StartRecording ();
156161 }
157162 } else {
@@ -211,6 +216,39 @@ void AudioState::UpdateNullAudioPollerState() {
211216 null_audio_poller_.Stop ();
212217 }
213218}
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+
214252} // namespace internal
215253
216254rtc::scoped_refptr<AudioState> AudioState::Create (
0 commit comments