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 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
309 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
310 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
311 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
312 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
313 static void free_stream (GstRtpBinStream * stream);
315 /* Manages the RTP stream for one SSRC.
317 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
318 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
319 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
320 * together (see below).
322 struct _GstRtpBinStream
324 /* the SSRC of this stream */
330 /* the session this SSRC belongs to */
331 GstRtpBinSession *session;
333 /* the jitterbuffer of the SSRC */
335 gulong buffer_handlesync_sig;
336 gulong buffer_ptreq_sig;
337 gulong buffer_ntpstop_sig;
340 /* the PT demuxer of the SSRC */
342 gulong demux_newpad_sig;
343 gulong demux_padremoved_sig;
344 gulong demux_ptreq_sig;
345 gulong demux_ptchange_sig;
347 /* if we have calculated a valid rt_delta for this stream */
349 /* mapping to local RTP and NTP time */
352 /* base rtptime in gst time */
356 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
357 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
359 /* Manages the receiving end of the packets.
361 * There is one such structure for each RTP session (audio/video/...).
362 * We get the RTP/RTCP packets and stuff them into the session manager. From
363 * there they are pushed into an SSRC demuxer that splits the stream based on
364 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
365 * the GstRtpBinStream above).
367 struct _GstRtpBinSession
373 /* the session element */
375 /* the SSRC demuxer */
377 gulong demux_newpad_sig;
378 gulong demux_padremoved_sig;
382 /* list of GstRtpBinStream */
385 /* mapping of payload type to caps */
388 /* the pads of the session */
389 GstPad *recv_rtp_sink;
390 GstPad *recv_rtp_sink_ghost;
391 GstPad *recv_rtp_src;
392 GstPad *recv_rtcp_sink;
393 GstPad *recv_rtcp_sink_ghost;
395 GstPad *send_rtp_sink;
396 GstPad *send_rtp_sink_ghost;
397 GstPad *send_rtp_src;
398 GstPad *send_rtp_src_ghost;
399 GstPad *send_rtcp_src;
400 GstPad *send_rtcp_src_ghost;
403 /* Manages the RTP streams that come from one client and should therefore be
406 struct _GstRtpBinClient
408 /* the common CNAME for the streams */
417 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
418 static GstRtpBinSession *
419 find_session_by_id (GstRtpBin * rtpbin, gint id)
423 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
424 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
432 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
433 static GstRtpBinSession *
434 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
438 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
439 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
441 if ((sess->recv_rtp_sink_ghost == pad) ||
442 (sess->recv_rtcp_sink_ghost == pad) ||
443 (sess->send_rtp_sink_ghost == pad)
444 || (sess->send_rtcp_src_ghost == pad))
451 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
453 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
458 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
460 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
465 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
467 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
472 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
474 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
479 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
481 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
486 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
488 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
493 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
495 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
498 if (sess->bin->priv->autoremove)
499 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
503 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
505 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
508 if (sess->bin->priv->autoremove)
509 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
513 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
515 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
520 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
522 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
523 stream->session->id, stream->ssrc);
526 /* must be called with the SESSION lock */
527 static GstRtpBinStream *
528 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
532 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
533 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
535 if (stream->ssrc == ssrc)
542 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
543 GstRtpBinSession * session)
545 GstRtpBinStream *stream = NULL;
547 GST_RTP_SESSION_LOCK (session);
548 if ((stream = find_stream_by_ssrc (session, ssrc)))
549 session->streams = g_slist_remove (session->streams, stream);
550 GST_RTP_SESSION_UNLOCK (session);
553 free_stream (stream);
556 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
557 static GstRtpBinSession *
558 create_session (GstRtpBin * rtpbin, gint id)
560 GstRtpBinSession *sess;
561 GstElement *session, *demux;
564 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
567 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
570 sess = g_new0 (GstRtpBinSession, 1);
571 g_mutex_init (&sess->lock);
574 sess->session = session;
576 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
577 (GDestroyNotify) gst_caps_unref);
578 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
580 /* configure SDES items */
581 GST_OBJECT_LOCK (rtpbin);
582 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
583 rtpbin->use_pipeline_clock, NULL);
584 GST_OBJECT_UNLOCK (rtpbin);
586 /* provide clock_rate to the session manager when needed */
587 g_signal_connect (session, "request-pt-map",
588 (GCallback) pt_map_requested, sess);
590 g_signal_connect (sess->session, "on-new-ssrc",
591 (GCallback) on_new_ssrc, sess);
592 g_signal_connect (sess->session, "on-ssrc-collision",
593 (GCallback) on_ssrc_collision, sess);
594 g_signal_connect (sess->session, "on-ssrc-validated",
595 (GCallback) on_ssrc_validated, sess);
596 g_signal_connect (sess->session, "on-ssrc-active",
597 (GCallback) on_ssrc_active, sess);
598 g_signal_connect (sess->session, "on-ssrc-sdes",
599 (GCallback) on_ssrc_sdes, sess);
600 g_signal_connect (sess->session, "on-bye-ssrc",
601 (GCallback) on_bye_ssrc, sess);
602 g_signal_connect (sess->session, "on-bye-timeout",
603 (GCallback) on_bye_timeout, sess);
604 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
605 g_signal_connect (sess->session, "on-sender-timeout",
606 (GCallback) on_sender_timeout, sess);
608 gst_bin_add (GST_BIN_CAST (rtpbin), session);
609 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
611 GST_OBJECT_LOCK (rtpbin);
612 target = GST_STATE_TARGET (rtpbin);
613 GST_OBJECT_UNLOCK (rtpbin);
615 /* change state only to what's needed */
616 gst_element_set_state (demux, target);
617 gst_element_set_state (session, target);
624 g_warning ("rtpbin: could not create gstrtpsession element");
629 gst_object_unref (session);
630 g_warning ("rtpbin: could not create gstrtpssrcdemux element");
636 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
640 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
642 gst_element_set_locked_state (sess->demux, TRUE);
643 gst_element_set_locked_state (sess->session, TRUE);
645 gst_element_set_state (sess->demux, GST_STATE_NULL);
646 gst_element_set_state (sess->session, GST_STATE_NULL);
648 GST_RTP_BIN_LOCK (bin);
649 remove_recv_rtp (bin, sess);
650 remove_recv_rtcp (bin, sess);
651 remove_send_rtp (bin, sess);
652 remove_rtcp (bin, sess);
653 GST_RTP_BIN_UNLOCK (bin);
655 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
656 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
658 /* remove any references in bin->clients to the streams in sess->streams */
659 client_walk = bin->clients;
660 while (client_walk) {
661 GSList *client_node = client_walk;
662 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
663 GSList *stream_walk = client->streams;
665 while (stream_walk) {
666 GSList *stream_node = stream_walk;
667 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
670 stream_walk = g_slist_next (stream_walk);
672 for (inner_walk = sess->streams; inner_walk;
673 inner_walk = g_slist_next (inner_walk)) {
674 if ((GstRtpBinStream *) inner_walk->data == stream) {
675 client->streams = g_slist_delete_link (client->streams, stream_node);
681 client_walk = g_slist_next (client_walk);
683 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
684 && client->streams == 0));
685 if (client->nstreams == 0) {
686 free_client (client, bin);
687 bin->clients = g_slist_delete_link (bin->clients, client_node);
691 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
692 g_slist_free (sess->streams);
694 g_mutex_clear (&sess->lock);
695 g_hash_table_destroy (sess->ptmap);
700 /* get the payload type caps for the specific payload @pt in @session */
702 get_pt_map (GstRtpBinSession * session, guint pt)
704 GstCaps *caps = NULL;
707 GValue args[3] = { {0}, {0}, {0} };
709 GST_DEBUG ("searching pt %d in cache", pt);
711 GST_RTP_SESSION_LOCK (session);
713 /* first look in the cache */
714 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
722 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
724 /* not in cache, send signal to request caps */
725 g_value_init (&args[0], GST_TYPE_ELEMENT);
726 g_value_set_object (&args[0], bin);
727 g_value_init (&args[1], G_TYPE_UINT);
728 g_value_set_uint (&args[1], session->id);
729 g_value_init (&args[2], G_TYPE_UINT);
730 g_value_set_uint (&args[2], pt);
732 g_value_init (&ret, GST_TYPE_CAPS);
733 g_value_set_boxed (&ret, NULL);
735 GST_RTP_SESSION_UNLOCK (session);
737 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
739 GST_RTP_SESSION_LOCK (session);
741 g_value_unset (&args[0]);
742 g_value_unset (&args[1]);
743 g_value_unset (&args[2]);
745 /* look in the cache again because we let the lock go */
746 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
749 g_value_unset (&ret);
753 caps = (GstCaps *) g_value_dup_boxed (&ret);
754 g_value_unset (&ret);
758 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
760 /* store in cache, take additional ref */
761 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
762 gst_caps_ref (caps));
765 GST_RTP_SESSION_UNLOCK (session);
772 GST_RTP_SESSION_UNLOCK (session);
773 GST_DEBUG ("no pt map could be obtained");
779 return_true (gpointer key, gpointer value, gpointer user_data)
785 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
787 GSList *clients, *streams;
789 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
791 GST_RTP_BIN_LOCK (rtpbin);
792 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
793 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
795 /* reset sync on all streams for this client */
796 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
797 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
799 /* make use require a new SR packet for this stream before we attempt new
801 stream->have_sync = FALSE;
802 stream->rt_delta = 0;
803 stream->rtp_delta = 0;
804 stream->clock_base = -100 * GST_SECOND;
807 GST_RTP_BIN_UNLOCK (rtpbin);
811 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
813 GSList *sessions, *streams;
815 GST_RTP_BIN_LOCK (bin);
816 GST_DEBUG_OBJECT (bin, "clearing pt map");
817 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
818 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
820 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
821 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
823 GST_RTP_SESSION_LOCK (session);
824 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
826 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
827 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
829 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
830 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
832 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
834 GST_RTP_SESSION_UNLOCK (session);
836 GST_RTP_BIN_UNLOCK (bin);
839 gst_rtp_bin_reset_sync (bin);
843 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
845 RTPSession *internal_session = NULL;
846 GstRtpBinSession *session;
848 GST_RTP_BIN_LOCK (bin);
849 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
851 session = find_session_by_id (bin, (gint) session_id);
853 g_object_get (session->session, "internal-session", &internal_session,
856 GST_RTP_BIN_UNLOCK (bin);
858 return internal_session;
862 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
863 const gchar * name, const GValue * value)
865 GSList *sessions, *streams;
867 GST_RTP_BIN_LOCK (bin);
868 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
869 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
871 GST_RTP_SESSION_LOCK (session);
872 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
873 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
875 g_object_set_property (G_OBJECT (stream->buffer), name, value);
877 GST_RTP_SESSION_UNLOCK (session);
879 GST_RTP_BIN_UNLOCK (bin);
882 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
883 static GstRtpBinClient *
884 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
886 GstRtpBinClient *result = NULL;
889 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
890 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
892 if (len != client->cname_len)
895 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
896 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
903 /* nothing found, create one */
904 if (result == NULL) {
905 result = g_new0 (GstRtpBinClient, 1);
906 result->cname = g_strndup ((gchar *) data, len);
907 result->cname_len = len;
908 bin->clients = g_slist_prepend (bin->clients, result);
909 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
916 free_client (GstRtpBinClient * client, GstRtpBin * bin)
918 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
919 g_slist_free (client->streams);
920 g_free (client->cname);
925 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
930 GstClockTime base_time, rt, clock_time;
932 GST_OBJECT_LOCK (bin);
933 if ((clock = GST_ELEMENT_CLOCK (bin))) {
934 base_time = GST_ELEMENT_CAST (bin)->base_time;
935 gst_object_ref (clock);
936 GST_OBJECT_UNLOCK (bin);
938 clock_time = gst_clock_get_time (clock);
940 if (bin->use_pipeline_clock) {
945 /* get current NTP time */
946 g_get_current_time (¤t);
947 ntpns = GST_TIMEVAL_TO_TIME (current);
950 /* add constant to convert from 1970 based time to 1900 based time */
951 ntpns += (2208988800LL * GST_SECOND);
953 /* get current clock time and convert to running time */
954 rt = clock_time - base_time;
956 gst_object_unref (clock);
958 GST_OBJECT_UNLOCK (bin);
969 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
972 gint64 prev_ts_offset;
974 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
976 /* delta changed, see how much */
977 if (prev_ts_offset != ts_offset) {
980 diff = prev_ts_offset - ts_offset;
982 GST_DEBUG_OBJECT (bin,
983 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
984 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
986 /* only change diff when it changed more than 4 milliseconds. This
987 * compensates for rounding errors in NTP to RTP timestamp
989 if (ABS (diff) > 4 * GST_MSECOND) {
990 if (ABS (diff) < (3 * GST_SECOND)) {
991 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
993 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
996 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
999 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1000 stream->ssrc, ts_offset);
1003 /* associate a stream to the given CNAME. This will make sure all streams for
1004 * that CNAME are synchronized together.
1005 * Must be called with GST_RTP_BIN_LOCK */
1007 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1008 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1009 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1010 gint64 rtp_clock_base)
1012 GstRtpBinClient *client;
1017 GstClockTime running_time;
1019 gint64 ntpdiff, rtdiff;
1022 /* first find or create the CNAME */
1023 client = get_client (bin, len, data, &created);
1025 /* find stream in the client */
1026 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1027 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1029 if (ostream == stream)
1032 /* not found, add it to the list */
1034 GST_DEBUG_OBJECT (bin,
1035 "new association of SSRC %08x with client %p with CNAME %s",
1036 stream->ssrc, client, client->cname);
1037 client->streams = g_slist_prepend (client->streams, stream);
1040 GST_DEBUG_OBJECT (bin,
1041 "found association of SSRC %08x with client %p with CNAME %s",
1042 stream->ssrc, client, client->cname);
1045 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1046 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1047 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1048 /* we don't need that data, so carry on,
1049 * but make some values look saner */
1050 last_extrtptime = base_rtptime;
1052 /* nothing we can do with this data in this case */
1053 GST_DEBUG_OBJECT (bin, "bailing out");
1058 /* Take the extended rtptime we found in the SR packet and map it to the
1059 * local rtptime. The local rtp time is used to construct timestamps on the
1060 * buffers so we will calculate what running_time corresponds to the RTP
1061 * timestamp in the SR packet. */
1062 local_rtp = last_extrtptime - base_rtptime;
1064 GST_DEBUG_OBJECT (bin,
1065 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1066 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1067 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1068 last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
1070 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1071 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1072 * into a corresponding gstreamer timestamp. Note that the base_time also
1073 * contains the drift between sender and receiver. */
1074 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1075 local_rt += base_time;
1077 /* convert ntptime to unix time since 1900 */
1078 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1079 (G_GINT64_CONSTANT (1) << 32));
1081 stream->have_sync = TRUE;
1083 GST_DEBUG_OBJECT (bin,
1084 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1085 local_rt, last_unix);
1087 /* recalc inter stream playout offset, but only if there is more than one
1088 * stream or we're doing NTP sync. */
1089 if (bin->ntp_sync) {
1090 /* For NTP sync we need to first get a snapshot of running_time and NTP
1091 * time. We know at what running_time we play a certain RTP time, we also
1092 * calculated when we would play the RTP time in the SR packet. Now we need
1093 * to know how the running_time and the NTP time relate to eachother. */
1094 get_current_times (bin, &running_time, &ntpnstime);
1096 /* see how far away the NTP time is. This is the difference between the
1097 * current NTP time and the NTP time in the last SR packet. */
1098 ntpdiff = ntpnstime - last_unix;
1099 /* see how far away the running_time is. This is the difference between the
1100 * current running_time and the running_time of the RTP timestamp in the
1101 * last SR packet. */
1102 rtdiff = running_time - local_rt;
1104 GST_DEBUG_OBJECT (bin,
1105 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1106 ntpnstime, last_unix);
1107 GST_DEBUG_OBJECT (bin,
1108 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1111 /* combine to get the final diff to apply to the running_time */
1112 stream->rt_delta = rtdiff - ntpdiff;
1114 stream_set_ts_offset (bin, stream, stream->rt_delta);
1116 gint64 min, rtp_min, clock_base = stream->clock_base;
1117 gboolean all_sync, use_rtp;
1118 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1120 /* calculate delta between server and receiver. last_unix is created by
1121 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1122 * delta expresses the difference to our timeline and the server timeline. The
1123 * difference in itself doesn't mean much but we can combine the delta of
1124 * multiple streams to create a stream specific offset. */
1125 stream->rt_delta = last_unix - local_rt;
1127 /* calculate the min of all deltas, ignoring streams that did not yet have a
1128 * valid rt_delta because we did not yet receive an SR packet for those
1130 * We calculate the mininum because we would like to only apply positive
1131 * offsets to streams, delaying their playback instead of trying to speed up
1132 * other streams (which might be imposible when we have to create negative
1134 * The stream that has the smallest diff is selected as the reference stream,
1135 * all other streams will have a positive offset to this difference. */
1137 /* some alternative setting allow ignoring RTCP as much as possible,
1138 * for servers generating bogus ntp timeline */
1139 min = rtp_min = G_MAXINT64;
1141 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1145 /* signed version for convienience */
1146 clock_base = base_rtptime;
1147 /* deal with possible wrap-around */
1148 ext_base = base_rtptime;
1149 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1150 /* sanity check; base rtp and provided clock_base should be close */
1151 if (rtp_clock_base >= clock_base) {
1152 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1153 rtp_clock_base = base_time +
1154 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1155 GST_SECOND, clock_rate);
1160 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1161 rtp_clock_base = base_time -
1162 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1163 GST_SECOND, clock_rate);
1168 /* warn and bail for clarity out if no sane values */
1170 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1173 /* store to track changes */
1174 clock_base = rtp_clock_base;
1175 /* generate a fake as before,
1176 * now equating rtptime obtained from RTP-Info,
1177 * where the large time represent the otherwise irrelevant npt/ntp time */
1178 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1180 clock_base = rtp_clock_base;
1184 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1185 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1187 if (!ostream->have_sync) {
1192 /* change in current stream's base from previously init'ed value
1193 * leads to reset of all stream's base */
1194 if (stream != ostream && stream->clock_base >= 0 &&
1195 (stream->clock_base != clock_base)) {
1196 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1197 ostream->clock_base = -100 * GST_SECOND;
1198 ostream->rtp_delta = 0;
1201 if (ostream->rt_delta < min)
1202 min = ostream->rt_delta;
1203 if (ostream->rtp_delta < rtp_min)
1204 rtp_min = ostream->rtp_delta;
1207 /* arrange to re-sync for each stream upon significant change,
1209 all_sync = all_sync && (stream->clock_base == clock_base);
1210 stream->clock_base = clock_base;
1212 /* may need init performed above later on, but nothing more to do now */
1213 if (client->nstreams <= 1)
1216 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1217 " all sync %d", client, min, all_sync);
1218 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1220 switch (rtcp_sync) {
1221 case GST_RTP_BIN_RTCP_SYNC_RTP:
1224 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1225 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1227 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1228 /* if all have been synced already, do not bother further */
1230 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1238 /* bail out if we adjusted recently enough */
1239 if (all_sync && (last_unix - bin->priv->last_unix) <
1240 bin->rtcp_sync_interval * GST_MSECOND) {
1241 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1242 "previous sender info too recent "
1243 "(previous UNIX %" G_GUINT64_FORMAT ")", bin->priv->last_unix);
1246 bin->priv->last_unix = last_unix;
1248 /* calculate offsets for each stream */
1249 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1250 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1253 /* ignore streams for which we didn't receive an SR packet yet, we
1254 * can't synchronize them yet. We can however sync other streams just
1256 if (!ostream->have_sync)
1259 /* calculate offset to our reference stream, this should always give a
1260 * positive number. */
1262 ts_offset = ostream->rtp_delta - rtp_min;
1264 ts_offset = ostream->rt_delta - min;
1266 stream_set_ts_offset (bin, ostream, ts_offset);
1272 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1273 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1274 (b) = gst_rtcp_packet_move_to_next ((packet)))
1276 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1277 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1278 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1280 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1281 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1282 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1285 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1286 GstRtpBinStream * stream)
1289 GstRTCPPacket packet;
1292 gboolean have_sr, have_sdes;
1294 guint64 base_rtptime;
1300 GstRTCPBuffer rtcp = { NULL, };
1304 GST_DEBUG_OBJECT (bin, "sync handler called");
1306 /* get the last relation between the rtp timestamps and the gstreamer
1307 * timestamps. We get this info directly from the jitterbuffer which
1308 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1309 * what the current situation is. */
1311 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1312 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1313 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1314 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1316 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1317 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1322 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1324 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1325 /* first packet must be SR or RR or else the validate would have failed */
1326 switch (gst_rtcp_packet_get_type (&packet)) {
1327 case GST_RTCP_TYPE_SR:
1328 /* only parse first. There is only supposed to be one SR in the packet
1329 * but we will deal with malformed packets gracefully */
1332 /* get NTP and RTP times */
1333 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1336 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1337 /* ignore SR that is not ours */
1338 if (ssrc != stream->ssrc)
1343 case GST_RTCP_TYPE_SDES:
1345 gboolean more_items, more_entries;
1347 /* only deal with first SDES, there is only supposed to be one SDES in
1348 * the RTCP packet but we deal with bad packets gracefully. Also bail
1349 * out if we have not seen an SR item yet. */
1350 if (have_sdes || !have_sr)
1353 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1354 /* skip items that are not about the SSRC of the sender */
1355 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1358 /* find the CNAME entry */
1359 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1360 GstRTCPSDESType type;
1364 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1366 if (type == GST_RTCP_SDES_CNAME) {
1367 GST_RTP_BIN_LOCK (bin);
1368 /* associate the stream to CNAME */
1369 gst_rtp_bin_associate (bin, stream, len, data,
1370 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1372 GST_RTP_BIN_UNLOCK (bin);
1380 /* we can ignore these packets */
1384 gst_rtcp_buffer_unmap (&rtcp);
1387 /* create a new stream with @ssrc in @session. Must be called with
1388 * RTP_SESSION_LOCK. */
1389 static GstRtpBinStream *
1390 create_stream (GstRtpBinSession * session, guint32 ssrc)
1392 GstElement *buffer, *demux = NULL;
1393 GstRtpBinStream *stream;
1397 rtpbin = session->bin;
1399 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1400 goto no_jitterbuffer;
1402 if (!rtpbin->ignore_pt)
1403 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1407 stream = g_new0 (GstRtpBinStream, 1);
1408 stream->ssrc = ssrc;
1409 stream->bin = rtpbin;
1410 stream->session = session;
1411 stream->buffer = buffer;
1412 stream->demux = demux;
1414 stream->have_sync = FALSE;
1415 stream->rt_delta = 0;
1416 stream->rtp_delta = 0;
1417 stream->percent = 100;
1418 stream->clock_base = -100 * GST_SECOND;
1419 session->streams = g_slist_prepend (session->streams, stream);
1421 /* provide clock_rate to the jitterbuffer when needed */
1422 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1423 (GCallback) pt_map_requested, session);
1424 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1425 (GCallback) on_npt_stop, stream);
1427 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1428 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1430 /* configure latency and packet lost */
1431 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1432 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1433 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1435 if (!rtpbin->ignore_pt)
1436 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1437 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1441 gst_element_link (buffer, demux);
1443 if (rtpbin->buffering) {
1446 GST_INFO_OBJECT (rtpbin,
1447 "bin is buffering, set jitterbuffer as not active");
1448 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1452 GST_OBJECT_LOCK (rtpbin);
1453 target = GST_STATE_TARGET (rtpbin);
1454 GST_OBJECT_UNLOCK (rtpbin);
1456 /* from sink to source */
1458 gst_element_set_state (demux, target);
1460 gst_element_set_state (buffer, target);
1467 g_warning ("rtpbin: could not create gstrtpjitterbuffer element");
1472 gst_object_unref (buffer);
1473 g_warning ("rtpbin: could not create gstrtpptdemux element");
1479 free_stream (GstRtpBinStream * stream)
1481 GstRtpBinSession *session;
1483 session = stream->session;
1485 if (stream->demux) {
1486 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1487 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1488 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1490 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1491 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1492 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1495 gst_element_set_locked_state (stream->demux, TRUE);
1496 gst_element_set_locked_state (stream->buffer, TRUE);
1499 gst_element_set_state (stream->demux, GST_STATE_NULL);
1500 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1502 /* now remove this signal, we need this while going to NULL because it to
1503 * do some cleanups */
1505 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1507 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1509 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1514 /* GObject vmethods */
1515 static void gst_rtp_bin_dispose (GObject * object);
1516 static void gst_rtp_bin_finalize (GObject * object);
1517 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1518 const GValue * value, GParamSpec * pspec);
1519 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1520 GValue * value, GParamSpec * pspec);
1522 /* GstElement vmethods */
1523 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1524 GstStateChange transition);
1525 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1526 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1527 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1528 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1530 #define gst_rtp_bin_parent_class parent_class
1531 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1534 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1536 GObjectClass *gobject_class;
1537 GstElementClass *gstelement_class;
1538 GstBinClass *gstbin_class;
1540 gobject_class = (GObjectClass *) klass;
1541 gstelement_class = (GstElementClass *) klass;
1542 gstbin_class = (GstBinClass *) klass;
1544 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1546 gobject_class->dispose = gst_rtp_bin_dispose;
1547 gobject_class->finalize = gst_rtp_bin_finalize;
1548 gobject_class->set_property = gst_rtp_bin_set_property;
1549 gobject_class->get_property = gst_rtp_bin_get_property;
1551 g_object_class_install_property (gobject_class, PROP_LATENCY,
1552 g_param_spec_uint ("latency", "Buffer latency in ms",
1553 "Default amount of ms to buffer in the jitterbuffers", 0,
1554 G_MAXUINT, DEFAULT_LATENCY_MS,
1555 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1558 * GstRtpBin::request-pt-map:
1559 * @rtpbin: the object which received the signal
1560 * @session: the session
1563 * Request the payload type as #GstCaps for @pt in @session.
1565 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1566 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1567 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1568 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1569 G_TYPE_UINT, G_TYPE_UINT);
1572 * GstRtpBin::payload-type-change:
1573 * @rtpbin: the object which received the signal
1574 * @session: the session
1577 * Signal that the current payload type changed to @pt in @session.
1581 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1582 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1583 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1584 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1585 G_TYPE_UINT, G_TYPE_UINT);
1588 * GstRtpBin::clear-pt-map:
1589 * @rtpbin: the object which received the signal
1591 * Clear all previously cached pt-mapping obtained with
1592 * #GstRtpBin::request-pt-map.
1594 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1595 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1596 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1597 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1601 * GstRtpBin::reset-sync:
1602 * @rtpbin: the object which received the signal
1604 * Reset all currently configured lip-sync parameters and require new SR
1605 * packets for all streams before lip-sync is attempted again.
1607 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1608 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1609 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1610 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1614 * GstRtpBin::get-internal-session:
1615 * @rtpbin: the object which received the signal
1616 * @id: the session id
1618 * Request the internal RTPSession object as #GObject in session @id.
1620 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1621 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1622 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1623 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1624 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1627 * GstRtpBin::on-new-ssrc:
1628 * @rtpbin: the object which received the signal
1629 * @session: the session
1632 * Notify of a new SSRC that entered @session.
1634 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1635 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1636 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1637 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1638 G_TYPE_UINT, G_TYPE_UINT);
1640 * GstRtpBin::on-ssrc-collision:
1641 * @rtpbin: the object which received the signal
1642 * @session: the session
1645 * Notify when we have an SSRC collision
1647 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1648 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1649 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1650 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1651 G_TYPE_UINT, G_TYPE_UINT);
1653 * GstRtpBin::on-ssrc-validated:
1654 * @rtpbin: the object which received the signal
1655 * @session: the session
1658 * Notify of a new SSRC that became validated.
1660 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1661 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1662 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1663 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1664 G_TYPE_UINT, G_TYPE_UINT);
1666 * GstRtpBin::on-ssrc-active:
1667 * @rtpbin: the object which received the signal
1668 * @session: the session
1671 * Notify of a SSRC that is active, i.e., sending RTCP.
1673 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1674 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1675 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1676 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1677 G_TYPE_UINT, G_TYPE_UINT);
1679 * GstRtpBin::on-ssrc-sdes:
1680 * @rtpbin: the object which received the signal
1681 * @session: the session
1684 * Notify of a SSRC that is active, i.e., sending RTCP.
1686 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1687 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1688 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1689 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1690 G_TYPE_UINT, G_TYPE_UINT);
1693 * GstRtpBin::on-bye-ssrc:
1694 * @rtpbin: the object which received the signal
1695 * @session: the session
1698 * Notify of an SSRC that became inactive because of a BYE packet.
1700 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1701 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1702 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1703 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1704 G_TYPE_UINT, G_TYPE_UINT);
1706 * GstRtpBin::on-bye-timeout:
1707 * @rtpbin: the object which received the signal
1708 * @session: the session
1711 * Notify of an SSRC that has timed out because of BYE
1713 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1714 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1715 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1716 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1717 G_TYPE_UINT, G_TYPE_UINT);
1719 * GstRtpBin::on-timeout:
1720 * @rtpbin: the object which received the signal
1721 * @session: the session
1724 * Notify of an SSRC that has timed out
1726 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1727 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1728 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1729 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1730 G_TYPE_UINT, G_TYPE_UINT);
1732 * GstRtpBin::on-sender-timeout:
1733 * @rtpbin: the object which received the signal
1734 * @session: the session
1737 * Notify of a sender SSRC that has timed out and became a receiver
1739 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1740 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1741 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1742 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1743 G_TYPE_UINT, G_TYPE_UINT);
1746 * GstRtpBin::on-npt-stop:
1747 * @rtpbin: the object which received the signal
1748 * @session: the session
1751 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1753 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1754 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1755 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1756 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1757 G_TYPE_UINT, G_TYPE_UINT);
1759 g_object_class_install_property (gobject_class, PROP_SDES,
1760 g_param_spec_boxed ("sdes", "SDES",
1761 "The SDES items of this session",
1762 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1764 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1765 g_param_spec_boolean ("do-lost", "Do Lost",
1766 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1767 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1769 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1770 g_param_spec_boolean ("autoremove", "Auto Remove",
1771 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1772 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1774 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1775 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1776 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1777 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1779 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1780 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1781 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1782 DEFAULT_USE_PIPELINE_CLOCK,
1783 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1785 * GstRtpBin::buffer-mode:
1787 * Control the buffering and timestamping mode used by the jitterbuffer.
1791 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1792 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1793 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1794 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1796 * GstRtpBin::ntp-sync:
1798 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1799 * between the receivers and the senders (such as when using ntpd) this option
1800 * can be used to synchronize receivers on multiple machines.
1804 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1805 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1806 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1807 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1810 * GstRtpBin::rtcp-sync:
1812 * If not synchronizing (directly) to the NTP clock, determines how to sync
1813 * the various streams.
1817 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
1818 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
1819 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
1820 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1823 * GstRtpBin::rtcp-sync-interval:
1825 * Determines how often to sync streams using RTCP data.
1829 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
1830 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
1831 "RTCP SR interval synchronization (ms) (0 = always)",
1832 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
1833 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1835 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1836 gstelement_class->request_new_pad =
1837 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1838 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1841 gst_element_class_add_pad_template (gstelement_class,
1842 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1843 gst_element_class_add_pad_template (gstelement_class,
1844 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1845 gst_element_class_add_pad_template (gstelement_class,
1846 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1849 gst_element_class_add_pad_template (gstelement_class,
1850 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1851 gst_element_class_add_pad_template (gstelement_class,
1852 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1853 gst_element_class_add_pad_template (gstelement_class,
1854 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1856 gst_element_class_set_details_simple (gstelement_class, "RTP Bin",
1857 "Filter/Network/RTP",
1858 "Real-Time Transport Protocol bin",
1859 "Wim Taymans <wim.taymans@gmail.com>");
1861 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1863 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1864 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1865 klass->get_internal_session =
1866 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1868 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1872 gst_rtp_bin_init (GstRtpBin * rtpbin)
1876 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1877 g_mutex_init (&rtpbin->priv->bin_lock);
1878 g_mutex_init (&rtpbin->priv->dyn_lock);
1880 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1881 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1882 rtpbin->do_lost = DEFAULT_DO_LOST;
1883 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1884 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1885 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
1886 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
1887 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1888 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1889 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1891 /* some default SDES entries */
1892 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
1893 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1894 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
1899 gst_rtp_bin_dispose (GObject * object)
1903 rtpbin = GST_RTP_BIN (object);
1905 GST_DEBUG_OBJECT (object, "freeing sessions");
1906 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1907 g_slist_free (rtpbin->sessions);
1908 rtpbin->sessions = NULL;
1909 GST_DEBUG_OBJECT (object, "freeing clients");
1910 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1911 g_slist_free (rtpbin->clients);
1912 rtpbin->clients = NULL;
1914 G_OBJECT_CLASS (parent_class)->dispose (object);
1918 gst_rtp_bin_finalize (GObject * object)
1922 rtpbin = GST_RTP_BIN (object);
1925 gst_structure_free (rtpbin->sdes);
1927 g_mutex_clear (&rtpbin->priv->bin_lock);
1928 g_mutex_clear (&rtpbin->priv->dyn_lock);
1930 G_OBJECT_CLASS (parent_class)->finalize (object);
1935 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1942 GST_RTP_BIN_LOCK (bin);
1944 GST_OBJECT_LOCK (bin);
1946 gst_structure_free (bin->sdes);
1947 bin->sdes = gst_structure_copy (sdes);
1948 GST_OBJECT_UNLOCK (bin);
1950 /* store in all sessions */
1951 for (item = bin->sessions; item; item = g_slist_next (item)) {
1952 GstRtpBinSession *session = item->data;
1953 g_object_set (session->session, "sdes", sdes, NULL);
1956 GST_RTP_BIN_UNLOCK (bin);
1959 static GstStructure *
1960 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1962 GstStructure *result;
1964 GST_OBJECT_LOCK (bin);
1965 result = gst_structure_copy (bin->sdes);
1966 GST_OBJECT_UNLOCK (bin);
1972 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1973 const GValue * value, GParamSpec * pspec)
1977 rtpbin = GST_RTP_BIN (object);
1981 GST_RTP_BIN_LOCK (rtpbin);
1982 rtpbin->latency_ms = g_value_get_uint (value);
1983 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1984 GST_RTP_BIN_UNLOCK (rtpbin);
1985 /* propagate the property down to the jitterbuffer */
1986 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1989 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1992 GST_RTP_BIN_LOCK (rtpbin);
1993 rtpbin->do_lost = g_value_get_boolean (value);
1994 GST_RTP_BIN_UNLOCK (rtpbin);
1995 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1998 rtpbin->ntp_sync = g_value_get_boolean (value);
2000 case PROP_RTCP_SYNC:
2001 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2003 case PROP_RTCP_SYNC_INTERVAL:
2004 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2006 case PROP_IGNORE_PT:
2007 rtpbin->ignore_pt = g_value_get_boolean (value);
2009 case PROP_AUTOREMOVE:
2010 rtpbin->priv->autoremove = g_value_get_boolean (value);
2012 case PROP_USE_PIPELINE_CLOCK:
2015 GST_RTP_BIN_LOCK (rtpbin);
2016 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2017 for (sessions = rtpbin->sessions; sessions;
2018 sessions = g_slist_next (sessions)) {
2019 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2021 g_object_set (G_OBJECT (session->session),
2022 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2024 GST_RTP_BIN_UNLOCK (rtpbin);
2027 case PROP_BUFFER_MODE:
2028 GST_RTP_BIN_LOCK (rtpbin);
2029 rtpbin->buffer_mode = g_value_get_enum (value);
2030 GST_RTP_BIN_UNLOCK (rtpbin);
2031 /* propagate the property down to the jitterbuffer */
2032 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2035 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2041 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2042 GValue * value, GParamSpec * pspec)
2046 rtpbin = GST_RTP_BIN (object);
2050 GST_RTP_BIN_LOCK (rtpbin);
2051 g_value_set_uint (value, rtpbin->latency_ms);
2052 GST_RTP_BIN_UNLOCK (rtpbin);
2055 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2058 GST_RTP_BIN_LOCK (rtpbin);
2059 g_value_set_boolean (value, rtpbin->do_lost);
2060 GST_RTP_BIN_UNLOCK (rtpbin);
2062 case PROP_IGNORE_PT:
2063 g_value_set_boolean (value, rtpbin->ignore_pt);
2066 g_value_set_boolean (value, rtpbin->ntp_sync);
2068 case PROP_RTCP_SYNC:
2069 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2071 case PROP_RTCP_SYNC_INTERVAL:
2072 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2074 case PROP_AUTOREMOVE:
2075 g_value_set_boolean (value, rtpbin->priv->autoremove);
2077 case PROP_BUFFER_MODE:
2078 g_value_set_enum (value, rtpbin->buffer_mode);
2080 case PROP_USE_PIPELINE_CLOCK:
2081 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2084 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2090 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
2094 rtpbin = GST_RTP_BIN (bin);
2096 switch (GST_MESSAGE_TYPE (message)) {
2097 case GST_MESSAGE_ELEMENT:
2099 const GstStructure *s = gst_message_get_structure (message);
2101 /* we change the structure name and add the session ID to it */
2102 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
2103 GstRtpBinSession *sess;
2105 /* find the session we set it as object data */
2106 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2107 "GstRTPBin.session");
2109 if (G_LIKELY (sess)) {
2110 message = gst_message_make_writable (message);
2111 s = gst_message_get_structure (message);
2112 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
2116 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2119 case GST_MESSAGE_BUFFERING:
2122 gint min_percent = 100;
2123 GSList *sessions, *streams;
2124 GstRtpBinStream *stream;
2125 gboolean change = FALSE, active = FALSE;
2126 GstClockTime min_out_time;
2127 GstBufferingMode mode;
2128 gint avg_in, avg_out;
2129 gint64 buffering_left;
2131 gst_message_parse_buffering (message, &percent);
2132 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
2136 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2137 "GstRTPBin.stream");
2139 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
2141 /* get the stream */
2142 if (G_LIKELY (stream)) {
2143 GST_RTP_BIN_LOCK (rtpbin);
2144 /* fill in the percent */
2145 stream->percent = percent;
2147 /* calculate the min value for all streams */
2148 for (sessions = rtpbin->sessions; sessions;
2149 sessions = g_slist_next (sessions)) {
2150 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2152 GST_RTP_SESSION_LOCK (session);
2153 if (session->streams) {
2154 for (streams = session->streams; streams;
2155 streams = g_slist_next (streams)) {
2156 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2158 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
2161 /* find min percent */
2162 if (min_percent > stream->percent)
2163 min_percent = stream->percent;
2166 GST_INFO_OBJECT (bin,
2167 "session has no streams, setting min_percent to 0");
2170 GST_RTP_SESSION_UNLOCK (session);
2172 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
2174 if (rtpbin->buffering) {
2175 if (min_percent == 100) {
2176 rtpbin->buffering = FALSE;
2181 if (min_percent < 100) {
2182 /* pause the streams */
2183 rtpbin->buffering = TRUE;
2188 GST_RTP_BIN_UNLOCK (rtpbin);
2190 gst_message_unref (message);
2192 /* make a new buffering message with the min value */
2194 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2195 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2198 if (G_UNLIKELY (change)) {
2200 guint64 running_time = 0;
2203 /* figure out the running time when we have a clock */
2204 if (G_LIKELY ((clock =
2205 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2206 guint64 now, base_time;
2208 now = gst_clock_get_time (clock);
2209 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2210 running_time = now - base_time;
2211 gst_object_unref (clock);
2213 GST_DEBUG_OBJECT (bin,
2214 "running time now %" GST_TIME_FORMAT,
2215 GST_TIME_ARGS (running_time));
2217 GST_RTP_BIN_LOCK (rtpbin);
2219 /* when we reactivate, calculate the offsets so that all streams have
2220 * an output time that is at least as big as the running_time */
2223 if (running_time > rtpbin->buffer_start) {
2224 offset = running_time - rtpbin->buffer_start;
2225 if (offset >= rtpbin->latency_ns)
2226 offset -= rtpbin->latency_ns;
2232 /* pause all streams */
2234 for (sessions = rtpbin->sessions; sessions;
2235 sessions = g_slist_next (sessions)) {
2236 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2238 GST_RTP_SESSION_LOCK (session);
2239 for (streams = session->streams; streams;
2240 streams = g_slist_next (streams)) {
2241 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2242 GstElement *element = stream->buffer;
2245 g_signal_emit_by_name (element, "set-active", active, offset,
2249 g_object_get (element, "percent", &stream->percent, NULL);
2253 if (min_out_time == -1 || last_out < min_out_time)
2254 min_out_time = last_out;
2257 GST_DEBUG_OBJECT (bin,
2258 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2259 GST_TIME_FORMAT ", percent %d", element, active,
2260 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2263 GST_RTP_SESSION_UNLOCK (session);
2265 GST_DEBUG_OBJECT (bin,
2266 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2268 /* the buffer_start is the min out time of all paused jitterbuffers */
2270 rtpbin->buffer_start = min_out_time;
2272 GST_RTP_BIN_UNLOCK (rtpbin);
2275 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2280 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2286 static GstStateChangeReturn
2287 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2289 GstStateChangeReturn res;
2291 GstRtpBinPrivate *priv;
2293 rtpbin = GST_RTP_BIN (element);
2294 priv = rtpbin->priv;
2296 switch (transition) {
2297 case GST_STATE_CHANGE_NULL_TO_READY:
2299 case GST_STATE_CHANGE_READY_TO_PAUSED:
2300 priv->last_unix = 0;
2301 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2302 g_atomic_int_set (&priv->shutdown, 0);
2304 case GST_STATE_CHANGE_PAUSED_TO_READY:
2305 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2306 g_atomic_int_set (&priv->shutdown, 1);
2307 /* wait for all callbacks to end by taking the lock. No new callbacks will
2308 * be able to happen as we set the shutdown flag. */
2309 GST_RTP_BIN_DYN_LOCK (rtpbin);
2310 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2311 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2317 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2319 switch (transition) {
2320 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2322 case GST_STATE_CHANGE_PAUSED_TO_READY:
2324 case GST_STATE_CHANGE_READY_TO_NULL:
2332 /* a new pad (SSRC) was created in @session. This signal is emited from the
2333 * payload demuxer. */
2335 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2336 GstRtpBinStream * stream)
2339 GstElementClass *klass;
2340 GstPadTemplate *templ;
2344 rtpbin = stream->bin;
2346 GST_DEBUG ("new payload pad %d", pt);
2348 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2350 /* ghost the pad to the parent */
2351 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2352 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2353 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2354 stream->session->id, stream->ssrc, pt);
2355 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2357 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2359 gst_pad_set_active (gpad, TRUE);
2360 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2362 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2368 GST_DEBUG ("ignoring, we are shutting down");
2374 payload_pad_removed (GstElement * element, GstPad * pad,
2375 GstRtpBinStream * stream)
2380 rtpbin = stream->bin;
2382 GST_DEBUG ("payload pad removed");
2384 GST_RTP_BIN_DYN_LOCK (rtpbin);
2385 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2386 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2388 gst_pad_set_active (gpad, FALSE);
2389 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2391 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2395 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2400 rtpbin = session->bin;
2402 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2405 caps = get_pt_map (session, pt);
2414 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2420 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2422 GST_DEBUG_OBJECT (session->bin,
2423 "emiting signal for pt type changed to %d in session %d", pt,
2426 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2427 0, session->id, pt);
2430 /* emited when caps changed for the session */
2432 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2437 const GstStructure *s;
2441 g_object_get (pad, "caps", &caps, NULL);
2446 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2448 s = gst_caps_get_structure (caps, 0);
2450 /* get payload, finish when it's not there */
2451 if (!gst_structure_get_int (s, "payload", &payload))
2454 GST_RTP_SESSION_LOCK (session);
2455 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2456 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2457 GST_RTP_SESSION_UNLOCK (session);
2460 /* a new pad (SSRC) was created in @session */
2462 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2463 GstRtpBinSession * session)
2466 GstRtpBinStream *stream;
2467 GstPad *sinkpad, *srcpad;
2470 rtpbin = session->bin;
2472 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2473 GST_DEBUG_PAD_NAME (pad));
2475 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2477 GST_RTP_SESSION_LOCK (session);
2479 /* create new stream */
2480 stream = create_stream (session, ssrc);
2484 /* get pad and link */
2485 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2486 padname = g_strdup_printf ("src_%u", ssrc);
2487 srcpad = gst_element_get_static_pad (element, padname);
2489 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2490 gst_pad_link (srcpad, sinkpad);
2491 gst_object_unref (sinkpad);
2492 gst_object_unref (srcpad);
2494 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2495 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
2496 srcpad = gst_element_get_static_pad (element, padname);
2498 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2499 gst_pad_link (srcpad, sinkpad);
2500 gst_object_unref (sinkpad);
2501 gst_object_unref (srcpad);
2503 /* connect to the RTCP sync signal from the jitterbuffer */
2504 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2505 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2506 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2508 if (stream->demux) {
2509 /* connect to the new-pad signal of the payload demuxer, this will expose the
2510 * new pad by ghosting it. */
2511 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2512 "new-payload-type", (GCallback) new_payload_found, stream);
2513 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2514 "pad-removed", (GCallback) payload_pad_removed, stream);
2516 /* connect to the request-pt-map signal. This signal will be emited by the
2517 * demuxer so that it can apply a proper caps on the buffers for the
2519 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2520 "request-pt-map", (GCallback) pt_map_requested, session);
2521 /* connect to the signal so it can be forwarded. */
2522 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2523 "payload-type-change", (GCallback) payload_type_change, session);
2525 /* add gstrtpjitterbuffer src pad to pads */
2526 GstElementClass *klass;
2527 GstPadTemplate *templ;
2531 pad = gst_element_get_static_pad (stream->buffer, "src");
2533 /* ghost the pad to the parent */
2534 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2535 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2536 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2537 stream->session->id, stream->ssrc, 255);
2538 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2541 gst_pad_set_active (gpad, TRUE);
2542 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2544 gst_object_unref (pad);
2547 GST_RTP_SESSION_UNLOCK (session);
2548 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2555 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2560 GST_RTP_SESSION_UNLOCK (session);
2561 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2562 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2567 /* Create a pad for receiving RTP for the session in @name. Must be called with
2571 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2575 GstRtpBinSession *session;
2576 GstPadLinkReturn lres;
2578 /* first get the session number */
2579 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
2582 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2584 /* get or create session */
2585 session = find_session_by_id (rtpbin, sessid);
2587 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2588 /* create session now */
2589 session = create_session (rtpbin, sessid);
2590 if (session == NULL)
2594 /* check if pad was requested */
2595 if (session->recv_rtp_sink_ghost != NULL)
2596 return session->recv_rtp_sink_ghost;
2598 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2599 /* get recv_rtp pad and store */
2600 session->recv_rtp_sink =
2601 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2602 if (session->recv_rtp_sink == NULL)
2605 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2606 (GCallback) caps_changed, session);
2608 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2609 /* get srcpad, link to SSRCDemux */
2610 session->recv_rtp_src =
2611 gst_element_get_static_pad (session->session, "recv_rtp_src");
2612 if (session->recv_rtp_src == NULL)
2615 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2616 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2617 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2618 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2619 gst_object_unref (sinkdpad);
2620 if (lres != GST_PAD_LINK_OK)
2623 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2624 session->demux_newpad_sig = g_signal_connect (session->demux,
2625 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2626 session->demux_padremoved_sig = g_signal_connect (session->demux,
2627 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2629 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2630 session->recv_rtp_sink_ghost =
2631 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2632 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2633 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2635 return session->recv_rtp_sink_ghost;
2640 g_warning ("rtpbin: invalid name given");
2645 /* create_session already warned */
2650 g_warning ("rtpbin: failed to get session pad");
2655 g_warning ("rtpbin: failed to link pads");
2661 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2663 if (session->demux_newpad_sig) {
2664 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2665 session->demux_newpad_sig = 0;
2667 if (session->demux_padremoved_sig) {
2668 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2669 session->demux_padremoved_sig = 0;
2671 if (session->recv_rtp_src) {
2672 gst_object_unref (session->recv_rtp_src);
2673 session->recv_rtp_src = NULL;
2675 if (session->recv_rtp_sink) {
2676 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2677 gst_object_unref (session->recv_rtp_sink);
2678 session->recv_rtp_sink = NULL;
2680 if (session->recv_rtp_sink_ghost) {
2681 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2682 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2683 session->recv_rtp_sink_ghost);
2684 session->recv_rtp_sink_ghost = NULL;
2688 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2692 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2696 GstRtpBinSession *session;
2698 GstPadLinkReturn lres;
2700 /* first get the session number */
2701 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
2704 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2706 /* get or create the session */
2707 session = find_session_by_id (rtpbin, sessid);
2709 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2710 /* create session now */
2711 session = create_session (rtpbin, sessid);
2712 if (session == NULL)
2716 /* check if pad was requested */
2717 if (session->recv_rtcp_sink_ghost != NULL)
2718 return session->recv_rtcp_sink_ghost;
2720 /* get recv_rtp pad and store */
2721 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2722 session->recv_rtcp_sink =
2723 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2724 if (session->recv_rtcp_sink == NULL)
2727 /* get srcpad, link to SSRCDemux */
2728 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2729 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2730 if (session->sync_src == NULL)
2733 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2734 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2735 lres = gst_pad_link (session->sync_src, sinkdpad);
2736 gst_object_unref (sinkdpad);
2737 if (lres != GST_PAD_LINK_OK)
2740 session->recv_rtcp_sink_ghost =
2741 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2742 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2743 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2744 session->recv_rtcp_sink_ghost);
2746 return session->recv_rtcp_sink_ghost;
2751 g_warning ("rtpbin: invalid name given");
2756 /* create_session already warned */
2761 g_warning ("rtpbin: failed to get session pad");
2766 g_warning ("rtpbin: failed to link pads");
2772 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2774 if (session->recv_rtcp_sink_ghost) {
2775 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2776 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2777 session->recv_rtcp_sink_ghost);
2778 session->recv_rtcp_sink_ghost = NULL;
2780 if (session->sync_src) {
2781 /* releasing the request pad should also unref the sync pad */
2782 gst_object_unref (session->sync_src);
2783 session->sync_src = NULL;
2785 if (session->recv_rtcp_sink) {
2786 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2787 gst_object_unref (session->recv_rtcp_sink);
2788 session->recv_rtcp_sink = NULL;
2792 /* Create a pad for sending RTP for the session in @name. Must be called with
2796 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2800 GstRtpBinSession *session;
2801 GstElementClass *klass;
2803 /* first get the session number */
2804 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
2807 /* get or create session */
2808 session = find_session_by_id (rtpbin, sessid);
2810 /* create session now */
2811 session = create_session (rtpbin, sessid);
2812 if (session == NULL)
2816 /* check if pad was requested */
2817 if (session->send_rtp_sink_ghost != NULL)
2818 return session->send_rtp_sink_ghost;
2820 /* get send_rtp pad and store */
2821 session->send_rtp_sink =
2822 gst_element_get_request_pad (session->session, "send_rtp_sink");
2823 if (session->send_rtp_sink == NULL)
2826 session->send_rtp_sink_ghost =
2827 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2828 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2829 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2832 session->send_rtp_src =
2833 gst_element_get_static_pad (session->session, "send_rtp_src");
2834 if (session->send_rtp_src == NULL)
2837 /* ghost the new source pad */
2838 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2839 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
2840 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
2841 session->send_rtp_src_ghost =
2842 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2843 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2844 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2847 return session->send_rtp_sink_ghost;
2852 g_warning ("rtpbin: invalid name given");
2857 /* create_session already warned */
2862 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
2867 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
2873 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2875 if (session->send_rtp_src_ghost) {
2876 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2877 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2878 session->send_rtp_src_ghost);
2879 session->send_rtp_src_ghost = NULL;
2881 if (session->send_rtp_src) {
2882 gst_object_unref (session->send_rtp_src);
2883 session->send_rtp_src = NULL;
2885 if (session->send_rtp_sink) {
2886 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2887 session->send_rtp_sink);
2888 gst_object_unref (session->send_rtp_sink);
2889 session->send_rtp_sink = NULL;
2891 if (session->send_rtp_sink_ghost) {
2892 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2893 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2894 session->send_rtp_sink_ghost);
2895 session->send_rtp_sink_ghost = NULL;
2899 /* Create a pad for sending RTCP for the session in @name. Must be called with
2903 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2906 GstRtpBinSession *session;
2908 /* first get the session number */
2909 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
2912 /* get or create session */
2913 session = find_session_by_id (rtpbin, sessid);
2917 /* check if pad was requested */
2918 if (session->send_rtcp_src_ghost != NULL)
2919 return session->send_rtcp_src_ghost;
2921 /* get rtcp_src pad and store */
2922 session->send_rtcp_src =
2923 gst_element_get_request_pad (session->session, "send_rtcp_src");
2924 if (session->send_rtcp_src == NULL)
2927 session->send_rtcp_src_ghost =
2928 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2929 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2930 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2932 return session->send_rtcp_src_ghost;
2937 g_warning ("rtpbin: invalid name given");
2942 g_warning ("rtpbin: session with id %d does not exist", sessid);
2947 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
2953 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2955 if (session->send_rtcp_src_ghost) {
2956 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2957 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2958 session->send_rtcp_src_ghost);
2959 session->send_rtcp_src_ghost = NULL;
2961 if (session->send_rtcp_src) {
2962 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2963 gst_object_unref (session->send_rtcp_src);
2964 session->send_rtcp_src = NULL;
2968 /* If the requested name is NULL we should create a name with
2969 * the session number assuming we want the lowest posible session
2970 * with a free pad like the template */
2972 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2974 gboolean name_found = FALSE;
2976 GstIterator *pad_it = NULL;
2977 gchar *pad_name = NULL;
2978 GValue data = { 0, };
2980 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2981 while (!name_found) {
2982 gboolean done = FALSE;
2985 pad_name = g_strdup_printf (templ->name_template, session++);
2986 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2989 switch (gst_iterator_next (pad_it, &data)) {
2990 case GST_ITERATOR_OK:
2995 pad = g_value_get_object (&data);
2996 name = gst_pad_get_name (pad);
2998 if (strcmp (name, pad_name) == 0) {
3003 g_value_reset (&data);
3006 case GST_ITERATOR_ERROR:
3007 case GST_ITERATOR_RESYNC:
3008 /* restart iteration */
3013 case GST_ITERATOR_DONE:
3018 g_value_unset (&data);
3019 gst_iterator_free (pad_it);
3022 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
3029 gst_rtp_bin_request_new_pad (GstElement * element,
3030 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
3033 GstElementClass *klass;
3036 gchar *pad_name = NULL;
3038 g_return_val_if_fail (templ != NULL, NULL);
3039 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
3041 rtpbin = GST_RTP_BIN (element);
3042 klass = GST_ELEMENT_GET_CLASS (element);
3044 GST_RTP_BIN_LOCK (rtpbin);
3047 /* use a free pad name */
3048 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
3050 /* use the provided name */
3051 pad_name = g_strdup (name);
3054 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
3056 /* figure out the template */
3057 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
3058 result = create_recv_rtp (rtpbin, templ, pad_name);
3059 } else if (templ == gst_element_class_get_pad_template (klass,
3060 "recv_rtcp_sink_%u")) {
3061 result = create_recv_rtcp (rtpbin, templ, pad_name);
3062 } else if (templ == gst_element_class_get_pad_template (klass,
3063 "send_rtp_sink_%u")) {
3064 result = create_send_rtp (rtpbin, templ, pad_name);
3065 } else if (templ == gst_element_class_get_pad_template (klass,
3066 "send_rtcp_src_%u")) {
3067 result = create_rtcp (rtpbin, templ, pad_name);
3069 goto wrong_template;
3072 GST_RTP_BIN_UNLOCK (rtpbin);
3080 GST_RTP_BIN_UNLOCK (rtpbin);
3081 g_warning ("rtpbin: this is not our template");
3087 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
3089 GstRtpBinSession *session;
3092 g_return_if_fail (GST_IS_GHOST_PAD (pad));
3093 g_return_if_fail (GST_IS_RTP_BIN (element));
3095 rtpbin = GST_RTP_BIN (element);
3097 GST_RTP_BIN_LOCK (rtpbin);
3098 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
3099 GST_DEBUG_PAD_NAME (pad));
3101 if (!(session = find_session_by_pad (rtpbin, pad)))
3104 if (session->recv_rtp_sink_ghost == pad) {
3105 remove_recv_rtp (rtpbin, session);
3106 } else if (session->recv_rtcp_sink_ghost == pad) {
3107 remove_recv_rtcp (rtpbin, session);
3108 } else if (session->send_rtp_sink_ghost == pad) {
3109 remove_send_rtp (rtpbin, session);
3110 } else if (session->send_rtcp_src_ghost == pad) {
3111 remove_rtcp (rtpbin, session);
3114 /* no more request pads, free the complete session */
3115 if (session->recv_rtp_sink_ghost == NULL
3116 && session->recv_rtcp_sink_ghost == NULL
3117 && session->send_rtp_sink_ghost == NULL
3118 && session->send_rtcp_src_ghost == NULL) {
3119 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
3120 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
3121 free_session (session, rtpbin);
3123 GST_RTP_BIN_UNLOCK (rtpbin);
3130 GST_RTP_BIN_UNLOCK (rtpbin);
3131 g_warning ("rtpbin: %s:%s is not one of our request pads",
3132 GST_DEBUG_PAD_NAME (pad));