Skip to content

Commit

Permalink
AudioUnit: Don't rely on category switch for mic indicator to turn off (
Browse files Browse the repository at this point in the history
#52)

* progress

* tweak

* clean

* simplify audio unit restart

call to SetupAudioBuffersForActiveAudioSession() might not be needed since sample rate won't change during restart. This might help reduce the unwanted noise when restarting audio unit.

* clean
  • Loading branch information
hiroshihorie authored Nov 23, 2022
1 parent 3fcec9f commit 8bd3991
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 110 deletions.
2 changes: 0 additions & 2 deletions sdk/objc/components/audio/RTCAudioSession+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic, assign) BOOL isInterrupted;

@property(nonatomic, strong) NSString *activeCategory;

/** Adds the delegate to the list of delegates, and places it at the front of
* the list. This delegate will be notified before other delegates of
* audio events.
Expand Down
3 changes: 0 additions & 3 deletions sdk/objc/components/audio/RTCAudioSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ RTC_OBJC_EXPORT
- (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession
audioUnitStartFailedWithError:(NSError *)error;

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

@end

/** This is a protocol used to inform RTCAudioSession when the audio session
Expand Down
18 changes: 0 additions & 18 deletions sdk/objc/components/audio/RTCAudioSession.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ - (instancetype)initWithAudioSession:(id)audioSession {
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class];

_activeCategory = _session.category;

RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self);
}
return self;
Expand Down Expand Up @@ -543,13 +541,6 @@ - (void)handleRouteChangeNotification:(NSNotification *)notification {
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
RTCLog(@"Audio route changed: CategoryChange to :%@", self.session.category);
{
if (![_session.category isEqualToString:_activeCategory]) {
_activeCategory = _session.category;
RTCLog(@"Audio route changed: Restarting Audio Unit");
[self notifyDidChangeAudioSessionRecordingEnabled];
}
}
break;
case AVAudioSessionRouteChangeReasonOverride:
RTCLog(@"Audio route changed: Override");
Expand Down Expand Up @@ -1005,13 +996,4 @@ - (void)notifyFailedToSetActive:(BOOL)active error:(NSError *)error {
}
}

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

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

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

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

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

bool RestartAudioUnit(bool enable_input);

// Uses current `playout_parameters_` and `record_parameters_` to inform the
// audio device buffer (ADB) about our internal audio parameters.
Expand Down Expand Up @@ -205,7 +205,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric,

// Activates our audio session, creates and initializes the voice-processing
// audio unit and verifies that we got the preferred native audio parameters.
bool InitPlayOrRecord();
bool InitPlayOrRecord(bool enable_input);

// Closes and deletes the voice-processing I/O unit.
void ShutdownPlayOrRecord();
Expand Down Expand Up @@ -269,19 +269,21 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
// will be changed dynamically to account for this behavior.
rtc::BufferT<int16_t> record_audio_buffer_;

// Set to 1 when recording is initialized and 0 otherwise.
volatile int recording_is_initialized_;

// Set to 1 when recording is active and 0 otherwise.
volatile int recording_;

// Set to 1 when playout is initialized and 0 otherwise.
volatile int playout_is_initialized_;

// Set to 1 when playout is active and 0 otherwise.
volatile int playing_;

// Set to true after successful call to Init(), false otherwise.
bool initialized_ RTC_GUARDED_BY(thread_checker_);

// Set to true after successful call to InitRecording() or InitPlayout(),
// false otherwise.
bool audio_is_initialized_;

// Set to true if audio session is interrupted, false otherwise.
bool is_interrupted_;

Expand Down
Loading

0 comments on commit 8bd3991

Please sign in to comment.