Skip to content

Commit

Permalink
release mic when category changes (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie authored and davidliu committed Feb 10, 2022
1 parent 1388778 commit b3db1ab
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion sdk/objc/components/audio/RTCAudioSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RTC_OBJC_EXPORT
audioUnitStartFailedWithError:(NSError *)error;

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

@end

Expand Down
29 changes: 16 additions & 13 deletions sdk/objc/components/audio/RTCAudioSession.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ - (instancetype)initWithAudioSession:(id)audioSession {
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class];

self.isRecordingEnabled = [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord];
_isRecordingEnabled = [self sessionCategoryIsRecordingEnabled];

RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self);
}
Expand Down Expand Up @@ -542,14 +542,13 @@ - (void)handleRouteChangeNotification:(NSNotification *)notification {
RTCLog(@"Audio route changed: OldDeviceUnavailable");
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
RTCLog(@"Audio route changed: CategoryChange to :%@",
self.session.category);
if (!self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
self.isRecordingEnabled = true;
[self notifyWillRecord];
}
if (self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayback]) {
self.isRecordingEnabled = false;
RTCLog(@"Audio route changed: CategoryChange to :%@", self.session.category);
{
BOOL newValue = [self sessionCategoryIsRecordingEnabled];
if (_isRecordingEnabled != newValue) {
_isRecordingEnabled = newValue;
[self notifyDidChangeAudioSessionRecordingEnabled];
}
}
break;
case AVAudioSessionRouteChangeReasonOverride:
Expand Down Expand Up @@ -782,7 +781,7 @@ - (BOOL)unconfigureWebRTCSession:(NSError **)outError {
}
RTCLog(@"Unconfiguring audio session for WebRTC.");
[self setActive:NO error:outError];
self.isRecordingEnabled = NO;
_isRecordingEnabled = NO;

return YES;
}
Expand Down Expand Up @@ -1007,14 +1006,18 @@ - (void)notifyFailedToSetActive:(BOOL)active error:(NSError *)error {
}
}

- (void)notifyWillRecord {
- (void)notifyDidChangeAudioSessionRecordingEnabled {
for (auto delegate : self.delegates) {
SEL sel = @selector(audioSessionWillRecord:);
SEL sel = @selector(audioSessionDidChangeRecordingEnabled:);
if ([delegate respondsToSelector:sel]) {
[delegate audioSessionWillRecord:self];
[delegate audioSessionDidChangeRecordingEnabled:self];
}
}
}

-(BOOL)sessionCategoryIsRecordingEnabled {
return [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord] ||
[_session.category isEqualToString:AVAudioSessionCategoryRecord];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ - (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession
_observer->OnChangedOutputVolume();
}

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

@end
4 changes: 2 additions & 2 deletions sdk/objc/native/src/audio/audio_device_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
void OnValidRouteChange() override;
void OnCanPlayOrRecordChange(bool can_play_or_record) override;
void OnChangedOutputVolume() override;
void OnAudioWillRecord() override;
void OnChangedRecordingEnabled() override;

// VoiceProcessingAudioUnitObserver methods.
OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
Expand Down Expand Up @@ -173,7 +173,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
void HandleSampleRateChange(float sample_rate);
void HandlePlayoutGlitchDetected();
void HandleOutputVolumeChange();
void HandleAudioWillRecord();
void HandleAudioSessionRecordingEnabledChange();

// Uses current `playout_parameters_` and `record_parameters_` to inform the
// audio device buffer (ADB) about our internal audio parameters.
Expand Down
15 changes: 8 additions & 7 deletions sdk/objc/native/src/audio/audio_device_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
kMessageTypeCanPlayOrRecordChange,
kMessageTypePlayoutGlitchDetected,
kMessageOutputVolumeChange,
kMessageTypeAudioWillRecord,
kMessageTypeRecordingEnabledChange,
};

using ios::CheckAndLogError;
Expand Down Expand Up @@ -374,9 +374,9 @@ static void LogDeviceInfo() {
thread_->Post(RTC_FROM_HERE, this, kMessageOutputVolumeChange);
}

void AudioDeviceIOS::OnAudioWillRecord() {
void AudioDeviceIOS::OnChangedRecordingEnabled() {
RTC_DCHECK(thread_);
thread_->Post(RTC_FROM_HERE, this, kMessageTypeAudioWillRecord);
thread_->Post(RTC_FROM_HERE, this, kMessageTypeRecordingEnabledChange);
}

OSStatus AudioDeviceIOS::OnDeliverRecordedData(AudioUnitRenderActionFlags* flags,
Expand Down Expand Up @@ -509,8 +509,9 @@ static void LogDeviceInfo() {
case kMessageOutputVolumeChange:
HandleOutputVolumeChange();
break;
case kMessageTypeAudioWillRecord:
HandleAudioWillRecord();
case kMessageTypeRecordingEnabledChange:
HandleAudioSessionRecordingEnabledChange();
break;
}
}

Expand Down Expand Up @@ -681,10 +682,10 @@ static void LogDeviceInfo() {
last_output_volume_change_time_ = rtc::TimeMillis();
}

void AudioDeviceIOS::HandleAudioWillRecord() {
void AudioDeviceIOS::HandleAudioSessionRecordingEnabledChange() {
RTC_DCHECK_RUN_ON(&thread_checker_);

LOGI() << "HandleAudioWillRecord";
LOGI() << "HandleAudioSessionRecordingEnabledChange";

// If we don't have an audio unit yet, or the audio unit is uninitialized,
// there is no work to do.
Expand Down
2 changes: 1 addition & 1 deletion sdk/objc/native/src/audio/audio_session_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AudioSessionObserver {

virtual void OnChangedOutputVolume() = 0;

virtual void OnAudioWillRecord() = 0;
virtual void OnChangedRecordingEnabled() = 0;

protected:
virtual ~AudioSessionObserver() {}
Expand Down

0 comments on commit b3db1ab

Please sign in to comment.