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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-gstrtpbin
22 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
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_%%d pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_%%d 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 * #GstRtpsSrcDemux element. The #GstRtpsSrcDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_%%d_%%d_%%d on
39 * gstrtpbin 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_%%d 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_%%d 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_%%d pad, which will
51 * automatically create a send_rtp_src_%%d 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_%%d 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
62 * <title>Example pipelines</title>
64 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
65 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
66 * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
68 * gst-launch gstrtpbin name=rtpbin \
69 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
70 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
71 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
72 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
73 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
74 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
75 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
76 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
77 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
78 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
79 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
80 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
81 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
82 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
83 * is received on port 5007. Since RTCP packets from the sender should be sent
84 * as soon as possible and do not participate in preroll, sync=false and
85 * async=false is configured on udpsink
87 * gst-launch -v gstrtpbin name=rtpbin \
88 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
89 * port=5000 ! rtpbin.recv_rtp_sink_0 \
90 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
91 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
92 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
93 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
94 * port=5002 ! rtpbin.recv_rtp_sink_1 \
95 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
96 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
97 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
98 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
99 * decode and display the video.
100 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
101 * decode and play the audio.
102 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
103 * session 1 on port 5003. These packets will be used for session management and
105 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
109 * Last reviewed on 2007-08-30 (0.10.6)
117 #include <gst/rtp/gstrtpbuffer.h>
118 #include <gst/rtp/gstrtcpbuffer.h>
120 #include "gstrtpbin-marshal.h"
121 #include "gstrtpbin.h"
122 #include "rtpsession.h"
123 #include "gstrtpsession.h"
124 #include "gstrtpjitterbuffer.h"
126 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
127 #define GST_CAT_DEFAULT gst_rtp_bin_debug
129 /* elementfactory information */
130 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
131 "Filter/Network/RTP",
132 "Implement an RTP bin",
133 "Wim Taymans <wim.taymans@gmail.com>");
136 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
137 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
140 GST_STATIC_CAPS ("application/x-rtp")
143 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
144 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
147 GST_STATIC_CAPS ("application/x-rtcp")
150 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
154 GST_STATIC_CAPS ("application/x-rtp")
158 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
159 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
162 GST_STATIC_CAPS ("application/x-rtp")
165 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
166 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
169 GST_STATIC_CAPS ("application/x-rtcp")
172 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
176 GST_STATIC_CAPS ("application/x-rtp")
179 #define GST_RTP_BIN_GET_PRIVATE(obj) \
180 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
182 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
183 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
185 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
186 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock ((bin)->priv->dyn_lock)
187 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->dyn_lock)
189 /* lock for shutdown */
190 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
192 if (g_atomic_int_get (&bin->priv->shutdown)) \
194 GST_RTP_BIN_DYN_LOCK (bin); \
195 if (g_atomic_int_get (&bin->priv->shutdown)) { \
196 GST_RTP_BIN_DYN_UNLOCK (bin); \
201 /* unlock for shutdown */
202 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
203 GST_RTP_BIN_DYN_UNLOCK (bin); \
205 struct _GstRtpBinPrivate
209 /* lock protecting dynamic adding/removing */
212 /* the time when we went to playing */
213 GstClockTime ntp_ns_base;
215 /* if we are shutting down or not */
219 /* signals and args */
222 SIGNAL_REQUEST_PT_MAP,
225 SIGNAL_GET_INTERNAL_SESSION,
228 SIGNAL_ON_SSRC_COLLISION,
229 SIGNAL_ON_SSRC_VALIDATED,
230 SIGNAL_ON_SSRC_ACTIVE,
233 SIGNAL_ON_BYE_TIMEOUT,
235 SIGNAL_ON_SENDER_TIMEOUT,
240 #define DEFAULT_LATENCY_MS 200
241 #define DEFAULT_SDES_CNAME NULL
242 #define DEFAULT_SDES_NAME NULL
243 #define DEFAULT_SDES_EMAIL NULL
244 #define DEFAULT_SDES_PHONE NULL
245 #define DEFAULT_SDES_LOCATION NULL
246 #define DEFAULT_SDES_TOOL NULL
247 #define DEFAULT_SDES_NOTE NULL
248 #define DEFAULT_DO_LOST FALSE
266 typedef struct _GstRtpBinSession GstRtpBinSession;
267 typedef struct _GstRtpBinStream GstRtpBinStream;
268 typedef struct _GstRtpBinClient GstRtpBinClient;
270 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
272 static GstCaps *pt_map_requested (GstElement * element, guint pt,
273 GstRtpBinSession * session);
274 static const gchar *sdes_type_to_name (GstRTCPSDESType type);
275 static void gst_rtp_bin_set_sdes_string (GstRtpBin * bin,
276 GstRTCPSDESType type, const gchar * data);
278 static void free_stream (GstRtpBinStream * stream);
280 /* Manages the RTP stream for one SSRC.
282 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
283 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
284 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
285 * together (see below).
287 struct _GstRtpBinStream
289 /* the SSRC of this stream */
295 /* the session this SSRC belongs to */
296 GstRtpBinSession *session;
298 /* the jitterbuffer of the SSRC */
301 /* the PT demuxer of the SSRC */
303 gulong demux_newpad_sig;
304 gulong demux_ptreq_sig;
305 gulong demux_pt_change_sig;
307 /* if we have calculated a valid unix_delta for this stream */
309 /* mapping to local RTP and NTP time */
313 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
314 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
316 /* Manages the receiving end of the packets.
318 * There is one such structure for each RTP session (audio/video/...).
319 * We get the RTP/RTCP packets and stuff them into the session manager. From
320 * there they are pushed into an SSRC demuxer that splits the stream based on
321 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
322 * the GstRtpBinStream above).
324 struct _GstRtpBinSession
330 /* the session element */
332 /* the SSRC demuxer */
334 gulong demux_newpad_sig;
338 /* list of GstRtpBinStream */
341 /* mapping of payload type to caps */
344 /* the pads of the session */
345 GstPad *recv_rtp_sink;
346 GstPad *recv_rtp_src;
347 GstPad *recv_rtcp_sink;
349 GstPad *send_rtp_sink;
350 GstPad *send_rtp_src;
351 GstPad *send_rtp_src_ghost;
352 GstPad *send_rtcp_src;
355 /* Manages the RTP streams that come from one client and should therefore be
358 struct _GstRtpBinClient
360 /* the common CNAME for the streams */
369 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
370 static GstRtpBinSession *
371 find_session_by_id (GstRtpBin * rtpbin, gint id)
375 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
376 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
384 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
385 static GstRtpBinSession *
386 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
390 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
391 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
393 if ((sess->recv_rtp_sink == pad) ||
394 (sess->recv_rtcp_sink == pad) ||
395 (sess->send_rtp_sink == pad) || (sess->send_rtcp_src == pad))
402 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
404 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
409 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
411 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
416 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
418 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
423 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
425 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
430 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
432 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
437 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
439 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
444 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
446 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
451 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
453 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
458 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
460 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
465 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
467 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
468 stream->session->id, stream->ssrc);
471 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
472 static GstRtpBinSession *
473 create_session (GstRtpBin * rtpbin, gint id)
475 GstRtpBinSession *sess;
476 GstElement *session, *demux;
479 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
482 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
485 sess = g_new0 (GstRtpBinSession, 1);
486 sess->lock = g_mutex_new ();
489 sess->session = session;
491 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
492 (GDestroyNotify) gst_caps_unref);
493 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
495 /* set NTP base or new session */
496 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
497 /* configure SDES items */
498 GST_OBJECT_LOCK (rtpbin);
499 for (i = GST_RTCP_SDES_CNAME; i < GST_RTCP_SDES_PRIV; i++) {
500 g_object_set (session, sdes_type_to_name (i), rtpbin->sdes[i], NULL);
502 GST_OBJECT_UNLOCK (rtpbin);
504 /* provide clock_rate to the session manager when needed */
505 g_signal_connect (session, "request-pt-map",
506 (GCallback) pt_map_requested, sess);
508 g_signal_connect (sess->session, "on-new-ssrc",
509 (GCallback) on_new_ssrc, sess);
510 g_signal_connect (sess->session, "on-ssrc-collision",
511 (GCallback) on_ssrc_collision, sess);
512 g_signal_connect (sess->session, "on-ssrc-validated",
513 (GCallback) on_ssrc_validated, sess);
514 g_signal_connect (sess->session, "on-ssrc-active",
515 (GCallback) on_ssrc_active, sess);
516 g_signal_connect (sess->session, "on-ssrc-sdes",
517 (GCallback) on_ssrc_sdes, sess);
518 g_signal_connect (sess->session, "on-bye-ssrc",
519 (GCallback) on_bye_ssrc, sess);
520 g_signal_connect (sess->session, "on-bye-timeout",
521 (GCallback) on_bye_timeout, sess);
522 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
523 g_signal_connect (sess->session, "on-sender-timeout",
524 (GCallback) on_sender_timeout, sess);
526 /* FIXME, change state only to what's needed */
527 gst_bin_add (GST_BIN_CAST (rtpbin), session);
528 gst_element_set_state (session, GST_STATE_PLAYING);
529 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
530 gst_element_set_state (demux, GST_STATE_PLAYING);
537 g_warning ("gstrtpbin: could not create gstrtpsession element");
542 gst_object_unref (session);
543 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
549 free_session (GstRtpBinSession * sess)
555 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
557 gst_element_set_state (sess->demux, GST_STATE_NULL);
558 gst_element_set_state (sess->session, GST_STATE_NULL);
560 if (sess->recv_rtp_sink != NULL) {
561 gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
562 gst_object_unref (sess->recv_rtp_sink);
564 if (sess->recv_rtp_src != NULL)
565 gst_object_unref (sess->recv_rtp_src);
566 if (sess->recv_rtcp_sink != NULL) {
567 gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
568 gst_object_unref (sess->recv_rtcp_sink);
570 if (sess->sync_src != NULL)
571 gst_object_unref (sess->sync_src);
572 if (sess->send_rtp_sink != NULL) {
573 gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
574 gst_object_unref (sess->send_rtp_sink);
576 if (sess->send_rtp_src != NULL)
577 gst_object_unref (sess->send_rtp_src);
578 if (sess->send_rtcp_src != NULL) {
579 gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
580 gst_object_unref (sess->send_rtcp_src);
583 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
584 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
586 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
587 g_slist_free (sess->streams);
589 g_mutex_free (sess->lock);
590 g_hash_table_destroy (sess->ptmap);
596 static GstRtpBinStream *
597 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
601 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
602 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
604 if (stream->ssrc == ssrc)
611 /* get the payload type caps for the specific payload @pt in @session */
613 get_pt_map (GstRtpBinSession * session, guint pt)
615 GstCaps *caps = NULL;
618 GValue args[3] = { {0}, {0}, {0} };
620 GST_DEBUG ("searching pt %d in cache", pt);
622 GST_RTP_SESSION_LOCK (session);
624 /* first look in the cache */
625 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
633 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
635 /* not in cache, send signal to request caps */
636 g_value_init (&args[0], GST_TYPE_ELEMENT);
637 g_value_set_object (&args[0], bin);
638 g_value_init (&args[1], G_TYPE_UINT);
639 g_value_set_uint (&args[1], session->id);
640 g_value_init (&args[2], G_TYPE_UINT);
641 g_value_set_uint (&args[2], pt);
643 g_value_init (&ret, GST_TYPE_CAPS);
644 g_value_set_boxed (&ret, NULL);
646 GST_RTP_SESSION_UNLOCK (session);
648 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
650 GST_RTP_SESSION_LOCK (session);
652 g_value_unset (&args[0]);
653 g_value_unset (&args[1]);
654 g_value_unset (&args[2]);
656 /* look in the cache again because we let the lock go */
657 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
660 g_value_unset (&ret);
664 caps = (GstCaps *) g_value_dup_boxed (&ret);
665 g_value_unset (&ret);
669 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
671 /* store in cache, take additional ref */
672 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
673 gst_caps_ref (caps));
676 GST_RTP_SESSION_UNLOCK (session);
683 GST_RTP_SESSION_UNLOCK (session);
684 GST_DEBUG ("no pt map could be obtained");
690 return_true (gpointer key, gpointer value, gpointer user_data)
696 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
698 GSList *clients, *streams;
700 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
702 GST_RTP_BIN_LOCK (rtpbin);
703 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
704 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
706 /* reset sync on all streams for this client */
707 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
708 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
710 /* make use require a new SR packet for this stream before we attempt new
712 stream->have_sync = FALSE;
713 stream->unix_delta = 0;
716 GST_RTP_BIN_UNLOCK (rtpbin);
720 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
722 GSList *sessions, *streams;
724 GST_RTP_BIN_LOCK (bin);
725 GST_DEBUG_OBJECT (bin, "clearing pt map");
726 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
727 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
729 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
730 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
732 GST_RTP_SESSION_LOCK (session);
733 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
735 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
736 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
738 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
739 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
740 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
742 GST_RTP_SESSION_UNLOCK (session);
744 GST_RTP_BIN_UNLOCK (bin);
747 gst_rtp_bin_reset_sync (bin);
751 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
753 RTPSession *internal_session = NULL;
754 GstRtpBinSession *session;
756 GST_RTP_BIN_LOCK (bin);
757 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
759 session = find_session_by_id (bin, (gint) session_id);
761 g_object_get (session->session, "internal-session", &internal_session,
764 GST_RTP_BIN_UNLOCK (bin);
766 return internal_session;
770 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
771 const gchar * name, const GValue * value)
773 GSList *sessions, *streams;
775 GST_RTP_BIN_LOCK (bin);
776 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
777 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
779 GST_RTP_SESSION_LOCK (session);
780 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
781 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
783 g_object_set_property (G_OBJECT (stream->buffer), name, value);
785 GST_RTP_SESSION_UNLOCK (session);
787 GST_RTP_BIN_UNLOCK (bin);
790 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
791 static GstRtpBinClient *
792 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
794 GstRtpBinClient *result = NULL;
797 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
798 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
800 if (len != client->cname_len)
803 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
804 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
811 /* nothing found, create one */
812 if (result == NULL) {
813 result = g_new0 (GstRtpBinClient, 1);
814 result->cname = g_strndup ((gchar *) data, len);
815 result->cname_len = len;
816 bin->clients = g_slist_prepend (bin->clients, result);
817 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
824 free_client (GstRtpBinClient * client)
826 g_slist_free (client->streams);
827 g_free (client->cname);
831 /* associate a stream to the given CNAME. This will make sure all streams for
832 * that CNAME are synchronized together.
833 * Must be called with GST_RTP_BIN_LOCK */
835 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
836 guint8 * data, guint64 last_unix, guint64 last_extrtptime,
837 guint64 clock_base, guint64 clock_base_time, guint clock_rate)
839 GstRtpBinClient *client;
845 /* first find or create the CNAME */
846 client = get_client (bin, len, data, &created);
848 /* find stream in the client */
849 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
850 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
852 if (ostream == stream)
855 /* not found, add it to the list */
857 GST_DEBUG_OBJECT (bin,
858 "new association of SSRC %08x with client %p with CNAME %s",
859 stream->ssrc, client, client->cname);
860 client->streams = g_slist_prepend (client->streams, stream);
863 GST_DEBUG_OBJECT (bin,
864 "found association of SSRC %08x with client %p with CNAME %s",
865 stream->ssrc, client, client->cname);
868 /* take the extended rtptime we found in the SR packet and map it to the
869 * local rtptime. The local rtp time is used to construct timestamps on the
871 local_rtp = last_extrtptime - clock_base;
873 GST_DEBUG_OBJECT (bin,
874 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
875 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", clock_base,
876 last_extrtptime, local_rtp, clock_rate);
878 /* calculate local NTP time in gstreamer timestamp, we essentially perform the
879 * same conversion that a jitterbuffer would use to convert an rtp timestamp
880 * into a corresponding gstreamer timestamp. */
881 local_unix = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
882 local_unix += clock_base_time;
884 /* calculate delta between server and receiver. last_unix is created by
885 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
886 * delta expresses the difference to our timeline and the server timeline. */
887 stream->unix_delta = last_unix - local_unix;
888 stream->have_sync = TRUE;
890 GST_DEBUG_OBJECT (bin,
891 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
892 ", delta %" G_GINT64_FORMAT, local_unix, last_unix, stream->unix_delta);
894 /* recalc inter stream playout offset, but only if there is more than one
896 if (client->nstreams > 1) {
899 /* calculate the min of all deltas, ignoring streams that did not yet have a
900 * valid unix_delta because we did not yet receive an SR packet for those
902 * We calculate the mininum because we would like to only apply positive
903 * offsets to streams, delaying their playback instead of trying to speed up
904 * other streams (which might be imposible when we have to create negative
906 * The stream that has the smallest diff is selected as the reference stream,
907 * all other streams will have a positive offset to this difference. */
909 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
910 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
912 if (!ostream->have_sync)
915 if (ostream->unix_delta < min)
916 min = ostream->unix_delta;
919 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
922 /* calculate offsets for each stream */
923 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
924 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
925 gint64 ts_offset, prev_ts_offset;
927 /* ignore streams for which we didn't receive an SR packet yet, we
928 * can't synchronize them yet. We can however sync other streams just
930 if (!ostream->have_sync)
933 /* calculate offset to our reference stream, this should always give a
934 * positive number. */
935 ts_offset = ostream->unix_delta - min;
937 g_object_get (ostream->buffer, "ts-offset", &prev_ts_offset, NULL);
939 /* delta changed, see how much */
940 if (prev_ts_offset != ts_offset) {
943 if (prev_ts_offset > ts_offset)
944 diff = prev_ts_offset - ts_offset;
946 diff = ts_offset - prev_ts_offset;
948 GST_DEBUG_OBJECT (bin,
949 "ts-offset %" G_GUINT64_FORMAT ", prev %" G_GUINT64_FORMAT
950 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
952 /* only change diff when it changed more than 4 milliseconds. This
953 * compensates for rounding errors in NTP to RTP timestamp
955 if (diff > 4 * GST_MSECOND && diff < (3 * GST_SECOND)) {
956 g_object_set (ostream->buffer, "ts-offset", ts_offset, NULL);
959 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
960 ostream->ssrc, ts_offset);
966 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
967 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
968 (b) = gst_rtcp_packet_move_to_next ((packet)))
970 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
971 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
972 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
974 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
975 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
976 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
979 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
980 GstRtpBinStream * stream)
983 GstRTCPPacket packet;
986 gboolean have_sr, have_sdes;
989 guint64 clock_base_time;
996 GST_DEBUG_OBJECT (bin, "sync handler called");
998 /* get the last relation between the rtp timestamps and the gstreamer
999 * timestamps. We get this info directly from the jitterbuffer which
1000 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1001 * what the current situation is. */
1002 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1004 g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1005 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1007 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1008 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1012 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
1013 /* first packet must be SR or RR or else the validate would have failed */
1014 switch (gst_rtcp_packet_get_type (&packet)) {
1015 case GST_RTCP_TYPE_SR:
1016 /* only parse first. There is only supposed to be one SR in the packet
1017 * but we will deal with malformed packets gracefully */
1020 /* get NTP and RTP times */
1021 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1024 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1025 /* ignore SR that is not ours */
1026 if (ssrc != stream->ssrc)
1031 case GST_RTCP_TYPE_SDES:
1033 gboolean more_items, more_entries;
1035 /* only deal with first SDES, there is only supposed to be one SDES in
1036 * the RTCP packet but we deal with bad packets gracefully. Also bail
1037 * out if we have not seen an SR item yet. */
1038 if (have_sdes || !have_sr)
1041 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1042 /* skip items that are not about the SSRC of the sender */
1043 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1046 /* find the CNAME entry */
1047 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1048 GstRTCPSDESType type;
1052 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1054 if (type == GST_RTCP_SDES_CNAME) {
1055 GST_RTP_BIN_LOCK (bin);
1056 /* associate the stream to CNAME */
1057 gst_rtp_bin_associate (bin, stream, len, data,
1058 gst_rtcp_ntp_to_unix (ntptime), extrtptime,
1059 clock_base, clock_base_time, clock_rate);
1060 GST_RTP_BIN_UNLOCK (bin);
1068 /* we can ignore these packets */
1074 /* create a new stream with @ssrc in @session. Must be called with
1075 * RTP_SESSION_LOCK. */
1076 static GstRtpBinStream *
1077 create_stream (GstRtpBinSession * session, guint32 ssrc)
1079 GstElement *buffer, *demux;
1080 GstRtpBinStream *stream;
1082 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
1083 goto no_jitterbuffer;
1085 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
1088 stream = g_new0 (GstRtpBinStream, 1);
1089 stream->ssrc = ssrc;
1090 stream->bin = session->bin;
1091 stream->session = session;
1092 stream->buffer = buffer;
1093 stream->demux = demux;
1094 stream->have_sync = FALSE;
1095 stream->unix_delta = 0;
1096 session->streams = g_slist_prepend (session->streams, stream);
1098 /* provide clock_rate to the jitterbuffer when needed */
1099 g_signal_connect (buffer, "request-pt-map",
1100 (GCallback) pt_map_requested, session);
1101 g_signal_connect (buffer, "on-npt-stop", (GCallback) on_npt_stop, stream);
1103 /* configure latency and packet lost */
1104 g_object_set (buffer, "latency", session->bin->latency, NULL);
1105 g_object_set (buffer, "do-lost", session->bin->do_lost, NULL);
1107 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
1108 gst_element_set_state (buffer, GST_STATE_PLAYING);
1109 gst_bin_add (GST_BIN_CAST (session->bin), demux);
1110 gst_element_set_state (demux, GST_STATE_PLAYING);
1113 gst_element_link (buffer, demux);
1120 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1125 gst_object_unref (buffer);
1126 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1132 free_stream (GstRtpBinStream * stream)
1134 GstRtpBinSession *session;
1136 session = stream->session;
1138 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1139 gst_element_set_state (stream->demux, GST_STATE_NULL);
1141 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1142 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1144 session->streams = g_slist_remove (session->streams, stream);
1149 /* GObject vmethods */
1150 static void gst_rtp_bin_dispose (GObject * object);
1151 static void gst_rtp_bin_finalize (GObject * object);
1152 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1153 const GValue * value, GParamSpec * pspec);
1154 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1155 GValue * value, GParamSpec * pspec);
1157 /* GstElement vmethods */
1158 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1159 GstStateChange transition);
1160 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1161 GstPadTemplate * templ, const gchar * name);
1162 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1163 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1164 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
1166 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1169 gst_rtp_bin_base_init (gpointer klass)
1171 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1174 gst_element_class_add_pad_template (element_class,
1175 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1176 gst_element_class_add_pad_template (element_class,
1177 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1178 gst_element_class_add_pad_template (element_class,
1179 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1182 gst_element_class_add_pad_template (element_class,
1183 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1184 gst_element_class_add_pad_template (element_class,
1185 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1186 gst_element_class_add_pad_template (element_class,
1187 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1189 gst_element_class_set_details (element_class, &rtpbin_details);
1193 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1195 GObjectClass *gobject_class;
1196 GstElementClass *gstelement_class;
1197 GstBinClass *gstbin_class;
1199 gobject_class = (GObjectClass *) klass;
1200 gstelement_class = (GstElementClass *) klass;
1201 gstbin_class = (GstBinClass *) klass;
1203 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1205 gobject_class->dispose = gst_rtp_bin_dispose;
1206 gobject_class->finalize = gst_rtp_bin_finalize;
1207 gobject_class->set_property = gst_rtp_bin_set_property;
1208 gobject_class->get_property = gst_rtp_bin_get_property;
1210 g_object_class_install_property (gobject_class, PROP_LATENCY,
1211 g_param_spec_uint ("latency", "Buffer latency in ms",
1212 "Default amount of ms to buffer in the jitterbuffers", 0,
1213 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1216 * GstRtpBin::request-pt-map:
1217 * @rtpbin: the object which received the signal
1218 * @session: the session
1221 * Request the payload type as #GstCaps for @pt in @session.
1223 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1224 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1225 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1226 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1227 G_TYPE_UINT, G_TYPE_UINT);
1229 * GstRtpBin::clear-pt-map:
1230 * @rtpbin: the object which received the signal
1232 * Clear all previously cached pt-mapping obtained with
1233 * #GstRtpBin::request-pt-map.
1235 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1236 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1237 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1238 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1241 * GstRtpBin::reset-sync:
1242 * @rtpbin: the object which received the signal
1244 * Reset all currently configured lip-sync parameters and require new SR
1245 * packets for all streams before lip-sync is attempted again.
1247 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1248 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1249 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1250 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1254 * GstRtpBin::get-internal-session:
1255 * @rtpbin: the object which received the signal
1256 * @id: the session id
1258 * Request the internal RTPSession object as #GObject in session @id.
1260 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1261 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1262 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1263 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1264 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1267 * GstRtpBin::on-new-ssrc:
1268 * @rtpbin: the object which received the signal
1269 * @session: the session
1272 * Notify of a new SSRC that entered @session.
1274 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1275 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1276 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1277 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1278 G_TYPE_UINT, G_TYPE_UINT);
1280 * GstRtpBin::on-ssrc-collision:
1281 * @rtpbin: the object which received the signal
1282 * @session: the session
1285 * Notify when we have an SSRC collision
1287 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1288 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1289 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1290 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1291 G_TYPE_UINT, G_TYPE_UINT);
1293 * GstRtpBin::on-ssrc-validated:
1294 * @rtpbin: the object which received the signal
1295 * @session: the session
1298 * Notify of a new SSRC that became validated.
1300 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1301 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1302 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1303 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1304 G_TYPE_UINT, G_TYPE_UINT);
1306 * GstRtpBin::on-ssrc-active:
1307 * @rtpbin: the object which received the signal
1308 * @session: the session
1311 * Notify of a SSRC that is active, i.e., sending RTCP.
1313 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1314 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1315 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1316 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1317 G_TYPE_UINT, G_TYPE_UINT);
1319 * GstRtpBin::on-ssrc-sdes:
1320 * @rtpbin: the object which received the signal
1321 * @session: the session
1324 * Notify of a SSRC that is active, i.e., sending RTCP.
1326 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1327 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1328 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1329 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1330 G_TYPE_UINT, G_TYPE_UINT);
1333 * GstRtpBin::on-bye-ssrc:
1334 * @rtpbin: the object which received the signal
1335 * @session: the session
1338 * Notify of an SSRC that became inactive because of a BYE packet.
1340 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1341 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1342 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1343 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1344 G_TYPE_UINT, G_TYPE_UINT);
1346 * GstRtpBin::on-bye-timeout:
1347 * @rtpbin: the object which received the signal
1348 * @session: the session
1351 * Notify of an SSRC that has timed out because of BYE
1353 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1354 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1355 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1356 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1357 G_TYPE_UINT, G_TYPE_UINT);
1359 * GstRtpBin::on-timeout:
1360 * @rtpbin: the object which received the signal
1361 * @session: the session
1364 * Notify of an SSRC that has timed out
1366 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1367 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1368 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1369 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1370 G_TYPE_UINT, G_TYPE_UINT);
1372 * GstRtpBin::on-sender-timeout:
1373 * @rtpbin: the object which received the signal
1374 * @session: the session
1377 * Notify of a sender SSRC that has timed out and became a receiver
1379 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1380 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1381 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1382 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1383 G_TYPE_UINT, G_TYPE_UINT);
1386 * GstRtpBin::on-npt-stop:
1387 * @rtpbin: the object which received the signal
1388 * @session: the session
1391 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1393 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1394 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1395 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1396 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1397 G_TYPE_UINT, G_TYPE_UINT);
1399 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
1400 g_param_spec_string ("sdes-cname", "SDES CNAME",
1401 "The CNAME to put in SDES messages of this session",
1402 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
1404 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
1405 g_param_spec_string ("sdes-name", "SDES NAME",
1406 "The NAME to put in SDES messages of this session",
1407 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
1409 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
1410 g_param_spec_string ("sdes-email", "SDES EMAIL",
1411 "The EMAIL to put in SDES messages of this session",
1412 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
1414 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
1415 g_param_spec_string ("sdes-phone", "SDES PHONE",
1416 "The PHONE to put in SDES messages of this session",
1417 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
1419 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
1420 g_param_spec_string ("sdes-location", "SDES LOCATION",
1421 "The LOCATION to put in SDES messages of this session",
1422 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
1424 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
1425 g_param_spec_string ("sdes-tool", "SDES TOOL",
1426 "The TOOL to put in SDES messages of this session",
1427 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
1429 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
1430 g_param_spec_string ("sdes-note", "SDES NOTE",
1431 "The NOTE to put in SDES messages of this session",
1432 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
1434 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1435 g_param_spec_boolean ("do-lost", "Do Lost",
1436 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1437 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1439 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1440 gstelement_class->request_new_pad =
1441 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1442 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1444 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1446 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1447 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1448 klass->get_internal_session =
1449 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1451 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1455 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1459 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1460 rtpbin->priv->bin_lock = g_mutex_new ();
1461 rtpbin->priv->dyn_lock = g_mutex_new ();
1463 rtpbin->latency = DEFAULT_LATENCY_MS;
1464 rtpbin->do_lost = DEFAULT_DO_LOST;
1466 /* some default SDES entries */
1467 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1468 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME, str);
1471 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME, g_get_real_name ());
1472 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL, "GStreamer");
1476 gst_rtp_bin_dispose (GObject * object)
1480 rtpbin = GST_RTP_BIN (object);
1482 GST_DEBUG_OBJECT (object, "freeing sessions");
1483 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1484 g_slist_free (rtpbin->sessions);
1485 rtpbin->sessions = NULL;
1486 GST_DEBUG_OBJECT (object, "freeing clients");
1487 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1488 g_slist_free (rtpbin->clients);
1489 rtpbin->clients = NULL;
1491 G_OBJECT_CLASS (parent_class)->dispose (object);
1495 gst_rtp_bin_finalize (GObject * object)
1500 rtpbin = GST_RTP_BIN (object);
1502 for (i = 0; i < 9; i++)
1503 g_free (rtpbin->sdes[i]);
1505 g_mutex_free (rtpbin->priv->bin_lock);
1506 g_mutex_free (rtpbin->priv->dyn_lock);
1508 G_OBJECT_CLASS (parent_class)->finalize (object);
1511 static const gchar *
1512 sdes_type_to_name (GstRTCPSDESType type)
1514 const gchar *result;
1517 case GST_RTCP_SDES_CNAME:
1518 result = "sdes-cname";
1520 case GST_RTCP_SDES_NAME:
1521 result = "sdes-name";
1523 case GST_RTCP_SDES_EMAIL:
1524 result = "sdes-email";
1526 case GST_RTCP_SDES_PHONE:
1527 result = "sdes-phone";
1529 case GST_RTCP_SDES_LOC:
1530 result = "sdes-location";
1532 case GST_RTCP_SDES_TOOL:
1533 result = "sdes-tool";
1535 case GST_RTCP_SDES_NOTE:
1536 result = "sdes-note";
1538 case GST_RTCP_SDES_PRIV:
1539 result = "sdes-priv";
1549 gst_rtp_bin_set_sdes_string (GstRtpBin * bin, GstRTCPSDESType type,
1555 if (type < 0 || type > 8)
1558 GST_RTP_BIN_LOCK (bin);
1560 GST_OBJECT_LOCK (bin);
1561 g_free (bin->sdes[type]);
1562 bin->sdes[type] = g_strdup (data);
1563 name = sdes_type_to_name (type);
1564 /* store in all sessions */
1565 for (item = bin->sessions; item; item = g_slist_next (item))
1566 g_object_set (item->data, name, bin->sdes[type], NULL);
1567 GST_OBJECT_UNLOCK (bin);
1569 GST_RTP_BIN_UNLOCK (bin);
1573 gst_rtp_bin_get_sdes_string (GstRtpBin * bin, GstRTCPSDESType type)
1577 if (type < 0 || type > 8)
1580 GST_OBJECT_LOCK (bin);
1581 result = g_strdup (bin->sdes[type]);
1582 GST_OBJECT_UNLOCK (bin);
1588 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1589 const GValue * value, GParamSpec * pspec)
1593 rtpbin = GST_RTP_BIN (object);
1597 GST_RTP_BIN_LOCK (rtpbin);
1598 rtpbin->latency = g_value_get_uint (value);
1599 GST_RTP_BIN_UNLOCK (rtpbin);
1600 /* propegate the property down to the jitterbuffer */
1601 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1603 case PROP_SDES_CNAME:
1604 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_CNAME,
1605 g_value_get_string (value));
1607 case PROP_SDES_NAME:
1608 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NAME,
1609 g_value_get_string (value));
1611 case PROP_SDES_EMAIL:
1612 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_EMAIL,
1613 g_value_get_string (value));
1615 case PROP_SDES_PHONE:
1616 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_PHONE,
1617 g_value_get_string (value));
1619 case PROP_SDES_LOCATION:
1620 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_LOC,
1621 g_value_get_string (value));
1623 case PROP_SDES_TOOL:
1624 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_TOOL,
1625 g_value_get_string (value));
1627 case PROP_SDES_NOTE:
1628 gst_rtp_bin_set_sdes_string (rtpbin, GST_RTCP_SDES_NOTE,
1629 g_value_get_string (value));
1632 GST_RTP_BIN_LOCK (rtpbin);
1633 rtpbin->do_lost = g_value_get_boolean (value);
1634 GST_RTP_BIN_UNLOCK (rtpbin);
1635 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1644 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1645 GValue * value, GParamSpec * pspec)
1649 rtpbin = GST_RTP_BIN (object);
1653 GST_RTP_BIN_LOCK (rtpbin);
1654 g_value_set_uint (value, rtpbin->latency);
1655 GST_RTP_BIN_UNLOCK (rtpbin);
1657 case PROP_SDES_CNAME:
1658 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1659 GST_RTCP_SDES_CNAME));
1661 case PROP_SDES_NAME:
1662 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1663 GST_RTCP_SDES_NAME));
1665 case PROP_SDES_EMAIL:
1666 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1667 GST_RTCP_SDES_EMAIL));
1669 case PROP_SDES_PHONE:
1670 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1671 GST_RTCP_SDES_PHONE));
1673 case PROP_SDES_LOCATION:
1674 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1675 GST_RTCP_SDES_LOC));
1677 case PROP_SDES_TOOL:
1678 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1679 GST_RTCP_SDES_TOOL));
1681 case PROP_SDES_NOTE:
1682 g_value_take_string (value, gst_rtp_bin_get_sdes_string (rtpbin,
1683 GST_RTCP_SDES_NOTE));
1686 GST_RTP_BIN_LOCK (rtpbin);
1687 g_value_set_boolean (value, rtpbin->do_lost);
1688 GST_RTP_BIN_UNLOCK (rtpbin);
1691 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1697 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1701 rtpbin = GST_RTP_BIN (bin);
1703 switch (GST_MESSAGE_TYPE (message)) {
1704 case GST_MESSAGE_ELEMENT:
1706 const GstStructure *s = gst_message_get_structure (message);
1708 /* we change the structure name and add the session ID to it */
1709 if (gst_structure_has_name (s, "GstRTPSessionSDES")) {
1712 /* find the session, the message source has it */
1713 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
1714 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
1716 /* if we found the session, change message. else we exit the loop and
1717 * leave the message unchanged */
1718 if (GST_OBJECT_CAST (sess->session) == GST_MESSAGE_SRC (message)) {
1719 message = gst_message_make_writable (message);
1720 s = gst_message_get_structure (message);
1722 gst_structure_set_name ((GstStructure *) s, "GstRTPBinSDES");
1724 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1730 /* fallthrough to forward the modified message to the parent */
1734 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1741 calc_ntp_ns_base (GstRtpBin * bin)
1747 /* get the current time and convert it to NTP time in nanoseconds */
1748 g_get_current_time (¤t);
1749 now = GST_TIMEVAL_TO_TIME (current);
1750 now += (2208988800LL * GST_SECOND);
1752 GST_RTP_BIN_LOCK (bin);
1753 bin->priv->ntp_ns_base = now;
1754 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1755 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1757 g_object_set (session->session, "ntp-ns-base", now, NULL);
1759 GST_RTP_BIN_UNLOCK (bin);
1764 static GstStateChangeReturn
1765 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1767 GstStateChangeReturn res;
1769 GstRtpBinPrivate *priv;
1771 rtpbin = GST_RTP_BIN (element);
1772 priv = rtpbin->priv;
1774 switch (transition) {
1775 case GST_STATE_CHANGE_NULL_TO_READY:
1777 case GST_STATE_CHANGE_READY_TO_PAUSED:
1778 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
1779 g_atomic_int_set (&priv->shutdown, 0);
1781 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1782 calc_ntp_ns_base (rtpbin);
1784 case GST_STATE_CHANGE_PAUSED_TO_READY:
1785 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
1786 g_atomic_int_set (&priv->shutdown, 1);
1787 /* wait for all callbacks to end by taking the lock. No new callbacks will
1788 * be able to happen as we set the shutdown flag. */
1789 GST_RTP_BIN_DYN_LOCK (rtpbin);
1790 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
1791 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
1797 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1799 switch (transition) {
1800 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1802 case GST_STATE_CHANGE_PAUSED_TO_READY:
1804 case GST_STATE_CHANGE_READY_TO_NULL:
1812 /* a new pad (SSRC) was created in @session. This signal is emited from the
1813 * payload demuxer. */
1815 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1816 GstRtpBinStream * stream)
1819 GstElementClass *klass;
1820 GstPadTemplate *templ;
1824 rtpbin = stream->bin;
1826 GST_DEBUG ("new payload pad %d", pt);
1828 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
1830 /* ghost the pad to the parent */
1831 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1832 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1833 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1834 stream->session->id, stream->ssrc, pt);
1835 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1838 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1839 gst_pad_set_active (gpad, TRUE);
1840 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1842 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
1848 GST_DEBUG ("ignoring, we are shutting down");
1854 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1859 rtpbin = session->bin;
1861 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1864 caps = get_pt_map (session, pt);
1873 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1878 /* emited when caps changed for the session */
1880 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1885 const GstStructure *s;
1889 g_object_get (pad, "caps", &caps, NULL);
1894 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1896 s = gst_caps_get_structure (caps, 0);
1898 /* get payload, finish when it's not there */
1899 if (!gst_structure_get_int (s, "payload", &payload))
1902 GST_RTP_SESSION_LOCK (session);
1903 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1904 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1905 GST_RTP_SESSION_UNLOCK (session);
1908 /* a new pad (SSRC) was created in @session */
1910 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1911 GstRtpBinSession * session)
1914 GstRtpBinStream *stream;
1915 GstPad *sinkpad, *srcpad;
1918 rtpbin = session->bin;
1920 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
1921 GST_DEBUG_PAD_NAME (pad));
1923 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
1925 GST_RTP_SESSION_LOCK (session);
1927 /* create new stream */
1928 stream = create_stream (session, ssrc);
1932 /* get pad and link */
1933 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
1934 padname = g_strdup_printf ("src_%d", ssrc);
1935 srcpad = gst_element_get_static_pad (element, padname);
1937 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1938 gst_pad_link (srcpad, sinkpad);
1939 gst_object_unref (sinkpad);
1940 gst_object_unref (srcpad);
1942 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
1943 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1944 srcpad = gst_element_get_static_pad (element, padname);
1946 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
1947 gst_pad_link (srcpad, sinkpad);
1948 gst_object_unref (sinkpad);
1949 gst_object_unref (srcpad);
1951 /* connect to the RTCP sync signal from the jitterbuffer */
1952 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
1953 g_signal_connect (stream->buffer,
1954 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
1956 /* connect to the new-pad signal of the payload demuxer, this will expose the
1957 * new pad by ghosting it. */
1958 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1959 "new-payload-type", (GCallback) new_payload_found, stream);
1960 /* connect to the request-pt-map signal. This signal will be emited by the
1961 * demuxer so that it can apply a proper caps on the buffers for the
1963 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1964 "request-pt-map", (GCallback) pt_map_requested, session);
1966 GST_RTP_SESSION_UNLOCK (session);
1967 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
1974 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
1979 GST_RTP_SESSION_UNLOCK (session);
1980 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
1981 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
1986 /* Create a pad for receiving RTP for the session in @name. Must be called with
1990 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1992 GstPad *result, *sinkdpad;
1994 GstRtpBinSession *session;
1995 GstPadLinkReturn lres;
1997 /* first get the session number */
1998 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2001 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2003 /* get or create session */
2004 session = find_session_by_id (rtpbin, sessid);
2006 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2007 /* create session now */
2008 session = create_session (rtpbin, sessid);
2009 if (session == NULL)
2013 /* check if pad was requested */
2014 if (session->recv_rtp_sink != NULL)
2017 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2018 /* get recv_rtp pad and store */
2019 session->recv_rtp_sink =
2020 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2021 if (session->recv_rtp_sink == NULL)
2024 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2025 (GCallback) caps_changed, session);
2027 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2028 /* get srcpad, link to SSRCDemux */
2029 session->recv_rtp_src =
2030 gst_element_get_static_pad (session->session, "recv_rtp_src");
2031 if (session->recv_rtp_src == NULL)
2034 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2035 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2036 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2037 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2038 gst_object_unref (sinkdpad);
2039 if (lres != GST_PAD_LINK_OK)
2042 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2043 session->demux_newpad_sig = g_signal_connect (session->demux,
2044 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2046 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2048 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2049 gst_pad_set_active (result, TRUE);
2050 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2057 g_warning ("gstrtpbin: invalid name given");
2062 /* create_session already warned */
2067 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
2073 g_warning ("gstrtpbin: failed to get session pad");
2078 g_warning ("gstrtpbin: failed to link pads");
2084 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session, GstPad * pad)
2086 if (session->demux_newpad_sig) {
2087 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2088 session->demux_newpad_sig = 0;
2091 if (session->recv_rtp_src) {
2092 gst_object_unref (session->recv_rtp_src);
2093 session->recv_rtp_src = NULL;
2096 if (session->recv_rtp_sink) {
2097 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2098 session->recv_rtp_sink = NULL;
2101 gst_pad_set_active (pad, FALSE);
2102 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), pad);
2105 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2109 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2114 GstRtpBinSession *session;
2116 GstPadLinkReturn lres;
2118 /* first get the session number */
2119 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2122 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2124 /* get or create the session */
2125 session = find_session_by_id (rtpbin, sessid);
2127 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2128 /* create session now */
2129 session = create_session (rtpbin, sessid);
2130 if (session == NULL)
2134 /* check if pad was requested */
2135 if (session->recv_rtcp_sink != NULL)
2138 /* get recv_rtp pad and store */
2139 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2140 session->recv_rtcp_sink =
2141 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2142 if (session->recv_rtcp_sink == NULL)
2145 /* get srcpad, link to SSRCDemux */
2146 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2147 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2148 if (session->sync_src == NULL)
2151 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2152 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2153 lres = gst_pad_link (session->sync_src, sinkdpad);
2154 gst_object_unref (sinkdpad);
2155 if (lres != GST_PAD_LINK_OK)
2159 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2160 gst_pad_set_active (result, TRUE);
2161 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2168 g_warning ("gstrtpbin: invalid name given");
2173 /* create_session already warned */
2178 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
2184 g_warning ("gstrtpbin: failed to get session pad");
2189 g_warning ("gstrtpbin: failed to link pads");
2195 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session, GstPad * pad)
2197 gst_pad_set_active (pad, FALSE);
2198 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), pad);
2200 if (session->sync_src) {
2201 /* releasing the request pad should also unref the sync pad */
2202 gst_object_unref (session->sync_src);
2203 session->sync_src = NULL;
2205 if (session->recv_rtcp_sink) {
2206 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2207 session->recv_rtcp_sink = NULL;
2211 /* Create a pad for sending RTP for the session in @name. Must be called with
2215 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2220 GstRtpBinSession *session;
2221 GstElementClass *klass;
2223 /* first get the session number */
2224 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2227 /* get or create session */
2228 session = find_session_by_id (rtpbin, sessid);
2230 /* create session now */
2231 session = create_session (rtpbin, sessid);
2232 if (session == NULL)
2236 /* check if pad was requested */
2237 if (session->send_rtp_sink != NULL)
2240 /* get send_rtp pad and store */
2241 session->send_rtp_sink =
2242 gst_element_get_request_pad (session->session, "send_rtp_sink");
2243 if (session->send_rtp_sink == NULL)
2247 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2248 gst_pad_set_active (result, TRUE);
2249 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2252 session->send_rtp_src =
2253 gst_element_get_static_pad (session->session, "send_rtp_src");
2254 if (session->send_rtp_src == NULL)
2257 /* ghost the new source pad */
2258 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2259 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2260 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2261 session->send_rtp_src_ghost =
2262 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2263 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2264 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2272 g_warning ("gstrtpbin: invalid name given");
2277 /* create_session already warned */
2282 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
2288 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2293 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2300 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session, GstPad * pad)
2302 if (session->send_rtp_src_ghost) {
2303 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2304 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2305 session->send_rtp_src_ghost);
2306 session->send_rtp_src_ghost = NULL;
2309 if (session->send_rtp_src) {
2310 gst_object_unref (session->send_rtp_src);
2311 session->send_rtp_src = NULL;
2314 if (session->send_rtp_sink) {
2315 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2316 session->send_rtp_sink);
2317 session->send_rtp_sink = NULL;
2320 gst_pad_set_active (pad, FALSE);
2321 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), pad);
2324 /* Create a pad for sending RTCP for the session in @name. Must be called with
2328 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2332 GstRtpBinSession *session;
2334 /* first get the session number */
2335 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2338 /* get or create session */
2339 session = find_session_by_id (rtpbin, sessid);
2343 /* check if pad was requested */
2344 if (session->send_rtcp_src != NULL)
2347 /* get rtcp_src pad and store */
2348 session->send_rtcp_src =
2349 gst_element_get_request_pad (session->session, "send_rtcp_src");
2350 if (session->send_rtcp_src == NULL)
2354 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2355 gst_pad_set_active (result, TRUE);
2356 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
2363 g_warning ("gstrtpbin: invalid name given");
2368 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2373 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
2379 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2385 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session, GstPad * pad)
2387 gst_pad_set_active (pad, FALSE);
2388 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), pad);
2390 if (session->send_rtcp_src) {
2391 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2392 session->send_rtcp_src = NULL;
2396 /* If the requested name is NULL we should create a name with
2397 * the session number assuming we want the lowest posible session
2398 * with a free pad like the template */
2400 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2402 gboolean name_found = FALSE;
2405 GstIterator *pad_it = NULL;
2406 gchar *pad_name = NULL;
2408 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2409 while (!name_found) {
2411 pad_name = g_strdup_printf (templ->name_template, session++);
2412 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2414 while (gst_iterator_next (pad_it, (gpointer) & pad) == GST_ITERATOR_OK) {
2417 name = gst_pad_get_name (pad);
2418 if (strcmp (name, pad_name) == 0)
2422 gst_iterator_free (pad_it);
2425 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2432 gst_rtp_bin_request_new_pad (GstElement * element,
2433 GstPadTemplate * templ, const gchar * name)
2436 GstElementClass *klass;
2439 gchar *pad_name = NULL;
2441 g_return_val_if_fail (templ != NULL, NULL);
2442 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2444 rtpbin = GST_RTP_BIN (element);
2445 klass = GST_ELEMENT_GET_CLASS (element);
2447 GST_RTP_BIN_LOCK (rtpbin);
2450 /* use a free pad name */
2451 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2453 /* use the provided name */
2454 pad_name = g_strdup (name);
2457 GST_DEBUG ("Trying to request a pad with name %s", pad_name);
2459 /* figure out the template */
2460 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2461 result = create_recv_rtp (rtpbin, templ, pad_name);
2462 } else if (templ == gst_element_class_get_pad_template (klass,
2463 "recv_rtcp_sink_%d")) {
2464 result = create_recv_rtcp (rtpbin, templ, pad_name);
2465 } else if (templ == gst_element_class_get_pad_template (klass,
2466 "send_rtp_sink_%d")) {
2467 result = create_send_rtp (rtpbin, templ, pad_name);
2468 } else if (templ == gst_element_class_get_pad_template (klass,
2469 "send_rtcp_src_%d")) {
2470 result = create_rtcp (rtpbin, templ, pad_name);
2472 goto wrong_template;
2475 GST_RTP_BIN_UNLOCK (rtpbin);
2483 GST_RTP_BIN_UNLOCK (rtpbin);
2484 g_warning ("gstrtpbin: this is not our template");
2490 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2492 GstRtpBinSession *session;
2494 GstPad *target = NULL;
2496 g_return_if_fail (GST_IS_GHOST_PAD (pad));
2497 g_return_if_fail (GST_IS_RTP_BIN (element));
2499 rtpbin = GST_RTP_BIN (element);
2501 target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
2502 g_return_if_fail (target);
2504 GST_RTP_BIN_LOCK (rtpbin);
2505 if (!(session = find_session_by_pad (rtpbin, target)))
2508 if (session->recv_rtp_sink == target) {
2509 remove_recv_rtp (rtpbin, session, pad);
2510 } else if (session->recv_rtcp_sink == target) {
2511 remove_recv_rtcp (rtpbin, session, pad);
2512 } else if (session->send_rtp_sink == target) {
2513 remove_send_rtp (rtpbin, session, pad);
2514 } else if (session->send_rtcp_src == target) {
2515 remove_rtcp (rtpbin, session, pad);
2518 /* no more request pads, free the complete session */
2519 if (session->recv_rtp_sink == NULL && session->recv_rtcp_sink == NULL &&
2520 session->send_rtp_sink == NULL && session->send_rtcp_src == NULL) {
2521 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2522 free_session (session);
2524 GST_RTP_BIN_UNLOCK (rtpbin);
2526 gst_object_unref (target);
2533 GST_RTP_BIN_UNLOCK (rtpbin);
2534 gst_object_unref (target);
2535 g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2536 GST_DEBUG_PAD_NAME (pad));