Revert "pulsesink: Make 2.0 dependency optional"
[platform/upstream/gstreamer.git] / ext / pulse / pulsesink.c
index 7500196..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,7 +125,7 @@ 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
@@ -138,14 +139,11 @@ struct _GstPulseRingBuffer
 
   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;
@@ -186,8 +184,7 @@ G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
 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);
 }
@@ -229,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;
@@ -249,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) {
@@ -265,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);
 
@@ -293,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);
 
@@ -334,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
@@ -416,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;
@@ -424,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))) {
@@ -437,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
@@ -480,7 +495,7 @@ gst_pulseringbuffer_open_device (GstAudioRingBuffer * 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);
@@ -517,7 +532,7 @@ gst_pulseringbuffer_open_device (GstAudioRingBuffer * 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 */
@@ -541,6 +556,11 @@ gst_pulseringbuffer_open_device (GstAudioRingBuffer * 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);
@@ -551,7 +571,7 @@ gst_pulseringbuffer_open_device (GstAudioRingBuffer * 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;
@@ -569,6 +589,12 @@ 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 */
@@ -667,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;
 
@@ -674,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,
@@ -738,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;
 
@@ -755,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);
   }
@@ -804,33 +857,24 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
   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);
 
@@ -838,6 +882,13 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
   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,
@@ -847,12 +898,8 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
   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)
@@ -861,17 +908,10 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
     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,
@@ -904,33 +944,30 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
   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;
@@ -949,7 +986,6 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
   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));
 
@@ -958,7 +994,6 @@ gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
       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.  */
@@ -1032,7 +1067,6 @@ gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
   gst_pulsering_destroy_stream (pbuf);
   pa_threaded_mainloop_unlock (mainloop);
 
-#ifdef HAVE_PULSE_1_0
   {
     GstPulseSink *psink;
 
@@ -1040,7 +1074,6 @@ gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
     g_atomic_int_set (&psink->format_lost, FALSE);
     psink->format_lost_time = GST_CLOCK_TIME_NONE;
   }
-#endif
 
   return TRUE;
 }
@@ -1063,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) {
@@ -1136,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);
 
@@ -1215,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);
@@ -1251,13 +1283,11 @@ gst_pulseringbuffer_stop (GstAudioRingBuffer * 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");
@@ -1428,12 +1458,10 @@ gst_pulseringbuffer_commit (GstAudioRingBuffer * 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;
@@ -1482,12 +1510,10 @@ gst_pulseringbuffer_commit (GstAudioRingBuffer * 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;
@@ -1538,13 +1564,11 @@ gst_pulseringbuffer_commit (GstAudioRingBuffer * 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 */
@@ -1626,9 +1650,7 @@ gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
     }
   }
 
-#ifdef HAVE_PULSE_1_0
 fake_done:
-#endif
   /* we consumed all samples here */
   data = data_end + bpf;
 
@@ -1748,13 +1770,9 @@ 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_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)
     );
 
@@ -1774,16 +1792,15 @@ static GstBuffer *
 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)
@@ -1791,14 +1808,14 @@ gst_pulsesink_payload (GstAudioBaseSink * 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);
@@ -1872,16 +1889,14 @@ 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",
+      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));
@@ -1910,14 +1925,29 @@ 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, GstAudioBaseSink * sink)
 {
@@ -1931,13 +1961,11 @@ gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * 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))
@@ -1971,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_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)
 {
-  GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK
-      (psink)->ringbuffer);
-  GstPad *pad = GST_BASE_SINK_PAD (psink);
+  GstPulseRingBuffer *pbuf = NULL;
+  GstPulseDeviceInfo device_info = { NULL, NULL };
   GstCaps *pad_caps;
   GstStructure *st;
   gboolean ret = FALSE;
 
   GstAudioRingBufferSpec spec = { 0 };
-  pa_stream *stream = NULL;
   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
@@ -2041,10 +2305,24 @@ gst_pulsesink_query_acceptcaps (GstPulseSink * psink, 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);
 
+  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;
@@ -2068,49 +2346,36 @@ gst_pulsesink_query_acceptcaps (GstPulseSink * psink, 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:
@@ -2120,15 +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:
+
   return ret;
 
 info_failed:
@@ -2139,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;
@@ -2162,10 +2422,8 @@ 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;
@@ -2188,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);
@@ -2240,16 +2488,12 @@ gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
   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)))
@@ -2541,8 +2785,9 @@ gst_pulsesink_device_description (GstPulseSink * psink)
   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) {
@@ -2555,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;
@@ -2603,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,
@@ -2650,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:
@@ -2749,11 +2994,12 @@ gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
   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)
@@ -2774,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
@@ -2861,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;
@@ -2874,11 +3121,25 @@ gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
 static gboolean
 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
 {
-#ifdef HAVE_PULSE_1_0
   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;
@@ -2894,9 +3155,6 @@ gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
       break;
   }
   return ret;
-#else
-  return GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
-#endif
 }
 
 static void
@@ -2912,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");
@@ -2920,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
@@ -2931,18 +3189,21 @@ 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:
@@ -2978,11 +3239,18 @@ 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) {