Revert "pulsesink: Make 2.0 dependency optional"
[platform/upstream/gstreamer.git] / ext / pulse / pulsesink.c
index 8b964f7..cd257d6 100644 (file)
  *
  *  You should have received a copy of the GNU Lesser General Public
  *  License along with gst-pulse; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  *  USA.
  */
 
 /**
  * SECTION:element-pulsesink
- * @see_also: pulsesrc, pulsemixer
+ * @see_also: pulsesrc
  *
  * This element outputs audio to a
  * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
  * <refsect2>
  * <title>Example pipelines</title>
  * |[
- * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
+ * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
  * ]| Play an Ogg/Vorbis file.
  * |[
- * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
+ * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
  * ]| Play a 440Hz sine wave.
  * |[
- * gst-launch -v audiotestsrc ! pulsesink stream-properties="props,media.title=test"
+ * gst-launch-1.0 -v audiotestsrc ! pulsesink stream-properties="props,media.title=test"
  * ]| Play a sine wave and set a stream property. The property can be checked
  * with "pactl list".
  * </refsect2>
 
 #include <gst/base/gstbasesink.h>
 #include <gst/gsttaglist.h>
-#include <gst/interfaces/streamvolume.h>
+#include <gst/audio/audio.h>
 #include <gst/gst-i18n-plugin.h>
-#include <gst/audio/gstaudioiec61937.h>
 
 #include <gst/pbutils/pbutils.h>        /* only used for GST_PLUGINS_BASE_VERSION_* */
 
+#include <gst/glib-compat-private.h>
+
 #include "pulsesink.h"
 #include "pulseutil.h"
 
@@ -79,7 +80,7 @@ enum
   PROP_DEVICE_NAME,
   PROP_VOLUME,
   PROP_MUTE,
-  PROP_CLIENT,
+  PROP_CLIENT_NAME,
   PROP_STREAM_PROPERTIES,
   PROP_LAST
 };
@@ -124,28 +125,25 @@ static pa_threaded_mainloop *mainloop = NULL;
 static guint mainloop_ref_ct = 0;
 
 /* lock for access to shared resources */
-static GMutex *pa_shared_resource_mutex = NULL;
+static GMutex pa_shared_resource_mutex;
 
 /* We keep a custom ringbuffer that is backed up by data allocated by
  * pulseaudio. We must also overide the commit function to write into
  * pulseaudio memory instead. */
 struct _GstPulseRingBuffer
 {
-  GstRingBuffer object;
+  GstAudioRingBuffer object;
 
   gchar *context_name;
   gchar *stream_name;
 
   pa_context *context;
   pa_stream *stream;
+  pa_stream *probe_stream;
 
-#ifdef HAVE_PULSE_1_0
   pa_format_info *format;
   guint channels;
   gboolean is_pcm;
-#else
-  pa_sample_spec sample_spec;
-#endif
 
   void *m_data;
   size_t m_towrite;
@@ -159,34 +157,34 @@ struct _GstPulseRingBuffer
 };
 struct _GstPulseRingBufferClass
 {
-  GstRingBufferClass parent_class;
+  GstAudioRingBufferClass parent_class;
 };
 
 static GType gst_pulseringbuffer_get_type (void);
 static void gst_pulseringbuffer_finalize (GObject * object);
 
-static GstRingBufferClass *ring_parent_class = NULL;
-
-static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
-static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
-static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
-    GstRingBufferSpec * spec);
-static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
-static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
-static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
-static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
-static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
-static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
+static GstAudioRingBufferClass *ring_parent_class = NULL;
+
+static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
+static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
+static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
+    GstAudioRingBufferSpec * spec);
+static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
+static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
+static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
+static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
+static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
+static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
     guint64 * sample, guchar * data, gint in_samples, gint out_samples,
     gint * accum);
 
-G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
+G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
+    GST_TYPE_AUDIO_RING_BUFFER);
 
 static void
 gst_pulsesink_init_contexts (void)
 {
-  g_assert (pa_shared_resource_mutex == NULL);
-  pa_shared_resource_mutex = g_mutex_new ();
+  g_mutex_init (&pa_shared_resource_mutex);
   gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
       g_free, NULL);
 }
@@ -195,10 +193,10 @@ static void
 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
 {
   GObjectClass *gobject_class;
-  GstRingBufferClass *gstringbuffer_class;
+  GstAudioRingBufferClass *gstringbuffer_class;
 
   gobject_class = (GObjectClass *) klass;
-  gstringbuffer_class = (GstRingBufferClass *) klass;
+  gstringbuffer_class = (GstAudioRingBufferClass *) klass;
 
   ring_parent_class = g_type_class_peek_parent (klass);
 
@@ -228,14 +226,11 @@ gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
   pbuf->stream_name = NULL;
   pbuf->context = NULL;
   pbuf->stream = NULL;
+  pbuf->probe_stream = NULL;
 
-#ifdef HAVE_PULSE_1_0
   pbuf->format = NULL;
   pbuf->channels = 0;
   pbuf->is_pcm = FALSE;
-#else
-  pa_sample_spec_init (&pbuf->sample_spec);
-#endif
 
   pbuf->m_data = NULL;
   pbuf->m_towrite = 0;
@@ -248,9 +243,32 @@ gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
   pbuf->paused = FALSE;
 }
 
+/* Call with mainloop lock held if wait == TRUE) */
+static void
+gst_pulse_destroy_stream (pa_stream * stream, gboolean wait)
+{
+  /* Make sure we don't get any further callbacks */
+  pa_stream_set_write_callback (stream, NULL, NULL);
+  pa_stream_set_underflow_callback (stream, NULL, NULL);
+  pa_stream_set_overflow_callback (stream, NULL, NULL);
+
+  pa_stream_disconnect (stream);
+
+  if (wait)
+    pa_threaded_mainloop_wait (mainloop);
+
+  pa_stream_set_state_callback (stream, NULL, NULL);
+  pa_stream_unref (stream);
+}
+
 static void
 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
 {
+  if (pbuf->probe_stream) {
+    gst_pulse_destroy_stream (pbuf->probe_stream, FALSE);
+    pbuf->probe_stream = NULL;
+  }
+
   if (pbuf->stream) {
 
     if (pbuf->m_data) {
@@ -264,14 +282,12 @@ gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
       pbuf->m_offset = 0;
       pbuf->m_lastoffset = 0;
     }
-#ifdef HAVE_PULSE_1_0
     if (pbuf->format) {
       pa_format_info_free (pbuf->format);
       pbuf->format = NULL;
       pbuf->channels = 0;
       pbuf->is_pcm = FALSE;
     }
-#endif
 
     pa_stream_disconnect (pbuf->stream);
 
@@ -292,7 +308,7 @@ gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
 static void
 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
 {
-  g_mutex_lock (pa_shared_resource_mutex);
+  g_mutex_lock (&pa_shared_resource_mutex);
 
   GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
 
@@ -333,7 +349,7 @@ gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
     g_free (pbuf->context_name);
     pbuf->context_name = NULL;
   }
-  g_mutex_unlock (pa_shared_resource_mutex);
+  g_mutex_unlock (&pa_shared_resource_mutex);
 }
 
 static void
@@ -415,7 +431,7 @@ gst_pulsering_context_subscribe_cb (pa_context * c,
     GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
     psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
 
-    GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
+    GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
 
     if (!pbuf->stream)
       continue;
@@ -423,7 +439,6 @@ gst_pulsering_context_subscribe_cb (pa_context * c,
     if (idx != pa_stream_get_index (pbuf->stream))
       continue;
 
-#ifdef HAVE_PULSE_1_0
     if (psink->device && pbuf->is_pcm &&
         !g_str_equal (psink->device,
             pa_stream_get_device_name (pbuf->stream))) {
@@ -436,13 +451,14 @@ gst_pulsering_context_subscribe_cb (pa_context * c,
 
       GST_INFO_OBJECT (psink, "emitting sink-changed");
 
+      /* FIXME: send reconfigure event instead and let decodebin/playbin
+       * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
       renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
           gst_structure_new_empty ("pulse-sink-changed"));
 
       if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
         GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
     }
-#endif
 
     /* Actually this event is also triggered when other properties of
      * the stream change that are unrelated to the volume. However it is
@@ -457,7 +473,7 @@ gst_pulsering_context_subscribe_cb (pa_context * c,
 /* will be called when the device should be opened. In this case we will connect
  * to the server. We should not try to open any streams in this state. */
 static gboolean
-gst_pulseringbuffer_open_device (GstRingBuffer * buf)
+gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -479,7 +495,7 @@ gst_pulseringbuffer_open_device (GstRingBuffer * buf)
 
   pa_threaded_mainloop_lock (mainloop);
 
-  g_mutex_lock (pa_shared_resource_mutex);
+  g_mutex_lock (&pa_shared_resource_mutex);
   need_unlock_shared = TRUE;
 
   pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
@@ -516,7 +532,7 @@ gst_pulseringbuffer_open_device (GstRingBuffer * buf)
     pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
   }
 
-  g_mutex_unlock (pa_shared_resource_mutex);
+  g_mutex_unlock (&pa_shared_resource_mutex);
   need_unlock_shared = FALSE;
 
   /* context created or shared okay */
@@ -540,6 +556,11 @@ gst_pulseringbuffer_open_device (GstRingBuffer * buf)
     pa_threaded_mainloop_wait (mainloop);
   }
 
+  if (pa_context_get_server_protocol_version (pbuf->context) < 22) {
+    /* We need PulseAudio >= 1.0 on the server side for the extended API */
+    goto bad_server_version;
+  }
+
   GST_LOG_OBJECT (psink, "opened the device");
 
   pa_threaded_mainloop_unlock (mainloop);
@@ -550,7 +571,7 @@ gst_pulseringbuffer_open_device (GstRingBuffer * buf)
 unlock_and_fail:
   {
     if (need_unlock_shared)
-      g_mutex_unlock (pa_shared_resource_mutex);
+      g_mutex_unlock (&pa_shared_resource_mutex);
     gst_pulsering_destroy_context (pbuf);
     pa_threaded_mainloop_unlock (mainloop);
     return FALSE;
@@ -568,11 +589,17 @@ connect_failed:
             pa_strerror (pa_context_errno (pctx->context))), (NULL));
     goto unlock_and_fail;
   }
+bad_server_version:
+  {
+    GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("PulseAudio server version "
+            "is too old."), (NULL));
+    goto unlock_and_fail;
+  }
 }
 
 /* close the device */
 static gboolean
-gst_pulseringbuffer_close_device (GstRingBuffer * buf)
+gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -621,10 +648,10 @@ static void
 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
 {
   GstPulseSink *psink;
-  GstRingBuffer *rbuf;
+  GstAudioRingBuffer *rbuf;
   GstPulseRingBuffer *pbuf;
 
-  rbuf = GST_RING_BUFFER_CAST (userdata);
+  rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
   pbuf = GST_PULSERING_BUFFER_CAST (userdata);
   psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
 
@@ -666,6 +693,7 @@ gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
+  GstAudioRingBuffer *ringbuf;
   const pa_timing_info *info;
   pa_usec_t sink_usec;
 
@@ -673,11 +701,26 @@ gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
 
   pbuf = GST_PULSERING_BUFFER_CAST (userdata);
   psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
+  ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
 
   if (!info) {
     GST_LOG_OBJECT (psink, "latency update (information unknown)");
     return;
   }
+
+  if (!info->read_index_corrupt) {
+    /* Update segdone based on the read index. segdone is of segment
+     * granularity, while the read index is at byte granularity. We take the
+     * ceiling while converting the latter to the former since it is more
+     * conservative to report that we've read more than we have than to report
+     * less. One concern here is that latency updates happen every 100ms, which
+     * means segdone is not updated very often, but increasing the update
+     * frequency would mean more communication overhead. */
+    g_atomic_int_set (&ringbuf->segdone,
+        (int) gst_util_uint64_scale_ceil (info->read_index, 1,
+            ringbuf->spec.segsize));
+  }
+
   sink_usec = info->configured_sink_usec;
 
   GST_LOG_OBJECT (psink,
@@ -737,7 +780,6 @@ gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
     gst_element_post_message (GST_ELEMENT_CAST (psink),
         gst_message_new_request_state (GST_OBJECT_CAST (psink),
             GST_STATE_PLAYING));
-#ifdef HAVE_PULSE_1_0
   } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
     GstEvent *renego;
 
@@ -754,15 +796,27 @@ gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
     g_free (psink->device);
     psink->device = g_strdup (pa_proplist_gets (pl, "device"));
 
+    /* FIXME: send reconfigure event instead and let decodebin/playbin
+     * handle that. Also take care of ac3 alignment */
     renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
         gst_structure_new_empty ("pulse-format-lost"));
 
+#if 0
+    if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
+      GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
+          "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
+
+      if (!gst_pad_push_event (pbin->sinkpad,
+              gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
+        GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
+    }
+#endif
+
     if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
       /* Nobody handled the format change - emit an error */
       GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
           ("Sink format changed"));
     }
-#endif
   } else {
     GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
   }
@@ -794,7 +848,8 @@ gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
 /* This method should create a new stream of the given @spec. No playback should
  * start yet so we start in the corked state. */
 static gboolean
-gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
+gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
+    GstAudioRingBufferSpec * spec)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -802,33 +857,24 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
   const pa_buffer_attr *actual;
   pa_channel_map channel_map;
   pa_operation *o = NULL;
-#ifdef HAVE_PULSE_0_9_20
   pa_cvolume v;
-#endif
   pa_cvolume *pv = NULL;
   pa_stream_flags_t flags;
   const gchar *name;
   GstAudioClock *clock;
-#ifdef HAVE_PULSE_1_0
   pa_format_info *formats[1];
 #ifndef GST_DISABLE_GST_DEBUG
   gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
 #endif
-#endif
 
   psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
   pbuf = GST_PULSERING_BUFFER_CAST (buf);
 
   GST_LOG_OBJECT (psink, "creating sample spec");
   /* convert the gstreamer sample spec to the pulseaudio format */
-#ifdef HAVE_PULSE_1_0
   if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
     goto invalid_spec;
   pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
-#else
-  if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
-    goto invalid_spec;
-#endif
 
   pa_threaded_mainloop_lock (mainloop);
 
@@ -836,6 +882,13 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
   g_assert (pbuf->context);
   g_assert (!pbuf->stream);
 
+  /* if we have a probe, disconnect it first so that if we're creating a
+   * compressed stream, it doesn't get blocked by a PCM stream */
+  if (pbuf->probe_stream) {
+    gst_pulse_destroy_stream (pbuf->probe_stream, TRUE);
+    pbuf->probe_stream = NULL;
+  }
+
   /* enable event notifications */
   GST_LOG_OBJECT (psink, "subscribing to context events");
   if (!(o = pa_context_subscribe (pbuf->context,
@@ -845,12 +898,8 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
   pa_operation_unref (o);
 
   /* initialize the channel map */
-#ifdef HAVE_PULSE_1_0
   if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
     pa_format_info_set_channel_map (pbuf->format, &channel_map);
-#else
-  gst_pulse_gst_to_channel_map (&channel_map, spec);
-#endif
 
   /* find a good name for the stream */
   if (psink->stream_name)
@@ -859,17 +908,10 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
     name = "Playback Stream";
 
   /* create a stream */
-#ifdef HAVE_PULSE_1_0
   formats[0] = pbuf->format;
   if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
               psink->proplist)))
     goto stream_failed;
-#else
-  GST_LOG_OBJECT (psink, "creating stream with name %s", name);
-  if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
-              &pbuf->sample_spec, &channel_map, psink->proplist)))
-    goto stream_failed;
-#endif
 
   /* install essential callbacks */
   pa_stream_set_state_callback (pbuf->stream,
@@ -902,33 +944,30 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
   GST_INFO_OBJECT (psink, "prebuf:    %d", wanted.prebuf);
   GST_INFO_OBJECT (psink, "minreq:    %d", wanted.minreq);
 
-#ifdef HAVE_PULSE_0_9_20
   /* configure volume when we changed it, else we leave the default */
   if (psink->volume_set) {
     GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
     pv = &v;
-#ifdef HAVE_PULSE_1_0
     if (pbuf->is_pcm)
       gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
     else {
       GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
       pv = NULL;
     }
-#else
-    gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
-        psink->volume);
-#endif
   } else {
     pv = NULL;
   }
-#endif
 
   /* construct the flags */
   flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
       PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
 
-  if (psink->mute_set && psink->mute)
-    flags |= PA_STREAM_START_MUTED;
+  if (psink->mute_set) {
+    if (psink->mute)
+      flags |= PA_STREAM_START_MUTED;
+    else
+      flags |= PA_STREAM_START_UNMUTED;
+  }
 
   /* we always start corked (see flags above) */
   pbuf->corked = TRUE;
@@ -941,13 +980,12 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
     goto connect_failed;
 
   /* our clock will now start from 0 again */
-  clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
+  clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
   gst_audio_clock_reset (clock, 0);
 
   if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
     goto connect_failed;
 
-#ifdef HAVE_PULSE_1_0
   g_free (psink->device);
   psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
 
@@ -956,7 +994,6 @@ gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
       pa_stream_get_format_info (pbuf->stream));
   GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
 #endif
-#endif
 
   /* After we passed the volume off of to PA we never want to set it
      again, since it is PA's job to save/restore volumes.  */
@@ -1020,7 +1057,7 @@ connect_failed:
 
 /* free the stream that we acquired before */
 static gboolean
-gst_pulseringbuffer_release (GstRingBuffer * buf)
+gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
 {
   GstPulseRingBuffer *pbuf;
 
@@ -1030,7 +1067,6 @@ gst_pulseringbuffer_release (GstRingBuffer * buf)
   gst_pulsering_destroy_stream (pbuf);
   pa_threaded_mainloop_unlock (mainloop);
 
-#ifdef HAVE_PULSE_1_0
   {
     GstPulseSink *psink;
 
@@ -1038,7 +1074,6 @@ gst_pulseringbuffer_release (GstRingBuffer * buf)
     g_atomic_int_set (&psink->format_lost, FALSE);
     psink->format_lost_time = GST_CLOCK_TIME_NONE;
   }
-#endif
 
   return TRUE;
 }
@@ -1061,12 +1096,10 @@ gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
 
   psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
 
-#ifdef HAVE_PULSE_1_0
   if (g_atomic_int_get (&psink->format_lost)) {
     /* Sink format changed, stream's gone so fake being paused */
     return TRUE;
   }
-#endif
 
   GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
   if (pbuf->corked != corked) {
@@ -1107,7 +1140,7 @@ cork_failed:
 }
 
 static void
-gst_pulseringbuffer_clear (GstRingBuffer * buf)
+gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -1134,13 +1167,13 @@ mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
   GstMessage *message;
   GValue val = { 0 };
 
-  g_value_init (&val, G_TYPE_POINTER);
-  g_value_set_pointer (&val, g_thread_self ());
-
   GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
   message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
       GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
+  g_value_init (&val, GST_TYPE_G_THREAD);
+  g_value_set_boxed (&val, g_thread_self ());
   gst_message_set_stream_status_object (message, &val);
+  g_value_unset (&val);
 
   gst_element_post_message (GST_ELEMENT (pulsesink), message);
 
@@ -1151,7 +1184,7 @@ mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
 
 /* start/resume playback ASAP, we don't uncork here but in the commit method */
 static gboolean
-gst_pulseringbuffer_start (GstRingBuffer * buf)
+gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -1171,7 +1204,7 @@ gst_pulseringbuffer_start (GstRingBuffer * buf)
 
   /* EOS needs running clock */
   if (GST_BASE_SINK_CAST (psink)->eos ||
-      g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->eos_rendering))
+      g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
     gst_pulsering_set_corked (pbuf, FALSE, FALSE);
 
   pa_threaded_mainloop_unlock (mainloop);
@@ -1181,7 +1214,7 @@ gst_pulseringbuffer_start (GstRingBuffer * buf)
 
 /* pause/stop playback ASAP */
 static gboolean
-gst_pulseringbuffer_pause (GstRingBuffer * buf)
+gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -1213,13 +1246,14 @@ mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
   GstMessage *message;
   GValue val = { 0 };
 
-  g_value_init (&val, G_TYPE_POINTER);
-  g_value_set_pointer (&val, g_thread_self ());
-
   GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
   message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
       GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
+  g_value_init (&val, GST_TYPE_G_THREAD);
+  g_value_set_boxed (&val, g_thread_self ());
   gst_message_set_stream_status_object (message, &val);
+  g_value_unset (&val);
+
   gst_element_post_message (GST_ELEMENT (pulsesink), message);
 
   g_return_if_fail (pulsesink->defer_pending);
@@ -1229,7 +1263,7 @@ mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
 
 /* stop playback, we flush everything. */
 static gboolean
-gst_pulseringbuffer_stop (GstRingBuffer * buf)
+gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -1249,13 +1283,11 @@ gst_pulseringbuffer_stop (GstRingBuffer * buf)
     GST_DEBUG_OBJECT (psink, "signal commit thread");
     pa_threaded_mainloop_signal (mainloop, 0);
   }
-#ifdef HAVE_PULSE_1_0
   if (g_atomic_int_get (&psink->format_lost)) {
     /* Don't try to flush, the stream's probably gone by now */
     res = TRUE;
     goto cleanup;
   }
-#endif
 
   /* then try to flush, it's not fatal when this fails */
   GST_DEBUG_OBJECT (psink, "flushing");
@@ -1366,7 +1398,7 @@ G_STMT_START {                                  \
 /* our custom commit function because we write into the buffer of pulseaudio
  * instead of keeping our own buffer */
 static guint
-gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
+gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
     guchar * data, gint in_samples, gint out_samples, gint * accum)
 {
   GstPulseSink *psink;
@@ -1390,13 +1422,13 @@ gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
 
   /* make sure the ringbuffer is started */
   if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
-          GST_RING_BUFFER_STATE_STARTED)) {
+          GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
     /* see if we are allowed to start it */
     if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
       goto no_start;
 
     GST_DEBUG_OBJECT (buf, "start!");
-    if (!gst_ring_buffer_start (buf))
+    if (!gst_audio_ring_buffer_start (buf))
       goto start_failed;
   }
 
@@ -1426,12 +1458,10 @@ gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
    * needed to properly handle reverse playback: it points to the last sample. */
   data_end = data + (bpf * inr);
 
-#ifdef HAVE_PULSE_1_0
   if (g_atomic_int_get (&psink->format_lost)) {
     /* Sink format changed, drop the data and hope upstream renegotiates */
     goto fake_done;
   }
-#endif
 
   if (pbuf->paused)
     goto was_paused;
@@ -1480,12 +1510,10 @@ gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
       for (;;) {
         pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
 
-#ifdef HAVE_PULSE_1_0
         if (g_atomic_int_get (&psink->format_lost)) {
           /* Sink format changed, give up and hope upstream renegotiates */
           goto fake_done;
         }
-#endif
 
         if (pbuf->m_writable == (size_t) - 1)
           goto writable_size_failed;
@@ -1536,13 +1564,11 @@ gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
     GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
         (guint) avail, offset);
 
-#ifdef HAVE_PULSE_1_0
     /* No trick modes for passthrough streams */
     if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
       GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
       goto unlock_and_fail;
     }
-#endif
 
     if (G_LIKELY (inr == outr && !reverse)) {
       /* no rate conversion, simply write out the samples */
@@ -1624,9 +1650,7 @@ gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
     }
   }
 
-#ifdef HAVE_PULSE_1_0
 fake_done:
-#endif
   /* we consumed all samples here */
   data = data_end + bpf;
 
@@ -1701,7 +1725,7 @@ gst_pulsering_flush (GstPulseRingBuffer * pbuf)
 #ifndef GST_DISABLE_GST_DEBUG
     gint bpf;
 
-    bpf = (GST_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
+    bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
     GST_LOG_OBJECT (psink,
         "flushing %u samples at offset %" G_GINT64_FORMAT,
         (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
@@ -1736,6 +1760,7 @@ static void gst_pulsesink_get_property (GObject * object, guint prop_id,
 static void gst_pulsesink_finalize (GObject * object);
 
 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
+static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
 
 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
     GstStateChange transition);
@@ -1745,20 +1770,16 @@ static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
 
-GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
-
 #define gst_pulsesink_parent_class parent_class
-G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_BASE_AUDIO_SINK,
+G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
     gst_pulsesink_init_contexts ();
-    G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
-        gst_pulsesink_property_probe_interface_init);
     G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
     );
 
-static GstRingBuffer *
-gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
+static GstAudioRingBuffer *
+gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
 {
-  GstRingBuffer *buffer;
+  GstAudioRingBuffer *buffer;
 
   GST_DEBUG_OBJECT (sink, "creating ringbuffer");
   buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
@@ -1768,19 +1789,18 @@ gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
 }
 
 static GstBuffer *
-gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
+gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
 {
   switch (sink->ringbuffer->spec.type) {
-    case GST_BUFTYPE_AC3:
-    case GST_BUFTYPE_EAC3:
-    case GST_BUFTYPE_DTS:
-    case GST_BUFTYPE_MPEG:
+    case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
+    case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
+    case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
+    case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
     {
       /* FIXME: alloc memory from PA if possible */
       gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
       GstBuffer *out;
-      guint8 *indata, *outdata;
-      gsize insize, outsize;
+      GstMapInfo inmap, outmap;
       gboolean res;
 
       if (framesize <= 0)
@@ -1788,14 +1808,14 @@ gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
 
       out = gst_buffer_new_and_alloc (framesize);
 
-      indata = gst_buffer_map (buf, &insize, NULL, GST_MAP_READ);
-      outdata = gst_buffer_map (out, &outsize, NULL, GST_MAP_WRITE);
+      gst_buffer_map (buf, &inmap, GST_MAP_READ);
+      gst_buffer_map (out, &outmap, GST_MAP_WRITE);
 
-      res = gst_audio_iec61937_payload (indata, insize,
-          outdata, outsize, &sink->ringbuffer->spec);
+      res = gst_audio_iec61937_payload (inmap.data, inmap.size,
+          outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
 
-      gst_buffer_unmap (buf, indata, insize);
-      gst_buffer_unmap (out, outdata, outsize);
+      gst_buffer_unmap (buf, &inmap);
+      gst_buffer_unmap (out, &outmap);
 
       if (!res) {
         gst_buffer_unref (out);
@@ -1817,14 +1837,16 @@ gst_pulsesink_class_init (GstPulseSinkClass * klass)
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
   GstBaseSinkClass *bc;
-  GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
+  GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+  gchar *clientname;
 
   gobject_class->finalize = gst_pulsesink_finalize;
   gobject_class->set_property = gst_pulsesink_set_property;
   gobject_class->get_property = gst_pulsesink_get_property;
 
   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
+  gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
 
   /* restore the original basesink pull methods */
   bc = g_type_class_peek (GST_TYPE_BASE_SINK);
@@ -1867,18 +1889,18 @@ gst_pulsesink_class_init (GstPulseSinkClass * klass)
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /**
-   * GstPulseSink:client
+   * GstPulseSink:client-name
    *
    * The PulseAudio client name to use.
-   *
-   * Since: 0.10.25
    */
+  clientname = gst_pulse_client_name ();
   g_object_class_install_property (gobject_class,
-      PROP_CLIENT,
-      g_param_spec_string ("client", "Client",
-          "The PulseAudio client name to use", gst_pulse_client_name (),
+      PROP_CLIENT_NAME,
+      g_param_spec_string ("client-name", "Client Name",
+          "The PulseAudio client name to use", clientname,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
           GST_PARAM_MUTABLE_READY));
+  g_free (clientname);
 
   /**
    * GstPulseSink:stream-properties
@@ -1903,16 +1925,31 @@ gst_pulsesink_class_init (GstPulseSinkClass * klass)
           "list of pulseaudio stream properties",
           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  gst_element_class_set_details_simple (gstelement_class,
+  gst_element_class_set_static_metadata (gstelement_class,
       "PulseAudio Audio Sink",
       "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
   gst_element_class_add_pad_template (gstelement_class,
       gst_static_pad_template_get (&pad_template));
 }
 
-/* returns the current time of the sink ringbuffer */
+static void
+free_device_info (GstPulseDeviceInfo * device_info)
+{
+  GList *l;
+
+  g_free (device_info->description);
+
+  for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
+    pa_format_info_free ((pa_format_info *) l->data);
+
+  g_list_free (device_info->formats);
+}
+
+/* Returns the current time of the sink ringbuffer. The timing_info is updated
+ * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
+ */
 static GstClockTime
-gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
+gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
 {
   GstPulseSink *psink;
   GstPulseRingBuffer *pbuf;
@@ -1924,13 +1961,11 @@ gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
   pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
   psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
 
-#ifdef HAVE_PULSE_1_0
   if (g_atomic_int_get (&psink->format_lost)) {
     /* Stream was lost in a format change, it'll get set up again once
      * upstream renegotiates */
     return psink->format_lost_time;
   }
-#endif
 
   pa_threaded_mainloop_lock (mainloop);
   if (gst_pulsering_is_dead (psink, pbuf, TRUE))
@@ -1964,69 +1999,305 @@ static void
 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
     void *userdata)
 {
-  GstPulseRingBuffer *pbuf;
-  GstPulseSink *psink;
-#ifdef HAVE_PULSE_1_0
-  GList *l;
+  GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
   guint8 j;
-#endif
-
-  pbuf = GST_PULSERING_BUFFER_CAST (userdata);
-  psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
 
   if (!i)
     goto done;
 
-  g_free (psink->device_description);
-  psink->device_description = g_strdup (i->description);
-
-#ifdef HAVE_PULSE_1_0
-  g_mutex_lock (psink->sink_formats_lock);
-
-  for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
-    pa_format_info_free ((pa_format_info *) l->data);
-
-  g_list_free (psink->sink_formats);
-  psink->sink_formats = NULL;
+  device_info->description = g_strdup (i->description);
 
+  device_info->formats = NULL;
   for (j = 0; j < i->n_formats; j++)
-    psink->sink_formats = g_list_prepend (psink->sink_formats,
+    device_info->formats = g_list_prepend (device_info->formats,
         pa_format_info_copy (i->formats[j]));
 
-  g_mutex_unlock (psink->sink_formats_lock);
-#endif
-
 done:
   pa_threaded_mainloop_signal (mainloop, 0);
 }
 
-#ifdef HAVE_PULSE_1_0
-/* NOTE: If you're making changes here, see if pulseaudiosink acceptcaps also
- * needs to be changed accordingly. */
 static gboolean
-gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
+gst_pulse_format_info_int_prop_to_value (pa_format_info * format,
+    const char *key, GValue * value)
+{
+  GValue v = { 0, };
+  int i;
+  int *a, n;
+  int min, max;
+
+  if (pa_format_info_get_prop_int (format, key, &i) == 0) {
+    g_value_init (value, G_TYPE_INT);
+    g_value_set_int (value, i);
+
+  } else if (pa_format_info_get_prop_int_array (format, key, &a, &n) == 0) {
+    g_value_init (value, GST_TYPE_LIST);
+    g_value_init (&v, G_TYPE_INT);
+
+    for (i = 0; i < n; i++) {
+      g_value_set_int (&v, a[i]);
+      gst_value_list_append_value (value, &v);
+    }
+
+    pa_xfree (a);
+
+  } else if (pa_format_info_get_prop_int_range (format, key, &min, &max) == 0) {
+    g_value_init (value, GST_TYPE_INT_RANGE);
+    gst_value_set_int_range (value, min, max);
+
+  } else {
+    /* Property not available or is not an int type */
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+static GstCaps *
+gst_pulse_format_info_to_caps (pa_format_info * format)
+{
+  GstCaps *ret = NULL;
+  GValue v = { 0, };
+  pa_sample_spec ss;
+
+  switch (format->encoding) {
+    case PA_ENCODING_PCM:{
+      char *tmp = NULL;
+
+      pa_format_info_to_sample_spec (format, &ss, NULL);
+
+      if (pa_format_info_get_prop_string (format,
+              PA_PROP_FORMAT_SAMPLE_FORMAT, &tmp)) {
+        /* No specific sample format means any sample format */
+        ret = gst_caps_from_string (_PULSE_SINK_CAPS_PCM);
+        goto out;
+
+      } else if (ss.format == PA_SAMPLE_ALAW) {
+        ret = gst_caps_from_string (_PULSE_SINK_CAPS_ALAW);
+
+      } else if (ss.format == PA_SAMPLE_ULAW) {
+        ret = gst_caps_from_string (_PULSE_SINK_CAPS_MP3);
+
+      } else {
+        /* Linear PCM format */
+        const char *sformat =
+            gst_pulse_sample_format_to_caps_format (ss.format);
+
+        ret = gst_caps_from_string (_PULSE_SINK_CAPS_LINEAR);
+
+        if (sformat)
+          gst_caps_set_simple (ret, "format", G_TYPE_STRING, NULL);
+      }
+
+      pa_xfree (tmp);
+      break;
+    }
+
+    case PA_ENCODING_AC3_IEC61937:
+      ret = gst_caps_from_string (_PULSE_SINK_CAPS_AC3);
+      break;
+
+    case PA_ENCODING_EAC3_IEC61937:
+      ret = gst_caps_from_string (_PULSE_SINK_CAPS_EAC3);
+      break;
+
+    case PA_ENCODING_DTS_IEC61937:
+      ret = gst_caps_from_string (_PULSE_SINK_CAPS_DTS);
+      break;
+
+    case PA_ENCODING_MPEG_IEC61937:
+      ret = gst_caps_from_string (_PULSE_SINK_CAPS_MP3);
+      break;
+
+    default:
+      GST_WARNING ("Found a PA format that we don't support yet");
+      goto out;
+  }
+
+  if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_RATE, &v))
+    gst_caps_set_value (ret, "rate", &v);
+
+  g_value_unset (&v);
+
+  if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_CHANNELS,
+          &v))
+    gst_caps_set_value (ret, "channels", &v);
+
+out:
+  return ret;
+}
+
+/* Call with mainloop lock held */
+static pa_stream *
+gst_pulsesink_create_probe_stream (GstPulseSink * psink,
+    GstPulseRingBuffer * pbuf, pa_format_info * format)
+{
+  pa_format_info *formats[1] = { format };
+  pa_stream *stream;
+  pa_stream_flags_t flags;
+
+  GST_LOG_OBJECT (psink, "Creating probe stream");
+
+  if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
+              formats, 1, psink->proplist)))
+    goto error;
+
+  /* construct the flags */
+  flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
+      PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
+
+  pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
+
+  if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
+          NULL) < 0)
+    goto error;
+
+  if (!gst_pulsering_wait_for_stream_ready (psink, stream))
+    goto error;
+
+  return stream;
+
+error:
+  if (stream)
+    pa_stream_unref (stream);
+  return NULL;
+}
+
+static GstCaps *
+gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
+{
+  GstPulseRingBuffer *pbuf = NULL;
+  GstPulseDeviceInfo device_info = { NULL, NULL };
+  GstCaps *ret = NULL;
+  GList *i;
+  pa_operation *o = NULL;
+  pa_stream *stream;
+
+  GST_OBJECT_LOCK (psink);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
+  if (pbuf != NULL)
+    gst_object_ref (pbuf);
+  GST_OBJECT_UNLOCK (psink);
+
+  if (!pbuf) {
+    ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
+    goto out;
+  }
+
+  GST_OBJECT_LOCK (pbuf);
+  pa_threaded_mainloop_lock (mainloop);
+
+  if (!pbuf->context) {
+    ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
+    goto unlock;
+  }
+
+  if (pbuf->stream) {
+    /* We're in PAUSED or higher */
+    stream = pbuf->stream;
+
+  } else if (pbuf->probe_stream) {
+    /* We're not paused, but have a cached probe stream */
+    stream = pbuf->probe_stream;
+
+  } else {
+    /* We're not yet in PAUSED and still need to create a probe stream.
+     *
+     * FIXME: PA doesn't accept "any" format. We fix something reasonable since
+     * this is merely a probe. This should eventually be fixed in PA and
+     * hard-coding the format should be dropped. */
+    pa_format_info *format = pa_format_info_new ();
+    format->encoding = PA_ENCODING_PCM;
+    pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
+    pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
+    pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
+
+    pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
+        format);
+    if (!pbuf->probe_stream) {
+      GST_WARNING_OBJECT (psink, "Could not create probe stream");
+      goto unlock;
+    }
+
+    pa_format_info_free (format);
+
+    stream = pbuf->probe_stream;
+  }
+
+  ret = gst_caps_new_empty ();
+
+  if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
+              pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
+              &device_info)))
+    goto info_failed;
+
+  while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
+    pa_threaded_mainloop_wait (mainloop);
+    if (gst_pulsering_is_dead (psink, pbuf, FALSE))
+      goto unlock;
+  }
+
+  for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
+    gst_caps_append (ret,
+        gst_pulse_format_info_to_caps ((pa_format_info *) i->data));
+  }
+
+  if (filter) {
+    GstCaps *tmp = gst_caps_intersect_full (filter, ret,
+        GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (ret);
+    ret = tmp;
+  }
+
+unlock:
+  pa_threaded_mainloop_unlock (mainloop);
+  /* FIXME: this could be freed after device_name is got */
+  GST_OBJECT_UNLOCK (pbuf);
+
+out:
+  free_device_info (&device_info);
+
+  if (o)
+    pa_operation_unref (o);
+
+  if (pbuf)
+    gst_object_unref (pbuf);
+
+  GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
+
+  return ret;
+
+info_failed:
+  {
+    GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
+        ("pa_context_get_sink_input_info() failed: %s",
+            pa_strerror (pa_context_errno (pbuf->context))), (NULL));
+    goto unlock;
+  }
+}
+
+static gboolean
+gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
 {
-  GstPulseSink *psink = GST_PULSESINK (gst_pad_get_parent_element (pad));
-  GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK
-      (psink)->ringbuffer);
+  GstPulseRingBuffer *pbuf = NULL;
+  GstPulseDeviceInfo device_info = { NULL, NULL };
   GstCaps *pad_caps;
   GstStructure *st;
   gboolean ret = FALSE;
 
-  GstRingBufferSpec spec = { 0 };
-  pa_stream *stream = NULL;
+  GstAudioRingBufferSpec spec = { 0 };
   pa_operation *o = NULL;
   pa_channel_map channel_map;
-  pa_stream_flags_t flags;
-  pa_format_info *format = NULL, *formats[1];
+  pa_format_info *format = NULL;
   guint channels;
 
-  pad_caps = gst_pad_get_caps (pad, caps);
-  ret = pad_caps != NULL;
+  pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
+  ret = gst_caps_is_subset (caps, pad_caps);
   gst_caps_unref (pad_caps);
 
-  /* Either template caps didn't match, or we're still in NULL state */
-  if (!ret || !pbuf->context)
+  GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
+
+  /* Template caps didn't match */
+  if (!ret)
     goto done;
 
   /* If we've not got fixed caps, creating a stream might fail, so let's just
@@ -2034,12 +2305,26 @@ gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
   if (!gst_caps_is_fixed (caps))
     goto done;
 
-  ret = FALSE;
+  GST_OBJECT_LOCK (psink);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
+  if (pbuf != NULL)
+    gst_object_ref (pbuf);
+  GST_OBJECT_UNLOCK (psink);
+
+  /* We're still in NULL state */
+  if (pbuf == NULL)
+    goto done;
 
+  GST_OBJECT_LOCK (pbuf);
   pa_threaded_mainloop_lock (mainloop);
 
-  spec.latency_time = GST_BASE_AUDIO_SINK (psink)->latency_time;
-  if (!gst_ring_buffer_parse_caps (&spec, caps))
+  if (pbuf->context == NULL)
+    goto out;
+
+  ret = FALSE;
+
+  spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
+  if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
     goto out;
 
   if (!gst_pulse_fill_format_info (&spec, &format, &channels))
@@ -2061,49 +2346,36 @@ gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
       gst_pulse_gst_to_channel_map (&channel_map, &spec))
     pa_format_info_set_channel_map (format, &channel_map);
 
-  if (pbuf->stream) {
+  if (pbuf->stream || pbuf->probe_stream) {
     /* We're already in PAUSED or above, so just reuse this stream to query
      * sink formats and use those. */
     GList *i;
+    const char *device_name = pa_stream_get_device_name (pbuf->stream ?
+        pbuf->stream : pbuf->probe_stream);
 
-    if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
-                gst_pulsesink_sink_info_cb, pbuf)))
+    if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
+                gst_pulsesink_sink_info_cb, &device_info)))
       goto info_failed;
 
     while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
       pa_threaded_mainloop_wait (mainloop);
-      if (gst_pulsering_is_dead (psink, pbuf, TRUE))
+      if (gst_pulsering_is_dead (psink, pbuf, FALSE))
         goto out;
     }
 
-    g_mutex_lock (psink->sink_formats_lock);
-    for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
+    for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
       if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
         ret = TRUE;
         break;
       }
     }
-    g_mutex_unlock (psink->sink_formats_lock);
   } else {
     /* We're in READY, let's connect a stream to see if the format is
-     * accpeted by whatever sink we're routed to */
-    formats[0] = format;
-
-    if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
-                formats, 1, psink->proplist)))
-      goto out;
-
-    /* construct the flags */
-    flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
-        PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
-
-    pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
-
-    if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
-            NULL) < 0)
-      goto out;
-
-    ret = gst_pulsering_wait_for_stream_ready (psink, stream);
+     * accepted by whatever sink we're routed to */
+    pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
+        format);
+    if (pbuf->probe_stream)
+      ret = TRUE;
   }
 
 out:
@@ -2113,16 +2385,14 @@ out:
   if (o)
     pa_operation_unref (o);
 
-  if (stream) {
-    pa_stream_set_state_callback (stream, NULL, NULL);
-    pa_stream_disconnect (stream);
-    pa_stream_unref (stream);
-  }
-
   pa_threaded_mainloop_unlock (mainloop);
+  GST_OBJECT_UNLOCK (pbuf);
+
+  gst_caps_replace (&spec.caps, NULL);
+  gst_object_unref (pbuf);
 
 done:
-  gst_object_unref (psink);
+
   return ret;
 
 info_failed:
@@ -2133,20 +2403,16 @@ info_failed:
     goto out;
   }
 }
-#endif
 
 static void
 gst_pulsesink_init (GstPulseSink * pulsesink)
 {
   pulsesink->server = NULL;
   pulsesink->device = NULL;
-  pulsesink->device_description = NULL;
+  pulsesink->device_info.description = NULL;
   pulsesink->client_name = gst_pulse_client_name ();
 
-#ifdef HAVE_PULSE_1_0
-  pulsesink->sink_formats_lock = g_mutex_new ();
-  pulsesink->sink_formats = NULL;
-#endif
+  pulsesink->device_info.formats = NULL;
 
   pulsesink->volume = DEFAULT_VOLUME;
   pulsesink->volume_set = FALSE;
@@ -2156,26 +2422,19 @@ gst_pulsesink_init (GstPulseSink * pulsesink)
 
   pulsesink->notify = 0;
 
-#ifdef HAVE_PULSE_1_0
   g_atomic_int_set (&pulsesink->format_lost, FALSE);
   pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
-#endif
 
   pulsesink->properties = NULL;
   pulsesink->proplist = NULL;
 
   /* override with a custom clock */
-  if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
-    gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
+  if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
+    gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
 
-  GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
+  GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
       gst_audio_clock_new ("GstPulseSinkClock",
-      (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
-
-#ifdef HAVE_PULSE_1_0
-  gst_pad_set_acceptcaps_function (GST_BASE_SINK (pulsesink)->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_pulsesink_pad_acceptcaps));
-#endif
+      (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
 
   /* TRUE for sinks, FALSE for sources */
   pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
@@ -2187,22 +2446,12 @@ static void
 gst_pulsesink_finalize (GObject * object)
 {
   GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
-#ifdef HAVE_PULSE_1_0
-  GList *i;
-#endif
 
   g_free (pulsesink->server);
   g_free (pulsesink->device);
-  g_free (pulsesink->device_description);
   g_free (pulsesink->client_name);
 
-#ifdef HAVE_PULSE_1_0
-  for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
-    pa_format_info_free ((pa_format_info *) i->data);
-
-  g_list_free (pulsesink->sink_formats);
-  g_mutex_free (pulsesink->sink_formats_lock);
-#endif
+  free_device_info (&pulsesink->device_info);
 
   if (pulsesink->properties)
     gst_structure_free (pulsesink->properties);
@@ -2232,23 +2481,19 @@ gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
 
   GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
 
   if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
     goto no_index;
 
-#ifdef HAVE_PULSE_1_0
   if (pbuf->is_pcm)
     gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
   else
     /* FIXME: this will eventually be superceded by checks to see if the volume
      * is readable/writable */
     goto unlock;
-#else
-  gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
-#endif
 
   if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
               &v, NULL, NULL)))
@@ -2309,7 +2554,7 @@ gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
 
   GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
 
@@ -2402,7 +2647,7 @@ gst_pulsesink_get_volume (GstPulseSink * psink)
 
   pa_threaded_mainloop_lock (mainloop);
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
 
@@ -2474,7 +2719,7 @@ gst_pulsesink_get_mute (GstPulseSink * psink)
   pa_threaded_mainloop_lock (mainloop);
   mute = psink->mute;
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
 
@@ -2536,12 +2781,13 @@ gst_pulsesink_device_description (GstPulseSink * psink)
     goto no_mainloop;
 
   pa_threaded_mainloop_lock (mainloop);
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL)
     goto no_buffer;
 
+  free_device_info (&psink->device_info);
   if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
-              psink->device, gst_pulsesink_sink_info_cb, pbuf)))
+              psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
     goto info_failed;
 
   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
@@ -2554,7 +2800,7 @@ unlock:
   if (o)
     pa_operation_unref (o);
 
-  t = g_strdup (psink->device_description);
+  t = g_strdup (psink->device_info.description);
   pa_threaded_mainloop_unlock (mainloop);
 
   return t;
@@ -2602,7 +2848,7 @@ gst_pulsesink_set_property (GObject * object,
     case PROP_MUTE:
       gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
       break;
-    case PROP_CLIENT:
+    case PROP_CLIENT_NAME:
       g_free (pulsesink->client_name);
       if (!g_value_get_string (value)) {
         GST_WARNING_OBJECT (pulsesink,
@@ -2649,7 +2895,7 @@ gst_pulsesink_get_property (GObject * object,
     case PROP_MUTE:
       g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
       break;
-    case PROP_CLIENT:
+    case PROP_CLIENT_NAME:
       g_value_set_string (value, pulsesink->client_name);
       break;
     case PROP_STREAM_PROPERTIES:
@@ -2669,7 +2915,7 @@ gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
 
   pa_threaded_mainloop_lock (mainloop);
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
 
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
@@ -2744,15 +2990,16 @@ gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
     goto finish;
 
   pa_threaded_mainloop_lock (mainloop);
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
 
+  /* We're not interested if this operation failed or not */
   if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
-              pl, NULL, NULL)))
-    goto update_failed;
+              pl, NULL, NULL))) {
+    GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
+  }
 
-  /* We're not interested if this operation failed or not */
 unlock:
 
   if (o)
@@ -2773,13 +3020,6 @@ no_buffer:
     GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
     goto unlock;
   }
-update_failed:
-  {
-    GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
-        ("pa_stream_proplist_update() failed: %s",
-            pa_strerror (pa_context_errno (pbuf->context))), (NULL));
-    goto unlock;
-  }
 }
 
 static void
@@ -2789,7 +3029,7 @@ gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
 
   pa_threaded_mainloop_lock (mainloop);
 
-  pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
+  pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
 
   if (pbuf == NULL || pbuf->stream == NULL)
     goto no_buffer;
@@ -2860,6 +3100,14 @@ gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
 
       break;
     }
+    case GST_EVENT_GAP:{
+      GstClockTime timestamp, duration;
+
+      gst_event_parse_gap (event, &timestamp, &duration);
+      if (duration == GST_CLOCK_TIME_NONE)
+        gst_pulsesink_flush_ringbuffer (pulsesink);
+      break;
+    }
     case GST_EVENT_EOS:
       gst_pulsesink_flush_ringbuffer (pulsesink);
       break;
@@ -2870,6 +3118,45 @@ gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
   return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
 }
 
+static gboolean
+gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
+{
+  GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
+  gboolean ret;
+
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_CAPS:
+    {
+      GstCaps *caps, *filter;
+
+      gst_query_parse_caps (query, &filter);
+      caps = gst_pulsesink_query_getcaps (pulsesink, filter);
+
+      if (caps) {
+        gst_query_set_caps_result (query, caps);
+        gst_caps_unref (caps);
+        return TRUE;
+      } else {
+        return FALSE;
+      }
+    }
+    case GST_QUERY_ACCEPT_CAPS:
+    {
+      GstCaps *caps;
+
+      gst_query_parse_accept_caps (query, &caps);
+      ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
+      gst_query_set_accept_caps_result (query, ret);
+      ret = TRUE;
+      break;
+    }
+    default:
+      ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
+      break;
+  }
+  return ret;
+}
+
 static void
 gst_pulsesink_release_mainloop (GstPulseSink * psink)
 {
@@ -2883,7 +3170,7 @@ gst_pulsesink_release_mainloop (GstPulseSink * psink)
   }
   pa_threaded_mainloop_unlock (mainloop);
 
-  g_mutex_lock (pa_shared_resource_mutex);
+  g_mutex_lock (&pa_shared_resource_mutex);
   mainloop_ref_ct--;
   if (!mainloop_ref_ct) {
     GST_INFO_OBJECT (psink, "terminating pa main loop thread");
@@ -2891,7 +3178,7 @@ gst_pulsesink_release_mainloop (GstPulseSink * psink)
     pa_threaded_mainloop_free (mainloop);
     mainloop = NULL;
   }
-  g_mutex_unlock (pa_shared_resource_mutex);
+  g_mutex_unlock (&pa_shared_resource_mutex);
 }
 
 static GstStateChangeReturn
@@ -2902,24 +3189,27 @@ gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
 
   switch (transition) {
     case GST_STATE_CHANGE_NULL_TO_READY:
-      g_mutex_lock (pa_shared_resource_mutex);
+      g_mutex_lock (&pa_shared_resource_mutex);
       if (!mainloop_ref_ct) {
         GST_INFO_OBJECT (element, "new pa main loop thread");
         if (!(mainloop = pa_threaded_mainloop_new ()))
           goto mainloop_failed;
+        if (pa_threaded_mainloop_start (mainloop) < 0) {
+          pa_threaded_mainloop_free (mainloop);
+          goto mainloop_start_failed;
+        }
         mainloop_ref_ct = 1;
-        pa_threaded_mainloop_start (mainloop);
-        g_mutex_unlock (pa_shared_resource_mutex);
+        g_mutex_unlock (&pa_shared_resource_mutex);
       } else {
         GST_INFO_OBJECT (element, "reusing pa main loop thread");
         mainloop_ref_ct++;
-        g_mutex_unlock (pa_shared_resource_mutex);
+        g_mutex_unlock (&pa_shared_resource_mutex);
       }
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
       gst_element_post_message (element,
           gst_message_new_clock_provide (GST_OBJECT_CAST (element),
-              GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
+              GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
       break;
 
     default:
@@ -2932,10 +3222,10 @@ gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
 
   switch (transition) {
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      /* format_lost is reset in release() in baseaudiosink */
+      /* format_lost is reset in release() in audiobasesink */
       gst_element_post_message (element,
           gst_message_new_clock_lost (GST_OBJECT_CAST (element),
-              GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
+              GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
       break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       gst_pulsesink_release_mainloop (pulsesink);
@@ -2949,15 +3239,22 @@ gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
   /* ERRORS */
 mainloop_failed:
   {
-    g_mutex_unlock (pa_shared_resource_mutex);
+    g_mutex_unlock (&pa_shared_resource_mutex);
     GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
         ("pa_threaded_mainloop_new() failed"), (NULL));
     return GST_STATE_CHANGE_FAILURE;
   }
+mainloop_start_failed:
+  {
+    g_mutex_unlock (&pa_shared_resource_mutex);
+    GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
+        ("pa_threaded_mainloop_start() failed"), (NULL));
+    return GST_STATE_CHANGE_FAILURE;
+  }
 state_failure:
   {
     if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
-      /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
+      /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
       g_assert (mainloop);
       gst_pulsesink_release_mainloop (pulsesink);
     }