2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-rtpbin
22 * @see_also: rtpjitterbuffer, rtpsession, rtpptdemux, rtpssrcdemux
24 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26 * RTP sessions that will be synchronized together using RTCP SR packets.
28 * #GstRtpBin is configured with a number of request pads that define the
29 * functionality that is activated, similar to the #GstRtpSession element.
31 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36 * the packets are released from the jitterbuffer, they will be forwarded to a
37 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39 * rtpbin with the session number, SSRC and payload type respectively as the pad
42 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43 * session number must be specified in the pad name.
45 * If you want the session manager to generate and send RTCP packets, request
46 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47 * on this pad contain SR/RR RTCP reports that should be sent to all participants
50 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52 * the pad from the lowest available session will be returned. The session manager will modify the
53 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54 * send_rtp_src_\%u pad after updating its internal state.
56 * The session manager needs the clock-rate of the payload types it is handling
57 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
61 * Access to the internal statistics of rtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
67 * <title>Example pipelines</title>
69 * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
70 * rtpbin ! rtptheoradepay ! theoradec ! xvimagesink
71 * ]| Receive RTP data from port 5000 and send to the session 0 in rtpbin.
73 * gst-launch-1.0 rtpbin name=rtpbin \
74 * v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
75 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
76 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
77 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
78 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
79 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
80 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
81 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
82 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
83 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
84 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
85 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
86 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
87 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
88 * is received on port 5007. Since RTCP packets from the sender should be sent
89 * as soon as possible and do not participate in preroll, sync=false and
90 * async=false is configured on udpsink
92 * gst-launch-1.0 -v rtpbin name=rtpbin \
93 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
94 * port=5000 ! rtpbin.recv_rtp_sink_0 \
95 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
96 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
97 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
98 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
99 * port=5002 ! rtpbin.recv_rtp_sink_1 \
100 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
101 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
103 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
104 * decode and display the video.
105 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
106 * decode and play the audio.
107 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
108 * session 1 on port 5003. These packets will be used for session management and
110 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
114 * Last reviewed on 2007-08-30 (0.10.6)
123 #include <gst/rtp/gstrtpbuffer.h>
124 #include <gst/rtp/gstrtcpbuffer.h>
126 #include "gstrtpbin.h"
127 #include "rtpsession.h"
128 #include "gstrtpsession.h"
129 #include "gstrtpjitterbuffer.h"
131 #include <gst/glib-compat-private.h>
133 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
134 #define GST_CAT_DEFAULT gst_rtp_bin_debug
137 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
138 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
141 GST_STATIC_CAPS ("application/x-rtp")
144 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
145 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
148 GST_STATIC_CAPS ("application/x-rtcp")
151 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
152 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
155 GST_STATIC_CAPS ("application/x-rtp")
159 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
160 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
163 GST_STATIC_CAPS ("application/x-rtp")
166 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
167 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
170 GST_STATIC_CAPS ("application/x-rtcp")
173 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
174 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
177 GST_STATIC_CAPS ("application/x-rtp")
180 #define GST_RTP_BIN_GET_PRIVATE(obj) \
181 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
183 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock (&(bin)->priv->bin_lock)
184 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
186 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
187 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock (&(bin)->priv->dyn_lock)
188 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->dyn_lock)
190 /* lock for shutdown */
191 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
193 if (g_atomic_int_get (&bin->priv->shutdown)) \
195 GST_RTP_BIN_DYN_LOCK (bin); \
196 if (g_atomic_int_get (&bin->priv->shutdown)) { \
197 GST_RTP_BIN_DYN_UNLOCK (bin); \
202 /* unlock for shutdown */
203 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
204 GST_RTP_BIN_DYN_UNLOCK (bin); \
206 struct _GstRtpBinPrivate
210 /* lock protecting dynamic adding/removing */
213 /* if we are shutting down or not */
218 /* UNIX (ntp) time of last SR sync used */
222 /* signals and args */
225 SIGNAL_REQUEST_PT_MAP,
226 SIGNAL_PAYLOAD_TYPE_CHANGE,
229 SIGNAL_GET_INTERNAL_SESSION,
232 SIGNAL_ON_SSRC_COLLISION,
233 SIGNAL_ON_SSRC_VALIDATED,
234 SIGNAL_ON_SSRC_ACTIVE,
237 SIGNAL_ON_BYE_TIMEOUT,
239 SIGNAL_ON_SENDER_TIMEOUT,
244 #define DEFAULT_LATENCY_MS 200
245 #define DEFAULT_DROP_ON_LATENCY FALSE
246 #define DEFAULT_SDES NULL
247 #define DEFAULT_DO_LOST FALSE
248 #define DEFAULT_IGNORE_PT FALSE
249 #define DEFAULT_NTP_SYNC FALSE
250 #define DEFAULT_AUTOREMOVE FALSE
251 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
252 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
253 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
254 #define DEFAULT_RTCP_SYNC_INTERVAL 0
255 #define DEFAULT_DO_SYNC_EVENT FALSE
256 #define DEFAULT_DO_RETRANSMISSION FALSE
262 PROP_DROP_ON_LATENCY,
268 PROP_RTCP_SYNC_INTERVAL,
271 PROP_USE_PIPELINE_CLOCK,
273 PROP_DO_RETRANSMISSION,
279 GST_RTP_BIN_RTCP_SYNC_ALWAYS,
280 GST_RTP_BIN_RTCP_SYNC_INITIAL,
281 GST_RTP_BIN_RTCP_SYNC_RTP
284 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
286 gst_rtp_bin_rtcp_sync_get_type (void)
288 static GType rtcp_sync_type = 0;
289 static const GEnumValue rtcp_sync_types[] = {
290 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
291 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
292 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
296 if (!rtcp_sync_type) {
297 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
299 return rtcp_sync_type;
303 typedef struct _GstRtpBinSession GstRtpBinSession;
304 typedef struct _GstRtpBinStream GstRtpBinStream;
305 typedef struct _GstRtpBinClient GstRtpBinClient;
307 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
309 static GstCaps *pt_map_requested (GstElement * element, guint pt,
310 GstRtpBinSession * session);
311 static void payload_type_change (GstElement * element, guint pt,
312 GstRtpBinSession * session);
313 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
314 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
315 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
316 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
317 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
318 static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
320 /* Manages the RTP stream for one SSRC.
322 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
323 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
324 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
325 * together (see below).
327 struct _GstRtpBinStream
329 /* the SSRC of this stream */
335 /* the session this SSRC belongs to */
336 GstRtpBinSession *session;
338 /* the jitterbuffer of the SSRC */
340 gulong buffer_handlesync_sig;
341 gulong buffer_ptreq_sig;
342 gulong buffer_ntpstop_sig;
345 /* the PT demuxer of the SSRC */
347 gulong demux_newpad_sig;
348 gulong demux_padremoved_sig;
349 gulong demux_ptreq_sig;
350 gulong demux_ptchange_sig;
352 /* if we have calculated a valid rt_delta for this stream */
354 /* mapping to local RTP and NTP time */
357 /* base rtptime in gst time */
361 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
362 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
364 /* Manages the receiving end of the packets.
366 * There is one such structure for each RTP session (audio/video/...).
367 * We get the RTP/RTCP packets and stuff them into the session manager. From
368 * there they are pushed into an SSRC demuxer that splits the stream based on
369 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
370 * the GstRtpBinStream above).
372 struct _GstRtpBinSession
378 /* the session element */
380 /* the SSRC demuxer */
382 gulong demux_newpad_sig;
383 gulong demux_padremoved_sig;
387 /* list of GstRtpBinStream */
390 /* mapping of payload type to caps */
393 /* the pads of the session */
394 GstPad *recv_rtp_sink;
395 GstPad *recv_rtp_sink_ghost;
396 GstPad *recv_rtp_src;
397 GstPad *recv_rtcp_sink;
398 GstPad *recv_rtcp_sink_ghost;
400 GstPad *send_rtp_sink;
401 GstPad *send_rtp_sink_ghost;
402 GstPad *send_rtp_src;
403 GstPad *send_rtp_src_ghost;
404 GstPad *send_rtcp_src;
405 GstPad *send_rtcp_src_ghost;
408 /* Manages the RTP streams that come from one client and should therefore be
411 struct _GstRtpBinClient
413 /* the common CNAME for the streams */
422 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
423 static GstRtpBinSession *
424 find_session_by_id (GstRtpBin * rtpbin, gint id)
428 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
429 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
437 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
438 static GstRtpBinSession *
439 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
443 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
444 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
446 if ((sess->recv_rtp_sink_ghost == pad) ||
447 (sess->recv_rtcp_sink_ghost == pad) ||
448 (sess->send_rtp_sink_ghost == pad)
449 || (sess->send_rtcp_src_ghost == pad))
456 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
458 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
463 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
465 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
470 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
472 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
477 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
479 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
484 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
486 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
491 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
493 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
498 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
500 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
503 if (sess->bin->priv->autoremove)
504 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
508 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
510 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
513 if (sess->bin->priv->autoremove)
514 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
518 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
520 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
525 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
527 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
528 stream->session->id, stream->ssrc);
531 /* must be called with the SESSION lock */
532 static GstRtpBinStream *
533 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
537 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
538 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
540 if (stream->ssrc == ssrc)
547 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
548 GstRtpBinSession * session)
550 GstRtpBinStream *stream = NULL;
553 rtpbin = session->bin;
555 GST_RTP_BIN_LOCK (rtpbin);
557 GST_RTP_SESSION_LOCK (session);
558 if ((stream = find_stream_by_ssrc (session, ssrc)))
559 session->streams = g_slist_remove (session->streams, stream);
560 GST_RTP_SESSION_UNLOCK (session);
563 free_stream (stream, rtpbin);
565 GST_RTP_BIN_UNLOCK (rtpbin);
568 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
569 static GstRtpBinSession *
570 create_session (GstRtpBin * rtpbin, gint id)
572 GstRtpBinSession *sess;
573 GstElement *session, *demux;
576 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
579 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
582 sess = g_new0 (GstRtpBinSession, 1);
583 g_mutex_init (&sess->lock);
586 sess->session = session;
588 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
589 (GDestroyNotify) gst_caps_unref);
590 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
592 /* configure SDES items */
593 GST_OBJECT_LOCK (rtpbin);
594 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
595 rtpbin->use_pipeline_clock, NULL);
596 GST_OBJECT_UNLOCK (rtpbin);
598 /* provide clock_rate to the session manager when needed */
599 g_signal_connect (session, "request-pt-map",
600 (GCallback) pt_map_requested, sess);
602 g_signal_connect (sess->session, "on-new-ssrc",
603 (GCallback) on_new_ssrc, sess);
604 g_signal_connect (sess->session, "on-ssrc-collision",
605 (GCallback) on_ssrc_collision, sess);
606 g_signal_connect (sess->session, "on-ssrc-validated",
607 (GCallback) on_ssrc_validated, sess);
608 g_signal_connect (sess->session, "on-ssrc-active",
609 (GCallback) on_ssrc_active, sess);
610 g_signal_connect (sess->session, "on-ssrc-sdes",
611 (GCallback) on_ssrc_sdes, sess);
612 g_signal_connect (sess->session, "on-bye-ssrc",
613 (GCallback) on_bye_ssrc, sess);
614 g_signal_connect (sess->session, "on-bye-timeout",
615 (GCallback) on_bye_timeout, sess);
616 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
617 g_signal_connect (sess->session, "on-sender-timeout",
618 (GCallback) on_sender_timeout, sess);
620 gst_bin_add (GST_BIN_CAST (rtpbin), session);
621 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
623 GST_OBJECT_LOCK (rtpbin);
624 target = GST_STATE_TARGET (rtpbin);
625 GST_OBJECT_UNLOCK (rtpbin);
627 /* change state only to what's needed */
628 gst_element_set_state (demux, target);
629 gst_element_set_state (session, target);
636 g_warning ("rtpbin: could not create rtpsession element");
641 gst_object_unref (session);
642 g_warning ("rtpbin: could not create rtpssrcdemux element");
647 /* called with RTP_BIN_LOCK */
649 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
651 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
653 gst_element_set_locked_state (sess->demux, TRUE);
654 gst_element_set_locked_state (sess->session, TRUE);
656 gst_element_set_state (sess->demux, GST_STATE_NULL);
657 gst_element_set_state (sess->session, GST_STATE_NULL);
659 remove_recv_rtp (bin, sess);
660 remove_recv_rtcp (bin, sess);
661 remove_send_rtp (bin, sess);
662 remove_rtcp (bin, sess);
664 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
665 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
667 g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
668 g_slist_free (sess->streams);
670 g_mutex_clear (&sess->lock);
671 g_hash_table_destroy (sess->ptmap);
676 /* get the payload type caps for the specific payload @pt in @session */
678 get_pt_map (GstRtpBinSession * session, guint pt)
680 GstCaps *caps = NULL;
683 GValue args[3] = { {0}, {0}, {0} };
685 GST_DEBUG ("searching pt %d in cache", pt);
687 GST_RTP_SESSION_LOCK (session);
689 /* first look in the cache */
690 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
698 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
700 /* not in cache, send signal to request caps */
701 g_value_init (&args[0], GST_TYPE_ELEMENT);
702 g_value_set_object (&args[0], bin);
703 g_value_init (&args[1], G_TYPE_UINT);
704 g_value_set_uint (&args[1], session->id);
705 g_value_init (&args[2], G_TYPE_UINT);
706 g_value_set_uint (&args[2], pt);
708 g_value_init (&ret, GST_TYPE_CAPS);
709 g_value_set_boxed (&ret, NULL);
711 GST_RTP_SESSION_UNLOCK (session);
713 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
715 GST_RTP_SESSION_LOCK (session);
717 g_value_unset (&args[0]);
718 g_value_unset (&args[1]);
719 g_value_unset (&args[2]);
721 /* look in the cache again because we let the lock go */
722 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
725 g_value_unset (&ret);
729 caps = (GstCaps *) g_value_dup_boxed (&ret);
730 g_value_unset (&ret);
734 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
736 /* store in cache, take additional ref */
737 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
738 gst_caps_ref (caps));
741 GST_RTP_SESSION_UNLOCK (session);
748 GST_RTP_SESSION_UNLOCK (session);
749 GST_DEBUG ("no pt map could be obtained");
755 return_true (gpointer key, gpointer value, gpointer user_data)
761 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
763 GSList *clients, *streams;
765 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
767 GST_RTP_BIN_LOCK (rtpbin);
768 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
769 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
771 /* reset sync on all streams for this client */
772 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
773 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
775 /* make use require a new SR packet for this stream before we attempt new
777 stream->have_sync = FALSE;
778 stream->rt_delta = 0;
779 stream->rtp_delta = 0;
780 stream->clock_base = -100 * GST_SECOND;
783 GST_RTP_BIN_UNLOCK (rtpbin);
787 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
789 GSList *sessions, *streams;
791 GST_RTP_BIN_LOCK (bin);
792 GST_DEBUG_OBJECT (bin, "clearing pt map");
793 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
794 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
796 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
797 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
799 GST_RTP_SESSION_LOCK (session);
800 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
802 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
803 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
805 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
806 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
808 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
810 GST_RTP_SESSION_UNLOCK (session);
812 GST_RTP_BIN_UNLOCK (bin);
815 gst_rtp_bin_reset_sync (bin);
819 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
821 RTPSession *internal_session = NULL;
822 GstRtpBinSession *session;
824 GST_RTP_BIN_LOCK (bin);
825 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
827 session = find_session_by_id (bin, (gint) session_id);
829 g_object_get (session->session, "internal-session", &internal_session,
832 GST_RTP_BIN_UNLOCK (bin);
834 return internal_session;
838 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
839 const gchar * name, const GValue * value)
841 GSList *sessions, *streams;
843 GST_RTP_BIN_LOCK (bin);
844 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
845 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
847 GST_RTP_SESSION_LOCK (session);
848 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
849 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
851 g_object_set_property (G_OBJECT (stream->buffer), name, value);
853 GST_RTP_SESSION_UNLOCK (session);
855 GST_RTP_BIN_UNLOCK (bin);
858 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
859 static GstRtpBinClient *
860 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
862 GstRtpBinClient *result = NULL;
865 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
866 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
868 if (len != client->cname_len)
871 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
872 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
879 /* nothing found, create one */
880 if (result == NULL) {
881 result = g_new0 (GstRtpBinClient, 1);
882 result->cname = g_strndup ((gchar *) data, len);
883 result->cname_len = len;
884 bin->clients = g_slist_prepend (bin->clients, result);
885 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
892 free_client (GstRtpBinClient * client, GstRtpBin * bin)
894 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
895 g_slist_free (client->streams);
896 g_free (client->cname);
901 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
906 GstClockTime base_time, rt, clock_time;
908 GST_OBJECT_LOCK (bin);
909 if ((clock = GST_ELEMENT_CLOCK (bin))) {
910 base_time = GST_ELEMENT_CAST (bin)->base_time;
911 gst_object_ref (clock);
912 GST_OBJECT_UNLOCK (bin);
914 clock_time = gst_clock_get_time (clock);
916 if (bin->use_pipeline_clock) {
917 ntpns = clock_time - base_time;
921 /* get current NTP time */
922 g_get_current_time (¤t);
923 ntpns = GST_TIMEVAL_TO_TIME (current);
926 /* add constant to convert from 1970 based time to 1900 based time */
927 ntpns += (2208988800LL * GST_SECOND);
929 /* get current clock time and convert to running time */
930 rt = clock_time - base_time;
932 gst_object_unref (clock);
934 GST_OBJECT_UNLOCK (bin);
945 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
946 gint64 ts_offset, gboolean check)
948 gint64 prev_ts_offset;
950 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
952 /* delta changed, see how much */
953 if (prev_ts_offset != ts_offset) {
956 diff = prev_ts_offset - ts_offset;
958 GST_DEBUG_OBJECT (bin,
959 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
960 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
963 /* only change diff when it changed more than 4 milliseconds. This
964 * compensates for rounding errors in NTP to RTP timestamp
966 if (ABS (diff) < 4 * GST_MSECOND) {
967 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
970 if (ABS (diff) > (3 * GST_SECOND)) {
971 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
975 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
977 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
978 stream->ssrc, ts_offset);
982 gst_rtp_bin_send_sync_event (GstRtpBinStream * stream)
984 if (stream->bin->send_sync_event) {
988 GST_DEBUG_OBJECT (stream->bin,
989 "sending GstRTCPSRReceived event downstream");
991 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
992 gst_structure_new_empty ("GstRTCPSRReceived"));
994 srcpad = gst_element_get_static_pad (stream->buffer, "src");
995 gst_pad_push_event (srcpad, event);
996 gst_object_unref (srcpad);
1000 /* associate a stream to the given CNAME. This will make sure all streams for
1001 * that CNAME are synchronized together.
1002 * Must be called with GST_RTP_BIN_LOCK */
1004 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1005 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1006 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1007 gint64 rtp_clock_base)
1009 GstRtpBinClient *client;
1014 GstClockTime running_time;
1016 gint64 ntpdiff, rtdiff;
1019 /* first find or create the CNAME */
1020 client = get_client (bin, len, data, &created);
1022 /* find stream in the client */
1023 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1024 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1026 if (ostream == stream)
1029 /* not found, add it to the list */
1031 GST_DEBUG_OBJECT (bin,
1032 "new association of SSRC %08x with client %p with CNAME %s",
1033 stream->ssrc, client, client->cname);
1034 client->streams = g_slist_prepend (client->streams, stream);
1037 GST_DEBUG_OBJECT (bin,
1038 "found association of SSRC %08x with client %p with CNAME %s",
1039 stream->ssrc, client, client->cname);
1042 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1043 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1044 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1045 /* we don't need that data, so carry on,
1046 * but make some values look saner */
1047 last_extrtptime = base_rtptime;
1049 /* nothing we can do with this data in this case */
1050 GST_DEBUG_OBJECT (bin, "bailing out");
1055 /* Take the extended rtptime we found in the SR packet and map it to the
1056 * local rtptime. The local rtp time is used to construct timestamps on the
1057 * buffers so we will calculate what running_time corresponds to the RTP
1058 * timestamp in the SR packet. */
1059 local_rtp = last_extrtptime - base_rtptime;
1061 GST_DEBUG_OBJECT (bin,
1062 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1063 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1064 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1065 last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
1067 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1068 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1069 * into a corresponding gstreamer timestamp. Note that the base_time also
1070 * contains the drift between sender and receiver. */
1071 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1072 local_rt += base_time;
1074 /* convert ntptime to unix time since 1900 */
1075 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1076 (G_GINT64_CONSTANT (1) << 32));
1078 stream->have_sync = TRUE;
1080 GST_DEBUG_OBJECT (bin,
1081 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1082 local_rt, last_unix);
1084 /* recalc inter stream playout offset, but only if there is more than one
1085 * stream or we're doing NTP sync. */
1086 if (bin->ntp_sync) {
1087 /* For NTP sync we need to first get a snapshot of running_time and NTP
1088 * time. We know at what running_time we play a certain RTP time, we also
1089 * calculated when we would play the RTP time in the SR packet. Now we need
1090 * to know how the running_time and the NTP time relate to eachother. */
1091 get_current_times (bin, &running_time, &ntpnstime);
1093 /* see how far away the NTP time is. This is the difference between the
1094 * current NTP time and the NTP time in the last SR packet. */
1095 ntpdiff = ntpnstime - last_unix;
1096 /* see how far away the running_time is. This is the difference between the
1097 * current running_time and the running_time of the RTP timestamp in the
1098 * last SR packet. */
1099 rtdiff = running_time - local_rt;
1101 GST_DEBUG_OBJECT (bin,
1102 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1103 ntpnstime, last_unix);
1104 GST_DEBUG_OBJECT (bin,
1105 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1108 /* combine to get the final diff to apply to the running_time */
1109 stream->rt_delta = rtdiff - ntpdiff;
1111 stream_set_ts_offset (bin, stream, stream->rt_delta, FALSE);
1113 gint64 min, rtp_min, clock_base = stream->clock_base;
1114 gboolean all_sync, use_rtp;
1115 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1117 /* calculate delta between server and receiver. last_unix is created by
1118 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1119 * delta expresses the difference to our timeline and the server timeline. The
1120 * difference in itself doesn't mean much but we can combine the delta of
1121 * multiple streams to create a stream specific offset. */
1122 stream->rt_delta = last_unix - local_rt;
1124 /* calculate the min of all deltas, ignoring streams that did not yet have a
1125 * valid rt_delta because we did not yet receive an SR packet for those
1127 * We calculate the mininum because we would like to only apply positive
1128 * offsets to streams, delaying their playback instead of trying to speed up
1129 * other streams (which might be imposible when we have to create negative
1131 * The stream that has the smallest diff is selected as the reference stream,
1132 * all other streams will have a positive offset to this difference. */
1134 /* some alternative setting allow ignoring RTCP as much as possible,
1135 * for servers generating bogus ntp timeline */
1136 min = rtp_min = G_MAXINT64;
1138 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1142 /* signed version for convienience */
1143 clock_base = base_rtptime;
1144 /* deal with possible wrap-around */
1145 ext_base = base_rtptime;
1146 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1147 /* sanity check; base rtp and provided clock_base should be close */
1148 if (rtp_clock_base >= clock_base) {
1149 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1150 rtp_clock_base = base_time +
1151 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1152 GST_SECOND, clock_rate);
1157 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1158 rtp_clock_base = base_time -
1159 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1160 GST_SECOND, clock_rate);
1165 /* warn and bail for clarity out if no sane values */
1167 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1170 /* store to track changes */
1171 clock_base = rtp_clock_base;
1172 /* generate a fake as before,
1173 * now equating rtptime obtained from RTP-Info,
1174 * where the large time represent the otherwise irrelevant npt/ntp time */
1175 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1177 clock_base = rtp_clock_base;
1181 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1182 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1184 if (!ostream->have_sync) {
1189 /* change in current stream's base from previously init'ed value
1190 * leads to reset of all stream's base */
1191 if (stream != ostream && stream->clock_base >= 0 &&
1192 (stream->clock_base != clock_base)) {
1193 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1194 ostream->clock_base = -100 * GST_SECOND;
1195 ostream->rtp_delta = 0;
1198 if (ostream->rt_delta < min)
1199 min = ostream->rt_delta;
1200 if (ostream->rtp_delta < rtp_min)
1201 rtp_min = ostream->rtp_delta;
1204 /* arrange to re-sync for each stream upon significant change,
1206 all_sync = all_sync && (stream->clock_base == clock_base);
1207 stream->clock_base = clock_base;
1209 /* may need init performed above later on, but nothing more to do now */
1210 if (client->nstreams <= 1)
1213 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1214 " all sync %d", client, min, all_sync);
1215 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1217 switch (rtcp_sync) {
1218 case GST_RTP_BIN_RTCP_SYNC_RTP:
1221 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1222 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1224 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1225 /* if all have been synced already, do not bother further */
1227 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1235 /* bail out if we adjusted recently enough */
1236 if (all_sync && (last_unix - bin->priv->last_unix) <
1237 bin->rtcp_sync_interval * GST_MSECOND) {
1238 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1239 "previous sender info too recent "
1240 "(previous UNIX %" G_GUINT64_FORMAT ")", bin->priv->last_unix);
1243 bin->priv->last_unix = last_unix;
1245 /* calculate offsets for each stream */
1246 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1247 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1250 /* ignore streams for which we didn't receive an SR packet yet, we
1251 * can't synchronize them yet. We can however sync other streams just
1253 if (!ostream->have_sync)
1256 /* calculate offset to our reference stream, this should always give a
1257 * positive number. */
1259 ts_offset = ostream->rtp_delta - rtp_min;
1261 ts_offset = ostream->rt_delta - min;
1263 stream_set_ts_offset (bin, ostream, ts_offset, TRUE);
1266 gst_rtp_bin_send_sync_event (stream);
1271 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1272 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1273 (b) = gst_rtcp_packet_move_to_next ((packet)))
1275 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1276 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1277 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1279 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1280 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1281 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1284 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1285 GstRtpBinStream * stream)
1288 GstRTCPPacket packet;
1291 gboolean have_sr, have_sdes;
1293 guint64 base_rtptime;
1299 GstRTCPBuffer rtcp = { NULL, };
1303 GST_DEBUG_OBJECT (bin, "sync handler called");
1305 /* get the last relation between the rtp timestamps and the gstreamer
1306 * timestamps. We get this info directly from the jitterbuffer which
1307 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1308 * what the current situation is. */
1310 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1311 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1312 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1313 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1315 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1316 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1321 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1323 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1324 /* first packet must be SR or RR or else the validate would have failed */
1325 switch (gst_rtcp_packet_get_type (&packet)) {
1326 case GST_RTCP_TYPE_SR:
1327 /* only parse first. There is only supposed to be one SR in the packet
1328 * but we will deal with malformed packets gracefully */
1331 /* get NTP and RTP times */
1332 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1335 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1336 /* ignore SR that is not ours */
1337 if (ssrc != stream->ssrc)
1342 case GST_RTCP_TYPE_SDES:
1344 gboolean more_items, more_entries;
1346 /* only deal with first SDES, there is only supposed to be one SDES in
1347 * the RTCP packet but we deal with bad packets gracefully. Also bail
1348 * out if we have not seen an SR item yet. */
1349 if (have_sdes || !have_sr)
1352 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1353 /* skip items that are not about the SSRC of the sender */
1354 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1357 /* find the CNAME entry */
1358 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1359 GstRTCPSDESType type;
1363 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1365 if (type == GST_RTCP_SDES_CNAME) {
1366 GST_RTP_BIN_LOCK (bin);
1367 /* associate the stream to CNAME */
1368 gst_rtp_bin_associate (bin, stream, len, data,
1369 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1371 GST_RTP_BIN_UNLOCK (bin);
1379 /* we can ignore these packets */
1383 gst_rtcp_buffer_unmap (&rtcp);
1386 /* create a new stream with @ssrc in @session. Must be called with
1387 * RTP_SESSION_LOCK. */
1388 static GstRtpBinStream *
1389 create_stream (GstRtpBinSession * session, guint32 ssrc)
1391 GstElement *buffer, *demux = NULL;
1392 GstRtpBinStream *stream;
1396 rtpbin = session->bin;
1398 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1399 goto no_jitterbuffer;
1401 if (!rtpbin->ignore_pt)
1402 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1406 stream = g_new0 (GstRtpBinStream, 1);
1407 stream->ssrc = ssrc;
1408 stream->bin = rtpbin;
1409 stream->session = session;
1410 stream->buffer = buffer;
1411 stream->demux = demux;
1413 stream->have_sync = FALSE;
1414 stream->rt_delta = 0;
1415 stream->rtp_delta = 0;
1416 stream->percent = 100;
1417 stream->clock_base = -100 * GST_SECOND;
1418 session->streams = g_slist_prepend (session->streams, stream);
1420 /* provide clock_rate to the jitterbuffer when needed */
1421 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1422 (GCallback) pt_map_requested, session);
1423 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1424 (GCallback) on_npt_stop, stream);
1426 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1427 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1429 /* configure latency and packet lost */
1430 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1431 g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1432 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1433 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1434 g_object_set (buffer, "do-retransmission", rtpbin->do_retransmission, NULL);
1436 if (!rtpbin->ignore_pt)
1437 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1438 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1442 gst_element_link_pads_full (buffer, "src", demux, "sink",
1443 GST_PAD_LINK_CHECK_NOTHING);
1445 if (rtpbin->buffering) {
1448 GST_INFO_OBJECT (rtpbin,
1449 "bin is buffering, set jitterbuffer as not active");
1450 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1454 GST_OBJECT_LOCK (rtpbin);
1455 target = GST_STATE_TARGET (rtpbin);
1456 GST_OBJECT_UNLOCK (rtpbin);
1458 /* from sink to source */
1460 gst_element_set_state (demux, target);
1462 gst_element_set_state (buffer, target);
1469 g_warning ("rtpbin: could not create rtpjitterbuffer element");
1474 gst_object_unref (buffer);
1475 g_warning ("rtpbin: could not create rtpptdemux element");
1480 /* called with RTP_BIN_LOCK */
1482 free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
1484 GSList *clients, *next_client;
1486 GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
1488 if (stream->demux) {
1489 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1490 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1491 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1493 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1494 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1495 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1498 gst_element_set_locked_state (stream->demux, TRUE);
1499 gst_element_set_locked_state (stream->buffer, TRUE);
1502 gst_element_set_state (stream->demux, GST_STATE_NULL);
1503 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1505 /* now remove this signal, we need this while going to NULL because it to
1506 * do some cleanups */
1508 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1510 gst_bin_remove (GST_BIN_CAST (bin), stream->buffer);
1512 gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
1514 for (clients = bin->clients; clients; clients = next_client) {
1515 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1516 GSList *streams, *next_stream;
1518 next_client = g_slist_next (clients);
1520 for (streams = client->streams; streams; streams = next_stream) {
1521 GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
1523 next_stream = g_slist_next (streams);
1525 if (ostream == stream) {
1526 client->streams = g_slist_delete_link (client->streams, streams);
1527 /* If this was the last stream belonging to this client,
1528 * clean up the client. */
1529 if (--client->nstreams == 0) {
1530 bin->clients = g_slist_delete_link (bin->clients, clients);
1531 free_client (client, bin);
1540 /* GObject vmethods */
1541 static void gst_rtp_bin_dispose (GObject * object);
1542 static void gst_rtp_bin_finalize (GObject * object);
1543 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1544 const GValue * value, GParamSpec * pspec);
1545 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1546 GValue * value, GParamSpec * pspec);
1548 /* GstElement vmethods */
1549 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1550 GstStateChange transition);
1551 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1552 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1553 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1554 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1556 #define gst_rtp_bin_parent_class parent_class
1557 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1560 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1562 GObjectClass *gobject_class;
1563 GstElementClass *gstelement_class;
1564 GstBinClass *gstbin_class;
1566 gobject_class = (GObjectClass *) klass;
1567 gstelement_class = (GstElementClass *) klass;
1568 gstbin_class = (GstBinClass *) klass;
1570 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1572 gobject_class->dispose = gst_rtp_bin_dispose;
1573 gobject_class->finalize = gst_rtp_bin_finalize;
1574 gobject_class->set_property = gst_rtp_bin_set_property;
1575 gobject_class->get_property = gst_rtp_bin_get_property;
1577 g_object_class_install_property (gobject_class, PROP_LATENCY,
1578 g_param_spec_uint ("latency", "Buffer latency in ms",
1579 "Default amount of ms to buffer in the jitterbuffers", 0,
1580 G_MAXUINT, DEFAULT_LATENCY_MS,
1581 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1583 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
1584 g_param_spec_boolean ("drop-on-latency",
1585 "Drop buffers when maximum latency is reached",
1586 "Tells the jitterbuffer to never exceed the given latency in size",
1587 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1590 * GstRtpBin::request-pt-map:
1591 * @rtpbin: the object which received the signal
1592 * @session: the session
1595 * Request the payload type as #GstCaps for @pt in @session.
1597 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1598 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1599 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1600 NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_CAPS, 2, G_TYPE_UINT,
1604 * GstRtpBin::payload-type-change:
1605 * @rtpbin: the object which received the signal
1606 * @session: the session
1609 * Signal that the current payload type changed to @pt in @session.
1611 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1612 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1613 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1614 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1618 * GstRtpBin::clear-pt-map:
1619 * @rtpbin: the object which received the signal
1621 * Clear all previously cached pt-mapping obtained with
1622 * #GstRtpBin::request-pt-map.
1624 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1625 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1626 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1627 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1631 * GstRtpBin::reset-sync:
1632 * @rtpbin: the object which received the signal
1634 * Reset all currently configured lip-sync parameters and require new SR
1635 * packets for all streams before lip-sync is attempted again.
1637 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1638 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1639 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1640 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1644 * GstRtpBin::get-internal-session:
1645 * @rtpbin: the object which received the signal
1646 * @id: the session id
1648 * Request the internal RTPSession object as #GObject in session @id.
1650 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1651 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1652 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1653 get_internal_session), NULL, NULL, g_cclosure_marshal_generic,
1654 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1657 * GstRtpBin::on-new-ssrc:
1658 * @rtpbin: the object which received the signal
1659 * @session: the session
1662 * Notify of a new SSRC that entered @session.
1664 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1665 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1666 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1667 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1670 * GstRtpBin::on-ssrc-collision:
1671 * @rtpbin: the object which received the signal
1672 * @session: the session
1675 * Notify when we have an SSRC collision
1677 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1678 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1679 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1680 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1683 * GstRtpBin::on-ssrc-validated:
1684 * @rtpbin: the object which received the signal
1685 * @session: the session
1688 * Notify of a new SSRC that became validated.
1690 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1691 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1692 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1693 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1696 * GstRtpBin::on-ssrc-active:
1697 * @rtpbin: the object which received the signal
1698 * @session: the session
1701 * Notify of a SSRC that is active, i.e., sending RTCP.
1703 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1704 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1705 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1706 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1709 * GstRtpBin::on-ssrc-sdes:
1710 * @rtpbin: the object which received the signal
1711 * @session: the session
1714 * Notify of a SSRC that is active, i.e., sending RTCP.
1716 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1717 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1718 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1719 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1723 * GstRtpBin::on-bye-ssrc:
1724 * @rtpbin: the object which received the signal
1725 * @session: the session
1728 * Notify of an SSRC that became inactive because of a BYE packet.
1730 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1731 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1732 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1733 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1736 * GstRtpBin::on-bye-timeout:
1737 * @rtpbin: the object which received the signal
1738 * @session: the session
1741 * Notify of an SSRC that has timed out because of BYE
1743 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1744 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1745 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1746 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1749 * GstRtpBin::on-timeout:
1750 * @rtpbin: the object which received the signal
1751 * @session: the session
1754 * Notify of an SSRC that has timed out
1756 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1757 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1758 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1759 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1762 * GstRtpBin::on-sender-timeout:
1763 * @rtpbin: the object which received the signal
1764 * @session: the session
1767 * Notify of a sender SSRC that has timed out and became a receiver
1769 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1770 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1771 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1772 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1776 * GstRtpBin::on-npt-stop:
1777 * @rtpbin: the object which received the signal
1778 * @session: the session
1781 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1783 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1784 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1785 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1786 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1789 g_object_class_install_property (gobject_class, PROP_SDES,
1790 g_param_spec_boxed ("sdes", "SDES",
1791 "The SDES items of this session",
1792 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1794 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1795 g_param_spec_boolean ("do-lost", "Do Lost",
1796 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1797 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1799 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1800 g_param_spec_boolean ("autoremove", "Auto Remove",
1801 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1802 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1804 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1805 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1806 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1807 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1809 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1810 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1811 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
1812 DEFAULT_USE_PIPELINE_CLOCK,
1813 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1815 * GstRtpBin:buffer-mode:
1817 * Control the buffering and timestamping mode used by the jitterbuffer.
1819 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1820 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1821 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1822 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1824 * GstRtpBin:ntp-sync:
1826 * Set the NTP time from the sender reports as the running-time on the
1827 * buffers. When both the sender and receiver have sychronized
1828 * running-time, i.e. when the clock and base-time is shared
1829 * between the receivers and the and the senders, this option can be
1830 * used to synchronize receivers on multiple machines.
1832 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1833 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1834 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1835 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1838 * GstRtpBin:rtcp-sync:
1840 * If not synchronizing (directly) to the NTP clock, determines how to sync
1841 * the various streams.
1843 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
1844 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
1845 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
1846 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1849 * GstRtpBin:rtcp-sync-interval:
1851 * Determines how often to sync streams using RTCP data.
1853 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
1854 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
1855 "RTCP SR interval synchronization (ms) (0 = always)",
1856 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
1857 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1859 g_object_class_install_property (gobject_class, PROP_DO_SYNC_EVENT,
1860 g_param_spec_boolean ("do-sync-event", "Do Sync Event",
1861 "Send event downstream when a stream is synchronized to the sender",
1862 DEFAULT_DO_SYNC_EVENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1864 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
1865 g_param_spec_boolean ("do-retransmission", "Do retransmission",
1866 "Send an event downstream to request packet retransmission",
1867 DEFAULT_DO_RETRANSMISSION,
1868 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1870 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1871 gstelement_class->request_new_pad =
1872 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1873 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1876 gst_element_class_add_pad_template (gstelement_class,
1877 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1878 gst_element_class_add_pad_template (gstelement_class,
1879 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1880 gst_element_class_add_pad_template (gstelement_class,
1881 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1884 gst_element_class_add_pad_template (gstelement_class,
1885 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1886 gst_element_class_add_pad_template (gstelement_class,
1887 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1888 gst_element_class_add_pad_template (gstelement_class,
1889 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1891 gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
1892 "Filter/Network/RTP",
1893 "Real-Time Transport Protocol bin",
1894 "Wim Taymans <wim.taymans@gmail.com>");
1896 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1898 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1899 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1900 klass->get_internal_session =
1901 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1903 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1907 gst_rtp_bin_init (GstRtpBin * rtpbin)
1911 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1912 g_mutex_init (&rtpbin->priv->bin_lock);
1913 g_mutex_init (&rtpbin->priv->dyn_lock);
1915 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1916 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1917 rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1918 rtpbin->do_lost = DEFAULT_DO_LOST;
1919 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1920 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1921 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
1922 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
1923 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1924 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1925 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1926 rtpbin->send_sync_event = DEFAULT_DO_SYNC_EVENT;
1927 rtpbin->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1929 /* some default SDES entries */
1930 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
1931 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1932 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
1937 gst_rtp_bin_dispose (GObject * object)
1941 rtpbin = GST_RTP_BIN (object);
1943 GST_RTP_BIN_LOCK (rtpbin);
1944 GST_DEBUG_OBJECT (object, "freeing sessions");
1945 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1946 g_slist_free (rtpbin->sessions);
1947 rtpbin->sessions = NULL;
1948 GST_RTP_BIN_UNLOCK (rtpbin);
1950 G_OBJECT_CLASS (parent_class)->dispose (object);
1954 gst_rtp_bin_finalize (GObject * object)
1958 rtpbin = GST_RTP_BIN (object);
1961 gst_structure_free (rtpbin->sdes);
1963 g_mutex_clear (&rtpbin->priv->bin_lock);
1964 g_mutex_clear (&rtpbin->priv->dyn_lock);
1966 G_OBJECT_CLASS (parent_class)->finalize (object);
1971 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1978 GST_RTP_BIN_LOCK (bin);
1980 GST_OBJECT_LOCK (bin);
1982 gst_structure_free (bin->sdes);
1983 bin->sdes = gst_structure_copy (sdes);
1984 GST_OBJECT_UNLOCK (bin);
1986 /* store in all sessions */
1987 for (item = bin->sessions; item; item = g_slist_next (item)) {
1988 GstRtpBinSession *session = item->data;
1989 g_object_set (session->session, "sdes", sdes, NULL);
1992 GST_RTP_BIN_UNLOCK (bin);
1995 static GstStructure *
1996 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1998 GstStructure *result;
2000 GST_OBJECT_LOCK (bin);
2001 result = gst_structure_copy (bin->sdes);
2002 GST_OBJECT_UNLOCK (bin);
2008 gst_rtp_bin_set_property (GObject * object, guint prop_id,
2009 const GValue * value, GParamSpec * pspec)
2013 rtpbin = GST_RTP_BIN (object);
2017 GST_RTP_BIN_LOCK (rtpbin);
2018 rtpbin->latency_ms = g_value_get_uint (value);
2019 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
2020 GST_RTP_BIN_UNLOCK (rtpbin);
2021 /* propagate the property down to the jitterbuffer */
2022 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2024 case PROP_DROP_ON_LATENCY:
2025 GST_RTP_BIN_LOCK (rtpbin);
2026 rtpbin->drop_on_latency = g_value_get_boolean (value);
2027 GST_RTP_BIN_UNLOCK (rtpbin);
2028 /* propagate the property down to the jitterbuffer */
2029 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2030 "drop-on-latency", value);
2033 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2036 GST_RTP_BIN_LOCK (rtpbin);
2037 rtpbin->do_lost = g_value_get_boolean (value);
2038 GST_RTP_BIN_UNLOCK (rtpbin);
2039 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2042 rtpbin->ntp_sync = g_value_get_boolean (value);
2044 case PROP_RTCP_SYNC:
2045 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2047 case PROP_RTCP_SYNC_INTERVAL:
2048 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2050 case PROP_IGNORE_PT:
2051 rtpbin->ignore_pt = g_value_get_boolean (value);
2053 case PROP_AUTOREMOVE:
2054 rtpbin->priv->autoremove = g_value_get_boolean (value);
2056 case PROP_USE_PIPELINE_CLOCK:
2059 GST_RTP_BIN_LOCK (rtpbin);
2060 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2061 for (sessions = rtpbin->sessions; sessions;
2062 sessions = g_slist_next (sessions)) {
2063 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2065 g_object_set (G_OBJECT (session->session),
2066 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2068 GST_RTP_BIN_UNLOCK (rtpbin);
2071 case PROP_DO_SYNC_EVENT:
2072 rtpbin->send_sync_event = g_value_get_boolean (value);
2074 case PROP_BUFFER_MODE:
2075 GST_RTP_BIN_LOCK (rtpbin);
2076 rtpbin->buffer_mode = g_value_get_enum (value);
2077 GST_RTP_BIN_UNLOCK (rtpbin);
2078 /* propagate the property down to the jitterbuffer */
2079 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2081 case PROP_DO_RETRANSMISSION:
2082 GST_RTP_BIN_LOCK (rtpbin);
2083 rtpbin->do_retransmission = g_value_get_boolean (value);
2084 GST_RTP_BIN_UNLOCK (rtpbin);
2085 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2086 "do-retransmission", value);
2089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2095 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2096 GValue * value, GParamSpec * pspec)
2100 rtpbin = GST_RTP_BIN (object);
2104 GST_RTP_BIN_LOCK (rtpbin);
2105 g_value_set_uint (value, rtpbin->latency_ms);
2106 GST_RTP_BIN_UNLOCK (rtpbin);
2108 case PROP_DROP_ON_LATENCY:
2109 GST_RTP_BIN_LOCK (rtpbin);
2110 g_value_set_boolean (value, rtpbin->drop_on_latency);
2111 GST_RTP_BIN_UNLOCK (rtpbin);
2114 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2117 GST_RTP_BIN_LOCK (rtpbin);
2118 g_value_set_boolean (value, rtpbin->do_lost);
2119 GST_RTP_BIN_UNLOCK (rtpbin);
2121 case PROP_IGNORE_PT:
2122 g_value_set_boolean (value, rtpbin->ignore_pt);
2125 g_value_set_boolean (value, rtpbin->ntp_sync);
2127 case PROP_RTCP_SYNC:
2128 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2130 case PROP_RTCP_SYNC_INTERVAL:
2131 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2133 case PROP_AUTOREMOVE:
2134 g_value_set_boolean (value, rtpbin->priv->autoremove);
2136 case PROP_BUFFER_MODE:
2137 g_value_set_enum (value, rtpbin->buffer_mode);
2139 case PROP_USE_PIPELINE_CLOCK:
2140 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2142 case PROP_DO_SYNC_EVENT:
2143 g_value_set_boolean (value, rtpbin->send_sync_event);
2145 case PROP_DO_RETRANSMISSION:
2146 GST_RTP_BIN_LOCK (rtpbin);
2147 g_value_set_boolean (value, rtpbin->do_retransmission);
2148 GST_RTP_BIN_UNLOCK (rtpbin);
2151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2157 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
2161 rtpbin = GST_RTP_BIN (bin);
2163 switch (GST_MESSAGE_TYPE (message)) {
2164 case GST_MESSAGE_ELEMENT:
2166 const GstStructure *s = gst_message_get_structure (message);
2168 /* we change the structure name and add the session ID to it */
2169 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
2170 GstRtpBinSession *sess;
2172 /* find the session we set it as object data */
2173 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2174 "GstRTPBin.session");
2176 if (G_LIKELY (sess)) {
2177 message = gst_message_make_writable (message);
2178 s = gst_message_get_structure (message);
2179 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
2183 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2186 case GST_MESSAGE_BUFFERING:
2189 gint min_percent = 100;
2190 GSList *sessions, *streams;
2191 GstRtpBinStream *stream;
2192 gboolean change = FALSE, active = FALSE;
2193 GstClockTime min_out_time;
2194 GstBufferingMode mode;
2195 gint avg_in, avg_out;
2196 gint64 buffering_left;
2198 gst_message_parse_buffering (message, &percent);
2199 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
2203 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2204 "GstRTPBin.stream");
2206 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
2208 /* get the stream */
2209 if (G_LIKELY (stream)) {
2210 GST_RTP_BIN_LOCK (rtpbin);
2211 /* fill in the percent */
2212 stream->percent = percent;
2214 /* calculate the min value for all streams */
2215 for (sessions = rtpbin->sessions; sessions;
2216 sessions = g_slist_next (sessions)) {
2217 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2219 GST_RTP_SESSION_LOCK (session);
2220 if (session->streams) {
2221 for (streams = session->streams; streams;
2222 streams = g_slist_next (streams)) {
2223 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2225 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
2228 /* find min percent */
2229 if (min_percent > stream->percent)
2230 min_percent = stream->percent;
2233 GST_INFO_OBJECT (bin,
2234 "session has no streams, setting min_percent to 0");
2237 GST_RTP_SESSION_UNLOCK (session);
2239 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
2241 if (rtpbin->buffering) {
2242 if (min_percent == 100) {
2243 rtpbin->buffering = FALSE;
2248 if (min_percent < 100) {
2249 /* pause the streams */
2250 rtpbin->buffering = TRUE;
2255 GST_RTP_BIN_UNLOCK (rtpbin);
2257 gst_message_unref (message);
2259 /* make a new buffering message with the min value */
2261 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2262 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2265 if (G_UNLIKELY (change)) {
2267 guint64 running_time = 0;
2270 /* figure out the running time when we have a clock */
2271 if (G_LIKELY ((clock =
2272 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2273 guint64 now, base_time;
2275 now = gst_clock_get_time (clock);
2276 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2277 running_time = now - base_time;
2278 gst_object_unref (clock);
2280 GST_DEBUG_OBJECT (bin,
2281 "running time now %" GST_TIME_FORMAT,
2282 GST_TIME_ARGS (running_time));
2284 GST_RTP_BIN_LOCK (rtpbin);
2286 /* when we reactivate, calculate the offsets so that all streams have
2287 * an output time that is at least as big as the running_time */
2290 if (running_time > rtpbin->buffer_start) {
2291 offset = running_time - rtpbin->buffer_start;
2292 if (offset >= rtpbin->latency_ns)
2293 offset -= rtpbin->latency_ns;
2299 /* pause all streams */
2301 for (sessions = rtpbin->sessions; sessions;
2302 sessions = g_slist_next (sessions)) {
2303 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2305 GST_RTP_SESSION_LOCK (session);
2306 for (streams = session->streams; streams;
2307 streams = g_slist_next (streams)) {
2308 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2309 GstElement *element = stream->buffer;
2312 g_signal_emit_by_name (element, "set-active", active, offset,
2316 g_object_get (element, "percent", &stream->percent, NULL);
2320 if (min_out_time == -1 || last_out < min_out_time)
2321 min_out_time = last_out;
2324 GST_DEBUG_OBJECT (bin,
2325 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2326 GST_TIME_FORMAT ", percent %d", element, active,
2327 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2330 GST_RTP_SESSION_UNLOCK (session);
2332 GST_DEBUG_OBJECT (bin,
2333 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2335 /* the buffer_start is the min out time of all paused jitterbuffers */
2337 rtpbin->buffer_start = min_out_time;
2339 GST_RTP_BIN_UNLOCK (rtpbin);
2342 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2347 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2353 static GstStateChangeReturn
2354 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2356 GstStateChangeReturn res;
2358 GstRtpBinPrivate *priv;
2360 rtpbin = GST_RTP_BIN (element);
2361 priv = rtpbin->priv;
2363 switch (transition) {
2364 case GST_STATE_CHANGE_NULL_TO_READY:
2366 case GST_STATE_CHANGE_READY_TO_PAUSED:
2367 priv->last_unix = 0;
2368 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2369 g_atomic_int_set (&priv->shutdown, 0);
2371 case GST_STATE_CHANGE_PAUSED_TO_READY:
2372 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2373 g_atomic_int_set (&priv->shutdown, 1);
2374 /* wait for all callbacks to end by taking the lock. No new callbacks will
2375 * be able to happen as we set the shutdown flag. */
2376 GST_RTP_BIN_DYN_LOCK (rtpbin);
2377 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2378 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2384 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2386 switch (transition) {
2387 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2389 case GST_STATE_CHANGE_PAUSED_TO_READY:
2391 case GST_STATE_CHANGE_READY_TO_NULL:
2399 /* a new pad (SSRC) was created in @session. This signal is emited from the
2400 * payload demuxer. */
2402 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2403 GstRtpBinStream * stream)
2406 GstElementClass *klass;
2407 GstPadTemplate *templ;
2411 rtpbin = stream->bin;
2413 GST_DEBUG ("new payload pad %d", pt);
2415 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2417 /* ghost the pad to the parent */
2418 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2419 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2420 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2421 stream->session->id, stream->ssrc, pt);
2422 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2424 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2426 gst_pad_set_active (gpad, TRUE);
2427 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2429 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2435 GST_DEBUG ("ignoring, we are shutting down");
2441 payload_pad_removed (GstElement * element, GstPad * pad,
2442 GstRtpBinStream * stream)
2447 rtpbin = stream->bin;
2449 GST_DEBUG ("payload pad removed");
2451 GST_RTP_BIN_DYN_LOCK (rtpbin);
2452 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2453 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2455 gst_pad_set_active (gpad, FALSE);
2456 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2458 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2462 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2467 rtpbin = session->bin;
2469 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2472 caps = get_pt_map (session, pt);
2481 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2487 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2489 GST_DEBUG_OBJECT (session->bin,
2490 "emiting signal for pt type changed to %d in session %d", pt,
2493 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2494 0, session->id, pt);
2497 /* emited when caps changed for the session */
2499 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2504 const GstStructure *s;
2508 g_object_get (pad, "caps", &caps, NULL);
2513 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2515 s = gst_caps_get_structure (caps, 0);
2517 /* get payload, finish when it's not there */
2518 if (!gst_structure_get_int (s, "payload", &payload))
2521 GST_RTP_SESSION_LOCK (session);
2522 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2523 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2524 GST_RTP_SESSION_UNLOCK (session);
2527 /* a new pad (SSRC) was created in @session */
2529 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2530 GstRtpBinSession * session)
2533 GstRtpBinStream *stream;
2534 GstPad *sinkpad, *srcpad;
2537 rtpbin = session->bin;
2539 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2540 GST_DEBUG_PAD_NAME (pad));
2542 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2544 GST_RTP_SESSION_LOCK (session);
2546 /* create new stream */
2547 stream = create_stream (session, ssrc);
2551 /* get pad and link */
2552 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2553 padname = g_strdup_printf ("src_%u", ssrc);
2554 srcpad = gst_element_get_static_pad (element, padname);
2556 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2557 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2558 gst_object_unref (sinkpad);
2559 gst_object_unref (srcpad);
2561 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2562 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
2563 srcpad = gst_element_get_static_pad (element, padname);
2565 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2566 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2567 gst_object_unref (sinkpad);
2568 gst_object_unref (srcpad);
2570 /* connect to the RTCP sync signal from the jitterbuffer */
2571 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2572 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2573 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2575 if (stream->demux) {
2576 /* connect to the new-pad signal of the payload demuxer, this will expose the
2577 * new pad by ghosting it. */
2578 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2579 "new-payload-type", (GCallback) new_payload_found, stream);
2580 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2581 "pad-removed", (GCallback) payload_pad_removed, stream);
2583 /* connect to the request-pt-map signal. This signal will be emited by the
2584 * demuxer so that it can apply a proper caps on the buffers for the
2586 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2587 "request-pt-map", (GCallback) pt_map_requested, session);
2588 /* connect to the signal so it can be forwarded. */
2589 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2590 "payload-type-change", (GCallback) payload_type_change, session);
2592 /* add rtpjitterbuffer src pad to pads */
2593 GstElementClass *klass;
2594 GstPadTemplate *templ;
2598 pad = gst_element_get_static_pad (stream->buffer, "src");
2600 /* ghost the pad to the parent */
2601 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2602 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2603 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2604 stream->session->id, stream->ssrc, 255);
2605 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2608 gst_pad_set_active (gpad, TRUE);
2609 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2611 gst_object_unref (pad);
2614 GST_RTP_SESSION_UNLOCK (session);
2615 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2622 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2627 GST_RTP_SESSION_UNLOCK (session);
2628 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2629 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2634 /* Create a pad for receiving RTP for the session in @name. Must be called with
2638 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2642 GstRtpBinSession *session;
2644 /* first get the session number */
2645 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
2648 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2650 /* get or create session */
2651 session = find_session_by_id (rtpbin, sessid);
2653 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2654 /* create session now */
2655 session = create_session (rtpbin, sessid);
2656 if (session == NULL)
2660 /* check if pad was requested */
2661 if (session->recv_rtp_sink_ghost != NULL)
2662 return session->recv_rtp_sink_ghost;
2664 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2665 /* get recv_rtp pad and store */
2666 session->recv_rtp_sink =
2667 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2668 if (session->recv_rtp_sink == NULL)
2671 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2672 (GCallback) caps_changed, session);
2674 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2675 /* get srcpad, link to SSRCDemux */
2676 session->recv_rtp_src =
2677 gst_element_get_static_pad (session->session, "recv_rtp_src");
2678 if (session->recv_rtp_src == NULL)
2681 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2682 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2683 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2684 gst_pad_link_full (session->recv_rtp_src, sinkdpad,
2685 GST_PAD_LINK_CHECK_NOTHING);
2686 gst_object_unref (sinkdpad);
2688 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2689 session->demux_newpad_sig = g_signal_connect (session->demux,
2690 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2691 session->demux_padremoved_sig = g_signal_connect (session->demux,
2692 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2694 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2695 session->recv_rtp_sink_ghost =
2696 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2697 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2698 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2700 return session->recv_rtp_sink_ghost;
2705 g_warning ("rtpbin: invalid name given");
2710 /* create_session already warned */
2715 g_warning ("rtpbin: failed to get session pad");
2721 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2723 if (session->demux_newpad_sig) {
2724 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2725 session->demux_newpad_sig = 0;
2727 if (session->demux_padremoved_sig) {
2728 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2729 session->demux_padremoved_sig = 0;
2731 if (session->recv_rtp_src) {
2732 gst_object_unref (session->recv_rtp_src);
2733 session->recv_rtp_src = NULL;
2735 if (session->recv_rtp_sink) {
2736 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2737 gst_object_unref (session->recv_rtp_sink);
2738 session->recv_rtp_sink = NULL;
2740 if (session->recv_rtp_sink_ghost) {
2741 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2742 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2743 session->recv_rtp_sink_ghost);
2744 session->recv_rtp_sink_ghost = NULL;
2748 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2752 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2756 GstRtpBinSession *session;
2759 /* first get the session number */
2760 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
2763 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2765 /* get or create the session */
2766 session = find_session_by_id (rtpbin, sessid);
2768 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2769 /* create session now */
2770 session = create_session (rtpbin, sessid);
2771 if (session == NULL)
2775 /* check if pad was requested */
2776 if (session->recv_rtcp_sink_ghost != NULL)
2777 return session->recv_rtcp_sink_ghost;
2779 /* get recv_rtp pad and store */
2780 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2781 session->recv_rtcp_sink =
2782 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2783 if (session->recv_rtcp_sink == NULL)
2786 /* get srcpad, link to SSRCDemux */
2787 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2788 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2789 if (session->sync_src == NULL)
2792 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2793 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2794 gst_pad_link_full (session->sync_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
2795 gst_object_unref (sinkdpad);
2797 session->recv_rtcp_sink_ghost =
2798 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2799 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2800 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2801 session->recv_rtcp_sink_ghost);
2803 return session->recv_rtcp_sink_ghost;
2808 g_warning ("rtpbin: invalid name given");
2813 /* create_session already warned */
2818 g_warning ("rtpbin: failed to get session pad");
2824 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2826 if (session->recv_rtcp_sink_ghost) {
2827 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2828 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2829 session->recv_rtcp_sink_ghost);
2830 session->recv_rtcp_sink_ghost = NULL;
2832 if (session->sync_src) {
2833 /* releasing the request pad should also unref the sync pad */
2834 gst_object_unref (session->sync_src);
2835 session->sync_src = NULL;
2837 if (session->recv_rtcp_sink) {
2838 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2839 gst_object_unref (session->recv_rtcp_sink);
2840 session->recv_rtcp_sink = NULL;
2844 /* Create a pad for sending RTP for the session in @name. Must be called with
2848 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2852 GstRtpBinSession *session;
2853 GstElementClass *klass;
2855 /* first get the session number */
2856 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
2859 /* get or create session */
2860 session = find_session_by_id (rtpbin, sessid);
2862 /* create session now */
2863 session = create_session (rtpbin, sessid);
2864 if (session == NULL)
2868 /* check if pad was requested */
2869 if (session->send_rtp_sink_ghost != NULL)
2870 return session->send_rtp_sink_ghost;
2872 /* get send_rtp pad and store */
2873 session->send_rtp_sink =
2874 gst_element_get_request_pad (session->session, "send_rtp_sink");
2875 if (session->send_rtp_sink == NULL)
2878 session->send_rtp_sink_ghost =
2879 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2880 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2881 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2884 session->send_rtp_src =
2885 gst_element_get_static_pad (session->session, "send_rtp_src");
2886 if (session->send_rtp_src == NULL)
2889 /* ghost the new source pad */
2890 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2891 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
2892 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
2893 session->send_rtp_src_ghost =
2894 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2895 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2896 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2899 return session->send_rtp_sink_ghost;
2904 g_warning ("rtpbin: invalid name given");
2909 /* create_session already warned */
2914 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
2919 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
2925 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2927 if (session->send_rtp_src_ghost) {
2928 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2929 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2930 session->send_rtp_src_ghost);
2931 session->send_rtp_src_ghost = NULL;
2933 if (session->send_rtp_src) {
2934 gst_object_unref (session->send_rtp_src);
2935 session->send_rtp_src = NULL;
2937 if (session->send_rtp_sink) {
2938 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2939 session->send_rtp_sink);
2940 gst_object_unref (session->send_rtp_sink);
2941 session->send_rtp_sink = NULL;
2943 if (session->send_rtp_sink_ghost) {
2944 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2945 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2946 session->send_rtp_sink_ghost);
2947 session->send_rtp_sink_ghost = NULL;
2951 /* Create a pad for sending RTCP for the session in @name. Must be called with
2955 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2958 GstRtpBinSession *session;
2960 /* first get the session number */
2961 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
2964 /* get or create session */
2965 session = find_session_by_id (rtpbin, sessid);
2969 /* check if pad was requested */
2970 if (session->send_rtcp_src_ghost != NULL)
2971 return session->send_rtcp_src_ghost;
2973 /* get rtcp_src pad and store */
2974 session->send_rtcp_src =
2975 gst_element_get_request_pad (session->session, "send_rtcp_src");
2976 if (session->send_rtcp_src == NULL)
2979 session->send_rtcp_src_ghost =
2980 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2981 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2982 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2984 return session->send_rtcp_src_ghost;
2989 g_warning ("rtpbin: invalid name given");
2994 g_warning ("rtpbin: session with id %d does not exist", sessid);
2999 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
3005 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
3007 if (session->send_rtcp_src_ghost) {
3008 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
3009 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
3010 session->send_rtcp_src_ghost);
3011 session->send_rtcp_src_ghost = NULL;
3013 if (session->send_rtcp_src) {
3014 gst_element_release_request_pad (session->session, session->send_rtcp_src);
3015 gst_object_unref (session->send_rtcp_src);
3016 session->send_rtcp_src = NULL;
3020 /* If the requested name is NULL we should create a name with
3021 * the session number assuming we want the lowest posible session
3022 * with a free pad like the template */
3024 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
3026 gboolean name_found = FALSE;
3028 GstIterator *pad_it = NULL;
3029 gchar *pad_name = NULL;
3030 GValue data = { 0, };
3032 GST_DEBUG_OBJECT (element, "find a free pad name for template");
3033 while (!name_found) {
3034 gboolean done = FALSE;
3037 pad_name = g_strdup_printf (templ->name_template, session++);
3038 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
3041 switch (gst_iterator_next (pad_it, &data)) {
3042 case GST_ITERATOR_OK:
3047 pad = g_value_get_object (&data);
3048 name = gst_pad_get_name (pad);
3050 if (strcmp (name, pad_name) == 0) {
3055 g_value_reset (&data);
3058 case GST_ITERATOR_ERROR:
3059 case GST_ITERATOR_RESYNC:
3060 /* restart iteration */
3065 case GST_ITERATOR_DONE:
3070 g_value_unset (&data);
3071 gst_iterator_free (pad_it);
3074 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
3081 gst_rtp_bin_request_new_pad (GstElement * element,
3082 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
3085 GstElementClass *klass;
3088 gchar *pad_name = NULL;
3090 g_return_val_if_fail (templ != NULL, NULL);
3091 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
3093 rtpbin = GST_RTP_BIN (element);
3094 klass = GST_ELEMENT_GET_CLASS (element);
3096 GST_RTP_BIN_LOCK (rtpbin);
3099 /* use a free pad name */
3100 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
3102 /* use the provided name */
3103 pad_name = g_strdup (name);
3106 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
3108 /* figure out the template */
3109 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
3110 result = create_recv_rtp (rtpbin, templ, pad_name);
3111 } else if (templ == gst_element_class_get_pad_template (klass,
3112 "recv_rtcp_sink_%u")) {
3113 result = create_recv_rtcp (rtpbin, templ, pad_name);
3114 } else if (templ == gst_element_class_get_pad_template (klass,
3115 "send_rtp_sink_%u")) {
3116 result = create_send_rtp (rtpbin, templ, pad_name);
3117 } else if (templ == gst_element_class_get_pad_template (klass,
3118 "send_rtcp_src_%u")) {
3119 result = create_rtcp (rtpbin, templ, pad_name);
3121 goto wrong_template;
3124 GST_RTP_BIN_UNLOCK (rtpbin);
3132 GST_RTP_BIN_UNLOCK (rtpbin);
3133 g_warning ("rtpbin: this is not our template");
3139 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
3141 GstRtpBinSession *session;
3144 g_return_if_fail (GST_IS_GHOST_PAD (pad));
3145 g_return_if_fail (GST_IS_RTP_BIN (element));
3147 rtpbin = GST_RTP_BIN (element);
3149 GST_RTP_BIN_LOCK (rtpbin);
3150 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
3151 GST_DEBUG_PAD_NAME (pad));
3153 if (!(session = find_session_by_pad (rtpbin, pad)))
3156 if (session->recv_rtp_sink_ghost == pad) {
3157 remove_recv_rtp (rtpbin, session);
3158 } else if (session->recv_rtcp_sink_ghost == pad) {
3159 remove_recv_rtcp (rtpbin, session);
3160 } else if (session->send_rtp_sink_ghost == pad) {
3161 remove_send_rtp (rtpbin, session);
3162 } else if (session->send_rtcp_src_ghost == pad) {
3163 remove_rtcp (rtpbin, session);
3166 /* no more request pads, free the complete session */
3167 if (session->recv_rtp_sink_ghost == NULL
3168 && session->recv_rtcp_sink_ghost == NULL
3169 && session->send_rtp_sink_ghost == NULL
3170 && session->send_rtcp_src_ghost == NULL) {
3171 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
3172 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
3173 free_session (session, rtpbin);
3175 GST_RTP_BIN_UNLOCK (rtpbin);
3182 GST_RTP_BIN_UNLOCK (rtpbin);
3183 g_warning ("rtpbin: %s:%s is not one of our request pads",
3184 GST_DEBUG_PAD_NAME (pad));