[Swfdec] configure.ac libswfdec-gtk/Makefile.am libswfdec-gtk/swfdec_playback_pa.c

Eric Anholt anholt at kemper.freedesktop.org
Wed Nov 7 18:03:33 PST 2007


 configure.ac                       |   18 ++
 libswfdec-gtk/Makefile.am          |    1 
 libswfdec-gtk/swfdec_playback_pa.c |  331 +++++++++++++++++++++++++++++++++++++
 3 files changed, 350 insertions(+)

New commits:
commit a14e3cf83f1a9e5bd0a226c1e794bdddf94ae7a9
Author: Eric Anholt <eric at anholt.net>
Date:   Wed Nov 7 17:42:04 2007 -0800

    Add a simple pulseaudio playback backend.
    
    It appears to be playing on par with the OSS backend for absinthe.swf.  Both
    suffer from issues with streams not ending when they should in the file.  It
    also successfully plays on my PA-eanbled Linux machine, while the ALSA
    backend fails (despite other ALSA programs working).
    
    The priority order for auto config is alsa, pulse, oss.  If common desktops
    move to PA, we may want to revisit this ordering.

diff --git a/configure.ac b/configure.ac
index 5708323..fc48f5c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -141,6 +141,23 @@ if test "$with_audio" = "auto" -o "$with_audio" = "alsa"; then
   fi
 fi
 
+dnl Use PA if ALSA wasn't found and we're auto and it's available.
+dnl Disabled for now until the PA backend works.
+if test "$with_audio" = "auto" -o "$with_audio" = "pa"; then
+  PKG_CHECK_MODULES(PA, libpulse-mainloop-glib, AUDIO_TYPE=pa)
+  if test "$AUDIO_TYPE" = "pa"; then
+    with_audio=pa
+    AUDIO_CFLAGS=$PA_CFLAGS
+    AUDIO_LIBS=$PA_LIBS
+  else
+    if test "$with_audio" = "pa"; then
+      AC_MSG_ERROR([no pulseaudio support])
+    else
+      AC_MSG_WARN([no pulseaudio support])
+    fi
+  fi
+fi
+
 dnl Assume OSS is available if ALSA wasn't found and we're "auto".
 if test "$with_audio" = "auto" -o "$with_audio" = "oss"; then
   with_audio="oss"
@@ -149,6 +166,7 @@ if test "$with_audio" = "auto" -o "$with_audio" = "oss"; then
   AUDIO_TYPE=oss
 fi
 
+dnl If all else fails, fall back to none.
 if test "$with_audio" = "auto" -o "$with_audio" = "none"; then
   AUDIO_CFLAGS=
   AUDIO_LIBS=
diff --git a/libswfdec-gtk/Makefile.am b/libswfdec-gtk/Makefile.am
index 5aa9af5..c3c140f 100644
--- a/libswfdec-gtk/Makefile.am
+++ b/libswfdec-gtk/Makefile.am
@@ -39,4 +39,5 @@ libswfdec_ at SWFDEC_MAJORMINOR@include_HEADERS = \
 EXTRA_DIST = \
 	swfdec_playback_alsa.c \
 	swfdec_playback_oss.c \
+	swfdec_playback_pa.c \
 	swfdec_playback_none.c
diff --git a/libswfdec-gtk/swfdec_playback_pa.c b/libswfdec-gtk/swfdec_playback_pa.c
new file mode 100644
index 0000000..598c273
--- /dev/null
+++ b/libswfdec-gtk/swfdec_playback_pa.c
@@ -0,0 +1,331 @@
+/* Swfdec
+ * Copyright © 2006 Benjamin Otte <otte at gnome.org>
+ * Copyright © 2007 Eric Anholt <eric at anholt.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "swfdec_playback.h"
+#include "pulse/pulseaudio.h"
+#include "pulse/glib-mainloop.h"
+
+/** @file Implements swfdec audio playback by dumping swfdec streams out
+ * using pulseaudio streams.
+ */
+
+/*** DEFINITIONS ***/
+
+struct _SwfdecPlayback {
+  SwfdecPlayer *	player;
+  GList *		streams;	/* all Stream objects */
+  GMainContext *	context;	/* glib context we work in */
+  pa_glib_mainloop *	pa_mainloop;	/* PA to glib mainloop connection */
+  pa_context *		pa;		/* PA context for sound rendering */
+};
+
+typedef struct {
+  SwfdecPlayback *     	sound;		/* reference to sound object */
+  SwfdecAudio *		audio;		/* the audio we play back */
+  guint			offset;		/* offset into sound */
+  pa_stream *		pa;		/* PA stream */
+  pa_cvolume		volume;		/* Volume control.  Not yet used. */
+  gboolean		no_more;
+} Stream;
+
+/* Size of one of our audio samples, in bytes */
+#define SAMPLESIZE	2
+#define CHANNELS	2
+
+/*** STREAMS ***/
+
+static void
+stream_write_callback (pa_stream *pa,
+		       size_t bytes,
+		       void *data)
+{
+  Stream *stream = data;
+  char *frag;
+  unsigned int samples = bytes / SAMPLESIZE / CHANNELS;
+  int err;
+
+  if (stream->no_more)
+    return;
+
+  /* Adjust to our rounded-down number */
+  bytes = samples * SAMPLESIZE * CHANNELS;
+
+  frag = malloc (bytes);
+  if (frag == NULL) {
+    g_printerr ("Failed to allocate fragment of size %d\n", bytes);
+    return;
+  }
+
+  /* Set up our fragment and render swfdec's audio into it. The swfdec audio
+   * decoder renders deltas from the existing data in the fragment.
+   */
+  memset (frag, 0, bytes);
+  swfdec_audio_render (stream->audio, (gint16 *)frag, stream->offset,
+		       samples);
+
+  /* Send the new fragment out the PA stream */
+  err = pa_stream_write (pa, frag, bytes, NULL, 0, PA_SEEK_RELATIVE);
+  if (err != 0) {
+    g_printerr ("Failed to write fragment to PA stream: %s\n",
+		pa_strerror(pa_context_errno(stream->sound->pa)));
+  }
+
+  /* Advance playback pointer */
+  stream->offset += samples;
+
+  free(frag);
+}
+
+static void
+stream_drain_complete (pa_stream *pa, int success, void *data)
+{
+  Stream *stream = data;
+
+  pa_stream_disconnect (stream->pa);
+  pa_stream_unref (stream->pa);
+  g_object_unref (stream->audio);
+  g_free (stream);
+}
+
+static void
+swfdec_stream_close (Stream *stream)
+{
+  /* Pull it off of the active stream list. */
+  stream->sound->streams = g_list_remove (stream->sound->streams, stream);
+
+  /* If we have created a PA stream, defer freeing until we drain it. */
+  if (stream->pa != NULL) {
+    stream->no_more = 1;
+    pa_operation_unref (pa_stream_drain (stream->pa,
+					 stream_drain_complete,
+					 stream));
+  } else {
+    g_object_unref (stream->audio);
+    g_free (stream);
+  }
+}
+
+static void
+stream_state_callback (pa_stream *pa, void *data)
+{
+  switch (pa_stream_get_state(pa)) {
+  case PA_STREAM_CREATING:
+  case PA_STREAM_TERMINATED:
+  case PA_STREAM_READY:
+    break;
+
+  case PA_STREAM_FAILED:
+    g_printerr("PA stream failed: %s\n",
+	       pa_strerror(pa_context_errno(pa_stream_get_context(pa))));
+  default:
+    break;
+  }
+}
+
+static void
+swfdec_stream_open (SwfdecPlayback *sound, SwfdecAudio *audio)
+{
+  Stream *stream;
+  pa_sample_spec spec = {
+    .format = PA_SAMPLE_S16LE,
+    .rate = 44100,
+    .channels = CHANNELS,
+  };
+  int err;
+
+  stream = g_new0 (Stream, 1);
+  stream->sound = sound;
+  stream->audio = g_object_ref (audio);
+  sound->streams = g_list_prepend (sound->streams, stream);
+
+  /* Create our stream */
+  stream->pa = pa_stream_new(sound->pa,
+			     "swfdec stream",
+			     &spec,
+			     NULL /* Default channel map */
+			     );
+  if (stream->pa == NULL) {
+    g_printerr("Failed to create PA stream\n");
+    swfdec_stream_close(stream);
+    return;
+  }
+
+  /* Start at default volume */
+  pa_cvolume_set(&stream->volume, CHANNELS, PA_VOLUME_NORM);
+
+  /* Hook up our stream write callback for when new data is needed */
+  pa_stream_set_state_callback(stream->pa, stream_state_callback, stream);
+  pa_stream_set_write_callback(stream->pa, stream_write_callback, stream);
+
+  /* Connect it up as a playback stream. */
+  err = pa_stream_connect_playback(stream->pa,
+				   NULL, /* Default device */
+				   NULL /* Default buffering */,
+				   0, /* No flags */
+				   &stream->volume,
+				   NULL /* Don't sync to any stream */
+				   );
+  if (err != 0) {
+    g_printerr ("Failed to connect PA stream: %s\n",
+		pa_strerror(pa_context_errno(sound->pa)));
+    swfdec_stream_close(stream);
+    return;
+  }
+}
+
+/*** SOUND ***/
+
+static void
+advance_before (SwfdecPlayer *player, guint msecs, guint audio_samples, gpointer data)
+{
+  SwfdecPlayback *sound = data;
+  GList *walk;
+
+  for (walk = sound->streams; walk; walk = walk->next) {
+    Stream *stream = walk->data;
+    if (audio_samples >= stream->offset) {
+      stream->offset = 0;
+    } else {
+      stream->offset -= audio_samples;
+    }
+  }
+}
+ 
+static void
+audio_added (SwfdecPlayer *player, SwfdecAudio *audio, SwfdecPlayback *sound)
+{
+  swfdec_stream_open (sound, audio);
+}
+
+static void
+audio_removed (SwfdecPlayer *player, SwfdecAudio *audio, SwfdecPlayback *sound)
+{
+  GList *walk;
+
+  for (walk = sound->streams; walk; walk = walk->next) {
+    Stream *stream = walk->data;
+    if (stream->audio == audio) {
+      swfdec_stream_close (stream);
+      return;
+    }
+  }
+  g_assert_not_reached ();
+}
+static void
+context_state_callback (pa_context *pa, void *data) {
+  switch (pa_context_get_state(pa)) {
+  case PA_CONTEXT_UNCONNECTED:
+  case PA_CONTEXT_CONNECTING:
+  case PA_CONTEXT_AUTHORIZING:
+  case PA_CONTEXT_SETTING_NAME:
+  case PA_CONTEXT_READY:
+    break;
+
+  case PA_CONTEXT_FAILED:
+    g_printerr("PA context failed");
+    break;
+
+  case PA_CONTEXT_TERMINATED:
+    g_printerr("PA context lost\n");
+    break;
+  }
+}
+
+SwfdecPlayback *
+swfdec_playback_open (SwfdecPlayer *player, GMainContext *context)
+{
+  SwfdecPlayback *sound;
+  const GList *walk;
+  pa_mainloop_api *pa_api;
+
+  g_return_val_if_fail (SWFDEC_IS_PLAYER (player), NULL);
+  g_return_val_if_fail (context != NULL, NULL);
+
+  sound = g_new0 (SwfdecPlayback, 1);
+  sound->player = player;
+  g_signal_connect (player, "advance", G_CALLBACK (advance_before), sound);
+  g_signal_connect (player, "audio-added", G_CALLBACK (audio_added), sound);
+  g_signal_connect (player, "audio-removed", G_CALLBACK (audio_removed), sound);
+
+  /* Create our mainloop attachment to glib.  XXX: I hope this means we don't
+   * have to run the main loop using pa functions.
+   */
+  sound->pa_mainloop = pa_glib_mainloop_new (context);
+  pa_api = pa_glib_mainloop_get_api (sound->pa_mainloop);
+
+  sound->pa = pa_context_new (pa_api, "swfdec");
+
+  pa_context_set_state_callback (sound->pa, context_state_callback, NULL);
+  pa_context_connect (sound->pa,
+		      NULL, /* default server */
+		      0, /* default flags */
+		      NULL /* spawning api */
+		      );
+
+  for (walk = swfdec_player_get_audio (player); walk; walk = walk->next) {
+    swfdec_stream_open (sound, walk->data);
+  }
+  g_main_context_ref (context);
+  sound->context = context;
+  return sound;
+}
+
+static void
+context_drain_complete (pa_context *pa, void *data)
+{
+  pa_context_disconnect (pa);
+  pa_context_unref (pa);
+}
+
+void
+swfdec_playback_close (SwfdecPlayback *sound)
+{
+#define REMOVE_HANDLER_FULL(obj,func,data,count) G_STMT_START {\
+  if (g_signal_handlers_disconnect_by_func ((obj), \
+	G_CALLBACK (func), (data)) != (count)) { \
+    g_assert_not_reached (); \
+  } \
+} G_STMT_END
+#define REMOVE_HANDLER(obj,func,data) REMOVE_HANDLER_FULL (obj, func, data, 1)
+
+  while (sound->streams)
+    swfdec_stream_close (sound->streams->data);
+  REMOVE_HANDLER (sound->player, advance_before, sound);
+  REMOVE_HANDLER (sound->player, audio_added, sound);
+  REMOVE_HANDLER (sound->player, audio_removed, sound);
+
+  pa_operation_unref (pa_context_drain (sound->pa,
+					context_drain_complete,
+					NULL));
+  pa_context_unref (sound->pa);
+  pa_glib_mainloop_free (sound->pa_mainloop);
+
+  g_main_context_unref (sound->context);
+  g_free (sound);
+}
+
+


More information about the Swfdec mailing list