2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
24 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26 * RTP sessions that will be synchronized together using RTCP SR packets.
28 * #GstRtpBin is configured with a number of request pads that define the
29 * functionality that is activated, similar to the #GstRtpSession element.
31 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36 * the packets are released from the jitterbuffer, they will be forwarded to a
37 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39 * gstrtpbin with the session number, SSRC and payload type respectively as the pad
42 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43 * session number must be specified in the pad name.
45 * If you want the session manager to generate and send RTCP packets, request
46 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47 * on this pad contain SR/RR RTCP reports that should be sent to all participants
50 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52 * the pad from the lowest available session will be returned. The session manager will modify the
53 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54 * send_rtp_src_\%u pad after updating its internal state.
56 * The session manager needs the clock-rate of the payload types it is handling
57 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
61 * Access to the internal statistics of gstrtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
67 * <title>Example pipelines</title>
69 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
70 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
71 * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
73 * gst-launch gstrtpbin name=rtpbin \
74 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
75 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
76 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
77 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
78 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
79 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
80 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
81 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
82 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
83 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
84 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
85 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
86 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
87 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
88 * is received on port 5007. Since RTCP packets from the sender should be sent
89 * as soon as possible and do not participate in preroll, sync=false and
90 * async=false is configured on udpsink
92 * gst-launch -v gstrtpbin name=rtpbin \
93 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
94 * port=5000 ! rtpbin.recv_rtp_sink_0 \
95 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
96 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
97 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
98 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
99 * port=5002 ! rtpbin.recv_rtp_sink_1 \
100 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
101 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
103 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
104 * decode and display the video.
105 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
106 * decode and play the audio.
107 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
108 * session 1 on port 5003. These packets will be used for session management and
110 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
114 * Last reviewed on 2007-08-30 (0.10.6)
123 #include <gst/rtp/gstrtpbuffer.h>
124 #include <gst/rtp/gstrtcpbuffer.h>
126 #include "gstrtpbin-marshal.h"
127 #include "gstrtpbin.h"
128 #include "rtpsession.h"
129 #include "gstrtpsession.h"
130 #include "gstrtpjitterbuffer.h"
132 #include <gst/glib-compat-private.h>
134 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
135 #define GST_CAT_DEFAULT gst_rtp_bin_debug
138 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
139 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
142 GST_STATIC_CAPS ("application/x-rtp")
145 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
146 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
149 GST_STATIC_CAPS ("application/x-rtcp")
152 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
153 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
156 GST_STATIC_CAPS ("application/x-rtp")
160 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
161 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
164 GST_STATIC_CAPS ("application/x-rtp")
167 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
168 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
171 GST_STATIC_CAPS ("application/x-rtcp")
174 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
175 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
178 GST_STATIC_CAPS ("application/x-rtp")
181 #define GST_RTP_BIN_GET_PRIVATE(obj) \
182 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
184 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock (&(bin)->priv->bin_lock)
185 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
187 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
188 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock (&(bin)->priv->dyn_lock)
189 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->dyn_lock)
191 /* lock for shutdown */
192 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
194 if (g_atomic_int_get (&bin->priv->shutdown)) \
196 GST_RTP_BIN_DYN_LOCK (bin); \
197 if (g_atomic_int_get (&bin->priv->shutdown)) { \
198 GST_RTP_BIN_DYN_UNLOCK (bin); \
203 /* unlock for shutdown */
204 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
205 GST_RTP_BIN_DYN_UNLOCK (bin); \
207 struct _GstRtpBinPrivate
211 /* lock protecting dynamic adding/removing */
214 /* if we are shutting down or not */
219 /* UNIX (ntp) time of last SR sync used */
223 /* signals and args */
226 SIGNAL_REQUEST_PT_MAP,
227 SIGNAL_PAYLOAD_TYPE_CHANGE,
230 SIGNAL_GET_INTERNAL_SESSION,
233 SIGNAL_ON_SSRC_COLLISION,
234 SIGNAL_ON_SSRC_VALIDATED,
235 SIGNAL_ON_SSRC_ACTIVE,
238 SIGNAL_ON_BYE_TIMEOUT,
240 SIGNAL_ON_SENDER_TIMEOUT,
245 #define DEFAULT_LATENCY_MS 200
246 #define DEFAULT_SDES NULL
247 #define DEFAULT_DO_LOST FALSE
248 #define DEFAULT_IGNORE_PT FALSE
249 #define DEFAULT_NTP_SYNC FALSE
250 #define DEFAULT_AUTOREMOVE FALSE
251 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
252 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
253 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
254 #define DEFAULT_RTCP_SYNC_INTERVAL 0
265 PROP_RTCP_SYNC_INTERVAL,
268 PROP_USE_PIPELINE_CLOCK,
274 GST_RTP_BIN_RTCP_SYNC_ALWAYS,
275 GST_RTP_BIN_RTCP_SYNC_INITIAL,
276 GST_RTP_BIN_RTCP_SYNC_RTP
279 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
281 gst_rtp_bin_rtcp_sync_get_type (void)
283 static GType rtcp_sync_type = 0;
284 static const GEnumValue rtcp_sync_types[] = {
285 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
286 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
287 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
291 if (!rtcp_sync_type) {
292 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
294 return rtcp_sync_type;
298 typedef struct _GstRtpBinSession GstRtpBinSession;
299 typedef struct _GstRtpBinStream GstRtpBinStream;
300 typedef struct _GstRtpBinClient GstRtpBinClient;
302 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
304 static GstCaps *pt_map_requested (GstElement * element, guint pt,
305 GstRtpBinSession * session);
306 static void payload_type_change (GstElement * element, guint pt,
307 GstRtpBinSession * session);
308 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
309 static void free_stream (GstRtpBinStream * stream);
311 /* Manages the RTP stream for one SSRC.
313 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
314 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
315 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
316 * together (see below).
318 struct _GstRtpBinStream
320 /* the SSRC of this stream */
326 /* the session this SSRC belongs to */
327 GstRtpBinSession *session;
329 /* the jitterbuffer of the SSRC */
331 gulong buffer_handlesync_sig;
332 gulong buffer_ptreq_sig;
333 gulong buffer_ntpstop_sig;
336 /* the PT demuxer of the SSRC */
338 gulong demux_newpad_sig;
339 gulong demux_padremoved_sig;
340 gulong demux_ptreq_sig;
341 gulong demux_ptchange_sig;
343 /* if we have calculated a valid rt_delta for this stream */
345 /* mapping to local RTP and NTP time */
348 /* base rtptime in gst time */
352 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
353 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
355 /* Manages the receiving end of the packets.
357 * There is one such structure for each RTP session (audio/video/...).
358 * We get the RTP/RTCP packets and stuff them into the session manager. From
359 * there they are pushed into an SSRC demuxer that splits the stream based on
360 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
361 * the GstRtpBinStream above).
363 struct _GstRtpBinSession
369 /* the session element */
371 /* the SSRC demuxer */
373 gulong demux_newpad_sig;
374 gulong demux_padremoved_sig;
378 /* list of GstRtpBinStream */
381 /* mapping of payload type to caps */
384 /* the pads of the session */
385 GstPad *recv_rtp_sink;
386 GstPad *recv_rtp_sink_ghost;
387 GstPad *recv_rtp_src;
388 GstPad *recv_rtcp_sink;
389 GstPad *recv_rtcp_sink_ghost;
391 GstPad *send_rtp_sink;
392 GstPad *send_rtp_sink_ghost;
393 GstPad *send_rtp_src;
394 GstPad *send_rtp_src_ghost;
395 GstPad *send_rtcp_src;
396 GstPad *send_rtcp_src_ghost;
399 /* Manages the RTP streams that come from one client and should therefore be
402 struct _GstRtpBinClient
404 /* the common CNAME for the streams */
413 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
414 static GstRtpBinSession *
415 find_session_by_id (GstRtpBin * rtpbin, gint id)
419 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
420 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
428 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
429 static GstRtpBinSession *
430 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
434 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
435 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
437 if ((sess->recv_rtp_sink_ghost == pad) ||
438 (sess->recv_rtcp_sink_ghost == pad) ||
439 (sess->send_rtp_sink_ghost == pad)
440 || (sess->send_rtcp_src_ghost == pad))
447 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
449 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
454 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
456 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
461 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
463 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
468 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
470 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
475 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
477 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
482 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
484 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
489 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
491 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
494 if (sess->bin->priv->autoremove)
495 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
499 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
501 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
504 if (sess->bin->priv->autoremove)
505 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
509 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
511 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
516 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
518 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
519 stream->session->id, stream->ssrc);
522 /* must be called with the SESSION lock */
523 static GstRtpBinStream *
524 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
528 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
529 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
531 if (stream->ssrc == ssrc)
538 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
539 GstRtpBinSession * session)
541 GstRtpBinStream *stream = NULL;
543 GST_RTP_SESSION_LOCK (session);
544 if ((stream = find_stream_by_ssrc (session, ssrc)))
545 session->streams = g_slist_remove (session->streams, stream);
546 GST_RTP_SESSION_UNLOCK (session);
549 free_stream (stream);
552 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
553 static GstRtpBinSession *
554 create_session (GstRtpBin * rtpbin, gint id)
556 GstRtpBinSession *sess;
557 GstElement *session, *demux;
560 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
563 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
566 sess = g_new0 (GstRtpBinSession, 1);
567 g_mutex_init (&sess->lock);
570 sess->session = session;
572 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
573 (GDestroyNotify) gst_caps_unref);
574 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
576 /* configure SDES items */
577 GST_OBJECT_LOCK (rtpbin);
578 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
579 rtpbin->use_pipeline_clock, NULL);
580 GST_OBJECT_UNLOCK (rtpbin);
582 /* provide clock_rate to the session manager when needed */
583 g_signal_connect (session, "request-pt-map",
584 (GCallback) pt_map_requested, sess);
586 g_signal_connect (sess->session, "on-new-ssrc",
587 (GCallback) on_new_ssrc, sess);
588 g_signal_connect (sess->session, "on-ssrc-collision",
589 (GCallback) on_ssrc_collision, sess);
590 g_signal_connect (sess->session, "on-ssrc-validated",
591 (GCallback) on_ssrc_validated, sess);
592 g_signal_connect (sess->session, "on-ssrc-active",
593 (GCallback) on_ssrc_active, sess);
594 g_signal_connect (sess->session, "on-ssrc-sdes",
595 (GCallback) on_ssrc_sdes, sess);
596 g_signal_connect (sess->session, "on-bye-ssrc",
597 (GCallback) on_bye_ssrc, sess);
598 g_signal_connect (sess->session, "on-bye-timeout",
599 (GCallback) on_bye_timeout, sess);
600 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
601 g_signal_connect (sess->session, "on-sender-timeout",
602 (GCallback) on_sender_timeout, sess);
604 gst_bin_add (GST_BIN_CAST (rtpbin), session);
605 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
607 GST_OBJECT_LOCK (rtpbin);
608 target = GST_STATE_TARGET (rtpbin);
609 GST_OBJECT_UNLOCK (rtpbin);
611 /* change state only to what's needed */
612 gst_element_set_state (demux, target);
613 gst_element_set_state (session, target);
620 g_warning ("rtpbin: could not create gstrtpsession element");
625 gst_object_unref (session);
626 g_warning ("rtpbin: could not create gstrtpssrcdemux element");
632 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
636 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
638 gst_element_set_locked_state (sess->demux, TRUE);
639 gst_element_set_locked_state (sess->session, TRUE);
641 gst_element_set_state (sess->demux, GST_STATE_NULL);
642 gst_element_set_state (sess->session, GST_STATE_NULL);
644 if (sess->recv_rtp_sink != NULL) {
645 gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
646 gst_object_unref (sess->recv_rtp_sink);
648 if (sess->recv_rtp_src != NULL)
649 gst_object_unref (sess->recv_rtp_src);
650 if (sess->recv_rtcp_sink != NULL) {
651 gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
652 gst_object_unref (sess->recv_rtcp_sink);
654 if (sess->sync_src != NULL)
655 gst_object_unref (sess->sync_src);
656 if (sess->send_rtp_sink != NULL) {
657 gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
658 gst_object_unref (sess->send_rtp_sink);
660 if (sess->send_rtp_src != NULL)
661 gst_object_unref (sess->send_rtp_src);
662 if (sess->send_rtcp_src != NULL) {
663 gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
664 gst_object_unref (sess->send_rtcp_src);
667 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
668 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
670 /* remove any references in bin->clients to the streams in sess->streams */
671 client_walk = bin->clients;
672 while (client_walk) {
673 GSList *client_node = client_walk;
674 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
675 GSList *stream_walk = client->streams;
677 while (stream_walk) {
678 GSList *stream_node = stream_walk;
679 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
682 stream_walk = g_slist_next (stream_walk);
684 for (inner_walk = sess->streams; inner_walk;
685 inner_walk = g_slist_next (inner_walk)) {
686 if ((GstRtpBinStream *) inner_walk->data == stream) {
687 client->streams = g_slist_delete_link (client->streams, stream_node);
693 client_walk = g_slist_next (client_walk);
695 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
696 && client->streams == 0));
697 if (client->nstreams == 0) {
698 free_client (client, bin);
699 bin->clients = g_slist_delete_link (bin->clients, client_node);
703 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
704 g_slist_free (sess->streams);
706 g_mutex_clear (&sess->lock);
707 g_hash_table_destroy (sess->ptmap);
712 /* get the payload type caps for the specific payload @pt in @session */
714 get_pt_map (GstRtpBinSession * session, guint pt)
716 GstCaps *caps = NULL;
719 GValue args[3] = { {0}, {0}, {0} };
721 GST_DEBUG ("searching pt %d in cache", pt);
723 GST_RTP_SESSION_LOCK (session);
725 /* first look in the cache */
726 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
734 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
736 /* not in cache, send signal to request caps */
737 g_value_init (&args[0], GST_TYPE_ELEMENT);
738 g_value_set_object (&args[0], bin);
739 g_value_init (&args[1], G_TYPE_UINT);
740 g_value_set_uint (&args[1], session->id);
741 g_value_init (&args[2], G_TYPE_UINT);
742 g_value_set_uint (&args[2], pt);
744 g_value_init (&ret, GST_TYPE_CAPS);
745 g_value_set_boxed (&ret, NULL);
747 GST_RTP_SESSION_UNLOCK (session);
749 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
751 GST_RTP_SESSION_LOCK (session);
753 g_value_unset (&args[0]);
754 g_value_unset (&args[1]);
755 g_value_unset (&args[2]);
757 /* look in the cache again because we let the lock go */
758 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
761 g_value_unset (&ret);
765 caps = (GstCaps *) g_value_dup_boxed (&ret);
766 g_value_unset (&ret);
770 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
772 /* store in cache, take additional ref */
773 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
774 gst_caps_ref (caps));
777 GST_RTP_SESSION_UNLOCK (session);
784 GST_RTP_SESSION_UNLOCK (session);
785 GST_DEBUG ("no pt map could be obtained");
791 return_true (gpointer key, gpointer value, gpointer user_data)
797 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
799 GSList *clients, *streams;
801 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
803 GST_RTP_BIN_LOCK (rtpbin);
804 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
805 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
807 /* reset sync on all streams for this client */
808 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
809 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
811 /* make use require a new SR packet for this stream before we attempt new
813 stream->have_sync = FALSE;
814 stream->rt_delta = 0;
815 stream->rtp_delta = 0;
816 stream->clock_base = -100 * GST_SECOND;
819 GST_RTP_BIN_UNLOCK (rtpbin);
823 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
825 GSList *sessions, *streams;
827 GST_RTP_BIN_LOCK (bin);
828 GST_DEBUG_OBJECT (bin, "clearing pt map");
829 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
830 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
832 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
833 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
835 GST_RTP_SESSION_LOCK (session);
836 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
838 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
839 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
841 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
842 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
844 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
846 GST_RTP_SESSION_UNLOCK (session);
848 GST_RTP_BIN_UNLOCK (bin);
851 gst_rtp_bin_reset_sync (bin);
855 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
857 RTPSession *internal_session = NULL;
858 GstRtpBinSession *session;
860 GST_RTP_BIN_LOCK (bin);
861 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
863 session = find_session_by_id (bin, (gint) session_id);
865 g_object_get (session->session, "internal-session", &internal_session,
868 GST_RTP_BIN_UNLOCK (bin);
870 return internal_session;
874 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
875 const gchar * name, const GValue * value)
877 GSList *sessions, *streams;
879 GST_RTP_BIN_LOCK (bin);
880 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
881 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
883 GST_RTP_SESSION_LOCK (session);
884 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
885 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
887 g_object_set_property (G_OBJECT (stream->buffer), name, value);
889 GST_RTP_SESSION_UNLOCK (session);
891 GST_RTP_BIN_UNLOCK (bin);
894 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
895 static GstRtpBinClient *
896 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
898 GstRtpBinClient *result = NULL;
901 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
902 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
904 if (len != client->cname_len)
907 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
908 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
915 /* nothing found, create one */
916 if (result == NULL) {
917 result = g_new0 (GstRtpBinClient, 1);
918 result->cname = g_strndup ((gchar *) data, len);
919 result->cname_len = len;
920 bin->clients = g_slist_prepend (bin->clients, result);
921 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
928 free_client (GstRtpBinClient * client, GstRtpBin * bin)
930 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
931 g_slist_free (client->streams);
932 g_free (client->cname);
937 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
942 GstClockTime base_time, rt, clock_time;
944 GST_OBJECT_LOCK (bin);
945 if ((clock = GST_ELEMENT_CLOCK (bin))) {
946 base_time = GST_ELEMENT_CAST (bin)->base_time;
947 gst_object_ref (clock);
948 GST_OBJECT_UNLOCK (bin);
950 clock_time = gst_clock_get_time (clock);
952 if (bin->use_pipeline_clock) {
957 /* get current NTP time */
958 g_get_current_time (¤t);
959 ntpns = GST_TIMEVAL_TO_TIME (current);
962 /* add constant to convert from 1970 based time to 1900 based time */
963 ntpns += (2208988800LL * GST_SECOND);
965 /* get current clock time and convert to running time */
966 rt = clock_time - base_time;
968 gst_object_unref (clock);
970 GST_OBJECT_UNLOCK (bin);
981 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
984 gint64 prev_ts_offset;
986 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
988 /* delta changed, see how much */
989 if (prev_ts_offset != ts_offset) {
992 diff = prev_ts_offset - ts_offset;
994 GST_DEBUG_OBJECT (bin,
995 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
996 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
998 /* only change diff when it changed more than 4 milliseconds. This
999 * compensates for rounding errors in NTP to RTP timestamp
1001 if (ABS (diff) > 4 * GST_MSECOND) {
1002 if (ABS (diff) < (3 * GST_SECOND)) {
1003 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1005 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
1008 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1011 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1012 stream->ssrc, ts_offset);
1015 /* associate a stream to the given CNAME. This will make sure all streams for
1016 * that CNAME are synchronized together.
1017 * Must be called with GST_RTP_BIN_LOCK */
1019 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1020 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1021 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1022 gint64 rtp_clock_base)
1024 GstRtpBinClient *client;
1029 GstClockTime running_time;
1031 gint64 ntpdiff, rtdiff;
1034 /* first find or create the CNAME */
1035 client = get_client (bin, len, data, &created);
1037 /* find stream in the client */
1038 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1039 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1041 if (ostream == stream)
1044 /* not found, add it to the list */
1046 GST_DEBUG_OBJECT (bin,
1047 "new association of SSRC %08x with client %p with CNAME %s",
1048 stream->ssrc, client, client->cname);
1049 client->streams = g_slist_prepend (client->streams, stream);
1052 GST_DEBUG_OBJECT (bin,
1053 "found association of SSRC %08x with client %p with CNAME %s",
1054 stream->ssrc, client, client->cname);
1057 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1058 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1059 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1060 /* we don't need that data, so carry on,
1061 * but make some values look saner */
1062 last_extrtptime = base_rtptime;
1064 /* nothing we can do with this data in this case */
1065 GST_DEBUG_OBJECT (bin, "bailing out");
1070 /* Take the extended rtptime we found in the SR packet and map it to the
1071 * local rtptime. The local rtp time is used to construct timestamps on the
1072 * buffers so we will calculate what running_time corresponds to the RTP
1073 * timestamp in the SR packet. */
1074 local_rtp = last_extrtptime - base_rtptime;
1076 GST_DEBUG_OBJECT (bin,
1077 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1078 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1079 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1080 last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
1082 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1083 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1084 * into a corresponding gstreamer timestamp. Note that the base_time also
1085 * contains the drift between sender and receiver. */
1086 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1087 local_rt += base_time;
1089 /* convert ntptime to unix time since 1900 */
1090 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1091 (G_GINT64_CONSTANT (1) << 32));
1093 stream->have_sync = TRUE;
1095 GST_DEBUG_OBJECT (bin,
1096 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1097 local_rt, last_unix);
1099 /* recalc inter stream playout offset, but only if there is more than one
1100 * stream or we're doing NTP sync. */
1101 if (bin->ntp_sync) {
1102 /* For NTP sync we need to first get a snapshot of running_time and NTP
1103 * time. We know at what running_time we play a certain RTP time, we also
1104 * calculated when we would play the RTP time in the SR packet. Now we need
1105 * to know how the running_time and the NTP time relate to eachother. */
1106 get_current_times (bin, &running_time, &ntpnstime);
1108 /* see how far away the NTP time is. This is the difference between the
1109 * current NTP time and the NTP time in the last SR packet. */
1110 ntpdiff = ntpnstime - last_unix;
1111 /* see how far away the running_time is. This is the difference between the
1112 * current running_time and the running_time of the RTP timestamp in the
1113 * last SR packet. */
1114 rtdiff = running_time - local_rt;
1116 GST_DEBUG_OBJECT (bin,
1117 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1118 ntpnstime, last_unix);
1119 GST_DEBUG_OBJECT (bin,
1120 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1123 /* combine to get the final diff to apply to the running_time */
1124 stream->rt_delta = rtdiff - ntpdiff;
1126 stream_set_ts_offset (bin, stream, stream->rt_delta);
1128 gint64 min, rtp_min, clock_base = stream->clock_base;
1129 gboolean all_sync, use_rtp;
1130 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1132 /* calculate delta between server and receiver. last_unix is created by
1133 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1134 * delta expresses the difference to our timeline and the server timeline. The
1135 * difference in itself doesn't mean much but we can combine the delta of
1136 * multiple streams to create a stream specific offset. */
1137 stream->rt_delta = last_unix - local_rt;
1139 /* calculate the min of all deltas, ignoring streams that did not yet have a
1140 * valid rt_delta because we did not yet receive an SR packet for those
1142 * We calculate the mininum because we would like to only apply positive
1143 * offsets to streams, delaying their playback instead of trying to speed up
1144 * other streams (which might be imposible when we have to create negative
1146 * The stream that has the smallest diff is selected as the reference stream,
1147 * all other streams will have a positive offset to this difference. */
1149 /* some alternative setting allow ignoring RTCP as much as possible,
1150 * for servers generating bogus ntp timeline */
1151 min = rtp_min = G_MAXINT64;
1153 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1157 /* signed version for convienience */
1158 clock_base = base_rtptime;
1159 /* deal with possible wrap-around */
1160 ext_base = base_rtptime;
1161 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1162 /* sanity check; base rtp and provided clock_base should be close */
1163 if (rtp_clock_base >= clock_base) {
1164 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1165 rtp_clock_base = base_time +
1166 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1167 GST_SECOND, clock_rate);
1172 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1173 rtp_clock_base = base_time -
1174 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1175 GST_SECOND, clock_rate);
1180 /* warn and bail for clarity out if no sane values */
1182 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1185 /* store to track changes */
1186 clock_base = rtp_clock_base;
1187 /* generate a fake as before,
1188 * now equating rtptime obtained from RTP-Info,
1189 * where the large time represent the otherwise irrelevant npt/ntp time */
1190 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1192 clock_base = rtp_clock_base;
1196 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1197 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1199 if (!ostream->have_sync) {
1204 /* change in current stream's base from previously init'ed value
1205 * leads to reset of all stream's base */
1206 if (stream != ostream && stream->clock_base >= 0 &&
1207 (stream->clock_base != clock_base)) {
1208 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1209 ostream->clock_base = -100 * GST_SECOND;
1210 ostream->rtp_delta = 0;
1213 if (ostream->rt_delta < min)
1214 min = ostream->rt_delta;
1215 if (ostream->rtp_delta < rtp_min)
1216 rtp_min = ostream->rtp_delta;
1219 /* arrange to re-sync for each stream upon significant change,
1221 all_sync = all_sync && (stream->clock_base == clock_base);
1222 stream->clock_base = clock_base;
1224 /* may need init performed above later on, but nothing more to do now */
1225 if (client->nstreams <= 1)
1228 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1229 " all sync %d", client, min, all_sync);
1230 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1232 switch (rtcp_sync) {
1233 case GST_RTP_BIN_RTCP_SYNC_RTP:
1236 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1237 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1239 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1240 /* if all have been synced already, do not bother further */
1242 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1250 /* bail out if we adjusted recently enough */
1251 if (all_sync && (last_unix - bin->priv->last_unix) <
1252 bin->rtcp_sync_interval * GST_MSECOND) {
1253 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1254 "previous sender info too recent "
1255 "(previous UNIX %" G_GUINT64_FORMAT ")", bin->priv->last_unix);
1258 bin->priv->last_unix = last_unix;
1260 /* calculate offsets for each stream */
1261 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1262 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1265 /* ignore streams for which we didn't receive an SR packet yet, we
1266 * can't synchronize them yet. We can however sync other streams just
1268 if (!ostream->have_sync)
1271 /* calculate offset to our reference stream, this should always give a
1272 * positive number. */
1274 ts_offset = ostream->rtp_delta - rtp_min;
1276 ts_offset = ostream->rt_delta - min;
1278 stream_set_ts_offset (bin, ostream, ts_offset);
1284 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1285 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1286 (b) = gst_rtcp_packet_move_to_next ((packet)))
1288 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1289 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1290 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1292 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1293 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1294 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1297 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1298 GstRtpBinStream * stream)
1301 GstRTCPPacket packet;
1304 gboolean have_sr, have_sdes;
1306 guint64 base_rtptime;
1312 GstRTCPBuffer rtcp = { NULL, };
1316 GST_DEBUG_OBJECT (bin, "sync handler called");
1318 /* get the last relation between the rtp timestamps and the gstreamer
1319 * timestamps. We get this info directly from the jitterbuffer which
1320 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1321 * what the current situation is. */
1323 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1324 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1325 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1326 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1328 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1329 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1334 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1336 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1337 /* first packet must be SR or RR or else the validate would have failed */
1338 switch (gst_rtcp_packet_get_type (&packet)) {
1339 case GST_RTCP_TYPE_SR:
1340 /* only parse first. There is only supposed to be one SR in the packet
1341 * but we will deal with malformed packets gracefully */
1344 /* get NTP and RTP times */
1345 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1348 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1349 /* ignore SR that is not ours */
1350 if (ssrc != stream->ssrc)
1355 case GST_RTCP_TYPE_SDES:
1357 gboolean more_items, more_entries;
1359 /* only deal with first SDES, there is only supposed to be one SDES in
1360 * the RTCP packet but we deal with bad packets gracefully. Also bail
1361 * out if we have not seen an SR item yet. */
1362 if (have_sdes || !have_sr)
1365 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1366 /* skip items that are not about the SSRC of the sender */
1367 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1370 /* find the CNAME entry */
1371 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1372 GstRTCPSDESType type;
1376 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1378 if (type == GST_RTCP_SDES_CNAME) {
1379 GST_RTP_BIN_LOCK (bin);
1380 /* associate the stream to CNAME */
1381 gst_rtp_bin_associate (bin, stream, len, data,
1382 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1384 GST_RTP_BIN_UNLOCK (bin);
1392 /* we can ignore these packets */
1396 gst_rtcp_buffer_unmap (&rtcp);
1399 /* create a new stream with @ssrc in @session. Must be called with
1400 * RTP_SESSION_LOCK. */
1401 static GstRtpBinStream *
1402 create_stream (GstRtpBinSession * session, guint32 ssrc)
1404 GstElement *buffer, *demux = NULL;
1405 GstRtpBinStream *stream;
1409 rtpbin = session->bin;
1411 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1412 goto no_jitterbuffer;
1414 if (!rtpbin->ignore_pt)
1415 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1419 stream = g_new0 (GstRtpBinStream, 1);
1420 stream->ssrc = ssrc;
1421 stream->bin = rtpbin;
1422 stream->session = session;
1423 stream->buffer = buffer;
1424 stream->demux = demux;
1426 stream->have_sync = FALSE;
1427 stream->rt_delta = 0;
1428 stream->rtp_delta = 0;
1429 stream->percent = 100;
1430 stream->clock_base = -100 * GST_SECOND;
1431 session->streams = g_slist_prepend (session->streams, stream);
1433 /* provide clock_rate to the jitterbuffer when needed */
1434 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1435 (GCallback) pt_map_requested, session);
1436 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1437 (GCallback) on_npt_stop, stream);
1439 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1440 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1442 /* configure latency and packet lost */
1443 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1444 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1445 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1447 if (!rtpbin->ignore_pt)
1448 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1449 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1453 gst_element_link (buffer, demux);
1455 if (rtpbin->buffering) {
1458 GST_INFO_OBJECT (rtpbin,
1459 "bin is buffering, set jitterbuffer as not active");
1460 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1464 GST_OBJECT_LOCK (rtpbin);
1465 target = GST_STATE_TARGET (rtpbin);
1466 GST_OBJECT_UNLOCK (rtpbin);
1468 /* from sink to source */
1470 gst_element_set_state (demux, target);
1472 gst_element_set_state (buffer, target);
1479 g_warning ("rtpbin: could not create gstrtpjitterbuffer element");
1484 gst_object_unref (buffer);
1485 g_warning ("rtpbin: could not create gstrtpptdemux element");
1491 free_stream (GstRtpBinStream * stream)
1493 GstRtpBinSession *session;
1495 session = stream->session;
1497 if (stream->demux) {
1498 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1499 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1500 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1502 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1503 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1504 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1507 gst_element_set_locked_state (stream->demux, TRUE);
1508 gst_element_set_locked_state (stream->buffer, TRUE);
1511 gst_element_set_state (stream->demux, GST_STATE_NULL);
1512 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1514 /* now remove this signal, we need this while going to NULL because it to
1515 * do some cleanups */
1517 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1519 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1521 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1526 /* GObject vmethods */
1527 static void gst_rtp_bin_dispose (GObject * object);
1528 static void gst_rtp_bin_finalize (GObject * object);
1529 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1530 const GValue * value, GParamSpec * pspec);
1531 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1532 GValue * value, GParamSpec * pspec);
1534 /* GstElement vmethods */
1535 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1536 GstStateChange transition);
1537 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1538 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1539 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1540 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1542 #define gst_rtp_bin_parent_class parent_class
1543 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1546 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1548 GObjectClass *gobject_class;
1549 GstElementClass *gstelement_class;
1550 GstBinClass *gstbin_class;
1552 gobject_class = (GObjectClass *) klass;
1553 gstelement_class = (GstElementClass *) klass;
1554 gstbin_class = (GstBinClass *) klass;
1556 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1558 gobject_class->dispose = gst_rtp_bin_dispose;
1559 gobject_class->finalize = gst_rtp_bin_finalize;
1560 gobject_class->set_property = gst_rtp_bin_set_property;
1561 gobject_class->get_property = gst_rtp_bin_get_property;
1563 g_object_class_install_property (gobject_class, PROP_LATENCY,
1564 g_param_spec_uint ("latency", "Buffer latency in ms",
1565 "Default amount of ms to buffer in the jitterbuffers", 0,
1566 G_MAXUINT, DEFAULT_LATENCY_MS,
1567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1570 * GstRtpBin::request-pt-map:
1571 * @rtpbin: the object which received the signal
1572 * @session: the session
1575 * Request the payload type as #GstCaps for @pt in @session.
1577 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1578 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1579 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1580 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1581 G_TYPE_UINT, G_TYPE_UINT);
1584 * GstRtpBin::payload-type-change:
1585 * @rtpbin: the object which received the signal
1586 * @session: the session
1589 * Signal that the current payload type changed to @pt in @session.
1593 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1594 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1595 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1596 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1597 G_TYPE_UINT, G_TYPE_UINT);
1600 * GstRtpBin::clear-pt-map:
1601 * @rtpbin: the object which received the signal
1603 * Clear all previously cached pt-mapping obtained with
1604 * #GstRtpBin::request-pt-map.
1606 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1607 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1608 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1609 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1613 * GstRtpBin::reset-sync:
1614 * @rtpbin: the object which received the signal
1616 * Reset all currently configured lip-sync parameters and require new SR
1617 * packets for all streams before lip-sync is attempted again.
1619 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1620 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1621 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1622 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1626 * GstRtpBin::get-internal-session:
1627 * @rtpbin: the object which received the signal
1628 * @id: the session id
1630 * Request the internal RTPSession object as #GObject in session @id.
1632 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1633 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1634 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1635 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1636 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1639 * GstRtpBin::on-new-ssrc:
1640 * @rtpbin: the object which received the signal
1641 * @session: the session
1644 * Notify of a new SSRC that entered @session.
1646 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1647 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1648 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1649 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1650 G_TYPE_UINT, G_TYPE_UINT);
1652 * GstRtpBin::on-ssrc-collision:
1653 * @rtpbin: the object which received the signal
1654 * @session: the session
1657 * Notify when we have an SSRC collision
1659 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1660 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1661 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1662 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1663 G_TYPE_UINT, G_TYPE_UINT);
1665 * GstRtpBin::on-ssrc-validated:
1666 * @rtpbin: the object which received the signal
1667 * @session: the session
1670 * Notify of a new SSRC that became validated.
1672 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1673 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1674 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1675 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1676 G_TYPE_UINT, G_TYPE_UINT);
1678 * GstRtpBin::on-ssrc-active:
1679 * @rtpbin: the object which received the signal
1680 * @session: the session
1683 * Notify of a SSRC that is active, i.e., sending RTCP.
1685 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1686 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1687 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1688 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1689 G_TYPE_UINT, G_TYPE_UINT);
1691 * GstRtpBin::on-ssrc-sdes:
1692 * @rtpbin: the object which received the signal
1693 * @session: the session
1696 * Notify of a SSRC that is active, i.e., sending RTCP.
1698 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1699 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1700 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1701 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1702 G_TYPE_UINT, G_TYPE_UINT);
1705 * GstRtpBin::on-bye-ssrc:
1706 * @rtpbin: the object which received the signal
1707 * @session: the session
1710 * Notify of an SSRC that became inactive because of a BYE packet.
1712 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1713 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1714 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1715 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1716 G_TYPE_UINT, G_TYPE_UINT);
1718 * GstRtpBin::on-bye-timeout:
1719 * @rtpbin: the object which received the signal
1720 * @session: the session
1723 * Notify of an SSRC that has timed out because of BYE
1725 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1726 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1727 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1728 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1729 G_TYPE_UINT, G_TYPE_UINT);
1731 * GstRtpBin::on-timeout:
1732 * @rtpbin: the object which received the signal
1733 * @session: the session
1736 * Notify of an SSRC that has timed out
1738 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1739 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1740 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1741 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1742 G_TYPE_UINT, G_TYPE_UINT);
1744 * GstRtpBin::on-sender-timeout:
1745 * @rtpbin: the object which received the signal
1746 * @session: the session
1749 * Notify of a sender SSRC that has timed out and became a receiver
1751 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1752 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1753 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1754 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1755 G_TYPE_UINT, G_TYPE_UINT);
1758 * GstRtpBin::on-npt-stop:
1759 * @rtpbin: the object which received the signal
1760 * @session: the session
1763 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1765 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1766 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1767 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1768 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1769 G_TYPE_UINT, G_TYPE_UINT);
1771 g_object_class_install_property (gobject_class, PROP_SDES,
1772 g_param_spec_boxed ("sdes", "SDES",
1773 "The SDES items of this session",
1774 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1776 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1777 g_param_spec_boolean ("do-lost", "Do Lost",
1778 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1779 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1781 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1782 g_param_spec_boolean ("autoremove", "Auto Remove",
1783 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1784 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1786 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1787 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1788 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1789 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1791 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1792 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1793 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1794 DEFAULT_USE_PIPELINE_CLOCK,
1795 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1797 * GstRtpBin::buffer-mode:
1799 * Control the buffering and timestamping mode used by the jitterbuffer.
1803 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1804 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1805 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1806 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1808 * GstRtpBin::ntp-sync:
1810 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1811 * between the receivers and the senders (such as when using ntpd) this option
1812 * can be used to synchronize receivers on multiple machines.
1816 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1817 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1818 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1819 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1822 * GstRtpBin::rtcp-sync:
1824 * If not synchronizing (directly) to the NTP clock, determines how to sync
1825 * the various streams.
1829 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
1830 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
1831 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
1832 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1835 * GstRtpBin::rtcp-sync-interval:
1837 * Determines how often to sync streams using RTCP data.
1841 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
1842 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
1843 "RTCP SR interval synchronization (ms) (0 = always)",
1844 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
1845 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1847 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1848 gstelement_class->request_new_pad =
1849 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1850 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1853 gst_element_class_add_pad_template (gstelement_class,
1854 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1855 gst_element_class_add_pad_template (gstelement_class,
1856 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1857 gst_element_class_add_pad_template (gstelement_class,
1858 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1861 gst_element_class_add_pad_template (gstelement_class,
1862 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1863 gst_element_class_add_pad_template (gstelement_class,
1864 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1865 gst_element_class_add_pad_template (gstelement_class,
1866 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1868 gst_element_class_set_details_simple (gstelement_class, "RTP Bin",
1869 "Filter/Network/RTP",
1870 "Real-Time Transport Protocol bin",
1871 "Wim Taymans <wim.taymans@gmail.com>");
1873 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1875 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1876 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1877 klass->get_internal_session =
1878 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1880 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1884 gst_rtp_bin_init (GstRtpBin * rtpbin)
1888 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1889 g_mutex_init (&rtpbin->priv->bin_lock);
1890 g_mutex_init (&rtpbin->priv->dyn_lock);
1892 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1893 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1894 rtpbin->do_lost = DEFAULT_DO_LOST;
1895 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1896 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1897 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
1898 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
1899 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1900 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1901 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1903 /* some default SDES entries */
1904 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
1905 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1906 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
1911 gst_rtp_bin_dispose (GObject * object)
1915 rtpbin = GST_RTP_BIN (object);
1917 GST_DEBUG_OBJECT (object, "freeing sessions");
1918 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1919 g_slist_free (rtpbin->sessions);
1920 rtpbin->sessions = NULL;
1921 GST_DEBUG_OBJECT (object, "freeing clients");
1922 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1923 g_slist_free (rtpbin->clients);
1924 rtpbin->clients = NULL;
1926 G_OBJECT_CLASS (parent_class)->dispose (object);
1930 gst_rtp_bin_finalize (GObject * object)
1934 rtpbin = GST_RTP_BIN (object);
1937 gst_structure_free (rtpbin->sdes);
1939 g_mutex_clear (&rtpbin->priv->bin_lock);
1940 g_mutex_clear (&rtpbin->priv->dyn_lock);
1942 G_OBJECT_CLASS (parent_class)->finalize (object);
1947 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1954 GST_RTP_BIN_LOCK (bin);
1956 GST_OBJECT_LOCK (bin);
1958 gst_structure_free (bin->sdes);
1959 bin->sdes = gst_structure_copy (sdes);
1960 GST_OBJECT_UNLOCK (bin);
1962 /* store in all sessions */
1963 for (item = bin->sessions; item; item = g_slist_next (item)) {
1964 GstRtpBinSession *session = item->data;
1965 g_object_set (session->session, "sdes", sdes, NULL);
1968 GST_RTP_BIN_UNLOCK (bin);
1971 static GstStructure *
1972 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1974 GstStructure *result;
1976 GST_OBJECT_LOCK (bin);
1977 result = gst_structure_copy (bin->sdes);
1978 GST_OBJECT_UNLOCK (bin);
1984 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1985 const GValue * value, GParamSpec * pspec)
1989 rtpbin = GST_RTP_BIN (object);
1993 GST_RTP_BIN_LOCK (rtpbin);
1994 rtpbin->latency_ms = g_value_get_uint (value);
1995 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1996 GST_RTP_BIN_UNLOCK (rtpbin);
1997 /* propagate the property down to the jitterbuffer */
1998 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2001 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2004 GST_RTP_BIN_LOCK (rtpbin);
2005 rtpbin->do_lost = g_value_get_boolean (value);
2006 GST_RTP_BIN_UNLOCK (rtpbin);
2007 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2010 rtpbin->ntp_sync = g_value_get_boolean (value);
2012 case PROP_RTCP_SYNC:
2013 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2015 case PROP_RTCP_SYNC_INTERVAL:
2016 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2018 case PROP_IGNORE_PT:
2019 rtpbin->ignore_pt = g_value_get_boolean (value);
2021 case PROP_AUTOREMOVE:
2022 rtpbin->priv->autoremove = g_value_get_boolean (value);
2024 case PROP_USE_PIPELINE_CLOCK:
2027 GST_RTP_BIN_LOCK (rtpbin);
2028 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2029 for (sessions = rtpbin->sessions; sessions;
2030 sessions = g_slist_next (sessions)) {
2031 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2033 g_object_set (G_OBJECT (session->session),
2034 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2036 GST_RTP_BIN_UNLOCK (rtpbin);
2039 case PROP_BUFFER_MODE:
2040 GST_RTP_BIN_LOCK (rtpbin);
2041 rtpbin->buffer_mode = g_value_get_enum (value);
2042 GST_RTP_BIN_UNLOCK (rtpbin);
2043 /* propagate the property down to the jitterbuffer */
2044 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2047 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2053 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2054 GValue * value, GParamSpec * pspec)
2058 rtpbin = GST_RTP_BIN (object);
2062 GST_RTP_BIN_LOCK (rtpbin);
2063 g_value_set_uint (value, rtpbin->latency_ms);
2064 GST_RTP_BIN_UNLOCK (rtpbin);
2067 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2070 GST_RTP_BIN_LOCK (rtpbin);
2071 g_value_set_boolean (value, rtpbin->do_lost);
2072 GST_RTP_BIN_UNLOCK (rtpbin);
2074 case PROP_IGNORE_PT:
2075 g_value_set_boolean (value, rtpbin->ignore_pt);
2078 g_value_set_boolean (value, rtpbin->ntp_sync);
2080 case PROP_RTCP_SYNC:
2081 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2083 case PROP_RTCP_SYNC_INTERVAL:
2084 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2086 case PROP_AUTOREMOVE:
2087 g_value_set_boolean (value, rtpbin->priv->autoremove);
2089 case PROP_BUFFER_MODE:
2090 g_value_set_enum (value, rtpbin->buffer_mode);
2092 case PROP_USE_PIPELINE_CLOCK:
2093 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2096 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2102 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
2106 rtpbin = GST_RTP_BIN (bin);
2108 switch (GST_MESSAGE_TYPE (message)) {
2109 case GST_MESSAGE_ELEMENT:
2111 const GstStructure *s = gst_message_get_structure (message);
2113 /* we change the structure name and add the session ID to it */
2114 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
2115 GstRtpBinSession *sess;
2117 /* find the session we set it as object data */
2118 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2119 "GstRTPBin.session");
2121 if (G_LIKELY (sess)) {
2122 message = gst_message_make_writable (message);
2123 s = gst_message_get_structure (message);
2124 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
2128 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2131 case GST_MESSAGE_BUFFERING:
2134 gint min_percent = 100;
2135 GSList *sessions, *streams;
2136 GstRtpBinStream *stream;
2137 gboolean change = FALSE, active = FALSE;
2138 GstClockTime min_out_time;
2139 GstBufferingMode mode;
2140 gint avg_in, avg_out;
2141 gint64 buffering_left;
2143 gst_message_parse_buffering (message, &percent);
2144 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
2148 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2149 "GstRTPBin.stream");
2151 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
2153 /* get the stream */
2154 if (G_LIKELY (stream)) {
2155 GST_RTP_BIN_LOCK (rtpbin);
2156 /* fill in the percent */
2157 stream->percent = percent;
2159 /* calculate the min value for all streams */
2160 for (sessions = rtpbin->sessions; sessions;
2161 sessions = g_slist_next (sessions)) {
2162 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2164 GST_RTP_SESSION_LOCK (session);
2165 if (session->streams) {
2166 for (streams = session->streams; streams;
2167 streams = g_slist_next (streams)) {
2168 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2170 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
2173 /* find min percent */
2174 if (min_percent > stream->percent)
2175 min_percent = stream->percent;
2178 GST_INFO_OBJECT (bin,
2179 "session has no streams, setting min_percent to 0");
2182 GST_RTP_SESSION_UNLOCK (session);
2184 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
2186 if (rtpbin->buffering) {
2187 if (min_percent == 100) {
2188 rtpbin->buffering = FALSE;
2193 if (min_percent < 100) {
2194 /* pause the streams */
2195 rtpbin->buffering = TRUE;
2200 GST_RTP_BIN_UNLOCK (rtpbin);
2202 gst_message_unref (message);
2204 /* make a new buffering message with the min value */
2206 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2207 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2210 if (G_UNLIKELY (change)) {
2212 guint64 running_time = 0;
2215 /* figure out the running time when we have a clock */
2216 if (G_LIKELY ((clock =
2217 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2218 guint64 now, base_time;
2220 now = gst_clock_get_time (clock);
2221 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2222 running_time = now - base_time;
2223 gst_object_unref (clock);
2225 GST_DEBUG_OBJECT (bin,
2226 "running time now %" GST_TIME_FORMAT,
2227 GST_TIME_ARGS (running_time));
2229 GST_RTP_BIN_LOCK (rtpbin);
2231 /* when we reactivate, calculate the offsets so that all streams have
2232 * an output time that is at least as big as the running_time */
2235 if (running_time > rtpbin->buffer_start) {
2236 offset = running_time - rtpbin->buffer_start;
2237 if (offset >= rtpbin->latency_ns)
2238 offset -= rtpbin->latency_ns;
2244 /* pause all streams */
2246 for (sessions = rtpbin->sessions; sessions;
2247 sessions = g_slist_next (sessions)) {
2248 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2250 GST_RTP_SESSION_LOCK (session);
2251 for (streams = session->streams; streams;
2252 streams = g_slist_next (streams)) {
2253 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2254 GstElement *element = stream->buffer;
2257 g_signal_emit_by_name (element, "set-active", active, offset,
2261 g_object_get (element, "percent", &stream->percent, NULL);
2265 if (min_out_time == -1 || last_out < min_out_time)
2266 min_out_time = last_out;
2269 GST_DEBUG_OBJECT (bin,
2270 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2271 GST_TIME_FORMAT ", percent %d", element, active,
2272 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2275 GST_RTP_SESSION_UNLOCK (session);
2277 GST_DEBUG_OBJECT (bin,
2278 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2280 /* the buffer_start is the min out time of all paused jitterbuffers */
2282 rtpbin->buffer_start = min_out_time;
2284 GST_RTP_BIN_UNLOCK (rtpbin);
2287 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2292 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2298 static GstStateChangeReturn
2299 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2301 GstStateChangeReturn res;
2303 GstRtpBinPrivate *priv;
2305 rtpbin = GST_RTP_BIN (element);
2306 priv = rtpbin->priv;
2308 switch (transition) {
2309 case GST_STATE_CHANGE_NULL_TO_READY:
2311 case GST_STATE_CHANGE_READY_TO_PAUSED:
2312 priv->last_unix = 0;
2313 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2314 g_atomic_int_set (&priv->shutdown, 0);
2316 case GST_STATE_CHANGE_PAUSED_TO_READY:
2317 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2318 g_atomic_int_set (&priv->shutdown, 1);
2319 /* wait for all callbacks to end by taking the lock. No new callbacks will
2320 * be able to happen as we set the shutdown flag. */
2321 GST_RTP_BIN_DYN_LOCK (rtpbin);
2322 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2323 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2329 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2331 switch (transition) {
2332 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2334 case GST_STATE_CHANGE_PAUSED_TO_READY:
2336 case GST_STATE_CHANGE_READY_TO_NULL:
2344 /* a new pad (SSRC) was created in @session. This signal is emited from the
2345 * payload demuxer. */
2347 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2348 GstRtpBinStream * stream)
2351 GstElementClass *klass;
2352 GstPadTemplate *templ;
2356 rtpbin = stream->bin;
2358 GST_DEBUG ("new payload pad %d", pt);
2360 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2362 /* ghost the pad to the parent */
2363 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2364 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2365 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2366 stream->session->id, stream->ssrc, pt);
2367 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2369 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2371 gst_pad_set_active (gpad, TRUE);
2372 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2374 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2380 GST_DEBUG ("ignoring, we are shutting down");
2386 payload_pad_removed (GstElement * element, GstPad * pad,
2387 GstRtpBinStream * stream)
2392 rtpbin = stream->bin;
2394 GST_DEBUG ("payload pad removed");
2396 GST_RTP_BIN_DYN_LOCK (rtpbin);
2397 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2398 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2400 gst_pad_set_active (gpad, FALSE);
2401 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2403 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2407 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2412 rtpbin = session->bin;
2414 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2417 caps = get_pt_map (session, pt);
2426 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2432 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2434 GST_DEBUG_OBJECT (session->bin,
2435 "emiting signal for pt type changed to %d in session %d", pt,
2438 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2439 0, session->id, pt);
2442 /* emited when caps changed for the session */
2444 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2449 const GstStructure *s;
2453 g_object_get (pad, "caps", &caps, NULL);
2458 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2460 s = gst_caps_get_structure (caps, 0);
2462 /* get payload, finish when it's not there */
2463 if (!gst_structure_get_int (s, "payload", &payload))
2466 GST_RTP_SESSION_LOCK (session);
2467 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2468 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2469 GST_RTP_SESSION_UNLOCK (session);
2472 /* a new pad (SSRC) was created in @session */
2474 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2475 GstRtpBinSession * session)
2478 GstRtpBinStream *stream;
2479 GstPad *sinkpad, *srcpad;
2482 rtpbin = session->bin;
2484 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2485 GST_DEBUG_PAD_NAME (pad));
2487 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2489 GST_RTP_SESSION_LOCK (session);
2491 /* create new stream */
2492 stream = create_stream (session, ssrc);
2496 /* get pad and link */
2497 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2498 padname = g_strdup_printf ("src_%u", ssrc);
2499 srcpad = gst_element_get_static_pad (element, padname);
2501 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2502 gst_pad_link (srcpad, sinkpad);
2503 gst_object_unref (sinkpad);
2504 gst_object_unref (srcpad);
2506 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2507 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
2508 srcpad = gst_element_get_static_pad (element, padname);
2510 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2511 gst_pad_link (srcpad, sinkpad);
2512 gst_object_unref (sinkpad);
2513 gst_object_unref (srcpad);
2515 /* connect to the RTCP sync signal from the jitterbuffer */
2516 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2517 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2518 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2520 if (stream->demux) {
2521 /* connect to the new-pad signal of the payload demuxer, this will expose the
2522 * new pad by ghosting it. */
2523 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2524 "new-payload-type", (GCallback) new_payload_found, stream);
2525 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2526 "pad-removed", (GCallback) payload_pad_removed, stream);
2528 /* connect to the request-pt-map signal. This signal will be emited by the
2529 * demuxer so that it can apply a proper caps on the buffers for the
2531 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2532 "request-pt-map", (GCallback) pt_map_requested, session);
2533 /* connect to the signal so it can be forwarded. */
2534 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2535 "payload-type-change", (GCallback) payload_type_change, session);
2537 /* add gstrtpjitterbuffer src pad to pads */
2538 GstElementClass *klass;
2539 GstPadTemplate *templ;
2543 pad = gst_element_get_static_pad (stream->buffer, "src");
2545 /* ghost the pad to the parent */
2546 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2547 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2548 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2549 stream->session->id, stream->ssrc, 255);
2550 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2553 gst_pad_set_active (gpad, TRUE);
2554 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2556 gst_object_unref (pad);
2559 GST_RTP_SESSION_UNLOCK (session);
2560 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2567 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2572 GST_RTP_SESSION_UNLOCK (session);
2573 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2574 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2579 /* Create a pad for receiving RTP for the session in @name. Must be called with
2583 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2587 GstRtpBinSession *session;
2588 GstPadLinkReturn lres;
2590 /* first get the session number */
2591 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
2594 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2596 /* get or create session */
2597 session = find_session_by_id (rtpbin, sessid);
2599 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2600 /* create session now */
2601 session = create_session (rtpbin, sessid);
2602 if (session == NULL)
2606 /* check if pad was requested */
2607 if (session->recv_rtp_sink_ghost != NULL)
2608 return session->recv_rtp_sink_ghost;
2610 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2611 /* get recv_rtp pad and store */
2612 session->recv_rtp_sink =
2613 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2614 if (session->recv_rtp_sink == NULL)
2617 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2618 (GCallback) caps_changed, session);
2620 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2621 /* get srcpad, link to SSRCDemux */
2622 session->recv_rtp_src =
2623 gst_element_get_static_pad (session->session, "recv_rtp_src");
2624 if (session->recv_rtp_src == NULL)
2627 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2628 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2629 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2630 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2631 gst_object_unref (sinkdpad);
2632 if (lres != GST_PAD_LINK_OK)
2635 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2636 session->demux_newpad_sig = g_signal_connect (session->demux,
2637 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2638 session->demux_padremoved_sig = g_signal_connect (session->demux,
2639 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2641 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2642 session->recv_rtp_sink_ghost =
2643 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2644 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2645 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2647 return session->recv_rtp_sink_ghost;
2652 g_warning ("rtpbin: invalid name given");
2657 /* create_session already warned */
2662 g_warning ("rtpbin: failed to get session pad");
2667 g_warning ("rtpbin: failed to link pads");
2673 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2675 if (session->demux_newpad_sig) {
2676 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2677 session->demux_newpad_sig = 0;
2679 if (session->demux_padremoved_sig) {
2680 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2681 session->demux_padremoved_sig = 0;
2683 if (session->recv_rtp_src) {
2684 gst_object_unref (session->recv_rtp_src);
2685 session->recv_rtp_src = NULL;
2687 if (session->recv_rtp_sink) {
2688 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2689 gst_object_unref (session->recv_rtp_sink);
2690 session->recv_rtp_sink = NULL;
2692 if (session->recv_rtp_sink_ghost) {
2693 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2694 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2695 session->recv_rtp_sink_ghost);
2696 session->recv_rtp_sink_ghost = NULL;
2700 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2704 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2708 GstRtpBinSession *session;
2710 GstPadLinkReturn lres;
2712 /* first get the session number */
2713 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
2716 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2718 /* get or create the session */
2719 session = find_session_by_id (rtpbin, sessid);
2721 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2722 /* create session now */
2723 session = create_session (rtpbin, sessid);
2724 if (session == NULL)
2728 /* check if pad was requested */
2729 if (session->recv_rtcp_sink_ghost != NULL)
2730 return session->recv_rtcp_sink_ghost;
2732 /* get recv_rtp pad and store */
2733 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2734 session->recv_rtcp_sink =
2735 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2736 if (session->recv_rtcp_sink == NULL)
2739 /* get srcpad, link to SSRCDemux */
2740 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2741 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2742 if (session->sync_src == NULL)
2745 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2746 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2747 lres = gst_pad_link (session->sync_src, sinkdpad);
2748 gst_object_unref (sinkdpad);
2749 if (lres != GST_PAD_LINK_OK)
2752 session->recv_rtcp_sink_ghost =
2753 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2754 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2755 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2756 session->recv_rtcp_sink_ghost);
2758 return session->recv_rtcp_sink_ghost;
2763 g_warning ("rtpbin: invalid name given");
2768 /* create_session already warned */
2773 g_warning ("rtpbin: failed to get session pad");
2778 g_warning ("rtpbin: failed to link pads");
2784 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2786 if (session->recv_rtcp_sink_ghost) {
2787 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2788 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2789 session->recv_rtcp_sink_ghost);
2790 session->recv_rtcp_sink_ghost = NULL;
2792 if (session->sync_src) {
2793 /* releasing the request pad should also unref the sync pad */
2794 gst_object_unref (session->sync_src);
2795 session->sync_src = NULL;
2797 if (session->recv_rtcp_sink) {
2798 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2799 gst_object_unref (session->recv_rtcp_sink);
2800 session->recv_rtcp_sink = NULL;
2804 /* Create a pad for sending RTP for the session in @name. Must be called with
2808 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2812 GstRtpBinSession *session;
2813 GstElementClass *klass;
2815 /* first get the session number */
2816 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
2819 /* get or create session */
2820 session = find_session_by_id (rtpbin, sessid);
2822 /* create session now */
2823 session = create_session (rtpbin, sessid);
2824 if (session == NULL)
2828 /* check if pad was requested */
2829 if (session->send_rtp_sink_ghost != NULL)
2830 return session->send_rtp_sink_ghost;
2832 /* get send_rtp pad and store */
2833 session->send_rtp_sink =
2834 gst_element_get_request_pad (session->session, "send_rtp_sink");
2835 if (session->send_rtp_sink == NULL)
2838 session->send_rtp_sink_ghost =
2839 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2840 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2841 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2844 session->send_rtp_src =
2845 gst_element_get_static_pad (session->session, "send_rtp_src");
2846 if (session->send_rtp_src == NULL)
2849 /* ghost the new source pad */
2850 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2851 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
2852 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
2853 session->send_rtp_src_ghost =
2854 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2855 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2856 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2859 return session->send_rtp_sink_ghost;
2864 g_warning ("rtpbin: invalid name given");
2869 /* create_session already warned */
2874 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
2879 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
2885 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2887 if (session->send_rtp_src_ghost) {
2888 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2889 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2890 session->send_rtp_src_ghost);
2891 session->send_rtp_src_ghost = NULL;
2893 if (session->send_rtp_src) {
2894 gst_object_unref (session->send_rtp_src);
2895 session->send_rtp_src = NULL;
2897 if (session->send_rtp_sink) {
2898 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2899 session->send_rtp_sink);
2900 gst_object_unref (session->send_rtp_sink);
2901 session->send_rtp_sink = NULL;
2903 if (session->send_rtp_sink_ghost) {
2904 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2905 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2906 session->send_rtp_sink_ghost);
2907 session->send_rtp_sink_ghost = NULL;
2911 /* Create a pad for sending RTCP for the session in @name. Must be called with
2915 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2918 GstRtpBinSession *session;
2920 /* first get the session number */
2921 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
2924 /* get or create session */
2925 session = find_session_by_id (rtpbin, sessid);
2929 /* check if pad was requested */
2930 if (session->send_rtcp_src_ghost != NULL)
2931 return session->send_rtcp_src_ghost;
2933 /* get rtcp_src pad and store */
2934 session->send_rtcp_src =
2935 gst_element_get_request_pad (session->session, "send_rtcp_src");
2936 if (session->send_rtcp_src == NULL)
2939 session->send_rtcp_src_ghost =
2940 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2941 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2942 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2944 return session->send_rtcp_src_ghost;
2949 g_warning ("rtpbin: invalid name given");
2954 g_warning ("rtpbin: session with id %d does not exist", sessid);
2959 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
2965 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2967 if (session->send_rtcp_src_ghost) {
2968 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2969 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2970 session->send_rtcp_src_ghost);
2971 session->send_rtcp_src_ghost = NULL;
2973 if (session->send_rtcp_src) {
2974 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2975 gst_object_unref (session->send_rtcp_src);
2976 session->send_rtcp_src = NULL;
2980 /* If the requested name is NULL we should create a name with
2981 * the session number assuming we want the lowest posible session
2982 * with a free pad like the template */
2984 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2986 gboolean name_found = FALSE;
2988 GstIterator *pad_it = NULL;
2989 gchar *pad_name = NULL;
2990 GValue data = { 0, };
2992 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2993 while (!name_found) {
2994 gboolean done = FALSE;
2997 pad_name = g_strdup_printf (templ->name_template, session++);
2998 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
3001 switch (gst_iterator_next (pad_it, &data)) {
3002 case GST_ITERATOR_OK:
3007 pad = g_value_get_object (&data);
3008 name = gst_pad_get_name (pad);
3010 if (strcmp (name, pad_name) == 0) {
3015 g_value_reset (&data);
3018 case GST_ITERATOR_ERROR:
3019 case GST_ITERATOR_RESYNC:
3020 /* restart iteration */
3025 case GST_ITERATOR_DONE:
3030 g_value_unset (&data);
3031 gst_iterator_free (pad_it);
3034 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
3041 gst_rtp_bin_request_new_pad (GstElement * element,
3042 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
3045 GstElementClass *klass;
3048 gchar *pad_name = NULL;
3050 g_return_val_if_fail (templ != NULL, NULL);
3051 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
3053 rtpbin = GST_RTP_BIN (element);
3054 klass = GST_ELEMENT_GET_CLASS (element);
3056 GST_RTP_BIN_LOCK (rtpbin);
3059 /* use a free pad name */
3060 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
3062 /* use the provided name */
3063 pad_name = g_strdup (name);
3066 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
3068 /* figure out the template */
3069 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
3070 result = create_recv_rtp (rtpbin, templ, pad_name);
3071 } else if (templ == gst_element_class_get_pad_template (klass,
3072 "recv_rtcp_sink_%u")) {
3073 result = create_recv_rtcp (rtpbin, templ, pad_name);
3074 } else if (templ == gst_element_class_get_pad_template (klass,
3075 "send_rtp_sink_%u")) {
3076 result = create_send_rtp (rtpbin, templ, pad_name);
3077 } else if (templ == gst_element_class_get_pad_template (klass,
3078 "send_rtcp_src_%u")) {
3079 result = create_rtcp (rtpbin, templ, pad_name);
3081 goto wrong_template;
3084 GST_RTP_BIN_UNLOCK (rtpbin);
3092 GST_RTP_BIN_UNLOCK (rtpbin);
3093 g_warning ("rtpbin: this is not our template");
3099 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
3101 GstRtpBinSession *session;
3104 g_return_if_fail (GST_IS_GHOST_PAD (pad));
3105 g_return_if_fail (GST_IS_RTP_BIN (element));
3107 rtpbin = GST_RTP_BIN (element);
3109 GST_RTP_BIN_LOCK (rtpbin);
3110 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
3111 GST_DEBUG_PAD_NAME (pad));
3113 if (!(session = find_session_by_pad (rtpbin, pad)))
3116 if (session->recv_rtp_sink_ghost == pad) {
3117 remove_recv_rtp (rtpbin, session);
3118 } else if (session->recv_rtcp_sink_ghost == pad) {
3119 remove_recv_rtcp (rtpbin, session);
3120 } else if (session->send_rtp_sink_ghost == pad) {
3121 remove_send_rtp (rtpbin, session);
3122 } else if (session->send_rtcp_src_ghost == pad) {
3123 remove_rtcp (rtpbin, session);
3126 /* no more request pads, free the complete session */
3127 if (session->recv_rtp_sink_ghost == NULL
3128 && session->recv_rtcp_sink_ghost == NULL
3129 && session->send_rtp_sink_ghost == NULL
3130 && session->send_rtcp_src_ghost == NULL) {
3131 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
3132 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
3133 free_session (session, rtpbin);
3135 GST_RTP_BIN_UNLOCK (rtpbin);
3142 GST_RTP_BIN_UNLOCK (rtpbin);
3143 g_warning ("rtpbin: %s:%s is not one of our request pads",
3144 GST_DEBUG_PAD_NAME (pad));