title: Audio unit, set format fails with -10581
body: <p>I can't figure out why the following returns OSStatus -10851:</p>
  
  <pre><code>status = AudioUnitSetProperty(*audioUnit, 
                  kAudioUnitProperty_StreamFormat, 
                  kAudioUnitScope_Output, 
                  kInputBus, 
                  &amp;outFormat, 
                  sizeof(outFormat));
  </code></pre>
  
  <p>Works on the simulator but not on the device. </p>
  
  <p>Here is the rest of the code:</p>
  
  <pre><code>    #import "VoipRecorder.h"
  #import &lt;AudioToolbox/AudioToolbox.h&gt;
  #import &lt;CoreAudio/CoreAudioTypes.h&gt;
  
  #define kOutputBus 0
  #define kInputBus 1
  
  void SetAUCanonical(AudioStreamBasicDescription *format, UInt32 nChannels, bool interleaved)
  // note: leaves sample rate untouched
  {
      format-&gt;mFormatID = kAudioFormatLinearPCM;
  #if TARGET_IPHONE_SIMULATOR
      int sampleSize = sizeof(Float32);
      format-&gt;mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
  #else
      int sampleSize = sizeof(AudioSampleType);
      format-&gt;mFormatFlags = kAudioFormatFlagsCanonical;
  #endif
      format-&gt;mBitsPerChannel = 8 * sampleSize;
      format-&gt;mChannelsPerFrame = nChannels;
      format-&gt;mFramesPerPacket = 1;
      if (interleaved)
          format-&gt;mBytesPerPacket = format-&gt;mBytesPerFrame = nChannels * sampleSize;
      else {
          format-&gt;mBytesPerPacket = format-&gt;mBytesPerFrame = sampleSize;
          format-&gt;mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
      }
  }
  
  int SetupRemoteIO (AudioUnit *audioUnit, AURenderCallbackStruct inRenderProc, AURenderCallbackStruct inOutputProc, AudioStreamBasicDescription * outFormat)
  {
      OSStatus status;
  
      // Open the output unit
      AudioComponentDescription desc;
      desc.componentType = kAudioUnitType_Output;
      desc.componentSubType = kAudioUnitSubType_RemoteIO;
      desc.componentManufacturer = kAudioUnitManufacturer_Apple;
      desc.componentFlags = 0;
      desc.componentFlagsMask = 0;
  
      AudioComponent comp = AudioComponentFindNext(NULL, &amp;desc);
  
      AudioComponentInstanceNew(comp, audioUnit);
  
      UInt32 flag = 1;
      // Enable IO for recording
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioOutputUnitProperty_EnableIO, 
                                    kAudioUnitScope_Input, 
                                    kInputBus,
                                    &amp;flag, 
                                    sizeof(flag));
  
      assert(status == 0);
  
      // Enable IO for playback
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioOutputUnitProperty_EnableIO, 
                                    kAudioUnitScope_Output, 
                                    kOutputBus,
                                    &amp;flag, 
                                    sizeof(flag));    
  
      assert(status == 0);
  
      // set our required format - Canonical AU format: LPCM non-interleaved 8.24 fixed point
      SetAUCanonical(outFormat, 1, NO);
  
      outFormat-&gt;mSampleRate = 44100.00; //8000;
  
      //Apply format
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioUnitProperty_StreamFormat, 
                                    kAudioUnitScope_Output, 
                                    kInputBus, 
                                    &amp;outFormat, 
                                    sizeof(outFormat));
  
      assert(status == 0);
  
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioUnitProperty_StreamFormat, 
                                    kAudioUnitScope_Input, 
                                    kOutputBus, 
                                    &amp;outFormat, 
                                    sizeof(outFormat));
  
      // Setup callbacks
      // Recording
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioOutputUnitProperty_SetInputCallback, 
                                    kAudioUnitScope_Input, 
                                    kInputBus, 
                                    &amp;inRenderProc, 
                                    sizeof(inRenderProc));
      assert(status == 0);
  
      // Playback
      status = AudioUnitSetProperty(*audioUnit, 
                                    kAudioUnitProperty_SetRenderCallback, 
                                    kAudioUnitScope_Output, 
                                    kOutputBus,
                                    &amp;inOutputProc, 
                                    sizeof(inOutputProc));
      assert(status == 0);
  
      status = AudioUnitInitialize(*audioUnit);
      assert(status == 0);
  
      return 0;
  }
  
  @implementation VoipRecorder
  
  @synthesize audioUnit;
  
  - (id)init
  {
      self = [super init];
      if (self) {
  
      }
  
      return self;
  }
  
  void rioInterruptionListener(void *inClientData, UInt32 inInterruption)
  {
      printf("Session interrupted! --- %s ---", inInterruption == kAudioSessionBeginInterruption ? "Begin Interruption" : "End Interruption");
  
      VoipRecorder *THIS = (VoipRecorder*)inClientData;
  
      if (inInterruption == kAudioSessionEndInterruption) {
          // make sure we are again the active session
          AudioSessionSetActive(true);
          AudioOutputUnitStart(THIS.audioUnit);
      }
  
      if (inInterruption == kAudioSessionBeginInterruption) {
          AudioOutputUnitStop(THIS.audioUnit);
      }
  }
  
  int buffer[1000000];
  int bufferSize = 2;
  
  static OSStatus PerformSpeaker(
                              void                        *inRefCon, 
                              AudioUnitRenderActionFlags  *ioActionFlags, 
                              const AudioTimeStamp        *inTimeStamp, 
                              UInt32                      inBusNumber, 
                              UInt32                      inNumberFrames, 
                              AudioBufferList             *ioData)
  {
      NSLog(@"Speaker");
  
      if (bufferSize == 0) {
          return 0;
      }
  
      if (ioData == NULL) {
          NSLog(@"err");
          return 0;
      }
  
      return 0;
  }
  
  AudioBufferList *AllocateBuffers(UInt32 nBytes)
  {
      int channelCount = 2;
  
      AudioBufferList *audioBufferList;
      audioBufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList));
      audioBufferList-&gt;mNumberBuffers = 1;
      audioBufferList-&gt;mBuffers[0].mNumberChannels = channelCount;
      audioBufferList-&gt;mBuffers[0].mDataByteSize = nBytes;
      audioBufferList-&gt;mBuffers[0].mData = (AudioUnitSampleType *)malloc(nBytes);
  
      return audioBufferList;
  }
  
  static OSStatus PerformThru(
                              void                        *inRefCon, 
                              AudioUnitRenderActionFlags  *ioActionFlags, 
                              const AudioTimeStamp        *inTimeStamp, 
                              UInt32                      inBusNumber, 
                              UInt32                      inNumberFrames, 
                              AudioBufferList             *ioData)
  {
      VoipRecorder *THIS = (VoipRecorder *)inRefCon;
  
      AudioBufferList *bufferList = AllocateBuffers(inNumberFrames*2);
  
      OSStatus err = AudioUnitRender(THIS.audioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, bufferList);
      if (err) {
          printf("PerformThru: error %d\n", (int)err); 
          free(bufferList); 
          return err; 
      }
  
  
      free(bufferList);
  
      return 0;
  }
  
  
  - (void)setupAudio {
  
      OSStatus status;
  
      inputProc.inputProc = PerformThru;
      inputProc.inputProcRefCon = self;
  
      outputProc.inputProc = PerformSpeaker;
      outputProc.inputProcRefCon = self;
  
      buffer[0] = 0x4444;
      buffer[1] = 0xffff;
      status = AudioSessionInitialize(NULL, NULL, rioInterruptionListener, self);
      assert(status == 0);
  
      UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
      status = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &amp;audioCategory);
      assert(status == 0);
  
      Float32 preferredBufferSize = .005;
      status = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &amp;preferredBufferSize);
      assert(status == 0);
  
      UInt32 size = sizeof(hwSampleRate);
      status = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &amp;size, &amp;hwSampleRate);
      assert(status == 0);
  
      status = AudioSessionSetActive(true);
      assert(status == 0);
  
      status = SetupRemoteIO(&amp;audioUnit, inputProc, outputProc, &amp;thruFormat);
      assert(status == 0);
  
      status = AudioOutputUnitStart(audioUnit);
      assert(status == 0);
  
      size = sizeof(thruFormat);
      status = AudioUnitGetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &amp;thruFormat, &amp;size);
      assert(status == 0);
  
      //NSLog(@"0x%X", status);
  }
  </code></pre>