I am using AVAssetReader
to read audio data from a file, on a secondary thread. Every code example I have seen has at the heart of it a loop like this:
while (assetReader.status == AVAssetReaderStatusReading)
{
// ...
buffer = [theAssetReaderTrackOutput copyNextSampleBuffer];
// ...
}
(or the Swift equivalent). My question is, does copyNextSampleBuffer
block while waiting for data from the AVAsset
, or does it return NULL
if the data is not yet available? If the latter, then we have a polling loop, which is poor practice, burning CPU cycles while repeatedly checking status
. The documentation for copyNextSampleBuffer
doesn't say whether or not it blocks the thread while waiting. I also can't tell if it attempts to return immediately with whatever data it has available, or fails/blocks unless it has enough data for a buffer of a certain minimum size.
This case is actually documented (always a pleasant surprise). The header file for copyNextSampleBuffer
says
Copies the next sample buffer for the output synchronously.
And copyNextSampleBuffer
is documented to return NULL
when no more samples can be read on EOF
status
will be set to AVAssetReaderStatusCompleted
https://developer.apple.com/documentation/avfoundation/avassetreaderoutput/1385732-copynextsamplebuffer
https://developer.apple.com/documentation/avfoundation/avassetreaderstatus?language=objc