stream: release some locks in error cases
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-stream.c
index fd8038e..fb20090 100644 (file)
  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  * Boston, MA 02110-1301, USA.
  */
+/**
+ * SECTION:rtsp-stream
+ * @short_description: A media stream
+ * @see_also: #GstRTSPMedia
+ *
+ * The #GstRTSPStream object manages the data transport for one stream. It
+ * is created from a payloader element and a source pad that produce the RTP
+ * packets for the stream.
+ *
+ * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
+ * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
+ *
+ * The #GstRTSPStream will use the configured addresspool, as set with
+ * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
+ * stream. With gst_rtsp_stream_get_multicast_address() you can get the
+ * configured address.
+ *
+ * With gst_rtsp_stream_get_server_port () you can get the port that the server
+ * will use to receive RTCP. This is the part that the clients will use to send
+ * RTCP to.
+ *
+ * With gst_rtsp_stream_add_transport() destinations can be added where the
+ * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
+ * the destination again.
+ *
+ * Last reviewed on 2013-07-16 (1.0.0)
+ */
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -41,6 +68,9 @@ struct _GstRTSPStreamPrivate
   gboolean is_joined;
   gchar *control;
 
+  GstRTSPProfile profiles;
+  GstRTSPLowerTrans protocols;
+
   /* pads on the rtpbin */
   GstPad *send_rtp_sink;
   GstPad *recv_sink[2];
@@ -91,14 +121,23 @@ struct _GstRTSPStreamPrivate
   GList *transports;
 
   gint dscp_qos;
+
+  /* stream blocking */
+  gulong blocked_id;
+  gboolean blocking;
 };
 
-#define DEFAULT_CONTROL NULL
+#define DEFAULT_CONTROL         NULL
+#define DEFAULT_PROFILES        GST_RTSP_PROFILE_AVP
+#define DEFAULT_PROTOCOLS       GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
+                                        GST_RTSP_LOWER_TRANS_TCP
 
 enum
 {
   PROP_0,
   PROP_CONTROL,
+  PROP_PROFILES,
+  PROP_PROTOCOLS,
   PROP_LAST
 };
 
@@ -134,6 +173,16 @@ gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
           "The control string for this stream", DEFAULT_CONTROL,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (gobject_class, PROP_PROFILES,
+      g_param_spec_flags ("profiles", "Profiles",
+          "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
+          DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
+      g_param_spec_flags ("protocols", "Protocols",
+          "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
+          DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
 
   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
@@ -150,6 +199,8 @@ gst_rtsp_stream_init (GstRTSPStream * stream)
 
   priv->dscp_qos = -1;
   priv->control = g_strdup (DEFAULT_CONTROL);
+  priv->profiles = DEFAULT_PROFILES;
+  priv->protocols = DEFAULT_PROTOCOLS;
 
   g_mutex_init (&priv->lock);
 }
@@ -196,6 +247,12 @@ gst_rtsp_stream_get_property (GObject * object, guint propid,
     case PROP_CONTROL:
       g_value_take_string (value, gst_rtsp_stream_get_control (stream));
       break;
+    case PROP_PROFILES:
+      g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
+      break;
+    case PROP_PROTOCOLS:
+      g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
   }
@@ -211,6 +268,12 @@ gst_rtsp_stream_set_property (GObject * object, guint propid,
     case PROP_CONTROL:
       gst_rtsp_stream_set_control (stream, g_value_get_string (value));
       break;
+    case PROP_PROFILES:
+      gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
+      break;
+    case PROP_PROTOCOLS:
+      gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
   }
@@ -263,12 +326,35 @@ gst_rtsp_stream_get_index (GstRTSPStream * stream)
 }
 
 /**
+ * gst_rtsp_stream_get_pt:
+ * @stream: a #GstRTSPStream
+ *
+ * Get the stream payload type.
+ *
+ * Return: the stream payload type.
+ */
+guint
+gst_rtsp_stream_get_pt (GstRTSPStream * stream)
+{
+  GstRTSPStreamPrivate *priv;
+  guint pt;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
+
+  priv = stream->priv;
+
+  g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
+
+  return pt;
+}
+
+/**
  * gst_rtsp_stream_get_srcpad:
  * @stream: a #GstRTSPStream
  *
  * Get the srcpad associated with @stream.
  *
- * Return: the srcpad. Unref after usage.
+ * Returns: (transfer full): the srcpad. Unref after usage.
  */
 GstPad *
 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
@@ -284,7 +370,7 @@ gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
  *
  * Get the control string to identify this stream.
  *
- * Return: the control string. free after usage.
+ * Returns: (transfer full): the control string. free after usage.
  */
 gchar *
 gst_rtsp_stream_get_control (GstRTSPStream * stream)
@@ -347,11 +433,14 @@ gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
 
   g_mutex_lock (&priv->lock);
   if (priv->control)
-    res = g_strcmp0 (priv->control, control);
+    res = (g_strcmp0 (priv->control, control) == 0);
   else {
     guint streamid;
-    sscanf (control, "stream=%u", &streamid);
-    res = (streamid == priv->idx);
+
+    if (sscanf (control, "stream=%u", &streamid) > 0)
+      res = (streamid == priv->idx);
+    else
+      res = FALSE;
   }
   g_mutex_unlock (&priv->lock);
 
@@ -471,6 +560,153 @@ gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
   return priv->dscp_qos;
 }
 
+/**
+ * gst_rtsp_stream_is_transport_supported:
+ * @stream: a #GstRTSPStream
+ * @transport: a #GstRTSPTransport
+ *
+ * Check if @transport can be handled by stream
+ *
+ * Returns: %TRUE if @transport can be handled by @stream.
+ */
+gboolean
+gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
+    GstRTSPTransport * transport)
+{
+  GstRTSPStreamPrivate *priv;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  if (transport->trans != GST_RTSP_TRANS_RTP)
+    goto unsupported_transmode;
+
+  if (!(transport->profile & priv->profiles))
+    goto unsupported_profile;
+
+  if (!(transport->lower_transport & priv->protocols))
+    goto unsupported_ltrans;
+
+  g_mutex_unlock (&priv->lock);
+
+  return TRUE;
+
+  /* ERRORS */
+unsupported_transmode:
+  {
+    GST_DEBUG ("unsupported transport mode %d", transport->trans);
+    g_mutex_unlock (&priv->lock);
+    return FALSE;
+  }
+unsupported_profile:
+  {
+    GST_DEBUG ("unsupported profile %d", transport->profile);
+    g_mutex_unlock (&priv->lock);
+    return FALSE;
+  }
+unsupported_ltrans:
+  {
+    GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
+    g_mutex_unlock (&priv->lock);
+    return FALSE;
+  }
+}
+
+/**
+ * gst_rtsp_stream_set_profiles:
+ * @stream: a #GstRTSPStream
+ * @profiles: the new profiles
+ *
+ * Configure the allowed profiles for @stream.
+ */
+void
+gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
+{
+  GstRTSPStreamPrivate *priv;
+
+  g_return_if_fail (GST_IS_RTSP_STREAM (stream));
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  priv->profiles = profiles;
+  g_mutex_unlock (&priv->lock);
+}
+
+/**
+ * gst_rtsp_stream_get_profiles:
+ * @stream: a #GstRTSPStream
+ *
+ * Get the allowed profiles of @stream.
+ *
+ * Returns: a #GstRTSPProfile
+ */
+GstRTSPProfile
+gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
+{
+  GstRTSPStreamPrivate *priv;
+  GstRTSPProfile res;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  res = priv->profiles;
+  g_mutex_unlock (&priv->lock);
+
+  return res;
+}
+
+/**
+ * gst_rtsp_stream_set_protocols:
+ * @stream: a #GstRTSPStream
+ * @protocols: the new flags
+ *
+ * Configure the allowed lower transport for @stream.
+ */
+void
+gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
+    GstRTSPLowerTrans protocols)
+{
+  GstRTSPStreamPrivate *priv;
+
+  g_return_if_fail (GST_IS_RTSP_STREAM (stream));
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  priv->protocols = protocols;
+  g_mutex_unlock (&priv->lock);
+}
+
+/**
+ * gst_rtsp_stream_get_protocols:
+ * @stream: a #GstRTSPStream
+ *
+ * Get the allowed protocols of @stream.
+ *
+ * Returns: a #GstRTSPLowerTrans
+ */
+GstRTSPLowerTrans
+gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
+{
+  GstRTSPStreamPrivate *priv;
+  GstRTSPLowerTrans res;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
+      GST_RTSP_LOWER_TRANS_UNKNOWN);
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  res = priv->protocols;
+  g_mutex_unlock (&priv->lock);
+
+  return res;
+}
 
 /**
  * gst_rtsp_stream_set_address_pool:
@@ -639,12 +875,14 @@ gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
 
   g_mutex_lock (&priv->lock);
   if (*addrp == NULL) {
+    GstRTSPAddressPoolResult res;
+
     if (priv->pool == NULL)
       goto no_pool;
 
-    *addrp = gst_rtsp_address_pool_reserve_address (priv->pool, address,
-        port, n_ports, ttl);
-    if (*addrp == NULL)
+    res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
+        port, n_ports, ttl, addrp);
+    if (res != GST_RTSP_ADDRESS_POOL_OK)
       goto no_address;
   } else {
     if (strcmp ((*addrp)->address, address) ||
@@ -912,10 +1150,6 @@ cleanup:
       gst_element_set_state (udpsink0, GST_STATE_NULL);
       gst_object_unref (udpsink0);
     }
-    if (udpsink1) {
-      gst_element_set_state (udpsink1, GST_STATE_NULL);
-      gst_object_unref (udpsink1);
-    }
     if (inetaddr)
       g_object_unref (inetaddr);
     g_list_free_full (rejected_addresses,
@@ -983,7 +1217,7 @@ gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
  *
  * Get the RTP session of this stream.
  *
- * Returns: The RTP session of this stream. Unref after usage.
+ * Returns: (transfer full): The RTP session of this stream. Unref after usage.
  */
 GObject *
 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
@@ -1255,7 +1489,7 @@ static GstAppSinkCallbacks sink_cb = {
  * @rtpbin: a rtpbin element in @bin
  * @state: the target state of the new elements
  *
- * Join the #Gstbin @bin that contains the element @rtpbin.
+ * Join the #GstBin @bin that contains the element @rtpbin.
  *
  * @stream will link to @rtpbin, which must be inside @bin. The elements
  * added to @bin will be set to the state given in @state.
@@ -1270,7 +1504,7 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
   gint i;
   guint idx;
   gchar *name;
-  GstPad *pad, *teepad, *queuepad, *selpad;
+  GstPad *pad, *sinkpad, *selpad;
   GstPadLinkReturn ret;
 
   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
@@ -1336,6 +1570,7 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
       stream);
 
   for (i = 0; i < 2; i++) {
+    GstPad *teepad, *queuepad;
     /* For the sender we create this bit of pipeline for both
      * RTP and RTCP. Sync and preroll are enabled on udpsink so
      * we need to add a queue before appsink to make the pipeline
@@ -1350,49 +1585,57 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
      *                 |     |    |  queue  |    | appsink |
      *                 |    src->sink      src->sink       |
      *                 '-----'    '---------'    '---------'
+     *
+     * When only UDP is allowed, we skip the tee, queue and appsink and link the
+     * udpsink directly to the session.
      */
-    /* make tee for RTP/RTCP */
-    priv->tee[i] = gst_element_factory_make ("tee", NULL);
-    gst_bin_add (bin, priv->tee[i]);
-
-    /* and link to rtpbin send pad */
-    pad = gst_element_get_static_pad (priv->tee[i], "sink");
-    gst_pad_link (priv->send_src[i], pad);
-    gst_object_unref (pad);
-
     /* add udpsink */
     gst_bin_add (bin, priv->udpsink[i]);
+    sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
 
-    /* link tee to udpsink */
-    teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
-    pad = gst_element_get_static_pad (priv->udpsink[i], "sink");
-    gst_pad_link (teepad, pad);
-    gst_object_unref (pad);
-    gst_object_unref (teepad);
-
-    /* make queue */
-    priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
-    gst_bin_add (bin, priv->appqueue[i]);
-    /* and link to tee */
-    teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
-    pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
-    gst_pad_link (teepad, pad);
-    gst_object_unref (pad);
-    gst_object_unref (teepad);
-
-    /* make appsink */
-    priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
-    g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
-    g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
-    gst_bin_add (bin, priv->appsink[i]);
-    gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
-        &sink_cb, stream, NULL);
-    /* and link to queue */
-    queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
-    pad = gst_element_get_static_pad (priv->appsink[i], "sink");
-    gst_pad_link (queuepad, pad);
-    gst_object_unref (pad);
-    gst_object_unref (queuepad);
+    if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
+      /* make tee for RTP/RTCP */
+      priv->tee[i] = gst_element_factory_make ("tee", NULL);
+      gst_bin_add (bin, priv->tee[i]);
+
+      /* and link to rtpbin send pad */
+      pad = gst_element_get_static_pad (priv->tee[i], "sink");
+      gst_pad_link (priv->send_src[i], pad);
+      gst_object_unref (pad);
+
+      /* link tee to udpsink */
+      teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
+      gst_pad_link (teepad, sinkpad);
+      gst_object_unref (teepad);
+
+      /* make queue */
+      priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
+      gst_bin_add (bin, priv->appqueue[i]);
+      /* and link to tee */
+      teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
+      pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
+      gst_pad_link (teepad, pad);
+      gst_object_unref (pad);
+      gst_object_unref (teepad);
+
+      /* make appsink */
+      priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
+      g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
+      g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
+      gst_bin_add (bin, priv->appsink[i]);
+      gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
+          &sink_cb, stream, NULL);
+      /* and link to queue */
+      queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
+      pad = gst_element_get_static_pad (priv->appsink[i], "sink");
+      gst_pad_link (queuepad, pad);
+      gst_object_unref (pad);
+      gst_object_unref (queuepad);
+    } else {
+      /* else only udpsink needed, link it to the session */
+      gst_pad_link (priv->send_src[i], sinkpad);
+    }
+    gst_object_unref (sinkpad);
 
     /* For the receiver we create this bit of pipeline for both
      * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
@@ -1444,24 +1687,32 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
       gst_object_unref (selpad);
     }
 
-    /* make and add appsrc */
-    priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
-    gst_bin_add (bin, priv->appsrc[i]);
-    /* and link to the funnel */
-    selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
-    pad = gst_element_get_static_pad (priv->appsrc[i], "src");
-    gst_pad_link (pad, selpad);
-    gst_object_unref (pad);
-    gst_object_unref (selpad);
+    if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
+      /* make and add appsrc */
+      priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
+      gst_bin_add (bin, priv->appsrc[i]);
+      /* and link to the funnel */
+      selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
+      pad = gst_element_get_static_pad (priv->appsrc[i], "src");
+      gst_pad_link (pad, selpad);
+      gst_object_unref (pad);
+      gst_object_unref (selpad);
+    }
 
     /* check if we need to set to a special state */
     if (state != GST_STATE_NULL) {
-      gst_element_set_state (priv->udpsink[i], state);
-      gst_element_set_state (priv->appsink[i], state);
-      gst_element_set_state (priv->appqueue[i], state);
-      gst_element_set_state (priv->tee[i], state);
-      gst_element_set_state (priv->funnel[i], state);
-      gst_element_set_state (priv->appsrc[i], state);
+      if (priv->udpsink[i])
+        gst_element_set_state (priv->udpsink[i], state);
+      if (priv->appsink[i])
+        gst_element_set_state (priv->appsink[i], state);
+      if (priv->appqueue[i])
+        gst_element_set_state (priv->appqueue[i], state);
+      if (priv->tee[i])
+        gst_element_set_state (priv->tee[i], state);
+      if (priv->funnel[i])
+        gst_element_set_state (priv->funnel[i], state);
+      if (priv->appsrc[i])
+        gst_element_set_state (priv->appsrc[i], state);
     }
   }
 
@@ -1535,12 +1786,18 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
   priv->send_rtp_sink = NULL;
 
   for (i = 0; i < 2; i++) {
-    gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
-    gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
-    gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
-    gst_element_set_state (priv->tee[i], GST_STATE_NULL);
-    gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
-    gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
+    if (priv->udpsink[i])
+      gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
+    if (priv->appsink[i])
+      gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
+    if (priv->appqueue[i])
+      gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
+    if (priv->tee[i])
+      gst_element_set_state (priv->tee[i], GST_STATE_NULL);
+    if (priv->funnel[i])
+      gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
+    if (priv->appsrc[i])
+      gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
     if (priv->udpsrc_v4[i]) {
       /* and set udpsrc to NULL now before removing */
       gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
@@ -1554,12 +1811,18 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
       gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
       gst_bin_remove (bin, priv->udpsrc_v6[i]);
     }
-    gst_bin_remove (bin, priv->udpsink[i]);
-    gst_bin_remove (bin, priv->appsrc[i]);
-    gst_bin_remove (bin, priv->appsink[i]);
-    gst_bin_remove (bin, priv->appqueue[i]);
-    gst_bin_remove (bin, priv->tee[i]);
-    gst_bin_remove (bin, priv->funnel[i]);
+    if (priv->udpsink[i])
+      gst_bin_remove (bin, priv->udpsink[i]);
+    if (priv->appsrc[i])
+      gst_bin_remove (bin, priv->appsrc[i]);
+    if (priv->appsink[i])
+      gst_bin_remove (bin, priv->appsink[i]);
+    if (priv->appqueue[i])
+      gst_bin_remove (bin, priv->appqueue[i]);
+    if (priv->tee[i])
+      gst_bin_remove (bin, priv->tee[i]);
+    if (priv->funnel[i])
+      gst_bin_remove (bin, priv->funnel[i]);
 
     gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
     gst_object_unref (priv->recv_sink[i]);
@@ -1594,6 +1857,7 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
 
 was_not_joined:
   {
+    g_mutex_unlock (&priv->lock);
     return TRUE;
   }
 }
@@ -1601,36 +1865,78 @@ was_not_joined:
 /**
  * gst_rtsp_stream_get_rtpinfo:
  * @stream: a #GstRTSPStream
- * @rtptime: result RTP timestamp
- * @seq: result RTP seqnum
+ * @rtptime: (allow-none): result RTP timestamp
+ * @seq: (allow-none): result RTP seqnum
+ * @clock_rate: the clock rate
+ * @running_time: (allow-none): result running-time
  *
- * Retrieve the current rtptime and seq. This is used to
+ * Retrieve the current rtptime, seq and running-time. This is used to
  * construct a RTPInfo reply header.
  *
- * Returns: %TRUE when rtptime and seq could be determined.
+ * Returns: %TRUE when rtptime, seq and running-time could be determined.
  */
 gboolean
 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
-    guint * rtptime, guint * seq)
+    guint * rtptime, guint * seq, guint * clock_rate,
+    GstClockTime * running_time)
 {
   GstRTSPStreamPrivate *priv;
+  GstStructure *stats;
   GObjectClass *payobjclass;
 
   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
-  g_return_val_if_fail (rtptime != NULL, FALSE);
-  g_return_val_if_fail (seq != NULL, FALSE);
 
   priv = stream->priv;
 
   payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
 
-  if (!g_object_class_find_property (payobjclass, "seqnum") ||
-      !g_object_class_find_property (payobjclass, "timestamp"))
-    return FALSE;
+  g_mutex_lock (&priv->lock);
+
+  if (g_object_class_find_property (payobjclass, "stats")) {
+    g_object_get (priv->payloader, "stats", &stats, NULL);
+    if (stats == NULL)
+      goto no_stats;
+
+    if (seq)
+      gst_structure_get_uint (stats, "seqnum", seq);
+
+    if (rtptime)
+      gst_structure_get_uint (stats, "timestamp", rtptime);
+
+    if (running_time)
+      gst_structure_get_clock_time (stats, "running-time", running_time);
+
+    if (clock_rate) {
+      gst_structure_get_uint (stats, "clock-rate", clock_rate);
+      if (*clock_rate == 0 && running_time)
+        *running_time = GST_CLOCK_TIME_NONE;
+    }
+    gst_structure_free (stats);
+  } else {
+    if (!g_object_class_find_property (payobjclass, "seqnum") ||
+        !g_object_class_find_property (payobjclass, "timestamp"))
+      goto no_stats;
+
+    if (seq)
+      g_object_get (priv->payloader, "seqnum", seq, NULL);
+
+    if (rtptime)
+      g_object_get (priv->payloader, "timestamp", rtptime, NULL);
 
-  g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
+    if (running_time)
+      *running_time = GST_CLOCK_TIME_NONE;
+  }
+  g_mutex_unlock (&priv->lock);
 
   return TRUE;
+
+  /* ERRORS */
+no_stats:
+  {
+    GST_WARNING ("Could not get payloader stats");
+    g_mutex_unlock (&priv->lock);
+    return FALSE;
+  }
 }
 
 /**
@@ -1685,13 +1991,18 @@ gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
   g_return_val_if_fail (priv->is_joined, FALSE);
 
   g_mutex_lock (&priv->lock);
-  element = gst_object_ref (priv->appsrc[0]);
+  if (priv->appsrc[0])
+    element = gst_object_ref (priv->appsrc[0]);
+  else
+    element = NULL;
   g_mutex_unlock (&priv->lock);
 
-  ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
-
-  gst_object_unref (element);
-
+  if (element) {
+    ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
+    gst_object_unref (element);
+  } else {
+    ret = GST_FLOW_OK;
+  }
   return ret;
 }
 
@@ -1720,13 +2031,18 @@ gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
   g_return_val_if_fail (priv->is_joined, FALSE);
 
   g_mutex_lock (&priv->lock);
-  element = gst_object_ref (priv->appsrc[1]);
+  if (priv->appsrc[1])
+    element = gst_object_ref (priv->appsrc[1]);
+  else
+    element = NULL;
   g_mutex_unlock (&priv->lock);
 
-  ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
-
-  gst_object_unref (element);
-
+  if (element) {
+    ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
+    gst_object_unref (element);
+  } else {
+    ret = GST_FLOW_OK;
+  }
   return ret;
 }
 
@@ -1759,14 +2075,14 @@ update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
       }
 
       if (add) {
-        GST_INFO ("adding %s:%d-%d", dest, min, max);
-        g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
-        g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
         if (ttl > 0) {
           GST_INFO ("setting ttl-mc %d", ttl);
           g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
           g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
         }
+        GST_INFO ("adding %s:%d-%d", dest, min, max);
+        g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
+        g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
         priv->transports = g_list_prepend (priv->transports, trans);
       } else {
         GST_INFO ("removing %s:%d-%d", dest, min, max);
@@ -1864,3 +2180,224 @@ gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
 
   return res;
 }
+
+/**
+ * gst_rtsp_stream_get_rtp_socket:
+ * @stream: a #GstRTSPStream
+ * @family: the socket family
+ *
+ * Get the RTP socket from @stream for a @family.
+ *
+ * @stream must be joined to a bin.
+ *
+ * Returns: (transfer full): the RTP socket or %NULL if no socket could be
+ *     allocated for @family. Unref after usage
+ */
+GSocket *
+gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
+{
+  GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
+  GSocket *socket;
+  const gchar *name;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
+  g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
+      family == G_SOCKET_FAMILY_IPV6, NULL);
+  g_return_val_if_fail (priv->udpsink[0], NULL);
+
+  if (family == G_SOCKET_FAMILY_IPV6)
+    name = "socket-v6";
+  else
+    name = "socket";
+
+  g_object_get (priv->udpsink[0], name, &socket, NULL);
+
+  return socket;
+}
+
+/**
+ * gst_rtsp_stream_get_rtcp_socket:
+ * @stream: a #GstRTSPStream
+ * @family: the socket family
+ *
+ * Get the RTCP socket from @stream for a @family.
+ *
+ * @stream must be joined to a bin.
+ *
+ * Returns: (transfer full): the RTCP socket or %NULL if no socket could be
+ *     allocated for @family. Unref after usage
+ */
+GSocket *
+gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
+{
+  GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
+  GSocket *socket;
+  const gchar *name;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
+  g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
+      family == G_SOCKET_FAMILY_IPV6, NULL);
+  g_return_val_if_fail (priv->udpsink[1], NULL);
+
+  if (family == G_SOCKET_FAMILY_IPV6)
+    name = "socket-v6";
+  else
+    name = "socket";
+
+  g_object_get (priv->udpsink[1], name, &socket, NULL);
+
+  return socket;
+}
+
+/**
+ * gst_rtsp_stream_transport_filter:
+ * @stream: a #GstRTSPStream
+ * @func: (scope call) (allow-none): a callback
+ * @user_data: user data passed to @func
+ *
+ * Call @func for each transport managed by @stream. The result value of @func
+ * determines what happens to the transport. @func will be called with @stream
+ * locked so no further actions on @stream can be performed from @func.
+ *
+ * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
+ * @stream.
+ *
+ * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
+ *
+ * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
+ * will also be added with an additional ref to the result #GList of this
+ * function..
+ *
+ * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
+ *
+ * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
+ * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
+ * element in the #GList should be unreffed before the list is freed.
+ */
+GList *
+gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
+    GstRTSPStreamTransportFilterFunc func, gpointer user_data)
+{
+  GstRTSPStreamPrivate *priv;
+  GList *result, *walk, *next;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
+
+  priv = stream->priv;
+
+  result = NULL;
+
+  g_mutex_lock (&priv->lock);
+  for (walk = priv->transports; walk; walk = next) {
+    GstRTSPStreamTransport *trans = walk->data;
+    GstRTSPFilterResult res;
+
+    next = g_list_next (walk);
+
+    if (func)
+      res = func (stream, trans, user_data);
+    else
+      res = GST_RTSP_FILTER_REF;
+
+    switch (res) {
+      case GST_RTSP_FILTER_REMOVE:
+        update_transport (stream, trans, FALSE);
+        break;
+      case GST_RTSP_FILTER_REF:
+        result = g_list_prepend (result, g_object_ref (trans));
+        break;
+      case GST_RTSP_FILTER_KEEP:
+      default:
+        break;
+    }
+  }
+  g_mutex_unlock (&priv->lock);
+
+  return result;
+}
+
+static GstPadProbeReturn
+pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
+{
+  GstRTSPStreamPrivate *priv;
+  GstRTSPStream *stream;
+
+  stream = user_data;
+  priv = stream->priv;
+
+  GST_DEBUG_OBJECT (pad, "now blocking");
+
+  g_mutex_lock (&priv->lock);
+  priv->blocking = TRUE;
+  g_mutex_unlock (&priv->lock);
+
+  gst_element_post_message (priv->payloader,
+      gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
+          gst_structure_new_empty ("GstRTSPStreamBlocking")));
+
+  return GST_PAD_PROBE_OK;
+}
+
+/**
+ * gst_rtsp_stream_set_blocked:
+ * @stream: a #GstRTSPStream
+ * @blocked: boolean indicating we should block or unblock
+ *
+ * Blocks or unblocks the dataflow on @stream.
+ *
+ * Returns: %TRUE on success
+ */
+gboolean
+gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
+{
+  GstRTSPStreamPrivate *priv;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  if (blocked) {
+    priv->blocking = FALSE;
+    if (priv->blocked_id == 0) {
+      priv->blocked_id = gst_pad_add_probe (priv->srcpad,
+          GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
+          GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
+          g_object_ref (stream), g_object_unref);
+    }
+  } else {
+    if (priv->blocked_id != 0) {
+      gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
+      priv->blocked_id = 0;
+      priv->blocking = FALSE;
+    }
+  }
+  g_mutex_unlock (&priv->lock);
+
+  return TRUE;
+}
+
+/**
+ * gst_rtsp_stream_is_blocking:
+ * @stream: a #GstRTSPStream
+ *
+ * Check if @stream is blocking on a #GstBuffer.
+ *
+ * Returns: %TRUE if @stream is blocking
+ */
+gboolean
+gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
+{
+  GstRTSPStreamPrivate *priv;
+  gboolean result;
+
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
+
+  priv = stream->priv;
+
+  g_mutex_lock (&priv->lock);
+  result = priv->blocking;
+  g_mutex_unlock (&priv->lock);
+
+  return result;
+}