title: stream media FROM iphone
body: <p>I need to stream audio from the mic to a http server.<br>
  These recording settings are what I need:</p>
  
  <pre><code>NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [NSNumber numberWithInt: kAudioFormatULaw],AVFormatIDKey,        
                                               [NSNumber numberWithFloat:8000.0],AVSampleRateKey,//was 44100.0
                                               [NSData dataWithBytes: &amp;acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
                                               [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                               [NSNumber numberWithInt:64000],AVEncoderBitRateKey,
                                               nil];
  </code></pre>
  
  <p>API im coding to states: </p>
  
  <blockquote>
    <p>Send a continuous stream of audio to the currently viewed camera.
    Audio needs to be encoded at G711 mu-law at 64 kbit/s for transfer to
    the Axis camera at the bedside. send (this should be a POST URL in SSL
    to connected server): POST /transmitaudio?id=
    Content-type: audio/basic Content-Length: 99999 (length is ignored)
    </p>
  </blockquote>
  
  <p>Below are a list of links I have tried to work with.</p>
  
  <p><a href="https://stackoverflow.com/a/8486682/530933">LINK</a> - (SO)basic explanation that only audio unit and audio queues will allow for nsdata as output when recording via the mic | <em>not an example but a good definition of whats needed (audio queues, or audio units)</em></p>
  
  <p><a href="https://stackoverflow.com/questions/8451084/audio-recorded-using-audio-queue-services-to-data">LINK</a> - (SO)audio callback example | <em>only includes the callback</em></p>
  
  <p><a href="https://stackoverflow.com/questions/10113977/recording-to-aac-from-remoteio-data-is-getting-written-but-file-unplayable">LINK</a> - (SO)REMOTE IO example | <em>doesnt have start/stop and is for saving to a file</em></p>
  
  <p><a href="https://stackoverflow.com/questions/10315621/remoteio-session-worked-in-ios-4-3-but-fails-in-ios-5-on-device-works-on-simula">LINK</a> - (SO)REMOTE IO example | <em>unanswered not working</em></p>
  
  <p><a href="https://stackoverflow.com/questions/1010343/how-do-i-record-audio-on-iphone-with-avaudiorecorder">LINK</a> - (SO)Basic audio recording example | <em>good example but records to file</em> </p>
  
  <p><a href="https://stackoverflow.com/questions/3008466/audioqueue-in-memory-playback-example">LINK</a> - (SO)Question that guided me to InMemoryAudioFile class (couldnt get working) | <em>followed links to inMemoryFile (or something like that) but couldn't get it working.</em></p>
  
  <p><a href="https://stackoverflow.com/questions/8699895/error-in-audio-unit-code-remoteio-for-iphone">LINK</a> - (SO)more audio unit and remote io example/problems | <em>got this one working but once again there isn't a stop function, and even when I tried to figure out what the call is and made it stop, it still didn't not seem to transmit the audio to the server.</em></p>
  
  <p><a href="http://atastypixel.com/blog/using-remoteio-audio-unit/comment-page-1/#comments" rel="nofollow noreferrer">LINK</a> - Decent remoteIO and audio queue example but | <em>another good example and almost got it working but had some problems with the code (compiler thinking its not obj-c++) and once again dont know how to get audio "data" from it instead of to a file.</em></p>
  
  <p><a href="http://developer.apple.com/library/ios/#documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/AQRecord/RecordingAudio.html#//apple_ref/doc/uid/TP40005343-CH4-SW1" rel="nofollow noreferrer">LINK</a> - Apple docs for audio queue | <em>had problems with frameworks.  worked through it (see question below) but in the end couldn't get it working however probably didn't give this one as much time as the others, and maybe should have.</em></p>
  
  <p><a href="https://stackoverflow.com/questions/10535455/audio-unit-undeclared-indentifier">LINK</a> - (SO)problems I have had when trying to implement audio queue/unit | <em>not an example</em></p>
  
  <p><a href="https://stackoverflow.com/questions/10067117/recording-from-remoteio-resulting-caf-is-pitch-shifted-slower-distorted">LINK</a> - (SO)another remoteIO example | <em>another good example but cant figure out how to get it to data instead of file.</em></p>
  
  <p><a href="http://atastypixel.com/blog/a-simple-fast-circular-buffer-implementation-for-audio-processing/" rel="nofollow noreferrer">LINK</a> - also looks interesting, circular buffers | <em>couldn't figure out how to incorporate this with the audio callback</em></p>
  
  <p>Here is my current class attempting to stream.  This seems to work although there is static coming out of the speakers at the receivers end (connected to the server).  Which seems to indicate a problem with the audio data format.</p>
  
  <p>IOS VERSION (minus delegate methods for GCD socket):</p>
  
  <pre><code>@implementation MicCommunicator {
  AVAssetWriter * assetWriter;
  AVAssetWriterInput * assetWriterInput;
  }
  
  @synthesize captureSession = _captureSession;
  @synthesize output = _output;
  @synthesize restClient = _restClient;
  @synthesize uploadAudio = _uploadAudio;
  @synthesize outputPath = _outputPath;
  @synthesize sendStream = _sendStream;
  @synthesize receiveStream = _receiveStream;
  
  @synthesize socket = _socket;
  @synthesize isSocketConnected = _isSocketConnected;
  
  -(id)init {
      if ((self = [super init])) {
  
          _receiveStream = [[NSStream alloc]init];
          _sendStream = [[NSStream alloc]init];
          _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
          _isSocketConnected = FALSE;
  
          _restClient = [RestClient sharedManager];
          _uploadAudio = false;
  
          NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          _outputPath = [NSURL fileURLWithPath:[[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"micOutput.output"]];
  
          NSError * assetError;
  
          AudioChannelLayout acl;
          bzero(&amp;acl, sizeof(acl));
          acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono; //kAudioChannelLayoutTag_Stereo;
          NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [NSNumber numberWithInt: kAudioFormatULaw],AVFormatIDKey,        
                                               [NSNumber numberWithFloat:8000.0],AVSampleRateKey,//was 44100.0
                                               [NSData dataWithBytes: &amp;acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
                                               [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                               [NSNumber numberWithInt:64000],AVEncoderBitRateKey,
                                               nil];
  
          assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings]retain];
          [assetWriterInput setExpectsMediaDataInRealTime:YES];
  
          assetWriter = [[AVAssetWriter assetWriterWithURL:_outputPath fileType:AVFileTypeWAVE error:&amp;assetError]retain]; //AVFileTypeAppleM4A
  
          if (assetError) {
              NSLog (@"error initing mic: %@", assetError);
              return nil;
          }
          if ([assetWriter canAddInput:assetWriterInput]) {
              [assetWriter addInput:assetWriterInput];
          } else {
              NSLog (@"can't add asset writer input...!");
              return nil;
          }
  
      }
      return self;
  }
  
  -(void)dealloc {
      [_output release];
      [_captureSession release];
      [_captureSession release];
      [assetWriter release];
      [assetWriterInput release];
      [super dealloc];
  }
  
  
  -(void)beginStreaming {
  
      NSLog(@"avassetwrter class is %@",NSStringFromClass([assetWriter class]));
  
      self.captureSession = [[AVCaptureSession alloc] init];
      AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
      NSError *error = nil;
      AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&amp;error];
      if (audioInput)
          [self.captureSession addInput:audioInput];
      else {
          NSLog(@"No audio input found.");
          return;
      }
  
      self.output = [[AVCaptureAudioDataOutput alloc] init];
  
      dispatch_queue_t outputQueue = dispatch_queue_create("micOutputDispatchQueue", NULL);
      [self.output setSampleBufferDelegate:self queue:outputQueue];
      dispatch_release(outputQueue);
  
      self.uploadAudio = FALSE;
  
      [self.captureSession addOutput:self.output];
      [assetWriter startWriting];
      [self.captureSession startRunning];
  }
  
  -(void)pauseStreaming
  {
      self.uploadAudio = FALSE;
  }
  
  -(void)resumeStreaming
  {
      self.uploadAudio = TRUE;
  }
  
  -(void)finishAudioWork
  {
      [self dealloc];
  }
  
  -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
  
  
      AudioBufferList audioBufferList;
      NSMutableData *data= [[NSMutableData alloc] init];
      CMBlockBufferRef blockBuffer;
      CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &amp;audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &amp;blockBuffer);
  
      for (int y = 0; y &lt; audioBufferList.mNumberBuffers; y++) {
          AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
          Float32 *frame = (Float32*)audioBuffer.mData;
  
          [data appendBytes:frame length:audioBuffer.mDataByteSize];
      }
  
      // append [data bytes] to your NSOutputStream 
  
      // These two lines write to disk, you may not need this, just providing an example
      [assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
      [assetWriterInput appendSampleBuffer:sampleBuffer];
  
      //start upload audio data
      if (self.uploadAudio) { 
  
          if (!self.isSocketConnected) {
              [self connect];
          }
              NSString *requestStr = [NSString stringWithFormat:@"POST /transmitaudio?id=%@ HTTP/1.0\r\n\r\n",self.restClient.sessionId];
  
              NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];        
          [self.socket writeData:requestData withTimeout:5 tag:0];     
          [self.socket writeData:data withTimeout:5 tag:0]; 
      }
      //stop upload audio data
  
      CFRelease(blockBuffer);
      blockBuffer=NULL;
      [data release];
  }
  </code></pre>
  
  <p>And the JAVA version:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.nio.ByteBuffer;
  import java.nio.ByteOrder;
  import java.util.Arrays;
  
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSocket;
  import javax.net.ssl.SSLSocketFactory;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.X509TrustManager;
  
  import android.media.AudioFormat;
  import android.media.AudioManager;
  import android.media.AudioRecord;
  import android.media.AudioTrack;
  import android.media.MediaRecorder.AudioSource;
  import android.util.Log;
  
  public class AudioWorker extends Thread
  { 
      private boolean stopped = false;
  
      private String host;
      private int port;
      private long id=0;
      boolean run=true;
      AudioRecord recorder;
  
      //ulaw encoder stuff
      private final static String TAG = "UlawEncoderInputStream";
  
      private final static int MAX_ULAW = 8192;
      private final static int SCALE_BITS = 16;
  
      private InputStream mIn;
  
      private int mMax = 0;
  
      private final byte[] mBuf = new byte[1024];
      private int mBufCount = 0; // should be 0 or 1
  
      private final byte[] mOneByte = new byte[1];
      ////
      /**
       * Give the thread high priority so that it's not canceled unexpectedly, and start it
       */
      public AudioWorker(String host, int port, long id)
      { 
          this.host = host;
          this.port = port;
          this.id = id;
          android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  //        start();
      }
  
      @Override
      public void run()
      { 
          Log.i("AudioWorker", "Running AudioWorker Thread");
          recorder = null;
          AudioTrack track = null;
          short[][]   buffers  = new short[256][160];
          int ix = 0;
  
          /*
           * Initialize buffer to hold continuously recorded AudioWorker data, start recording, and start
           * playback.
           */
          try
          {
              int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
              recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
              track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,   AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
              recorder.startRecording();
  //            track.play();
              /*
               * Loops until something outside of this thread stops it.
               * Reads the data from the recorder and writes it to the AudioWorker track for playback.
               */
  
  
              SSLContext sc = SSLContext.getInstance("SSL");
              sc.init(null, trustAllCerts, new java.security.SecureRandom());
              SSLSocketFactory sslFact = sc.getSocketFactory();
              SSLSocket socket = (SSLSocket)sslFact.createSocket(host, port);
  
              socket.setSoTimeout(10000);
              InputStream inputStream = socket.getInputStream();
              DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream));
              OutputStream outputStream = socket.getOutputStream();
              DataOutputStream os = new DataOutputStream(new BufferedOutputStream(outputStream));
              PrintWriter socketPrinter = new PrintWriter(os);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
  
  //          socketPrinter.println("POST /transmitaudio?patient=1333369798370 HTTP/1.0");
              socketPrinter.println("POST /transmitaudio?id="+id+" HTTP/1.0");
              socketPrinter.println("Content-Type: audio/basic");
              socketPrinter.println("Content-Length: 99999");
              socketPrinter.println("Connection: Keep-Alive");
              socketPrinter.println("Cache-Control: no-cache");
              socketPrinter.println();
              socketPrinter.flush();
  
  
              while(!stopped)
              { 
                  Log.i("Map", "Writing new data to buffer");
                  short[] buffer = buffers[ix++ % buffers.length];
  
                  N = recorder.read(buffer,0,buffer.length);
                  track.write(buffer, 0, buffer.length);
  
                  byte[] bytes2 = new byte[buffer.length * 2];
                  ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);
  
                  read(bytes2, 0, bytes2.length);
                  os.write(bytes2,0,bytes2.length);
  
  //
  //                ByteBuffer byteBuf = ByteBuffer.allocate(2*N);
  //              System.out.println("byteBuf length "+2*N);
  //                int i = 0;
  //                while (buffer.length &gt; i) {
  //                    byteBuf.putShort(buffer[i]);
  //                    i++;
  //                }         
  //                byte[] b = new byte[byteBuf.remaining()];
              }
              os.close();
          }
          catch(Throwable x)
          { 
              Log.w("AudioWorker", "Error reading voice AudioWorker", x);
          }
          /*
           * Frees the thread's resources after the loop completes so that it can be run again
           */
          finally
          { 
              recorder.stop();
              recorder.release();
              track.stop();
              track.release();
          }
      }
  
      /**
       * Called from outside of the thread in order to stop the recording/playback loop
       */
      public void close()
      { 
           stopped = true;
      }
      public void resumeThread()
      { 
           stopped = false;
           run();
      }
  
      TrustManager[] trustAllCerts = new TrustManager[]{
              new X509TrustManager() {
                  public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                      return null;
                  }
                  public void checkClientTrusted(
                          java.security.cert.X509Certificate[] certs, String authType) {
                  }
                  public void checkServerTrusted(
                          java.security.cert.X509Certificate[] chain, String authType) {
                      for (int j=0; j&lt;chain.length; j++)
                      {
                          System.out.println("Client certificate information:");
                          System.out.println("  Subject DN: " + chain[j].getSubjectDN());
                          System.out.println("  Issuer DN: " + chain[j].getIssuerDN());
                          System.out.println("  Serial number: " + chain[j].getSerialNumber());
                          System.out.println("");
                      }
                  }
              }
      };
  
  
      public static void encode(byte[] pcmBuf, int pcmOffset,
              byte[] ulawBuf, int ulawOffset, int length, int max) {
  
          // from  'ulaw' in wikipedia
          // +8191 to +8159                          0x80
          // +8158 to +4063 in 16 intervals of 256   0x80 + interval number
          // +4062 to +2015 in 16 intervals of 128   0x90 + interval number
          // +2014 to  +991 in 16 intervals of  64   0xA0 + interval number
          //  +990 to  +479 in 16 intervals of  32   0xB0 + interval number
          //  +478 to  +223 in 16 intervals of  16   0xC0 + interval number
          //  +222 to   +95 in 16 intervals of   8   0xD0 + interval number
          //   +94 to   +31 in 16 intervals of   4   0xE0 + interval number
          //   +30 to    +1 in 15 intervals of   2   0xF0 + interval number
          //     0                                   0xFF
  
          //    -1                                   0x7F
          //   -31 to    -2 in 15 intervals of   2   0x70 + interval number
          //   -95 to   -32 in 16 intervals of   4   0x60 + interval number
          //  -223 to   -96 in 16 intervals of   8   0x50 + interval number
          //  -479 to  -224 in 16 intervals of  16   0x40 + interval number
          //  -991 to  -480 in 16 intervals of  32   0x30 + interval number
          // -2015 to  -992 in 16 intervals of  64   0x20 + interval number
          // -4063 to -2016 in 16 intervals of 128   0x10 + interval number
          // -8159 to -4064 in 16 intervals of 256   0x00 + interval number
          // -8192 to -8160                          0x00
  
          // set scale factors
          if (max &lt;= 0) max = MAX_ULAW;
  
          int coef = MAX_ULAW * (1 &lt;&lt; SCALE_BITS) / max;
  
          for (int i = 0; i &lt; length; i++) {
              int pcm = (0xff &amp; pcmBuf[pcmOffset++]) + (pcmBuf[pcmOffset++] &lt;&lt; 8);
              pcm = (pcm * coef) &gt;&gt; SCALE_BITS;
  
              int ulaw;
              if (pcm &gt;= 0) {
                  ulaw = pcm &lt;= 0 ? 0xff :
                          pcm &lt;=   30 ? 0xf0 + ((  30 - pcm) &gt;&gt; 1) :
                          pcm &lt;=   94 ? 0xe0 + ((  94 - pcm) &gt;&gt; 2) :
                          pcm &lt;=  222 ? 0xd0 + (( 222 - pcm) &gt;&gt; 3) :
                          pcm &lt;=  478 ? 0xc0 + (( 478 - pcm) &gt;&gt; 4) :
                          pcm &lt;=  990 ? 0xb0 + (( 990 - pcm) &gt;&gt; 5) :
                          pcm &lt;= 2014 ? 0xa0 + ((2014 - pcm) &gt;&gt; 6) :
                          pcm &lt;= 4062 ? 0x90 + ((4062 - pcm) &gt;&gt; 7) :
                          pcm &lt;= 8158 ? 0x80 + ((8158 - pcm) &gt;&gt; 8) :
                          0x80;
              } else {
                  ulaw = -1 &lt;= pcm ? 0x7f :
                            -31 &lt;= pcm ? 0x70 + ((pcm -   -31) &gt;&gt; 1) :
                            -95 &lt;= pcm ? 0x60 + ((pcm -   -95) &gt;&gt; 2) :
                           -223 &lt;= pcm ? 0x50 + ((pcm -  -223) &gt;&gt; 3) :
                           -479 &lt;= pcm ? 0x40 + ((pcm -  -479) &gt;&gt; 4) :
                           -991 &lt;= pcm ? 0x30 + ((pcm -  -991) &gt;&gt; 5) :
                          -2015 &lt;= pcm ? 0x20 + ((pcm - -2015) &gt;&gt; 6) :
                          -4063 &lt;= pcm ? 0x10 + ((pcm - -4063) &gt;&gt; 7) :
                          -8159 &lt;= pcm ? 0x00 + ((pcm - -8159) &gt;&gt; 8) :
                          0x00;
              }
              ulawBuf[ulawOffset++] = (byte)ulaw;
          }
      }
      public static int maxAbsPcm(byte[] pcmBuf, int offset, int length) {
          int max = 0;
          for (int i = 0; i &lt; length; i++) {
              int pcm = (0xff &amp; pcmBuf[offset++]) + (pcmBuf[offset++] &lt;&lt; 8);
              if (pcm &lt; 0) pcm = -pcm;
              if (pcm &gt; max) max = pcm;
          }
          return max;
      }
  
      public int read(byte[] buf, int offset, int length) throws IOException {
          if (recorder == null) throw new IllegalStateException("not open");
  
          // return at least one byte, but try to fill 'length'
          while (mBufCount &lt; 2) {
              int n = recorder.read(mBuf, mBufCount, Math.min(length * 2, mBuf.length - mBufCount));
              if (n == -1) return -1;
              mBufCount += n;
          }
  
          // compand data
          int n = Math.min(mBufCount / 2, length);
          encode(mBuf, 0, buf, offset, n, mMax);
  
          // move data to bottom of mBuf
          mBufCount -= n * 2;
          for (int i = 0; i &lt; mBufCount; i++) mBuf[i] = mBuf[i + n * 2];
  
          return n;
      }
  
  }
  </code></pre>