Blob Blame History Raw
diff -Nur audacious-plugin-fc-0.5.1-orig/src/main.c audacious-plugin-fc-0.5.1/src/main.c
--- audacious-plugin-fc-0.5.1-orig/src/main.c	2010-07-10 12:46:00.000000000 +0200
+++ audacious-plugin-fc-0.5.1/src/main.c	2010-07-23 12:03:05.000000000 +0200
@@ -35,6 +35,7 @@
 static GMutex *seek_mutex;
 static GCond *seek_cond;
 static gint jumpToTime = -1;
+static gboolean pause_flag;
 
 void ip_init(void) {
     jumpToTime = -1;
@@ -74,6 +75,7 @@
     gboolean audioDriverOK = FALSE;
     gboolean haveSampleBuf = FALSE;
     struct audioFormat myFormat;
+    gshort paused = 0;
 
     playback->playing = FALSE;
     jumpToTime = -1;
@@ -173,26 +175,16 @@
         aud_tuple_associate_string(t, FIELD_QUALITY, NULL, "sequenced");
         playback->set_tuple( playback, t );
 
+        g_mutex_lock(seek_mutex);
         /* bitrate => 4*1000 will be displayed as "4 CHANNELS" */
         playback->set_params( playback, NULL, 0, 1000*4, myFormat.freq, myFormat.channels );
-        
+        pause_flag = FALSE;
         playback->playing = TRUE;
+        playback->eof = FALSE;
         playback->set_pb_ready(playback);
+        g_mutex_unlock(seek_mutex);
 
         while ( playback->playing ) {
-            fc14dec_buffer_fill(decoder,sampleBuf,sampleBufSize);
-            if ( playback->playing && jumpToTime<0 ) {
-#if __AUDACIOUS_PLUGIN_API__ >= 13
-                playback->output->write_audio(sampleBuf,sampleBufSize);
-#else
-                playback->pass_audio(playback,myFormat.xmmsAFormat,myFormat.channels,sampleBufSize,sampleBuf,NULL);
-#endif
-            }
-            if ( fc14dec_song_end(decoder) && jumpToTime<0 ) {
-                playback->eof = TRUE;
-                playback->playing = FALSE;
-            }
-
             g_mutex_lock(seek_mutex);
             if ( jumpToTime != -1 ) {
                 fc14dec_seek(decoder,jumpToTime);
@@ -200,11 +192,38 @@
                 jumpToTime = -1;
                 g_cond_signal(seek_cond);
             }
+            if (pause_flag != paused) {
+                playback->output->pause(pause_flag);
+                paused = pause_flag;
+                g_cond_signal(seek_cond);
+            }
+            if (paused) {
+                g_cond_wait(seek_cond, seek_mutex);
+                g_mutex_unlock(seek_mutex);
+                continue;
+            }
             g_mutex_unlock(seek_mutex);
+
+            fc14dec_buffer_fill(decoder,sampleBuf,sampleBufSize);
+            if ( playback->playing && jumpToTime<0 ) {
+                playback->pass_audio(playback,myFormat.xmmsAFormat,myFormat.channels,sampleBufSize,sampleBuf,NULL);
+            }
+            if ( fc14dec_song_end(decoder) && jumpToTime<0 ) {
+                playback->eof = TRUE;
+                playback->playing = FALSE;
+            }
         }
-        playback->playing = FALSE;
-        playback->output->close_audio();
     }
+    g_mutex_lock(seek_mutex);
+
+    while (playback->playing && playback->output->buffer_playing())
+        g_usleep(20000);
+
+    playback->playing = FALSE;
+    g_cond_signal(seek_cond);  /* wake up any waiting request */
+    g_mutex_unlock(seek_mutex);
+
+    playback->output->close_audio();
 
  PLAY_FAILURE_2:
     g_free(sampleBuf);
@@ -213,17 +232,31 @@
 }
     
 void ip_stop(InputPlayback *playback) {
+    g_mutex_lock(seek_mutex);
     playback->playing = FALSE;
+    g_cond_signal(seek_cond);
+    g_mutex_unlock(seek_mutex);
+    g_thread_join(playback->thread);
+    playback->thread = NULL;
 }
 
 void ip_pause(InputPlayback *playback, gshort p) {
-    playback->output->pause(p);
+    g_mutex_lock(seek_mutex);
+    if (playback->playing) {
+        pause_flag = p;
+        g_cond_signal(seek_cond);
+        g_cond_wait(seek_cond, seek_mutex);
+    }
+    g_mutex_unlock(seek_mutex);
 }
 
 void ip_mseek(InputPlayback *playback, gulong msec) {
     g_mutex_lock(seek_mutex);
-    jumpToTime = msec;
-    g_cond_wait(seek_cond, seek_mutex);
+    if (playback->playing) {
+        jumpToTime = msec;
+        g_cond_signal(seek_cond);
+        g_cond_wait(seek_cond, seek_mutex);
+    }
     g_mutex_unlock(seek_mutex);
 }