Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release mic when category changes #5

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/objc/components/audio/RTCAudioSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ RTC_OBJC_EXPORT
error:(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 @@ -103,7 +103,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 @@ -492,14 +492,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 @@ -713,7 +712,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 @@ -926,14 +925,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 @@ -368,9 +368,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 @@ -503,8 +503,9 @@ static void LogDeviceInfo() {
case kMessageOutputVolumeChange:
HandleOutputVolumeChange();
break;
case kMessageTypeAudioWillRecord:
HandleAudioWillRecord();
case kMessageTypeRecordingEnabledChange:
HandleAudioSessionRecordingEnabledChange();
break;
}
}

Expand Down Expand Up @@ -668,10 +669,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