Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / media / sound-ao.c
index 6ac936d..eb6ad12 100644 (file)
@@ -74,7 +74,7 @@ sound_ao_print(Lisp_Object device, Lisp_Object pcfun, int ef)
                /* now that we are here, mark AO device as dead */
                XAUDIO_DEVICE_STATE(device) = ASTATE_DEAD;
                return;
-       } 
+       }
 
        /* info about the connected output plugin */
        write_c_string(" :driver \"", pcfun);
@@ -108,7 +108,7 @@ sound_ao_create(Lisp_Object ao_options)
 
        /* -- initialise -- */
        ao_initialize();
-       fmt = xmalloc(sizeof(ao_sample_format));
+       fmt = xmalloc_and_zero(sizeof(ao_sample_format));
 
        /* -- Setup for driver -- */
        if (optext_driver != NULL)
@@ -356,6 +356,7 @@ sound_ao_play(audio_job_t aj)
 
        /* ... and play it */
        SXE_MUTEX_LOCK(&aj->mtx);
+       mtp = aj->play_state;
        if (aj->buffer_alloc_size < SOUND_MAX_AUDIO_FRAME_SIZE) {
                alloced_myself = 1;
                aj->buffer = xmalloc_atomic(SOUND_MAX_AUDIO_FRAME_SIZE);
@@ -364,7 +365,7 @@ sound_ao_play(audio_job_t aj)
        SXE_MUTEX_UNLOCK(&aj->mtx);
        resolution = (sasd->mtap->samplerate * MTPSTATE_REACT_TIME) / 1000000;
 
-       while (aj->play_state != MTPSTATE_STOP) {
+       while (mtp != MTPSTATE_STOP) {
 
 #ifdef EF_USE_ASYNEQ
                /* events are there? */
@@ -374,26 +375,27 @@ sound_ao_play(audio_job_t aj)
 #endif
 
                SXE_MUTEX_LOCK(&aj->mtx);
-               mtp = aj->play_state;
-               SXE_MUTEX_UNLOCK(&aj->mtx);
-               switch (mtp) {
+               switch (aj->play_state) {
                case MTPSTATE_RUN:
                        if (!ao_push(aj, resolution))
                                aj->play_state = MTPSTATE_STOP;
                        break;
                case MTPSTATE_PAUSE:
-                       AO_DEBUG("sleeping for %d\n", resolution);
-                       usleep(resolution);
-                       break;
+                 /* must sleep resolution outside of lock */
 
                case MTPSTATE_UNKNOWN:
                case MTPSTATE_STOP:
                case NUMBER_OF_MEDIA_THREAD_PLAY_STATES:
                default:
                        AO_DEBUG("ACK, quit\n");
-                       SXE_MUTEX_LOCK(&aj->mtx);
                        aj->play_state = MTPSTATE_STOP;
-                       SXE_MUTEX_UNLOCK(&aj->mtx);
+                       break;
+               }
+               mtp = aj->play_state;
+               SXE_MUTEX_UNLOCK(&aj->mtx);
+               if (mtp == MTPSTATE_PAUSE) {
+                       AO_DEBUG("sleeping for %d\n", resolution);
+                       usleep(resolution);
                        break;
                }
        }
@@ -407,7 +409,8 @@ sound_ao_play(audio_job_t aj)
        aj->buffer_alloc_size = 0;
        SXE_MUTEX_UNLOCK(&aj->mtx);
 
-       if (sasd && sasd->dev) {
+       /* sasd is always != NULL here per its initialization */
+       if (sasd->dev) {
                ao_close(sasd->dev);
                sasd->dev = NULL;
        }