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
213 /* signals and args */
216 SIGNAL_REQUEST_PT_MAP,
220 SIGNAL_ON_SSRC_COLLISION,
221 SIGNAL_ON_SSRC_VALIDATED,
223 SIGNAL_ON_BYE_TIMEOUT,
228 #define DEFAULT_LATENCY_MS 200
237 typedef struct _GstRtpBinSession GstRtpBinSession;
238 typedef struct _GstRtpBinStream GstRtpBinStream;
239 typedef struct _GstRtpBinClient GstRtpBinClient;
241 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
243 static GstCaps *pt_map_requested (GstElement * element, guint pt,
244 GstRtpBinSession * session);
246 /* Manages the RTP stream for one SSRC.
248 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
249 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
250 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
251 * together (see below).
253 struct _GstRtpBinStream
255 /* the SSRC of this stream */
261 /* the session this SSRC belongs to */
262 GstRtpBinSession *session;
264 /* the jitterbuffer of the SSRC */
267 /* the PT demuxer of the SSRC */
269 gulong demux_newpad_sig;
270 gulong demux_ptreq_sig;
272 /* the internal pad we use to get RTCP sync messages */
276 guint64 last_extrtptime;
278 /* mapping to local RTP and NTP time */
287 gint64 prev_ts_offset;
290 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
291 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
293 /* Manages the receiving end of the packets.
295 * There is one such structure for each RTP session (audio/video/...).
296 * We get the RTP/RTCP packets and stuff them into the session manager. From
297 * there they are pushed into an SSRC demuxer that splits the stream based on
298 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
299 * the GstRtpBinStream above).
301 struct _GstRtpBinSession
307 /* the session element */
309 /* the SSRC demuxer */
311 gulong demux_newpad_sig;
315 /* list of GstRtpBinStream */
318 /* mapping of payload type to caps */
321 /* the pads of the session */
322 GstPad *recv_rtp_sink;
323 GstPad *recv_rtp_src;
324 GstPad *recv_rtcp_sink;
326 GstPad *send_rtp_sink;
327 GstPad *send_rtp_src;
328 GstPad *send_rtcp_src;
331 /* Manages the RTP streams that come from one client and should therefore be
334 struct _GstRtpBinClient
336 /* the common CNAME for the streams */
347 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
348 static GstRtpBinSession *
349 find_session_by_id (GstRtpBin * rtpbin, gint id)
353 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
354 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
363 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
365 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
370 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
372 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
377 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
379 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
384 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
386 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
391 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
393 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
398 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
400 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
404 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
405 static GstRtpBinSession *
406 create_session (GstRtpBin * rtpbin, gint id)
408 GstRtpBinSession *sess;
409 GstElement *session, *demux;
411 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
414 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
417 sess = g_new0 (GstRtpBinSession, 1);
418 sess->lock = g_mutex_new ();
421 sess->session = session;
423 sess->ptmap = g_hash_table_new (NULL, NULL);
424 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
426 /* provide clock_rate to the session manager when needed */
427 g_signal_connect (session, "request-pt-map",
428 (GCallback) pt_map_requested, sess);
430 g_signal_connect (sess->session, "on-new-ssrc",
431 (GCallback) on_new_ssrc, sess);
432 g_signal_connect (sess->session, "on-ssrc-collision",
433 (GCallback) on_ssrc_collision, sess);
434 g_signal_connect (sess->session, "on-ssrc-validated",
435 (GCallback) on_ssrc_validated, sess);
436 g_signal_connect (sess->session, "on-bye-ssrc",
437 (GCallback) on_bye_ssrc, sess);
438 g_signal_connect (sess->session, "on-bye-timeout",
439 (GCallback) on_bye_timeout, sess);
440 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
442 gst_bin_add (GST_BIN_CAST (rtpbin), session);
443 gst_element_set_state (session, GST_STATE_PLAYING);
444 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
445 gst_element_set_state (demux, GST_STATE_PLAYING);
452 g_warning ("gstrtpbin: could not create gstrtpsession element");
457 gst_object_unref (session);
458 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
464 static GstRtpBinStream *
465 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
469 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
470 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
472 if (stream->ssrc == ssrc)
479 /* get the payload type caps for the specific payload @pt in @session */
481 get_pt_map (GstRtpBinSession * session, guint pt)
483 GstCaps *caps = NULL;
486 GValue args[3] = { {0}, {0}, {0} };
488 GST_DEBUG ("searching pt %d in cache", pt);
490 GST_RTP_SESSION_LOCK (session);
492 /* first look in the cache */
493 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
499 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
501 /* not in cache, send signal to request caps */
502 g_value_init (&args[0], GST_TYPE_ELEMENT);
503 g_value_set_object (&args[0], bin);
504 g_value_init (&args[1], G_TYPE_UINT);
505 g_value_set_uint (&args[1], session->id);
506 g_value_init (&args[2], G_TYPE_UINT);
507 g_value_set_uint (&args[2], pt);
509 g_value_init (&ret, GST_TYPE_CAPS);
510 g_value_set_boxed (&ret, NULL);
512 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
514 caps = (GstCaps *) g_value_get_boxed (&ret);
518 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
521 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
524 GST_RTP_SESSION_UNLOCK (session);
531 GST_RTP_SESSION_UNLOCK (session);
532 GST_DEBUG ("no pt map could be obtained");
538 return_true (gpointer key, gpointer value, gpointer user_data)
544 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
548 GST_RTP_BIN_LOCK (bin);
549 GST_DEBUG_OBJECT (bin, "clearing pt map");
550 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
551 GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
553 GST_RTP_SESSION_LOCK (session);
555 /* This requires GLib 2.12 */
556 g_hash_table_remove_all (session->ptmap);
558 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
560 GST_RTP_SESSION_UNLOCK (session);
562 GST_RTP_BIN_UNLOCK (bin);
565 static GstRtpBinClient *
566 gst_rtp_bin_get_client (GstRtpBin * bin, guint8 len, guint8 * data,
569 GstRtpBinClient *result = NULL;
572 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
573 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
575 if (len != client->cname_len)
578 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
579 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
586 /* nothing found, create one */
587 if (result == NULL) {
588 result = g_new0 (GstRtpBinClient, 1);
589 result->cname = g_strndup ((gchar *) data, len);
590 result->cname_len = len;
591 result->min_delta = G_MAXINT64;
592 bin->clients = g_slist_prepend (bin->clients, result);
593 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
599 /* associate a stream to the given CNAME. This will make sure all streams for
600 * that CNAME are synchronized together. */
602 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
605 GstRtpBinClient *client;
609 /* first find or create the CNAME */
610 client = gst_rtp_bin_get_client (bin, len, data, &created);
612 /* find stream in the client */
613 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
614 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
616 if (ostream == stream)
619 /* not found, add it to the list */
621 GST_DEBUG_OBJECT (bin,
622 "new association of SSRC %08x with client %p with CNAME %s",
623 stream->ssrc, client, client->cname);
624 client->streams = g_slist_prepend (client->streams, stream);
627 GST_DEBUG_OBJECT (bin,
628 "found association of SSRC %08x with client %p with CNAME %s",
629 stream->ssrc, client, client->cname);
632 /* we can only continue if we know the local clock-base and clock-rate */
633 if (stream->clock_base == -1)
635 if (stream->clock_rate <= 0)
638 /* map last RTP time to local timeline using our clock-base */
639 stream->local_rtp = stream->last_extrtptime - stream->clock_base;
641 GST_DEBUG_OBJECT (bin,
642 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
643 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
644 stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
646 /* calculate local NTP time in gstreamer timestamp */
648 gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
650 /* calculate delta between server and receiver */
651 stream->unix_delta = stream->last_unix - stream->local_unix;
653 GST_DEBUG_OBJECT (bin,
654 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
655 ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
658 /* recalc inter stream playout offset, but only if there are more than one
660 if (client->nstreams > 1) {
663 /* calculate the min of all deltas */
665 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
666 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
668 if (ostream->unix_delta < min)
669 min = ostream->unix_delta;
672 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
675 /* calculate offsets for each stream */
676 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
677 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
679 ostream->ts_offset = ostream->unix_delta - min;
681 /* delta changed, see how much */
682 if (ostream->prev_ts_offset != ostream->ts_offset) {
685 if (ostream->prev_ts_offset > ostream->ts_offset)
686 diff = ostream->prev_ts_offset - ostream->ts_offset;
688 diff = ostream->ts_offset - ostream->prev_ts_offset;
690 /* only change diff when it changed more than 1 millisecond. This
691 * compensates for rounding errors in NTP to RTP timestamp
693 if (diff > GST_MSECOND)
694 g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
696 ostream->prev_ts_offset = ostream->ts_offset;
698 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
699 ostream->ssrc, ostream->ts_offset);
706 GST_WARNING_OBJECT (bin, "we have no clock-base");
711 GST_WARNING_OBJECT (bin, "we have no clock-rate");
716 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
717 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
718 (b) = gst_rtcp_packet_move_to_next ((packet)))
720 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
721 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
722 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
724 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
725 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
726 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
729 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
731 GstFlowReturn ret = GST_FLOW_OK;
732 GstRtpBinStream *stream;
734 GstRTCPPacket packet;
738 gboolean have_sr, have_sdes;
741 stream = gst_pad_get_element_private (pad);
744 GST_DEBUG_OBJECT (bin, "received sync packet");
746 if (!gst_rtcp_buffer_validate (buffer))
751 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
752 /* first packet must be SR or RR or else the validate would have failed */
753 switch (gst_rtcp_packet_get_type (&packet)) {
754 case GST_RTCP_TYPE_SR:
755 /* only parse first. There is only supposed to be one SR in the packet
756 * but we will deal with malformed packets gracefully */
759 /* get NTP and RTP times */
760 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
763 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
764 /* ignore SR that is not ours */
765 if (ssrc != stream->ssrc)
770 /* store values in the stream */
771 stream->have_sync = TRUE;
772 stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
773 /* use extended timestamp */
774 gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
776 case GST_RTCP_TYPE_SDES:
778 gboolean more_items, more_entries;
780 /* only deal with first SDES, there is only supposed to be one SDES in
781 * the RTCP packet but we deal with bad packets gracefully. Also bail
782 * out if we have not seen an SR item yet. */
783 if (have_sdes || !have_sr)
786 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
787 /* skip items that are not about the SSRC of the sender */
788 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
791 /* find the CNAME entry */
792 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
793 GstRTCPSDESType type;
797 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
799 if (type == GST_RTCP_SDES_CNAME) {
800 stream->clock_base = GST_BUFFER_OFFSET (buffer);
801 /* associate the stream to CNAME */
802 gst_rtp_bin_associate (bin, stream, len, data);
810 /* we can ignore these packets */
815 gst_buffer_unref (buffer);
822 /* this is fatal and should be filtered earlier */
823 GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
824 ("invalid RTCP packet received"));
825 gst_buffer_unref (buffer);
826 return GST_FLOW_ERROR;
830 /* create a new stream with @ssrc in @session. Must be called with
831 * RTP_SESSION_LOCK. */
832 static GstRtpBinStream *
833 create_stream (GstRtpBinSession * session, guint32 ssrc)
835 GstElement *buffer, *demux;
836 GstRtpBinStream *stream;
837 GstPadTemplate *templ;
840 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
841 goto no_jitterbuffer;
843 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
846 stream = g_new0 (GstRtpBinStream, 1);
848 stream->bin = session->bin;
849 stream->session = session;
850 stream->buffer = buffer;
851 stream->demux = demux;
852 stream->last_extrtptime = -1;
853 stream->have_sync = FALSE;
854 session->streams = g_slist_prepend (session->streams, stream);
856 /* make an internal sinkpad for RTCP sync packets. Take ownership of the
857 * pad. We will link this pad later. */
858 padname = g_strdup_printf ("sync_%d", ssrc);
859 templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
860 stream->sync_pad = gst_pad_new_from_template (templ, padname);
861 gst_object_unref (templ);
862 gst_object_ref (stream->sync_pad);
863 gst_object_sink (stream->sync_pad);
864 gst_pad_set_element_private (stream->sync_pad, stream);
865 gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
866 gst_pad_set_active (stream->sync_pad, TRUE);
868 /* provide clock_rate to the jitterbuffer when needed */
869 g_signal_connect (buffer, "request-pt-map",
870 (GCallback) pt_map_requested, session);
872 /* configure latency */
873 g_object_set (buffer, "latency", session->bin->latency, NULL);
875 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
876 gst_element_set_state (buffer, GST_STATE_PLAYING);
877 gst_bin_add (GST_BIN_CAST (session->bin), demux);
878 gst_element_set_state (demux, GST_STATE_PLAYING);
881 gst_element_link (buffer, demux);
888 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
893 gst_object_unref (buffer);
894 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
899 /* GObject vmethods */
900 static void gst_rtp_bin_finalize (GObject * object);
901 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
902 const GValue * value, GParamSpec * pspec);
903 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
904 GValue * value, GParamSpec * pspec);
906 /* GstElement vmethods */
907 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
908 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
909 GstStateChange transition);
910 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
911 GstPadTemplate * templ, const gchar * name);
912 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
913 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
915 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
918 gst_rtp_bin_base_init (gpointer klass)
920 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
923 gst_element_class_add_pad_template (element_class,
924 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
925 gst_element_class_add_pad_template (element_class,
926 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
927 gst_element_class_add_pad_template (element_class,
928 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
931 gst_element_class_add_pad_template (element_class,
932 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
933 gst_element_class_add_pad_template (element_class,
934 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
935 gst_element_class_add_pad_template (element_class,
936 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
938 gst_element_class_set_details (element_class, &rtpbin_details);
942 gst_rtp_bin_class_init (GstRtpBinClass * klass)
944 GObjectClass *gobject_class;
945 GstElementClass *gstelement_class;
947 gobject_class = (GObjectClass *) klass;
948 gstelement_class = (GstElementClass *) klass;
950 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
952 gobject_class->finalize = gst_rtp_bin_finalize;
953 gobject_class->set_property = gst_rtp_bin_set_property;
954 gobject_class->get_property = gst_rtp_bin_get_property;
956 g_object_class_install_property (gobject_class, PROP_LATENCY,
957 g_param_spec_uint ("latency", "Buffer latency in ms",
958 "Default amount of ms to buffer in the jitterbuffers", 0,
959 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
962 * GstRtpBin::request-pt-map:
963 * @rtpbin: the object which received the signal
964 * @session: the session
967 * Request the payload type as #GstCaps for @pt in @session.
969 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
970 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
971 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
972 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
973 G_TYPE_UINT, G_TYPE_UINT);
975 * GstRtpBin::clear-pt-map:
976 * @rtpbin: the object which received the signal
978 * Clear all previously cached pt-mapping obtained with
979 * GstRtpBin::request-pt-map.
981 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
982 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
983 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass, clear_pt_map),
984 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
987 * GstRtpBin::on-new-ssrc:
988 * @rtpbin: the object which received the signal
989 * @session: the session
992 * Notify of a new SSRC that entered @session.
994 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
995 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
996 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
997 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
998 G_TYPE_UINT, G_TYPE_UINT);
1000 * GstRtpBin::on-ssrc_collision:
1001 * @rtpbin: the object which received the signal
1002 * @session: the session
1005 * Notify when we have an SSRC collision
1007 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1008 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1009 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1010 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1011 G_TYPE_UINT, G_TYPE_UINT);
1013 * GstRtpBin::on-ssrc_validated:
1014 * @rtpbin: the object which received the signal
1015 * @session: the session
1018 * Notify of a new SSRC that became validated.
1020 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1021 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1022 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1023 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1024 G_TYPE_UINT, G_TYPE_UINT);
1027 * GstRtpBin::on-bye-ssrc:
1028 * @rtpbin: the object which received the signal
1029 * @session: the session
1032 * Notify of an SSRC that became inactive because of a BYE packet.
1034 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1035 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1036 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1037 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1038 G_TYPE_UINT, G_TYPE_UINT);
1040 * GstRtpBin::on-bye-timeout:
1041 * @rtpbin: the object which received the signal
1042 * @session: the session
1045 * Notify of an SSRC that has timed out because of BYE
1047 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1048 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1049 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1050 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1051 G_TYPE_UINT, G_TYPE_UINT);
1053 * GstRtpBin::on-timeout:
1054 * @rtpbin: the object which received the signal
1055 * @session: the session
1058 * Notify of an SSRC that has timed out
1060 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1061 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1062 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1063 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1064 G_TYPE_UINT, G_TYPE_UINT);
1066 gstelement_class->provide_clock =
1067 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1068 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1069 gstelement_class->request_new_pad =
1070 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1071 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1073 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1075 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1079 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1081 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1082 rtpbin->priv->bin_lock = g_mutex_new ();
1083 rtpbin->provided_clock = gst_system_clock_obtain ();
1084 rtpbin->latency = DEFAULT_LATENCY_MS;
1088 gst_rtp_bin_finalize (GObject * object)
1092 rtpbin = GST_RTP_BIN (object);
1094 g_mutex_free (rtpbin->priv->bin_lock);
1096 G_OBJECT_CLASS (parent_class)->finalize (object);
1100 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1101 const GValue * value, GParamSpec * pspec)
1105 rtpbin = GST_RTP_BIN (object);
1109 rtpbin->latency = g_value_get_uint (value);
1112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1118 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1119 GValue * value, GParamSpec * pspec)
1123 rtpbin = GST_RTP_BIN (object);
1127 g_value_set_uint (value, rtpbin->latency);
1130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1136 gst_rtp_bin_provide_clock (GstElement * element)
1140 rtpbin = GST_RTP_BIN (element);
1142 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1145 static GstStateChangeReturn
1146 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1148 GstStateChangeReturn res;
1151 rtpbin = GST_RTP_BIN (element);
1153 switch (transition) {
1154 case GST_STATE_CHANGE_NULL_TO_READY:
1156 case GST_STATE_CHANGE_READY_TO_PAUSED:
1158 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1164 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1166 switch (transition) {
1167 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1169 case GST_STATE_CHANGE_PAUSED_TO_READY:
1171 case GST_STATE_CHANGE_READY_TO_NULL:
1179 /* a new pad (SSRC) was created in @session */
1181 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1182 GstRtpBinStream * stream)
1185 GstElementClass *klass;
1186 GstPadTemplate *templ;
1190 rtpbin = stream->bin;
1192 GST_DEBUG ("new payload pad %d", pt);
1194 /* ghost the pad to the parent */
1195 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1196 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1197 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1198 stream->session->id, stream->ssrc, pt);
1199 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1202 gst_pad_set_active (gpad, TRUE);
1203 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1207 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1212 rtpbin = session->bin;
1214 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1217 caps = get_pt_map (session, pt);
1226 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1231 /* emited when caps changed for the session */
1233 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1238 const GstStructure *s;
1242 g_object_get (pad, "caps", &caps, NULL);
1247 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1249 s = gst_caps_get_structure (caps, 0);
1251 /* get payload, finish when it's not there */
1252 if (!gst_structure_get_int (s, "payload", &payload))
1255 GST_RTP_SESSION_LOCK (session);
1256 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1257 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1258 GST_RTP_SESSION_UNLOCK (session);
1261 /* a new pad (SSRC) was created in @session */
1263 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1264 GstRtpBinSession * session)
1266 GstRtpBinStream *stream;
1267 GstPad *sinkpad, *srcpad;
1271 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1273 GST_RTP_SESSION_LOCK (session);
1275 /* create new stream */
1276 stream = create_stream (session, ssrc);
1280 /* get the caps of the pad, we need the clock-rate and base_time if any. */
1281 if ((caps = gst_pad_get_caps (pad))) {
1282 const GstStructure *s;
1285 GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1287 s = gst_caps_get_structure (caps, 0);
1289 if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1290 stream->clock_rate = -1;
1292 if (gst_structure_get_uint (s, "clock-base", &val))
1293 stream->clock_base = val;
1295 stream->clock_base = -1;
1298 /* get pad and link */
1299 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1300 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1301 gst_pad_link (pad, sinkpad);
1302 gst_object_unref (sinkpad);
1304 /* get the RTCP sync pad */
1305 GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1306 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1307 srcpad = gst_element_get_pad (element, padname);
1309 gst_pad_link (srcpad, stream->sync_pad);
1310 gst_object_unref (srcpad);
1312 /* connect to the new-pad signal of the payload demuxer, this will expose the
1313 * new pad by ghosting it. */
1314 stream->demux_newpad_sig = g_signal_connect (stream->demux,
1315 "new-payload-type", (GCallback) new_payload_found, stream);
1316 /* connect to the request-pt-map signal. This signal will be emited by the
1317 * demuxer so that it can apply a proper caps on the buffers for the
1319 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1320 "request-pt-map", (GCallback) pt_map_requested, session);
1322 GST_RTP_SESSION_UNLOCK (session);
1329 GST_RTP_SESSION_UNLOCK (session);
1330 GST_DEBUG ("could not create stream");
1335 /* Create a pad for receiving RTP for the session in @name. Must be called with
1339 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1341 GstPad *result, *sinkdpad;
1343 GstRtpBinSession *session;
1344 GstPadLinkReturn lres;
1346 /* first get the session number */
1347 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1350 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1352 /* get or create session */
1353 session = find_session_by_id (rtpbin, sessid);
1355 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1356 /* create session now */
1357 session = create_session (rtpbin, sessid);
1358 if (session == NULL)
1362 /* check if pad was requested */
1363 if (session->recv_rtp_sink != NULL)
1366 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1367 /* get recv_rtp pad and store */
1368 session->recv_rtp_sink =
1369 gst_element_get_request_pad (session->session, "recv_rtp_sink");
1370 if (session->recv_rtp_sink == NULL)
1373 g_signal_connect (session->recv_rtp_sink, "notify::caps",
1374 (GCallback) caps_changed, session);
1376 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1377 /* get srcpad, link to SSRCDemux */
1378 session->recv_rtp_src =
1379 gst_element_get_static_pad (session->session, "recv_rtp_src");
1380 if (session->recv_rtp_src == NULL)
1383 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1384 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1385 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1386 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1387 gst_object_unref (sinkdpad);
1388 if (lres != GST_PAD_LINK_OK)
1391 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1392 session->demux_newpad_sig = g_signal_connect (session->demux,
1393 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1395 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1397 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1398 gst_pad_set_active (result, TRUE);
1399 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1406 g_warning ("gstrtpbin: invalid name given");
1411 /* create_session already warned */
1416 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1422 g_warning ("gstrtpbin: failed to get session pad");
1427 g_warning ("gstrtpbin: failed to link pads");
1432 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1436 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1441 GstRtpBinSession *session;
1443 GstPadLinkReturn lres;
1445 /* first get the session number */
1446 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1449 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1451 /* get or create the session */
1452 session = find_session_by_id (rtpbin, sessid);
1454 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1455 /* create session now */
1456 session = create_session (rtpbin, sessid);
1457 if (session == NULL)
1461 /* check if pad was requested */
1462 if (session->recv_rtcp_sink != NULL)
1465 /* get recv_rtp pad and store */
1466 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1467 session->recv_rtcp_sink =
1468 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1469 if (session->recv_rtcp_sink == NULL)
1472 /* get srcpad, link to SSRCDemux */
1473 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1474 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1475 if (session->sync_src == NULL)
1478 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1479 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1480 lres = gst_pad_link (session->sync_src, sinkdpad);
1481 gst_object_unref (sinkdpad);
1482 if (lres != GST_PAD_LINK_OK)
1486 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1487 gst_pad_set_active (result, TRUE);
1488 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1495 g_warning ("gstrtpbin: invalid name given");
1500 /* create_session already warned */
1505 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1511 g_warning ("gstrtpbin: failed to get session pad");
1516 g_warning ("gstrtpbin: failed to link pads");
1521 /* Create a pad for sending RTP for the session in @name. Must be called with
1525 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1527 GstPad *result, *srcghost;
1530 GstRtpBinSession *session;
1531 GstElementClass *klass;
1533 /* first get the session number */
1534 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1537 /* get or create session */
1538 session = find_session_by_id (rtpbin, sessid);
1540 /* create session now */
1541 session = create_session (rtpbin, sessid);
1542 if (session == NULL)
1546 /* check if pad was requested */
1547 if (session->send_rtp_sink != NULL)
1550 /* get send_rtp pad and store */
1551 session->send_rtp_sink =
1552 gst_element_get_request_pad (session->session, "send_rtp_sink");
1553 if (session->send_rtp_sink == NULL)
1556 g_signal_connect (session->send_rtp_sink, "notify::caps",
1557 (GCallback) caps_changed, session);
1560 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1561 gst_pad_set_active (result, TRUE);
1562 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1565 session->send_rtp_src =
1566 gst_element_get_static_pad (session->session, "send_rtp_src");
1567 if (session->send_rtp_src == NULL)
1570 /* ghost the new source pad */
1571 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1572 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1573 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1575 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1576 gst_pad_set_active (srcghost, TRUE);
1577 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1585 g_warning ("gstrtpbin: invalid name given");
1590 /* create_session already warned */
1595 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1601 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1606 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1612 /* Create a pad for sending RTCP for the session in @name. Must be called with
1616 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1620 GstRtpBinSession *session;
1622 /* first get the session number */
1623 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1626 /* get or create session */
1627 session = find_session_by_id (rtpbin, sessid);
1631 /* check if pad was requested */
1632 if (session->send_rtcp_src != NULL)
1635 /* get rtcp_src pad and store */
1636 session->send_rtcp_src =
1637 gst_element_get_request_pad (session->session, "send_rtcp_src");
1638 if (session->send_rtcp_src == NULL)
1642 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1643 gst_pad_set_active (result, TRUE);
1644 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1651 g_warning ("gstrtpbin: invalid name given");
1656 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1661 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1667 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1675 gst_rtp_bin_request_new_pad (GstElement * element,
1676 GstPadTemplate * templ, const gchar * name)
1679 GstElementClass *klass;
1682 g_return_val_if_fail (templ != NULL, NULL);
1683 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1685 rtpbin = GST_RTP_BIN (element);
1686 klass = GST_ELEMENT_GET_CLASS (element);
1688 GST_RTP_BIN_LOCK (rtpbin);
1690 /* figure out the template */
1691 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1692 result = create_recv_rtp (rtpbin, templ, name);
1693 } else if (templ == gst_element_class_get_pad_template (klass,
1694 "recv_rtcp_sink_%d")) {
1695 result = create_recv_rtcp (rtpbin, templ, name);
1696 } else if (templ == gst_element_class_get_pad_template (klass,
1697 "send_rtp_sink_%d")) {
1698 result = create_send_rtp (rtpbin, templ, name);
1699 } else if (templ == gst_element_class_get_pad_template (klass,
1700 "send_rtcp_src_%d")) {
1701 result = create_rtcp (rtpbin, templ, name);
1703 goto wrong_template;
1705 GST_RTP_BIN_UNLOCK (rtpbin);
1712 GST_RTP_BIN_UNLOCK (rtpbin);
1713 g_warning ("gstrtpbin: this is not our template");
1719 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)