title: Using Audio Queue Services to play PCM data over a socket connection
body: <p>I'm writing a remote desktop client for the iPhone and I'm trying to implement audio redirection.<br>
  The client is connected to the server over a socket connection, and the server sends 32K chunks of PCM data at a time.  </p>
  
  <p>I'm trying to use AQS to play the data and it plays the first two seconds (1 buffer worth). However, since the next chunk of data hasn't come in over the socket yet, the next AudioQueueBuffer is empty. When the data comes in, I fill the next available buffer with the data and enqueue it with AudioQueueEnqueueBuffer. However, it never plays these buffers.</p>
  
  <p>Does the queue stop playing if there are no buffers in the queue, even if you later add a buffer? </p>
  
  <p>Here's the relevant part of the code:</p>
  
  <pre><code>void
  wave_out_write(STREAM s, uint16 tick, uint8 index)
  {
  
      if(items_in_queue == NUM_BUFFERS){
          return;
      }
      if(!playState.busy){
          OSStatus status;
          status = AudioQueueNewOutput(&amp;playState.dataFormat, AudioOutputCallback, &amp;playState, CFRunLoopGetCurrent(), NULL, 0, &amp;playState.queue);
  
          if(status == 0){
              for(int i=0; i&lt;NUM_BUFFERS; i++){
                  AudioQueueAllocateBuffer(playState.queue, 40000, &amp;playState.buffers[i]);
  
              }
              AudioQueueAddPropertyListener(playState.queue, kAudioQueueProperty_IsRunning, MyAudioQueuePropertyListenerProc, &amp;playState);
  
              status = AudioQueueStart(playState.queue, NULL);
              if(status ==0){
                  playState.busy = True;
              }
              else{
                  return;
              }
          }
          else{
              return;
          }
      }
      playState.buffers[queue_hi]-&gt;mAudioDataByteSize = s-&gt;size;
  
      memcpy(playState.buffers[queue_hi]-&gt;mAudioData, s-&gt;data, s-&gt;size);
  
      AudioQueueEnqueueBuffer(playState.queue, playState.buffers[queue_hi], 0, 0);
      queue_hi++;
      queue_hi = queue_hi % NUM_BUFFERS;
      items_in_queue++;
  }
  
  
  void AudioOutputCallback(void* inUserData, AudioQueueRef outAQ, AudioQueueBufferRef outBuffer)
  {
      PlayState *playState = (PlayState *)inUserData;
      items_in_queue--;
  }
  </code></pre>
  
  <p>Thanks!</p>