AudioUnitRender error -50 with odd length buffers

2019-07-19 10:57发布

问题:

I have a RemoteIO unit configured with AVAudioSessionCategoryPlayAndRecord. I find some strange behavior in it. I open the app, and immediately close it before audioUnit initializes fully (it actually initializes in background as I quit the app too soon). Next I bring the app to foreground and immediately on relaunch, I see AudioUnitRender failing with error -50 continuously. I find inNumberFrames to be 1115 and it fails whenever this number is odd.

  func recordingCallback(inRefCon:UnsafeMutableRawPointer,
                ioActionFlags:UnsafeMutablePointer<AudioUnitRenderActionFlags>,
                inTimeStamp:UnsafePointer<AudioTimeStamp>,
                inBusNumber:UInt32,
                inNumberFrames:UInt32,
                ioData:UnsafeMutablePointer<AudioBufferList>?) -> OSStatus
  {
       let controller = unsafeBitCast(inRefCon, to: MicrophoneOutput.self) 

      let listPtr = controller.audioBufferList.unsafeMutablePointer

     let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

   for var buf in buffers {
       buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
    }

   let status = AudioUnitRender(controller.audioUnit!, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, listPtr)

if noErr != status {
   print("Error \(status)");
   NSLog("\(AVAudioSession.sharedInstance().currentRoute.inputs[0])")
   fatalError("Render status \(status)")
  // return status;
}

return noErr

}

Strangely, if I hardcode inNumberFrames to be 1114 in AudioUnitRender call, it succeeds! Does anyone know what is going on? The pattern is as soon as the app resumes from background, there is a route change notification, and then the first sample in callback is of length 1114 frames but the one next to it has 1115 frames which fails.

回答1:

It turned out it was a programming error due to misunderstandings in Swift language. The line that was culprit was this:

for var buf in buffers {
   buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
}

This answer provides more detail - Swift vs Objective C pointer manipulation issue