@@ -71,6 +71,25 @@ void AudioState::SetPlayout(bool enabled) {
7171 UpdateNullAudioPollerState ();
7272}
7373
74+ void AudioState::SetRecording (bool enabled) {
75+ RTC_LOG (LS_INFO) << " SetRecording(" << enabled << " )" ;
76+ RTC_DCHECK_RUN_ON (&thread_checker_);
77+ auto * adm = config_.audio_device_module .get ();
78+ if (enabled) {
79+ if (ShouldRecord ()) {
80+ if (!adm->Recording ()) {
81+ if (adm->InitRecording () == 0 ) {
82+ adm->StartRecording ();
83+ }
84+ }
85+ }
86+ } else {
87+ // Disable recording.
88+ adm->StopRecording ();
89+ }
90+ recording_enabled_ = enabled;
91+ }
92+
7493void AudioState::AddReceivingStream (
7594 webrtc::AudioReceiveStreamInterface* stream) {
7695 RTC_DCHECK_RUN_ON (&thread_checker_);
@@ -106,25 +125,6 @@ void AudioState::RemoveReceivingStream(
106125 UpdateNullAudioPollerState ();
107126}
108127
109- void AudioState::SetRecording (bool enabled) {
110- RTC_LOG (LS_INFO) << " SetRecording(" << enabled << " )" ;
111- RTC_DCHECK_RUN_ON (&thread_checker_);
112- auto * adm = config_.audio_device_module .get ();
113- if (enabled) {
114- if (!sending_streams_.empty ()) {
115- if (!adm->Recording ()) {
116- if (adm->InitRecording () == 0 ) {
117- adm->StartRecording ();
118- }
119- }
120- }
121- } else {
122- // Disable recording.
123- adm->StopRecording ();
124- }
125- recording_enabled_ = enabled;
126- }
127-
128128void AudioState::AddSendingStream (webrtc::AudioSendStream* stream,
129129 int sample_rate_hz,
130130 size_t num_channels) {
@@ -135,13 +135,13 @@ 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_) {
140- if (!adm-> Recording () ) {
141- if (adm->InitRecording () == 0 ) {
142- adm->StartRecording ();
143- } else {
144- RTC_DLOG_F (LS_ERROR) << " Failed to initialize recording. " ;
138+ if ( ShouldRecord ()) {
139+ auto * adm = config_. audio_device_module . get ();
140+ if (recording_enabled_ ) {
141+ if (! adm->Recording () ) {
142+ if ( adm->InitRecording () == 0 ) {
143+ adm-> StartRecording ();
144+ }
145145 }
146146 }
147147 }
@@ -152,7 +152,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
152152 auto count = sending_streams_.erase (stream);
153153 RTC_DCHECK_EQ (1 , count);
154154 UpdateAudioTransportWithSendingStreams ();
155- if (sending_streams_.empty ()) {
155+
156+ if (!ShouldRecord ()) {
156157 config_.audio_device_module ->StopRecording ();
157158 }
158159}
@@ -208,6 +209,39 @@ void AudioState::UpdateNullAudioPollerState() {
208209 null_audio_poller_.Stop ();
209210 }
210211}
212+
213+ void AudioState::OnMuteStreamChanged () {
214+
215+ auto * adm = config_.audio_device_module .get ();
216+ bool should_record = ShouldRecord ();
217+
218+ if (should_record && !adm->Recording ()) {
219+ if (adm->InitRecording () == 0 ) {
220+ adm->StartRecording ();
221+ }
222+ } else if (!should_record && adm->Recording ()) {
223+ adm->StopRecording ();
224+ }
225+ }
226+
227+ bool AudioState::ShouldRecord () {
228+ // no streams to send
229+ if (sending_streams_.empty ()) {
230+ return false ;
231+ }
232+
233+ int stream_count = sending_streams_.size ();
234+
235+ int muted_count = 0 ;
236+ for (const auto & kv : sending_streams_) {
237+ if (kv.first ->GetMuted ()) {
238+ muted_count++;
239+ }
240+ }
241+
242+ return muted_count != stream_count;
243+ }
244+
211245} // namespace internal
212246
213247scoped_refptr<AudioState> AudioState::Create (const AudioState::Config& config) {
0 commit comments