title: Audio over sockets
body: <h1>Description</h1>
  
  <p>I need to design a solution which will allow two way audio communication via sockets for an iOS App.</p>
  
  <p>Essentially, the App needs to take audio from the device's microphone and pipe it to a destination via a socket while, also reading audio from (presumably a different) socket and play it via the device's speaker (or what ever audio route is established)</p>
  
  <h1>The problem</h1>
  
  <p>I've spent several days researching the problem and trying to look for examples which might provide a base line from which I can derive a solution, but I'm just getting more confused.</p>
  
  <p>My basic issue is I don't now if I should start with CoreAudio directly, try using <code>AVAudioEngine</code> or if there is some other API available (within AVFoundation or AudioToolbox) which I could use; basically, I can no longer see the forest for the trees.</p>
  
  <p>I've played around with <code>AVAudioRecorder</code>, but this seems to be focused on writing content to a file (ok <code>URL</code>, but it still doesn't seem appropriate for my needs)</p>
  
  <p>I've begun researching <code>AVAudioEngine</code>, but I don't know if it's really the right tool for the job.</p>
  
  <h1>The Question</h1>
  
  <p>Essentially I'm looking for some direction (apologies for the broadness of that statement, but I'm completely lost) on the best approach to take and a starting pointing.</p>
  
  <p>I'm primarily concerned with taking the audio from the microphone, probably performing some kind of encoding on it, and then piping it the socket (I'm hoping to be able to use <a href="https://github.com/robbiehanson/CocoaAsyncSocket" rel="nofollow noreferrer">CocoaAsyncSocket</a>, so the socket isn't really part of the question, but is provided for context, I just need to get to the point that I have a data packet I can write).</p>
  
  <p>What API would be the best place to start?  CoreAudio, <code>AVAudioEngine</code>, some other AVFoundation class/API?</p>
  
  <p>As a side note, would the incoming audio require a different approach, I'm assuming that the concept is the same, but in reverse (read socket, decode packet, write to audio output route)</p>
  
  <h1>Research and side notes</h1>
  
  <p>I've basically been searching for "iOS microphone socket" and "iOS microphone streaming" style examples, but I've not yet quite come up with a viable answer/example which seems to be the right direction</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/38430805/get-audio-from-microphone-and-write-it-to-socket-on-ios">Get audio from microphone and write it to socket on iOS</a></li>
  <li><a href="https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started" rel="nofollow noreferrer">AVAudioEngine Tutorial for iOS: Getting Started</a></li>
  <li><a href="https://fastlearner.io/blog/streaming-audio-with-effects-using-avaudioengine" rel="nofollow noreferrer">Streaming Audio With Effects Using AVAudioEngine</a></li>
  <li><a href="https://stackoverflow.com/questions/18784105/audio-stream-wifi-to-wifi-broadcast">Audio Stream WiFi to WiFi - Broadcast</a>, <a href="https://stackoverflow.com/questions/12573097/ios-audio-unit-remoteio-playback-while-recording/12575037#12575037">ios audio unit remoteIO playback while recording</a> (and by extension <a href="https://code.google.com/archive/p/ios-coreaudio-example/downloads" rel="nofollow noreferrer">ios-coreaudio-example</a>, which seems promising, if a little out of date (>= 2012))</li>
  </ul>
  
  <p>I've also looked at examples of <code>AVAudioRecorder</code>, but I'm pretty sure that's a dead end solution for what I need to achieve</p>
  
  <p>I also don't want/need a "preexisting UI", as this should be controlled via the App's custom UI (so something like <code>AVPlayerViewController</code> won't help)</p>
  
  <p>I appreciate that this is somewhat broad, I'm not looking for a ready made solution (examples would be nice), but some general direction on the best choices of core APIs I should be focused on that will help me solve the solution.</p>