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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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-1.0 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-1.0 gstrtpbin name=rtpbin \
74 * v4l2src ! videoconvert ! 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-1.0 -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_DROP_ON_LATENCY FALSE
247 #define DEFAULT_SDES NULL
248 #define DEFAULT_DO_LOST FALSE
249 #define DEFAULT_IGNORE_PT FALSE
250 #define DEFAULT_NTP_SYNC FALSE
251 #define DEFAULT_AUTOREMOVE FALSE
252 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
253 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
254 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
255 #define DEFAULT_RTCP_SYNC_INTERVAL 0
261 PROP_DROP_ON_LATENCY,
267 PROP_RTCP_SYNC_INTERVAL,
270 PROP_USE_PIPELINE_CLOCK,
276 GST_RTP_BIN_RTCP_SYNC_ALWAYS,
277 GST_RTP_BIN_RTCP_SYNC_INITIAL,
278 GST_RTP_BIN_RTCP_SYNC_RTP
281 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
283 gst_rtp_bin_rtcp_sync_get_type (void)
285 static GType rtcp_sync_type = 0;
286 static const GEnumValue rtcp_sync_types[] = {
287 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
288 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
289 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
293 if (!rtcp_sync_type) {
294 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
296 return rtcp_sync_type;
300 typedef struct _GstRtpBinSession GstRtpBinSession;
301 typedef struct _GstRtpBinStream GstRtpBinStream;
302 typedef struct _GstRtpBinClient GstRtpBinClient;
304 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
306 static GstCaps *pt_map_requested (GstElement * element, guint pt,
307 GstRtpBinSession * session);
308 static void payload_type_change (GstElement * element, guint pt,
309 GstRtpBinSession * session);
310 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
311 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
312 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
313 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
314 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
315 static void free_stream (GstRtpBinStream * stream);
317 /* Manages the RTP stream for one SSRC.
319 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
320 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
321 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
322 * together (see below).
324 struct _GstRtpBinStream
326 /* the SSRC of this stream */
332 /* the session this SSRC belongs to */
333 GstRtpBinSession *session;
335 /* the jitterbuffer of the SSRC */
337 gulong buffer_handlesync_sig;
338 gulong buffer_ptreq_sig;
339 gulong buffer_ntpstop_sig;
342 /* the PT demuxer of the SSRC */
344 gulong demux_newpad_sig;
345 gulong demux_padremoved_sig;
346 gulong demux_ptreq_sig;
347 gulong demux_ptchange_sig;
349 /* if we have calculated a valid rt_delta for this stream */
351 /* mapping to local RTP and NTP time */
354 /* base rtptime in gst time */
358 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
359 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
361 /* Manages the receiving end of the packets.
363 * There is one such structure for each RTP session (audio/video/...).
364 * We get the RTP/RTCP packets and stuff them into the session manager. From
365 * there they are pushed into an SSRC demuxer that splits the stream based on
366 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
367 * the GstRtpBinStream above).
369 struct _GstRtpBinSession
375 /* the session element */
377 /* the SSRC demuxer */
379 gulong demux_newpad_sig;
380 gulong demux_padremoved_sig;
384 /* list of GstRtpBinStream */
387 /* mapping of payload type to caps */
390 /* the pads of the session */
391 GstPad *recv_rtp_sink;
392 GstPad *recv_rtp_sink_ghost;
393 GstPad *recv_rtp_src;
394 GstPad *recv_rtcp_sink;
395 GstPad *recv_rtcp_sink_ghost;
397 GstPad *send_rtp_sink;
398 GstPad *send_rtp_sink_ghost;
399 GstPad *send_rtp_src;
400 GstPad *send_rtp_src_ghost;
401 GstPad *send_rtcp_src;
402 GstPad *send_rtcp_src_ghost;
405 /* Manages the RTP streams that come from one client and should therefore be
408 struct _GstRtpBinClient
410 /* the common CNAME for the streams */
419 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
420 static GstRtpBinSession *
421 find_session_by_id (GstRtpBin * rtpbin, gint id)
425 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
426 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
434 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
435 static GstRtpBinSession *
436 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
440 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
441 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
443 if ((sess->recv_rtp_sink_ghost == pad) ||
444 (sess->recv_rtcp_sink_ghost == pad) ||
445 (sess->send_rtp_sink_ghost == pad)
446 || (sess->send_rtcp_src_ghost == pad))
453 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
455 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
460 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
462 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
467 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
469 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
474 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
476 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
481 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
483 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
488 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
490 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
495 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
497 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
500 if (sess->bin->priv->autoremove)
501 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
505 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
507 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
510 if (sess->bin->priv->autoremove)
511 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
515 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
517 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
522 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
524 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
525 stream->session->id, stream->ssrc);
528 /* must be called with the SESSION lock */
529 static GstRtpBinStream *
530 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
534 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
535 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
537 if (stream->ssrc == ssrc)
544 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
545 GstRtpBinSession * session)
547 GstRtpBinStream *stream = NULL;
549 GST_RTP_SESSION_LOCK (session);
550 if ((stream = find_stream_by_ssrc (session, ssrc)))
551 session->streams = g_slist_remove (session->streams, stream);
552 GST_RTP_SESSION_UNLOCK (session);
555 free_stream (stream);
558 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
559 static GstRtpBinSession *
560 create_session (GstRtpBin * rtpbin, gint id)
562 GstRtpBinSession *sess;
563 GstElement *session, *demux;
566 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
569 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
572 sess = g_new0 (GstRtpBinSession, 1);
573 g_mutex_init (&sess->lock);
576 sess->session = session;
578 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
579 (GDestroyNotify) gst_caps_unref);
580 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
582 /* configure SDES items */
583 GST_OBJECT_LOCK (rtpbin);
584 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
585 rtpbin->use_pipeline_clock, NULL);
586 GST_OBJECT_UNLOCK (rtpbin);
588 /* provide clock_rate to the session manager when needed */
589 g_signal_connect (session, "request-pt-map",
590 (GCallback) pt_map_requested, sess);
592 g_signal_connect (sess->session, "on-new-ssrc",
593 (GCallback) on_new_ssrc, sess);
594 g_signal_connect (sess->session, "on-ssrc-collision",
595 (GCallback) on_ssrc_collision, sess);
596 g_signal_connect (sess->session, "on-ssrc-validated",
597 (GCallback) on_ssrc_validated, sess);
598 g_signal_connect (sess->session, "on-ssrc-active",
599 (GCallback) on_ssrc_active, sess);
600 g_signal_connect (sess->session, "on-ssrc-sdes",
601 (GCallback) on_ssrc_sdes, sess);
602 g_signal_connect (sess->session, "on-bye-ssrc",
603 (GCallback) on_bye_ssrc, sess);
604 g_signal_connect (sess->session, "on-bye-timeout",
605 (GCallback) on_bye_timeout, sess);
606 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
607 g_signal_connect (sess->session, "on-sender-timeout",
608 (GCallback) on_sender_timeout, sess);
610 gst_bin_add (GST_BIN_CAST (rtpbin), session);
611 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
613 GST_OBJECT_LOCK (rtpbin);
614 target = GST_STATE_TARGET (rtpbin);
615 GST_OBJECT_UNLOCK (rtpbin);
617 /* change state only to what's needed */
618 gst_element_set_state (demux, target);
619 gst_element_set_state (session, target);
626 g_warning ("rtpbin: could not create gstrtpsession element");
631 gst_object_unref (session);
632 g_warning ("rtpbin: could not create gstrtpssrcdemux element");
637 /* called with RTP_BIN_LOCK */
639 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
643 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
645 gst_element_set_locked_state (sess->demux, TRUE);
646 gst_element_set_locked_state (sess->session, TRUE);
648 gst_element_set_state (sess->demux, GST_STATE_NULL);
649 gst_element_set_state (sess->session, GST_STATE_NULL);
651 remove_recv_rtp (bin, sess);
652 remove_recv_rtcp (bin, sess);
653 remove_send_rtp (bin, sess);
654 remove_rtcp (bin, sess);
656 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
657 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
659 /* remove any references in bin->clients to the streams in sess->streams */
660 client_walk = bin->clients;
661 while (client_walk) {
662 GSList *client_node = client_walk;
663 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
664 GSList *stream_walk = client->streams;
666 while (stream_walk) {
667 GSList *stream_node = stream_walk;
668 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
671 stream_walk = g_slist_next (stream_walk);
673 for (inner_walk = sess->streams; inner_walk;
674 inner_walk = g_slist_next (inner_walk)) {
675 if ((GstRtpBinStream *) inner_walk->data == stream) {
676 client->streams = g_slist_delete_link (client->streams, stream_node);
682 client_walk = g_slist_next (client_walk);
684 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
685 && client->streams == 0));
686 if (client->nstreams == 0) {
687 free_client (client, bin);
688 bin->clients = g_slist_delete_link (bin->clients, client_node);
692 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
693 g_slist_free (sess->streams);
695 g_mutex_clear (&sess->lock);
696 g_hash_table_destroy (sess->ptmap);
701 /* get the payload type caps for the specific payload @pt in @session */
703 get_pt_map (GstRtpBinSession * session, guint pt)
705 GstCaps *caps = NULL;
708 GValue args[3] = { {0}, {0}, {0} };
710 GST_DEBUG ("searching pt %d in cache", pt);
712 GST_RTP_SESSION_LOCK (session);
714 /* first look in the cache */
715 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
723 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
725 /* not in cache, send signal to request caps */
726 g_value_init (&args[0], GST_TYPE_ELEMENT);
727 g_value_set_object (&args[0], bin);
728 g_value_init (&args[1], G_TYPE_UINT);
729 g_value_set_uint (&args[1], session->id);
730 g_value_init (&args[2], G_TYPE_UINT);
731 g_value_set_uint (&args[2], pt);
733 g_value_init (&ret, GST_TYPE_CAPS);
734 g_value_set_boxed (&ret, NULL);
736 GST_RTP_SESSION_UNLOCK (session);
738 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
740 GST_RTP_SESSION_LOCK (session);
742 g_value_unset (&args[0]);
743 g_value_unset (&args[1]);
744 g_value_unset (&args[2]);
746 /* look in the cache again because we let the lock go */
747 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
750 g_value_unset (&ret);
754 caps = (GstCaps *) g_value_dup_boxed (&ret);
755 g_value_unset (&ret);
759 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
761 /* store in cache, take additional ref */
762 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
763 gst_caps_ref (caps));
766 GST_RTP_SESSION_UNLOCK (session);
773 GST_RTP_SESSION_UNLOCK (session);
774 GST_DEBUG ("no pt map could be obtained");
780 return_true (gpointer key, gpointer value, gpointer user_data)
786 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
788 GSList *clients, *streams;
790 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
792 GST_RTP_BIN_LOCK (rtpbin);
793 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
794 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
796 /* reset sync on all streams for this client */
797 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
798 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
800 /* make use require a new SR packet for this stream before we attempt new
802 stream->have_sync = FALSE;
803 stream->rt_delta = 0;
804 stream->rtp_delta = 0;
805 stream->clock_base = -100 * GST_SECOND;
808 GST_RTP_BIN_UNLOCK (rtpbin);
812 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
814 GSList *sessions, *streams;
816 GST_RTP_BIN_LOCK (bin);
817 GST_DEBUG_OBJECT (bin, "clearing pt map");
818 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
819 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
821 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
822 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
824 GST_RTP_SESSION_LOCK (session);
825 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
827 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
828 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
830 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
831 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
833 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
835 GST_RTP_SESSION_UNLOCK (session);
837 GST_RTP_BIN_UNLOCK (bin);
840 gst_rtp_bin_reset_sync (bin);
844 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
846 RTPSession *internal_session = NULL;
847 GstRtpBinSession *session;
849 GST_RTP_BIN_LOCK (bin);
850 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
852 session = find_session_by_id (bin, (gint) session_id);
854 g_object_get (session->session, "internal-session", &internal_session,
857 GST_RTP_BIN_UNLOCK (bin);
859 return internal_session;
863 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
864 const gchar * name, const GValue * value)
866 GSList *sessions, *streams;
868 GST_RTP_BIN_LOCK (bin);
869 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
870 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
872 GST_RTP_SESSION_LOCK (session);
873 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
874 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
876 g_object_set_property (G_OBJECT (stream->buffer), name, value);
878 GST_RTP_SESSION_UNLOCK (session);
880 GST_RTP_BIN_UNLOCK (bin);
883 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
884 static GstRtpBinClient *
885 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
887 GstRtpBinClient *result = NULL;
890 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
891 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
893 if (len != client->cname_len)
896 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
897 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
904 /* nothing found, create one */
905 if (result == NULL) {
906 result = g_new0 (GstRtpBinClient, 1);
907 result->cname = g_strndup ((gchar *) data, len);
908 result->cname_len = len;
909 bin->clients = g_slist_prepend (bin->clients, result);
910 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
917 free_client (GstRtpBinClient * client, GstRtpBin * bin)
919 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
920 g_slist_free (client->streams);
921 g_free (client->cname);
926 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
931 GstClockTime base_time, rt, clock_time;
933 GST_OBJECT_LOCK (bin);
934 if ((clock = GST_ELEMENT_CLOCK (bin))) {
935 base_time = GST_ELEMENT_CAST (bin)->base_time;
936 gst_object_ref (clock);
937 GST_OBJECT_UNLOCK (bin);
939 clock_time = gst_clock_get_time (clock);
941 if (bin->use_pipeline_clock) {
942 ntpns = clock_time - base_time;
946 /* get current NTP time */
947 g_get_current_time (¤t);
948 ntpns = GST_TIMEVAL_TO_TIME (current);
951 /* add constant to convert from 1970 based time to 1900 based time */
952 ntpns += (2208988800LL * GST_SECOND);
954 /* get current clock time and convert to running time */
955 rt = clock_time - base_time;
957 gst_object_unref (clock);
959 GST_OBJECT_UNLOCK (bin);
970 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
971 gint64 ts_offset, gboolean check)
973 gint64 prev_ts_offset;
975 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
977 /* delta changed, see how much */
978 if (prev_ts_offset != ts_offset) {
981 diff = prev_ts_offset - ts_offset;
983 GST_DEBUG_OBJECT (bin,
984 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
985 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
988 /* only change diff when it changed more than 4 milliseconds. This
989 * compensates for rounding errors in NTP to RTP timestamp
991 if (ABS (diff) < 4 * GST_MSECOND) {
992 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
995 if (ABS (diff) > (3 * GST_SECOND)) {
996 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
1000 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1002 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1003 stream->ssrc, ts_offset);
1006 /* associate a stream to the given CNAME. This will make sure all streams for
1007 * that CNAME are synchronized together.
1008 * Must be called with GST_RTP_BIN_LOCK */
1010 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1011 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1012 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1013 gint64 rtp_clock_base)
1015 GstRtpBinClient *client;
1020 GstClockTime running_time;
1022 gint64 ntpdiff, rtdiff;
1025 /* first find or create the CNAME */
1026 client = get_client (bin, len, data, &created);
1028 /* find stream in the client */
1029 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1030 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1032 if (ostream == stream)
1035 /* not found, add it to the list */
1037 GST_DEBUG_OBJECT (bin,
1038 "new association of SSRC %08x with client %p with CNAME %s",
1039 stream->ssrc, client, client->cname);
1040 client->streams = g_slist_prepend (client->streams, stream);
1043 GST_DEBUG_OBJECT (bin,
1044 "found association of SSRC %08x with client %p with CNAME %s",
1045 stream->ssrc, client, client->cname);
1048 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1049 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1050 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1051 /* we don't need that data, so carry on,
1052 * but make some values look saner */
1053 last_extrtptime = base_rtptime;
1055 /* nothing we can do with this data in this case */
1056 GST_DEBUG_OBJECT (bin, "bailing out");
1061 /* Take the extended rtptime we found in the SR packet and map it to the
1062 * local rtptime. The local rtp time is used to construct timestamps on the
1063 * buffers so we will calculate what running_time corresponds to the RTP
1064 * timestamp in the SR packet. */
1065 local_rtp = last_extrtptime - base_rtptime;
1067 GST_DEBUG_OBJECT (bin,
1068 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1069 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1070 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1071 last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
1073 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1074 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1075 * into a corresponding gstreamer timestamp. Note that the base_time also
1076 * contains the drift between sender and receiver. */
1077 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1078 local_rt += base_time;
1080 /* convert ntptime to unix time since 1900 */
1081 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1082 (G_GINT64_CONSTANT (1) << 32));
1084 stream->have_sync = TRUE;
1086 GST_DEBUG_OBJECT (bin,
1087 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1088 local_rt, last_unix);
1090 /* recalc inter stream playout offset, but only if there is more than one
1091 * stream or we're doing NTP sync. */
1092 if (bin->ntp_sync) {
1093 /* For NTP sync we need to first get a snapshot of running_time and NTP
1094 * time. We know at what running_time we play a certain RTP time, we also
1095 * calculated when we would play the RTP time in the SR packet. Now we need
1096 * to know how the running_time and the NTP time relate to eachother. */
1097 get_current_times (bin, &running_time, &ntpnstime);
1099 /* see how far away the NTP time is. This is the difference between the
1100 * current NTP time and the NTP time in the last SR packet. */
1101 ntpdiff = ntpnstime - last_unix;
1102 /* see how far away the running_time is. This is the difference between the
1103 * current running_time and the running_time of the RTP timestamp in the
1104 * last SR packet. */
1105 rtdiff = running_time - local_rt;
1107 GST_DEBUG_OBJECT (bin,
1108 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1109 ntpnstime, last_unix);
1110 GST_DEBUG_OBJECT (bin,
1111 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1114 /* combine to get the final diff to apply to the running_time */
1115 stream->rt_delta = rtdiff - ntpdiff;
1117 stream_set_ts_offset (bin, stream, stream->rt_delta, FALSE);
1119 gint64 min, rtp_min, clock_base = stream->clock_base;
1120 gboolean all_sync, use_rtp;
1121 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1123 /* calculate delta between server and receiver. last_unix is created by
1124 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1125 * delta expresses the difference to our timeline and the server timeline. The
1126 * difference in itself doesn't mean much but we can combine the delta of
1127 * multiple streams to create a stream specific offset. */
1128 stream->rt_delta = last_unix - local_rt;
1130 /* calculate the min of all deltas, ignoring streams that did not yet have a
1131 * valid rt_delta because we did not yet receive an SR packet for those
1133 * We calculate the mininum because we would like to only apply positive
1134 * offsets to streams, delaying their playback instead of trying to speed up
1135 * other streams (which might be imposible when we have to create negative
1137 * The stream that has the smallest diff is selected as the reference stream,
1138 * all other streams will have a positive offset to this difference. */
1140 /* some alternative setting allow ignoring RTCP as much as possible,
1141 * for servers generating bogus ntp timeline */
1142 min = rtp_min = G_MAXINT64;
1144 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1148 /* signed version for convienience */
1149 clock_base = base_rtptime;
1150 /* deal with possible wrap-around */
1151 ext_base = base_rtptime;
1152 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1153 /* sanity check; base rtp and provided clock_base should be close */
1154 if (rtp_clock_base >= clock_base) {
1155 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1156 rtp_clock_base = base_time +
1157 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1158 GST_SECOND, clock_rate);
1163 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1164 rtp_clock_base = base_time -
1165 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1166 GST_SECOND, clock_rate);
1171 /* warn and bail for clarity out if no sane values */
1173 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1176 /* store to track changes */
1177 clock_base = rtp_clock_base;
1178 /* generate a fake as before,
1179 * now equating rtptime obtained from RTP-Info,
1180 * where the large time represent the otherwise irrelevant npt/ntp time */
1181 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1183 clock_base = rtp_clock_base;
1187 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1188 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1190 if (!ostream->have_sync) {
1195 /* change in current stream's base from previously init'ed value
1196 * leads to reset of all stream's base */
1197 if (stream != ostream && stream->clock_base >= 0 &&
1198 (stream->clock_base != clock_base)) {
1199 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1200 ostream->clock_base = -100 * GST_SECOND;
1201 ostream->rtp_delta = 0;
1204 if (ostream->rt_delta < min)
1205 min = ostream->rt_delta;
1206 if (ostream->rtp_delta < rtp_min)
1207 rtp_min = ostream->rtp_delta;
1210 /* arrange to re-sync for each stream upon significant change,
1212 all_sync = all_sync && (stream->clock_base == clock_base);
1213 stream->clock_base = clock_base;
1215 /* may need init performed above later on, but nothing more to do now */
1216 if (client->nstreams <= 1)
1219 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1220 " all sync %d", client, min, all_sync);
1221 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1223 switch (rtcp_sync) {
1224 case GST_RTP_BIN_RTCP_SYNC_RTP:
1227 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1228 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1230 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1231 /* if all have been synced already, do not bother further */
1233 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1241 /* bail out if we adjusted recently enough */
1242 if (all_sync && (last_unix - bin->priv->last_unix) <
1243 bin->rtcp_sync_interval * GST_MSECOND) {
1244 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1245 "previous sender info too recent "
1246 "(previous UNIX %" G_GUINT64_FORMAT ")", bin->priv->last_unix);
1249 bin->priv->last_unix = last_unix;
1251 /* calculate offsets for each stream */
1252 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1253 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1256 /* ignore streams for which we didn't receive an SR packet yet, we
1257 * can't synchronize them yet. We can however sync other streams just
1259 if (!ostream->have_sync)
1262 /* calculate offset to our reference stream, this should always give a
1263 * positive number. */
1265 ts_offset = ostream->rtp_delta - rtp_min;
1267 ts_offset = ostream->rt_delta - min;
1269 stream_set_ts_offset (bin, ostream, ts_offset, TRUE);
1275 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1276 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1277 (b) = gst_rtcp_packet_move_to_next ((packet)))
1279 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1280 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1281 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1283 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1284 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1285 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1288 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1289 GstRtpBinStream * stream)
1292 GstRTCPPacket packet;
1295 gboolean have_sr, have_sdes;
1297 guint64 base_rtptime;
1303 GstRTCPBuffer rtcp = { NULL, };
1307 GST_DEBUG_OBJECT (bin, "sync handler called");
1309 /* get the last relation between the rtp timestamps and the gstreamer
1310 * timestamps. We get this info directly from the jitterbuffer which
1311 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1312 * what the current situation is. */
1314 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1315 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1316 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1317 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1319 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1320 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1325 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1327 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1328 /* first packet must be SR or RR or else the validate would have failed */
1329 switch (gst_rtcp_packet_get_type (&packet)) {
1330 case GST_RTCP_TYPE_SR:
1331 /* only parse first. There is only supposed to be one SR in the packet
1332 * but we will deal with malformed packets gracefully */
1335 /* get NTP and RTP times */
1336 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1339 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1340 /* ignore SR that is not ours */
1341 if (ssrc != stream->ssrc)
1346 case GST_RTCP_TYPE_SDES:
1348 gboolean more_items, more_entries;
1350 /* only deal with first SDES, there is only supposed to be one SDES in
1351 * the RTCP packet but we deal with bad packets gracefully. Also bail
1352 * out if we have not seen an SR item yet. */
1353 if (have_sdes || !have_sr)
1356 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1357 /* skip items that are not about the SSRC of the sender */
1358 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1361 /* find the CNAME entry */
1362 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1363 GstRTCPSDESType type;
1367 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1369 if (type == GST_RTCP_SDES_CNAME) {
1370 GST_RTP_BIN_LOCK (bin);
1371 /* associate the stream to CNAME */
1372 gst_rtp_bin_associate (bin, stream, len, data,
1373 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1375 GST_RTP_BIN_UNLOCK (bin);
1383 /* we can ignore these packets */
1387 gst_rtcp_buffer_unmap (&rtcp);
1390 /* create a new stream with @ssrc in @session. Must be called with
1391 * RTP_SESSION_LOCK. */
1392 static GstRtpBinStream *
1393 create_stream (GstRtpBinSession * session, guint32 ssrc)
1395 GstElement *buffer, *demux = NULL;
1396 GstRtpBinStream *stream;
1400 rtpbin = session->bin;
1402 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1403 goto no_jitterbuffer;
1405 if (!rtpbin->ignore_pt)
1406 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1410 stream = g_new0 (GstRtpBinStream, 1);
1411 stream->ssrc = ssrc;
1412 stream->bin = rtpbin;
1413 stream->session = session;
1414 stream->buffer = buffer;
1415 stream->demux = demux;
1417 stream->have_sync = FALSE;
1418 stream->rt_delta = 0;
1419 stream->rtp_delta = 0;
1420 stream->percent = 100;
1421 stream->clock_base = -100 * GST_SECOND;
1422 session->streams = g_slist_prepend (session->streams, stream);
1424 /* provide clock_rate to the jitterbuffer when needed */
1425 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1426 (GCallback) pt_map_requested, session);
1427 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1428 (GCallback) on_npt_stop, stream);
1430 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1431 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1433 /* configure latency and packet lost */
1434 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1435 g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1436 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1437 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1439 if (!rtpbin->ignore_pt)
1440 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1441 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1445 gst_element_link (buffer, demux);
1447 if (rtpbin->buffering) {
1450 GST_INFO_OBJECT (rtpbin,
1451 "bin is buffering, set jitterbuffer as not active");
1452 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1456 GST_OBJECT_LOCK (rtpbin);
1457 target = GST_STATE_TARGET (rtpbin);
1458 GST_OBJECT_UNLOCK (rtpbin);
1460 /* from sink to source */
1462 gst_element_set_state (demux, target);
1464 gst_element_set_state (buffer, target);
1471 g_warning ("rtpbin: could not create gstrtpjitterbuffer element");
1476 gst_object_unref (buffer);
1477 g_warning ("rtpbin: could not create gstrtpptdemux element");
1483 free_stream (GstRtpBinStream * stream)
1485 GstRtpBinSession *session;
1487 session = stream->session;
1489 if (stream->demux) {
1490 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1491 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1492 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1494 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1495 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1496 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1499 gst_element_set_locked_state (stream->demux, TRUE);
1500 gst_element_set_locked_state (stream->buffer, TRUE);
1503 gst_element_set_state (stream->demux, GST_STATE_NULL);
1504 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1506 /* now remove this signal, we need this while going to NULL because it to
1507 * do some cleanups */
1509 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1511 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1513 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1518 /* GObject vmethods */
1519 static void gst_rtp_bin_dispose (GObject * object);
1520 static void gst_rtp_bin_finalize (GObject * object);
1521 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1522 const GValue * value, GParamSpec * pspec);
1523 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1524 GValue * value, GParamSpec * pspec);
1526 /* GstElement vmethods */
1527 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1528 GstStateChange transition);
1529 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1530 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1531 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1532 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1534 #define gst_rtp_bin_parent_class parent_class
1535 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1538 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1540 GObjectClass *gobject_class;
1541 GstElementClass *gstelement_class;
1542 GstBinClass *gstbin_class;
1544 gobject_class = (GObjectClass *) klass;
1545 gstelement_class = (GstElementClass *) klass;
1546 gstbin_class = (GstBinClass *) klass;
1548 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1550 gobject_class->dispose = gst_rtp_bin_dispose;
1551 gobject_class->finalize = gst_rtp_bin_finalize;
1552 gobject_class->set_property = gst_rtp_bin_set_property;
1553 gobject_class->get_property = gst_rtp_bin_get_property;
1555 g_object_class_install_property (gobject_class, PROP_LATENCY,
1556 g_param_spec_uint ("latency", "Buffer latency in ms",
1557 "Default amount of ms to buffer in the jitterbuffers", 0,
1558 G_MAXUINT, DEFAULT_LATENCY_MS,
1559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1561 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
1562 g_param_spec_boolean ("drop-on-latency",
1563 "Drop buffers when maximum latency is reached",
1564 "Tells the jitterbuffer to never exceed the given latency in size",
1565 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1568 * GstRtpBin::request-pt-map:
1569 * @rtpbin: the object which received the signal
1570 * @session: the session
1573 * Request the payload type as #GstCaps for @pt in @session.
1575 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1576 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1577 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1578 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1579 G_TYPE_UINT, G_TYPE_UINT);
1582 * GstRtpBin::payload-type-change:
1583 * @rtpbin: the object which received the signal
1584 * @session: the session
1587 * Signal that the current payload type changed to @pt in @session.
1591 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1592 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1593 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1594 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1595 G_TYPE_UINT, G_TYPE_UINT);
1598 * GstRtpBin::clear-pt-map:
1599 * @rtpbin: the object which received the signal
1601 * Clear all previously cached pt-mapping obtained with
1602 * #GstRtpBin::request-pt-map.
1604 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1605 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1606 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1607 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1611 * GstRtpBin::reset-sync:
1612 * @rtpbin: the object which received the signal
1614 * Reset all currently configured lip-sync parameters and require new SR
1615 * packets for all streams before lip-sync is attempted again.
1617 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1618 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1619 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1620 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1624 * GstRtpBin::get-internal-session:
1625 * @rtpbin: the object which received the signal
1626 * @id: the session id
1628 * Request the internal RTPSession object as #GObject in session @id.
1630 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1631 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1632 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1633 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1634 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1637 * GstRtpBin::on-new-ssrc:
1638 * @rtpbin: the object which received the signal
1639 * @session: the session
1642 * Notify of a new SSRC that entered @session.
1644 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1645 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1646 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1647 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1648 G_TYPE_UINT, G_TYPE_UINT);
1650 * GstRtpBin::on-ssrc-collision:
1651 * @rtpbin: the object which received the signal
1652 * @session: the session
1655 * Notify when we have an SSRC collision
1657 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1658 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1659 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1660 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1661 G_TYPE_UINT, G_TYPE_UINT);
1663 * GstRtpBin::on-ssrc-validated:
1664 * @rtpbin: the object which received the signal
1665 * @session: the session
1668 * Notify of a new SSRC that became validated.
1670 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1671 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1672 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1673 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1674 G_TYPE_UINT, G_TYPE_UINT);
1676 * GstRtpBin::on-ssrc-active:
1677 * @rtpbin: the object which received the signal
1678 * @session: the session
1681 * Notify of a SSRC that is active, i.e., sending RTCP.
1683 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1684 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1685 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1686 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1687 G_TYPE_UINT, G_TYPE_UINT);
1689 * GstRtpBin::on-ssrc-sdes:
1690 * @rtpbin: the object which received the signal
1691 * @session: the session
1694 * Notify of a SSRC that is active, i.e., sending RTCP.
1696 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1697 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1698 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1699 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1700 G_TYPE_UINT, G_TYPE_UINT);
1703 * GstRtpBin::on-bye-ssrc:
1704 * @rtpbin: the object which received the signal
1705 * @session: the session
1708 * Notify of an SSRC that became inactive because of a BYE packet.
1710 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1711 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1712 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1713 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1714 G_TYPE_UINT, G_TYPE_UINT);
1716 * GstRtpBin::on-bye-timeout:
1717 * @rtpbin: the object which received the signal
1718 * @session: the session
1721 * Notify of an SSRC that has timed out because of BYE
1723 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1724 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1725 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1726 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1727 G_TYPE_UINT, G_TYPE_UINT);
1729 * GstRtpBin::on-timeout:
1730 * @rtpbin: the object which received the signal
1731 * @session: the session
1734 * Notify of an SSRC that has timed out
1736 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1737 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1738 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1739 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1740 G_TYPE_UINT, G_TYPE_UINT);
1742 * GstRtpBin::on-sender-timeout:
1743 * @rtpbin: the object which received the signal
1744 * @session: the session
1747 * Notify of a sender SSRC that has timed out and became a receiver
1749 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1750 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1751 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1752 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1753 G_TYPE_UINT, G_TYPE_UINT);
1756 * GstRtpBin::on-npt-stop:
1757 * @rtpbin: the object which received the signal
1758 * @session: the session
1761 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1763 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1764 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1765 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1766 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1767 G_TYPE_UINT, G_TYPE_UINT);
1769 g_object_class_install_property (gobject_class, PROP_SDES,
1770 g_param_spec_boxed ("sdes", "SDES",
1771 "The SDES items of this session",
1772 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1774 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1775 g_param_spec_boolean ("do-lost", "Do Lost",
1776 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1777 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1779 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1780 g_param_spec_boolean ("autoremove", "Auto Remove",
1781 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1782 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1784 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1785 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1786 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1787 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1789 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1790 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1791 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
1792 DEFAULT_USE_PIPELINE_CLOCK,
1793 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1795 * GstRtpBin::buffer-mode:
1797 * Control the buffering and timestamping mode used by the jitterbuffer.
1801 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1802 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1803 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1804 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1806 * GstRtpBin::ntp-sync:
1808 * Set the NTP time from the sender reports as the running-time on the
1809 * buffers. When both the sender and receiver have sychronized
1810 * running-time, i.e. when the clock and base-time is shared
1811 * between the receivers and the and the senders, this option can be
1812 * 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_static_metadata (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->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1895 rtpbin->do_lost = DEFAULT_DO_LOST;
1896 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1897 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1898 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
1899 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
1900 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1901 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1902 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1904 /* some default SDES entries */
1905 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
1906 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1907 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
1912 gst_rtp_bin_dispose (GObject * object)
1916 rtpbin = GST_RTP_BIN (object);
1918 GST_RTP_BIN_LOCK (rtpbin);
1919 GST_DEBUG_OBJECT (object, "freeing sessions");
1920 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1921 g_slist_free (rtpbin->sessions);
1922 rtpbin->sessions = NULL;
1923 GST_DEBUG_OBJECT (object, "freeing clients");
1924 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1925 g_slist_free (rtpbin->clients);
1926 rtpbin->clients = NULL;
1927 GST_RTP_BIN_UNLOCK (rtpbin);
1929 G_OBJECT_CLASS (parent_class)->dispose (object);
1933 gst_rtp_bin_finalize (GObject * object)
1937 rtpbin = GST_RTP_BIN (object);
1940 gst_structure_free (rtpbin->sdes);
1942 g_mutex_clear (&rtpbin->priv->bin_lock);
1943 g_mutex_clear (&rtpbin->priv->dyn_lock);
1945 G_OBJECT_CLASS (parent_class)->finalize (object);
1950 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1957 GST_RTP_BIN_LOCK (bin);
1959 GST_OBJECT_LOCK (bin);
1961 gst_structure_free (bin->sdes);
1962 bin->sdes = gst_structure_copy (sdes);
1963 GST_OBJECT_UNLOCK (bin);
1965 /* store in all sessions */
1966 for (item = bin->sessions; item; item = g_slist_next (item)) {
1967 GstRtpBinSession *session = item->data;
1968 g_object_set (session->session, "sdes", sdes, NULL);
1971 GST_RTP_BIN_UNLOCK (bin);
1974 static GstStructure *
1975 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1977 GstStructure *result;
1979 GST_OBJECT_LOCK (bin);
1980 result = gst_structure_copy (bin->sdes);
1981 GST_OBJECT_UNLOCK (bin);
1987 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1988 const GValue * value, GParamSpec * pspec)
1992 rtpbin = GST_RTP_BIN (object);
1996 GST_RTP_BIN_LOCK (rtpbin);
1997 rtpbin->latency_ms = g_value_get_uint (value);
1998 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1999 GST_RTP_BIN_UNLOCK (rtpbin);
2000 /* propagate the property down to the jitterbuffer */
2001 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2003 case PROP_DROP_ON_LATENCY:
2004 GST_RTP_BIN_LOCK (rtpbin);
2005 rtpbin->drop_on_latency = g_value_get_boolean (value);
2006 GST_RTP_BIN_UNLOCK (rtpbin);
2007 /* propagate the property down to the jitterbuffer */
2008 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2009 "drop-on-latency", value);
2012 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2015 GST_RTP_BIN_LOCK (rtpbin);
2016 rtpbin->do_lost = g_value_get_boolean (value);
2017 GST_RTP_BIN_UNLOCK (rtpbin);
2018 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2021 rtpbin->ntp_sync = g_value_get_boolean (value);
2023 case PROP_RTCP_SYNC:
2024 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2026 case PROP_RTCP_SYNC_INTERVAL:
2027 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2029 case PROP_IGNORE_PT:
2030 rtpbin->ignore_pt = g_value_get_boolean (value);
2032 case PROP_AUTOREMOVE:
2033 rtpbin->priv->autoremove = g_value_get_boolean (value);
2035 case PROP_USE_PIPELINE_CLOCK:
2038 GST_RTP_BIN_LOCK (rtpbin);
2039 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2040 for (sessions = rtpbin->sessions; sessions;
2041 sessions = g_slist_next (sessions)) {
2042 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2044 g_object_set (G_OBJECT (session->session),
2045 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2047 GST_RTP_BIN_UNLOCK (rtpbin);
2050 case PROP_BUFFER_MODE:
2051 GST_RTP_BIN_LOCK (rtpbin);
2052 rtpbin->buffer_mode = g_value_get_enum (value);
2053 GST_RTP_BIN_UNLOCK (rtpbin);
2054 /* propagate the property down to the jitterbuffer */
2055 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2058 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2064 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2065 GValue * value, GParamSpec * pspec)
2069 rtpbin = GST_RTP_BIN (object);
2073 GST_RTP_BIN_LOCK (rtpbin);
2074 g_value_set_uint (value, rtpbin->latency_ms);
2075 GST_RTP_BIN_UNLOCK (rtpbin);
2077 case PROP_DROP_ON_LATENCY:
2078 GST_RTP_BIN_LOCK (rtpbin);
2079 g_value_set_boolean (value, rtpbin->drop_on_latency);
2080 GST_RTP_BIN_UNLOCK (rtpbin);
2083 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2086 GST_RTP_BIN_LOCK (rtpbin);
2087 g_value_set_boolean (value, rtpbin->do_lost);
2088 GST_RTP_BIN_UNLOCK (rtpbin);
2090 case PROP_IGNORE_PT:
2091 g_value_set_boolean (value, rtpbin->ignore_pt);
2094 g_value_set_boolean (value, rtpbin->ntp_sync);
2096 case PROP_RTCP_SYNC:
2097 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2099 case PROP_RTCP_SYNC_INTERVAL:
2100 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2102 case PROP_AUTOREMOVE:
2103 g_value_set_boolean (value, rtpbin->priv->autoremove);
2105 case PROP_BUFFER_MODE:
2106 g_value_set_enum (value, rtpbin->buffer_mode);
2108 case PROP_USE_PIPELINE_CLOCK:
2109 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2118 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
2122 rtpbin = GST_RTP_BIN (bin);
2124 switch (GST_MESSAGE_TYPE (message)) {
2125 case GST_MESSAGE_ELEMENT:
2127 const GstStructure *s = gst_message_get_structure (message);
2129 /* we change the structure name and add the session ID to it */
2130 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
2131 GstRtpBinSession *sess;
2133 /* find the session we set it as object data */
2134 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2135 "GstRTPBin.session");
2137 if (G_LIKELY (sess)) {
2138 message = gst_message_make_writable (message);
2139 s = gst_message_get_structure (message);
2140 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
2144 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2147 case GST_MESSAGE_BUFFERING:
2150 gint min_percent = 100;
2151 GSList *sessions, *streams;
2152 GstRtpBinStream *stream;
2153 gboolean change = FALSE, active = FALSE;
2154 GstClockTime min_out_time;
2155 GstBufferingMode mode;
2156 gint avg_in, avg_out;
2157 gint64 buffering_left;
2159 gst_message_parse_buffering (message, &percent);
2160 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
2164 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2165 "GstRTPBin.stream");
2167 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
2169 /* get the stream */
2170 if (G_LIKELY (stream)) {
2171 GST_RTP_BIN_LOCK (rtpbin);
2172 /* fill in the percent */
2173 stream->percent = percent;
2175 /* calculate the min value for all streams */
2176 for (sessions = rtpbin->sessions; sessions;
2177 sessions = g_slist_next (sessions)) {
2178 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2180 GST_RTP_SESSION_LOCK (session);
2181 if (session->streams) {
2182 for (streams = session->streams; streams;
2183 streams = g_slist_next (streams)) {
2184 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2186 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
2189 /* find min percent */
2190 if (min_percent > stream->percent)
2191 min_percent = stream->percent;
2194 GST_INFO_OBJECT (bin,
2195 "session has no streams, setting min_percent to 0");
2198 GST_RTP_SESSION_UNLOCK (session);
2200 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
2202 if (rtpbin->buffering) {
2203 if (min_percent == 100) {
2204 rtpbin->buffering = FALSE;
2209 if (min_percent < 100) {
2210 /* pause the streams */
2211 rtpbin->buffering = TRUE;
2216 GST_RTP_BIN_UNLOCK (rtpbin);
2218 gst_message_unref (message);
2220 /* make a new buffering message with the min value */
2222 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2223 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2226 if (G_UNLIKELY (change)) {
2228 guint64 running_time = 0;
2231 /* figure out the running time when we have a clock */
2232 if (G_LIKELY ((clock =
2233 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2234 guint64 now, base_time;
2236 now = gst_clock_get_time (clock);
2237 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2238 running_time = now - base_time;
2239 gst_object_unref (clock);
2241 GST_DEBUG_OBJECT (bin,
2242 "running time now %" GST_TIME_FORMAT,
2243 GST_TIME_ARGS (running_time));
2245 GST_RTP_BIN_LOCK (rtpbin);
2247 /* when we reactivate, calculate the offsets so that all streams have
2248 * an output time that is at least as big as the running_time */
2251 if (running_time > rtpbin->buffer_start) {
2252 offset = running_time - rtpbin->buffer_start;
2253 if (offset >= rtpbin->latency_ns)
2254 offset -= rtpbin->latency_ns;
2260 /* pause all streams */
2262 for (sessions = rtpbin->sessions; sessions;
2263 sessions = g_slist_next (sessions)) {
2264 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2266 GST_RTP_SESSION_LOCK (session);
2267 for (streams = session->streams; streams;
2268 streams = g_slist_next (streams)) {
2269 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2270 GstElement *element = stream->buffer;
2273 g_signal_emit_by_name (element, "set-active", active, offset,
2277 g_object_get (element, "percent", &stream->percent, NULL);
2281 if (min_out_time == -1 || last_out < min_out_time)
2282 min_out_time = last_out;
2285 GST_DEBUG_OBJECT (bin,
2286 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2287 GST_TIME_FORMAT ", percent %d", element, active,
2288 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2291 GST_RTP_SESSION_UNLOCK (session);
2293 GST_DEBUG_OBJECT (bin,
2294 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2296 /* the buffer_start is the min out time of all paused jitterbuffers */
2298 rtpbin->buffer_start = min_out_time;
2300 GST_RTP_BIN_UNLOCK (rtpbin);
2303 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2308 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2314 static GstStateChangeReturn
2315 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2317 GstStateChangeReturn res;
2319 GstRtpBinPrivate *priv;
2321 rtpbin = GST_RTP_BIN (element);
2322 priv = rtpbin->priv;
2324 switch (transition) {
2325 case GST_STATE_CHANGE_NULL_TO_READY:
2327 case GST_STATE_CHANGE_READY_TO_PAUSED:
2328 priv->last_unix = 0;
2329 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2330 g_atomic_int_set (&priv->shutdown, 0);
2332 case GST_STATE_CHANGE_PAUSED_TO_READY:
2333 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2334 g_atomic_int_set (&priv->shutdown, 1);
2335 /* wait for all callbacks to end by taking the lock. No new callbacks will
2336 * be able to happen as we set the shutdown flag. */
2337 GST_RTP_BIN_DYN_LOCK (rtpbin);
2338 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2339 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2345 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2347 switch (transition) {
2348 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2350 case GST_STATE_CHANGE_PAUSED_TO_READY:
2352 case GST_STATE_CHANGE_READY_TO_NULL:
2360 /* a new pad (SSRC) was created in @session. This signal is emited from the
2361 * payload demuxer. */
2363 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2364 GstRtpBinStream * stream)
2367 GstElementClass *klass;
2368 GstPadTemplate *templ;
2372 rtpbin = stream->bin;
2374 GST_DEBUG ("new payload pad %d", pt);
2376 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2378 /* ghost the pad to the parent */
2379 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2380 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2381 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2382 stream->session->id, stream->ssrc, pt);
2383 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2385 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2387 gst_pad_set_active (gpad, TRUE);
2388 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2390 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2396 GST_DEBUG ("ignoring, we are shutting down");
2402 payload_pad_removed (GstElement * element, GstPad * pad,
2403 GstRtpBinStream * stream)
2408 rtpbin = stream->bin;
2410 GST_DEBUG ("payload pad removed");
2412 GST_RTP_BIN_DYN_LOCK (rtpbin);
2413 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2414 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2416 gst_pad_set_active (gpad, FALSE);
2417 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2419 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2423 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2428 rtpbin = session->bin;
2430 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2433 caps = get_pt_map (session, pt);
2442 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2448 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2450 GST_DEBUG_OBJECT (session->bin,
2451 "emiting signal for pt type changed to %d in session %d", pt,
2454 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2455 0, session->id, pt);
2458 /* emited when caps changed for the session */
2460 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2465 const GstStructure *s;
2469 g_object_get (pad, "caps", &caps, NULL);
2474 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2476 s = gst_caps_get_structure (caps, 0);
2478 /* get payload, finish when it's not there */
2479 if (!gst_structure_get_int (s, "payload", &payload))
2482 GST_RTP_SESSION_LOCK (session);
2483 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2484 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2485 GST_RTP_SESSION_UNLOCK (session);
2488 /* a new pad (SSRC) was created in @session */
2490 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2491 GstRtpBinSession * session)
2494 GstRtpBinStream *stream;
2495 GstPad *sinkpad, *srcpad;
2498 rtpbin = session->bin;
2500 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2501 GST_DEBUG_PAD_NAME (pad));
2503 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2505 GST_RTP_SESSION_LOCK (session);
2507 /* create new stream */
2508 stream = create_stream (session, ssrc);
2512 /* get pad and link */
2513 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2514 padname = g_strdup_printf ("src_%u", ssrc);
2515 srcpad = gst_element_get_static_pad (element, padname);
2517 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2518 gst_pad_link (srcpad, sinkpad);
2519 gst_object_unref (sinkpad);
2520 gst_object_unref (srcpad);
2522 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2523 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
2524 srcpad = gst_element_get_static_pad (element, padname);
2526 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2527 gst_pad_link (srcpad, sinkpad);
2528 gst_object_unref (sinkpad);
2529 gst_object_unref (srcpad);
2531 /* connect to the RTCP sync signal from the jitterbuffer */
2532 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2533 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2534 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2536 if (stream->demux) {
2537 /* connect to the new-pad signal of the payload demuxer, this will expose the
2538 * new pad by ghosting it. */
2539 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2540 "new-payload-type", (GCallback) new_payload_found, stream);
2541 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2542 "pad-removed", (GCallback) payload_pad_removed, stream);
2544 /* connect to the request-pt-map signal. This signal will be emited by the
2545 * demuxer so that it can apply a proper caps on the buffers for the
2547 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2548 "request-pt-map", (GCallback) pt_map_requested, session);
2549 /* connect to the signal so it can be forwarded. */
2550 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2551 "payload-type-change", (GCallback) payload_type_change, session);
2553 /* add gstrtpjitterbuffer src pad to pads */
2554 GstElementClass *klass;
2555 GstPadTemplate *templ;
2559 pad = gst_element_get_static_pad (stream->buffer, "src");
2561 /* ghost the pad to the parent */
2562 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2563 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2564 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2565 stream->session->id, stream->ssrc, 255);
2566 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2569 gst_pad_set_active (gpad, TRUE);
2570 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2572 gst_object_unref (pad);
2575 GST_RTP_SESSION_UNLOCK (session);
2576 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2583 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2588 GST_RTP_SESSION_UNLOCK (session);
2589 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2590 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2595 /* Create a pad for receiving RTP for the session in @name. Must be called with
2599 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2603 GstRtpBinSession *session;
2604 GstPadLinkReturn lres;
2606 /* first get the session number */
2607 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
2610 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2612 /* get or create session */
2613 session = find_session_by_id (rtpbin, sessid);
2615 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2616 /* create session now */
2617 session = create_session (rtpbin, sessid);
2618 if (session == NULL)
2622 /* check if pad was requested */
2623 if (session->recv_rtp_sink_ghost != NULL)
2624 return session->recv_rtp_sink_ghost;
2626 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2627 /* get recv_rtp pad and store */
2628 session->recv_rtp_sink =
2629 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2630 if (session->recv_rtp_sink == NULL)
2633 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2634 (GCallback) caps_changed, session);
2636 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2637 /* get srcpad, link to SSRCDemux */
2638 session->recv_rtp_src =
2639 gst_element_get_static_pad (session->session, "recv_rtp_src");
2640 if (session->recv_rtp_src == NULL)
2643 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2644 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2645 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2646 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2647 gst_object_unref (sinkdpad);
2648 if (lres != GST_PAD_LINK_OK)
2651 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2652 session->demux_newpad_sig = g_signal_connect (session->demux,
2653 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2654 session->demux_padremoved_sig = g_signal_connect (session->demux,
2655 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2657 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2658 session->recv_rtp_sink_ghost =
2659 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2660 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2661 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2663 return session->recv_rtp_sink_ghost;
2668 g_warning ("rtpbin: invalid name given");
2673 /* create_session already warned */
2678 g_warning ("rtpbin: failed to get session pad");
2683 g_warning ("rtpbin: failed to link pads");
2689 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2691 if (session->demux_newpad_sig) {
2692 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2693 session->demux_newpad_sig = 0;
2695 if (session->demux_padremoved_sig) {
2696 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2697 session->demux_padremoved_sig = 0;
2699 if (session->recv_rtp_src) {
2700 gst_object_unref (session->recv_rtp_src);
2701 session->recv_rtp_src = NULL;
2703 if (session->recv_rtp_sink) {
2704 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2705 gst_object_unref (session->recv_rtp_sink);
2706 session->recv_rtp_sink = NULL;
2708 if (session->recv_rtp_sink_ghost) {
2709 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2710 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2711 session->recv_rtp_sink_ghost);
2712 session->recv_rtp_sink_ghost = NULL;
2716 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2720 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2724 GstRtpBinSession *session;
2726 GstPadLinkReturn lres;
2728 /* first get the session number */
2729 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
2732 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2734 /* get or create the session */
2735 session = find_session_by_id (rtpbin, sessid);
2737 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2738 /* create session now */
2739 session = create_session (rtpbin, sessid);
2740 if (session == NULL)
2744 /* check if pad was requested */
2745 if (session->recv_rtcp_sink_ghost != NULL)
2746 return session->recv_rtcp_sink_ghost;
2748 /* get recv_rtp pad and store */
2749 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2750 session->recv_rtcp_sink =
2751 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2752 if (session->recv_rtcp_sink == NULL)
2755 /* get srcpad, link to SSRCDemux */
2756 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2757 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2758 if (session->sync_src == NULL)
2761 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2762 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2763 lres = gst_pad_link (session->sync_src, sinkdpad);
2764 gst_object_unref (sinkdpad);
2765 if (lres != GST_PAD_LINK_OK)
2768 session->recv_rtcp_sink_ghost =
2769 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2770 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2771 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2772 session->recv_rtcp_sink_ghost);
2774 return session->recv_rtcp_sink_ghost;
2779 g_warning ("rtpbin: invalid name given");
2784 /* create_session already warned */
2789 g_warning ("rtpbin: failed to get session pad");
2794 g_warning ("rtpbin: failed to link pads");
2800 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2802 if (session->recv_rtcp_sink_ghost) {
2803 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2804 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2805 session->recv_rtcp_sink_ghost);
2806 session->recv_rtcp_sink_ghost = NULL;
2808 if (session->sync_src) {
2809 /* releasing the request pad should also unref the sync pad */
2810 gst_object_unref (session->sync_src);
2811 session->sync_src = NULL;
2813 if (session->recv_rtcp_sink) {
2814 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2815 gst_object_unref (session->recv_rtcp_sink);
2816 session->recv_rtcp_sink = NULL;
2820 /* Create a pad for sending RTP for the session in @name. Must be called with
2824 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2828 GstRtpBinSession *session;
2829 GstElementClass *klass;
2831 /* first get the session number */
2832 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
2835 /* get or create session */
2836 session = find_session_by_id (rtpbin, sessid);
2838 /* create session now */
2839 session = create_session (rtpbin, sessid);
2840 if (session == NULL)
2844 /* check if pad was requested */
2845 if (session->send_rtp_sink_ghost != NULL)
2846 return session->send_rtp_sink_ghost;
2848 /* get send_rtp pad and store */
2849 session->send_rtp_sink =
2850 gst_element_get_request_pad (session->session, "send_rtp_sink");
2851 if (session->send_rtp_sink == NULL)
2854 session->send_rtp_sink_ghost =
2855 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2856 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2857 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2860 session->send_rtp_src =
2861 gst_element_get_static_pad (session->session, "send_rtp_src");
2862 if (session->send_rtp_src == NULL)
2865 /* ghost the new source pad */
2866 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2867 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
2868 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
2869 session->send_rtp_src_ghost =
2870 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2871 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2872 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2875 return session->send_rtp_sink_ghost;
2880 g_warning ("rtpbin: invalid name given");
2885 /* create_session already warned */
2890 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
2895 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
2901 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2903 if (session->send_rtp_src_ghost) {
2904 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2905 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2906 session->send_rtp_src_ghost);
2907 session->send_rtp_src_ghost = NULL;
2909 if (session->send_rtp_src) {
2910 gst_object_unref (session->send_rtp_src);
2911 session->send_rtp_src = NULL;
2913 if (session->send_rtp_sink) {
2914 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2915 session->send_rtp_sink);
2916 gst_object_unref (session->send_rtp_sink);
2917 session->send_rtp_sink = NULL;
2919 if (session->send_rtp_sink_ghost) {
2920 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2921 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2922 session->send_rtp_sink_ghost);
2923 session->send_rtp_sink_ghost = NULL;
2927 /* Create a pad for sending RTCP for the session in @name. Must be called with
2931 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2934 GstRtpBinSession *session;
2936 /* first get the session number */
2937 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
2940 /* get or create session */
2941 session = find_session_by_id (rtpbin, sessid);
2945 /* check if pad was requested */
2946 if (session->send_rtcp_src_ghost != NULL)
2947 return session->send_rtcp_src_ghost;
2949 /* get rtcp_src pad and store */
2950 session->send_rtcp_src =
2951 gst_element_get_request_pad (session->session, "send_rtcp_src");
2952 if (session->send_rtcp_src == NULL)
2955 session->send_rtcp_src_ghost =
2956 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2957 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2958 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2960 return session->send_rtcp_src_ghost;
2965 g_warning ("rtpbin: invalid name given");
2970 g_warning ("rtpbin: session with id %d does not exist", sessid);
2975 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
2981 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2983 if (session->send_rtcp_src_ghost) {
2984 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2985 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2986 session->send_rtcp_src_ghost);
2987 session->send_rtcp_src_ghost = NULL;
2989 if (session->send_rtcp_src) {
2990 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2991 gst_object_unref (session->send_rtcp_src);
2992 session->send_rtcp_src = NULL;
2996 /* If the requested name is NULL we should create a name with
2997 * the session number assuming we want the lowest posible session
2998 * with a free pad like the template */
3000 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
3002 gboolean name_found = FALSE;
3004 GstIterator *pad_it = NULL;
3005 gchar *pad_name = NULL;
3006 GValue data = { 0, };
3008 GST_DEBUG_OBJECT (element, "find a free pad name for template");
3009 while (!name_found) {
3010 gboolean done = FALSE;
3013 pad_name = g_strdup_printf (templ->name_template, session++);
3014 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
3017 switch (gst_iterator_next (pad_it, &data)) {
3018 case GST_ITERATOR_OK:
3023 pad = g_value_get_object (&data);
3024 name = gst_pad_get_name (pad);
3026 if (strcmp (name, pad_name) == 0) {
3031 g_value_reset (&data);
3034 case GST_ITERATOR_ERROR:
3035 case GST_ITERATOR_RESYNC:
3036 /* restart iteration */
3041 case GST_ITERATOR_DONE:
3046 g_value_unset (&data);
3047 gst_iterator_free (pad_it);
3050 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
3057 gst_rtp_bin_request_new_pad (GstElement * element,
3058 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
3061 GstElementClass *klass;
3064 gchar *pad_name = NULL;
3066 g_return_val_if_fail (templ != NULL, NULL);
3067 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
3069 rtpbin = GST_RTP_BIN (element);
3070 klass = GST_ELEMENT_GET_CLASS (element);
3072 GST_RTP_BIN_LOCK (rtpbin);
3075 /* use a free pad name */
3076 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
3078 /* use the provided name */
3079 pad_name = g_strdup (name);
3082 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
3084 /* figure out the template */
3085 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
3086 result = create_recv_rtp (rtpbin, templ, pad_name);
3087 } else if (templ == gst_element_class_get_pad_template (klass,
3088 "recv_rtcp_sink_%u")) {
3089 result = create_recv_rtcp (rtpbin, templ, pad_name);
3090 } else if (templ == gst_element_class_get_pad_template (klass,
3091 "send_rtp_sink_%u")) {
3092 result = create_send_rtp (rtpbin, templ, pad_name);
3093 } else if (templ == gst_element_class_get_pad_template (klass,
3094 "send_rtcp_src_%u")) {
3095 result = create_rtcp (rtpbin, templ, pad_name);
3097 goto wrong_template;
3100 GST_RTP_BIN_UNLOCK (rtpbin);
3108 GST_RTP_BIN_UNLOCK (rtpbin);
3109 g_warning ("rtpbin: this is not our template");
3115 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
3117 GstRtpBinSession *session;
3120 g_return_if_fail (GST_IS_GHOST_PAD (pad));
3121 g_return_if_fail (GST_IS_RTP_BIN (element));
3123 rtpbin = GST_RTP_BIN (element);
3125 GST_RTP_BIN_LOCK (rtpbin);
3126 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
3127 GST_DEBUG_PAD_NAME (pad));
3129 if (!(session = find_session_by_pad (rtpbin, pad)))
3132 if (session->recv_rtp_sink_ghost == pad) {
3133 remove_recv_rtp (rtpbin, session);
3134 } else if (session->recv_rtcp_sink_ghost == pad) {
3135 remove_recv_rtcp (rtpbin, session);
3136 } else if (session->send_rtp_sink_ghost == pad) {
3137 remove_send_rtp (rtpbin, session);
3138 } else if (session->send_rtcp_src_ghost == pad) {
3139 remove_rtcp (rtpbin, session);
3142 /* no more request pads, free the complete session */
3143 if (session->recv_rtp_sink_ghost == NULL
3144 && session->recv_rtcp_sink_ghost == NULL
3145 && session->send_rtp_sink_ghost == NULL
3146 && session->send_rtcp_src_ghost == NULL) {
3147 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
3148 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
3149 free_session (session, rtpbin);
3151 GST_RTP_BIN_UNLOCK (rtpbin);
3158 GST_RTP_BIN_UNLOCK (rtpbin);
3159 g_warning ("rtpbin: %s:%s is not one of our request pads",
3160 GST_DEBUG_PAD_NAME (pad));