Reputation: 51
I am trying to use audio queues in Objective C AudioToolbox/AudioQueue.h
to record and play sound in real time. I can get each to work individually, but when running both side by side I have issues.
If I start the output queue after the input queue, the input logic works fine and receives data from the microphone. The output logic fails on a call to AudioQueueStart
with the OSStatus code of 560034163 (!aqs)
. What does this mean? I could not find any documentation on the internet, which is very frustrating.
If I start the output queue before the input queue then the AudioQueueStart
call succeeds on the output queue. However, the input queue behaves strangely with its callback receiving 0 data (as indicated by AudioQueueBufferRef->mAudioDataByteSize == 0
) for all buffers. My code requeues these buffers but then never receives any further callbacks.
Why are my input and output audio queues clashing like this?
Upvotes: 0
Views: 709
Reputation: 51
After a lot of playing around I have figured out (at least for my purposes) some answers.
!aqs error
The !aqs
error seems to be related to threading, or may be a generic last resort error from audio queues API.
To prevent !aqs
from occurring as described above I had to ensure that each audio queue was fully setup in a synchronised fashion. Previously I was initialising both in separate threads at the same time; the API does not seem to be thread safe, so initialise each in sequence.
Full initialisation must take place, for example with the output queue I perform the following steps in isolation (I don't allow more than one thread to do this at a time):
AudioQueueNewOutput
AudioQueueAllocateBuffer
(for all buffers)
AudioQueueEnqueueBuffer
: data must be loaded into all of the buffers; I tried to initialise to 0 bytes (mAudioDataByteSize) but this causes the command to fail. My application does not have valid data to output straight away so for now I just initialise with data to produce no noise.
AudioQueuePrime
AudioQueueStart
: Note that if no buffers are queued this may not work properly (I found this to be the case).
Input audio queue callback problems
As described in my original question, I was seeing input audio buffers come back with no data, and then the callback never called again in certain scenarios.
I found that if I call AudioQueueStart
on the input audio queue before calling AudioQueueStart
on the output audio queue then I do not experience problems. This is apparently a requirement, calling them in the alternative order (output first) triggers the problem I describe. Which thread you call AudioQueueStart
from does not appear to matter. The same goes for threading with regards to other steps involved in initialisation of the audio queues. And the time between calls to AudioQueueStart
(even if e.g. 10 seconds) also has no impact.
Summary
The first issue is 'fair enough' I guess; but I don't see it documented anywhere which isn't great.
The second issue seems like an enormous bug which probably has and probably will cause mass confusion.
Conceptually I would have expected input and output audio queues to be disjoint and not suffer from these issues; but apparently this is not the case.
Upvotes: 2