shithub: sox

Download patch

ref: 5b8340684cd6f9b6853bab1a244347fb9bf7fc4b
parent: b90cd902c5f6252122b0c526b624c5f3896239a1
author: cbagwell <cbagwell>
date: Wed Feb 23 21:03:51 EST 2011

Free pthread items and remove callbacks.  Allows back-to-back
open()/close() cycles.  Previously, it would hang or crash app.

--- a/ChangeLog
+++ b/ChangeLog
@@ -30,8 +30,11 @@
 
 Audio device drivers:
 
-  o Fix immediate segfault on OSX while attempting to record.  May also
-    prevent segfaults on playing that some people reported.  (Adam Fritzler)
+  o Fix immediate segfault on OSX while attempting to record. (Adam Fritzler)
+  o Fix segfault on OSX playback for some HW that gives smaller then
+    requested buffers. (cbagwell)
+  o Clean up system resource in coreaudio on close.  Allows running
+    back-to-back open()/close()'s without exiting app first. (cbagwell)
   o Add support for 32-bit samples to OSS driver. (Eric Lammerts)
   o Add support for 24 and 32-bit samples to waveaudio (Win32) driver.
     (Doug Cook)
--- a/src/coreaudio.c
+++ b/src/coreaudio.c
@@ -320,6 +320,9 @@
   priv_t *ac = (priv_t *)ft->priv;
 
   AudioDeviceStop(ac->adid, RecIOProc);
+  AudioDeviceRemoveIOProc(ac->adid, RecIOProc);
+  pthread_cond_destroy(&ac->cond);
+  pthread_mutex_destroy(&ac->mutex);
 
   return SOX_SUCCESS;
 }
@@ -386,7 +389,21 @@
 {
   priv_t *ac = (priv_t *)ft->priv;
 
-  AudioDeviceStop(ac->adid, PlaybackIOProc);
+  if (!ac->device_started)
+  {
+    pthread_mutex_lock(&ac->mutex);
+
+    while (ac->buf_offset)
+	pthread_cond_wait(&ac->cond, &ac->mutex);
+
+    pthread_mutex_unlock(&ac->mutex);
+
+    AudioDeviceStop(ac->adid, PlaybackIOProc);
+  }
+
+  AudioDeviceRemoveIOProc(ac->adid, PlaybackIOProc);
+  pthread_cond_destroy(&ac->cond);
+  pthread_mutex_destroy(&ac->mutex);
 
   return SOX_SUCCESS;
 }