title: Recording Audio on iPhone and Sending Over Network with NSOutputStream
body: <p>I am writing an iPhone application that needs to record audio from the built-in microphone and then send that audio data to a server for processing.</p>
  
  <p>The application uses a socket connection to connect to the server and Audio Queue Services to do the recording. What I am unsure of is when to actually send the data. Audio Queue Services fires a callback each time it has filled a buffer with some audio data.  NSOutputStream fires an event each time it has space available.  </p>
  
  <p>My first thought was to send the data to the server on the Audio Queue callback.  But it seems like this would run into a problem if the NSOutputStream does not have space available at that time.</p>
  
  <p>Then I thought about buffering the data as it comes back from the Audio Queue and sending some each time the NSOutputStream fires a space available event. But this would seem to have a problem if the sending to the server gets ahead of the audio recording then there will be a situation where there is nothing to write on the space available event, so the event will not be fired again and the data transfer will effectivly be stalled.</p>
  
  <p>So what is the best way to handle this?  Should I have a timer that fires repeatedly and see if there is space available and there is data that needs to be sent?  Also, I think I will need to do some thread synchronization so that I can take chunks of data out of my buffer to send across the network but also add chunks of data to the buffer as the recording proceeds without risking mangling my buffer.  </p>