66#pragma clang diagnostic push
77#pragma clang diagnostic ignored "-Wimplicit-retain-self"
88
9+ @interface NSCAudioBufferSourceNode ()
10+ - (nullable NSCAudioBuffer *)processBuffer : (nullable NSCAudioBuffer *)input context : (NSCAudioContext *)context ;
11+ - (void )scheduleRetryWithContext : (NSCAudioContext *)ctx ;
12+ - (void )registerPendingIfNeededWithContext : (NSCAudioContext *)ctx ;
13+ @end
14+
915@implementation NSCAudioBufferSourceNode {
1016 AVAudioPlayerNode *_player;
1117 NSCAudioBuffer *_buffer;
@@ -151,28 +157,13 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
151157 AVAudioEngine *eng = ctx.engine ;
152158 if (!eng)
153159 return NO ;
154- if (!eng.isRunning )
160+ if (!eng.isRunning ) {
161+ NSCLogError (@" NSCAudioBSN: tryStartPlayer engine NOT running – returning NO" );
155162 return NO ;
156-
157- @try {
158- AVAudioTime *lrt = nil ;
159- @try {
160- lrt = eng.outputNode .lastRenderTime ;
161- } @catch (NSException *e) {
162- lrt = nil ;
163- }
164- if (lrt) {
165- NSCLogDebug (@" NSCAudioBufferSourceNode: tryStartPlayer initial engRunning=%d "
166- @" lastRenderTime=%p sampleTime=%lld sampleRate=%f " ,
167- (int )eng.isRunning, lrt, (long long )lrt.sampleTime, lrt.sampleRate);
168- } else {
169- NSCLogDebug (@" NSCAudioBufferSourceNode: tryStartPlayer initial engRunning=%d "
170- @" lastRenderTime=NULL" ,
171- (int )eng.isRunning);
172- }
173- } @catch (NSException *e) {
174163 }
175164
165+ NSCLogError (@" NSCAudioBSN: tryStartPlayer engine running, checking connection" );
166+
176167 if (player.engine != eng) {
177168 @try {
178169 if (player.engine ) {
@@ -194,8 +185,8 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
194185 MsgSendFn fn = (MsgSendFn)objc_msgSend;
195186 NSArray *pts = fn (eng, ocpSel, player, (AVAudioNodeBus)0 );
196187 isConnected = pts && pts.count > 0 ;
197- NSCLogDebug (@" NSCAudioBufferSourceNode : connectionPoints count=%lu " ,
198- (unsigned long )(pts ? pts.count : 0 ));
188+ NSCLogError (@" NSCAudioBSN : connectionPoints count=%lu isConnected= %d " ,
189+ (unsigned long )(pts ? pts.count : 0 ), ( int )isConnected );
199190 } @catch (NSException *e) {
200191 isConnected = NO ;
201192 }
@@ -209,8 +200,8 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
209200 if (_lastDestination)
210201 targetNode = _lastDestination.avNode ;
211202 if (!targetNode) {
212- targetNode = eng. mainMixerNode ;
213- usingMainMixerFallback = YES ;
203+ NSCLogDebug ( @" NSCAudioBufferSourceNode: deferring start until connected to a real destination " ) ;
204+ return NO ;
214205 }
215206 __block BOOL didImmediateConnect = NO ;
216207 void (^immediateConnectBlock)(void ) = ^{
@@ -313,13 +304,9 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
313304 safeToPlay = YES ;
314305 } else {
315306 NSCLogDebug (@" NSCAudioBufferSourceNode: poll immediate now=%p "
316- @" playerTime=nil" ,
307+ @" playerTime=nil, forcing play " ,
317308 now);
318- #if TARGET_OS_SIMULATOR
319- NSCLogDebug (@" NSCAudioBufferSourceNode: simulator forcing play "
320- @" despite nil playerTime (immediate)" );
321309 safeToPlay = YES ;
322- #endif
323310 }
324311 }
325312 } @catch (NSException *e) {
@@ -399,8 +386,8 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
399386 }
400387 }
401388 } @catch (NSException *e) {
402- } @
403- try {
389+ }
390+ @ try {
404391 AVAudioEngine *e = ctx.engine ;
405392 NSMutableArray *queue = [NSMutableArray
406393 arrayWithObject: (AVAudioNode *)player];
@@ -532,6 +519,7 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
532519 }
533520 }
534521#endif
522+ NSCLogError (@" NSCAudioBSN: POLL-1 calling [player play]" );
535523 [player play ];
536524 if (_pendingStart) {
537525 _pendingStart = NO ;
@@ -540,6 +528,21 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
540528 }
541529 _retryCount = 0 ;
542530 } @catch (NSException *ex) {
531+ AVAudioEngine *eng2 = ctx.engine ;
532+ BOOL engRunning = eng2 ? eng2.isRunning : NO ;
533+ id attachedNodes = nil ;
534+ if (eng2 && [eng2 respondsToSelector: @selector (attachedNodes )]) {
535+ @try {
536+ attachedNodes = [eng2 attachedNodes ];
537+ } @catch (NSException *e) {
538+ attachedNodes = nil ;
539+ }
540+ }
541+ NSCLogDebug (@" NSCAudioBufferSourceNode: deferred play exception: %@ ; "
542+ @" ctx=%p eng=%p engRunning=%d player=%p player.engine=%p "
543+ @" attachedNodesCount=%lu " ,
544+ ex, ctx, eng2, engRunning, player, player.engine,
545+ attachedNodes ? (unsigned long )[attachedNodes count] : 0ul);
543546 @try {
544547 [player stop ];
545548 } @catch (NSException *inner) {
@@ -549,7 +552,6 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
549552 return ;
550553 }
551554 }
552-
553555 pollAttempts -= 1 ;
554556 if (pollAttempts > 0 ) {
555557 void (^nextPoll)(void ) = weakPollBlock;
@@ -560,7 +562,6 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
560562 }
561563 return ;
562564 }
563-
564565 [strongSelf scheduleRetryWithContext: ctx];
565566 };
566567 weakPollBlock = pollBlock;
@@ -638,12 +639,8 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
638639 (long long )playerTime.sampleTime, playerTime.sampleRate);
639640 safeToPlay = YES ;
640641 } else {
641- NSCLogDebug (@" NSCAudioBufferSourceNode: poll now=%p playerTime=nil" , now);
642- #if TARGET_OS_SIMULATOR
643- NSCLogDebug (@" NSCAudioBufferSourceNode: simulator forcing play despite "
644- @" nil playerTime" );
642+ NSCLogDebug (@" NSCAudioBufferSourceNode: poll now=%p playerTime=nil, forcing play" , now);
645643 safeToPlay = YES ;
646- #endif
647644 }
648645 }
649646 } @catch (NSException *e) {
@@ -758,6 +755,7 @@ - (BOOL)tryStartPlayer:(AVAudioPlayerNode *)player
758755 }
759756 }
760757 }
758+ NSCLogError (@" NSCAudioBSN: [player play] POLL-2 called engRunning=%d " , (int )(ctx.engine.isRunning));
761759 [player play ];
762760 if (_pendingStart) {
763761 _pendingStart = NO ;
@@ -1139,6 +1137,7 @@ - (void)start {
11391137 _gaveUp = NO ;
11401138 AVAudioPlayerNode *player = (AVAudioPlayerNode *)self.avNode ;
11411139 AVAudioPCMBuffer *pcm = [_buffer getBuffer ];
1140+ NSCLogError (@" NSCAudioBSN: start called buffer=%p engRunning=%d " , pcm, (int )(ctx.engine.isRunning));
11421141 if (pcm != nil ) {
11431142 __weak typeof (self) weakSelf = self;
11441143
@@ -1182,6 +1181,7 @@ - (void)start {
11821181 }
11831182 __weak typeof (self) weakSelf = self;
11841183
1184+ NSCLogError (@" NSCAudioBSN: start engRunning=%d before tryStart" , (int )(ctx.engine.isRunning));
11851185 if (ctx.engine .isRunning ) {
11861186 if ([self tryStartPlayer: player context: ctx]) {
11871187 return ;
0 commit comments