rtsp-stream: Fix indentation
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-stream.c
index 6c7cacf..d64b5c3 100644 (file)
@@ -55,6 +55,8 @@
 #include <gst/app/gstappsrc.h>
 #include <gst/app/gstappsink.h>
 
+#include <gst/rtp/gstrtpbuffer.h>
+
 #include "rtsp-stream.h"
 
 #define GST_RTSP_STREAM_GET_PRIVATE(obj)  \
@@ -105,10 +107,12 @@ struct _GstRTSPStreamPrivate
    * sockets */
   GstElement *udpsrc_v6[2];
 
+  GstElement *udpqueue[2];
   GstElement *udpsink[2];
 
   /* for TCP transport */
   GstElement *appsrc[2];
+  GstClockTime appsrc_base_time[2];
   GstElement *appqueue[2];
   GstElement *appsink[2];
 
@@ -1385,8 +1389,8 @@ gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
 }
 
 /**
- * gst_rtsp_media_get_retransmission_time:
- * @media: a #GstRTSPMedia
+ * gst_rtsp_stream_get_retransmission_time:
+ * @stream: a #GstRTSPStream
  *
  * Get the amount of time to store retransmission data.
  *
@@ -1406,6 +1410,13 @@ gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
   return ret;
 }
 
+/**
+ * gst_rtsp_stream_set_retransmission_pt:
+ * @stream: a #GstRTSPStream
+ * @rtx_pt: a #guint
+ *
+ * Set the payload type (pt) for retransmission of this stream.
+ */
 void
 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
 {
@@ -1427,6 +1438,14 @@ gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
   g_mutex_unlock (&stream->priv->lock);
 }
 
+/**
+ * gst_rtsp_stream_get_retransmission_pt:
+ * @stream: a #GstRTSPStream
+ *
+ * Get the payload-type used for retransmission of this stream
+ *
+ * Returns: The retransmission PT.
+ */
 guint
 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
 {
@@ -1441,6 +1460,46 @@ gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
   return rtx_pt;
 }
 
+/**
+ * gst_rtsp_stream_set_buffer_size:
+ * @stream: a #GstRTSPStream
+ * @size: the buffer size
+ *
+ * Set the size of the UDP transmission buffer (in bytes)
+ * Needs to be set before the stream is joined to a bin.
+ *
+ * Since: 1.6
+ */
+void
+gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
+{
+  g_mutex_lock (&stream->priv->lock);
+  stream->priv->buffer_size = size;
+  g_mutex_unlock (&stream->priv->lock);
+}
+
+/**
+ * gst_rtsp_stream_get_buffer_size:
+ * @stream: a #GstRTSPStream
+ *
+ * Get the size of the UDP transmission buffer (in bytes)
+ *
+ * Returns: the size of the UDP TX buffer
+ *
+ * Since: 1.6
+ */
+guint
+gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
+{
+  guint buffer_size;
+
+  g_mutex_lock (&stream->priv->lock);
+  buffer_size = stream->priv->buffer_size;
+  g_mutex_unlock (&stream->priv->lock);
+
+  return buffer_size;
+}
+
 /* executed from streaming thread */
 static void
 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
@@ -1624,6 +1683,38 @@ on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
 }
 
 static void
+on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
+{
+  GST_INFO ("%p: new sender source %p", stream, source);
+#ifndef DUMP_STATS
+  {
+    GstStructure *stats;
+    g_object_get (source, "stats", &stats, NULL);
+    if (stats) {
+      dump_structure (stats);
+      gst_structure_free (stats);
+    }
+  }
+#endif
+}
+
+static void
+on_sender_ssrc_active (GObject * session, GObject * source,
+    GstRTSPStream * stream)
+{
+#ifndef DUMP_STATS
+  {
+    GstStructure *stats;
+    g_object_get (source, "stats", &stats, NULL);
+    if (stats) {
+      dump_structure (stats);
+      gst_structure_free (stats);
+    }
+  }
+#endif
+}
+
+static void
 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
 {
   if (is_rtp) {
@@ -1813,8 +1904,19 @@ request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
   return gst_object_ref (priv->srtpdec);
 }
 
-static GstElement *
-request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
+/**
+ * gst_rtsp_stream_request_aux_sender:
+ * @stream: a #GstRTSPStream
+ * @sessid: the session id
+ *
+ * Creating a rtxsend bin
+ *
+ * Returns: (transfer full): a #GstElement.
+ *
+ * Since: 1.6
+ */
+GstElement *
+gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
 {
   GstElement *bin;
   GstPad *pad;
@@ -1823,6 +1925,8 @@ request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
   guint pt, rtx_pt;
   gchar *pt_s;
 
+  g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
+
   pt = gst_rtsp_stream_get_pt (stream);
   pt_s = g_strdup_printf ("%u", pt);
   rtx_pt = stream->priv->rtx_pt;
@@ -1971,8 +2075,9 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
   gint i;
   guint idx;
   gchar *name;
-  GstPad *pad, *sinkpad, *selpad;
+  GstPad *pad, *sinkpad = NULL, *selpad;
   GstPadLinkReturn ret;
+  gboolean is_tcp = FALSE, is_udp = FALSE;
 
   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
@@ -1989,7 +2094,12 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
 
   GST_INFO ("stream %p joining bin as session %u", stream, idx);
 
-  if (!alloc_ports (stream))
+  is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
+
+  is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
+      (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
+
+  if (is_udp && !alloc_ports (stream))
     goto no_ports;
 
   /* update the dscp qos field in the sinks */
@@ -2008,42 +2118,38 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
         (GCallback) request_rtp_rtcp_decoder, stream);
   }
 
-  if (priv->rtx_time > 0 && priv->srcpad) {
-    /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
-    g_signal_connect (rtpbin, "request-aux-sender",
-        (GCallback) request_aux_sender, stream);
-  }
   if (priv->sinkpad) {
     g_signal_connect (rtpbin, "request-pt-map",
         (GCallback) request_pt_map, stream);
   }
 
-  /* get a pad for sending RTP */
-  name = g_strdup_printf ("send_rtp_sink_%u", idx);
-  priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
-  g_free (name);
-
+  /* get pads from the RTP session element for sending and receiving
+   * RTP/RTCP*/
   if (priv->srcpad) {
+    /* get a pad for sending RTP */
+    name = g_strdup_printf ("send_rtp_sink_%u", idx);
+    priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
+    g_free (name);
+
     /* link the RTP pad to the session manager, it should not really fail unless
      * this is not really an RTP pad */
     ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
     if (ret != GST_PAD_LINK_OK)
       goto link_failed;
+
+    name = g_strdup_printf ("send_rtp_src_%u", idx);
+    priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
+    g_free (name);
   } else {
     /* Need to connect our sinkpad from here */
     g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
     /* EOS */
     g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
-  }
 
-  /* get pads from the RTP session element for sending and receiving
-   * RTP/RTCP*/
-  name = g_strdup_printf ("send_rtp_src_%u", idx);
-  priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
-  g_free (name);
-  name = g_strdup_printf ("recv_rtp_sink_%u", idx);
-  priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
-  g_free (name);
+    name = g_strdup_printf ("recv_rtp_sink_%u", idx);
+    priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
+    g_free (name);
+  }
 
   name = g_strdup_printf ("send_rtcp_src_%u", idx);
   priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
@@ -2068,161 +2174,216 @@ gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
   g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
       stream);
 
+  /* signal for sender ssrc */
+  g_signal_connect (priv->session, "on-new-sender-ssrc",
+      (GCallback) on_new_sender_ssrc, stream);
+  g_signal_connect (priv->session, "on-sender-ssrc-active",
+      (GCallback) on_sender_ssrc_active, 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
-     * not block. For the TCP case, we want to pump data to the
-     * client as fast as possible anyway.
+     * we need to add a queue before appsink and udpsink to make
+     * the pipeline not block. For the TCP case, we want to pump
+     * client as fast as possible anyway. This pipeline is used
+     * when both TCP and UDP are present.
      *
-     * .--------.      .-----.    .---------.
-     * | rtpbin |      | tee |    | udpsink |
-     * |       send->sink   src->sink       |
-     * '--------'      |     |    '---------'
+     * .--------.      .-----.    .---------.    .---------.
+     * | rtpbin |      | tee |    |  queue  |    | udpsink |
+     * |       send->sink   src->sink      src->sink       |
+     * '--------'      |     |    '---------'    '---------'
      *                 |     |    .---------.    .---------.
      *                 |     |    |  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.
+     * When only UDP or only TCP is allowed, we skip the tee and queue
+     * and link the udpsink (for UDP) or appsink (for TCP) directly to
+     * the session.
      */
-    /* add udpsink */
-    gst_bin_add (bin, priv->udpsink[i]);
-    sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
-
-    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
-     * and it is all funneled into the rtpbin receive pad.
-     *
-     * .--------.     .--------.    .--------.
-     * | udpsrc |     | funnel |    | rtpbin |
-     * |       src->sink      src->sink      |
-     * '--------'     |        |    '--------'
-     * .--------.     |        |
-     * | appsrc |     |        |
-     * |       src->sink       |
-     * '--------'     '--------'
-     */
-    /* make funnel for the RTP/RTCP receivers */
-    priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
-    gst_bin_add (bin, priv->funnel[i]);
+    /* Only link the RTP send src if we're going to send RTP, link
+     * the RTCP send src always */
+    if (priv->srcpad || i == 1) {
+      if (is_udp) {
+        /* add udpsink */
+        gst_bin_add (bin, priv->udpsink[i]);
+        sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
+      }
 
-    pad = gst_element_get_static_pad (priv->funnel[i], "src");
-    gst_pad_link (pad, priv->recv_sink[i]);
-    gst_object_unref (pad);
+      if (is_tcp) {
+        /* make appsink */
+        priv->appsink[i] = gst_element_factory_make ("appsink", 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);
+      }
 
-    if (priv->udpsrc_v4[i]) {
-      if (priv->srcpad) {
-        /* we set and keep these to playing so that they don't cause NO_PREROLL return
-         * values. This is only relevant for PLAY pipelines */
-        gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
-        gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
+      if (is_udp && is_tcp) {
+        g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
+
+        /* 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);
+
+        priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
+        g_object_set (priv->udpqueue[i], "max-size-buffers",
+            1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
+            NULL);
+        gst_bin_add (bin, priv->udpqueue[i]);
+        /* link tee to udpqueue */
+        teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
+        pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
+        gst_pad_link (teepad, pad);
+        gst_object_unref (pad);
+        gst_object_unref (teepad);
+
+        /* link udpqueue to udpsink */
+        queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
+        gst_pad_link (queuepad, sinkpad);
+        gst_object_unref (queuepad);
+        gst_object_unref (sinkpad);
+
+        /* make appqueue */
+        priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
+        g_object_set (priv->appqueue[i], "max-size-buffers",
+            1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
+            NULL);
+        gst_bin_add (bin, priv->appqueue[i]);
+        /* and link tee to appqueue */
+        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);
+
+        /* and link appqueue to appsink */
+        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 if (is_tcp) {
+        /* only appsink needed, link it to the session */
+        pad = gst_element_get_static_pad (priv->appsink[i], "sink");
+        gst_pad_link (priv->send_src[i], pad);
+        gst_object_unref (pad);
+
+        /* when its only TCP, we need to set sync and preroll to FALSE
+         * for the sink to avoid deadlock. And this is only needed for
+         * sink used for RTCP data, not the RTP data. */
+        if (i == 1)
+          g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
+      } else {
+        /* else only udpsink needed, link it to the session */
+        gst_pad_link (priv->send_src[i], sinkpad);
+        gst_object_unref (sinkpad);
       }
-      /* add udpsrc */
-      gst_bin_add (bin, priv->udpsrc_v4[i]);
+    }
 
-      /* and link to the funnel v4 */
-      selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
-      pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
-      gst_pad_link (pad, selpad);
+    /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
+     * RTCP sink always */
+    if (priv->sinkpad || i == 1) {
+      /* For the receiver we create this bit of pipeline for both
+       * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
+       * and it is all funneled into the rtpbin receive pad.
+       *
+       * .--------.     .--------.    .--------.
+       * | udpsrc |     | funnel |    | rtpbin |
+       * |       src->sink      src->sink      |
+       * '--------'     |        |    '--------'
+       * .--------.     |        |
+       * | appsrc |     |        |
+       * |       src->sink       |
+       * '--------'     '--------'
+       */
+      /* make funnel for the RTP/RTCP receivers */
+      priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
+      gst_bin_add (bin, priv->funnel[i]);
+
+      pad = gst_element_get_static_pad (priv->funnel[i], "src");
+      gst_pad_link (pad, priv->recv_sink[i]);
       gst_object_unref (pad);
-      gst_object_unref (selpad);
-    }
 
-    if (priv->udpsrc_v6[i]) {
-      if (priv->srcpad) {
-        gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
-        gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
+      if (priv->udpsrc_v4[i]) {
+        if (priv->srcpad) {
+          /* we set and keep these to playing so that they don't cause NO_PREROLL return
+           * values. This is only relevant for PLAY pipelines */
+          gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
+          gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
+        }
+        /* add udpsrc */
+        gst_bin_add (bin, priv->udpsrc_v4[i]);
+
+        /* and link to the funnel v4 */
+        selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
+        pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
+        gst_pad_link (pad, selpad);
+        gst_object_unref (pad);
+        gst_object_unref (selpad);
       }
-      gst_bin_add (bin, priv->udpsrc_v6[i]);
 
-      /* and link to the funnel v6 */
-      selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
-      pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
-      gst_pad_link (pad, selpad);
-      gst_object_unref (pad);
-      gst_object_unref (selpad);
-    }
+      if (priv->udpsrc_v6[i]) {
+        if (priv->srcpad) {
+          gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
+          gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
+        }
+        gst_bin_add (bin, priv->udpsrc_v6[i]);
+
+        /* and link to the funnel v6 */
+        selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
+        pad = gst_element_get_static_pad (priv->udpsrc_v6[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);
-      g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, 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 (is_tcp) {
+        /* make and add appsrc */
+        priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
+        priv->appsrc_base_time[i] = -1;
+        g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, 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) {
-      if (priv->udpsink[i])
+      if (priv->udpsink[i] && (priv->srcpad || i == 1))
         gst_element_set_state (priv->udpsink[i], state);
-      if (priv->appsink[i])
+      if (priv->appsink[i] && (priv->srcpad || i == 1))
         gst_element_set_state (priv->appsink[i], state);
-      if (priv->appqueue[i])
+      if (priv->appqueue[i] && (priv->srcpad || i == 1))
         gst_element_set_state (priv->appqueue[i], state);
-      if (priv->tee[i])
+      if (priv->udpqueue[i] && (priv->srcpad || i == 1))
+        gst_element_set_state (priv->udpqueue[i], state);
+      if (priv->tee[i] && (priv->srcpad || i == 1))
         gst_element_set_state (priv->tee[i], state);
-      if (priv->funnel[i])
+      if (priv->funnel[i] && (priv->sinkpad || i == 1))
         gst_element_set_state (priv->funnel[i], state);
-      if (priv->appsrc[i])
+      if (priv->appsrc[i] && (priv->sinkpad || i == 1))
         gst_element_set_state (priv->appsrc[i], state);
     }
   }
 
-  /* be notified of caps changes */
-  priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
-      (GCallback) caps_notify, stream);
+  if (priv->srcpad) {
+    /* be notified of caps changes */
+    priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
+        (GCallback) caps_notify, stream);
+  }
 
   priv->is_joined = TRUE;
   g_mutex_unlock (&priv->lock);
@@ -2268,6 +2429,7 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
   GstRTSPStreamPrivate *priv;
   gint i;
   GList *l;
+  gboolean is_tcp, is_udp;
 
   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
@@ -2290,15 +2452,22 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
 
   if (priv->srcpad) {
     gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
+
+    g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
+    gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
+    gst_object_unref (priv->send_rtp_sink);
+    priv->send_rtp_sink = NULL;
   } else if (priv->recv_rtp_src) {
     gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
     gst_object_unref (priv->recv_rtp_src);
     priv->recv_rtp_src = NULL;
   }
-  g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
-  gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
-  gst_object_unref (priv->send_rtp_sink);
-  priv->send_rtp_sink = NULL;
+
+  is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
+
+  is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
+      (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
+
 
   for (i = 0; i < 2; i++) {
     if (priv->udpsink[i])
@@ -2307,24 +2476,39 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
       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->udpqueue[i])
+      gst_element_set_state (priv->udpqueue[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);
-      gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
-      /* removing them should also nicely release the request
-       * pads when they finalize */
-      gst_bin_remove (bin, priv->udpsrc_v4[i]);
+      if (priv->sinkpad || i == 1) {
+        /* and set udpsrc to NULL now before removing */
+        gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
+        gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
+        /* removing them should also nicely release the request
+         * pads when they finalize */
+        gst_bin_remove (bin, priv->udpsrc_v4[i]);
+      } else {
+        /* we need to set the state to NULL before unref */
+        gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
+        gst_object_unref (priv->udpsrc_v4[i]);
+      }
     }
+
     if (priv->udpsrc_v6[i]) {
-      gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
-      gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
-      gst_bin_remove (bin, priv->udpsrc_v6[i]);
+      if (priv->sinkpad || i == 1) {
+        gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
+        gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
+        gst_bin_remove (bin, priv->udpsrc_v6[i]);
+      } else {
+        gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
+        gst_object_unref (priv->udpsrc_v6[i]);
+      }
     }
 
     for (l = priv->transport_sources; l; l = l->next) {
@@ -2338,22 +2522,26 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
       gst_bin_remove (bin, s->udpsrc[i]);
     }
 
-    if (priv->udpsink[i])
+    if (priv->udpsink[i] && is_udp && (priv->srcpad || i == 1))
       gst_bin_remove (bin, priv->udpsink[i]);
-    if (priv->appsrc[i])
+    if (priv->appsrc[i] && (priv->sinkpad || i == 1))
       gst_bin_remove (bin, priv->appsrc[i]);
-    if (priv->appsink[i])
+    if (priv->appsink[i] && is_tcp && (priv->srcpad || i == 1))
       gst_bin_remove (bin, priv->appsink[i]);
-    if (priv->appqueue[i])
+    if (priv->appqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
       gst_bin_remove (bin, priv->appqueue[i]);
-    if (priv->tee[i])
+    if (priv->udpqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
+      gst_bin_remove (bin, priv->udpqueue[i]);
+    if (priv->tee[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
       gst_bin_remove (bin, priv->tee[i]);
-    if (priv->funnel[i])
+    if (priv->funnel[i] && (priv->sinkpad || i == 1))
       gst_bin_remove (bin, priv->funnel[i]);
 
-    gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
-    gst_object_unref (priv->recv_sink[i]);
-    priv->recv_sink[i] = NULL;
+    if (priv->sinkpad || i == 1) {
+      gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
+      gst_object_unref (priv->recv_sink[i]);
+      priv->recv_sink[i] = NULL;
+    }
 
     priv->udpsrc_v4[i] = NULL;
     priv->udpsrc_v6[i] = NULL;
@@ -2361,6 +2549,7 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
     priv->appsrc[i] = NULL;
     priv->appsink[i] = NULL;
     priv->appqueue[i] = NULL;
+    priv->udpqueue[i] = NULL;
     priv->tee[i] = NULL;
     priv->funnel[i] = NULL;
   }
@@ -2372,8 +2561,10 @@ gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
   g_list_free (priv->transport_sources);
   priv->transport_sources = NULL;
 
-  gst_object_unref (priv->send_src[0]);
-  priv->send_src[0] = NULL;
+  if (priv->srcpad) {
+    gst_object_unref (priv->send_src[0]);
+    priv->send_src[0] = NULL;
+  }
 
   gst_element_release_request_pad (rtpbin, priv->send_src[1]);
   gst_object_unref (priv->send_src[1]);
@@ -2438,6 +2629,62 @@ gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
 
   g_mutex_lock (&priv->lock);
 
+  /* First try to extract the information from the last buffer on the sinks.
+   * This will have a more accurate sequence number and timestamp, as between
+   * the payloader and the sink there can be some queues
+   */
+  if (priv->udpsink[0] || priv->appsink[0]) {
+    GstSample *last_sample;
+
+    if (priv->udpsink[0])
+      g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
+    else
+      g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
+
+    if (last_sample) {
+      GstCaps *caps;
+      GstBuffer *buffer;
+      GstSegment *segment;
+      GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
+
+      caps = gst_sample_get_caps (last_sample);
+      buffer = gst_sample_get_buffer (last_sample);
+      segment = gst_sample_get_segment (last_sample);
+
+      if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
+        if (seq) {
+          *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
+        }
+
+        if (rtptime) {
+          *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
+        }
+
+        gst_rtp_buffer_unmap (&rtp_buffer);
+
+        if (running_time) {
+          *running_time =
+              gst_segment_to_running_time (segment, GST_FORMAT_TIME,
+              GST_BUFFER_TIMESTAMP (buffer));
+        }
+
+        if (clock_rate) {
+          GstStructure *s = gst_caps_get_structure (caps, 0);
+
+          gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
+
+          if (*clock_rate == 0 && running_time)
+            *running_time = GST_CLOCK_TIME_NONE;
+        }
+        gst_sample_unref (last_sample);
+
+        goto done;
+      } else {
+        gst_sample_unref (last_sample);
+      }
+    }
+  }
+
   if (g_object_class_find_property (payobjclass, "stats")) {
     g_object_get (priv->payloader, "stats", &stats, NULL);
     if (stats == NULL)
@@ -2472,6 +2719,8 @@ gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
     if (running_time)
       *running_time = GST_CLOCK_TIME_NONE;
   }
+
+done:
   g_mutex_unlock (&priv->lock);
 
   return TRUE;
@@ -2544,6 +2793,31 @@ gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
   g_mutex_unlock (&priv->lock);
 
   if (element) {
+    if (priv->appsrc_base_time[0] == -1) {
+      /* Take current running_time. This timestamp will be put on
+       * the first buffer of each stream because we are a live source and so we
+       * timestamp with the running_time. When we are dealing with TCP, we also
+       * only timestamp the first buffer (using the DISCONT flag) because a server
+       * typically bursts data, for which we don't want to compensate by speeding
+       * up the media. The other timestamps will be interpollated from this one
+       * using the RTP timestamps. */
+      GST_OBJECT_LOCK (element);
+      if (GST_ELEMENT_CLOCK (element)) {
+        GstClockTime now;
+        GstClockTime base_time;
+
+        now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
+        base_time = GST_ELEMENT_CAST (element)->base_time;
+
+        priv->appsrc_base_time[0] = now - base_time;
+        GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
+        GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
+            ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
+            GST_TIME_ARGS (base_time));
+      }
+      GST_OBJECT_UNLOCK (element);
+    }
+
     ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
     gst_object_unref (element);
   } else {
@@ -2587,6 +2861,31 @@ gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
   g_mutex_unlock (&priv->lock);
 
   if (element) {
+    if (priv->appsrc_base_time[1] == -1) {
+      /* Take current running_time. This timestamp will be put on
+       * the first buffer of each stream because we are a live source and so we
+       * timestamp with the running_time. When we are dealing with TCP, we also
+       * only timestamp the first buffer (using the DISCONT flag) because a server
+       * typically bursts data, for which we don't want to compensate by speeding
+       * up the media. The other timestamps will be interpollated from this one
+       * using the RTP timestamps. */
+      GST_OBJECT_LOCK (element);
+      if (GST_ELEMENT_CLOCK (element)) {
+        GstClockTime now;
+        GstClockTime base_time;
+
+        now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
+        base_time = GST_ELEMENT_CAST (element)->base_time;
+
+        priv->appsrc_base_time[1] = now - base_time;
+        GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
+        GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
+            ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
+            GST_TIME_ARGS (base_time));
+      }
+      GST_OBJECT_UNLOCK (element);
+    }
+
     ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
     gst_object_unref (element);
   } else {
@@ -2629,6 +2928,7 @@ update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
           source->udpsrc[i] =
               gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
           g_free (host);
+          g_object_set (source->udpsrc[i], "loop", FALSE, NULL);
 
           if (priv->srcpad) {
             /* we set and keep these to playing so that they don't cause NO_PREROLL return
@@ -2647,7 +2947,6 @@ update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
           gst_object_unref (pad);
           gst_object_unref (selpad);
         }
-        gst_object_unref (bin);
 
         priv->transport_sources =
             g_list_prepend (priv->transport_sources, source);
@@ -2683,6 +2982,8 @@ update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
         }
       }
 
+      gst_object_unref (bin);
+
       /* fall through for the generic case */
     }
     case GST_RTSP_LOWER_TRANS_UDP:
@@ -3154,7 +3455,14 @@ gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
   priv = stream->priv;
 
   g_mutex_lock (&priv->lock);
-  if ((sink = priv->udpsink[0]))
+  /* depending on the transport type, it should query corresponding sink */
+  if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
+      (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
+    sink = priv->udpsink[0];
+  else
+    sink = priv->appsink[0];
+
+  if (sink)
     gst_object_ref (sink);
   g_mutex_unlock (&priv->lock);
 
@@ -3189,7 +3497,14 @@ gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
   priv = stream->priv;
 
   g_mutex_lock (&priv->lock);
-  if ((sink = priv->udpsink[0]))
+  /* depending on the transport type, it should query corresponding sink */
+  if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
+      (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
+    sink = priv->udpsink[0];
+  else
+    sink = priv->appsink[0];
+
+  if (sink)
     gst_object_ref (sink);
   g_mutex_unlock (&priv->lock);