Skip to content

Commit 61e2247

Browse files
hiroshihoriecloudwebrtc
authored andcommitted
release mic when category changes (#5)
1 parent d2da856 commit 61e2247

File tree

6 files changed

+26
-23
lines changed

6 files changed

+26
-23
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
@@ -147,7 +147,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
147147
void OnValidRouteChange() override;
148148
void OnCanPlayOrRecordChange(bool can_play_or_record) override;
149149
void OnChangedOutputVolume() override;
150-
void OnAudioWillRecord() override;
150+
void OnChangedRecordingEnabled() override;
151151

152152
// VoiceProcessingAudioUnitObserver methods.
153153
OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
@@ -172,7 +172,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
172172
void HandleSampleRateChange();
173173
void HandlePlayoutGlitchDetected();
174174
void HandleOutputVolumeChange();
175-
void HandleAudioWillRecord();
175+
void HandleAudioSessionRecordingEnabledChange();
176176

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ static void LogDeviceInfo() {
360360
thread_->PostTask(SafeTask(safety_, [this] { HandleOutputVolumeChange(); }));
361361
}
362362

363-
void AudioDeviceIOS::OnAudioWillRecord() {
363+
void AudioDeviceIOS::OnChangedRecordingEnabled() {
364364
RTC_DCHECK(thread_);
365-
thread_->PostTask(SafeTask(safety_, [this] { HandleAudioWillRecord(); }));
365+
thread_->PostTask(SafeTask(safety_, [this] { HandleAudioSessionRecordingEnabledChange(); }));
366366
}
367367

368368
OSStatus AudioDeviceIOS::OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
@@ -638,10 +638,10 @@ static void LogDeviceInfo() {
638638
last_output_volume_change_time_ = rtc::TimeMillis();
639639
}
640640

641-
void AudioDeviceIOS::HandleAudioWillRecord() {
641+
void AudioDeviceIOS::HandleAudioSessionRecordingEnabledChange() {
642642
RTC_DCHECK_RUN_ON(&io_thread_checker_);
643643

644-
LOGI() << "HandleAudioWillRecord";
644+
LOGI() << "HandleAudioSessionRecordingEnabledChange";
645645

646646
// If we don't have an audio unit yet, or the audio unit is uninitialized,
647647
// 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)