jitterbuffer: signal timestamp discont
[platform/upstream/gstreamer.git] / gst / rtpmanager / gstrtpbin.c
index e9b3164..37d6d19 100644 (file)
@@ -13,8 +13,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
  * #GstRtpBin is configured with a number of request pads that define the
  * functionality that is activated, similar to the #GstRtpSession element.
  *
- * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%d pad. The session
+ * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
  * number must be specified in the pad name.
- * Data received on the recv_rtp_sink_\%d pad will be processed in the #GstRtpSession
+ * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
  * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
  * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
  * the packets are released from the jitterbuffer, they will be forwarded to a
  * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
- * on the payload type and will create a unique pad recv_rtp_src_\%d_\%d_\%d on
+ * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
  * gstrtpbin with the session number, SSRC and payload type respectively as the pad
  * name.
  *
- * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%d pad. The
+ * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
  * session number must be specified in the pad name.
  *
  * If you want the session manager to generate and send RTCP packets, request
- * the send_rtcp_src_\%d pad with the session number in the pad name. Packet pushed
+ * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
  * on this pad contain SR/RR RTCP reports that should be sent to all participants
  * in the session.
  *
- * To use #GstRtpBin as a sender, request a send_rtp_sink_\%d pad, which will
- * automatically create a send_rtp_src_\%d pad. If the session number is not provided,
+ * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
+ * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
  * the pad from the lowest available session will be returned. The session manager will modify the
  * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
- * send_rtp_src_\%d pad after updating its internal state.
+ * send_rtp_src_\%u pad after updating its internal state.
  *
  * The session manager needs the clock-rate of the payload types it is handling
  * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
  * <refsect2>
  * <title>Example pipelines</title>
  * |[
- * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
+ * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
  *     gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
  * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
  * |[
- * gst-launch gstrtpbin name=rtpbin \
- *         v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
+ * gst-launch-1.0 gstrtpbin name=rtpbin \
+ *         v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
  *                   rtpbin.send_rtp_src_0 ! udpsink port=5000                            \
  *                   rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false    \
  *                   udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0                           \
@@ -89,7 +89,7 @@
  * as soon as possible and do not participate in preroll, sync=false and
  * async=false is configured on udpsink
  * |[
- * gst-launch -v gstrtpbin name=rtpbin                                          \
+ * gst-launch-1.0 -v gstrtpbin name=rtpbin                                          \
  *     udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
  *             port=5000 ! rtpbin.recv_rtp_sink_0                                \
  *         rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink                    \
 #include "gstrtpsession.h"
 #include "gstrtpjitterbuffer.h"
 
+#include <gst/glib-compat-private.h>
+
 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
 #define GST_CAT_DEFAULT gst_rtp_bin_debug
 
 /* sink pads */
 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
-GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
+GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
     GST_PAD_SINK,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS ("application/x-rtp")
     );
 
 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
-GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
+GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
     GST_PAD_SINK,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS ("application/x-rtcp")
     );
 
 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
-GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
+GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
     GST_PAD_SINK,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS ("application/x-rtp")
@@ -156,21 +158,21 @@ GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
 
 /* src pads */
 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
-GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
+GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
     GST_PAD_SRC,
     GST_PAD_SOMETIMES,
     GST_STATIC_CAPS ("application/x-rtp")
     );
 
 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
-GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
+GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
     GST_PAD_SRC,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS ("application/x-rtcp")
     );
 
 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
-GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
+GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
     GST_PAD_SRC,
     GST_PAD_SOMETIMES,
     GST_STATIC_CAPS ("application/x-rtp")
@@ -179,12 +181,12 @@ GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
 #define GST_RTP_BIN_GET_PRIVATE(obj)  \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
 
-#define GST_RTP_BIN_LOCK(bin)   g_mutex_lock ((bin)->priv->bin_lock)
-#define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
+#define GST_RTP_BIN_LOCK(bin)   g_mutex_lock (&(bin)->priv->bin_lock)
+#define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
 
 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
-#define GST_RTP_BIN_DYN_LOCK(bin)    g_mutex_lock ((bin)->priv->dyn_lock)
-#define GST_RTP_BIN_DYN_UNLOCK(bin)  g_mutex_unlock ((bin)->priv->dyn_lock)
+#define GST_RTP_BIN_DYN_LOCK(bin)    g_mutex_lock (&(bin)->priv->dyn_lock)
+#define GST_RTP_BIN_DYN_UNLOCK(bin)  g_mutex_unlock (&(bin)->priv->dyn_lock)
 
 /* lock for shutdown */
 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label)     \
@@ -204,10 +206,10 @@ G_STMT_START {                                   \
 
 struct _GstRtpBinPrivate
 {
-  GMutex *bin_lock;
+  GMutex bin_lock;
 
   /* lock protecting dynamic adding/removing */
-  GMutex *dyn_lock;
+  GMutex dyn_lock;
 
   /* if we are shutting down or not */
   gint shutdown;
@@ -241,6 +243,7 @@ enum
 };
 
 #define DEFAULT_LATENCY_MS           200
+#define DEFAULT_DROP_ON_LATENCY      FALSE
 #define DEFAULT_SDES                 NULL
 #define DEFAULT_DO_LOST              FALSE
 #define DEFAULT_IGNORE_PT            FALSE
@@ -248,16 +251,19 @@ enum
 #define DEFAULT_AUTOREMOVE           FALSE
 #define DEFAULT_BUFFER_MODE          RTP_JITTER_BUFFER_MODE_SLAVE
 #define DEFAULT_USE_PIPELINE_CLOCK   FALSE
+#define DEFAULT_RTCP_SYNC            GST_RTP_BIN_RTCP_SYNC_ALWAYS
 #define DEFAULT_RTCP_SYNC_INTERVAL   0
 
 enum
 {
   PROP_0,
   PROP_LATENCY,
+  PROP_DROP_ON_LATENCY,
   PROP_SDES,
   PROP_DO_LOST,
   PROP_IGNORE_PT,
   PROP_NTP_SYNC,
+  PROP_RTCP_SYNC,
   PROP_RTCP_SYNC_INTERVAL,
   PROP_AUTOREMOVE,
   PROP_BUFFER_MODE,
@@ -265,6 +271,31 @@ enum
   PROP_LAST
 };
 
+enum
+{
+  GST_RTP_BIN_RTCP_SYNC_ALWAYS,
+  GST_RTP_BIN_RTCP_SYNC_INITIAL,
+  GST_RTP_BIN_RTCP_SYNC_RTP
+};
+
+#define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
+static GType
+gst_rtp_bin_rtcp_sync_get_type (void)
+{
+  static GType rtcp_sync_type = 0;
+  static const GEnumValue rtcp_sync_types[] = {
+    {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
+    {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
+    {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
+    {0, NULL, NULL},
+  };
+
+  if (!rtcp_sync_type) {
+    rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
+  }
+  return rtcp_sync_type;
+}
+
 /* helper objects */
 typedef struct _GstRtpBinSession GstRtpBinSession;
 typedef struct _GstRtpBinStream GstRtpBinStream;
@@ -276,8 +307,12 @@ static GstCaps *pt_map_requested (GstElement * element, guint pt,
     GstRtpBinSession * session);
 static void payload_type_change (GstElement * element, guint pt,
     GstRtpBinSession * session);
+static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
+static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
+static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
+static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
-static void free_stream (GstRtpBinStream * stream);
+static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
 
 /* Manages the RTP stream for one SSRC.
  *
@@ -315,10 +350,13 @@ struct _GstRtpBinStream
   gboolean have_sync;
   /* mapping to local RTP and NTP time */
   gint64 rt_delta;
+  gint64 rtp_delta;
+  /* base rtptime in gst time */
+  gint64 clock_base;
 };
 
-#define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock ((sess)->lock)
-#define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
+#define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock (&(sess)->lock)
+#define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
 
 /* Manages the receiving end of the packets.
  *
@@ -341,7 +379,7 @@ struct _GstRtpBinSession
   gulong demux_newpad_sig;
   gulong demux_padremoved_sig;
 
-  GMutex *lock;
+  GMutex lock;
 
   /* list of GstRtpBinStream */
   GSList *streams;
@@ -507,6 +545,11 @@ ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
     GstRtpBinSession * session)
 {
   GstRtpBinStream *stream = NULL;
+  GstRtpBin *rtpbin;
+
+  rtpbin = session->bin;
+
+  GST_RTP_BIN_LOCK (rtpbin);
 
   GST_RTP_SESSION_LOCK (session);
   if ((stream = find_stream_by_ssrc (session, ssrc)))
@@ -514,7 +557,9 @@ ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
   GST_RTP_SESSION_UNLOCK (session);
 
   if (stream)
-    free_stream (stream);
+    free_stream (stream, rtpbin);
+
+  GST_RTP_BIN_UNLOCK (rtpbin);
 }
 
 /* create a session with the given id.  Must be called with RTP_BIN_LOCK */
@@ -525,14 +570,14 @@ create_session (GstRtpBin * rtpbin, gint id)
   GstElement *session, *demux;
   GstState target;
 
-  if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
+  if (!(session = gst_element_factory_make ("rtpsession", NULL)))
     goto no_session;
 
-  if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
+  if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
     goto no_demux;
 
   sess = g_new0 (GstRtpBinSession, 1);
-  sess->lock = g_mutex_new ();
+  g_mutex_init (&sess->lock);
   sess->id = id;
   sess->bin = rtpbin;
   sess->session = session;
@@ -585,22 +630,21 @@ create_session (GstRtpBin * rtpbin, gint id)
   /* ERRORS */
 no_session:
   {
-    g_warning ("gstrtpbin: could not create gstrtpsession element");
+    g_warning ("rtpbin: could not create gstrtpsession element");
     return NULL;
   }
 no_demux:
   {
     gst_object_unref (session);
-    g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
+    g_warning ("rtpbin: could not create gstrtpssrcdemux element");
     return NULL;
   }
 }
 
+/* called with RTP_BIN_LOCK */
 static void
 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
 {
-  GSList *client_walk;
-
   GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
 
   gst_element_set_locked_state (sess->demux, TRUE);
@@ -609,69 +653,18 @@ free_session (GstRtpBinSession * sess, GstRtpBin * bin)
   gst_element_set_state (sess->demux, GST_STATE_NULL);
   gst_element_set_state (sess->session, GST_STATE_NULL);
 
-  if (sess->recv_rtp_sink != NULL) {
-    gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
-    gst_object_unref (sess->recv_rtp_sink);
-  }
-  if (sess->recv_rtp_src != NULL)
-    gst_object_unref (sess->recv_rtp_src);
-  if (sess->recv_rtcp_sink != NULL) {
-    gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
-    gst_object_unref (sess->recv_rtcp_sink);
-  }
-  if (sess->sync_src != NULL)
-    gst_object_unref (sess->sync_src);
-  if (sess->send_rtp_sink != NULL) {
-    gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
-    gst_object_unref (sess->send_rtp_sink);
-  }
-  if (sess->send_rtp_src != NULL)
-    gst_object_unref (sess->send_rtp_src);
-  if (sess->send_rtcp_src != NULL) {
-    gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
-    gst_object_unref (sess->send_rtcp_src);
-  }
+  remove_recv_rtp (bin, sess);
+  remove_recv_rtcp (bin, sess);
+  remove_send_rtp (bin, sess);
+  remove_rtcp (bin, sess);
 
   gst_bin_remove (GST_BIN_CAST (bin), sess->session);
   gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
 
-  /* remove any references in bin->clients to the streams in sess->streams */
-  client_walk = bin->clients;
-  while (client_walk) {
-    GSList *client_node = client_walk;
-    GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
-    GSList *stream_walk = client->streams;
-
-    while (stream_walk) {
-      GSList *stream_node = stream_walk;
-      GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
-      GSList *inner_walk;
-
-      stream_walk = g_slist_next (stream_walk);
-
-      for (inner_walk = sess->streams; inner_walk;
-          inner_walk = g_slist_next (inner_walk)) {
-        if ((GstRtpBinStream *) inner_walk->data == stream) {
-          client->streams = g_slist_delete_link (client->streams, stream_node);
-          --client->nstreams;
-          break;
-        }
-      }
-    }
-    client_walk = g_slist_next (client_walk);
-
-    g_assert ((client->streams && client->nstreams > 0) || (!client->streams
-            && client->streams == 0));
-    if (client->nstreams == 0) {
-      free_client (client, bin);
-      bin->clients = g_slist_delete_link (bin->clients, client_node);
-    }
-  }
-
-  g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
+  g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
   g_slist_free (sess->streams);
 
-  g_mutex_free (sess->lock);
+  g_mutex_clear (&sess->lock);
   g_hash_table_destroy (sess->ptmap);
 
   g_free (sess);
@@ -780,6 +773,8 @@ gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
        * lip-sync */
       stream->have_sync = FALSE;
       stream->rt_delta = 0;
+      stream->rtp_delta = 0;
+      stream->clock_base = -100 * GST_SECOND;
     }
   }
   GST_RTP_BIN_UNLOCK (rtpbin);
@@ -916,7 +911,7 @@ get_current_times (GstRtpBin * bin, GstClockTime * running_time,
     clock_time = gst_clock_get_time (clock);
 
     if (bin->use_pipeline_clock) {
-      ntpns = clock_time;
+      ntpns = clock_time - base_time;
     } else {
       GTimeVal current;
 
@@ -945,7 +940,7 @@ get_current_times (GstRtpBin * bin, GstClockTime * running_time,
 
 static void
 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
-    gint64 ts_offset)
+    gint64 ts_offset, gboolean check)
 {
   gint64 prev_ts_offset;
 
@@ -961,18 +956,20 @@ stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
         "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
         ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
 
-    /* only change diff when it changed more than 4 milliseconds. This
-     * compensates for rounding errors in NTP to RTP timestamp
-     * conversions */
-    if (ABS (diff) > 4 * GST_MSECOND) {
-      if (ABS (diff) < (3 * GST_SECOND)) {
-        g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
-      } else {
+    if (check) {
+      /* only change diff when it changed more than 4 milliseconds. This
+       * compensates for rounding errors in NTP to RTP timestamp
+       * conversions */
+      if (ABS (diff) < 4 * GST_MSECOND) {
+        GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
+        return;
+      }
+      if (ABS (diff) > (3 * GST_SECOND)) {
         GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
+        return;
       }
-    } else {
-      GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
     }
+    g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
   }
   GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
       stream->ssrc, ts_offset);
@@ -984,7 +981,8 @@ stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
 static void
 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
     guint8 * data, guint64 ntptime, guint64 last_extrtptime,
-    guint64 base_rtptime, guint64 base_time, guint clock_rate)
+    guint64 base_rtptime, guint64 base_time, guint clock_rate,
+    gint64 rtp_clock_base)
 {
   GstRtpBinClient *client;
   gboolean created;
@@ -1019,6 +1017,19 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
         stream->ssrc, client, client->cname);
   }
 
+  if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
+    GST_DEBUG_OBJECT (bin, "invalidated sync data");
+    if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
+      /* we don't need that data, so carry on,
+       * but make some values look saner */
+      last_extrtptime = base_rtptime;
+    } else {
+      /* nothing we can do with this data in this case */
+      GST_DEBUG_OBJECT (bin, "bailing out");
+      return;
+    }
+  }
+
   /* Take the extended rtptime we found in the SR packet and map it to the
    * local rtptime. The local rtp time is used to construct timestamps on the
    * buffers so we will calculate what running_time corresponds to the RTP
@@ -1027,8 +1038,9 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
 
   GST_DEBUG_OBJECT (bin,
       "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
-      ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", base_rtptime,
-      last_extrtptime, local_rtp, clock_rate);
+      ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
+      "clock-base %" G_GINT64_FORMAT, base_rtptime,
+      last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
 
   /* calculate local RTP time in gstreamer timestamp, we essentially perform the
    * same conversion that a jitterbuffer would use to convert an rtp timestamp
@@ -1074,9 +1086,11 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
     /* combine to get the final diff to apply to the running_time */
     stream->rt_delta = rtdiff - ntpdiff;
 
-    stream_set_ts_offset (bin, stream, stream->rt_delta);
-  } else if (client->nstreams > 1) {
-    gint64 min;
+    stream_set_ts_offset (bin, stream, stream->rt_delta, FALSE);
+  } else {
+    gint64 min, rtp_min, clock_base = stream->clock_base;
+    gboolean all_sync, use_rtp;
+    gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
 
     /* calculate delta between server and receiver. last_unix is created by
      * converting the ntptime in the last SR packet to a gstreamer timestamp. This
@@ -1094,19 +1108,107 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
      * latencies).
      * The stream that has the smallest diff is selected as the reference stream,
      * all other streams will have a positive offset to this difference. */
-    min = G_MAXINT64;
+
+    /* some alternative setting allow ignoring RTCP as much as possible,
+     * for servers generating bogus ntp timeline */
+    min = rtp_min = G_MAXINT64;
+    use_rtp = FALSE;
+    if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
+      guint64 ext_base;
+
+      use_rtp = TRUE;
+      /* signed version for convienience */
+      clock_base = base_rtptime;
+      /* deal with possible wrap-around */
+      ext_base = base_rtptime;
+      rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
+      /* sanity check; base rtp and provided clock_base should be close */
+      if (rtp_clock_base >= clock_base) {
+        if (rtp_clock_base - clock_base < 10 * clock_rate) {
+          rtp_clock_base = base_time +
+              gst_util_uint64_scale_int (rtp_clock_base - clock_base,
+              GST_SECOND, clock_rate);
+        } else {
+          use_rtp = FALSE;
+        }
+      } else {
+        if (clock_base - rtp_clock_base < 10 * clock_rate) {
+          rtp_clock_base = base_time -
+              gst_util_uint64_scale_int (clock_base - rtp_clock_base,
+              GST_SECOND, clock_rate);
+        } else {
+          use_rtp = FALSE;
+        }
+      }
+      /* warn and bail for clarity out if no sane values */
+      if (!use_rtp) {
+        GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
+        return;
+      }
+      /* store to track changes */
+      clock_base = rtp_clock_base;
+      /* generate a fake as before,
+       * now equating rtptime obtained from RTP-Info,
+       * where the large time represent the otherwise irrelevant npt/ntp time */
+      stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
+    } else {
+      clock_base = rtp_clock_base;
+    }
+
+    all_sync = TRUE;
     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
 
-      if (!ostream->have_sync)
+      if (!ostream->have_sync) {
+        all_sync = FALSE;
         continue;
+      }
+
+      /* change in current stream's base from previously init'ed value
+       * leads to reset of all stream's base */
+      if (stream != ostream && stream->clock_base >= 0 &&
+          (stream->clock_base != clock_base)) {
+        GST_DEBUG_OBJECT (bin, "reset upon clock base change");
+        ostream->clock_base = -100 * GST_SECOND;
+        ostream->rtp_delta = 0;
+      }
 
       if (ostream->rt_delta < min)
         min = ostream->rt_delta;
+      if (ostream->rtp_delta < rtp_min)
+        rtp_min = ostream->rtp_delta;
     }
 
-    GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
-        min);
+    /* arrange to re-sync for each stream upon significant change,
+     * e.g. post-seek */
+    all_sync = all_sync && (stream->clock_base == clock_base);
+    stream->clock_base = clock_base;
+
+    /* may need init performed above later on, but nothing more to do now */
+    if (client->nstreams <= 1)
+      return;
+
+    GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
+        " all sync %d", client, min, all_sync);
+    GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
+
+    switch (rtcp_sync) {
+      case GST_RTP_BIN_RTCP_SYNC_RTP:
+        if (!use_rtp)
+          break;
+        GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
+            "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
+        /* fall-through */
+      case GST_RTP_BIN_RTCP_SYNC_INITIAL:
+        /* if all have been synced already, do not bother further */
+        if (all_sync) {
+          GST_DEBUG_OBJECT (bin, "all streams already synced; done");
+          return;
+        }
+        break;
+      default:
+        break;
+    }
 
     /* bail out if we adjusted recently enough */
     if (all_sync && (last_unix - bin->priv->last_unix) <
@@ -1131,9 +1233,12 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
 
       /* calculate offset to our reference stream, this should always give a
        * positive number. */
-      ts_offset = ostream->rt_delta - min;
+      if (use_rtp)
+        ts_offset = ostream->rtp_delta - rtp_min;
+      else
+        ts_offset = ostream->rt_delta - min;
 
-      stream_set_ts_offset (bin, ostream, ts_offset);
+      stream_set_ts_offset (bin, ostream, ts_offset, TRUE);
     }
   }
   return;
@@ -1164,8 +1269,10 @@ gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
   guint64 base_rtptime;
   guint64 base_time;
   guint clock_rate;
+  guint64 clock_base;
   guint64 extrtptime;
   GstBuffer *buffer;
+  GstRTCPBuffer rtcp = { NULL, };
 
   bin = stream->bin;
 
@@ -1179,13 +1286,17 @@ gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
       g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
   base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
   clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
+  clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
   extrtptime =
       g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
   buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
 
   have_sr = FALSE;
   have_sdes = FALSE;
-  GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
+
+  gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
+
+  GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
     /* first packet must be SR or RR or else the validate would have failed */
     switch (gst_rtcp_packet_get_type (&packet)) {
       case GST_RTCP_TYPE_SR:
@@ -1231,7 +1342,8 @@ gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
               GST_RTP_BIN_LOCK (bin);
               /* associate the stream to CNAME */
               gst_rtp_bin_associate (bin, stream, len, data,
-                  ntptime, extrtptime, base_rtptime, base_time, clock_rate);
+                  ntptime, extrtptime, base_rtptime, base_time, clock_rate,
+                  clock_base);
               GST_RTP_BIN_UNLOCK (bin);
             }
           }
@@ -1244,6 +1356,7 @@ gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
         break;
     }
   }
+  gst_rtcp_buffer_unmap (&rtcp);
 }
 
 /* create a new stream with @ssrc in @session. Must be called with
@@ -1258,11 +1371,11 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
 
   rtpbin = session->bin;
 
-  if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
+  if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
     goto no_jitterbuffer;
 
   if (!rtpbin->ignore_pt)
-    if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
+    if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
       goto no_demux;
 
 
@@ -1275,7 +1388,9 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
 
   stream->have_sync = FALSE;
   stream->rt_delta = 0;
+  stream->rtp_delta = 0;
   stream->percent = 100;
+  stream->clock_base = -100 * GST_SECOND;
   session->streams = g_slist_prepend (session->streams, stream);
 
   /* provide clock_rate to the jitterbuffer when needed */
@@ -1289,6 +1404,7 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
 
   /* configure latency and packet lost */
   g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
+  g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
   g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
   g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
 
@@ -1324,23 +1440,24 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
   /* ERRORS */
 no_jitterbuffer:
   {
-    g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
+    g_warning ("rtpbin: could not create gstrtpjitterbuffer element");
     return NULL;
   }
 no_demux:
   {
     gst_object_unref (buffer);
-    g_warning ("gstrtpbin: could not create gstrtpptdemux element");
+    g_warning ("rtpbin: could not create gstrtpptdemux element");
     return NULL;
   }
 }
 
+/* called with RTP_BIN_LOCK */
 static void
-free_stream (GstRtpBinStream * stream)
+free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
 {
-  GstRtpBinSession *session;
+  GSList *clients, *next_client;
 
-  session = stream->session;
+  GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
 
   if (stream->demux) {
     g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
@@ -1364,10 +1481,33 @@ free_stream (GstRtpBinStream * stream)
   if (stream->demux)
     g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
 
-  gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
+  gst_bin_remove (GST_BIN_CAST (bin), stream->buffer);
   if (stream->demux)
-    gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
+    gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
+
+  for (clients = bin->clients; clients; clients = next_client) {
+    GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
+    GSList *streams, *next_stream;
+
+    next_client = g_slist_next (clients);
 
+    for (streams = client->streams; streams; streams = next_stream) {
+      GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
+
+      next_stream = g_slist_next (streams);
+
+      if (ostream == stream) {
+        client->streams = g_slist_delete_link (client->streams, streams);
+        /* If this was the last stream belonging to this client,
+         * clean up the client. */
+        if (--client->nstreams == 0) {
+          bin->clients = g_slist_delete_link (bin->clients, clients);
+          free_client (client, bin);
+          break;
+        }
+      }
+    }
+  }
   g_free (stream);
 }
 
@@ -1383,38 +1523,12 @@ static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
     GstStateChange transition);
 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * name);
+    GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
 
-GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
-
-static void
-gst_rtp_bin_base_init (gpointer klass)
-{
-  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
-
-  /* sink pads */
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
-
-  /* src pads */
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
-
-  gst_element_class_set_details_simple (element_class, "RTP Bin",
-      "Filter/Network/RTP",
-      "Real-Time Transport Protocol bin",
-      "Wim Taymans <wim.taymans@gmail.com>");
-}
+#define gst_rtp_bin_parent_class parent_class
+G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
 
 static void
 gst_rtp_bin_class_init (GstRtpBinClass * klass)
@@ -1440,6 +1554,12 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
           G_MAXUINT, DEFAULT_LATENCY_MS,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
+      g_param_spec_boolean ("drop-on-latency",
+          "Drop buffers when maximum latency is reached",
+          "Tells the jitterbuffer to never exceed the given latency in size",
+          DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   /**
    * GstRtpBin::request-pt-map:
    * @rtpbin: the object which received the signal
@@ -1664,7 +1784,7 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
 
   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
-          "Use the pipeline clock to set the NTP time in the RTCP SR messages",
+          "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
           DEFAULT_USE_PIPELINE_CLOCK,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   /**
@@ -1681,9 +1801,11 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
   /**
    * GstRtpBin::ntp-sync:
    *
-   * Synchronize received streams to the NTP clock. When the NTP clock is shared
-   * between the receivers and the senders (such as when using ntpd) this option
-   * can be used to synchronize receivers on multiple machines.
+   * Set the NTP time from the sender reports as the running-time on the
+   * buffers. When both the sender and receiver have sychronized
+   * running-time, i.e. when the clock and base-time is shared
+   * between the receivers and the and the senders, this option can be
+   * used to synchronize receivers on multiple machines.
    *
    * Since: 0.10.21
    */
@@ -1693,6 +1815,19 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /**
+   * GstRtpBin::rtcp-sync:
+   *
+   * If not synchronizing (directly) to the NTP clock, determines how to sync
+   * the various streams.
+   *
+   * Since: 0.10.31
+   */
+  g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
+      g_param_spec_enum ("rtcp-sync", "RTCP Sync",
+          "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
+          DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
    * GstRtpBin::rtcp-sync-interval:
    *
    * Determines how often to sync streams using RTCP data.
@@ -1710,6 +1845,27 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
 
+  /* sink pads */
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
+
+  /* src pads */
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
+
+  gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
+      "Filter/Network/RTP",
+      "Real-Time Transport Protocol bin",
+      "Wim Taymans <wim.taymans@gmail.com>");
+
   gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
 
   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
@@ -1721,31 +1877,31 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
 }
 
 static void
-gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
+gst_rtp_bin_init (GstRtpBin * rtpbin)
 {
-  gchar *str;
+  gchar *cname;
 
   rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
-  rtpbin->priv->bin_lock = g_mutex_new ();
-  rtpbin->priv->dyn_lock = g_mutex_new ();
+  g_mutex_init (&rtpbin->priv->bin_lock);
+  g_mutex_init (&rtpbin->priv->dyn_lock);
 
   rtpbin->latency_ms = DEFAULT_LATENCY_MS;
   rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
+  rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
   rtpbin->do_lost = DEFAULT_DO_LOST;
   rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
   rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
+  rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
   rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
   rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
   rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
   rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
 
   /* some default SDES entries */
-  str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
+  cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
   rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
-      "cname", G_TYPE_STRING, str,
-      "name", G_TYPE_STRING, g_get_real_name (),
-      "tool", G_TYPE_STRING, "GStreamer", NULL);
-  g_free (str);
+      "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
+  g_free (cname);
 }
 
 static void
@@ -1755,14 +1911,12 @@ gst_rtp_bin_dispose (GObject * object)
 
   rtpbin = GST_RTP_BIN (object);
 
+  GST_RTP_BIN_LOCK (rtpbin);
   GST_DEBUG_OBJECT (object, "freeing sessions");
   g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
   g_slist_free (rtpbin->sessions);
   rtpbin->sessions = NULL;
-  GST_DEBUG_OBJECT (object, "freeing clients");
-  g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
-  g_slist_free (rtpbin->clients);
-  rtpbin->clients = NULL;
+  GST_RTP_BIN_UNLOCK (rtpbin);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -1777,8 +1931,8 @@ gst_rtp_bin_finalize (GObject * object)
   if (rtpbin->sdes)
     gst_structure_free (rtpbin->sdes);
 
-  g_mutex_free (rtpbin->priv->bin_lock);
-  g_mutex_free (rtpbin->priv->dyn_lock);
+  g_mutex_clear (&rtpbin->priv->bin_lock);
+  g_mutex_clear (&rtpbin->priv->dyn_lock);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -1838,6 +1992,14 @@ gst_rtp_bin_set_property (GObject * object, guint prop_id,
       /* propagate the property down to the jitterbuffer */
       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
       break;
+    case PROP_DROP_ON_LATENCY:
+      GST_RTP_BIN_LOCK (rtpbin);
+      rtpbin->drop_on_latency = g_value_get_boolean (value);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      /* propagate the property down to the jitterbuffer */
+      gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
+          "drop-on-latency", value);
+      break;
     case PROP_SDES:
       gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
       break;
@@ -1850,6 +2012,9 @@ gst_rtp_bin_set_property (GObject * object, guint prop_id,
     case PROP_NTP_SYNC:
       rtpbin->ntp_sync = g_value_get_boolean (value);
       break;
+    case PROP_RTCP_SYNC:
+      g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
+      break;
     case PROP_RTCP_SYNC_INTERVAL:
       rtpbin->rtcp_sync_interval = g_value_get_uint (value);
       break;
@@ -1901,6 +2066,11 @@ gst_rtp_bin_get_property (GObject * object, guint prop_id,
       g_value_set_uint (value, rtpbin->latency_ms);
       GST_RTP_BIN_UNLOCK (rtpbin);
       break;
+    case PROP_DROP_ON_LATENCY:
+      GST_RTP_BIN_LOCK (rtpbin);
+      g_value_set_boolean (value, rtpbin->drop_on_latency);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      break;
     case PROP_SDES:
       g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
       break;
@@ -1915,6 +2085,9 @@ gst_rtp_bin_get_property (GObject * object, guint prop_id,
     case PROP_NTP_SYNC:
       g_value_set_boolean (value, rtpbin->ntp_sync);
       break;
+    case PROP_RTCP_SYNC:
+      g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
+      break;
     case PROP_RTCP_SYNC_INTERVAL:
       g_value_set_uint (value, rtpbin->rtcp_sync_interval);
       break;
@@ -2055,6 +2228,7 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
             now = gst_clock_get_time (clock);
             base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
             running_time = now - base_time;
+            gst_object_unref (clock);
           }
           GST_DEBUG_OBJECT (bin,
               "running time now %" GST_TIME_FORMAT,
@@ -2195,14 +2369,13 @@ new_payload_found (GstElement * element, guint pt, GstPad * pad,
 
   /* ghost the pad to the parent */
   klass = GST_ELEMENT_GET_CLASS (rtpbin);
-  templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
-  padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
+  templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
+  padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
       stream->session->id, stream->ssrc, pt);
   gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
   g_free (padname);
   g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
 
-  gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
   gst_pad_set_active (gpad, TRUE);
   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
 
@@ -2330,7 +2503,7 @@ new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
 
   /* get pad and link */
   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
-  padname = g_strdup_printf ("src_%d", ssrc);
+  padname = g_strdup_printf ("src_%u", ssrc);
   srcpad = gst_element_get_static_pad (element, padname);
   g_free (padname);
   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
@@ -2339,7 +2512,7 @@ new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
   gst_object_unref (srcpad);
 
   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
-  padname = g_strdup_printf ("rtcp_src_%d", ssrc);
+  padname = g_strdup_printf ("rtcp_src_%u", ssrc);
   srcpad = gst_element_get_static_pad (element, padname);
   g_free (padname);
   sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
@@ -2379,13 +2552,12 @@ new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
 
     /* ghost the pad to the parent */
     klass = GST_ELEMENT_GET_CLASS (rtpbin);
-    templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
-    padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
+    templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
+    padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
         stream->session->id, stream->ssrc, 255);
     gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
     g_free (padname);
 
-    gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
     gst_pad_set_active (gpad, TRUE);
     gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
 
@@ -2424,7 +2596,7 @@ create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   GstPadLinkReturn lres;
 
   /* first get the session number */
-  if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
+  if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
     goto no_name;
 
   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
@@ -2485,7 +2657,7 @@ create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   /* ERRORS */
 no_name:
   {
-    g_warning ("gstrtpbin: invalid name given");
+    g_warning ("rtpbin: invalid name given");
     return NULL;
   }
 create_error:
@@ -2495,12 +2667,12 @@ create_error:
   }
 pad_failed:
   {
-    g_warning ("gstrtpbin: failed to get session pad");
+    g_warning ("rtpbin: failed to get session pad");
     return NULL;
   }
 link_failed:
   {
-    g_warning ("gstrtpbin: failed to link pads");
+    g_warning ("rtpbin: failed to link pads");
     return NULL;
   }
 }
@@ -2546,7 +2718,7 @@ create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
   GstPadLinkReturn lres;
 
   /* first get the session number */
-  if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
+  if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
     goto no_name;
 
   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
@@ -2596,7 +2768,7 @@ create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
   /* ERRORS */
 no_name:
   {
-    g_warning ("gstrtpbin: invalid name given");
+    g_warning ("rtpbin: invalid name given");
     return NULL;
   }
 create_error:
@@ -2606,12 +2778,12 @@ create_error:
   }
 pad_failed:
   {
-    g_warning ("gstrtpbin: failed to get session pad");
+    g_warning ("rtpbin: failed to get session pad");
     return NULL;
   }
 link_failed:
   {
-    g_warning ("gstrtpbin: failed to link pads");
+    g_warning ("rtpbin: failed to link pads");
     return NULL;
   }
 }
@@ -2649,7 +2821,7 @@ create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   GstElementClass *klass;
 
   /* first get the session number */
-  if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
+  if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
     goto no_name;
 
   /* get or create session */
@@ -2684,8 +2856,8 @@ create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
 
   /* ghost the new source pad */
   klass = GST_ELEMENT_GET_CLASS (rtpbin);
-  gname = g_strdup_printf ("send_rtp_src_%d", sessid);
-  templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
+  gname = g_strdup_printf ("send_rtp_src_%u", sessid);
+  templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
   session->send_rtp_src_ghost =
       gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
   gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
@@ -2697,7 +2869,7 @@ create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   /* ERRORS */
 no_name:
   {
-    g_warning ("gstrtpbin: invalid name given");
+    g_warning ("rtpbin: invalid name given");
     return NULL;
   }
 create_error:
@@ -2707,13 +2879,12 @@ create_error:
   }
 pad_failed:
   {
-    g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
+    g_warning ("rtpbin: failed to get session pad for session %d", sessid);
     return NULL;
   }
 no_srcpad:
   {
-    g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
-        sessid);
+    g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
     return NULL;
   }
 }
@@ -2755,7 +2926,7 @@ create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   GstRtpBinSession *session;
 
   /* first get the session number */
-  if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
+  if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
     goto no_name;
 
   /* get or create session */
@@ -2783,17 +2954,17 @@ create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
   /* ERRORS */
 no_name:
   {
-    g_warning ("gstrtpbin: invalid name given");
+    g_warning ("rtpbin: invalid name given");
     return NULL;
   }
 no_session:
   {
-    g_warning ("gstrtpbin: session with id %d does not exist", sessid);
+    g_warning ("rtpbin: session with id %d does not exist", sessid);
     return NULL;
   }
 pad_failed:
   {
-    g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
+    g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
     return NULL;
   }
 }
@@ -2824,24 +2995,24 @@ gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
   gint session = 0;
   GstIterator *pad_it = NULL;
   gchar *pad_name = NULL;
+  GValue data = { 0, };
 
   GST_DEBUG_OBJECT (element, "find a free pad name for template");
   while (!name_found) {
     gboolean done = FALSE;
+
     g_free (pad_name);
     pad_name = g_strdup_printf (templ->name_template, session++);
     pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
     name_found = TRUE;
     while (!done) {
-      gpointer data;
-
       switch (gst_iterator_next (pad_it, &data)) {
         case GST_ITERATOR_OK:
         {
           GstPad *pad;
           gchar *name;
 
-          pad = GST_PAD_CAST (data);
+          pad = g_value_get_object (&data);
           name = gst_pad_get_name (pad);
 
           if (strcmp (name, pad_name) == 0) {
@@ -2849,7 +3020,7 @@ gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
             name_found = FALSE;
           }
           g_free (name);
-          gst_object_unref (pad);
+          g_value_reset (&data);
           break;
         }
         case GST_ITERATOR_ERROR:
@@ -2864,6 +3035,7 @@ gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
           break;
       }
     }
+    g_value_unset (&data);
     gst_iterator_free (pad_it);
   }
 
@@ -2875,7 +3047,7 @@ gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
  */
 static GstPad *
 gst_rtp_bin_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * name)
+    GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
 {
   GstRtpBin *rtpbin;
   GstElementClass *klass;
@@ -2902,16 +3074,16 @@ gst_rtp_bin_request_new_pad (GstElement * element,
   GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
 
   /* figure out the template */
-  if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
+  if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
     result = create_recv_rtp (rtpbin, templ, pad_name);
   } else if (templ == gst_element_class_get_pad_template (klass,
-          "recv_rtcp_sink_%d")) {
+          "recv_rtcp_sink_%u")) {
     result = create_recv_rtcp (rtpbin, templ, pad_name);
   } else if (templ == gst_element_class_get_pad_template (klass,
-          "send_rtp_sink_%d")) {
+          "send_rtp_sink_%u")) {
     result = create_send_rtp (rtpbin, templ, pad_name);
   } else if (templ == gst_element_class_get_pad_template (klass,
-          "send_rtcp_src_%d")) {
+          "send_rtcp_src_%u")) {
     result = create_rtcp (rtpbin, templ, pad_name);
   } else
     goto wrong_template;
@@ -2926,7 +3098,7 @@ wrong_template:
   {
     g_free (pad_name);
     GST_RTP_BIN_UNLOCK (rtpbin);
-    g_warning ("gstrtpbin: this is not our template");
+    g_warning ("rtpbin: this is not our template");
     return NULL;
   }
 }
@@ -2976,7 +3148,7 @@ gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
 unknown_pad:
   {
     GST_RTP_BIN_UNLOCK (rtpbin);
-    g_warning ("gstrtpbin: %s:%s is not one of our request pads",
+    g_warning ("rtpbin: %s:%s is not one of our request pads",
         GST_DEBUG_PAD_NAME (pad));
     return;
   }