gst/rtpmanager/gstrtpbin.c: Propagate the do-lost and latency properties to the jitte...
[platform/upstream/gstreamer.git] / gst / rtpmanager / gstrtpbin.c
index 0e770e2..121e359 100644 (file)
@@ -228,7 +228,7 @@ enum
   LAST_SIGNAL
 };
 
-#define DEFAULT_LATENCY_MS     200
+#define DEFAULT_LATENCY_MS          200
 #define DEFAULT_SDES_CNAME           NULL
 #define DEFAULT_SDES_NAME            NULL
 #define DEFAULT_SDES_EMAIL           NULL
@@ -236,6 +236,7 @@ enum
 #define DEFAULT_SDES_LOCATION        NULL
 #define DEFAULT_SDES_TOOL            NULL
 #define DEFAULT_SDES_NOTE            NULL
+#define DEFAULT_DO_LOST              FALSE
 
 enum
 {
@@ -248,6 +249,7 @@ enum
   PROP_SDES_LOCATION,
   PROP_SDES_TOOL,
   PROP_SDES_NOTE,
+  PROP_DO_LOST,
   PROP_LAST
 };
 
@@ -291,6 +293,7 @@ struct _GstRtpBinStream
   GstElement *demux;
   gulong demux_newpad_sig;
   gulong demux_ptreq_sig;
+  gulong demux_pt_change_sig;
 
   /* the internal pad we use to get RTCP sync messages */
   GstPad *sync_pad;
@@ -305,9 +308,11 @@ struct _GstRtpBinStream
 
   /* for lip-sync */
   guint64 clock_base;
+  guint64 clock_base_time;
   gint clock_rate;
   gint64 ts_offset;
   gint64 prev_ts_offset;
+  gint last_pt;
 };
 
 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock ((sess)->lock)
@@ -458,7 +463,8 @@ create_session (GstRtpBin * rtpbin, gint id)
   sess->bin = rtpbin;
   sess->session = session;
   sess->demux = demux;
-  sess->ptmap = g_hash_table_new (NULL, NULL);
+  sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
+      (GDestroyNotify) gst_caps_unref);
   rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
 
   /* set NTP base or new session */
@@ -522,6 +528,21 @@ free_session (GstRtpBinSession * sess)
   gst_element_set_state (sess->session, GST_STATE_NULL);
   gst_element_set_state (sess->demux, GST_STATE_NULL);
 
+  if (sess->recv_rtp_sink != NULL)
+    gst_element_release_request_pad (sess->session, 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);
+  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);
+  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_bin_remove (GST_BIN_CAST (bin), sess->session);
   gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
 
@@ -567,8 +588,10 @@ get_pt_map (GstRtpBinSession * session, guint pt)
 
   /* first look in the cache */
   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
-  if (caps)
+  if (caps) {
+    gst_caps_ref (caps);
     goto done;
+  }
 
   bin = session->bin;
 
@@ -587,17 +610,21 @@ get_pt_map (GstRtpBinSession * session, guint pt)
 
   g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
 
-  caps = (GstCaps *) g_value_get_boxed (&ret);
+  g_value_unset (&args[0]);
+  g_value_unset (&args[1]);
+  g_value_unset (&args[2]);
+  caps = (GstCaps *) g_value_dup_boxed (&ret);
+  g_value_unset (&ret);
   if (!caps)
     goto no_caps;
 
   GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
 
-  /* store in cache */
-  g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
+  /* store in cache, take additional ref */
+  g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
+      gst_caps_ref (caps));
 
 done:
-  gst_caps_ref (caps);
   GST_RTP_SESSION_UNLOCK (session);
 
   return caps;
@@ -645,6 +672,28 @@ gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
   GST_RTP_BIN_UNLOCK (bin);
 }
 
+static void
+gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
+    const gchar * name, const GValue * value)
+{
+  GSList *sessions, *streams;
+
+  GST_RTP_BIN_LOCK (bin);
+  for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
+    GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
+
+    GST_RTP_SESSION_LOCK (session);
+    for (streams = session->streams; streams; streams = g_slist_next (streams)) {
+      GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
+
+      g_object_set_property (G_OBJECT (stream->buffer), name, value);
+    }
+    GST_RTP_SESSION_UNLOCK (session);
+  }
+  GST_RTP_BIN_UNLOCK (bin);
+}
+
+/* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
 static GstRtpBinClient *
 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
 {
@@ -681,6 +730,7 @@ get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
 static void
 free_client (GstRtpBinClient * client)
 {
+  g_slist_free (client->streams);
   g_free (client->cname);
   g_free (client);
 }
@@ -696,6 +746,7 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
   GSList *walk;
 
   /* first find or create the CNAME */
+  GST_RTP_BIN_LOCK (bin);
   client = get_client (bin, len, data, &created);
 
   /* find stream in the client */
@@ -721,8 +772,30 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
   /* we can only continue if we know the local clock-base and clock-rate */
   if (stream->clock_base == -1)
     goto no_clock_base;
-  if (stream->clock_rate <= 0)
-    goto no_clock_rate;
+
+  if (stream->clock_rate <= 0) {
+    gint pt = -1;
+    GstCaps *caps = NULL;
+    GstStructure *s = NULL;
+
+    GST_RTP_SESSION_LOCK (stream->session);
+    pt = stream->last_pt;
+    GST_RTP_SESSION_UNLOCK (stream->session);
+
+    if (pt < 0)
+      goto no_clock_rate;
+
+    caps = get_pt_map (stream->session, pt);
+    if (!caps)
+      goto no_clock_rate;
+
+    s = gst_caps_get_structure (caps, 0);
+    gst_structure_get_int (s, "clock-rate", &stream->clock_rate);
+    gst_caps_unref (caps);
+
+    if (stream->clock_rate <= 0)
+      goto no_clock_rate;
+  }
 
   /* map last RTP time to local timeline using our clock-base */
   stream->local_rtp = stream->last_extrtptime - stream->clock_base;
@@ -736,6 +809,7 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
   stream->local_unix =
       gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
       stream->clock_rate);
+  stream->local_unix += stream->clock_base_time;
   /* calculate delta between server and receiver */
   stream->unix_delta = stream->last_unix - stream->local_unix;
 
@@ -754,7 +828,7 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
 
-      if (ostream->unix_delta < min)
+      if (ostream->unix_delta && ostream->unix_delta < min)
         min = ostream->unix_delta;
     }
 
@@ -765,6 +839,9 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
 
+      if (ostream->unix_delta == 0)
+        continue;
+
       ostream->ts_offset = ostream->unix_delta - min;
 
       /* delta changed, see how much */
@@ -788,16 +865,19 @@ gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
           ostream->ssrc, ostream->ts_offset);
     }
   }
+  GST_RTP_BIN_UNLOCK (bin);
   return;
 
 no_clock_base:
   {
     GST_WARNING_OBJECT (bin, "we have no clock-base");
+    GST_RTP_BIN_UNLOCK (bin);
     return;
   }
 no_clock_rate:
   {
     GST_WARNING_OBJECT (bin, "we have no clock-rate");
+    GST_RTP_BIN_UNLOCK (bin);
     return;
   }
 }
@@ -887,6 +967,7 @@ gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
 
             if (type == GST_RTCP_SDES_CNAME) {
               stream->clock_base = GST_BUFFER_OFFSET (buffer);
+              stream->clock_base_time = GST_BUFFER_OFFSET_END (buffer);
               /* associate the stream to CNAME */
               gst_rtp_bin_associate (bin, stream, len, data);
             }
@@ -939,6 +1020,7 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
   stream->buffer = buffer;
   stream->demux = demux;
   stream->last_extrtptime = -1;
+  stream->last_pt = -1;
   stream->have_sync = FALSE;
   session->streams = g_slist_prepend (session->streams, stream);
 
@@ -959,8 +1041,9 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
   g_signal_connect (buffer, "request-pt-map",
       (GCallback) pt_map_requested, session);
 
-  /* configure latency */
+  /* configure latency and packet lost */
   g_object_set (buffer, "latency", session->bin->latency, NULL);
+  g_object_set (buffer, "do-lost", session->bin->do_lost, NULL);
 
   gst_bin_add (GST_BIN_CAST (session->bin), buffer);
   gst_element_set_state (buffer, GST_STATE_PLAYING);
@@ -1240,6 +1323,11 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
           "The NOTE to put in SDES messages of this session",
           DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
 
+  g_object_class_install_property (gobject_class, PROP_DO_LOST,
+      g_param_spec_boolean ("do-lost", "Do Lost",
+          "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gstelement_class->provide_clock =
       GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
@@ -1262,7 +1350,9 @@ gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
   rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
   rtpbin->priv->bin_lock = g_mutex_new ();
   rtpbin->provided_clock = gst_system_clock_obtain ();
+
   rtpbin->latency = DEFAULT_LATENCY_MS;
+  rtpbin->do_lost = DEFAULT_DO_LOST;
 
   /* some default SDES entries */
   str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
@@ -1392,6 +1482,8 @@ gst_rtp_bin_set_property (GObject * object, guint prop_id,
       GST_RTP_BIN_LOCK (rtpbin);
       rtpbin->latency = g_value_get_uint (value);
       GST_RTP_BIN_UNLOCK (rtpbin);
+      /* propegate the property down to the jitterbuffer */
+      gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
       break;
     case PROP_SDES_CNAME:
       gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME,
@@ -1421,6 +1513,12 @@ gst_rtp_bin_set_property (GObject * object, guint prop_id,
       gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NOTE,
           g_value_get_string (value));
       break;
+    case PROP_DO_LOST:
+      GST_RTP_BIN_LOCK (rtpbin);
+      rtpbin->do_lost = g_value_get_boolean (value);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1469,6 +1567,11 @@ gst_rtp_bin_get_property (GObject * object, guint prop_id,
       g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
               GST_RTCP_SDES_NOTE));
       break;
+    case PROP_DO_LOST:
+      GST_RTP_BIN_LOCK (rtpbin);
+      g_value_set_boolean (value, rtpbin->do_lost);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1671,6 +1774,15 @@ caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
   GST_RTP_SESSION_UNLOCK (session);
 }
 
+/* Stores the last payload type received on a particular stream */
+static void
+payload_type_change (GstElement * element, guint pt, GstRtpBinStream * stream)
+{
+  GST_RTP_SESSION_LOCK (stream->session);
+  stream->last_pt = pt;
+  GST_RTP_SESSION_UNLOCK (stream->session);
+}
+
 /* a new pad (SSRC) was created in @session */
 static void
 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
@@ -1699,28 +1811,36 @@ new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
 
     s = gst_caps_get_structure (caps, 0);
 
-    if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
+    if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate)) {
       stream->clock_rate = -1;
 
+      GST_WARNING_OBJECT (session->bin,
+          "Caps have no clock rate %s from pad %s:%s",
+          gst_caps_to_string (caps), GST_DEBUG_PAD_NAME (pad));
+    }
+
     if (gst_structure_get_uint (s, "clock-base", &val))
       stream->clock_base = val;
     else
       stream->clock_base = -1;
+
+    gst_caps_unref (caps);
   }
 
   /* get pad and link */
   GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
   padname = g_strdup_printf ("src_%d", ssrc);
-  srcpad = gst_element_get_pad (element, padname);
+  srcpad = gst_element_get_static_pad (element, padname);
   g_free (padname);
   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
   gst_pad_link (srcpad, sinkpad);
   gst_object_unref (sinkpad);
+  gst_object_unref (srcpad);
 
   /* get the RTCP sync pad */
   GST_DEBUG_OBJECT (session->bin, "linking sync pad");
   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
-  srcpad = gst_element_get_pad (element, padname);
+  srcpad = gst_element_get_static_pad (element, padname);
   g_free (padname);
   gst_pad_link (srcpad, stream->sync_pad);
   gst_object_unref (srcpad);
@@ -1734,6 +1854,11 @@ new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
    * depayloaders. */
   stream->demux_ptreq_sig = g_signal_connect (stream->demux,
       "request-pt-map", (GCallback) pt_map_requested, session);
+  /* connect to the payload-type-change signal so that we can know which
+   * PT is the current PT so that the jitterbuffer can be matched to the right
+   * offset. */
+  stream->demux_pt_change_sig = g_signal_connect (stream->demux,
+      "payload-type-change", (GCallback) payload_type_change, stream);
 
   GST_RTP_SESSION_UNLOCK (session);
 
@@ -2101,8 +2226,12 @@ gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
     pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
     name_found = TRUE;
     while (gst_iterator_next (pad_it, (gpointer) & pad) == GST_ITERATOR_OK) {
-      if (strcmp (gst_pad_get_name (pad), pad_name) == 0)
+      gchar *name;
+
+      name = gst_pad_get_name (pad);
+      if (strcmp (name, pad_name) == 0)
         name_found = FALSE;
+      g_free (name);
     }
     gst_iterator_free (pad_it);
   }