Bug 778675 - Make cubeb_stream_init errors non-fatal in AudioUnit backend. r=doublec
authorMatthew Gregan <kinetik@flim.org>
Thu, 18 Oct 2012 18:03:45 +1300
changeset 110874 e0b2ebcbd56aebd151202e1a0d4f16c4462c172d
parent 110873 4a4acb36e6701b595910f179d2930e5d006e17a5
child 110875 ee0b7a6877089a40b00ed8aeeada3ea24c4660d0
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersdoublec
bugs778675
milestone19.0a1
Bug 778675 - Make cubeb_stream_init errors non-fatal in AudioUnit backend. r=doublec
media/libcubeb/README_MOZILLA
media/libcubeb/src/cubeb_audiounit.c
--- a/media/libcubeb/README_MOZILLA
+++ b/media/libcubeb/README_MOZILLA
@@ -1,8 +1,9 @@
 The source from this directory was copied from the cubeb 
 git repository using the update.sh script.  The only changes
 made were those applied by update.sh and the addition of
 Makefile.in build files for the Mozilla build system.
 
 The cubeb git repository is: git://github.com/kinetiknz/cubeb.git
 
 The git commit ID used was 2d7d3e8f2ecabb70d2723f3c86fcb591a84b7f85.
+Plus the single commit f4c927fb1c2dc0a0580d0bca4bd267c34febada4.
--- a/media/libcubeb/src/cubeb_audiounit.c
+++ b/media/libcubeb/src/cubeb_audiounit.c
@@ -2,17 +2,16 @@
  * Copyright © 2011 Mozilla Foundation
  *
  * This program is made available under an ISC-style license.  See the
  * accompanying file LICENSE for details.
  */
 #undef NDEBUG
 #include <assert.h>
 #include <pthread.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <CoreServices/CoreServices.h>
 #include <AudioUnit/AudioUnit.h>
 #include "cubeb/cubeb.h"
 
 #define NBUFS 4
 
 struct cubeb_stream {
@@ -178,68 +177,70 @@ cubeb_stream_init(cubeb * context, cubeb
   r = pthread_mutex_init(&stm->mutex, NULL);
   assert(r == 0);
 
   stm->frames_played = 0;
   stm->frames_queued = 0;
 
   r = OpenAComponent(comp, &stm->unit);
   if (r != 0) {
-    fprintf(stderr, "cubeb_audiounit: FATAL: OpenAComponent returned %ld\n", (long) r);
+    cubeb_stream_destroy(stm);
+    return CUBEB_ERROR;
   }
-  assert(r == 0);
 
   input.inputProc = audio_unit_output_callback;
   input.inputProcRefCon = stm;
   r = AudioUnitSetProperty(stm->unit, kAudioUnitProperty_SetRenderCallback,
                            kAudioUnitScope_Global, 0, &input, sizeof(input));
   if (r != 0) {
-    fprintf(stderr, "cubeb_audiounit: FATAL: AudioUnitSetProperty(SetRenderCallback) returned %ld\n", (long) r);
+    cubeb_stream_destroy(stm);
+    return CUBEB_ERROR;
   }
-  assert(r == 0);
 
   r = AudioUnitSetProperty(stm->unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
                            0, &ss, sizeof(ss));
   if (r != 0) {
-    fprintf(stderr, "cubeb_audiounit: FATAL: AudioUnitSetProperty(StreamFormat) returned %ld\n", (long) r);
+    cubeb_stream_destroy(stm);
+    return CUBEB_ERROR;
   }
-  assert(r == 0);
 
   buffer_size = ss.mSampleRate / 1000.0 * latency * ss.mBytesPerFrame / NBUFS;
   if (buffer_size % ss.mBytesPerFrame != 0) {
     buffer_size += ss.mBytesPerFrame - (buffer_size % ss.mBytesPerFrame);
   }
   assert(buffer_size % ss.mBytesPerFrame == 0);
 
   r = AudioUnitInitialize(stm->unit);
   if (r != 0) {
-    fprintf(stderr, "cubeb_audiounit: FATAL: AudioUnitInitialize returned %ld\n", (long) r);
+    cubeb_stream_destroy(stm);
+    return CUBEB_ERROR;
   }
-  assert(r == 0);
 
   *stream = stm;
 
   return CUBEB_OK;
 }
 
 void
 cubeb_stream_destroy(cubeb_stream * stm)
 {
   OSStatus r;
 
   stm->shutdown = 1;
 
-  r = AudioOutputUnitStop(stm->unit);
-  assert(r == 0);
+  if (stm->unit) {
+    r = AudioOutputUnitStop(stm->unit);
+    assert(r == 0);
 
-  r = AudioUnitUninitialize(stm->unit);
-  assert(r == 0);
+    r = AudioUnitUninitialize(stm->unit);
+    assert(r == 0);
 
-  r = CloseComponent(stm->unit);
-  assert(r == 0);
+    r = CloseComponent(stm->unit);
+    assert(r == 0);
+  }
 
   r = pthread_mutex_destroy(&stm->mutex);
   assert(r == 0);
 
   free(stm);
 }
 
 int