2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 * @short_description: handle media from one RTP bin
23 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
27 * RTP bin combines the functions of gstrtpsession, gstrtpssrcdemux, gstrtpjitterbuffer
28 * and gstrtpptdemux in one element. It allows for multiple RTP sessions that will
29 * be synchronized together using RTCP SR packets.
32 * gstrtpbin is configured with a number of request pads that define the
33 * functionality that is activated, similar to the gstrtpsession element.
36 * To use gstrtpbin as an RTP receiver, request a recv_rtp_sink_%%d pad. The session
37 * number must be specified in the pad name.
38 * Data received on the recv_rtp_sink_%%d pad will be processed in the gstrtpsession
39 * manager and after being validated forwarded on gstrtpssrcdemuxer element. Each
40 * RTP stream is demuxed based on the SSRC and send to a gstrtpjitterbuffer. After
41 * the packets are released from the jitterbuffer, they will be forwarded to a
42 * gstrtpptdemuxer element. The gstrtpptdemuxer element will demux the packets based
43 * on the payload type and will create a unique pad recv_rtp_src_%%d_%%d_%%d on
44 * gstrtpbin with the session number, SSRC and payload type respectively as the pad
48 * To also use gstrtpbin as an RTCP receiver, request a recv_rtcp_sink_%%d pad. The
49 * session number must be specified in the pad name.
52 * If you want the session manager to generate and send RTCP packets, request
53 * the send_rtcp_src_%%d pad with the session number in the pad name. Packet pushed
54 * on this pad contain SR/RR RTCP reports that should be sent to all participants
58 * To use gstrtpbin as a sender, request a send_rtp_sink_%%d pad, which will
59 * automatically create a send_rtp_src_%%d pad. The session number must be specified when
60 * requesting the sink pad. The session manager will modify the
61 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
62 * send_rtp_src_%%d pad after updating its internal state.
65 * The session manager needs the clock-rate of the payload types it is handling
66 * and will signal the GstRtpSession::request-pt-map signal when it needs such a
67 * mapping. One can clear the cached values with the GstRtpSession::clear-pt-map
70 * <title>Example pipelines</title>
73 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
74 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
76 * Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
80 * gst-launch gstrtpbin name=rtpbin \
81 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
82 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
83 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
84 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
85 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
86 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
87 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
88 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
90 * Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
91 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
92 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
93 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
94 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
95 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
96 * is received on port 5007. Since RTCP packets from the sender should be sent
97 * as soon as possible and do not participate in preroll, sync=false and
98 * async=false is configured on udpsink
102 * gst-launch -v gstrtpbin name=rtpbin \
103 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
104 * port=5000 ! rtpbin.recv_rtp_sink_0 \
105 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
106 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
107 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
108 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
109 * port=5002 ! rtpbin.recv_rtp_sink_1 \
110 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
111 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
112 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
114 * Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
115 * decode and display the video.
116 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
117 * decode and play the audio.
118 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
119 * session 1 on port 5003. These packets will be used for session management and
121 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
126 * Last reviewed on 2007-08-30 (0.10.6)
134 #include <gst/rtp/gstrtpbuffer.h>
135 #include <gst/rtp/gstrtcpbuffer.h>
137 #include "gstrtpbin-marshal.h"
138 #include "gstrtpbin.h"
140 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
141 #define GST_CAT_DEFAULT gst_rtp_bin_debug
144 /* elementfactory information */
145 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
146 "Filter/Network/RTP",
147 "Implement an RTP bin",
148 "Wim Taymans <wim@fluendo.com>");
151 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
152 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
155 GST_STATIC_CAPS ("application/x-rtp")
158 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
159 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
162 GST_STATIC_CAPS ("application/x-rtcp")
165 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
166 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
169 GST_STATIC_CAPS ("application/x-rtp")
173 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
174 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
177 GST_STATIC_CAPS ("application/x-rtp")
180 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
181 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
184 GST_STATIC_CAPS ("application/x-rtcp")
187 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
188 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
191 GST_STATIC_CAPS ("application/x-rtp")
194 /* padtemplate for the internal pad */
195 static GstStaticPadTemplate rtpbin_sync_sink_template =
196 GST_STATIC_PAD_TEMPLATE ("sink_%d",
199 GST_STATIC_CAPS ("application/x-rtcp")
202 #define GST_RTP_BIN_GET_PRIVATE(obj) \
203 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
205 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
206 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
208 struct _GstRtpBinPrivate
212 GstClockTime ntp_ns_base;
215 /* signals and args */
218 SIGNAL_REQUEST_PT_MAP,
222 SIGNAL_ON_SSRC_COLLISION,
223 SIGNAL_ON_SSRC_VALIDATED,
224 SIGNAL_ON_SSRC_ACTIVE,
226 SIGNAL_ON_BYE_TIMEOUT,
231 #define DEFAULT_LATENCY_MS 200
240 typedef struct _GstRtpBinSession GstRtpBinSession;
241 typedef struct _GstRtpBinStream GstRtpBinStream;
242 typedef struct _GstRtpBinClient GstRtpBinClient;
244 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
246 static GstCaps *pt_map_requested (GstElement * element, guint pt,
247 GstRtpBinSession * session);
249 static void free_stream (GstRtpBinStream * stream);
251 /* Manages the RTP stream for one SSRC.
253 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
254 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
255 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
256 * together (see below).
258 struct _GstRtpBinStream
260 /* the SSRC of this stream */
266 /* the session this SSRC belongs to */
267 GstRtpBinSession *session;
269 /* the jitterbuffer of the SSRC */
272 /* the PT demuxer of the SSRC */
274 gulong demux_newpad_sig;
275 gulong demux_ptreq_sig;
277 /* the internal pad we use to get RTCP sync messages */
281 guint64 last_extrtptime;
283 /* mapping to local RTP and NTP time */
292 gint64 prev_ts_offset;
295 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
296 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
298 /* Manages the receiving end of the packets.
300 * There is one such structure for each RTP session (audio/video/...).
301 * We get the RTP/RTCP packets and stuff them into the session manager. From
302 * there they are pushed into an SSRC demuxer that splits the stream based on
303 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
304 * the GstRtpBinStream above).
306 struct _GstRtpBinSession
312 /* the session element */
314 /* the SSRC demuxer */
316 gulong demux_newpad_sig;
320 /* list of GstRtpBinStream */
323 /* mapping of payload type to caps */
326 /* the pads of the session */
327 GstPad *recv_rtp_sink;
328 GstPad *recv_rtp_src;
329 GstPad *recv_rtcp_sink;
331 GstPad *send_rtp_sink;
332 GstPad *send_rtp_src;
333 GstPad *send_rtcp_src;
336 /* Manages the RTP streams that come from one client and should therefore be
339 struct _GstRtpBinClient
341 /* the common CNAME for the streams */
352 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
353 static GstRtpBinSession *
354 find_session_by_id (GstRtpBin * rtpbin, gint id)
358 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
359 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
368 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
370 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
375 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
377 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
382 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
384 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
389 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
391 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
396 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
398 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
403 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
405 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
410 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
412 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
416 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
417 static GstRtpBinSession *
418 create_session (GstRtpBin * rtpbin, gint id)
420 GstRtpBinSession *sess;
421 GstElement *session, *demux;
423 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
426 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
429 sess = g_new0 (GstRtpBinSession, 1);
430 sess->lock = g_mutex_new ();
433 sess->session = session;
435 sess->ptmap = g_hash_table_new (NULL, NULL);
436 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
438 /* set NTP base or new session */
439 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
441 /* provide clock_rate to the session manager when needed */
442 g_signal_connect (session, "request-pt-map",
443 (GCallback) pt_map_requested, sess);
445 g_signal_connect (sess->session, "on-new-ssrc",
446 (GCallback) on_new_ssrc, sess);
447 g_signal_connect (sess->session, "on-ssrc-collision",
448 (GCallback) on_ssrc_collision, sess);
449 g_signal_connect (sess->session, "on-ssrc-validated",
450 (GCallback) on_ssrc_validated, sess);
451 g_signal_connect (sess->session, "on-ssrc-active",
452 (GCallback) on_ssrc_active, sess);
453 g_signal_connect (sess->session, "on-bye-ssrc",
454 (GCallback) on_bye_ssrc, sess);
455 g_signal_connect (sess->session, "on-bye-timeout",
456 (GCallback) on_bye_timeout, sess);
457 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
459 gst_bin_add (GST_BIN_CAST (rtpbin), session);
460 gst_element_set_state (session, GST_STATE_PLAYING);
461 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
462 gst_element_set_state (demux, GST_STATE_PLAYING);
469 g_warning ("gstrtpbin: could not create gstrtpsession element");
474 gst_object_unref (session);
475 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
481 free_session (GstRtpBinSession * sess)
487 gst_element_set_state (sess->session, GST_STATE_NULL);
488 gst_element_set_state (sess->demux, GST_STATE_NULL);
490 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
491 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
493 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
494 g_slist_free (sess->streams);
496 g_mutex_free (sess->lock);
497 g_hash_table_destroy (sess->ptmap);
499 bin->sessions = g_slist_remove (bin->sessions, sess);
505 static GstRtpBinStream *
506 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
510 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
511 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
513 if (stream->ssrc == ssrc)
520 /* get the payload type caps for the specific payload @pt in @session */
522 get_pt_map (GstRtpBinSession * session, guint pt)
524 GstCaps *caps = NULL;
527 GValue args[3] = { {0}, {0}, {0} };
529 GST_DEBUG ("searching pt %d in cache", pt);
531 GST_RTP_SESSION_LOCK (session);
533 /* first look in the cache */
534 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
540 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
542 /* not in cache, send signal to request caps */
543 g_value_init (&args[0], GST_TYPE_ELEMENT);
544 g_value_set_object (&args[0], bin);
545 g_value_init (&args[1], G_TYPE_UINT);
546 g_value_set_uint (&args[1], session->id);
547 g_value_init (&args[2], G_TYPE_UINT);
548 g_value_set_uint (&args[2], pt);
550 g_value_init (&ret, GST_TYPE_CAPS);
551 g_value_set_boxed (&ret, NULL);
553 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
555 caps = (GstCaps *) g_value_get_boxed (&ret);
559 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
562 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
565 GST_RTP_SESSION_UNLOCK (session);
572 GST_RTP_SESSION_UNLOCK (session);
573 GST_DEBUG ("no pt map could be obtained");
579 return_true (gpointer key, gpointer value, gpointer user_data)
585 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
589 GST_RTP_BIN_LOCK (bin);
590 GST_DEBUG_OBJECT (bin, "clearing pt map");
591 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
592 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
594 GST_RTP_SESSION_LOCK (session);
596 /* This requires GLib 2.12 */
597 g_hash_table_remove_all (session->ptmap);
599 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
601 GST_RTP_SESSION_UNLOCK (session);
603 GST_RTP_BIN_UNLOCK (bin);
606 static GstRtpBinClient *
607 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
609 GstRtpBinClient *result = NULL;
612 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
613 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
615 if (len != client->cname_len)
618 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
619 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
626 /* nothing found, create one */
627 if (result == NULL) {
628 result = g_new0 (GstRtpBinClient, 1);
629 result->cname = g_strndup ((gchar *) data, len);
630 result->cname_len = len;
631 result->min_delta = G_MAXINT64;
632 bin->clients = g_slist_prepend (bin->clients, result);
633 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
640 free_client (GstRtpBinClient * client)
642 g_free (client->cname);
646 /* associate a stream to the given CNAME. This will make sure all streams for
647 * that CNAME are synchronized together. */
649 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
652 GstRtpBinClient *client;
656 /* first find or create the CNAME */
657 client = get_client (bin, len, data, &created);
659 /* find stream in the client */
660 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
661 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
663 if (ostream == stream)
666 /* not found, add it to the list */
668 GST_DEBUG_OBJECT (bin,
669 "new association of SSRC %08x with client %p with CNAME %s",
670 stream->ssrc, client, client->cname);
671 client->streams = g_slist_prepend (client->streams, stream);
674 GST_DEBUG_OBJECT (bin,
675 "found association of SSRC %08x with client %p with CNAME %s",
676 stream->ssrc, client, client->cname);
679 /* we can only continue if we know the local clock-base and clock-rate */
680 if (stream->clock_base == -1)
682 if (stream->clock_rate <= 0)
685 /* map last RTP time to local timeline using our clock-base */
686 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
688 GST_DEBUG_OBJECT (bin,
689 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
690 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
691 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
693 /* calculate local NTP time in gstreamer timestamp */
695 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
697 /* calculate delta between server and receiver */
698 stream->unix_delta = stream->last_unix - stream->local_unix;
700 GST_DEBUG_OBJECT (bin,
701 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
702 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
705 /* recalc inter stream playout offset, but only if there are more than one
707 if (client->nstreams > 1) {
710 /* calculate the min of all deltas */
712 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
713 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
715 if (ostream->unix_delta < min)
716 min = ostream->unix_delta;
719 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
722 /* calculate offsets for each stream */
723 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
724 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
726 ostream->ts_offset = ostream->unix_delta - min;
728 /* delta changed, see how much */
729 if (ostream->prev_ts_offset != ostream->ts_offset) {
732 if (ostream->prev_ts_offset > ostream->ts_offset)
733 diff = ostream->prev_ts_offset - ostream->ts_offset;
735 diff = ostream->ts_offset - ostream->prev_ts_offset;
737 /* only change diff when it changed more than 1 millisecond. This
738 * compensates for rounding errors in NTP to RTP timestamp
740 if (diff > GST_MSECOND)
741 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
743 ostream->prev_ts_offset = ostream->ts_offset;
745 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
746 ostream->ssrc, ostream->ts_offset);
753 GST_WARNING_OBJECT (bin, "we have no clock-base");
758 GST_WARNING_OBJECT (bin, "we have no clock-rate");
763 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
764 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
765 (b) = gst_rtcp_packet_move_to_next ((packet)))
767 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
768 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
769 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
771 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
772 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
773 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
776 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
778 GstFlowReturn ret = GST_FLOW_OK;
779 GstRtpBinStream *stream;
781 GstRTCPPacket packet;
785 gboolean have_sr, have_sdes;
788 stream = gst_pad_get_element_private (pad);
791 GST_DEBUG_OBJECT (bin, "received sync packet");
793 if (!gst_rtcp_buffer_validate (buffer))
798 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
799 /* first packet must be SR or RR or else the validate would have failed */
800 switch (gst_rtcp_packet_get_type (&packet)) {
801 case GST_RTCP_TYPE_SR:
802 /* only parse first. There is only supposed to be one SR in the packet
803 * but we will deal with malformed packets gracefully */
806 /* get NTP and RTP times */
807 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
810 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
811 /* ignore SR that is not ours */
812 if (ssrc != stream->ssrc)
817 /* store values in the stream */
818 stream->have_sync = TRUE;
819 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
820 /* use extended timestamp */
821 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
823 case GST_RTCP_TYPE_SDES:
825 gboolean more_items, more_entries;
827 /* only deal with first SDES, there is only supposed to be one SDES in
828 * the RTCP packet but we deal with bad packets gracefully. Also bail
829 * out if we have not seen an SR item yet. */
830 if (have_sdes || !have_sr)
833 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
834 /* skip items that are not about the SSRC of the sender */
835 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
838 /* find the CNAME entry */
839 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
840 GstRTCPSDESType type;
844 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
846 if (type == GST_RTCP_SDES_CNAME) {
847 stream->clock_base = GST_BUFFER_OFFSET (buffer);
848 /* associate the stream to CNAME */
849 gst_rtp_bin_associate (bin, stream, len, data);
857 /* we can ignore these packets */
862 gst_buffer_unref (buffer);
869 /* this is fatal and should be filtered earlier */
870 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
871 ("invalid RTCP packet received"));
872 gst_buffer_unref (buffer);
873 return GST_FLOW_ERROR;
877 /* create a new stream with @ssrc in @session. Must be called with
878 * RTP_SESSION_LOCK. */
879 static GstRtpBinStream *
880 create_stream (GstRtpBinSession * session, guint32 ssrc)
882 GstElement *buffer, *demux;
883 GstRtpBinStream *stream;
884 GstPadTemplate *templ;
887 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
888 goto no_jitterbuffer;
890 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
893 stream = g_new0 (GstRtpBinStream, 1);
895 stream->bin = session->bin;
896 stream->session = session;
897 stream->buffer = buffer;
898 stream->demux = demux;
899 stream->last_extrtptime = -1;
900 stream->have_sync = FALSE;
901 session->streams = g_slist_prepend (session->streams, stream);
903 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
904 * pad. We will link this pad later. */
905 padname = g_strdup_printf ("sync_%d", ssrc);
906 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
907 stream->sync_pad = gst_pad_new_from_template (templ, padname);
908 gst_object_unref (templ);
909 gst_object_ref (stream->sync_pad);
910 gst_object_sink (stream->sync_pad);
911 gst_pad_set_element_private (stream->sync_pad, stream);
912 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
913 gst_pad_set_active (stream->sync_pad, TRUE);
915 /* provide clock_rate to the jitterbuffer when needed */
916 g_signal_connect (buffer, "request-pt-map",
917 (GCallback) pt_map_requested, session);
919 /* configure latency */
920 g_object_set (buffer, "latency", session->bin->latency, NULL);
922 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
923 gst_element_set_state (buffer, GST_STATE_PLAYING);
924 gst_bin_add (GST_BIN_CAST (session->bin), demux);
925 gst_element_set_state (demux, GST_STATE_PLAYING);
928 gst_element_link (buffer, demux);
935 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
940 gst_object_unref (buffer);
941 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
947 free_stream (GstRtpBinStream * stream)
949 GstRtpBinSession *session;
951 session = stream->session;
953 gst_element_set_state (stream->buffer, GST_STATE_NULL);
954 gst_element_set_state (stream->demux, GST_STATE_NULL);
956 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
957 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
959 gst_object_unref (stream->sync_pad);
961 session->streams = g_slist_remove (session->streams, stream);
966 /* GObject vmethods */
967 static void gst_rtp_bin_dispose (GObject * object);
968 static void gst_rtp_bin_finalize (GObject * object);
969 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
970 const GValue * value, GParamSpec * pspec);
971 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
972 GValue * value, GParamSpec * pspec);
974 /* GstElement vmethods */
975 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
976 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
977 GstStateChange transition);
978 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
979 GstPadTemplate * templ, const gchar * name);
980 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
981 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
983 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
986 gst_rtp_bin_base_init (gpointer klass)
988 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
991 gst_element_class_add_pad_template (element_class,
992 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
993 gst_element_class_add_pad_template (element_class,
994 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
995 gst_element_class_add_pad_template (element_class,
996 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
999 gst_element_class_add_pad_template (element_class,
1000 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1001 gst_element_class_add_pad_template (element_class,
1002 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1003 gst_element_class_add_pad_template (element_class,
1004 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1006 gst_element_class_set_details (element_class, &rtpbin_details);
1010 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1012 GObjectClass *gobject_class;
1013 GstElementClass *gstelement_class;
1015 gobject_class = (GObjectClass *) klass;
1016 gstelement_class = (GstElementClass *) klass;
1018 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1020 gobject_class->dispose = gst_rtp_bin_dispose;
1021 gobject_class->finalize = gst_rtp_bin_finalize;
1022 gobject_class->set_property = gst_rtp_bin_set_property;
1023 gobject_class->get_property = gst_rtp_bin_get_property;
1025 g_object_class_install_property (gobject_class, PROP_LATENCY,
1026 g_param_spec_uint ("latency", "Buffer latency in ms",
1027 "Default amount of ms to buffer in the jitterbuffers", 0,
1028 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1031 * GstRtpBin::request-pt-map:
1032 * @rtpbin: the object which received the signal
1033 * @session: the session
1036 * Request the payload type as #GstCaps for @pt in @session.
1038 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1039 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1040 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1041 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1042 G_TYPE_UINT, G_TYPE_UINT);
1044 * GstRtpBin::clear-pt-map:
1045 * @rtpbin: the object which received the signal
1047 * Clear all previously cached pt-mapping obtained with
1048 * GstRtpBin::request-pt-map.
1050 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1051 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1052 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass, clear_pt_map),
1053 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
1056 * GstRtpBin::on-new-ssrc:
1057 * @rtpbin: the object which received the signal
1058 * @session: the session
1061 * Notify of a new SSRC that entered @session.
1063 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1064 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1065 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1066 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1067 G_TYPE_UINT, G_TYPE_UINT);
1069 * GstRtpBin::on-ssrc_collision:
1070 * @rtpbin: the object which received the signal
1071 * @session: the session
1074 * Notify when we have an SSRC collision
1076 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1077 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1078 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1079 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1080 G_TYPE_UINT, G_TYPE_UINT);
1082 * GstRtpBin::on-ssrc_validated:
1083 * @rtpbin: the object which received the signal
1084 * @session: the session
1087 * Notify of a new SSRC that became validated.
1089 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1090 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1091 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1092 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1093 G_TYPE_UINT, G_TYPE_UINT);
1095 * GstRtpBin::on-ssrc_active:
1096 * @rtpbin: the object which received the signal
1097 * @session: the session
1100 * Notify of a SSRC that is active, i.e., sending RTCP.
1102 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1103 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1104 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1105 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1106 G_TYPE_UINT, G_TYPE_UINT);
1109 * GstRtpBin::on-bye-ssrc:
1110 * @rtpbin: the object which received the signal
1111 * @session: the session
1114 * Notify of an SSRC that became inactive because of a BYE packet.
1116 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1117 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1118 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1119 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1120 G_TYPE_UINT, G_TYPE_UINT);
1122 * GstRtpBin::on-bye-timeout:
1123 * @rtpbin: the object which received the signal
1124 * @session: the session
1127 * Notify of an SSRC that has timed out because of BYE
1129 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1130 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1131 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1132 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1133 G_TYPE_UINT, G_TYPE_UINT);
1135 * GstRtpBin::on-timeout:
1136 * @rtpbin: the object which received the signal
1137 * @session: the session
1140 * Notify of an SSRC that has timed out
1142 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1143 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1145 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1146 G_TYPE_UINT, G_TYPE_UINT);
1148 gstelement_class->provide_clock =
1149 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1150 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1151 gstelement_class->request_new_pad =
1152 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1153 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1155 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1157 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1161 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1163 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1164 rtpbin->priv->bin_lock = g_mutex_new ();
1165 rtpbin->provided_clock = gst_system_clock_obtain ();
1166 rtpbin->latency = DEFAULT_LATENCY_MS;
1170 gst_rtp_bin_dispose (GObject * object)
1174 rtpbin = GST_RTP_BIN (object);
1176 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1177 g_slist_free (rtpbin->sessions);
1178 rtpbin->sessions = NULL;
1179 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1180 g_slist_free (rtpbin->clients);
1181 rtpbin->clients = NULL;
1183 G_OBJECT_CLASS (parent_class)->dispose (object);
1187 gst_rtp_bin_finalize (GObject * object)
1191 rtpbin = GST_RTP_BIN (object);
1193 g_mutex_free (rtpbin->priv->bin_lock);
1194 gst_object_unref (rtpbin->provided_clock);
1196 G_OBJECT_CLASS (parent_class)->finalize (object);
1200 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1201 const GValue * value, GParamSpec * pspec)
1205 rtpbin = GST_RTP_BIN (object);
1209 GST_RTP_BIN_LOCK (rtpbin);
1210 rtpbin->latency = g_value_get_uint (value);
1211 GST_RTP_BIN_UNLOCK (rtpbin);
1214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1220 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1221 GValue * value, GParamSpec * pspec)
1225 rtpbin = GST_RTP_BIN (object);
1229 GST_RTP_BIN_LOCK (rtpbin);
1230 g_value_set_uint (value, rtpbin->latency);
1231 GST_RTP_BIN_UNLOCK (rtpbin);
1234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1240 gst_rtp_bin_provide_clock (GstElement * element)
1244 rtpbin = GST_RTP_BIN (element);
1246 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1250 calc_ntp_ns_base (GstRtpBin * bin)
1256 /* get the current time and convert it to NTP time in nanoseconds */
1257 g_get_current_time (¤t);
1258 now = GST_TIMEVAL_TO_TIME (current);
1259 now += (2208988800LL * GST_SECOND);
1261 GST_RTP_BIN_LOCK (bin);
1262 bin->priv->ntp_ns_base = now;
1263 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1264 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1266 g_object_set (session->session, "ntp-ns-base", now, NULL);
1268 GST_RTP_BIN_UNLOCK (bin);
1273 static GstStateChangeReturn
1274 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1276 GstStateChangeReturn res;
1279 rtpbin = GST_RTP_BIN (element);
1281 switch (transition) {
1282 case GST_STATE_CHANGE_NULL_TO_READY:
1284 case GST_STATE_CHANGE_READY_TO_PAUSED:
1286 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1287 calc_ntp_ns_base (rtpbin);
1293 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1295 switch (transition) {
1296 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1298 case GST_STATE_CHANGE_PAUSED_TO_READY:
1300 case GST_STATE_CHANGE_READY_TO_NULL:
1308 /* a new pad (SSRC) was created in @session */
1310 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1311 GstRtpBinStream * stream)
1314 GstElementClass *klass;
1315 GstPadTemplate *templ;
1319 rtpbin = stream->bin;
1321 GST_DEBUG ("new payload pad %d", pt);
1323 /* ghost the pad to the parent */
1324 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1325 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1326 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1327 stream->session->id, stream->ssrc, pt);
1328 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1331 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1332 gst_pad_set_active (gpad, TRUE);
1333 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1337 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1342 rtpbin = session->bin;
1344 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1347 caps = get_pt_map (session, pt);
1356 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1361 /* emited when caps changed for the session */
1363 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1368 const GstStructure *s;
1372 g_object_get (pad, "caps", &caps, NULL);
1377 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1379 s = gst_caps_get_structure (caps, 0);
1381 /* get payload, finish when it's not there */
1382 if (!gst_structure_get_int (s, "payload", &payload))
1385 GST_RTP_SESSION_LOCK (session);
1386 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1387 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1388 GST_RTP_SESSION_UNLOCK (session);
1391 /* a new pad (SSRC) was created in @session */
1393 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1394 GstRtpBinSession * session)
1396 GstRtpBinStream *stream;
1397 GstPad *sinkpad, *srcpad;
1401 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1403 GST_RTP_SESSION_LOCK (session);
1405 /* create new stream */
1406 stream = create_stream (session, ssrc);
1410 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1411 if ((caps = gst_pad_get_caps (pad))) {
1412 const GstStructure *s;
1415 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1417 s = gst_caps_get_structure (caps, 0);
1419 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1420 stream->clock_rate = -1;
1422 if (gst_structure_get_uint (s, "clock-base", &val))
1423 stream->clock_base = val;
1425 stream->clock_base = -1;
1428 /* get pad and link */
1429 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1430 padname = g_strdup_printf ("src_%d", ssrc);
1431 srcpad = gst_element_get_pad (element, padname);
1433 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1434 gst_pad_link (srcpad, sinkpad);
1435 gst_object_unref (sinkpad);
1437 /* get the RTCP sync pad */
1438 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1439 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1440 srcpad = gst_element_get_pad (element, padname);
1442 gst_pad_link (srcpad, stream->sync_pad);
1443 gst_object_unref (srcpad);
1445 /* connect to the new-pad signal of the payload demuxer, this will expose the
1446 * new pad by ghosting it. */
1447 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1448 "new-payload-type", (GCallback) new_payload_found, stream);
1449 /* connect to the request-pt-map signal. This signal will be emited by the
1450 * demuxer so that it can apply a proper caps on the buffers for the
1452 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1453 "request-pt-map", (GCallback) pt_map_requested, session);
1455 GST_RTP_SESSION_UNLOCK (session);
1462 GST_RTP_SESSION_UNLOCK (session);
1463 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1468 /* Create a pad for receiving RTP for the session in @name. Must be called with
1472 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1474 GstPad *result, *sinkdpad;
1476 GstRtpBinSession *session;
1477 GstPadLinkReturn lres;
1479 /* first get the session number */
1480 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1483 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1485 /* get or create session */
1486 session = find_session_by_id (rtpbin, sessid);
1488 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1489 /* create session now */
1490 session = create_session (rtpbin, sessid);
1491 if (session == NULL)
1495 /* check if pad was requested */
1496 if (session->recv_rtp_sink != NULL)
1499 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1500 /* get recv_rtp pad and store */
1501 session->recv_rtp_sink =
1502 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1503 if (session->recv_rtp_sink == NULL)
1506 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1507 (GCallback) caps_changed, session);
1509 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1510 /* get srcpad, link to SSRCDemux */
1511 session->recv_rtp_src =
1512 gst_element_get_static_pad (session->session, "recv_rtp_src");
1513 if (session->recv_rtp_src == NULL)
1516 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1517 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1518 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1519 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1520 gst_object_unref (sinkdpad);
1521 if (lres != GST_PAD_LINK_OK)
1524 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1525 session->demux_newpad_sig = g_signal_connect (session->demux,
1526 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1528 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1530 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1531 gst_pad_set_active (result, TRUE);
1532 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1539 g_warning ("gstrtpbin: invalid name given");
1544 /* create_session already warned */
1549 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1555 g_warning ("gstrtpbin: failed to get session pad");
1560 g_warning ("gstrtpbin: failed to link pads");
1565 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1569 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1574 GstRtpBinSession *session;
1576 GstPadLinkReturn lres;
1578 /* first get the session number */
1579 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1582 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1584 /* get or create the session */
1585 session = find_session_by_id (rtpbin, sessid);
1587 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1588 /* create session now */
1589 session = create_session (rtpbin, sessid);
1590 if (session == NULL)
1594 /* check if pad was requested */
1595 if (session->recv_rtcp_sink != NULL)
1598 /* get recv_rtp pad and store */
1599 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1600 session->recv_rtcp_sink =
1601 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1602 if (session->recv_rtcp_sink == NULL)
1605 /* get srcpad, link to SSRCDemux */
1606 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1607 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1608 if (session->sync_src == NULL)
1611 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1612 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1613 lres = gst_pad_link (session->sync_src, sinkdpad);
1614 gst_object_unref (sinkdpad);
1615 if (lres != GST_PAD_LINK_OK)
1619 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1620 gst_pad_set_active (result, TRUE);
1621 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1628 g_warning ("gstrtpbin: invalid name given");
1633 /* create_session already warned */
1638 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1644 g_warning ("gstrtpbin: failed to get session pad");
1649 g_warning ("gstrtpbin: failed to link pads");
1654 /* Create a pad for sending RTP for the session in @name. Must be called with
1658 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1660 GstPad *result, *srcghost;
1663 GstRtpBinSession *session;
1664 GstElementClass *klass;
1666 /* first get the session number */
1667 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1670 /* get or create session */
1671 session = find_session_by_id (rtpbin, sessid);
1673 /* create session now */
1674 session = create_session (rtpbin, sessid);
1675 if (session == NULL)
1679 /* check if pad was requested */
1680 if (session->send_rtp_sink != NULL)
1683 /* get send_rtp pad and store */
1684 session->send_rtp_sink =
1685 gst_element_get_request_pad (session->session, "send_rtp_sink");
1686 if (session->send_rtp_sink == NULL)
1690 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1691 gst_pad_set_active (result, TRUE);
1692 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1695 session->send_rtp_src =
1696 gst_element_get_static_pad (session->session, "send_rtp_src");
1697 if (session->send_rtp_src == NULL)
1700 /* ghost the new source pad */
1701 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1702 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1703 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1705 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1706 gst_pad_set_active (srcghost, TRUE);
1707 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1715 g_warning ("gstrtpbin: invalid name given");
1720 /* create_session already warned */
1725 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1731 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1736 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1742 /* Create a pad for sending RTCP for the session in @name. Must be called with
1746 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1750 GstRtpBinSession *session;
1752 /* first get the session number */
1753 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1756 /* get or create session */
1757 session = find_session_by_id (rtpbin, sessid);
1761 /* check if pad was requested */
1762 if (session->send_rtcp_src != NULL)
1765 /* get rtcp_src pad and store */
1766 session->send_rtcp_src =
1767 gst_element_get_request_pad (session->session, "send_rtcp_src");
1768 if (session->send_rtcp_src == NULL)
1772 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1773 gst_pad_set_active (result, TRUE);
1774 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1781 g_warning ("gstrtpbin: invalid name given");
1786 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1791 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1797 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1805 gst_rtp_bin_request_new_pad (GstElement * element,
1806 GstPadTemplate * templ, const gchar * name)
1809 GstElementClass *klass;
1812 g_return_val_if_fail (templ != NULL, NULL);
1813 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1815 rtpbin = GST_RTP_BIN (element);
1816 klass = GST_ELEMENT_GET_CLASS (element);
1818 GST_RTP_BIN_LOCK (rtpbin);
1820 /* figure out the template */
1821 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1822 result = create_recv_rtp (rtpbin, templ, name);
1823 } else if (templ == gst_element_class_get_pad_template (klass,
1824 "recv_rtcp_sink_%d")) {
1825 result = create_recv_rtcp (rtpbin, templ, name);
1826 } else if (templ == gst_element_class_get_pad_template (klass,
1827 "send_rtp_sink_%d")) {
1828 result = create_send_rtp (rtpbin, templ, name);
1829 } else if (templ == gst_element_class_get_pad_template (klass,
1830 "send_rtcp_src_%d")) {
1831 result = create_rtcp (rtpbin, templ, name);
1833 goto wrong_template;
1835 GST_RTP_BIN_UNLOCK (rtpbin);
1842 GST_RTP_BIN_UNLOCK (rtpbin);
1843 g_warning ("gstrtpbin: this is not our template");
1849 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)