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, GstRtpBin * bin)
642 bin->clients = g_slist_remove (bin->clients, client);
643 g_free (client->cname);
647 /* associate a stream to the given CNAME. This will make sure all streams for
648 * that CNAME are synchronized together. */
650 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
653 GstRtpBinClient *client;
657 /* first find or create the CNAME */
658 client = get_client (bin, len, data, &created);
660 /* find stream in the client */
661 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
662 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
664 if (ostream == stream)
667 /* not found, add it to the list */
669 GST_DEBUG_OBJECT (bin,
670 "new association of SSRC %08x with client %p with CNAME %s",
671 stream->ssrc, client, client->cname);
672 client->streams = g_slist_prepend (client->streams, stream);
675 GST_DEBUG_OBJECT (bin,
676 "found association of SSRC %08x with client %p with CNAME %s",
677 stream->ssrc, client, client->cname);
680 /* we can only continue if we know the local clock-base and clock-rate */
681 if (stream->clock_base == -1)
683 if (stream->clock_rate <= 0)
686 /* map last RTP time to local timeline using our clock-base */
687 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
689 GST_DEBUG_OBJECT (bin,
690 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
691 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
692 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
694 /* calculate local NTP time in gstreamer timestamp */
696 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
698 /* calculate delta between server and receiver */
699 stream->unix_delta = stream->last_unix - stream->local_unix;
701 GST_DEBUG_OBJECT (bin,
702 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
703 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
706 /* recalc inter stream playout offset, but only if there are more than one
708 if (client->nstreams > 1) {
711 /* calculate the min of all deltas */
713 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
714 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
716 if (ostream->unix_delta < min)
717 min = ostream->unix_delta;
720 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
723 /* calculate offsets for each stream */
724 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
725 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
727 ostream->ts_offset = ostream->unix_delta - min;
729 /* delta changed, see how much */
730 if (ostream->prev_ts_offset != ostream->ts_offset) {
733 if (ostream->prev_ts_offset > ostream->ts_offset)
734 diff = ostream->prev_ts_offset - ostream->ts_offset;
736 diff = ostream->ts_offset - ostream->prev_ts_offset;
738 /* only change diff when it changed more than 1 millisecond. This
739 * compensates for rounding errors in NTP to RTP timestamp
741 if (diff > GST_MSECOND)
742 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
744 ostream->prev_ts_offset = ostream->ts_offset;
746 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
747 ostream->ssrc, ostream->ts_offset);
754 GST_WARNING_OBJECT (bin, "we have no clock-base");
759 GST_WARNING_OBJECT (bin, "we have no clock-rate");
764 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
765 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
766 (b) = gst_rtcp_packet_move_to_next ((packet)))
768 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
769 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
770 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
772 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
773 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
774 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
777 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
779 GstFlowReturn ret = GST_FLOW_OK;
780 GstRtpBinStream *stream;
782 GstRTCPPacket packet;
786 gboolean have_sr, have_sdes;
789 stream = gst_pad_get_element_private (pad);
792 GST_DEBUG_OBJECT (bin, "received sync packet");
794 if (!gst_rtcp_buffer_validate (buffer))
799 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
800 /* first packet must be SR or RR or else the validate would have failed */
801 switch (gst_rtcp_packet_get_type (&packet)) {
802 case GST_RTCP_TYPE_SR:
803 /* only parse first. There is only supposed to be one SR in the packet
804 * but we will deal with malformed packets gracefully */
807 /* get NTP and RTP times */
808 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
811 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
812 /* ignore SR that is not ours */
813 if (ssrc != stream->ssrc)
818 /* store values in the stream */
819 stream->have_sync = TRUE;
820 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
821 /* use extended timestamp */
822 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
824 case GST_RTCP_TYPE_SDES:
826 gboolean more_items, more_entries;
828 /* only deal with first SDES, there is only supposed to be one SDES in
829 * the RTCP packet but we deal with bad packets gracefully. Also bail
830 * out if we have not seen an SR item yet. */
831 if (have_sdes || !have_sr)
834 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
835 /* skip items that are not about the SSRC of the sender */
836 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
839 /* find the CNAME entry */
840 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
841 GstRTCPSDESType type;
845 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
847 if (type == GST_RTCP_SDES_CNAME) {
848 stream->clock_base = GST_BUFFER_OFFSET (buffer);
849 /* associate the stream to CNAME */
850 gst_rtp_bin_associate (bin, stream, len, data);
858 /* we can ignore these packets */
863 gst_buffer_unref (buffer);
870 /* this is fatal and should be filtered earlier */
871 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
872 ("invalid RTCP packet received"));
873 gst_buffer_unref (buffer);
874 return GST_FLOW_ERROR;
878 /* create a new stream with @ssrc in @session. Must be called with
879 * RTP_SESSION_LOCK. */
880 static GstRtpBinStream *
881 create_stream (GstRtpBinSession * session, guint32 ssrc)
883 GstElement *buffer, *demux;
884 GstRtpBinStream *stream;
885 GstPadTemplate *templ;
888 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
889 goto no_jitterbuffer;
891 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
894 stream = g_new0 (GstRtpBinStream, 1);
896 stream->bin = session->bin;
897 stream->session = session;
898 stream->buffer = buffer;
899 stream->demux = demux;
900 stream->last_extrtptime = -1;
901 stream->have_sync = FALSE;
902 session->streams = g_slist_prepend (session->streams, stream);
904 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
905 * pad. We will link this pad later. */
906 padname = g_strdup_printf ("sync_%d", ssrc);
907 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
908 stream->sync_pad = gst_pad_new_from_template (templ, padname);
909 gst_object_unref (templ);
910 gst_object_ref (stream->sync_pad);
911 gst_object_sink (stream->sync_pad);
912 gst_pad_set_element_private (stream->sync_pad, stream);
913 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
914 gst_pad_set_active (stream->sync_pad, TRUE);
916 /* provide clock_rate to the jitterbuffer when needed */
917 g_signal_connect (buffer, "request-pt-map",
918 (GCallback) pt_map_requested, session);
920 /* configure latency */
921 g_object_set (buffer, "latency", session->bin->latency, NULL);
923 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
924 gst_element_set_state (buffer, GST_STATE_PLAYING);
925 gst_bin_add (GST_BIN_CAST (session->bin), demux);
926 gst_element_set_state (demux, GST_STATE_PLAYING);
929 gst_element_link (buffer, demux);
936 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
941 gst_object_unref (buffer);
942 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
948 free_stream (GstRtpBinStream * stream)
950 GstRtpBinSession *session;
952 session = stream->session;
954 gst_element_set_state (stream->buffer, GST_STATE_NULL);
955 gst_element_set_state (stream->demux, GST_STATE_NULL);
957 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
958 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
960 gst_object_unref (stream->sync_pad);
962 session->streams = g_slist_remove (session->streams, stream);
967 /* GObject vmethods */
968 static void gst_rtp_bin_dispose (GObject * object);
969 static void gst_rtp_bin_finalize (GObject * object);
970 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
971 const GValue * value, GParamSpec * pspec);
972 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
973 GValue * value, GParamSpec * pspec);
975 /* GstElement vmethods */
976 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
977 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
978 GstStateChange transition);
979 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
980 GstPadTemplate * templ, const gchar * name);
981 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
982 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
984 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
987 gst_rtp_bin_base_init (gpointer klass)
989 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
992 gst_element_class_add_pad_template (element_class,
993 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
994 gst_element_class_add_pad_template (element_class,
995 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
996 gst_element_class_add_pad_template (element_class,
997 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1000 gst_element_class_add_pad_template (element_class,
1001 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1002 gst_element_class_add_pad_template (element_class,
1003 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1004 gst_element_class_add_pad_template (element_class,
1005 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1007 gst_element_class_set_details (element_class, &rtpbin_details);
1011 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1013 GObjectClass *gobject_class;
1014 GstElementClass *gstelement_class;
1016 gobject_class = (GObjectClass *) klass;
1017 gstelement_class = (GstElementClass *) klass;
1019 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1021 gobject_class->dispose = gst_rtp_bin_dispose;
1022 gobject_class->finalize = gst_rtp_bin_finalize;
1023 gobject_class->set_property = gst_rtp_bin_set_property;
1024 gobject_class->get_property = gst_rtp_bin_get_property;
1026 g_object_class_install_property (gobject_class, PROP_LATENCY,
1027 g_param_spec_uint ("latency", "Buffer latency in ms",
1028 "Default amount of ms to buffer in the jitterbuffers", 0,
1029 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1032 * GstRtpBin::request-pt-map:
1033 * @rtpbin: the object which received the signal
1034 * @session: the session
1037 * Request the payload type as #GstCaps for @pt in @session.
1039 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1040 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1041 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1042 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1043 G_TYPE_UINT, G_TYPE_UINT);
1045 * GstRtpBin::clear-pt-map:
1046 * @rtpbin: the object which received the signal
1048 * Clear all previously cached pt-mapping obtained with
1049 * GstRtpBin::request-pt-map.
1051 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1052 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1053 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass, clear_pt_map),
1054 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
1057 * GstRtpBin::on-new-ssrc:
1058 * @rtpbin: the object which received the signal
1059 * @session: the session
1062 * Notify of a new SSRC that entered @session.
1064 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1065 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1066 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1067 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1068 G_TYPE_UINT, G_TYPE_UINT);
1070 * GstRtpBin::on-ssrc_collision:
1071 * @rtpbin: the object which received the signal
1072 * @session: the session
1075 * Notify when we have an SSRC collision
1077 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1078 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1079 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1080 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1081 G_TYPE_UINT, G_TYPE_UINT);
1083 * GstRtpBin::on-ssrc_validated:
1084 * @rtpbin: the object which received the signal
1085 * @session: the session
1088 * Notify of a new SSRC that became validated.
1090 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1091 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1092 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1093 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1094 G_TYPE_UINT, G_TYPE_UINT);
1096 * GstRtpBin::on-ssrc_active:
1097 * @rtpbin: the object which received the signal
1098 * @session: the session
1101 * Notify of a SSRC that is active, i.e., sending RTCP.
1103 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1104 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1105 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1106 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1107 G_TYPE_UINT, G_TYPE_UINT);
1110 * GstRtpBin::on-bye-ssrc:
1111 * @rtpbin: the object which received the signal
1112 * @session: the session
1115 * Notify of an SSRC that became inactive because of a BYE packet.
1117 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1118 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1119 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1120 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1121 G_TYPE_UINT, G_TYPE_UINT);
1123 * GstRtpBin::on-bye-timeout:
1124 * @rtpbin: the object which received the signal
1125 * @session: the session
1128 * Notify of an SSRC that has timed out because of BYE
1130 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1131 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1132 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1133 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1134 G_TYPE_UINT, G_TYPE_UINT);
1136 * GstRtpBin::on-timeout:
1137 * @rtpbin: the object which received the signal
1138 * @session: the session
1141 * Notify of an SSRC that has timed out
1143 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1144 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1145 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1146 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1147 G_TYPE_UINT, G_TYPE_UINT);
1149 gstelement_class->provide_clock =
1150 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1151 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1152 gstelement_class->request_new_pad =
1153 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1154 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1156 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1158 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1162 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1164 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1165 rtpbin->priv->bin_lock = g_mutex_new ();
1166 rtpbin->provided_clock = gst_system_clock_obtain ();
1167 rtpbin->latency = DEFAULT_LATENCY_MS;
1171 gst_rtp_bin_dispose (GObject * object)
1175 rtpbin = GST_RTP_BIN (object);
1177 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1178 g_slist_free (rtpbin->sessions);
1179 rtpbin->sessions = NULL;
1180 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1181 g_slist_free (rtpbin->clients);
1182 rtpbin->clients = NULL;
1184 G_OBJECT_CLASS (parent_class)->dispose (object);
1188 gst_rtp_bin_finalize (GObject * object)
1192 rtpbin = GST_RTP_BIN (object);
1194 g_mutex_free (rtpbin->priv->bin_lock);
1195 gst_object_unref (rtpbin->provided_clock);
1197 G_OBJECT_CLASS (parent_class)->finalize (object);
1201 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1202 const GValue * value, GParamSpec * pspec)
1206 rtpbin = GST_RTP_BIN (object);
1210 GST_RTP_BIN_LOCK (rtpbin);
1211 rtpbin->latency = g_value_get_uint (value);
1212 GST_RTP_BIN_UNLOCK (rtpbin);
1215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1221 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1222 GValue * value, GParamSpec * pspec)
1226 rtpbin = GST_RTP_BIN (object);
1230 GST_RTP_BIN_LOCK (rtpbin);
1231 g_value_set_uint (value, rtpbin->latency);
1232 GST_RTP_BIN_UNLOCK (rtpbin);
1235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1241 gst_rtp_bin_provide_clock (GstElement * element)
1245 rtpbin = GST_RTP_BIN (element);
1247 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1251 calc_ntp_ns_base (GstRtpBin * bin)
1257 /* get the current time and convert it to NTP time in nanoseconds */
1258 g_get_current_time (¤t);
1259 now = GST_TIMEVAL_TO_TIME (current);
1260 now += (2208988800LL * GST_SECOND);
1262 GST_RTP_BIN_LOCK (bin);
1263 bin->priv->ntp_ns_base = now;
1264 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1265 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1267 g_object_set (session->session, "ntp-ns-base", now, NULL);
1269 GST_RTP_BIN_UNLOCK (bin);
1274 static GstStateChangeReturn
1275 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1277 GstStateChangeReturn res;
1280 rtpbin = GST_RTP_BIN (element);
1282 switch (transition) {
1283 case GST_STATE_CHANGE_NULL_TO_READY:
1285 case GST_STATE_CHANGE_READY_TO_PAUSED:
1287 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1288 calc_ntp_ns_base (rtpbin);
1294 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1296 switch (transition) {
1297 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1299 case GST_STATE_CHANGE_PAUSED_TO_READY:
1301 case GST_STATE_CHANGE_READY_TO_NULL:
1309 /* a new pad (SSRC) was created in @session */
1311 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1312 GstRtpBinStream * stream)
1315 GstElementClass *klass;
1316 GstPadTemplate *templ;
1320 rtpbin = stream->bin;
1322 GST_DEBUG ("new payload pad %d", pt);
1324 /* ghost the pad to the parent */
1325 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1326 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1327 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1328 stream->session->id, stream->ssrc, pt);
1329 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1332 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1333 gst_pad_set_active (gpad, TRUE);
1334 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1338 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1343 rtpbin = session->bin;
1345 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1348 caps = get_pt_map (session, pt);
1357 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1362 /* emited when caps changed for the session */
1364 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1369 const GstStructure *s;
1373 g_object_get (pad, "caps", &caps, NULL);
1378 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1380 s = gst_caps_get_structure (caps, 0);
1382 /* get payload, finish when it's not there */
1383 if (!gst_structure_get_int (s, "payload", &payload))
1386 GST_RTP_SESSION_LOCK (session);
1387 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1388 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1389 GST_RTP_SESSION_UNLOCK (session);
1392 /* a new pad (SSRC) was created in @session */
1394 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1395 GstRtpBinSession * session)
1397 GstRtpBinStream *stream;
1398 GstPad *sinkpad, *srcpad;
1402 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1404 GST_RTP_SESSION_LOCK (session);
1406 /* create new stream */
1407 stream = create_stream (session, ssrc);
1411 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1412 if ((caps = gst_pad_get_caps (pad))) {
1413 const GstStructure *s;
1416 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1418 s = gst_caps_get_structure (caps, 0);
1420 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1421 stream->clock_rate = -1;
1423 if (gst_structure_get_uint (s, "clock-base", &val))
1424 stream->clock_base = val;
1426 stream->clock_base = -1;
1429 /* get pad and link */
1430 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1431 padname = g_strdup_printf ("src_%d", ssrc);
1432 srcpad = gst_element_get_pad (element, padname);
1434 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1435 gst_pad_link (srcpad, sinkpad);
1436 gst_object_unref (sinkpad);
1438 /* get the RTCP sync pad */
1439 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1440 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1441 srcpad = gst_element_get_pad (element, padname);
1443 gst_pad_link (srcpad, stream->sync_pad);
1444 gst_object_unref (srcpad);
1446 /* connect to the new-pad signal of the payload demuxer, this will expose the
1447 * new pad by ghosting it. */
1448 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1449 "new-payload-type", (GCallback) new_payload_found, stream);
1450 /* connect to the request-pt-map signal. This signal will be emited by the
1451 * demuxer so that it can apply a proper caps on the buffers for the
1453 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1454 "request-pt-map", (GCallback) pt_map_requested, session);
1456 GST_RTP_SESSION_UNLOCK (session);
1463 GST_RTP_SESSION_UNLOCK (session);
1464 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1469 /* Create a pad for receiving RTP for the session in @name. Must be called with
1473 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1475 GstPad *result, *sinkdpad;
1477 GstRtpBinSession *session;
1478 GstPadLinkReturn lres;
1480 /* first get the session number */
1481 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1484 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1486 /* get or create session */
1487 session = find_session_by_id (rtpbin, sessid);
1489 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1490 /* create session now */
1491 session = create_session (rtpbin, sessid);
1492 if (session == NULL)
1496 /* check if pad was requested */
1497 if (session->recv_rtp_sink != NULL)
1500 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1501 /* get recv_rtp pad and store */
1502 session->recv_rtp_sink =
1503 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1504 if (session->recv_rtp_sink == NULL)
1507 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1508 (GCallback) caps_changed, session);
1510 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1511 /* get srcpad, link to SSRCDemux */
1512 session->recv_rtp_src =
1513 gst_element_get_static_pad (session->session, "recv_rtp_src");
1514 if (session->recv_rtp_src == NULL)
1517 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1518 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1519 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1520 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1521 gst_object_unref (sinkdpad);
1522 if (lres != GST_PAD_LINK_OK)
1525 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1526 session->demux_newpad_sig = g_signal_connect (session->demux,
1527 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1529 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1531 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1532 gst_pad_set_active (result, TRUE);
1533 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1540 g_warning ("gstrtpbin: invalid name given");
1545 /* create_session already warned */
1550 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1556 g_warning ("gstrtpbin: failed to get session pad");
1561 g_warning ("gstrtpbin: failed to link pads");
1566 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1570 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1575 GstRtpBinSession *session;
1577 GstPadLinkReturn lres;
1579 /* first get the session number */
1580 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1583 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1585 /* get or create the session */
1586 session = find_session_by_id (rtpbin, sessid);
1588 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1589 /* create session now */
1590 session = create_session (rtpbin, sessid);
1591 if (session == NULL)
1595 /* check if pad was requested */
1596 if (session->recv_rtcp_sink != NULL)
1599 /* get recv_rtp pad and store */
1600 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1601 session->recv_rtcp_sink =
1602 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1603 if (session->recv_rtcp_sink == NULL)
1606 /* get srcpad, link to SSRCDemux */
1607 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1608 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1609 if (session->sync_src == NULL)
1612 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1613 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1614 lres = gst_pad_link (session->sync_src, sinkdpad);
1615 gst_object_unref (sinkdpad);
1616 if (lres != GST_PAD_LINK_OK)
1620 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1621 gst_pad_set_active (result, TRUE);
1622 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1629 g_warning ("gstrtpbin: invalid name given");
1634 /* create_session already warned */
1639 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1645 g_warning ("gstrtpbin: failed to get session pad");
1650 g_warning ("gstrtpbin: failed to link pads");
1655 /* Create a pad for sending RTP for the session in @name. Must be called with
1659 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1661 GstPad *result, *srcghost;
1664 GstRtpBinSession *session;
1665 GstElementClass *klass;
1667 /* first get the session number */
1668 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1671 /* get or create session */
1672 session = find_session_by_id (rtpbin, sessid);
1674 /* create session now */
1675 session = create_session (rtpbin, sessid);
1676 if (session == NULL)
1680 /* check if pad was requested */
1681 if (session->send_rtp_sink != NULL)
1684 /* get send_rtp pad and store */
1685 session->send_rtp_sink =
1686 gst_element_get_request_pad (session->session, "send_rtp_sink");
1687 if (session->send_rtp_sink == NULL)
1691 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1692 gst_pad_set_active (result, TRUE);
1693 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1696 session->send_rtp_src =
1697 gst_element_get_static_pad (session->session, "send_rtp_src");
1698 if (session->send_rtp_src == NULL)
1701 /* ghost the new source pad */
1702 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1703 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1704 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1706 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1707 gst_pad_set_active (srcghost, TRUE);
1708 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1716 g_warning ("gstrtpbin: invalid name given");
1721 /* create_session already warned */
1726 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1732 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1737 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1743 /* Create a pad for sending RTCP for the session in @name. Must be called with
1747 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1751 GstRtpBinSession *session;
1753 /* first get the session number */
1754 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1757 /* get or create session */
1758 session = find_session_by_id (rtpbin, sessid);
1762 /* check if pad was requested */
1763 if (session->send_rtcp_src != NULL)
1766 /* get rtcp_src pad and store */
1767 session->send_rtcp_src =
1768 gst_element_get_request_pad (session->session, "send_rtcp_src");
1769 if (session->send_rtcp_src == NULL)
1773 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1774 gst_pad_set_active (result, TRUE);
1775 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1782 g_warning ("gstrtpbin: invalid name given");
1787 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1792 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1798 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1806 gst_rtp_bin_request_new_pad (GstElement * element,
1807 GstPadTemplate * templ, const gchar * name)
1810 GstElementClass *klass;
1813 g_return_val_if_fail (templ != NULL, NULL);
1814 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1816 rtpbin = GST_RTP_BIN (element);
1817 klass = GST_ELEMENT_GET_CLASS (element);
1819 GST_RTP_BIN_LOCK (rtpbin);
1821 /* figure out the template */
1822 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1823 result = create_recv_rtp (rtpbin, templ, name);
1824 } else if (templ == gst_element_class_get_pad_template (klass,
1825 "recv_rtcp_sink_%d")) {
1826 result = create_recv_rtcp (rtpbin, templ, name);
1827 } else if (templ == gst_element_class_get_pad_template (klass,
1828 "send_rtp_sink_%d")) {
1829 result = create_send_rtp (rtpbin, templ, name);
1830 } else if (templ == gst_element_class_get_pad_template (klass,
1831 "send_rtcp_src_%d")) {
1832 result = create_rtcp (rtpbin, templ, name);
1834 goto wrong_template;
1836 GST_RTP_BIN_UNLOCK (rtpbin);
1843 GST_RTP_BIN_UNLOCK (rtpbin);
1844 g_warning ("gstrtpbin: this is not our template");
1850 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)