What causes ExtAudioFileRead to make ioData->mBuffers[0].mDataByteSize negative?

508 views Asked by At

The problem occurs when I often stop and start audio playback and seek a lot back and forth in an AAC audio file through an ExtAudioFileRef object. In few cases, this strange behaviour is shown by ExtAudioFileRead:

Sometimes it assigns these numbers to the mDataByteSize of the only AudioBuffer of the AudioBufferList:

-51604480
-51227648
-51350528
-51440640
-51240960

In hex, these numbers have the pattern 0xFC....00.

The code:

status = ExtAudioFileRead(_file, &numberFramesRead, ioData);

printf("s=%li d=%p d.nb=%li, d.b.d=%p, d.b.dbs=%li, d.b.nc=%li\n", status, ioData, ioData->mNumberBuffers, ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize, ioData->mBuffers[0].mNumberChannels);

Output:

s=0 d=0x16668bd0 d.nb=1, d.b.d=0x30de000, d.b.dbs=1024, d.b.nc=2 // good (usual)
s=0 d=0x16668bd0 d.nb=1, d.b.d=0x30de000, d.b.dbs=-51240960, d.b.nc=2 // misbehaving

The problem occurs on an iPhone 4S on iOS 7. I could not reproduce the problem in the Simulator.

1

There are 1 answers

0
Daniel S. On BEST ANSWER

The problem occurs when concurrently calling ExtAudioFileRead() and ExtAudioFileSeek() for the same ExtAudioFileRef from two different threads/queues.

The read function was called directly from the AURenderCallback, so it was executed on AudioUnit's real-time thread while the seek was done on my own serial queue.

I've modified the code of the render callback to also dispatch_sync() to the same serial queue to which the seek gets dispatched. That solved the problem.