update for ringbuffer change
[platform/upstream/gstreamer.git] / ext / pulse / pulsesrc.c
index bd05ffa..7a0b7e4 100644 (file)
@@ -30,7 +30,7 @@
  * <title>Example pipelines</title>
  * |[
  * gst-launch -v pulsesrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
- * ]| Record from a sound card using ALSA and encode to Ogg/Vorbis.
+ * ]| Record from a sound card using pulseaudio and encode to Ogg/Vorbis.
  * </refsect2>
  */
 
 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
 #define GST_CAT_DEFAULT pulse_debug
 
+#define DEFAULT_SERVER            NULL
+#define DEFAULT_DEVICE            NULL
+#define DEFAULT_DEVICE_NAME       NULL
+
 enum
 {
-  PROP_SERVER = 1,
+  PROP_0,
+  PROP_SERVER,
   PROP_DEVICE,
-  PROP_DEVICE_NAME
+  PROP_DEVICE_NAME,
+  PROP_CLIENT,
+  PROP_STREAM_PROPERTIES,
+  PROP_SOURCE_OUTPUT_INDEX,
+  PROP_LAST
 };
 
 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
-
 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
 
 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
@@ -68,12 +76,14 @@ static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
 static void gst_pulsesrc_finalize (GObject * object);
 
+static gboolean gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked,
+    gboolean wait);
 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
 
 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
 
 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
-    GstRingBufferSpec * spec);
+    GstAudioRingBufferSpec * spec);
 
 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
 
@@ -88,114 +98,35 @@ static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
     element, GstStateChange transition);
 
-static void gst_pulsesrc_init_interfaces (GType type);
-
 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
-# define ENDIANNESS   "LITTLE_ENDIAN, BIG_ENDIAN"
+# define FORMATS "{ S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, U8 }"
 #else
-# define ENDIANNESS   "BIG_ENDIAN, LITTLE_ENDIAN"
+# define FORMATS "{ S16BE, S16LE, F32BE, F32LE, S32BE, S32LE, U8 }"
 #endif
 
-GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
-GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
-GST_BOILERPLATE_FULL (GstPulseSrc, gst_pulsesrc, GstAudioSrc,
-    GST_TYPE_AUDIO_SRC, gst_pulsesrc_init_interfaces);
-
-static gboolean
-gst_pulsesrc_interface_supported (GstImplementsInterface *
-    iface, GType interface_type)
-{
-  GstPulseSrc *this = GST_PULSESRC (iface);
-
-  if (interface_type == GST_TYPE_MIXER && this->mixer)
-    return TRUE;
-
-  if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
-    return TRUE;
-
-  return FALSE;
-}
+static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/x-raw, "
+        "format = (string) " FORMATS ", "
+        "rate = (int) [ 1, MAX ], "
+        "channels = (int) [ 1, 32 ];"
+        "audio/x-alaw, "
+        "rate = (int) [ 1, MAX], "
+        "channels = (int) [ 1, 32 ];"
+        "audio/x-mulaw, "
+        "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
+    );
 
-static void
-gst_pulsesrc_implements_interface_init (GstImplementsInterfaceClass * klass)
-{
-  klass->supported = gst_pulsesrc_interface_supported;
-}
-
-static void
-gst_pulsesrc_init_interfaces (GType type)
-{
-  static const GInterfaceInfo implements_iface_info = {
-    (GInterfaceInitFunc) gst_pulsesrc_implements_interface_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo mixer_iface_info = {
-    (GInterfaceInitFunc) gst_pulsesrc_mixer_interface_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo probe_iface_info = {
-    (GInterfaceInitFunc) gst_pulsesrc_property_probe_interface_init,
-    NULL,
-    NULL,
-  };
-
-  g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
-      &implements_iface_info);
-  g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
-  g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
-      &probe_iface_info);
-}
 
-static void
-gst_pulsesrc_base_init (gpointer g_class)
-{
+GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
+GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
 
-  static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
-      GST_PAD_SRC,
-      GST_PAD_ALWAYS,
-      GST_STATIC_CAPS ("audio/x-raw-int, "
-          "endianness = (int) { " ENDIANNESS " }, "
-          "signed = (boolean) TRUE, "
-          "width = (int) 16, "
-          "depth = (int) 16, "
-          "rate = (int) [ 1, MAX ], "
-          "channels = (int) [ 1, 32 ];"
-          "audio/x-raw-float, "
-          "endianness = (int) { " ENDIANNESS " }, "
-          "width = (int) 32, "
-          "rate = (int) [ 1, MAX ], "
-          "channels = (int) [ 1, 32 ];"
-          "audio/x-raw-int, "
-          "endianness = (int) { " ENDIANNESS " }, "
-          "signed = (boolean) TRUE, "
-          "width = (int) 32, "
-          "depth = (int) 32, "
-          "rate = (int) [ 1, MAX ], "
-          "channels = (int) [ 1, 32 ];"
-          "audio/x-raw-int, "
-          "signed = (boolean) FALSE, "
-          "width = (int) 8, "
-          "depth = (int) 8, "
-          "rate = (int) [ 1, MAX ], "
-          "channels = (int) [ 1, 32 ];"
-          "audio/x-alaw, "
-          "rate = (int) [ 1, MAX], "
-          "channels = (int) [ 1, 32 ];"
-          "audio/x-mulaw, "
-          "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
-      );
-
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
-  gst_element_class_set_details_simple (element_class,
-      "PulseAudio Audio Source",
-      "Source/Audio",
-      "Captures audio from a PulseAudio server", "Lennart Poettering");
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&pad_template));
-}
+#define gst_pulsesrc_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
+    G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_pulsesrc_mixer_interface_init);
+    G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
+        gst_pulsesrc_property_probe_interface_init));
 
 static void
 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
@@ -204,10 +135,11 @@ gst_pulsesrc_class_init (GstPulseSrcClass * klass)
   GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+  gchar *clientname;
 
-  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pulsesrc_finalize);
-  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pulsesrc_set_property);
-  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pulsesrc_get_property);
+  gobject_class->finalize = gst_pulsesrc_finalize;
+  gobject_class->set_property = gst_pulsesrc_set_property;
+  gobject_class->get_property = gst_pulsesrc_get_property;
 
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
@@ -226,54 +158,112 @@ gst_pulsesrc_class_init (GstPulseSrcClass * klass)
   g_object_class_install_property (gobject_class,
       PROP_SERVER,
       g_param_spec_string ("server", "Server",
-          "The PulseAudio server to connect to", NULL,
+          "The PulseAudio server to connect to", DEFAULT_SERVER,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_property (gobject_class, PROP_DEVICE,
-      g_param_spec_string ("device", "Source",
-          "The PulseAudio source device to connect to", NULL,
+      g_param_spec_string ("device", "Device",
+          "The PulseAudio source device to connect to", DEFAULT_DEVICE,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class,
       PROP_DEVICE_NAME,
       g_param_spec_string ("device-name", "Device name",
-          "Human-readable name of the sound device", NULL,
+          "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
+          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  clientname = gst_pulse_client_name ();
+  /**
+   * GstPulseSink:client
+   *
+   * The PulseAudio client name to use.
+   *
+   * Since: 0.10.27
+   */
+  g_object_class_install_property (gobject_class,
+      PROP_CLIENT,
+      g_param_spec_string ("client", "Client",
+          "The PulseAudio client_name_to_use", clientname,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
+          GST_PARAM_MUTABLE_READY));
+  g_free (clientname);
+
+  /**
+   * GstPulseSrc:stream-properties
+   *
+   * List of pulseaudio stream properties. A list of defined properties can be
+   * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
+   *
+   * Below is an example for registering as a music application to pulseaudio.
+   * |[
+   * GstStructure *props;
+   *
+   * props = gst_structure_from_string ("props,media.role=music", NULL);
+   * g_object_set (pulse, "stream-properties", props, NULL);
+   * gst_structure_free (props);
+   * ]|
+   *
+   * Since: 0.10.26
+   */
+  g_object_class_install_property (gobject_class,
+      PROP_STREAM_PROPERTIES,
+      g_param_spec_boxed ("stream-properties", "stream properties",
+          "list of pulseaudio stream properties",
+          GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  /**
+   * GstPulseSrc:source-output-index
+   *
+   * The index of the PulseAudio source output corresponding to this element.
+   *
+   * Since: 0.10.31
+   */
+  g_object_class_install_property (gobject_class,
+      PROP_SOURCE_OUTPUT_INDEX,
+      g_param_spec_uint ("source-output-index", "source output index",
+          "The index of the PulseAudio source output corresponding to this "
+          "record stream", 0, G_MAXUINT, PA_INVALID_INDEX,
           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  gst_element_class_set_details_simple (gstelement_class,
+      "PulseAudio Audio Source",
+      "Source/Audio",
+      "Captures audio from a PulseAudio server", "Lennart Poettering");
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&pad_template));
 }
 
 static void
-gst_pulsesrc_init (GstPulseSrc * pulsesrc, GstPulseSrcClass * klass)
+gst_pulsesrc_init (GstPulseSrc * pulsesrc)
 {
-  int e;
-
-  pulsesrc->server = pulsesrc->device = pulsesrc->device_description = NULL;
+  pulsesrc->server = NULL;
+  pulsesrc->device = NULL;
+  pulsesrc->client_name = gst_pulse_client_name ();
+  pulsesrc->device_description = NULL;
 
   pulsesrc->context = NULL;
   pulsesrc->stream = NULL;
+  pulsesrc->stream_connected = FALSE;
+  pulsesrc->source_output_idx = PA_INVALID_INDEX;
 
   pulsesrc->read_buffer = NULL;
   pulsesrc->read_buffer_length = 0;
 
-#if HAVE_PULSE_0_9_13
   pa_sample_spec_init (&pulsesrc->sample_spec);
-#else
-  pulsesrc->sample_spec.format = PA_SAMPLE_INVALID;
-  pulsesrc->sample_spec.rate = 0;
-  pulsesrc->sample_spec.channels = 0;
-#endif
 
   pulsesrc->operation_success = FALSE;
-  pulsesrc->did_reset = FALSE;
+  pulsesrc->paused = TRUE;
   pulsesrc->in_read = FALSE;
 
-  pulsesrc->mainloop = pa_threaded_mainloop_new ();
-  g_assert (pulsesrc->mainloop);
+  pulsesrc->mixer = NULL;
 
-  e = pa_threaded_mainloop_start (pulsesrc->mainloop);
-  g_assert (e == 0);
+  pulsesrc->properties = NULL;
+  pulsesrc->proplist = NULL;
 
-  pulsesrc->mixer = NULL;
+  pulsesrc->probe = gst_pulseprobe_new (G_OBJECT (pulsesrc), G_OBJECT_GET_CLASS (pulsesrc), PROP_DEVICE, pulsesrc->server, FALSE, TRUE);        /* FALSE for sinks, TRUE for sources */
 
-  pulsesrc->probe = gst_pulseprobe_new (G_OBJECT (pulsesrc), G_OBJECT_GET_CLASS (pulsesrc), PROP_DEVICE, pulsesrc->device, FALSE, TRUE);        /* FALSE for sinks, TRUE for sources */
+  /* this should be the default but it isn't yet */
+  gst_base_audio_src_set_slave_method (GST_BASE_AUDIO_SRC (pulsesrc),
+      GST_BASE_AUDIO_SRC_SLAVE_SKEW);
 }
 
 static void
@@ -283,6 +273,9 @@ gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
     pa_stream_disconnect (pulsesrc->stream);
     pa_stream_unref (pulsesrc->stream);
     pulsesrc->stream = NULL;
+    pulsesrc->stream_connected = FALSE;
+    pulsesrc->source_output_idx = PA_INVALID_INDEX;
+    g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
   }
 
   g_free (pulsesrc->device_description);
@@ -305,16 +298,16 @@ gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
 static void
 gst_pulsesrc_finalize (GObject * object)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (object);
-
-  pa_threaded_mainloop_stop (pulsesrc->mainloop);
-
-  gst_pulsesrc_destroy_context (pulsesrc);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
 
   g_free (pulsesrc->server);
   g_free (pulsesrc->device);
+  g_free (pulsesrc->client_name);
 
-  pa_threaded_mainloop_free (pulsesrc->mainloop);
+  if (pulsesrc->properties)
+    gst_structure_free (pulsesrc->properties);
+  if (pulsesrc->proplist)
+    pa_proplist_free (pulsesrc->proplist);
 
   if (pulsesrc->mixer) {
     gst_pulsemixer_ctrl_free (pulsesrc->mixer);
@@ -329,70 +322,44 @@ gst_pulsesrc_finalize (GObject * object)
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+#define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
+#define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
+
 static gboolean
-gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc)
+gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
 {
+  if (!CONTEXT_OK (pulsesrc->context))
+    goto error;
+
+  if (check_stream && !STREAM_OK (pulsesrc->stream))
+    goto error;
+
+  return FALSE;
 
-  if (!pulsesrc->context
-      || !PA_CONTEXT_IS_GOOD (pa_context_get_state (pulsesrc->context))
-      || !pulsesrc->stream
-      || !PA_STREAM_IS_GOOD (pa_stream_get_state (pulsesrc->stream))) {
+error:
+  {
     const gchar *err_str = pulsesrc->context ?
         pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
-
     GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
             err_str), (NULL));
     return TRUE;
   }
-
-  return FALSE;
-}
-
-
-static void
-gst_pulsesrc_set_property (GObject * object,
-    guint prop_id, const GValue * value, GParamSpec * pspec)
-{
-
-  GstPulseSrc *pulsesrc = GST_PULSESRC (object);
-
-  switch (prop_id) {
-    case PROP_SERVER:
-      g_free (pulsesrc->server);
-      pulsesrc->server = g_value_dup_string (value);
-
-      if (pulsesrc->probe)
-        gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
-
-      break;
-
-    case PROP_DEVICE:
-      g_free (pulsesrc->device);
-      pulsesrc->device = g_value_dup_string (value);
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
 }
 
 static void
 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
     void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
 
   if (!i)
-    return;
-
-  if (!pulsesrc->stream)
-    return;
-
-  g_assert (i->index == pa_stream_get_device_index (pulsesrc->stream));
+    goto done;
 
   g_free (pulsesrc->device_description);
   pulsesrc->device_description = g_strdup (i->description);
+
+done:
+  pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
 }
 
 static gchar *
@@ -401,14 +368,13 @@ gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
   pa_operation *o = NULL;
   gchar *t;
 
-  pa_threaded_mainloop_lock (pulsesrc->mainloop);
+  if (!pulsesrc->mainloop)
+    goto no_mainloop;
 
-  if (!pulsesrc->stream)
-    goto unlock;
+  pa_threaded_mainloop_lock (pulsesrc->mainloop);
 
-  if (!(o = pa_context_get_source_info_by_index (pulsesrc->context,
-              pa_stream_get_device_index (pulsesrc->stream),
-              gst_pulsesrc_source_info_cb, pulsesrc))) {
+  if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
+              pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
 
     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
         ("pa_stream_get_source_info() failed: %s",
@@ -418,7 +384,7 @@ gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
 
   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
 
-    if (gst_pulsesrc_is_dead (pulsesrc))
+    if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
       goto unlock;
 
     pa_threaded_mainloop_wait (pulsesrc->mainloop);
@@ -434,6 +400,54 @@ unlock:
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
   return t;
+
+no_mainloop:
+  {
+    GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
+    return NULL;
+  }
+}
+
+static void
+gst_pulsesrc_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec)
+{
+
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
+
+  switch (prop_id) {
+    case PROP_SERVER:
+      g_free (pulsesrc->server);
+      pulsesrc->server = g_value_dup_string (value);
+      if (pulsesrc->probe)
+        gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
+      break;
+    case PROP_DEVICE:
+      g_free (pulsesrc->device);
+      pulsesrc->device = g_value_dup_string (value);
+      break;
+    case PROP_CLIENT:
+      g_free (pulsesrc->client_name);
+      if (!g_value_get_string (value)) {
+        GST_WARNING_OBJECT (pulsesrc,
+            "Empty PulseAudio client name not allowed. Resetting to default value");
+        pulsesrc->client_name = gst_pulse_client_name ();
+      } else
+        pulsesrc->client_name = g_value_dup_string (value);
+      break;
+    case PROP_STREAM_PROPERTIES:
+      if (pulsesrc->properties)
+        gst_structure_free (pulsesrc->properties);
+      pulsesrc->properties =
+          gst_structure_copy (gst_value_get_structure (value));
+      if (pulsesrc->proplist)
+        pa_proplist_free (pulsesrc->proplist);
+      pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
 }
 
 static void
@@ -441,24 +455,27 @@ gst_pulsesrc_get_property (GObject * object,
     guint prop_id, GValue * value, GParamSpec * pspec)
 {
 
-  GstPulseSrc *pulsesrc = GST_PULSESRC (object);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
 
   switch (prop_id) {
     case PROP_SERVER:
       g_value_set_string (value, pulsesrc->server);
       break;
-
     case PROP_DEVICE:
       g_value_set_string (value, pulsesrc->device);
       break;
-
-    case PROP_DEVICE_NAME:{
-      char *t = gst_pulsesrc_device_description (pulsesrc);
-      g_value_set_string (value, t);
-      g_free (t);
+    case PROP_DEVICE_NAME:
+      g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
+      break;
+    case PROP_CLIENT:
+      g_value_set_string (value, pulsesrc->client_name);
+      break;
+    case PROP_STREAM_PROPERTIES:
+      gst_value_set_structure (value, pulsesrc->properties);
+      break;
+    case PROP_SOURCE_OUTPUT_INDEX:
+      g_value_set_uint (value, pulsesrc->source_output_idx);
       break;
-    }
-
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -468,7 +485,7 @@ gst_pulsesrc_get_property (GObject * object,
 static void
 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
 
   switch (pa_context_get_state (c)) {
     case PA_CONTEXT_READY:
@@ -488,7 +505,7 @@ gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
 static void
 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
 
   switch (pa_stream_get_state (s)) {
 
@@ -507,33 +524,66 @@ gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
 static void
 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
 
-  pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
+  GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
+
+  if (pulsesrc->in_read) {
+    /* only signal when reading */
+    pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
+  }
 }
 
 static void
 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  const pa_timing_info *info;
+  pa_usec_t source_usec;
 
-  pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
+  info = pa_stream_get_timing_info (s);
+
+  if (!info) {
+    GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
+        "latency update (information unknown)");
+    return;
+  }
+  source_usec = info->configured_source_usec;
+
+  GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
+      "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
+      G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
+      GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
+      info->write_index, info->read_index_corrupt, info->read_index,
+      info->source_usec, source_usec);
+}
+
+static void
+gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
+{
+  GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
+}
+
+static void
+gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
+{
+  GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
 }
 
 static gboolean
 gst_pulsesrc_open (GstAudioSrc * asrc)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
-  gchar *name = gst_pulse_client_name ();
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
 
   g_assert (!pulsesrc->context);
   g_assert (!pulsesrc->stream);
 
+  GST_DEBUG_OBJECT (pulsesrc, "opening device");
+
   if (!(pulsesrc->context =
           pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
-              name))) {
+              pulsesrc->client_name))) {
     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
         (NULL));
     goto unlock_and_fail;
@@ -542,6 +592,9 @@ gst_pulsesrc_open (GstAudioSrc * asrc)
   pa_context_set_state_callback (pulsesrc->context,
       gst_pulsesrc_context_state_cb, pulsesrc);
 
+  GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
+      GST_STR_NULL (pulsesrc->server));
+
   if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
@@ -565,26 +618,27 @@ gst_pulsesrc_open (GstAudioSrc * asrc)
     /* Wait until the context is ready */
     pa_threaded_mainloop_wait (pulsesrc->mainloop);
   }
+  GST_DEBUG_OBJECT (pulsesrc, "connected");
 
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
-  g_free (name);
   return TRUE;
 
+  /* ERRORS */
 unlock_and_fail:
+  {
+    gst_pulsesrc_destroy_context (pulsesrc);
 
-  gst_pulsesrc_destroy_context (pulsesrc);
-
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+    pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
-  g_free (name);
-  return FALSE;
+    return FALSE;
+  }
 }
 
 static gboolean
 gst_pulsesrc_close (GstAudioSrc * asrc)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
   gst_pulsesrc_destroy_context (pulsesrc);
@@ -596,7 +650,7 @@ gst_pulsesrc_close (GstAudioSrc * asrc)
 static gboolean
 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
   gst_pulsesrc_destroy_stream (pulsesrc);
@@ -612,42 +666,48 @@ gst_pulsesrc_unprepare (GstAudioSrc * asrc)
 static guint
 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
   size_t sum = 0;
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
-
   pulsesrc->in_read = TRUE;
 
+  if (pulsesrc->paused)
+    goto was_paused;
+
   while (length > 0) {
     size_t l;
 
-    if (!pulsesrc->read_buffer) {
+    GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
 
+    /*check if we have a leftover buffer */
+    if (!pulsesrc->read_buffer) {
       for (;;) {
-        if (gst_pulsesrc_is_dead (pulsesrc))
+        if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
           goto unlock_and_fail;
 
+        /* read all available data, we keep a pointer to the data and the length
+         * and take from it what we need. */
         if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
-                &pulsesrc->read_buffer_length) < 0) {
-          GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-              ("pa_stream_peek() failed: %s",
-                  pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-          goto unlock_and_fail;
-        }
+                &pulsesrc->read_buffer_length) < 0)
+          goto peek_failed;
 
-        if (pulsesrc->read_buffer)
-          break;
+        GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
+            pulsesrc->read_buffer_length);
 
-        if (pulsesrc->did_reset)
-          goto unlock_and_fail;
+        /* if we have data, process if */
+        if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
+          break;
 
+        /* now wait for more data to become available */
+        GST_LOG_OBJECT (pulsesrc, "waiting for data");
         pa_threaded_mainloop_wait (pulsesrc->mainloop);
+
+        if (pulsesrc->paused)
+          goto was_paused;
       }
     }
 
-    g_assert (pulsesrc->read_buffer && pulsesrc->read_buffer_length);
-
     l = pulsesrc->read_buffer_length >
         length ? length : pulsesrc->read_buffer_length;
 
@@ -658,77 +718,90 @@ gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
 
     data = (guint8 *) data + l;
     length -= l;
-
     sum += l;
 
     if (pulsesrc->read_buffer_length <= 0) {
+      /* we copied all of the data, drop it now */
+      if (pa_stream_drop (pulsesrc->stream) < 0)
+        goto drop_failed;
 
-      if (pa_stream_drop (pulsesrc->stream) < 0) {
-        GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-            ("pa_stream_drop() failed: %s",
-                pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-        goto unlock_and_fail;
-      }
-
+      /* reset pointer to data */
       pulsesrc->read_buffer = NULL;
       pulsesrc->read_buffer_length = 0;
     }
   }
 
-  pulsesrc->did_reset = FALSE;
   pulsesrc->in_read = FALSE;
-
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+
   return sum;
 
+  /* ERRORS */
+was_paused:
+  {
+    GST_LOG_OBJECT (pulsesrc, "we are paused");
+    goto unlock_and_fail;
+  }
+peek_failed:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
+        ("pa_stream_peek() failed: %s",
+            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
+    goto unlock_and_fail;
+  }
+drop_failed:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
+        ("pa_stream_drop() failed: %s",
+            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
+    goto unlock_and_fail;
+  }
 unlock_and_fail:
+  {
+    pulsesrc->in_read = FALSE;
+    pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
-  pulsesrc->did_reset = FALSE;
-  pulsesrc->in_read = FALSE;
-
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
-  return (guint) - 1;
+    return (guint) - 1;
+  }
 }
 
+/* return the delay in samples */
 static guint
 gst_pulsesrc_delay (GstAudioSrc * asrc)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
-
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
   pa_usec_t t;
-
-  int negative;
+  int negative, res;
+  guint result;
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
+  if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
+    goto server_dead;
 
-  for (;;) {
-    if (gst_pulsesrc_is_dead (pulsesrc))
-      goto unlock_and_fail;
-
-    if (pa_stream_get_latency (pulsesrc->stream, &t, &negative) >= 0)
-      break;
-
-    if (pa_context_errno (pulsesrc->context) != PA_ERR_NODATA) {
-      GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-          ("pa_stream_get_latency() failed: %s",
-              pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-      goto unlock_and_fail;
-    }
-
-    pa_threaded_mainloop_wait (pulsesrc->mainloop);
-  }
-
-  if (negative)
-    t = 0;
+  /* get the latency, this can fail when we don't have a latency update yet.
+   * We don't want to wait for latency updates here but we just return 0. */
+  res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
 
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
-  return (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
-
-unlock_and_fail:
+  if (res > 0) {
+    GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
+    result = 0;
+  } else {
+    if (negative)
+      result = 0;
+    else
+      result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
+  }
+  return result;
 
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
-  return 0;
+  /* ERRORS */
+server_dead:
+  {
+    GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
+    pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+    return 0;
+  }
 }
 
 static gboolean
@@ -737,51 +810,48 @@ gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
   pa_channel_map channel_map;
   GstStructure *s;
   gboolean need_channel_layout = FALSE;
-  GstRingBufferSpec spec;
+  GstAudioRingBufferSpec spec;
+  const gchar *name;
 
-  memset (&spec, 0, sizeof (GstRingBufferSpec));
+  memset (&spec, 0, sizeof (GstAudioRingBufferSpec));
   spec.latency_time = GST_SECOND;
-  if (!gst_ring_buffer_parse_caps (&spec, caps)) {
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
-        ("Can't parse caps."), (NULL));
-    goto fail;
-  }
+  if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
+    goto invalid_caps;
+
   /* Keep the refcount of the caps at 1 to make them writable */
   gst_caps_unref (spec.caps);
 
-  if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec)) {
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
-        ("Invalid sample specification."), (NULL));
-    goto fail;
-  }
+  if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec))
+    goto invalid_spec;
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
 
-  if (!pulsesrc->context) {
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
-    goto unlock_and_fail;
-  }
+  if (!pulsesrc->context)
+    goto bad_context;
 
   s = gst_caps_get_structure (caps, 0);
   if (!gst_structure_has_field (s, "channel-layout") ||
       !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
-    if (spec.channels == 1)
+    if (spec.info.channels == 1)
       pa_channel_map_init_mono (&channel_map);
-    else if (spec.channels == 2)
+    else if (spec.info.channels == 2)
       pa_channel_map_init_stereo (&channel_map);
     else
       need_channel_layout = TRUE;
   }
 
-  if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
-              "Record Stream",
-              &pulsesrc->sample_spec,
-              (need_channel_layout) ? NULL : &channel_map))) {
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-        ("Failed to create stream: %s",
-            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-    goto unlock_and_fail;
-  }
+  name = "Record Stream";
+  if (pulsesrc->proplist) {
+    if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
+                name, &pulsesrc->sample_spec,
+                (need_channel_layout) ? NULL : &channel_map,
+                pulsesrc->proplist)))
+      goto create_failed;
+
+  } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
+              name, &pulsesrc->sample_spec,
+              (need_channel_layout) ? NULL : &channel_map)))
+    goto create_failed;
 
   if (need_channel_layout) {
     const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
@@ -796,6 +866,10 @@ gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
       pulsesrc);
   pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
       pulsesrc);
+  pa_stream_set_underflow_callback (pulsesrc->stream,
+      gst_pulsesrc_stream_underflow_cb, pulsesrc);
+  pa_stream_set_overflow_callback (pulsesrc->stream,
+      gst_pulsesrc_stream_overflow_cb, pulsesrc);
   pa_stream_set_latency_update_callback (pulsesrc->stream,
       gst_pulsesrc_stream_latency_update_cb, pulsesrc);
 
@@ -803,13 +877,40 @@ gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
 
   return TRUE;
 
+  /* ERRORS */
+invalid_caps:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
+        ("Can't parse caps."), (NULL));
+    goto fail;
+  }
+invalid_spec:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
+        ("Invalid sample specification."), (NULL));
+    goto fail;
+  }
+bad_context:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
+    goto unlock_and_fail;
+  }
+create_failed:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
+        ("Failed to create stream: %s",
+            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
+    goto unlock_and_fail;
+  }
 unlock_and_fail:
-  gst_pulsesrc_destroy_stream (pulsesrc);
+  {
+    gst_pulsesrc_destroy_stream (pulsesrc);
 
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+    pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
-fail:
-  return FALSE;
+  fail:
+    return FALSE;
+  }
 }
 
 /* This is essentially gst_base_src_negotiate_default() but the caps
@@ -818,45 +919,40 @@ fail:
 static gboolean
 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
 {
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (basesrc);
   GstCaps *thiscaps;
   GstCaps *caps = NULL;
   GstCaps *peercaps = NULL;
   gboolean result = FALSE;
 
   /* first see what is possible on our source pad */
-  thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
+  thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
   /* nothing or anything is allowed, we're done */
   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
     goto no_nego_needed;
 
   /* get the peer caps */
-  peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
+  peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
   if (peercaps) {
-    GstCaps *icaps;
-
     /* get intersection */
-    icaps = gst_caps_intersect (thiscaps, peercaps);
-    GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
+    caps = gst_caps_intersect (thiscaps, peercaps);
+    GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
     gst_caps_unref (thiscaps);
     gst_caps_unref (peercaps);
-    if (icaps) {
-      /* take first (and best, since they are sorted) possibility */
-      caps = gst_caps_copy_nth (icaps, 0);
-      gst_caps_unref (icaps);
-    }
   } else {
     /* no peer, work with our own caps then */
     caps = thiscaps;
   }
   if (caps) {
+    /* take first (and best, since they are sorted) possibility */
     caps = gst_caps_make_writable (caps);
     gst_caps_truncate (caps);
 
     /* now fixate */
     if (!gst_caps_is_empty (caps)) {
-      gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
+      GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
 
       if (gst_caps_is_any (caps)) {
@@ -865,10 +961,9 @@ gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
         result = TRUE;
       } else if (gst_caps_is_fixed (caps)) {
         /* yay, fixed caps, use those then */
-        result = gst_pulsesrc_create_stream (GST_PULSESRC (basesrc), caps);
+        result = gst_pulsesrc_create_stream (pulsesrc, caps);
         if (result)
-          gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
-        result = TRUE;
+          result = gst_base_src_set_caps (basesrc, caps);
       }
     }
     gst_caps_unref (caps);
@@ -885,41 +980,41 @@ no_nego_needed:
 }
 
 static gboolean
-gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
+gst_pulsesrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
 {
-  pa_buffer_attr buf_attr;
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
+  pa_buffer_attr wanted;
+  const pa_buffer_attr *actual;
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
 
-  memset (&buf_attr, 0, sizeof (buf_attr));
-  buf_attr.maxlength = spec->segtotal * spec->segsize * 2;
-  buf_attr.fragsize = spec->segsize;
+  wanted.maxlength = -1;
+  wanted.tlength = -1;
+  wanted.prebuf = 0;
+  wanted.minreq = -1;
+  wanted.fragsize = spec->segsize;
 
-  if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &buf_attr,
-          PA_STREAM_INTERPOLATE_TIMING |
-          PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONOUS |
-#if HAVE_PULSE_0_9_11
-          PA_STREAM_ADJUST_LATENCY |
-#endif
-          PA_STREAM_START_CORKED) < 0) {
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-        ("Failed to connect stream: %s",
-            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-    goto unlock_and_fail;
-  }
+  GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
+  GST_INFO_OBJECT (pulsesrc, "tlength:   %d", wanted.tlength);
+  GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", wanted.prebuf);
+  GST_INFO_OBJECT (pulsesrc, "minreq:    %d", wanted.minreq);
+  GST_INFO_OBJECT (pulsesrc, "fragsize:  %d", wanted.fragsize);
+
+  if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
+          PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
+          PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
+          PA_STREAM_START_CORKED) < 0)
+    goto connect_failed;
+
+  pulsesrc->corked = TRUE;
 
   for (;;) {
     pa_stream_state_t state;
 
     state = pa_stream_get_state (pulsesrc->stream);
 
-    if (!PA_STREAM_IS_GOOD (state)) {
-      GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-          ("Failed to connect stream: %s",
-              pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-      goto unlock_and_fail;
-    }
+    if (!PA_STREAM_IS_GOOD (state))
+      goto stream_is_bad;
 
     if (state == PA_STREAM_READY)
       break;
@@ -927,37 +1022,82 @@ gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
     /* Wait until the stream is ready */
     pa_threaded_mainloop_wait (pulsesrc->mainloop);
   }
+  pulsesrc->stream_connected = TRUE;
+
+  /* store the source output index so it can be accessed via a property */
+  pulsesrc->source_output_idx = pa_stream_get_index (pulsesrc->stream);
+  g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
+
+  /* get the actual buffering properties now */
+  actual = pa_stream_get_buffer_attr (pulsesrc->stream);
+
+  GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
+  GST_INFO_OBJECT (pulsesrc, "tlength:   %d (wanted: %d)",
+      actual->tlength, wanted.tlength);
+  GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", actual->prebuf);
+  GST_INFO_OBJECT (pulsesrc, "minreq:    %d (wanted %d)", actual->minreq,
+      wanted.minreq);
+  GST_INFO_OBJECT (pulsesrc, "fragsize:  %d (wanted %d)",
+      actual->fragsize, wanted.fragsize);
+
+  if (actual->fragsize >= wanted.fragsize) {
+    spec->segsize = actual->fragsize;
+  } else {
+    spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
+  }
+  spec->segtotal = actual->maxlength / spec->segsize;
 
+  if (!pulsesrc->paused) {
+    GST_DEBUG_OBJECT (pulsesrc, "uncorking because we are playing");
+    gst_pulsesrc_set_corked (pulsesrc, FALSE, FALSE);
+  }
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 
   return TRUE;
 
+  /* ERRORS */
+connect_failed:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
+        ("Failed to connect stream: %s",
+            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
+    goto unlock_and_fail;
+  }
+stream_is_bad:
+  {
+    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
+        ("Failed to connect stream: %s",
+            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
+    goto unlock_and_fail;
+  }
 unlock_and_fail:
+  {
+    gst_pulsesrc_destroy_stream (pulsesrc);
 
-  gst_pulsesrc_destroy_stream (pulsesrc);
-
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
-  return FALSE;
+    pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+    return FALSE;
+  }
 }
 
 static void
 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
 
-  pulsesrc->operation_success = !!success;
+  pulsesrc->operation_success = ! !success;
   pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
 }
 
 static void
 gst_pulsesrc_reset (GstAudioSrc * asrc)
 {
-  GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
+  GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
   pa_operation *o = NULL;
 
   pa_threaded_mainloop_lock (pulsesrc->mainloop);
+  GST_DEBUG_OBJECT (pulsesrc, "reset");
 
-  if (gst_pulsesrc_is_dead (pulsesrc))
+  if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
     goto unlock_and_fail;
 
   if (!(o =
@@ -969,16 +1109,16 @@ gst_pulsesrc_reset (GstAudioSrc * asrc)
     goto unlock_and_fail;
   }
 
+  pulsesrc->paused = TRUE;
   /* Inform anyone waiting in _write() call that it shall wakeup */
   if (pulsesrc->in_read) {
-    pulsesrc->did_reset = TRUE;
     pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
   }
 
   pulsesrc->operation_success = FALSE;
   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
 
-    if (gst_pulsesrc_is_dead (pulsesrc))
+    if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
       goto unlock_and_fail;
 
     pa_threaded_mainloop_wait (pulsesrc->mainloop);
@@ -1000,77 +1140,145 @@ unlock_and_fail:
   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
 }
 
-static void
-gst_pulsesrc_pause (GstPulseSrc * pulsesrc, gboolean b)
+/* update the corked state of a stream, must be called with the mainloop
+ * lock */
+static gboolean
+gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
 {
   pa_operation *o = NULL;
+  gboolean res = FALSE;
 
-  pa_threaded_mainloop_lock (pulsesrc->mainloop);
-
-  if (gst_pulsesrc_is_dead (pulsesrc))
-    goto unlock;
+  GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
+  if (!psrc->stream_connected)
+    return TRUE;
 
-  if (!(o = pa_stream_cork (pulsesrc->stream, b, NULL, NULL))) {
+  if (psrc->corked != corked) {
+    if (!(o = pa_stream_cork (psrc->stream, corked,
+                gst_pulsesrc_success_cb, psrc)))
+      goto cork_failed;
 
-    GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
-        ("pa_stream_cork() failed: %s",
-            pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
-    goto unlock;
+    while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
+      pa_threaded_mainloop_wait (psrc->mainloop);
+      if (gst_pulsesrc_is_dead (psrc, TRUE))
+        goto server_dead;
+    }
+    psrc->corked = corked;
+  } else {
+    GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
   }
+  res = TRUE;
 
-  while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
+cleanup:
+  if (o)
+    pa_operation_unref (o);
 
-    if (gst_pulsesrc_is_dead (pulsesrc))
-      goto unlock;
+  return res;
 
-    pa_threaded_mainloop_wait (pulsesrc->mainloop);
+  /* ERRORS */
+server_dead:
+  {
+    GST_DEBUG_OBJECT (psrc, "the server is dead");
+    goto cleanup;
+  }
+cork_failed:
+  {
+    GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
+        ("pa_stream_cork() failed: %s",
+            pa_strerror (pa_context_errno (psrc->context))), (NULL));
+    goto cleanup;
   }
+}
 
-unlock:
+/* start/resume playback ASAP */
+static gboolean
+gst_pulsesrc_play (GstPulseSrc * psrc)
+{
+  pa_threaded_mainloop_lock (psrc->mainloop);
+  GST_DEBUG_OBJECT (psrc, "playing");
+  psrc->paused = FALSE;
+  gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
+  pa_threaded_mainloop_unlock (psrc->mainloop);
 
-  if (o)
-    pa_operation_unref (o);
+  return TRUE;
+}
 
-  pa_threaded_mainloop_unlock (pulsesrc->mainloop);
+/* pause/stop playback ASAP */
+static gboolean
+gst_pulsesrc_pause (GstPulseSrc * psrc)
+{
+  pa_threaded_mainloop_lock (psrc->mainloop);
+  GST_DEBUG_OBJECT (psrc, "pausing");
+  /* make sure the commit method stops writing */
+  psrc->paused = TRUE;
+  if (psrc->in_read) {
+    /* we are waiting in a read, signal */
+    GST_DEBUG_OBJECT (psrc, "signal read");
+    pa_threaded_mainloop_signal (psrc->mainloop, 0);
+  }
+  pa_threaded_mainloop_unlock (psrc->mainloop);
+
+  return TRUE;
 }
 
 static GstStateChangeReturn
 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
 {
-  GstPulseSrc *this = GST_PULSESRC (element);
+  GstStateChangeReturn ret;
+  GstPulseSrc *this = GST_PULSESRC_CAST (element);
 
   switch (transition) {
-
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      gst_pulsesrc_pause (this,
-          GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED);
-      break;
-
     case GST_STATE_CHANGE_NULL_TO_READY:
+      this->mainloop = pa_threaded_mainloop_new ();
+      g_assert (this->mainloop);
+
+      pa_threaded_mainloop_start (this->mainloop);
 
       if (!this->mixer)
         this->mixer =
             gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
             this->device, GST_PULSEMIXER_SOURCE);
-
       break;
+    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
+      /* uncork and start recording */
+      gst_pulsesrc_play (this);
+      break;
+    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
+      /* stop recording ASAP by corking */
+      pa_threaded_mainloop_lock (this->mainloop);
+      GST_DEBUG_OBJECT (this, "corking");
+      gst_pulsesrc_set_corked (this, TRUE, FALSE);
+      pa_threaded_mainloop_unlock (this->mainloop);
+      break;
+    default:
+      break;
+  }
 
-    case GST_STATE_CHANGE_READY_TO_NULL:
+  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
 
+  switch (transition) {
+    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
+      /* now make sure we get out of the _read method */
+      gst_pulsesrc_pause (this);
+      break;
+    case GST_STATE_CHANGE_READY_TO_NULL:
       if (this->mixer) {
         gst_pulsemixer_ctrl_free (this->mixer);
         this->mixer = NULL;
       }
 
-      break;
+      if (this->mainloop)
+        pa_threaded_mainloop_stop (this->mainloop);
 
+      gst_pulsesrc_destroy_context (this);
+
+      if (this->mainloop) {
+        pa_threaded_mainloop_free (this->mainloop);
+        this->mainloop = NULL;
+      }
+      break;
     default:
-      ;
+      break;
   }
 
-  if (GST_ELEMENT_CLASS (parent_class)->change_state)
-    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
-
-  return GST_STATE_CHANGE_SUCCESS;
+  return ret;
 }