Skip to content

Commit c7167ee

Browse files
hiroshihoriedavidliu
authored andcommitted
release mic when category changes (webrtc-sdk#5)
1 parent febc492 commit c7167ee

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
lines changed

sdk/objc/components/audio/RTCAudioSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ RTC_OBJC_EXPORT
103103
audioUnitStartFailedWithError:(NSError *)error;
104104

105105
/** Called when audio session changed from output-only to input & output */
106-
- (void)audioSessionWillRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession;
106+
- (void)audioSessionDidChangeRecordingEnabled:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession;
107107

108108
@end
109109

sdk/objc/components/audio/RTCAudioSession.mm

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ - (instancetype)initWithAudioSession:(id)audioSession {
114114
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
115115
context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class];
116116

117-
self.isRecordingEnabled = [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord];
117+
_isRecordingEnabled = [self sessionCategoryIsRecordingEnabled];
118118

119119
RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self);
120120
}
@@ -542,14 +542,13 @@ - (void)handleRouteChangeNotification:(NSNotification *)notification {
542542
RTCLog(@"Audio route changed: OldDeviceUnavailable");
543543
break;
544544
case AVAudioSessionRouteChangeReasonCategoryChange:
545-
RTCLog(@"Audio route changed: CategoryChange to :%@",
546-
self.session.category);
547-
if (!self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
548-
self.isRecordingEnabled = true;
549-
[self notifyWillRecord];
550-
}
551-
if (self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayback]) {
552-
self.isRecordingEnabled = false;
545+
RTCLog(@"Audio route changed: CategoryChange to :%@", self.session.category);
546+
{
547+
BOOL newValue = [self sessionCategoryIsRecordingEnabled];
548+
if (_isRecordingEnabled != newValue) {
549+
_isRecordingEnabled = newValue;
550+
[self notifyDidChangeAudioSessionRecordingEnabled];
551+
}
553552
}
554553
break;
555554
case AVAudioSessionRouteChangeReasonOverride:
@@ -782,7 +781,7 @@ - (BOOL)unconfigureWebRTCSession:(NSError **)outError {
782781
}
783782
RTCLog(@"Unconfiguring audio session for WebRTC.");
784783
[self setActive:NO error:outError];
785-
self.isRecordingEnabled = NO;
784+
_isRecordingEnabled = NO;
786785

787786
return YES;
788787
}
@@ -1007,14 +1006,18 @@ - (void)notifyFailedToSetActive:(BOOL)active error:(NSError *)error {
10071006
}
10081007
}
10091008

1010-
- (void)notifyWillRecord {
1009+
- (void)notifyDidChangeAudioSessionRecordingEnabled {
10111010
for (auto delegate : self.delegates) {
1012-
SEL sel = @selector(audioSessionWillRecord:);
1011+
SEL sel = @selector(audioSessionDidChangeRecordingEnabled:);
10131012
if ([delegate respondsToSelector:sel]) {
1014-
[delegate audioSessionWillRecord:self];
1013+
[delegate audioSessionDidChangeRecordingEnabled:self];
10151014
}
10161015
}
10171016
}
10181017

1018+
-(BOOL)sessionCategoryIsRecordingEnabled {
1019+
return [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord] ||
1020+
[_session.category isEqualToString:AVAudioSessionCategoryRecord];
1021+
}
10191022

10201023
@end

sdk/objc/components/audio/RTCNativeAudioSessionDelegateAdapter.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ - (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession
8686
_observer->OnChangedOutputVolume();
8787
}
8888

89-
- (void)audioSessionWillRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
89+
- (void)audioSessionDidChangeRecordingEnabled:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
9090
// re-trigger audio unit init, by using interrupt ended callback
91-
_observer->OnAudioWillRecord();
91+
_observer->OnChangedRecordingEnabled();
9292
}
9393

9494
@end

sdk/objc/native/src/audio/audio_device_ios.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
145145
void OnValidRouteChange() override;
146146
void OnCanPlayOrRecordChange(bool can_play_or_record) override;
147147
void OnChangedOutputVolume() override;
148-
void OnAudioWillRecord() override;
148+
void OnChangedRecordingEnabled() override;
149149

150150
// VoiceProcessingAudioUnitObserver methods.
151151
OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
@@ -173,7 +173,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
173173
void HandleSampleRateChange(float sample_rate);
174174
void HandlePlayoutGlitchDetected();
175175
void HandleOutputVolumeChange();
176-
void HandleAudioWillRecord();
176+
void HandleAudioSessionRecordingEnabledChange();
177177

178178
// Uses current `playout_parameters_` and `record_parameters_` to inform the
179179
// audio device buffer (ADB) about our internal audio parameters.

sdk/objc/native/src/audio/audio_device_ios.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
kMessageTypeCanPlayOrRecordChange,
6969
kMessageTypePlayoutGlitchDetected,
7070
kMessageOutputVolumeChange,
71-
kMessageTypeAudioWillRecord,
71+
kMessageTypeRecordingEnabledChange,
7272
};
7373

7474
using ios::CheckAndLogError;
@@ -374,9 +374,9 @@ static void LogDeviceInfo() {
374374
thread_->Post(RTC_FROM_HERE, this, kMessageOutputVolumeChange);
375375
}
376376

377-
void AudioDeviceIOS::OnAudioWillRecord() {
377+
void AudioDeviceIOS::OnChangedRecordingEnabled() {
378378
RTC_DCHECK(thread_);
379-
thread_->Post(RTC_FROM_HERE, this, kMessageTypeAudioWillRecord);
379+
thread_->Post(RTC_FROM_HERE, this, kMessageTypeRecordingEnabledChange);
380380
}
381381

382382
OSStatus AudioDeviceIOS::OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
@@ -509,8 +509,9 @@ static void LogDeviceInfo() {
509509
case kMessageOutputVolumeChange:
510510
HandleOutputVolumeChange();
511511
break;
512-
case kMessageTypeAudioWillRecord:
513-
HandleAudioWillRecord();
512+
case kMessageTypeRecordingEnabledChange:
513+
HandleAudioSessionRecordingEnabledChange();
514+
break;
514515
}
515516
}
516517

@@ -681,10 +682,10 @@ static void LogDeviceInfo() {
681682
last_output_volume_change_time_ = rtc::TimeMillis();
682683
}
683684

684-
void AudioDeviceIOS::HandleAudioWillRecord() {
685+
void AudioDeviceIOS::HandleAudioSessionRecordingEnabledChange() {
685686
RTC_DCHECK_RUN_ON(&thread_checker_);
686687

687-
LOGI() << "HandleAudioWillRecord";
688+
LOGI() << "HandleAudioSessionRecordingEnabledChange";
688689

689690
// If we don't have an audio unit yet, or the audio unit is uninitialized,
690691
// there is no work to do.

sdk/objc/native/src/audio/audio_session_observer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AudioSessionObserver {
3232

3333
virtual void OnChangedOutputVolume() = 0;
3434

35-
virtual void OnAudioWillRecord() = 0;
35+
virtual void OnChangedRecordingEnabled() = 0;
3636

3737
protected:
3838
virtual ~AudioSessionObserver() {}

0 commit comments

Comments
 (0)