Subject: Support for Audacious new playback thread handling.

From: Joker
Date: Tue Jul 24 21:57:12 2007

To: UADE-Developer-discussion
Audacious switched to a new threading model for input plugins.

Audacious creates and handles the thread itself now. Instead of
expecting play_file() to create a thread and exit, it creates a thread,
then calls play_file() in it and expects it to run until playback is done.

While this makes new plugins much simpler, it's not compatible with
the previous model.

I've created a patch which should work on both using small #if blocks.

[code:1:abb3a2a3ac]
diff -ruN uade.orig/src/frontends/audacious/plugin.c uade/src/frontends/audacious/plugin.c
--- uade.orig/src/frontends/audacious/plugin.c	2007-07-08 12&#58;47&#58;28.000000000 +0200
+++ uade/src/frontends/audacious/plugin.c	2007-07-24 21&#58;53&#58;20.000000000 +0200
@@ -826,6 +826,11 @@
   if &#40;initialize_song&#40;filename&#41; == FALSE&#41;
     goto err;
 
+#if __AUDACIOUS_PLUGIN_API__ &gt;= 3
+  decode_thread = pthread_self&#40;&#41;;
+  uade_thread_running = 1;
+  play_loop&#40;playhandle&#41;;
+#else
   if &#40;pthread_create&#40;&amp;decode_thread, NULL, play_loop, playhandle&#41;&#41; &#123;
     fprintf&#40;stderr, &quot;uade&#58; can't create play_loop&#40;&#41; thread\n&quot;&#41;;
     uade_unalloc_song&#40;uadesong&#41;;
@@ -834,10 +839,10 @@
     uade_unlock&#40;&#41;;
     goto err;
   &#125;
+  uade_thread_running = 1;
+#endif
 
   free&#40;decoded&#41;;
-
-  uade_thread_running = 1;
   return;
 
  err&#58;
@@ -933,7 +938,11 @@
 static int uade_get_time&#40;void&#41;
 #endif
 &#123;
+#if __AUDACIOUS_PLUGIN_API__ &gt;= 3
+  if &#40;abort_playing || last_beat_played || uade_thread_running == 0&#41;
+#else
   if &#40;abort_playing || last_beat_played&#41;
+#endif
     return -1;
 
   if &#40;gui_info_set == 0 &amp;&amp; uadesong-&gt;max_subsong != -1&#41; &#123;
[/code:1:abb3a2a3ac]

Source:
[url]http://www.netswarm.net/misc/uade-audacious-plugin3.diff[/url]

I'm not entirly sure why the additional uade_thread_running check is needed now. However it would segfault hard without.

Maybe the &quot;free(decoded);&quot; could me moved up before the #if block. I don't think it gets used after that anymore.

Bye,
Christian Birchinger &lt;[email-masked]&gt;

---

From: shd
Date: Tue Jul 31 13:33:09 2007

[quote:5ae0d7c0a8]

Bye,
Christian Birchinger &lt;[email-masked]&gt;[/quote:5ae0d7c0a8]
Thanks, I figured out the problem, I think. Please do git-pull. If this works, I will rapidly make a new UADE release. I don't want to block Audacious-UADE users :-)

[code:1:5ae0d7c0a8]commit 6afd393b8164724292f67ca319bba904313d4167
Author&#58; Christian Birchinger &lt;[email-masked]&gt;
Date&#58;   Tue Jul 31 16&#58;22&#58;02 2007 +0300

    Two fixes&#58;
    
    1. Fix a race condition in thread stopping when creating the thread
       actually failed. Playtime could get recorded badly in XMMS and
       Audacious plugins
    
    2. Fix a race condition for starting a thread. uade_get_time&#40;&#41; can get
       called during play_file&#40;&#41; initialization and uadesong == NULL is
       dereferenced -&gt; segfault. This affects newer versions of Audacious.

 src/frontends/audacious/plugin.c |   61 ++++++++++++++++++++++++-------------
 src/frontends/xmms/plugin.c      |   22 ++++++++++----
 2 files changed, 56 insertions&#40;+&#41;, 27 deletions&#40;-&#41;
[/code:1:5ae0d7c0a8]

---

From: Joker
Date: Sun Aug 12 12:16:54 2007

To: UADE-Developer-discussion
Heres a small update, which is needed for the latest dev core.

[url]http://www.netswarm.net/misc/uade-audacious-set_pb_ready.patch[/url]
[code:1:05a0c63ff6]
diff -ruN uade.orig/src/frontends/audacious/plugin.c uade/src/frontends/audacious/plugin.c
--- uade.orig/src/frontends/audacious/plugin.c	2007-08-11 15&#58;05&#58;01.000000000 +0200
+++ uade/src/frontends/audacious/plugin.c	2007-08-11 15&#58;05&#58;13.000000000 +0200
@@ -835,6 +835,7 @@
 #if __AUDACIOUS_PLUGIN_API__ &gt;= 3
   decode_thread = pthread_self&#40;&#41;;
   uade_thread_running = 1;
+  playhandle-&gt;set_pb_ready&#40;playhandle&#41;;
   play_loop&#40;playhandle&#41;;
 #else
   if &#40;pthread_create&#40;&amp;decode_thread, NULL, play_loop, playhandle&#41;&#41; &#123;
[/code:1:05a0c63ff6]

---

From: shd
Date: Sun Aug 12 14:54:48 2007

To: UADE-Developer-discussion
[quote:ec0c75dd93="Joker"]Heres a small update, which is needed for the latest dev core.
[/quote:ec0c75dd93]
Could you please explain the patch so that I can commit with an explanation?

---

From: Joker
Date: Sun Aug 12 15:12:35 2007

To: UADE-Developer-discussion
Since the core creates the thread now, it has no direct knowledge of when the playback is really ready. This could cause race conditions when sending &quot;stop&quot; before the plugin even started to play.

Because of this the plugins now notify the core when it's all initiated. This is usualy when play_song() exited in the old threading model and the thread was running.

---

From: shd
Date: Sun Aug 12 15:24:05 2007

To: UADE-Developer-discussion
[quote:728e44aad5="Joker"]Since the core creates the thread now, it has no direct knowledge of when the playback is really ready. This could cause race conditions when sending &quot;stop&quot; before the plugin even started to play.

Because of this the plugins now notify the core when it's all initiated. This is usualy when play_song() exited in the old threading model and the thread was running.[/quote:728e44aad5]
Committed and pushed.

---

From: Joker
Date: Mon Oct 1 17:06:23 2007

To: UADE-Developer-discussion
Audacious removed the xmms_usleep() function and uses g_usleep()
everywhere instead

[url]http://www.netswarm.net/misc/uade-audacious-usleep.patch[/url]

[code:1:bf4e0980d3]
diff -ruN uade-9999.orig/src/frontends/audacious/plugin.c uade-9999/src/frontends/audacious/plugin.c
--- uade-9999.orig/src/frontends/audacious/plugin.c	2007-09-18 18&#58;01&#58;46.000000000 +0200
+++ uade-9999/src/frontends/audacious/plugin.c	2007-10-01 14&#58;37&#58;35.000000000 +0200
@@ -481,7 +481,11 @@
 	    uade_change_subsong&#40;&amp;effects, &amp;config, uadesong, &amp;uadeipc&#41;;
 
 	    while &#40;playhandle-&gt;output-&gt;buffer_playing&#40;&#41;&#41;
+#if __AUDACIOUS_PLUGIN_API__ &gt;= 6
+	      g_usleep&#40;10000&#41;;
+#else
 	      xmms_usleep&#40;10000&#41;;
+#endif
 
 	    playhandle-&gt;output-&gt;flush&#40;0&#41;;
 
@@ -502,7 +506,11 @@
 	/* We must drain the audio fast if abort_playing happens &#40;e.g.
 	   the user changes song when we are here waiting the sound device&#41; */
 	while &#40;playhandle-&gt;output-&gt;buffer_playing&#40;&#41; &amp;&amp; abort_playing == 0&#41;
+#if __AUDACIOUS_PLUGIN_API__ &gt;= 6
+	  g_usleep&#40;10000&#41;;
+#else
	  xmms_usleep&#40;10000&#41;;
+#endif
 
 	break;
       &#125;
[/code:1:bf4e0980d3]

(Tried to post it before but couldn't login from work)

---

From: shd
Date: Tue Oct 2 23:02:07 2007

To: UADE-Developer-discussion
[quote:6274f80d33="Joker"]Audacious removed the xmms_usleep() function and uses g_usleep()
everywhere instead
[/quote:6274f80d33]
Merged. May I suggest that you use git-format-patch next time you send me something? That would include the email author and changelog message in a single file that I can use to apply to my copy of the repository..

Just commit your change, and then use:
[code:1:6274f80d33]$ git-format-patch LAST_VERSION_BEFORE_YOUR_COMMIT
[/code:1:6274f80d33]
If you only did 1 commit, that would be
[code:1:6274f80d33]$ git-format-patch HEAD^[/code:1:6274f80d33]
which will generate a patch file that you can send me (or put on your website)..

Now I had to manually edit your changelog message and find your email address.. The result was:
[code:1:6274f80d33]commit 494ab47e61713e9f3c914e8ee2046fa2094ff540
Author&#58; Christian Birchinger &lt;[email-masked]&gt;
Date&#58;   Mon Oct 1 23&#58;02&#58;43 2007 +0300

    Audacious removed the xmms_usleep&#40;&#41; function and uses g_usleep&#40;&#41;
    everywhere instead &#91;ADAPTIVE&#93;
[/code:1:6274f80d33]

---

From: Joker
Date: Mon Oct 15 13:30:57 2007

To: UADE-Developer-discussion
Here are the latest and hopefully final changes before 1.4:

http://www.netswarm.net/misc/0001-Latest-API-updates-for-Audacious-1.4-beta1.patch
(a bit big to inline here)

Contains: Latest API updates for Audacious 1.4 beta1                                                                                       

- produce_audio() is now playhandle-&gt;pass_audio()
- set_info() got replaced by playhandle-&gt;set_params()

---

From: shd
Date: Mon Oct 15 14:00:29 2007

To: UADE-Developer-discussion
[quote:29745726c4="Joker"]Here are the latest and hopefully final changes before 1.4:

- produce_audio() is now playhandle-&gt;pass_audio()
- set_info() got replaced by playhandle-&gt;set_params()[/quote:29745726c4]

Thanks! I did some whitespace cleaning, and removed an #ifdef.. See if it works, I don't have audacious here. So, git-pull..

btw. are you happy with indentation in audacious/plugin.c? Changing to 4 spaces or 8 spaces (tabs) is entirely acceptable to me. When I started doing UADE I used the emacs default of 2 spaces, but now I'm moving to 4 or 8 spaces (depending on the project), because I now find it more readable..

---

From: Joker
Date: Mon Oct 15 20:36:16 2007

To: UADE-Developer-discussion
I try to adapt to the original indenting style when sending patches to projects.
If i did wrong in this case, it's not because i wanted to change the style, but
interpreted something wrong.

Once 1.4 is out, it would maybe be more readable to habe only one set of 
#ifdef for 1.3 and 1.4 and not dev versions in between.

---

From: shd
Date: Mon Oct 15 20:56:44 2007

To: UADE-Developer-discussion
[quote:dc8d13519d="Joker"]I try to adapt to the original indenting style when sending patches to projects.
If i did wrong in this case, it's not because i wanted to change the style, but
interpreted something wrong.
[/quote:dc8d13519d]
Nah, you didn't do anything wrong.. Two space indentation is just beginning to bother me, and since you seem to be doing most changes into audacious plugin thesedays, it's only fair to ask your opinion on this matter..

[quote:dc8d13519d]Once 1.4 is out, it would maybe be more readable to habe only one set of 
#ifdef for 1.3 and 1.4 and not dev versions in between.[/quote:dc8d13519d]
Agreed. Supporting dev versions is badly spent effort.. If only we knew how many users we have, and which versions they have..