BAD_ACCESS on RemoteIO callback only when headphone jack plugged in

201 Views Asked by At

I have this following render callback for a remoteIO audio unit. Simply accessing the 0th element of the ioData parameter results in a crash. Very simply put, this works with no headphone jack connection but as soon as I plug a jack into my iphone6+, I get a bad access error when accessing the buffer.

If I plug it in while the app is running it crashes. If I plug it in first, and then build and run the app, it still crashes. I checked to see if inNumberFrames perhaps is changing based on a line out connection but it remains consistently at 512 frames.

OSStatus playbackCallback(void * inRefCon,
                          AudioUnitRenderActionFlags * ioActionFlags,
                          const AudioTimeStamp * inTimeStamp,
                          UInt32 inBusNumber,
                          UInt32 inNumberFrames,
                          AudioBufferList * ioData) {

   float * output = (float*)ioData->mBuffers[0].mData;
   output[0] = 1;

   return noErr;
}

Apparently an AVAudioSession route change callback is called even if the headphones are plugged in before app launch. One of the things I tried was to delay remoteIO start until that point. However, the following code produces an error:

- (void)handleRouteChange:(NSNotification *)notification {   
   OSStatus err = AudioOutputUnitStart(remoteIOUnit);
}

The error:

Error: should alloc (-10849)

1

There are 1 best solutions below

0
On

If ioData is nil in an Audio Unit callback, you need to have allocated your own AudioBufferList and buffer data memory (* mData), and use that memory instead. Some audio routes may have their own buffers, but this is not guaranteed. So your code needs to account for the case of a nil ioData.

Another possible cause of a nil ioData parameter is that your callback is an input callback, not an output callback, due to a bug in setting the callback for the correct RemoteIO bus.

The -10849 error might mean you are trying to start an audio unit that is already started and not stopped yet (it takes awhile after any audio stop command).