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
143 /* elementfactory information */
144 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
145 "Filter/Network/RTP",
146 "Implement an RTP bin",
147 "Wim Taymans <wim@fluendo.com>");
150 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
154 GST_STATIC_CAPS ("application/x-rtp")
157 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
158 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
161 GST_STATIC_CAPS ("application/x-rtcp")
164 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
168 GST_STATIC_CAPS ("application/x-rtp")
172 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
176 GST_STATIC_CAPS ("application/x-rtp")
179 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
180 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
183 GST_STATIC_CAPS ("application/x-rtcp")
186 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
190 GST_STATIC_CAPS ("application/x-rtp")
193 /* padtemplate for the internal pad */
194 static GstStaticPadTemplate rtpbin_sync_sink_template =
195 GST_STATIC_PAD_TEMPLATE ("sink_%d",
198 GST_STATIC_CAPS ("application/x-rtcp")
201 #define GST_RTP_BIN_GET_PRIVATE(obj) \
202 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
204 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
205 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
207 struct _GstRtpBinPrivate
211 GstClockTime ntp_ns_base;
214 /* signals and args */
217 SIGNAL_REQUEST_PT_MAP,
221 SIGNAL_ON_SSRC_COLLISION,
222 SIGNAL_ON_SSRC_VALIDATED,
223 SIGNAL_ON_SSRC_ACTIVE,
225 SIGNAL_ON_BYE_TIMEOUT,
230 #define DEFAULT_LATENCY_MS 200
239 typedef struct _GstRtpBinSession GstRtpBinSession;
240 typedef struct _GstRtpBinStream GstRtpBinStream;
241 typedef struct _GstRtpBinClient GstRtpBinClient;
243 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
245 static GstCaps *pt_map_requested (GstElement * element, guint pt,
246 GstRtpBinSession * session);
248 static void free_stream (GstRtpBinStream * stream);
250 /* Manages the RTP stream for one SSRC.
252 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
253 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
254 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
255 * together (see below).
257 struct _GstRtpBinStream
259 /* the SSRC of this stream */
265 /* the session this SSRC belongs to */
266 GstRtpBinSession *session;
268 /* the jitterbuffer of the SSRC */
271 /* the PT demuxer of the SSRC */
273 gulong demux_newpad_sig;
274 gulong demux_ptreq_sig;
276 /* the internal pad we use to get RTCP sync messages */
280 guint64 last_extrtptime;
282 /* mapping to local RTP and NTP time */
291 gint64 prev_ts_offset;
294 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
295 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
297 /* Manages the receiving end of the packets.
299 * There is one such structure for each RTP session (audio/video/...).
300 * We get the RTP/RTCP packets and stuff them into the session manager. From
301 * there they are pushed into an SSRC demuxer that splits the stream based on
302 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
303 * the GstRtpBinStream above).
305 struct _GstRtpBinSession
311 /* the session element */
313 /* the SSRC demuxer */
315 gulong demux_newpad_sig;
319 /* list of GstRtpBinStream */
322 /* mapping of payload type to caps */
325 /* the pads of the session */
326 GstPad *recv_rtp_sink;
327 GstPad *recv_rtp_src;
328 GstPad *recv_rtcp_sink;
330 GstPad *send_rtp_sink;
331 GstPad *send_rtp_src;
332 GstPad *send_rtcp_src;
335 /* Manages the RTP streams that come from one client and should therefore be
338 struct _GstRtpBinClient
340 /* the common CNAME for the streams */
351 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
352 static GstRtpBinSession *
353 find_session_by_id (GstRtpBin * rtpbin, gint id)
357 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
358 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
367 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
369 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
374 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
376 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
381 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
383 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
388 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
390 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
395 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
397 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
402 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
404 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
409 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
411 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
415 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
416 static GstRtpBinSession *
417 create_session (GstRtpBin * rtpbin, gint id)
419 GstRtpBinSession *sess;
420 GstElement *session, *demux;
422 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
425 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
428 sess = g_new0 (GstRtpBinSession, 1);
429 sess->lock = g_mutex_new ();
432 sess->session = session;
434 sess->ptmap = g_hash_table_new (NULL, NULL);
435 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
437 /* set NTP base or new session */
438 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
440 /* provide clock_rate to the session manager when needed */
441 g_signal_connect (session, "request-pt-map",
442 (GCallback) pt_map_requested, sess);
444 g_signal_connect (sess->session, "on-new-ssrc",
445 (GCallback) on_new_ssrc, sess);
446 g_signal_connect (sess->session, "on-ssrc-collision",
447 (GCallback) on_ssrc_collision, sess);
448 g_signal_connect (sess->session, "on-ssrc-validated",
449 (GCallback) on_ssrc_validated, sess);
450 g_signal_connect (sess->session, "on-ssrc-active",
451 (GCallback) on_ssrc_active, sess);
452 g_signal_connect (sess->session, "on-bye-ssrc",
453 (GCallback) on_bye_ssrc, sess);
454 g_signal_connect (sess->session, "on-bye-timeout",
455 (GCallback) on_bye_timeout, sess);
456 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
458 gst_bin_add (GST_BIN_CAST (rtpbin), session);
459 gst_element_set_state (session, GST_STATE_PLAYING);
460 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
461 gst_element_set_state (demux, GST_STATE_PLAYING);
468 g_warning ("gstrtpbin: could not create gstrtpsession element");
473 gst_object_unref (session);
474 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
480 free_session (GstRtpBinSession * sess)
486 gst_element_set_state (sess->session, GST_STATE_NULL);
487 gst_element_set_state (sess->demux, GST_STATE_NULL);
489 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
490 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
492 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
493 g_slist_free (sess->streams);
495 g_mutex_free (sess->lock);
496 g_hash_table_destroy (sess->ptmap);
498 bin->sessions = g_slist_remove (bin->sessions, sess);
504 static GstRtpBinStream *
505 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
509 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
510 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
512 if (stream->ssrc == ssrc)
519 /* get the payload type caps for the specific payload @pt in @session */
521 get_pt_map (GstRtpBinSession * session, guint pt)
523 GstCaps *caps = NULL;
526 GValue args[3] = { {0}, {0}, {0} };
528 GST_DEBUG ("searching pt %d in cache", pt);
530 GST_RTP_SESSION_LOCK (session);
532 /* first look in the cache */
533 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
539 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
541 /* not in cache, send signal to request caps */
542 g_value_init (&args[0], GST_TYPE_ELEMENT);
543 g_value_set_object (&args[0], bin);
544 g_value_init (&args[1], G_TYPE_UINT);
545 g_value_set_uint (&args[1], session->id);
546 g_value_init (&args[2], G_TYPE_UINT);
547 g_value_set_uint (&args[2], pt);
549 g_value_init (&ret, GST_TYPE_CAPS);
550 g_value_set_boxed (&ret, NULL);
552 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
554 caps = (GstCaps *) g_value_get_boxed (&ret);
558 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
561 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)
587 GSList *sessions, *streams;
589 GST_RTP_BIN_LOCK (bin);
590 GST_DEBUG_OBJECT (bin, "clearing pt map");
591 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
592 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
594 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
595 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
597 GST_RTP_SESSION_LOCK (session);
598 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
600 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
601 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
603 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
604 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
605 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
607 GST_RTP_SESSION_UNLOCK (session);
609 GST_RTP_BIN_UNLOCK (bin);
612 static GstRtpBinClient *
613 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
615 GstRtpBinClient *result = NULL;
618 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
619 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
621 if (len != client->cname_len)
624 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
625 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
632 /* nothing found, create one */
633 if (result == NULL) {
634 result = g_new0 (GstRtpBinClient, 1);
635 result->cname = g_strndup ((gchar *) data, len);
636 result->cname_len = len;
637 result->min_delta = G_MAXINT64;
638 bin->clients = g_slist_prepend (bin->clients, result);
639 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
646 free_client (GstRtpBinClient * client)
648 g_free (client->cname);
652 /* associate a stream to the given CNAME. This will make sure all streams for
653 * that CNAME are synchronized together. */
655 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
658 GstRtpBinClient *client;
662 /* first find or create the CNAME */
663 client = get_client (bin, len, data, &created);
665 /* find stream in the client */
666 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
667 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
669 if (ostream == stream)
672 /* not found, add it to the list */
674 GST_DEBUG_OBJECT (bin,
675 "new association of SSRC %08x with client %p with CNAME %s",
676 stream->ssrc, client, client->cname);
677 client->streams = g_slist_prepend (client->streams, stream);
680 GST_DEBUG_OBJECT (bin,
681 "found association of SSRC %08x with client %p with CNAME %s",
682 stream->ssrc, client, client->cname);
685 /* we can only continue if we know the local clock-base and clock-rate */
686 if (stream->clock_base == -1)
688 if (stream->clock_rate <= 0)
691 /* map last RTP time to local timeline using our clock-base */
692 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
694 GST_DEBUG_OBJECT (bin,
695 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
696 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
697 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
699 /* calculate local NTP time in gstreamer timestamp */
701 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
703 /* calculate delta between server and receiver */
704 stream->unix_delta = stream->last_unix - stream->local_unix;
706 GST_DEBUG_OBJECT (bin,
707 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
708 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
711 /* recalc inter stream playout offset, but only if there are more than one
713 if (client->nstreams > 1) {
716 /* calculate the min of all deltas */
718 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
719 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
721 if (ostream->unix_delta < min)
722 min = ostream->unix_delta;
725 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
728 /* calculate offsets for each stream */
729 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
730 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
732 ostream->ts_offset = ostream->unix_delta - min;
734 /* delta changed, see how much */
735 if (ostream->prev_ts_offset != ostream->ts_offset) {
738 if (ostream->prev_ts_offset > ostream->ts_offset)
739 diff = ostream->prev_ts_offset - ostream->ts_offset;
741 diff = ostream->ts_offset - ostream->prev_ts_offset;
743 /* only change diff when it changed more than 1 millisecond. This
744 * compensates for rounding errors in NTP to RTP timestamp
746 if (diff > GST_MSECOND)
747 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
749 ostream->prev_ts_offset = ostream->ts_offset;
751 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
752 ostream->ssrc, ostream->ts_offset);
759 GST_WARNING_OBJECT (bin, "we have no clock-base");
764 GST_WARNING_OBJECT (bin, "we have no clock-rate");
769 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
770 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
771 (b) = gst_rtcp_packet_move_to_next ((packet)))
773 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
774 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
775 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
777 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
778 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
779 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
782 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
784 GstFlowReturn ret = GST_FLOW_OK;
785 GstRtpBinStream *stream;
787 GstRTCPPacket packet;
791 gboolean have_sr, have_sdes;
794 stream = gst_pad_get_element_private (pad);
797 GST_DEBUG_OBJECT (bin, "received sync packet");
799 if (!gst_rtcp_buffer_validate (buffer))
804 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
805 /* first packet must be SR or RR or else the validate would have failed */
806 switch (gst_rtcp_packet_get_type (&packet)) {
807 case GST_RTCP_TYPE_SR:
808 /* only parse first. There is only supposed to be one SR in the packet
809 * but we will deal with malformed packets gracefully */
812 /* get NTP and RTP times */
813 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
816 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
817 /* ignore SR that is not ours */
818 if (ssrc != stream->ssrc)
823 /* store values in the stream */
824 stream->have_sync = TRUE;
825 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
826 /* use extended timestamp */
827 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
829 case GST_RTCP_TYPE_SDES:
831 gboolean more_items, more_entries;
833 /* only deal with first SDES, there is only supposed to be one SDES in
834 * the RTCP packet but we deal with bad packets gracefully. Also bail
835 * out if we have not seen an SR item yet. */
836 if (have_sdes || !have_sr)
839 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
840 /* skip items that are not about the SSRC of the sender */
841 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
844 /* find the CNAME entry */
845 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
846 GstRTCPSDESType type;
850 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
852 if (type == GST_RTCP_SDES_CNAME) {
853 stream->clock_base = GST_BUFFER_OFFSET (buffer);
854 /* associate the stream to CNAME */
855 gst_rtp_bin_associate (bin, stream, len, data);
863 /* we can ignore these packets */
868 gst_buffer_unref (buffer);
875 /* this is fatal and should be filtered earlier */
876 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
877 ("invalid RTCP packet received"));
878 gst_buffer_unref (buffer);
879 return GST_FLOW_ERROR;
883 /* create a new stream with @ssrc in @session. Must be called with
884 * RTP_SESSION_LOCK. */
885 static GstRtpBinStream *
886 create_stream (GstRtpBinSession * session, guint32 ssrc)
888 GstElement *buffer, *demux;
889 GstRtpBinStream *stream;
890 GstPadTemplate *templ;
893 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
894 goto no_jitterbuffer;
896 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
899 stream = g_new0 (GstRtpBinStream, 1);
901 stream->bin = session->bin;
902 stream->session = session;
903 stream->buffer = buffer;
904 stream->demux = demux;
905 stream->last_extrtptime = -1;
906 stream->have_sync = FALSE;
907 session->streams = g_slist_prepend (session->streams, stream);
909 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
910 * pad. We will link this pad later. */
911 padname = g_strdup_printf ("sync_%d", ssrc);
912 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
913 stream->sync_pad = gst_pad_new_from_template (templ, padname);
914 gst_object_unref (templ);
915 gst_object_ref (stream->sync_pad);
916 gst_object_sink (stream->sync_pad);
917 gst_pad_set_element_private (stream->sync_pad, stream);
918 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
919 gst_pad_set_active (stream->sync_pad, TRUE);
921 /* provide clock_rate to the jitterbuffer when needed */
922 g_signal_connect (buffer, "request-pt-map",
923 (GCallback) pt_map_requested, session);
925 /* configure latency */
926 g_object_set (buffer, "latency", session->bin->latency, NULL);
928 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
929 gst_element_set_state (buffer, GST_STATE_PLAYING);
930 gst_bin_add (GST_BIN_CAST (session->bin), demux);
931 gst_element_set_state (demux, GST_STATE_PLAYING);
934 gst_element_link (buffer, demux);
941 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
946 gst_object_unref (buffer);
947 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
953 free_stream (GstRtpBinStream * stream)
955 GstRtpBinSession *session;
957 session = stream->session;
959 gst_element_set_state (stream->buffer, GST_STATE_NULL);
960 gst_element_set_state (stream->demux, GST_STATE_NULL);
962 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
963 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
965 gst_object_unref (stream->sync_pad);
967 session->streams = g_slist_remove (session->streams, stream);
972 /* GObject vmethods */
973 static void gst_rtp_bin_dispose (GObject * object);
974 static void gst_rtp_bin_finalize (GObject * object);
975 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
976 const GValue * value, GParamSpec * pspec);
977 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
978 GValue * value, GParamSpec * pspec);
980 /* GstElement vmethods */
981 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
982 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
983 GstStateChange transition);
984 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
985 GstPadTemplate * templ, const gchar * name);
986 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
987 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
989 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
992 gst_rtp_bin_base_init (gpointer klass)
994 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
997 gst_element_class_add_pad_template (element_class,
998 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
999 gst_element_class_add_pad_template (element_class,
1000 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1001 gst_element_class_add_pad_template (element_class,
1002 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1005 gst_element_class_add_pad_template (element_class,
1006 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1007 gst_element_class_add_pad_template (element_class,
1008 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1009 gst_element_class_add_pad_template (element_class,
1010 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1012 gst_element_class_set_details (element_class, &rtpbin_details);
1016 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1018 GObjectClass *gobject_class;
1019 GstElementClass *gstelement_class;
1021 gobject_class = (GObjectClass *) klass;
1022 gstelement_class = (GstElementClass *) klass;
1024 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1026 gobject_class->dispose = gst_rtp_bin_dispose;
1027 gobject_class->finalize = gst_rtp_bin_finalize;
1028 gobject_class->set_property = gst_rtp_bin_set_property;
1029 gobject_class->get_property = gst_rtp_bin_get_property;
1031 g_object_class_install_property (gobject_class, PROP_LATENCY,
1032 g_param_spec_uint ("latency", "Buffer latency in ms",
1033 "Default amount of ms to buffer in the jitterbuffers", 0,
1034 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1037 * GstRtpBin::request-pt-map:
1038 * @rtpbin: the object which received the signal
1039 * @session: the session
1042 * Request the payload type as #GstCaps for @pt in @session.
1044 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1045 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1046 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1047 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1048 G_TYPE_UINT, G_TYPE_UINT);
1050 * GstRtpBin::clear-pt-map:
1051 * @rtpbin: the object which received the signal
1053 * Clear all previously cached pt-mapping obtained with
1054 * GstRtpBin::request-pt-map.
1056 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1057 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1058 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1059 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1063 * GstRtpBin::on-new-ssrc:
1064 * @rtpbin: the object which received the signal
1065 * @session: the session
1068 * Notify of a new SSRC that entered @session.
1070 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1071 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1072 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1073 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1074 G_TYPE_UINT, G_TYPE_UINT);
1076 * GstRtpBin::on-ssrc_collision:
1077 * @rtpbin: the object which received the signal
1078 * @session: the session
1081 * Notify when we have an SSRC collision
1083 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1084 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1085 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1086 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1087 G_TYPE_UINT, G_TYPE_UINT);
1089 * GstRtpBin::on-ssrc_validated:
1090 * @rtpbin: the object which received the signal
1091 * @session: the session
1094 * Notify of a new SSRC that became validated.
1096 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1097 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1098 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1099 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1100 G_TYPE_UINT, G_TYPE_UINT);
1102 * GstRtpBin::on-ssrc_active:
1103 * @rtpbin: the object which received the signal
1104 * @session: the session
1107 * Notify of a SSRC that is active, i.e., sending RTCP.
1109 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1110 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1111 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1112 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1113 G_TYPE_UINT, G_TYPE_UINT);
1116 * GstRtpBin::on-bye-ssrc:
1117 * @rtpbin: the object which received the signal
1118 * @session: the session
1121 * Notify of an SSRC that became inactive because of a BYE packet.
1123 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1124 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1125 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1126 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1127 G_TYPE_UINT, G_TYPE_UINT);
1129 * GstRtpBin::on-bye-timeout:
1130 * @rtpbin: the object which received the signal
1131 * @session: the session
1134 * Notify of an SSRC that has timed out because of BYE
1136 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1137 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1138 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1139 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1140 G_TYPE_UINT, G_TYPE_UINT);
1142 * GstRtpBin::on-timeout:
1143 * @rtpbin: the object which received the signal
1144 * @session: the session
1147 * Notify of an SSRC that has timed out
1149 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1150 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1151 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1152 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1153 G_TYPE_UINT, G_TYPE_UINT);
1155 gstelement_class->provide_clock =
1156 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1157 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1158 gstelement_class->request_new_pad =
1159 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1160 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1162 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1164 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1168 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1170 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1171 rtpbin->priv->bin_lock = g_mutex_new ();
1172 rtpbin->provided_clock = gst_system_clock_obtain ();
1173 rtpbin->latency = DEFAULT_LATENCY_MS;
1177 gst_rtp_bin_dispose (GObject * object)
1181 rtpbin = GST_RTP_BIN (object);
1183 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1184 g_slist_free (rtpbin->sessions);
1185 rtpbin->sessions = NULL;
1186 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1187 g_slist_free (rtpbin->clients);
1188 rtpbin->clients = NULL;
1190 G_OBJECT_CLASS (parent_class)->dispose (object);
1194 gst_rtp_bin_finalize (GObject * object)
1198 rtpbin = GST_RTP_BIN (object);
1200 g_mutex_free (rtpbin->priv->bin_lock);
1201 gst_object_unref (rtpbin->provided_clock);
1203 G_OBJECT_CLASS (parent_class)->finalize (object);
1207 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1208 const GValue * value, GParamSpec * pspec)
1212 rtpbin = GST_RTP_BIN (object);
1216 GST_RTP_BIN_LOCK (rtpbin);
1217 rtpbin->latency = g_value_get_uint (value);
1218 GST_RTP_BIN_UNLOCK (rtpbin);
1221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1227 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1228 GValue * value, GParamSpec * pspec)
1232 rtpbin = GST_RTP_BIN (object);
1236 GST_RTP_BIN_LOCK (rtpbin);
1237 g_value_set_uint (value, rtpbin->latency);
1238 GST_RTP_BIN_UNLOCK (rtpbin);
1241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1247 gst_rtp_bin_provide_clock (GstElement * element)
1251 rtpbin = GST_RTP_BIN (element);
1253 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1257 calc_ntp_ns_base (GstRtpBin * bin)
1263 /* get the current time and convert it to NTP time in nanoseconds */
1264 g_get_current_time (¤t);
1265 now = GST_TIMEVAL_TO_TIME (current);
1266 now += (2208988800LL * GST_SECOND);
1268 GST_RTP_BIN_LOCK (bin);
1269 bin->priv->ntp_ns_base = now;
1270 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1271 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1273 g_object_set (session->session, "ntp-ns-base", now, NULL);
1275 GST_RTP_BIN_UNLOCK (bin);
1280 static GstStateChangeReturn
1281 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1283 GstStateChangeReturn res;
1286 rtpbin = GST_RTP_BIN (element);
1288 switch (transition) {
1289 case GST_STATE_CHANGE_NULL_TO_READY:
1291 case GST_STATE_CHANGE_READY_TO_PAUSED:
1293 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1294 calc_ntp_ns_base (rtpbin);
1300 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1302 switch (transition) {
1303 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1305 case GST_STATE_CHANGE_PAUSED_TO_READY:
1307 case GST_STATE_CHANGE_READY_TO_NULL:
1315 /* a new pad (SSRC) was created in @session */
1317 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1318 GstRtpBinStream * stream)
1321 GstElementClass *klass;
1322 GstPadTemplate *templ;
1326 rtpbin = stream->bin;
1328 GST_DEBUG ("new payload pad %d", pt);
1330 /* ghost the pad to the parent */
1331 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1332 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1333 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1334 stream->session->id, stream->ssrc, pt);
1335 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1338 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1339 gst_pad_set_active (gpad, TRUE);
1340 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1344 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1349 rtpbin = session->bin;
1351 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1354 caps = get_pt_map (session, pt);
1363 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1368 /* emited when caps changed for the session */
1370 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1375 const GstStructure *s;
1379 g_object_get (pad, "caps", &caps, NULL);
1384 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1386 s = gst_caps_get_structure (caps, 0);
1388 /* get payload, finish when it's not there */
1389 if (!gst_structure_get_int (s, "payload", &payload))
1392 GST_RTP_SESSION_LOCK (session);
1393 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1394 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1395 GST_RTP_SESSION_UNLOCK (session);
1398 /* a new pad (SSRC) was created in @session */
1400 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1401 GstRtpBinSession * session)
1403 GstRtpBinStream *stream;
1404 GstPad *sinkpad, *srcpad;
1408 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1410 GST_RTP_SESSION_LOCK (session);
1412 /* create new stream */
1413 stream = create_stream (session, ssrc);
1417 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1418 if ((caps = gst_pad_get_caps (pad))) {
1419 const GstStructure *s;
1422 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1424 s = gst_caps_get_structure (caps, 0);
1426 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1427 stream->clock_rate = -1;
1429 if (gst_structure_get_uint (s, "clock-base", &val))
1430 stream->clock_base = val;
1432 stream->clock_base = -1;
1435 /* get pad and link */
1436 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1437 padname = g_strdup_printf ("src_%d", ssrc);
1438 srcpad = gst_element_get_pad (element, padname);
1440 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1441 gst_pad_link (srcpad, sinkpad);
1442 gst_object_unref (sinkpad);
1444 /* get the RTCP sync pad */
1445 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1446 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1447 srcpad = gst_element_get_pad (element, padname);
1449 gst_pad_link (srcpad, stream->sync_pad);
1450 gst_object_unref (srcpad);
1452 /* connect to the new-pad signal of the payload demuxer, this will expose the
1453 * new pad by ghosting it. */
1454 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1455 "new-payload-type", (GCallback) new_payload_found, stream);
1456 /* connect to the request-pt-map signal. This signal will be emited by the
1457 * demuxer so that it can apply a proper caps on the buffers for the
1459 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1460 "request-pt-map", (GCallback) pt_map_requested, session);
1462 GST_RTP_SESSION_UNLOCK (session);
1469 GST_RTP_SESSION_UNLOCK (session);
1470 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1475 /* Create a pad for receiving RTP for the session in @name. Must be called with
1479 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1481 GstPad *result, *sinkdpad;
1483 GstRtpBinSession *session;
1484 GstPadLinkReturn lres;
1486 /* first get the session number */
1487 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1490 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1492 /* get or create session */
1493 session = find_session_by_id (rtpbin, sessid);
1495 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1496 /* create session now */
1497 session = create_session (rtpbin, sessid);
1498 if (session == NULL)
1502 /* check if pad was requested */
1503 if (session->recv_rtp_sink != NULL)
1506 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1507 /* get recv_rtp pad and store */
1508 session->recv_rtp_sink =
1509 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1510 if (session->recv_rtp_sink == NULL)
1513 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1514 (GCallback) caps_changed, session);
1516 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1517 /* get srcpad, link to SSRCDemux */
1518 session->recv_rtp_src =
1519 gst_element_get_static_pad (session->session, "recv_rtp_src");
1520 if (session->recv_rtp_src == NULL)
1523 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1524 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1525 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1526 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1527 gst_object_unref (sinkdpad);
1528 if (lres != GST_PAD_LINK_OK)
1531 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1532 session->demux_newpad_sig = g_signal_connect (session->demux,
1533 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1535 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1537 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1538 gst_pad_set_active (result, TRUE);
1539 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1546 g_warning ("gstrtpbin: invalid name given");
1551 /* create_session already warned */
1556 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1562 g_warning ("gstrtpbin: failed to get session pad");
1567 g_warning ("gstrtpbin: failed to link pads");
1572 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1576 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1581 GstRtpBinSession *session;
1583 GstPadLinkReturn lres;
1585 /* first get the session number */
1586 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1589 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1591 /* get or create the session */
1592 session = find_session_by_id (rtpbin, sessid);
1594 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1595 /* create session now */
1596 session = create_session (rtpbin, sessid);
1597 if (session == NULL)
1601 /* check if pad was requested */
1602 if (session->recv_rtcp_sink != NULL)
1605 /* get recv_rtp pad and store */
1606 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1607 session->recv_rtcp_sink =
1608 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1609 if (session->recv_rtcp_sink == NULL)
1612 /* get srcpad, link to SSRCDemux */
1613 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1614 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1615 if (session->sync_src == NULL)
1618 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1619 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1620 lres = gst_pad_link (session->sync_src, sinkdpad);
1621 gst_object_unref (sinkdpad);
1622 if (lres != GST_PAD_LINK_OK)
1626 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1627 gst_pad_set_active (result, TRUE);
1628 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1635 g_warning ("gstrtpbin: invalid name given");
1640 /* create_session already warned */
1645 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1651 g_warning ("gstrtpbin: failed to get session pad");
1656 g_warning ("gstrtpbin: failed to link pads");
1661 /* Create a pad for sending RTP for the session in @name. Must be called with
1665 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1667 GstPad *result, *srcghost;
1670 GstRtpBinSession *session;
1671 GstElementClass *klass;
1673 /* first get the session number */
1674 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1677 /* get or create session */
1678 session = find_session_by_id (rtpbin, sessid);
1680 /* create session now */
1681 session = create_session (rtpbin, sessid);
1682 if (session == NULL)
1686 /* check if pad was requested */
1687 if (session->send_rtp_sink != NULL)
1690 /* get send_rtp pad and store */
1691 session->send_rtp_sink =
1692 gst_element_get_request_pad (session->session, "send_rtp_sink");
1693 if (session->send_rtp_sink == NULL)
1697 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1698 gst_pad_set_active (result, TRUE);
1699 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1702 session->send_rtp_src =
1703 gst_element_get_static_pad (session->session, "send_rtp_src");
1704 if (session->send_rtp_src == NULL)
1707 /* ghost the new source pad */
1708 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1709 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1710 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1712 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1713 gst_pad_set_active (srcghost, TRUE);
1714 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1722 g_warning ("gstrtpbin: invalid name given");
1727 /* create_session already warned */
1732 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1738 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1743 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1749 /* Create a pad for sending RTCP for the session in @name. Must be called with
1753 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1757 GstRtpBinSession *session;
1759 /* first get the session number */
1760 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1763 /* get or create session */
1764 session = find_session_by_id (rtpbin, sessid);
1768 /* check if pad was requested */
1769 if (session->send_rtcp_src != NULL)
1772 /* get rtcp_src pad and store */
1773 session->send_rtcp_src =
1774 gst_element_get_request_pad (session->session, "send_rtcp_src");
1775 if (session->send_rtcp_src == NULL)
1779 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1780 gst_pad_set_active (result, TRUE);
1781 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1788 g_warning ("gstrtpbin: invalid name given");
1793 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1798 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1804 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1812 gst_rtp_bin_request_new_pad (GstElement * element,
1813 GstPadTemplate * templ, const gchar * name)
1816 GstElementClass *klass;
1819 g_return_val_if_fail (templ != NULL, NULL);
1820 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1822 rtpbin = GST_RTP_BIN (element);
1823 klass = GST_ELEMENT_GET_CLASS (element);
1825 GST_RTP_BIN_LOCK (rtpbin);
1827 /* figure out the template */
1828 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1829 result = create_recv_rtp (rtpbin, templ, name);
1830 } else if (templ == gst_element_class_get_pad_template (klass,
1831 "recv_rtcp_sink_%d")) {
1832 result = create_recv_rtcp (rtpbin, templ, name);
1833 } else if (templ == gst_element_class_get_pad_template (klass,
1834 "send_rtp_sink_%d")) {
1835 result = create_send_rtp (rtpbin, templ, name);
1836 } else if (templ == gst_element_class_get_pad_template (klass,
1837 "send_rtcp_src_%d")) {
1838 result = create_rtcp (rtpbin, templ, name);
1840 goto wrong_template;
1842 GST_RTP_BIN_UNLOCK (rtpbin);
1849 GST_RTP_BIN_UNLOCK (rtpbin);
1850 g_warning ("gstrtpbin: this is not our template");
1856 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)