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,
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_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
390 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
395 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
397 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
402 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
404 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
408 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
409 static GstRtpBinSession *
410 create_session (GstRtpBin * rtpbin, gint id)
412 GstRtpBinSession *sess;
413 GstElement *session, *demux;
415 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
418 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
421 sess = g_new0 (GstRtpBinSession, 1);
422 sess->lock = g_mutex_new ();
425 sess->session = session;
427 sess->ptmap = g_hash_table_new (NULL, NULL);
428 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
430 /* set NTP base or new session */
431 g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
433 /* provide clock_rate to the session manager when needed */
434 g_signal_connect (session, "request-pt-map",
435 (GCallback) pt_map_requested, sess);
437 g_signal_connect (sess->session, "on-new-ssrc",
438 (GCallback) on_new_ssrc, sess);
439 g_signal_connect (sess->session, "on-ssrc-collision",
440 (GCallback) on_ssrc_collision, sess);
441 g_signal_connect (sess->session, "on-ssrc-validated",
442 (GCallback) on_ssrc_validated, sess);
443 g_signal_connect (sess->session, "on-bye-ssrc",
444 (GCallback) on_bye_ssrc, sess);
445 g_signal_connect (sess->session, "on-bye-timeout",
446 (GCallback) on_bye_timeout, sess);
447 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
449 gst_bin_add (GST_BIN_CAST (rtpbin), session);
450 gst_element_set_state (session, GST_STATE_PLAYING);
451 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
452 gst_element_set_state (demux, GST_STATE_PLAYING);
459 g_warning ("gstrtpbin: could not create gstrtpsession element");
464 gst_object_unref (session);
465 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
471 free_session (GstRtpBinSession * sess)
477 gst_element_set_state (sess->session, GST_STATE_NULL);
478 gst_element_set_state (sess->demux, GST_STATE_NULL);
480 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
481 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
483 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
484 g_slist_free (sess->streams);
486 g_mutex_free (sess->lock);
487 g_hash_table_destroy (sess->ptmap);
489 bin->sessions = g_slist_remove (bin->sessions, sess);
495 static GstRtpBinStream *
496 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
500 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
501 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
503 if (stream->ssrc == ssrc)
510 /* get the payload type caps for the specific payload @pt in @session */
512 get_pt_map (GstRtpBinSession * session, guint pt)
514 GstCaps *caps = NULL;
517 GValue args[3] = { {0}, {0}, {0} };
519 GST_DEBUG ("searching pt %d in cache", pt);
521 GST_RTP_SESSION_LOCK (session);
523 /* first look in the cache */
524 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
530 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
532 /* not in cache, send signal to request caps */
533 g_value_init (&args[0], GST_TYPE_ELEMENT);
534 g_value_set_object (&args[0], bin);
535 g_value_init (&args[1], G_TYPE_UINT);
536 g_value_set_uint (&args[1], session->id);
537 g_value_init (&args[2], G_TYPE_UINT);
538 g_value_set_uint (&args[2], pt);
540 g_value_init (&ret, GST_TYPE_CAPS);
541 g_value_set_boxed (&ret, NULL);
543 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
545 caps = (GstCaps *) g_value_get_boxed (&ret);
549 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
552 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
555 GST_RTP_SESSION_UNLOCK (session);
562 GST_RTP_SESSION_UNLOCK (session);
563 GST_DEBUG ("no pt map could be obtained");
569 return_true (gpointer key, gpointer value, gpointer user_data)
575 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
579 GST_RTP_BIN_LOCK (bin);
580 GST_DEBUG_OBJECT (bin, "clearing pt map");
581 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
582 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
584 GST_RTP_SESSION_LOCK (session);
586 /* This requires GLib 2.12 */
587 g_hash_table_remove_all (session->ptmap);
589 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
591 GST_RTP_SESSION_UNLOCK (session);
593 GST_RTP_BIN_UNLOCK (bin);
596 static GstRtpBinClient *
597 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
599 GstRtpBinClient *result = NULL;
602 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
603 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
605 if (len != client->cname_len)
608 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
609 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
616 /* nothing found, create one */
617 if (result == NULL) {
618 result = g_new0 (GstRtpBinClient, 1);
619 result->cname = g_strndup ((gchar *) data, len);
620 result->cname_len = len;
621 result->min_delta = G_MAXINT64;
622 bin->clients = g_slist_prepend (bin->clients, result);
623 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
630 free_client (GstRtpBinClient * client, GstRtpBin * bin)
632 bin->clients = g_slist_remove (bin->clients, client);
633 g_free (client->cname);
637 /* associate a stream to the given CNAME. This will make sure all streams for
638 * that CNAME are synchronized together. */
640 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
643 GstRtpBinClient *client;
647 /* first find or create the CNAME */
648 client = get_client (bin, len, data, &created);
650 /* find stream in the client */
651 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
652 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
654 if (ostream == stream)
657 /* not found, add it to the list */
659 GST_DEBUG_OBJECT (bin,
660 "new association of SSRC %08x with client %p with CNAME %s",
661 stream->ssrc, client, client->cname);
662 client->streams = g_slist_prepend (client->streams, stream);
665 GST_DEBUG_OBJECT (bin,
666 "found association of SSRC %08x with client %p with CNAME %s",
667 stream->ssrc, client, client->cname);
670 /* we can only continue if we know the local clock-base and clock-rate */
671 if (stream->clock_base == -1)
673 if (stream->clock_rate <= 0)
676 /* map last RTP time to local timeline using our clock-base */
677 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
679 GST_DEBUG_OBJECT (bin,
680 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
681 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
682 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
684 /* calculate local NTP time in gstreamer timestamp */
686 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
688 /* calculate delta between server and receiver */
689 stream->unix_delta = stream->last_unix - stream->local_unix;
691 GST_DEBUG_OBJECT (bin,
692 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
693 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
696 /* recalc inter stream playout offset, but only if there are more than one
698 if (client->nstreams > 1) {
701 /* calculate the min of all deltas */
703 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
704 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
706 if (ostream->unix_delta < min)
707 min = ostream->unix_delta;
710 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
713 /* calculate offsets for each stream */
714 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
715 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
717 ostream->ts_offset = ostream->unix_delta - min;
719 /* delta changed, see how much */
720 if (ostream->prev_ts_offset != ostream->ts_offset) {
723 if (ostream->prev_ts_offset > ostream->ts_offset)
724 diff = ostream->prev_ts_offset - ostream->ts_offset;
726 diff = ostream->ts_offset - ostream->prev_ts_offset;
728 /* only change diff when it changed more than 1 millisecond. This
729 * compensates for rounding errors in NTP to RTP timestamp
731 if (diff > GST_MSECOND)
732 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
734 ostream->prev_ts_offset = ostream->ts_offset;
736 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
737 ostream->ssrc, ostream->ts_offset);
744 GST_WARNING_OBJECT (bin, "we have no clock-base");
749 GST_WARNING_OBJECT (bin, "we have no clock-rate");
754 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
755 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
756 (b) = gst_rtcp_packet_move_to_next ((packet)))
758 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
759 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
760 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
762 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
763 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
764 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
767 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
769 GstFlowReturn ret = GST_FLOW_OK;
770 GstRtpBinStream *stream;
772 GstRTCPPacket packet;
776 gboolean have_sr, have_sdes;
779 stream = gst_pad_get_element_private (pad);
782 GST_DEBUG_OBJECT (bin, "received sync packet");
784 if (!gst_rtcp_buffer_validate (buffer))
789 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
790 /* first packet must be SR or RR or else the validate would have failed */
791 switch (gst_rtcp_packet_get_type (&packet)) {
792 case GST_RTCP_TYPE_SR:
793 /* only parse first. There is only supposed to be one SR in the packet
794 * but we will deal with malformed packets gracefully */
797 /* get NTP and RTP times */
798 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
801 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
802 /* ignore SR that is not ours */
803 if (ssrc != stream->ssrc)
808 /* store values in the stream */
809 stream->have_sync = TRUE;
810 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
811 /* use extended timestamp */
812 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
814 case GST_RTCP_TYPE_SDES:
816 gboolean more_items, more_entries;
818 /* only deal with first SDES, there is only supposed to be one SDES in
819 * the RTCP packet but we deal with bad packets gracefully. Also bail
820 * out if we have not seen an SR item yet. */
821 if (have_sdes || !have_sr)
824 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
825 /* skip items that are not about the SSRC of the sender */
826 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
829 /* find the CNAME entry */
830 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
831 GstRTCPSDESType type;
835 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
837 if (type == GST_RTCP_SDES_CNAME) {
838 stream->clock_base = GST_BUFFER_OFFSET (buffer);
839 /* associate the stream to CNAME */
840 gst_rtp_bin_associate (bin, stream, len, data);
848 /* we can ignore these packets */
853 gst_buffer_unref (buffer);
860 /* this is fatal and should be filtered earlier */
861 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
862 ("invalid RTCP packet received"));
863 gst_buffer_unref (buffer);
864 return GST_FLOW_ERROR;
868 /* create a new stream with @ssrc in @session. Must be called with
869 * RTP_SESSION_LOCK. */
870 static GstRtpBinStream *
871 create_stream (GstRtpBinSession * session, guint32 ssrc)
873 GstElement *buffer, *demux;
874 GstRtpBinStream *stream;
875 GstPadTemplate *templ;
878 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
879 goto no_jitterbuffer;
881 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
884 stream = g_new0 (GstRtpBinStream, 1);
886 stream->bin = session->bin;
887 stream->session = session;
888 stream->buffer = buffer;
889 stream->demux = demux;
890 stream->last_extrtptime = -1;
891 stream->have_sync = FALSE;
892 session->streams = g_slist_prepend (session->streams, stream);
894 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
895 * pad. We will link this pad later. */
896 padname = g_strdup_printf ("sync_%d", ssrc);
897 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
898 stream->sync_pad = gst_pad_new_from_template (templ, padname);
899 gst_object_unref (templ);
900 gst_object_ref (stream->sync_pad);
901 gst_object_sink (stream->sync_pad);
902 gst_pad_set_element_private (stream->sync_pad, stream);
903 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
904 gst_pad_set_active (stream->sync_pad, TRUE);
906 /* provide clock_rate to the jitterbuffer when needed */
907 g_signal_connect (buffer, "request-pt-map",
908 (GCallback) pt_map_requested, session);
910 /* configure latency */
911 g_object_set (buffer, "latency", session->bin->latency, NULL);
913 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
914 gst_element_set_state (buffer, GST_STATE_PLAYING);
915 gst_bin_add (GST_BIN_CAST (session->bin), demux);
916 gst_element_set_state (demux, GST_STATE_PLAYING);
919 gst_element_link (buffer, demux);
926 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
931 gst_object_unref (buffer);
932 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
938 free_stream (GstRtpBinStream * stream)
940 GstRtpBinSession *session;
942 session = stream->session;
944 gst_element_set_state (stream->buffer, GST_STATE_NULL);
945 gst_element_set_state (stream->demux, GST_STATE_NULL);
947 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
948 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
950 gst_object_unref (stream->sync_pad);
952 session->streams = g_slist_remove (session->streams, stream);
957 /* GObject vmethods */
958 static void gst_rtp_bin_dispose (GObject * object);
959 static void gst_rtp_bin_finalize (GObject * object);
960 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
961 const GValue * value, GParamSpec * pspec);
962 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
963 GValue * value, GParamSpec * pspec);
965 /* GstElement vmethods */
966 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
967 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
968 GstStateChange transition);
969 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
970 GstPadTemplate * templ, const gchar * name);
971 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
972 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
974 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
977 gst_rtp_bin_base_init (gpointer klass)
979 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
982 gst_element_class_add_pad_template (element_class,
983 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
984 gst_element_class_add_pad_template (element_class,
985 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
986 gst_element_class_add_pad_template (element_class,
987 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
990 gst_element_class_add_pad_template (element_class,
991 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
992 gst_element_class_add_pad_template (element_class,
993 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
994 gst_element_class_add_pad_template (element_class,
995 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
997 gst_element_class_set_details (element_class, &rtpbin_details);
1001 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1003 GObjectClass *gobject_class;
1004 GstElementClass *gstelement_class;
1006 gobject_class = (GObjectClass *) klass;
1007 gstelement_class = (GstElementClass *) klass;
1009 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1011 gobject_class->dispose = gst_rtp_bin_dispose;
1012 gobject_class->finalize = gst_rtp_bin_finalize;
1013 gobject_class->set_property = gst_rtp_bin_set_property;
1014 gobject_class->get_property = gst_rtp_bin_get_property;
1016 g_object_class_install_property (gobject_class, PROP_LATENCY,
1017 g_param_spec_uint ("latency", "Buffer latency in ms",
1018 "Default amount of ms to buffer in the jitterbuffers", 0,
1019 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1022 * GstRtpBin::request-pt-map:
1023 * @rtpbin: the object which received the signal
1024 * @session: the session
1027 * Request the payload type as #GstCaps for @pt in @session.
1029 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1030 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1031 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1032 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1033 G_TYPE_UINT, G_TYPE_UINT);
1035 * GstRtpBin::clear-pt-map:
1036 * @rtpbin: the object which received the signal
1038 * Clear all previously cached pt-mapping obtained with
1039 * GstRtpBin::request-pt-map.
1041 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1042 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1043 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass, clear_pt_map),
1044 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
1047 * GstRtpBin::on-new-ssrc:
1048 * @rtpbin: the object which received the signal
1049 * @session: the session
1052 * Notify of a new SSRC that entered @session.
1054 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1055 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1056 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1057 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1058 G_TYPE_UINT, G_TYPE_UINT);
1060 * GstRtpBin::on-ssrc_collision:
1061 * @rtpbin: the object which received the signal
1062 * @session: the session
1065 * Notify when we have an SSRC collision
1067 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1068 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1069 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1070 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1071 G_TYPE_UINT, G_TYPE_UINT);
1073 * GstRtpBin::on-ssrc_validated:
1074 * @rtpbin: the object which received the signal
1075 * @session: the session
1078 * Notify of a new SSRC that became validated.
1080 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1081 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1082 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1083 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1084 G_TYPE_UINT, G_TYPE_UINT);
1087 * GstRtpBin::on-bye-ssrc:
1088 * @rtpbin: the object which received the signal
1089 * @session: the session
1092 * Notify of an SSRC that became inactive because of a BYE packet.
1094 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1095 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1096 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1097 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1098 G_TYPE_UINT, G_TYPE_UINT);
1100 * GstRtpBin::on-bye-timeout:
1101 * @rtpbin: the object which received the signal
1102 * @session: the session
1105 * Notify of an SSRC that has timed out because of BYE
1107 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1108 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1109 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1110 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1111 G_TYPE_UINT, G_TYPE_UINT);
1113 * GstRtpBin::on-timeout:
1114 * @rtpbin: the object which received the signal
1115 * @session: the session
1118 * Notify of an SSRC that has timed out
1120 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1121 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1122 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1123 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1124 G_TYPE_UINT, G_TYPE_UINT);
1126 gstelement_class->provide_clock =
1127 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1128 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1129 gstelement_class->request_new_pad =
1130 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1131 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1133 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1135 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1139 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1141 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1142 rtpbin->priv->bin_lock = g_mutex_new ();
1143 rtpbin->provided_clock = gst_system_clock_obtain ();
1144 rtpbin->latency = DEFAULT_LATENCY_MS;
1148 gst_rtp_bin_dispose (GObject * object)
1152 rtpbin = GST_RTP_BIN (object);
1154 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1155 g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1156 g_slist_free (rtpbin->sessions);
1157 rtpbin->sessions = NULL;
1159 G_OBJECT_CLASS (parent_class)->dispose (object);
1163 gst_rtp_bin_finalize (GObject * object)
1167 rtpbin = GST_RTP_BIN (object);
1169 g_mutex_free (rtpbin->priv->bin_lock);
1170 gst_object_unref (rtpbin->provided_clock);
1171 g_slist_free (rtpbin->sessions);
1173 G_OBJECT_CLASS (parent_class)->finalize (object);
1177 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1178 const GValue * value, GParamSpec * pspec)
1182 rtpbin = GST_RTP_BIN (object);
1186 GST_RTP_BIN_LOCK (rtpbin);
1187 rtpbin->latency = g_value_get_uint (value);
1188 GST_RTP_BIN_UNLOCK (rtpbin);
1191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1197 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1198 GValue * value, GParamSpec * pspec)
1202 rtpbin = GST_RTP_BIN (object);
1206 GST_RTP_BIN_LOCK (rtpbin);
1207 g_value_set_uint (value, rtpbin->latency);
1208 GST_RTP_BIN_UNLOCK (rtpbin);
1211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1217 gst_rtp_bin_provide_clock (GstElement * element)
1221 rtpbin = GST_RTP_BIN (element);
1223 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1227 calc_ntp_ns_base (GstRtpBin * bin)
1233 /* get the current time and convert it to NTP time in nanoseconds */
1234 g_get_current_time (¤t);
1235 now = GST_TIMEVAL_TO_TIME (current);
1236 now += (2208988800LL * GST_SECOND);
1238 GST_RTP_BIN_LOCK (bin);
1239 bin->priv->ntp_ns_base = now;
1240 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1241 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1243 g_object_set (session->session, "ntp-ns-base", now, NULL);
1245 GST_RTP_BIN_UNLOCK (bin);
1250 static GstStateChangeReturn
1251 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1253 GstStateChangeReturn res;
1256 rtpbin = GST_RTP_BIN (element);
1258 switch (transition) {
1259 case GST_STATE_CHANGE_NULL_TO_READY:
1261 case GST_STATE_CHANGE_READY_TO_PAUSED:
1263 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1264 calc_ntp_ns_base (rtpbin);
1270 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1272 switch (transition) {
1273 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1275 case GST_STATE_CHANGE_PAUSED_TO_READY:
1277 case GST_STATE_CHANGE_READY_TO_NULL:
1285 /* a new pad (SSRC) was created in @session */
1287 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1288 GstRtpBinStream * stream)
1291 GstElementClass *klass;
1292 GstPadTemplate *templ;
1296 rtpbin = stream->bin;
1298 GST_DEBUG ("new payload pad %d", pt);
1300 /* ghost the pad to the parent */
1301 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1302 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1303 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1304 stream->session->id, stream->ssrc, pt);
1305 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1308 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1309 gst_pad_set_active (gpad, TRUE);
1310 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1314 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1319 rtpbin = session->bin;
1321 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1324 caps = get_pt_map (session, pt);
1333 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1338 /* emited when caps changed for the session */
1340 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1345 const GstStructure *s;
1349 g_object_get (pad, "caps", &caps, NULL);
1354 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1356 s = gst_caps_get_structure (caps, 0);
1358 /* get payload, finish when it's not there */
1359 if (!gst_structure_get_int (s, "payload", &payload))
1362 GST_RTP_SESSION_LOCK (session);
1363 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1364 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1365 GST_RTP_SESSION_UNLOCK (session);
1368 /* a new pad (SSRC) was created in @session */
1370 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1371 GstRtpBinSession * session)
1373 GstRtpBinStream *stream;
1374 GstPad *sinkpad, *srcpad;
1378 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1380 GST_RTP_SESSION_LOCK (session);
1382 /* create new stream */
1383 stream = create_stream (session, ssrc);
1387 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1388 if ((caps = gst_pad_get_caps (pad))) {
1389 const GstStructure *s;
1392 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1394 s = gst_caps_get_structure (caps, 0);
1396 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1397 stream->clock_rate = -1;
1399 if (gst_structure_get_uint (s, "clock-base", &val))
1400 stream->clock_base = val;
1402 stream->clock_base = -1;
1405 /* get pad and link */
1406 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1407 padname = g_strdup_printf ("src_%d", ssrc);
1408 srcpad = gst_element_get_pad (element, padname);
1410 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1411 gst_pad_link (srcpad, sinkpad);
1412 gst_object_unref (sinkpad);
1414 /* get the RTCP sync pad */
1415 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1416 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1417 srcpad = gst_element_get_pad (element, padname);
1419 gst_pad_link (srcpad, stream->sync_pad);
1420 gst_object_unref (srcpad);
1422 /* connect to the new-pad signal of the payload demuxer, this will expose the
1423 * new pad by ghosting it. */
1424 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1425 "new-payload-type", (GCallback) new_payload_found, stream);
1426 /* connect to the request-pt-map signal. This signal will be emited by the
1427 * demuxer so that it can apply a proper caps on the buffers for the
1429 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1430 "request-pt-map", (GCallback) pt_map_requested, session);
1432 GST_RTP_SESSION_UNLOCK (session);
1439 GST_RTP_SESSION_UNLOCK (session);
1440 GST_DEBUG_OBJECT (session->bin, "could not create stream");
1445 /* Create a pad for receiving RTP for the session in @name. Must be called with
1449 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1451 GstPad *result, *sinkdpad;
1453 GstRtpBinSession *session;
1454 GstPadLinkReturn lres;
1456 /* first get the session number */
1457 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1460 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1462 /* get or create session */
1463 session = find_session_by_id (rtpbin, sessid);
1465 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1466 /* create session now */
1467 session = create_session (rtpbin, sessid);
1468 if (session == NULL)
1472 /* check if pad was requested */
1473 if (session->recv_rtp_sink != NULL)
1476 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1477 /* get recv_rtp pad and store */
1478 session->recv_rtp_sink =
1479 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1480 if (session->recv_rtp_sink == NULL)
1483 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1484 (GCallback) caps_changed, session);
1486 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1487 /* get srcpad, link to SSRCDemux */
1488 session->recv_rtp_src =
1489 gst_element_get_static_pad (session->session, "recv_rtp_src");
1490 if (session->recv_rtp_src == NULL)
1493 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1494 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1495 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1496 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1497 gst_object_unref (sinkdpad);
1498 if (lres != GST_PAD_LINK_OK)
1501 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1502 session->demux_newpad_sig = g_signal_connect (session->demux,
1503 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1505 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1507 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1508 gst_pad_set_active (result, TRUE);
1509 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1516 g_warning ("gstrtpbin: invalid name given");
1521 /* create_session already warned */
1526 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1532 g_warning ("gstrtpbin: failed to get session pad");
1537 g_warning ("gstrtpbin: failed to link pads");
1542 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1546 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1551 GstRtpBinSession *session;
1553 GstPadLinkReturn lres;
1555 /* first get the session number */
1556 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1559 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1561 /* get or create the session */
1562 session = find_session_by_id (rtpbin, sessid);
1564 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1565 /* create session now */
1566 session = create_session (rtpbin, sessid);
1567 if (session == NULL)
1571 /* check if pad was requested */
1572 if (session->recv_rtcp_sink != NULL)
1575 /* get recv_rtp pad and store */
1576 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1577 session->recv_rtcp_sink =
1578 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1579 if (session->recv_rtcp_sink == NULL)
1582 /* get srcpad, link to SSRCDemux */
1583 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1584 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1585 if (session->sync_src == NULL)
1588 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1589 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1590 lres = gst_pad_link (session->sync_src, sinkdpad);
1591 gst_object_unref (sinkdpad);
1592 if (lres != GST_PAD_LINK_OK)
1596 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1597 gst_pad_set_active (result, TRUE);
1598 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1605 g_warning ("gstrtpbin: invalid name given");
1610 /* create_session already warned */
1615 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1621 g_warning ("gstrtpbin: failed to get session pad");
1626 g_warning ("gstrtpbin: failed to link pads");
1631 /* Create a pad for sending RTP for the session in @name. Must be called with
1635 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1637 GstPad *result, *srcghost;
1640 GstRtpBinSession *session;
1641 GstElementClass *klass;
1643 /* first get the session number */
1644 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1647 /* get or create session */
1648 session = find_session_by_id (rtpbin, sessid);
1650 /* create session now */
1651 session = create_session (rtpbin, sessid);
1652 if (session == NULL)
1656 /* check if pad was requested */
1657 if (session->send_rtp_sink != NULL)
1660 /* get send_rtp pad and store */
1661 session->send_rtp_sink =
1662 gst_element_get_request_pad (session->session, "send_rtp_sink");
1663 if (session->send_rtp_sink == NULL)
1667 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1668 gst_pad_set_active (result, TRUE);
1669 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1672 session->send_rtp_src =
1673 gst_element_get_static_pad (session->session, "send_rtp_src");
1674 if (session->send_rtp_src == NULL)
1677 /* ghost the new source pad */
1678 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1679 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1680 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1682 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1683 gst_pad_set_active (srcghost, TRUE);
1684 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1692 g_warning ("gstrtpbin: invalid name given");
1697 /* create_session already warned */
1702 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1708 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1713 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1719 /* Create a pad for sending RTCP for the session in @name. Must be called with
1723 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1727 GstRtpBinSession *session;
1729 /* first get the session number */
1730 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1733 /* get or create session */
1734 session = find_session_by_id (rtpbin, sessid);
1738 /* check if pad was requested */
1739 if (session->send_rtcp_src != NULL)
1742 /* get rtcp_src pad and store */
1743 session->send_rtcp_src =
1744 gst_element_get_request_pad (session->session, "send_rtcp_src");
1745 if (session->send_rtcp_src == NULL)
1749 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1750 gst_pad_set_active (result, TRUE);
1751 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1758 g_warning ("gstrtpbin: invalid name given");
1763 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1768 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1774 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1782 gst_rtp_bin_request_new_pad (GstElement * element,
1783 GstPadTemplate * templ, const gchar * name)
1786 GstElementClass *klass;
1789 g_return_val_if_fail (templ != NULL, NULL);
1790 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1792 rtpbin = GST_RTP_BIN (element);
1793 klass = GST_ELEMENT_GET_CLASS (element);
1795 GST_RTP_BIN_LOCK (rtpbin);
1797 /* figure out the template */
1798 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1799 result = create_recv_rtp (rtpbin, templ, name);
1800 } else if (templ == gst_element_class_get_pad_template (klass,
1801 "recv_rtcp_sink_%d")) {
1802 result = create_recv_rtcp (rtpbin, templ, name);
1803 } else if (templ == gst_element_class_get_pad_template (klass,
1804 "send_rtp_sink_%d")) {
1805 result = create_send_rtp (rtpbin, templ, name);
1806 } else if (templ == gst_element_class_get_pad_template (klass,
1807 "send_rtcp_src_%d")) {
1808 result = create_rtcp (rtpbin, templ, name);
1810 goto wrong_template;
1812 GST_RTP_BIN_UNLOCK (rtpbin);
1819 GST_RTP_BIN_UNLOCK (rtpbin);
1820 g_warning ("gstrtpbin: this is not our template");
1826 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)