@@ -23,6 +23,9 @@ typedef NS_ENUM(NSInteger, FishjamAudioUnitState) {
2323@interface FishjamRTCAudioDevice () {
2424 AudioUnit _vpioUnit;
2525 FishjamAudioUnitState _state;
26+ Float64 _configuredSampleRate;
27+ double _reportedInputSampleRate;
28+ double _reportedOutputSampleRate;
2629}
2730
2831@property (nonatomic , weak ) id <RTCAudioDeviceDelegate> delegate;
@@ -39,6 +42,9 @@ - (instancetype)init {
3942 if (self) {
4043 _vpioUnit = NULL ;
4144 _state = FishjamAudioUnitStateInitRequired;
45+ _configuredSampleRate = 0.0 ;
46+ _reportedInputSampleRate = 0.0 ;
47+ _reportedOutputSampleRate = 0.0 ;
4248 }
4349 return self;
4450}
@@ -54,11 +60,11 @@ - (AVAudioSession *)audioSession {
5460#pragma mark - RTCAudioDevice properties
5561
5662- (double )deviceInputSampleRate {
57- return self.audioSession .sampleRate ;
63+ return _reportedInputSampleRate > 0 ? _reportedInputSampleRate : self.audioSession .sampleRate ;
5864}
5965
6066- (double )deviceOutputSampleRate {
61- return self.audioSession .sampleRate ;
67+ return _reportedOutputSampleRate > 0 ? _reportedOutputSampleRate : self.audioSession .sampleRate ;
6268}
6369
6470- (NSTimeInterval )inputIOBufferDuration {
@@ -108,6 +114,8 @@ - (BOOL)terminateDevice {
108114 self.shouldPlay = NO ;
109115 self.shouldRecord = NO ;
110116 [self disposeAudioUnit ];
117+ _reportedInputSampleRate = 0.0 ;
118+ _reportedOutputSampleRate = 0.0 ;
111119 self.delegate = nil ;
112120 return YES ;
113121}
@@ -464,6 +472,7 @@ - (BOOL)initializeAudioUnitWithSampleRate:(Float64)sampleRate {
464472 }
465473 }
466474
475+ _configuredSampleRate = sampleRate;
467476 _state = FishjamAudioUnitStateInitialized;
468477 return YES ;
469478}
@@ -529,6 +538,7 @@ - (void)disposeAudioUnit {
529538 }
530539 _vpioUnit = NULL ;
531540 _state = FishjamAudioUnitStateInitRequired;
541+ _configuredSampleRate = 0.0 ;
532542}
533543
534544#pragma mark - Top-level update (mirrors audio_device_ios.mm UpdateAudioUnit)
@@ -565,6 +575,15 @@ - (void)updateAudioUnit {
565575
566576 [self configureAudioSession ];
567577
578+ Float64 targetSampleRate = self.audioSession .sampleRate ;
579+ if (targetSampleRate <= 0 ) {
580+ targetSampleRate = kHighPerformanceSampleRate ;
581+ }
582+
583+ if (_vpioUnit != NULL && _state >= FishjamAudioUnitStateInitialized && _configuredSampleRate != targetSampleRate) {
584+ [self uninitializeAudioUnit ];
585+ }
586+
568587 if ([self audioUnitNeedsRecreate ]) {
569588 [self disposeAudioUnit ];
570589 if (![self createAudioUnit ]) {
@@ -573,14 +592,52 @@ - (void)updateAudioUnit {
573592 }
574593
575594 if (_state == FishjamAudioUnitStateUninitialized) {
576- if (![self initializeAudioUnitWithSampleRate: self .audioSession.sampleRate ]) {
595+ if (![self initializeAudioUnitWithSampleRate: targetSampleRate ]) {
577596 return ;
578597 }
579598 }
580599
581600 if (_state == FishjamAudioUnitStateInitialized) {
582- [self startAudioUnit ];
601+ if (![self startAudioUnit ]) {
602+ return ;
603+ }
604+ }
605+
606+ [self notifySampleRateChangeIfNeeded ];
607+ }
608+
609+ - (void )notifySampleRateChangeIfNeeded {
610+ id <RTCAudioDeviceDelegate> delegate = self.delegate ;
611+ if (delegate == nil || _configuredSampleRate <= 0 ) {
612+ return ;
613+ }
614+
615+ BOOL inputChanged = self.shouldRecord && _reportedInputSampleRate != _configuredSampleRate;
616+ BOOL outputChanged = self.shouldPlay && _reportedOutputSampleRate != _configuredSampleRate;
617+ if (!inputChanged && !outputChanged) {
618+ return ;
619+ }
620+
621+ if (inputChanged) {
622+ _reportedInputSampleRate = _configuredSampleRate;
583623 }
624+ if (outputChanged) {
625+ _reportedOutputSampleRate = _configuredSampleRate;
626+ }
627+
628+ __weak typeof (self) weakSelf = self;
629+ [delegate dispatchAsync: ^{
630+ id <RTCAudioDeviceDelegate> strongDelegate = weakSelf.delegate ;
631+ if (strongDelegate == nil ) {
632+ return ;
633+ }
634+ if (inputChanged) {
635+ [strongDelegate notifyAudioInputParametersChange ];
636+ }
637+ if (outputChanged) {
638+ [strongDelegate notifyAudioOutputParametersChange ];
639+ }
640+ }];
584641}
585642
586643#pragma mark - Audio Session Notifications
0 commit comments