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-rtpbin
23 * @see_also: rtpjitterbuffer, rtpsession, rtpptdemux, rtpssrcdemux
25 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
26 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
27 * RTP sessions that will be synchronized together using RTCP SR packets.
29 * #GstRtpBin is configured with a number of request pads that define the
30 * functionality that is activated, similar to the #GstRtpSession element.
32 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
33 * number must be specified in the pad name.
34 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
35 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
36 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
37 * the packets are released from the jitterbuffer, they will be forwarded to a
38 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
39 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
40 * rtpbin with the session number, SSRC and payload type respectively as the pad
43 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
44 * session number must be specified in the pad name.
46 * If you want the session manager to generate and send RTCP packets, request
47 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
48 * on this pad contain SR/RR RTCP reports that should be sent to all participants
51 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
52 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
53 * the pad from the lowest available session will be returned. The session manager will modify the
54 * SSRC in the RTP packets to its own SSRC and will forward the packets on the
55 * send_rtp_src_\%u pad after updating its internal state.
57 * The session manager needs the clock-rate of the payload types it is handling
58 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
59 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
62 * Access to the internal statistics of rtpbin is provided with the
63 * get-internal-session property. This action signal gives access to the
64 * RTPSession object which further provides action signals to retrieve the
65 * internal source and other sources.
67 * #GstRtpBin also has signals (#GstRtpBin::request-rtp-encoder,
68 * #GstRtpBin::request-rtp-decoder, #GstRtpBin::request-rtcp-encoder and
69 * #GstRtpBin::request-rtp-decoder) to dynamically request for RTP and RTCP encoders
70 * and decoders in order to support SRTP. The encoders must provide the pads
71 * rtp_sink_\%u and rtp_src_\%u for RTP and rtcp_sink_\%u and rtcp_src_\%u for
72 * RTCP. The session number will be used in the pad name. The decoders must provide
73 * rtp_sink and rtp_src for RTP and rtcp_sink and rtcp_src for RTCP. The decoders will
74 * be placed before the #GstRtpSession element, thus they must support SSRC demuxing
77 * #GstRtpBin has signals (#GstRtpBin::request-aux-sender and
78 * #GstRtpBin::request-aux-receiver to dynamically request an element that can be
79 * used to create or merge additional RTP streams. AUX elements are needed to
80 * implement FEC or retransmission (such as RFC 4588). An AUX sender must have one
81 * sink_\%u pad that matches the sessionid in the signal and it should have 1 or
82 * more src_\%u pads. For each src_%\u pad, a session will be made (if needed)
83 * and the pad will be linked to the session send_rtp_sink pad. Each session will
84 * then expose its source pad as send_rtp_src_\%u on #GstRtpBin.
85 * An AUX receiver has 1 src_\%u pad that much match the sessionid in the signal
86 * and 1 or more sink_\%u pads. A session will be made for each sink_\%u pad
87 * when the corresponding recv_rtp_sink_\%u pad is requested on #GstRtpBin.
88 * The #GstRtpBin::request-jitterbuffer signal can be used to provide a custom
89 * element to perform arrival time smoothing, reordering and optionally packet
90 * loss detection and retransmission requests.
92 * ## Example pipelines
95 * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
96 * rtpbin ! rtptheoradepay ! theoradec ! xvimagesink
97 * ]| Receive RTP data from port 5000 and send to the session 0 in rtpbin.
99 * gst-launch-1.0 rtpbin name=rtpbin \
100 * v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
101 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
102 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
103 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
104 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
105 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
106 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
107 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
108 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
109 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
110 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
111 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
112 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
113 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
114 * is received on port 5007. Since RTCP packets from the sender should be sent
115 * as soon as possible and do not participate in preroll, sync=false and
116 * async=false is configured on udpsink
118 * gst-launch-1.0 -v rtpbin name=rtpbin \
119 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
120 * port=5000 ! rtpbin.recv_rtp_sink_0 \
121 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
122 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
123 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
124 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
125 * port=5002 ! rtpbin.recv_rtp_sink_1 \
126 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
127 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
128 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
129 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
130 * decode and display the video.
131 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
132 * decode and play the audio.
133 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
134 * session 1 on port 5003. These packets will be used for session management and
136 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
147 #include <gst/rtp/gstrtpbuffer.h>
148 #include <gst/rtp/gstrtcpbuffer.h>
150 #include "gstrtpbin.h"
151 #include "rtpsession.h"
152 #include "gstrtpsession.h"
153 #include "gstrtpjitterbuffer.h"
154 #include "gstrtputils.h"
156 #include <gst/glib-compat-private.h>
158 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
159 #define GST_CAT_DEFAULT gst_rtp_bin_debug
162 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
163 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
166 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
170 * GstRtpBin!recv_fec_sink_%u_%u:
172 * Sink template for receiving Forward Error Correction packets,
173 * in the form recv_fec_sink_<session_idx>_<fec_stream_idx>
175 * See #GstRTPST_2022_1_FecDec for example usage
179 static GstStaticPadTemplate rtpbin_recv_fec_sink_template =
180 GST_STATIC_PAD_TEMPLATE ("recv_fec_sink_%u_%u",
183 GST_STATIC_CAPS ("application/x-rtp")
187 * GstRtpBin!send_fec_src_%u_%u:
189 * Src template for sending Forward Error Correction packets,
190 * in the form send_fec_src_<session_idx>_<fec_stream_idx>
192 * See #GstRTPST_2022_1_FecEnc for example usage
196 static GstStaticPadTemplate rtpbin_send_fec_src_template =
197 GST_STATIC_PAD_TEMPLATE ("send_fec_src_%u_%u",
200 GST_STATIC_CAPS ("application/x-rtp")
203 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
204 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
207 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
210 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
211 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
214 GST_STATIC_CAPS ("application/x-rtp")
218 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
219 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
222 GST_STATIC_CAPS ("application/x-rtp")
225 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
226 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
229 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
232 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
233 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
236 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
239 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock (&(bin)->priv->bin_lock)
240 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
242 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
243 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock (&(bin)->priv->dyn_lock)
244 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->dyn_lock)
246 /* lock for shutdown */
247 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
249 if (g_atomic_int_get (&bin->priv->shutdown)) \
251 GST_RTP_BIN_DYN_LOCK (bin); \
252 if (g_atomic_int_get (&bin->priv->shutdown)) { \
253 GST_RTP_BIN_DYN_UNLOCK (bin); \
258 /* unlock for shutdown */
259 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
260 GST_RTP_BIN_DYN_UNLOCK (bin); \
262 /* Minimum time offset to apply. This compensates for rounding errors in NTP to
263 * RTP timestamp conversions */
264 #define MIN_TS_OFFSET_ROUND_OFF_COMP (4 * GST_MSECOND)
266 struct _GstRtpBinPrivate
270 /* lock protecting dynamic adding/removing */
273 /* if we are shutting down or not */
278 /* NTP time in ns of last SR sync used */
279 guint64 last_ntpnstime;
281 /* list of extra elements */
285 /* signals and args */
288 SIGNAL_REQUEST_PT_MAP,
289 SIGNAL_PAYLOAD_TYPE_CHANGE,
293 SIGNAL_GET_INTERNAL_SESSION,
295 SIGNAL_GET_INTERNAL_STORAGE,
299 SIGNAL_ON_SSRC_COLLISION,
300 SIGNAL_ON_SSRC_VALIDATED,
301 SIGNAL_ON_SSRC_ACTIVE,
304 SIGNAL_ON_BYE_TIMEOUT,
306 SIGNAL_ON_SENDER_TIMEOUT,
309 SIGNAL_REQUEST_RTP_ENCODER,
310 SIGNAL_REQUEST_RTP_DECODER,
311 SIGNAL_REQUEST_RTCP_ENCODER,
312 SIGNAL_REQUEST_RTCP_DECODER,
314 SIGNAL_REQUEST_FEC_DECODER,
315 SIGNAL_REQUEST_FEC_DECODER_FULL,
316 SIGNAL_REQUEST_FEC_ENCODER,
318 SIGNAL_REQUEST_JITTERBUFFER,
320 SIGNAL_NEW_JITTERBUFFER,
323 SIGNAL_REQUEST_AUX_SENDER,
324 SIGNAL_REQUEST_AUX_RECEIVER,
326 SIGNAL_ON_NEW_SENDER_SSRC,
327 SIGNAL_ON_SENDER_SSRC_ACTIVE,
329 SIGNAL_ON_BUNDLED_SSRC,
334 #define DEFAULT_LATENCY_MS 200
335 #define DEFAULT_DROP_ON_LATENCY FALSE
336 #define DEFAULT_SDES NULL
337 #define DEFAULT_DO_LOST FALSE
338 #define DEFAULT_IGNORE_PT FALSE
339 #define DEFAULT_NTP_SYNC FALSE
340 #define DEFAULT_AUTOREMOVE FALSE
341 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
342 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
343 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
344 #define DEFAULT_RTCP_SYNC_INTERVAL 0
345 #define DEFAULT_DO_SYNC_EVENT FALSE
346 #define DEFAULT_DO_RETRANSMISSION FALSE
347 #define DEFAULT_RTP_PROFILE GST_RTP_PROFILE_AVP
348 #define DEFAULT_NTP_TIME_SOURCE GST_RTP_NTP_TIME_SOURCE_NTP
349 #define DEFAULT_RTCP_SYNC_SEND_TIME TRUE
350 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
351 #define DEFAULT_MAX_DROPOUT_TIME 60000
352 #define DEFAULT_MAX_MISORDER_TIME 2000
353 #define DEFAULT_RFC7273_SYNC FALSE
354 #define DEFAULT_ADD_REFERENCE_TIMESTAMP_META FALSE
355 #define DEFAULT_MAX_STREAMS G_MAXUINT
356 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
357 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
358 #define DEFAULT_MIN_TS_OFFSET MIN_TS_OFFSET_ROUND_OFF_COMP
359 #define DEFAULT_TS_OFFSET_SMOOTHING_FACTOR 0
365 PROP_DROP_ON_LATENCY,
371 PROP_RTCP_SYNC_INTERVAL,
374 PROP_USE_PIPELINE_CLOCK,
376 PROP_DO_RETRANSMISSION,
378 PROP_NTP_TIME_SOURCE,
379 PROP_RTCP_SYNC_SEND_TIME,
380 PROP_MAX_RTCP_RTP_TIME_DIFF,
381 PROP_MAX_DROPOUT_TIME,
382 PROP_MAX_MISORDER_TIME,
384 PROP_ADD_REFERENCE_TIMESTAMP_META,
386 PROP_MAX_TS_OFFSET_ADJUSTMENT,
389 PROP_TS_OFFSET_SMOOTHING_FACTOR,
394 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
396 gst_rtp_bin_rtcp_sync_get_type (void)
398 static GType rtcp_sync_type = 0;
399 static const GEnumValue rtcp_sync_types[] = {
400 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
401 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
402 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
406 if (!rtcp_sync_type) {
407 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
409 return rtcp_sync_type;
413 typedef struct _GstRtpBinSession GstRtpBinSession;
414 typedef struct _GstRtpBinStream GstRtpBinStream;
415 typedef struct _GstRtpBinClient GstRtpBinClient;
417 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
419 static GstCaps *pt_map_requested (GstElement * element, guint pt,
420 GstRtpBinSession * session);
421 static void payload_type_change (GstElement * element, guint pt,
422 GstRtpBinSession * session);
423 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
424 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
425 static void remove_recv_fec (GstRtpBin * rtpbin, GstRtpBinSession * session);
426 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
427 static void remove_send_fec (GstRtpBin * rtpbin, GstRtpBinSession * session);
428 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
429 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
430 static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
431 static GstRtpBinSession *create_session (GstRtpBin * rtpbin, gint id);
432 static GstPad *complete_session_sink (GstRtpBin * rtpbin,
433 GstRtpBinSession * session);
435 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
437 static GstPad *complete_session_rtcp (GstRtpBin * rtpbin,
438 GstRtpBinSession * session, guint sessid);
439 static GstElement *session_request_element (GstRtpBinSession * session,
442 /* Manages the RTP stream for one SSRC.
444 * We pipe the stream (coming from the SSRC demuxer) into a jitterbuffer.
445 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
446 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
447 * together (see below).
449 struct _GstRtpBinStream
451 /* the SSRC of this stream */
457 /* the session this SSRC belongs to */
458 GstRtpBinSession *session;
460 /* the jitterbuffer of the SSRC */
462 gulong buffer_handlesync_sig;
463 gulong buffer_ptreq_sig;
464 gulong buffer_ntpstop_sig;
467 /* the PT demuxer of the SSRC */
469 gulong demux_newpad_sig;
470 gulong demux_padremoved_sig;
471 gulong demux_ptreq_sig;
472 gulong demux_ptchange_sig;
474 /* if we have calculated a valid rt_delta for this stream */
476 /* mapping to local RTP and NTP time */
479 gint64 avg_ts_offset;
480 gboolean is_initialized;
481 /* base rtptime in gst time */
485 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
486 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
488 /* Manages the receiving end of the packets.
490 * There is one such structure for each RTP session (audio/video/...).
491 * We get the RTP/RTCP packets and stuff them into the session manager. From
492 * there they are pushed into an SSRC demuxer that splits the stream based on
493 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
494 * the GstRtpBinStream above).
496 * Before the SSRC demuxer, a storage element may be inserted for the purpose
497 * of Forward Error Correction.
499 struct _GstRtpBinSession
505 /* the session element */
507 /* the SSRC demuxer */
509 gulong demux_newpad_sig;
510 gulong demux_padremoved_sig;
517 /* list of GstRtpBinStream */
520 /* list of elements */
523 /* mapping of payload type to caps */
526 /* the pads of the session */
527 GstPad *recv_rtp_sink;
528 GstPad *recv_rtp_sink_ghost;
529 GstPad *recv_rtp_src;
530 GstPad *recv_rtcp_sink;
531 GstPad *recv_rtcp_sink_ghost;
533 GstPad *send_rtp_sink;
534 GstPad *send_rtp_sink_ghost;
535 GstPad *send_rtp_src_ghost;
536 GstPad *send_rtcp_src;
537 GstPad *send_rtcp_src_ghost;
539 GSList *recv_fec_sinks;
540 GSList *recv_fec_sink_ghosts;
541 /* fec decoder placed before the rtpjitterbuffer but after the rtpssrcdemux.
542 * XXX: This does not yet support multiple ssrc's in the same rtp session
544 GstElement *early_fec_decoder;
546 GSList *send_fec_src_ghosts;
549 /* Manages the RTP streams that come from one client and should therefore be
552 struct _GstRtpBinClient
554 /* the common CNAME for the streams */
563 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
564 static GstRtpBinSession *
565 find_session_by_id (GstRtpBin * rtpbin, gint id)
569 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
570 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
579 pad_is_recv_fec (GstRtpBinSession * session, GstPad * pad)
581 return g_slist_find (session->recv_fec_sink_ghosts, pad) != NULL;
584 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
585 static GstRtpBinSession *
586 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
590 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
591 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
593 if ((sess->recv_rtp_sink_ghost == pad) ||
594 (sess->recv_rtcp_sink_ghost == pad) ||
595 (sess->send_rtp_sink_ghost == pad) ||
596 (sess->send_rtcp_src_ghost == pad) || pad_is_recv_fec (sess, pad))
603 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
605 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
610 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
612 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
617 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
619 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
624 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
626 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
631 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
633 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
638 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
640 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
645 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
647 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
650 if (sess->bin->priv->autoremove)
651 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
655 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
657 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
660 if (sess->bin->priv->autoremove)
661 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
665 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
667 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
672 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
674 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
675 stream->session->id, stream->ssrc);
679 on_new_sender_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
681 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
686 on_sender_ssrc_active (GstElement * session, guint32 ssrc,
687 GstRtpBinSession * sess)
689 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE],
693 /* must be called with the SESSION lock */
694 static GstRtpBinStream *
695 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
699 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
700 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
702 if (stream->ssrc == ssrc)
709 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
710 GstRtpBinSession * session)
712 GstRtpBinStream *stream = NULL;
715 rtpbin = session->bin;
717 GST_RTP_BIN_LOCK (rtpbin);
719 GST_RTP_SESSION_LOCK (session);
720 if ((stream = find_stream_by_ssrc (session, ssrc)))
721 session->streams = g_slist_remove (session->streams, stream);
722 GST_RTP_SESSION_UNLOCK (session);
725 free_stream (stream, rtpbin);
727 GST_RTP_BIN_UNLOCK (rtpbin);
730 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
731 static GstRtpBinSession *
732 create_session (GstRtpBin * rtpbin, gint id)
734 GstRtpBinSession *sess;
735 GstElement *session, *demux;
736 GstElement *storage = NULL;
739 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
742 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
745 if (!(storage = gst_element_factory_make ("rtpstorage", NULL)))
748 /* need to sink the storage or otherwise signal handlers from bindings will
749 * take ownership of it and we don't own it anymore */
750 gst_object_ref_sink (storage);
751 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_STORAGE], 0, storage,
754 sess = g_new0 (GstRtpBinSession, 1);
755 g_mutex_init (&sess->lock);
758 sess->session = session;
760 sess->storage = storage;
762 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
763 (GDestroyNotify) gst_caps_unref);
764 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
766 /* configure SDES items */
767 GST_OBJECT_LOCK (rtpbin);
768 g_object_set (demux, "max-streams", rtpbin->max_streams, NULL);
769 g_object_set (session, "sdes", rtpbin->sdes, "rtp-profile",
770 rtpbin->rtp_profile, "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time,
772 if (rtpbin->use_pipeline_clock)
773 g_object_set (session, "use-pipeline-clock", rtpbin->use_pipeline_clock,
776 g_object_set (session, "ntp-time-source", rtpbin->ntp_time_source, NULL);
778 g_object_set (session, "max-dropout-time", rtpbin->max_dropout_time,
779 "max-misorder-time", rtpbin->max_misorder_time, NULL);
780 GST_OBJECT_UNLOCK (rtpbin);
782 /* provide clock_rate to the session manager when needed */
783 g_signal_connect (session, "request-pt-map",
784 (GCallback) pt_map_requested, sess);
786 g_signal_connect (sess->session, "on-new-ssrc",
787 (GCallback) on_new_ssrc, sess);
788 g_signal_connect (sess->session, "on-ssrc-collision",
789 (GCallback) on_ssrc_collision, sess);
790 g_signal_connect (sess->session, "on-ssrc-validated",
791 (GCallback) on_ssrc_validated, sess);
792 g_signal_connect (sess->session, "on-ssrc-active",
793 (GCallback) on_ssrc_active, sess);
794 g_signal_connect (sess->session, "on-ssrc-sdes",
795 (GCallback) on_ssrc_sdes, sess);
796 g_signal_connect (sess->session, "on-bye-ssrc",
797 (GCallback) on_bye_ssrc, sess);
798 g_signal_connect (sess->session, "on-bye-timeout",
799 (GCallback) on_bye_timeout, sess);
800 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
801 g_signal_connect (sess->session, "on-sender-timeout",
802 (GCallback) on_sender_timeout, sess);
803 g_signal_connect (sess->session, "on-new-sender-ssrc",
804 (GCallback) on_new_sender_ssrc, sess);
805 g_signal_connect (sess->session, "on-sender-ssrc-active",
806 (GCallback) on_sender_ssrc_active, sess);
808 gst_bin_add (GST_BIN_CAST (rtpbin), session);
809 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
810 gst_bin_add (GST_BIN_CAST (rtpbin), storage);
812 /* unref the storage again, the bin has a reference now and
813 * we don't need it anymore */
814 gst_object_unref (storage);
816 GST_OBJECT_LOCK (rtpbin);
817 target = GST_STATE_TARGET (rtpbin);
818 GST_OBJECT_UNLOCK (rtpbin);
820 /* change state only to what's needed */
821 gst_element_set_state (demux, target);
822 gst_element_set_state (session, target);
823 gst_element_set_state (storage, target);
830 g_warning ("rtpbin: could not create rtpsession element");
835 gst_object_unref (session);
836 g_warning ("rtpbin: could not create rtpssrcdemux element");
841 gst_object_unref (session);
842 gst_object_unref (demux);
843 g_warning ("rtpbin: could not create rtpstorage element");
849 bin_manage_element (GstRtpBin * bin, GstElement * element)
851 GstRtpBinPrivate *priv = bin->priv;
853 if (g_list_find (priv->elements, element)) {
854 GST_DEBUG_OBJECT (bin, "requested element %p already in bin", element);
856 GST_DEBUG_OBJECT (bin, "adding requested element %p", element);
858 if (g_object_is_floating (element))
859 element = gst_object_ref_sink (element);
861 if (!gst_bin_add (GST_BIN_CAST (bin), element))
863 if (!gst_element_sync_state_with_parent (element))
864 GST_WARNING_OBJECT (bin, "unable to sync element state with rtpbin");
866 /* we add the element multiple times, each we need an equal number of
867 * removes to really remove the element from the bin */
868 priv->elements = g_list_prepend (priv->elements, element);
875 GST_WARNING_OBJECT (bin, "unable to add element");
876 gst_object_unref (element);
882 remove_bin_element (GstElement * element, GstRtpBin * bin)
884 GstRtpBinPrivate *priv = bin->priv;
887 find = g_list_find (priv->elements, element);
889 priv->elements = g_list_delete_link (priv->elements, find);
891 if (!g_list_find (priv->elements, element)) {
892 gst_element_set_locked_state (element, TRUE);
893 gst_bin_remove (GST_BIN_CAST (bin), element);
894 gst_element_set_state (element, GST_STATE_NULL);
897 gst_object_unref (element);
901 /* called with RTP_BIN_LOCK */
903 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
905 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
907 gst_element_set_locked_state (sess->demux, TRUE);
908 gst_element_set_locked_state (sess->session, TRUE);
909 gst_element_set_locked_state (sess->storage, TRUE);
911 gst_element_set_state (sess->demux, GST_STATE_NULL);
912 gst_element_set_state (sess->session, GST_STATE_NULL);
913 gst_element_set_state (sess->storage, GST_STATE_NULL);
915 remove_recv_rtp (bin, sess);
916 remove_recv_rtcp (bin, sess);
917 remove_recv_fec (bin, sess);
918 remove_send_rtp (bin, sess);
919 remove_send_fec (bin, sess);
920 remove_rtcp (bin, sess);
922 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
923 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
924 gst_bin_remove (GST_BIN_CAST (bin), sess->storage);
926 g_slist_foreach (sess->elements, (GFunc) remove_bin_element, bin);
927 g_slist_free (sess->elements);
928 sess->elements = NULL;
930 g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
931 g_slist_free (sess->streams);
933 g_mutex_clear (&sess->lock);
934 g_hash_table_destroy (sess->ptmap);
939 /* get the payload type caps for the specific payload @pt in @session */
941 get_pt_map (GstRtpBinSession * session, guint pt)
943 GstCaps *caps = NULL;
946 GValue args[3] = { {0}, {0}, {0} };
948 GST_DEBUG ("searching pt %u in cache", pt);
950 GST_RTP_SESSION_LOCK (session);
952 /* first look in the cache */
953 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
961 GST_DEBUG ("emitting signal for pt %u in session %u", pt, session->id);
963 /* not in cache, send signal to request caps */
964 g_value_init (&args[0], GST_TYPE_ELEMENT);
965 g_value_set_object (&args[0], bin);
966 g_value_init (&args[1], G_TYPE_UINT);
967 g_value_set_uint (&args[1], session->id);
968 g_value_init (&args[2], G_TYPE_UINT);
969 g_value_set_uint (&args[2], pt);
971 g_value_init (&ret, GST_TYPE_CAPS);
972 g_value_set_boxed (&ret, NULL);
974 GST_RTP_SESSION_UNLOCK (session);
976 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
978 GST_RTP_SESSION_LOCK (session);
980 g_value_unset (&args[0]);
981 g_value_unset (&args[1]);
982 g_value_unset (&args[2]);
984 /* look in the cache again because we let the lock go */
985 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
988 g_value_unset (&ret);
992 caps = (GstCaps *) g_value_dup_boxed (&ret);
993 g_value_unset (&ret);
997 GST_DEBUG ("caching pt %u as %" GST_PTR_FORMAT, pt, caps);
999 /* store in cache, take additional ref */
1000 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
1001 gst_caps_ref (caps));
1004 GST_RTP_SESSION_UNLOCK (session);
1011 GST_RTP_SESSION_UNLOCK (session);
1012 GST_DEBUG ("no pt map could be obtained");
1018 return_true (gpointer key, gpointer value, gpointer user_data)
1024 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
1026 GSList *clients, *streams;
1028 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
1030 GST_RTP_BIN_LOCK (rtpbin);
1031 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
1032 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1034 /* reset sync on all streams for this client */
1035 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
1036 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1038 /* make use require a new SR packet for this stream before we attempt new
1040 stream->have_sync = FALSE;
1041 stream->rt_delta = 0;
1042 stream->avg_ts_offset = 0;
1043 stream->is_initialized = FALSE;
1044 stream->rtp_delta = 0;
1045 stream->clock_base = -100 * GST_SECOND;
1048 GST_RTP_BIN_UNLOCK (rtpbin);
1052 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
1054 GSList *sessions, *streams;
1056 GST_RTP_BIN_LOCK (bin);
1057 GST_DEBUG_OBJECT (bin, "clearing pt map");
1058 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1059 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1061 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
1062 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
1064 GST_RTP_SESSION_LOCK (session);
1065 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
1067 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1068 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1070 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
1071 if (g_signal_lookup ("clear-pt-map", G_OBJECT_TYPE (stream->buffer)) != 0)
1072 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
1074 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
1076 GST_RTP_SESSION_UNLOCK (session);
1078 GST_RTP_BIN_UNLOCK (bin);
1080 /* reset sync too */
1081 gst_rtp_bin_reset_sync (bin);
1085 gst_rtp_bin_get_session (GstRtpBin * bin, guint session_id)
1087 GstRtpBinSession *session;
1088 GstElement *ret = NULL;
1090 GST_RTP_BIN_LOCK (bin);
1091 GST_DEBUG_OBJECT (bin, "retrieving GstRtpSession, index: %u", session_id);
1092 session = find_session_by_id (bin, (gint) session_id);
1094 ret = gst_object_ref (session->session);
1096 GST_RTP_BIN_UNLOCK (bin);
1102 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
1104 RTPSession *internal_session = NULL;
1105 GstRtpBinSession *session;
1107 GST_RTP_BIN_LOCK (bin);
1108 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %u",
1110 session = find_session_by_id (bin, (gint) session_id);
1112 g_object_get (session->session, "internal-session", &internal_session,
1115 GST_RTP_BIN_UNLOCK (bin);
1117 return internal_session;
1121 gst_rtp_bin_get_storage (GstRtpBin * bin, guint session_id)
1123 GstRtpBinSession *session;
1124 GstElement *res = NULL;
1126 GST_RTP_BIN_LOCK (bin);
1127 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1129 session = find_session_by_id (bin, (gint) session_id);
1130 if (session && session->storage) {
1131 res = gst_object_ref (session->storage);
1133 GST_RTP_BIN_UNLOCK (bin);
1139 gst_rtp_bin_get_internal_storage (GstRtpBin * bin, guint session_id)
1141 GObject *internal_storage = NULL;
1142 GstRtpBinSession *session;
1144 GST_RTP_BIN_LOCK (bin);
1145 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1147 session = find_session_by_id (bin, (gint) session_id);
1148 if (session && session->storage) {
1149 g_object_get (session->storage, "internal-storage", &internal_storage,
1152 GST_RTP_BIN_UNLOCK (bin);
1154 return internal_storage;
1158 gst_rtp_bin_clear_ssrc (GstRtpBin * bin, guint session_id, guint32 ssrc)
1160 GstRtpBinSession *session;
1161 GstElement *demux = NULL;
1163 GST_RTP_BIN_LOCK (bin);
1164 GST_DEBUG_OBJECT (bin, "clearing ssrc %u for session %u", ssrc, session_id);
1165 session = find_session_by_id (bin, (gint) session_id);
1167 demux = gst_object_ref (session->demux);
1168 GST_RTP_BIN_UNLOCK (bin);
1171 g_signal_emit_by_name (demux, "clear-ssrc", ssrc, NULL);
1172 gst_object_unref (demux);
1177 gst_rtp_bin_request_encoder (GstRtpBin * bin, guint session_id)
1179 GST_DEBUG_OBJECT (bin, "return NULL encoder");
1184 gst_rtp_bin_request_decoder (GstRtpBin * bin, guint session_id)
1186 GST_DEBUG_OBJECT (bin, "return NULL decoder");
1191 gst_rtp_bin_request_jitterbuffer (GstRtpBin * bin, guint session_id)
1193 return gst_element_factory_make ("rtpjitterbuffer", NULL);
1197 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
1198 const gchar * name, const GValue * value)
1200 GSList *sessions, *streams;
1202 GST_RTP_BIN_LOCK (bin);
1203 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1204 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1206 GST_RTP_SESSION_LOCK (session);
1207 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1208 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1209 GObjectClass *jb_class;
1211 jb_class = G_OBJECT_GET_CLASS (G_OBJECT (stream->buffer));
1212 if (g_object_class_find_property (jb_class, name))
1213 g_object_set_property (G_OBJECT (stream->buffer), name, value);
1215 GST_WARNING_OBJECT (bin,
1216 "Stream jitterbuffer does not expose property %s", name);
1218 GST_RTP_SESSION_UNLOCK (session);
1220 GST_RTP_BIN_UNLOCK (bin);
1224 gst_rtp_bin_propagate_property_to_session (GstRtpBin * bin,
1225 const gchar * name, const GValue * value)
1229 GST_RTP_BIN_LOCK (bin);
1230 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1231 GstRtpBinSession *sess = (GstRtpBinSession *) sessions->data;
1233 g_object_set_property (G_OBJECT (sess->session), name, value);
1235 GST_RTP_BIN_UNLOCK (bin);
1238 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
1239 static GstRtpBinClient *
1240 get_client (GstRtpBin * bin, guint8 len, const guint8 * data,
1243 GstRtpBinClient *result = NULL;
1246 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
1247 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
1249 if (len != client->cname_len)
1252 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
1253 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
1260 /* nothing found, create one */
1261 if (result == NULL) {
1262 result = g_new0 (GstRtpBinClient, 1);
1263 result->cname = g_strndup ((gchar *) data, len);
1264 result->cname_len = len;
1265 bin->clients = g_slist_prepend (bin->clients, result);
1266 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
1273 free_client (GstRtpBinClient * client, GstRtpBin * bin)
1275 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
1276 g_slist_free (client->streams);
1277 g_free (client->cname);
1282 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
1283 guint64 * ntpnstime)
1287 GstClockTime base_time, rt, clock_time;
1289 GST_OBJECT_LOCK (bin);
1290 if ((clock = GST_ELEMENT_CLOCK (bin))) {
1291 base_time = GST_ELEMENT_CAST (bin)->base_time;
1292 gst_object_ref (clock);
1293 GST_OBJECT_UNLOCK (bin);
1295 /* get current clock time and convert to running time */
1296 clock_time = gst_clock_get_time (clock);
1297 rt = clock_time - base_time;
1299 if (bin->use_pipeline_clock) {
1301 /* add constant to convert from 1970 based time to 1900 based time */
1302 ntpns += (GST_RTP_NTP_UNIX_OFFSET * GST_SECOND);
1304 switch (bin->ntp_time_source) {
1305 case GST_RTP_NTP_TIME_SOURCE_NTP:
1306 case GST_RTP_NTP_TIME_SOURCE_UNIX:{
1307 /* get current NTP time */
1308 ntpns = g_get_real_time () * GST_USECOND;
1310 /* add constant to convert from 1970 based time to 1900 based time */
1311 if (bin->ntp_time_source == GST_RTP_NTP_TIME_SOURCE_NTP)
1312 ntpns += (GST_RTP_NTP_UNIX_OFFSET * GST_SECOND);
1315 case GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME:
1318 case GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME:
1322 ntpns = -1; /* Fix uninited compiler warning */
1323 g_assert_not_reached ();
1328 gst_object_unref (clock);
1330 GST_OBJECT_UNLOCK (bin);
1341 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
1342 gint64 ts_offset, gint64 max_ts_offset, guint64 min_ts_offset,
1343 gboolean allow_positive_ts_offset)
1345 gint64 prev_ts_offset;
1346 GObjectClass *jb_class;
1348 jb_class = G_OBJECT_GET_CLASS (G_OBJECT (stream->buffer));
1350 if (!g_object_class_find_property (jb_class, "ts-offset")) {
1351 GST_LOG_OBJECT (bin,
1352 "stream's jitterbuffer does not expose ts-offset property");
1356 if (bin->ts_offset_smoothing_factor > 0) {
1357 if (!stream->is_initialized) {
1358 stream->avg_ts_offset = ts_offset;
1359 stream->is_initialized = TRUE;
1361 /* RMA algorithm using smoothing factor is following, but split into
1362 * parts to check for overflows:
1363 * stream->avg_ts_offset =
1364 * ((bin->ts_offset_smoothing_factor - 1) * stream->avg_ts_offset
1365 * + ts_offset) / bin->ts_offset_smoothing_factor
1367 guint64 max_possible_smoothing_factor = G_MAXUINT64;
1368 gint64 cur_avg_product =
1369 (bin->ts_offset_smoothing_factor - 1) * stream->avg_ts_offset;
1370 if (stream->avg_ts_offset != 0)
1371 max_possible_smoothing_factor =
1372 G_MAXINT64 / ABS (stream->avg_ts_offset);
1374 if ((max_possible_smoothing_factor < bin->ts_offset_smoothing_factor) ||
1375 (cur_avg_product > 0 && G_MAXINT64 - cur_avg_product < ts_offset) ||
1376 (cur_avg_product < 0 && G_MININT64 - cur_avg_product > ts_offset)) {
1377 GST_WARNING_OBJECT (bin,
1378 "ts-offset-smoothing-factor calculation overflow, fallback to using ts-offset directly");
1379 stream->avg_ts_offset = ts_offset;
1381 stream->avg_ts_offset =
1382 (cur_avg_product + ts_offset) / bin->ts_offset_smoothing_factor;
1386 stream->avg_ts_offset = ts_offset;
1389 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
1391 /* delta changed, see how much */
1392 if (prev_ts_offset != stream->avg_ts_offset) {
1395 diff = prev_ts_offset - stream->avg_ts_offset;
1397 GST_DEBUG_OBJECT (bin,
1398 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
1399 ", diff: %" G_GINT64_FORMAT, stream->avg_ts_offset, prev_ts_offset,
1402 /* ignore minor offsets */
1403 if (ABS (diff) < min_ts_offset) {
1404 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1408 /* sanity check offset */
1409 if (max_ts_offset > 0) {
1410 if (stream->avg_ts_offset > 0 && !allow_positive_ts_offset) {
1411 GST_DEBUG_OBJECT (bin,
1412 "offset is positive (clocks are out of sync), ignoring");
1415 if (ABS (stream->avg_ts_offset) > max_ts_offset) {
1416 GST_DEBUG_OBJECT (bin, "offset too large, ignoring");
1421 g_object_set (stream->buffer, "ts-offset", stream->avg_ts_offset, NULL);
1423 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1424 stream->ssrc, stream->avg_ts_offset);
1428 gst_rtp_bin_send_sync_event (GstRtpBinStream * stream)
1430 if (stream->bin->send_sync_event) {
1434 GST_DEBUG_OBJECT (stream->bin,
1435 "sending GstRTCPSRReceived event downstream");
1437 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1438 gst_structure_new_empty ("GstRTCPSRReceived"));
1440 srcpad = gst_element_get_static_pad (stream->buffer, "src");
1441 gst_pad_push_event (srcpad, event);
1442 gst_object_unref (srcpad);
1446 /* associate a stream to the given CNAME. This will make sure all streams for
1447 * that CNAME are synchronized together.
1448 * Must be called with GST_RTP_BIN_LOCK */
1450 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1451 const guint8 * data, guint64 ntpnstime, guint64 last_extrtptime,
1452 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1453 gint64 rtp_clock_base)
1455 GstRtpBinClient *client;
1458 GstClockTime running_time, running_time_rtp;
1460 /* first find or create the CNAME */
1461 client = get_client (bin, len, data, &created);
1463 /* find stream in the client */
1464 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1465 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1467 if (ostream == stream)
1470 /* not found, add it to the list */
1472 GST_DEBUG_OBJECT (bin,
1473 "new association of SSRC %08x with client %p with CNAME %s",
1474 stream->ssrc, client, client->cname);
1475 client->streams = g_slist_prepend (client->streams, stream);
1478 GST_DEBUG_OBJECT (bin,
1479 "found association of SSRC %08x with client %p with CNAME %s",
1480 stream->ssrc, client, client->cname);
1483 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1484 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1485 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1486 /* we don't need that data, so carry on,
1487 * but make some values look saner */
1488 last_extrtptime = base_rtptime;
1490 /* nothing we can do with this data in this case */
1491 GST_DEBUG_OBJECT (bin, "bailing out");
1496 /* Take the extended rtptime we found in the SR packet and map it to the
1497 * local rtptime. The local rtp time is used to construct timestamps on the
1498 * buffers so we will calculate what running_time corresponds to the RTP
1499 * timestamp in the SR packet. */
1500 running_time_rtp = last_extrtptime - base_rtptime;
1502 GST_DEBUG_OBJECT (bin,
1503 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1504 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1505 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1506 last_extrtptime, running_time_rtp, clock_rate, rtp_clock_base);
1508 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1509 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1510 * into a corresponding gstreamer timestamp. Note that the base_time also
1511 * contains the drift between sender and receiver. */
1513 gst_util_uint64_scale_int (running_time_rtp, GST_SECOND, clock_rate);
1514 running_time += base_time;
1516 stream->have_sync = TRUE;
1518 GST_DEBUG_OBJECT (bin,
1519 "SR RTP running time %" G_GUINT64_FORMAT ", SR NTP %" G_GUINT64_FORMAT,
1520 running_time, ntpnstime);
1522 /* recalc inter stream playout offset, but only if there is more than one
1523 * stream or we're doing NTP sync. */
1524 if (bin->ntp_sync) {
1525 gint64 ntpdiff, rtdiff;
1526 guint64 local_ntpnstime;
1527 GstClockTime local_running_time;
1529 /* For NTP sync we need to first get a snapshot of running_time and NTP
1530 * time. We know at what running_time we play a certain RTP time, we also
1531 * calculated when we would play the RTP time in the SR packet. Now we need
1532 * to know how the running_time and the NTP time relate to each other. */
1533 get_current_times (bin, &local_running_time, &local_ntpnstime);
1535 /* see how far away the NTP time is. This is the difference between the
1536 * current NTP time and the NTP time in the last SR packet. */
1537 ntpdiff = local_ntpnstime - ntpnstime;
1538 /* see how far away the running_time is. This is the difference between the
1539 * current running_time and the running_time of the RTP timestamp in the
1540 * last SR packet. */
1541 rtdiff = local_running_time - running_time;
1543 GST_DEBUG_OBJECT (bin,
1544 "local NTP time %" G_GUINT64_FORMAT ", SR NTP time %" G_GUINT64_FORMAT,
1545 local_ntpnstime, ntpnstime);
1546 GST_DEBUG_OBJECT (bin,
1547 "local running time %" G_GUINT64_FORMAT ", SR RTP running time %"
1548 G_GUINT64_FORMAT, local_running_time, running_time);
1549 GST_DEBUG_OBJECT (bin,
1550 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1553 /* combine to get the final diff to apply to the running_time */
1554 stream->rt_delta = rtdiff - ntpdiff;
1556 stream_set_ts_offset (bin, stream, stream->rt_delta, bin->max_ts_offset,
1557 bin->min_ts_offset, FALSE);
1559 gint64 min, rtp_min, clock_base = stream->clock_base;
1560 gboolean all_sync, use_rtp;
1561 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1563 /* calculate delta between server and receiver. ntpnstime is created by
1564 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1565 * delta expresses the difference to our timeline and the server timeline. The
1566 * difference in itself doesn't mean much but we can combine the delta of
1567 * multiple streams to create a stream specific offset. */
1568 stream->rt_delta = ntpnstime - running_time;
1570 /* calculate the min of all deltas, ignoring streams that did not yet have a
1571 * valid rt_delta because we did not yet receive an SR packet for those
1573 * We calculate the minimum because we would like to only apply positive
1574 * offsets to streams, delaying their playback instead of trying to speed up
1575 * other streams (which might be impossible when we have to create negative
1577 * The stream that has the smallest diff is selected as the reference stream,
1578 * all other streams will have a positive offset to this difference. */
1580 /* some alternative setting allow ignoring RTCP as much as possible,
1581 * for servers generating bogus ntp timeline */
1582 min = rtp_min = G_MAXINT64;
1584 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1588 /* signed version for convenience */
1589 clock_base = base_rtptime;
1590 /* deal with possible wrap-around */
1591 ext_base = base_rtptime;
1592 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1593 /* sanity check; base rtp and provided clock_base should be close */
1594 if (rtp_clock_base >= clock_base) {
1595 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1596 rtp_clock_base = base_time +
1597 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1598 GST_SECOND, clock_rate);
1603 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1604 rtp_clock_base = base_time -
1605 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1606 GST_SECOND, clock_rate);
1611 /* warn and bail for clarity out if no sane values */
1613 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1616 /* store to track changes */
1617 clock_base = rtp_clock_base;
1618 /* generate a fake as before,
1619 * now equating rtptime obtained from RTP-Info,
1620 * where the large time represent the otherwise irrelevant npt/ntp time */
1621 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1623 clock_base = rtp_clock_base;
1627 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1628 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1630 if (!ostream->have_sync) {
1635 /* change in current stream's base from previously init'ed value
1636 * leads to reset of all stream's base */
1637 if (stream != ostream && stream->clock_base >= 0 &&
1638 (stream->clock_base != clock_base)) {
1639 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1640 ostream->clock_base = -100 * GST_SECOND;
1641 ostream->rtp_delta = 0;
1644 if (ostream->rt_delta < min)
1645 min = ostream->rt_delta;
1646 if (ostream->rtp_delta < rtp_min)
1647 rtp_min = ostream->rtp_delta;
1650 /* arrange to re-sync for each stream upon significant change,
1652 all_sync = all_sync && (stream->clock_base == clock_base);
1653 stream->clock_base = clock_base;
1655 /* may need init performed above later on, but nothing more to do now */
1656 if (client->nstreams <= 1)
1659 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1660 " all sync %d", client, min, all_sync);
1661 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1663 switch (rtcp_sync) {
1664 case GST_RTP_BIN_RTCP_SYNC_RTP:
1667 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1668 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1670 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1671 /* if all have been synced already, do not bother further */
1673 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1681 /* bail out if we adjusted recently enough */
1682 if (all_sync && (ntpnstime - bin->priv->last_ntpnstime) <
1683 bin->rtcp_sync_interval * GST_MSECOND) {
1684 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1685 "previous sender info too recent "
1686 "(previous NTP %" G_GUINT64_FORMAT ")", bin->priv->last_ntpnstime);
1689 bin->priv->last_ntpnstime = ntpnstime;
1691 /* calculate offsets for each stream */
1692 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1693 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1696 /* ignore streams for which we didn't receive an SR packet yet, we
1697 * can't synchronize them yet. We can however sync other streams just
1699 if (!ostream->have_sync)
1702 /* calculate offset to our reference stream, this should always give a
1703 * positive number. */
1705 ts_offset = ostream->rtp_delta - rtp_min;
1707 ts_offset = ostream->rt_delta - min;
1709 stream_set_ts_offset (bin, ostream, ts_offset, bin->max_ts_offset,
1710 bin->min_ts_offset, TRUE);
1713 gst_rtp_bin_send_sync_event (stream);
1718 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1719 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1720 (b) = gst_rtcp_packet_move_to_next ((packet)))
1722 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1723 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1724 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1726 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1727 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1728 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1731 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1732 GstRtpBinStream * stream)
1735 GstRTCPPacket packet;
1737 guint64 ntpnstime, inband_ntpnstime;
1740 guint64 base_rtptime;
1744 guint64 extrtptime, inband_ext_rtptime;
1747 GstRTCPBuffer rtcp = { NULL, };
1751 GST_DEBUG_OBJECT (bin, "sync handler called");
1753 /* get the last relation between the rtp timestamps and the gstreamer
1754 * timestamps. We get this info directly from the jitterbuffer which
1755 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1756 * what the current situation is. */
1757 if (!gst_structure_get_uint64 (s, "base-rtptime", &base_rtptime) ||
1758 !gst_structure_get_uint64 (s, "base-time", &base_time) ||
1759 !gst_structure_get_uint (s, "clock-rate", &clock_rate) ||
1760 !gst_structure_get_uint64 (s, "clock-base", &clock_base)) {
1761 /* invalid structure */
1765 cname = gst_structure_get_string (s, "cname");
1767 /* if the jitterbuffer directly got the NTP timestamp then don't work
1768 * through the RTCP SR, otherwise extract it from there */
1769 if (gst_structure_get_uint64 (s, "inband-ntpnstime", &inband_ntpnstime)
1770 && gst_structure_get_uint64 (s, "inband-ext-rtptime", &inband_ext_rtptime)
1771 && (cname = gst_structure_get_string (s, "cname"))
1772 && gst_structure_get_uint (s, "ssrc", &ssrc)) {
1773 GST_DEBUG_OBJECT (bin,
1774 "handle sync from inband NTP-64 information for SSRC %08x", ssrc);
1776 if (ssrc != stream->ssrc)
1779 GST_RTP_BIN_LOCK (bin);
1780 gst_rtp_bin_associate (bin, stream, strlen (cname), (const guint8 *) cname,
1781 inband_ntpnstime, inband_ext_rtptime, base_rtptime, base_time,
1782 clock_rate, clock_base);
1783 GST_RTP_BIN_UNLOCK (bin);
1787 if (!gst_structure_get_uint64 (s, "sr-ext-rtptime", &extrtptime)
1788 || !gst_structure_has_field_typed (s, "sr-buffer", GST_TYPE_BUFFER)) {
1789 /* invalid structure */
1793 GST_DEBUG_OBJECT (bin, "handle sync from RTCP SR information");
1795 /* get RTCP SR ntpnstime if available */
1796 if (gst_structure_get_uint64 (s, "sr-ntpnstime", &ntpnstime) && cname) {
1797 GST_RTP_BIN_LOCK (bin);
1798 /* associate the stream to CNAME */
1799 gst_rtp_bin_associate (bin, stream, strlen (cname),
1800 (const guint8 *) cname, ntpnstime, extrtptime, base_rtptime,
1801 base_time, clock_rate, clock_base);
1802 GST_RTP_BIN_UNLOCK (bin);
1806 /* otherwise parse the RTCP packet */
1807 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1811 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1813 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1814 /* first packet must be SR or RR or else the validate would have failed */
1815 switch (gst_rtcp_packet_get_type (&packet)) {
1816 case GST_RTCP_TYPE_SR:
1817 /* only parse first. There is only supposed to be one SR in the packet
1818 * but we will deal with malformed packets gracefully by trying the
1819 * next RTCP packet. */
1824 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntpnstime, NULL,
1827 /* convert ntptime to nanoseconds */
1828 ntpnstime = gst_util_uint64_scale (ntpnstime, GST_SECOND,
1829 (G_GINT64_CONSTANT (1) << 32));
1831 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1833 /* ignore SR that is not ours and check the next RTCP packet */
1834 if (ssrc != stream->ssrc)
1839 /* If we already have the CNAME don't require parsing SDES */
1841 GST_RTP_BIN_LOCK (bin);
1842 /* associate the stream to CNAME */
1843 gst_rtp_bin_associate (bin, stream, strlen (cname),
1844 (const guint8 *) cname, ntpnstime, extrtptime, base_rtptime,
1845 base_time, clock_rate, clock_base);
1846 GST_RTP_BIN_UNLOCK (bin);
1852 case GST_RTCP_TYPE_SDES:
1854 gboolean more_items;
1856 /* Bail out if we have not seen an SR item yet. */
1860 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1861 gboolean more_entries;
1863 /* skip items that are not about the SSRC of the sender */
1864 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1867 /* find the CNAME entry */
1868 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1869 GstRTCPSDESType type;
1873 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len,
1874 (guint8 **) & data);
1876 if (type == GST_RTCP_SDES_CNAME) {
1877 GST_RTP_BIN_LOCK (bin);
1878 /* associate the stream to CNAME */
1879 gst_rtp_bin_associate (bin, stream, len, data,
1880 ntpnstime, extrtptime, base_rtptime, base_time, clock_rate,
1882 GST_RTP_BIN_UNLOCK (bin);
1889 /* only deal with first SDES, there is only supposed to be one SDES in
1890 * the RTCP packet but we deal with bad packets gracefully. */
1894 /* we can ignore these packets */
1899 gst_rtcp_buffer_unmap (&rtcp);
1902 /* create a new stream with @ssrc in @session. Must be called with
1903 * RTP_SESSION_LOCK. */
1904 static GstRtpBinStream *
1905 create_stream (GstRtpBinSession * session, guint32 ssrc)
1907 GstElement *buffer, *demux = NULL;
1908 GstRtpBinStream *stream;
1911 GObjectClass *jb_class;
1913 rtpbin = session->bin;
1915 if (g_slist_length (session->streams) >= rtpbin->max_streams)
1919 session_request_element (session, SIGNAL_REQUEST_JITTERBUFFER)))
1920 goto no_jitterbuffer;
1922 if (!rtpbin->ignore_pt) {
1923 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1927 stream = g_new0 (GstRtpBinStream, 1);
1928 stream->ssrc = ssrc;
1929 stream->bin = rtpbin;
1930 stream->session = session;
1931 stream->buffer = gst_object_ref (buffer);
1932 stream->demux = demux;
1934 stream->have_sync = FALSE;
1935 stream->rt_delta = 0;
1936 stream->avg_ts_offset = 0;
1937 stream->is_initialized = FALSE;
1938 stream->rtp_delta = 0;
1939 stream->percent = 100;
1940 stream->clock_base = -100 * GST_SECOND;
1941 session->streams = g_slist_prepend (session->streams, stream);
1943 jb_class = G_OBJECT_GET_CLASS (G_OBJECT (buffer));
1945 if (g_signal_lookup ("request-pt-map", G_OBJECT_TYPE (buffer)) != 0) {
1946 /* provide clock_rate to the jitterbuffer when needed */
1947 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1948 (GCallback) pt_map_requested, session);
1950 if (g_signal_lookup ("on-npt-stop", G_OBJECT_TYPE (buffer)) != 0) {
1951 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1952 (GCallback) on_npt_stop, stream);
1955 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1956 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1958 /* configure latency and packet lost */
1959 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1961 if (g_object_class_find_property (jb_class, "drop-on-latency"))
1962 g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1963 if (g_object_class_find_property (jb_class, "do-lost"))
1964 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1965 if (g_object_class_find_property (jb_class, "mode"))
1966 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1967 if (g_object_class_find_property (jb_class, "do-retransmission"))
1968 g_object_set (buffer, "do-retransmission", rtpbin->do_retransmission, NULL);
1969 if (g_object_class_find_property (jb_class, "max-rtcp-rtp-time-diff"))
1970 g_object_set (buffer, "max-rtcp-rtp-time-diff",
1971 rtpbin->max_rtcp_rtp_time_diff, NULL);
1972 if (g_object_class_find_property (jb_class, "max-dropout-time"))
1973 g_object_set (buffer, "max-dropout-time", rtpbin->max_dropout_time, NULL);
1974 if (g_object_class_find_property (jb_class, "max-misorder-time"))
1975 g_object_set (buffer, "max-misorder-time", rtpbin->max_misorder_time, NULL);
1976 if (g_object_class_find_property (jb_class, "rfc7273-sync"))
1977 g_object_set (buffer, "rfc7273-sync", rtpbin->rfc7273_sync, NULL);
1978 if (g_object_class_find_property (jb_class, "add-reference-timestamp-meta"))
1979 g_object_set (buffer, "add-reference-timestamp-meta",
1980 rtpbin->add_reference_timestamp_meta, NULL);
1981 if (g_object_class_find_property (jb_class, "max-ts-offset-adjustment"))
1982 g_object_set (buffer, "max-ts-offset-adjustment",
1983 rtpbin->max_ts_offset_adjustment, NULL);
1984 if (g_object_class_find_property (jb_class, "sync-interval"))
1985 g_object_set (buffer, "sync-interval", rtpbin->rtcp_sync_interval, NULL);
1987 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER], 0,
1988 buffer, session->id, ssrc);
1990 if (!rtpbin->ignore_pt)
1991 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1995 gst_element_link_pads_full (buffer, "src", demux, "sink",
1996 GST_PAD_LINK_CHECK_NOTHING);
1998 if (rtpbin->buffering) {
2001 if (g_signal_lookup ("set-active", G_OBJECT_TYPE (buffer)) != 0) {
2002 GST_INFO_OBJECT (rtpbin,
2003 "bin is buffering, set jitterbuffer as not active");
2004 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0,
2010 GST_OBJECT_LOCK (rtpbin);
2011 target = GST_STATE_TARGET (rtpbin);
2012 GST_OBJECT_UNLOCK (rtpbin);
2014 /* from sink to source */
2016 gst_element_set_state (demux, target);
2018 gst_element_set_state (buffer, target);
2025 GST_WARNING_OBJECT (rtpbin, "stream exceeds maximum (%d)",
2026 rtpbin->max_streams);
2031 g_warning ("rtpbin: could not create rtpjitterbuffer element");
2036 gst_object_unref (buffer);
2037 g_warning ("rtpbin: could not create rtpptdemux element");
2042 /* called with RTP_BIN_LOCK */
2044 free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
2046 GstRtpBinSession *sess = stream->session;
2047 GSList *clients, *next_client;
2049 GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
2051 gst_element_set_locked_state (stream->buffer, TRUE);
2053 gst_element_set_locked_state (stream->demux, TRUE);
2055 gst_element_set_state (stream->buffer, GST_STATE_NULL);
2057 gst_element_set_state (stream->demux, GST_STATE_NULL);
2059 if (stream->demux) {
2060 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
2061 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
2062 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
2063 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
2066 if (stream->buffer_handlesync_sig)
2067 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
2068 if (stream->buffer_ptreq_sig)
2069 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
2070 if (stream->buffer_ntpstop_sig)
2071 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
2073 sess->elements = g_slist_remove (sess->elements, stream->buffer);
2074 remove_bin_element (stream->buffer, bin);
2075 gst_object_unref (stream->buffer);
2078 gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
2080 for (clients = bin->clients; clients; clients = next_client) {
2081 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
2082 GSList *streams, *next_stream;
2084 next_client = g_slist_next (clients);
2086 for (streams = client->streams; streams; streams = next_stream) {
2087 GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
2089 next_stream = g_slist_next (streams);
2091 if (ostream == stream) {
2092 client->streams = g_slist_delete_link (client->streams, streams);
2093 /* If this was the last stream belonging to this client,
2094 * clean up the client. */
2095 if (--client->nstreams == 0) {
2096 bin->clients = g_slist_delete_link (bin->clients, clients);
2097 free_client (client, bin);
2106 /* GObject vmethods */
2107 static void gst_rtp_bin_dispose (GObject * object);
2108 static void gst_rtp_bin_finalize (GObject * object);
2109 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
2110 const GValue * value, GParamSpec * pspec);
2111 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
2112 GValue * value, GParamSpec * pspec);
2114 /* GstElement vmethods */
2115 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
2116 GstStateChange transition);
2117 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
2118 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
2119 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
2120 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
2122 #define gst_rtp_bin_parent_class parent_class
2123 G_DEFINE_TYPE_WITH_PRIVATE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
2124 GST_ELEMENT_REGISTER_DEFINE (rtpbin, "rtpbin", GST_RANK_NONE, GST_TYPE_RTP_BIN);
2127 _gst_element_accumulator (GSignalInvocationHint * ihint,
2128 GValue * return_accu, const GValue * handler_return, gpointer dummy)
2130 GstElement *element;
2132 element = g_value_get_object (handler_return);
2133 GST_DEBUG ("got element %" GST_PTR_FORMAT, element);
2135 g_value_set_object (return_accu, element);
2137 /* stop emission if we have an element */
2138 return (element == NULL);
2142 _gst_caps_accumulator (GSignalInvocationHint * ihint,
2143 GValue * return_accu, const GValue * handler_return, gpointer dummy)
2147 caps = g_value_get_boxed (handler_return);
2148 GST_DEBUG ("got caps %" GST_PTR_FORMAT, caps);
2150 g_value_set_boxed (return_accu, caps);
2152 /* stop emission if we have a caps */
2153 return (caps == NULL);
2157 gst_rtp_bin_class_init (GstRtpBinClass * klass)
2159 GObjectClass *gobject_class;
2160 GstElementClass *gstelement_class;
2161 GstBinClass *gstbin_class;
2163 gobject_class = (GObjectClass *) klass;
2164 gstelement_class = (GstElementClass *) klass;
2165 gstbin_class = (GstBinClass *) klass;
2167 gobject_class->dispose = gst_rtp_bin_dispose;
2168 gobject_class->finalize = gst_rtp_bin_finalize;
2169 gobject_class->set_property = gst_rtp_bin_set_property;
2170 gobject_class->get_property = gst_rtp_bin_get_property;
2172 g_object_class_install_property (gobject_class, PROP_LATENCY,
2173 g_param_spec_uint ("latency", "Buffer latency in ms",
2174 "Default amount of ms to buffer in the jitterbuffers", 0,
2175 G_MAXUINT, DEFAULT_LATENCY_MS,
2176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2178 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
2179 g_param_spec_boolean ("drop-on-latency",
2180 "Drop buffers when maximum latency is reached",
2181 "Tells the jitterbuffer to never exceed the given latency in size",
2182 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2185 * GstRtpBin::request-pt-map:
2186 * @rtpbin: the object which received the signal
2187 * @session: the session
2190 * Request the payload type as #GstCaps for @pt in @session.
2192 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
2193 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
2194 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
2195 _gst_caps_accumulator, NULL, NULL, GST_TYPE_CAPS, 2, G_TYPE_UINT,
2199 * GstRtpBin::payload-type-change:
2200 * @rtpbin: the object which received the signal
2201 * @session: the session
2204 * Signal that the current payload type changed to @pt in @session.
2206 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
2207 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
2208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
2209 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2212 * GstRtpBin::clear-pt-map:
2213 * @rtpbin: the object which received the signal
2215 * Clear all previously cached pt-mapping obtained with
2216 * #GstRtpBin::request-pt-map.
2218 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
2219 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
2220 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2221 clear_pt_map), NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
2224 * GstRtpBin::reset-sync:
2225 * @rtpbin: the object which received the signal
2227 * Reset all currently configured lip-sync parameters and require new SR
2228 * packets for all streams before lip-sync is attempted again.
2230 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
2231 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
2232 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2233 reset_sync), NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
2236 * GstRtpBin::get-session:
2237 * @rtpbin: the object which received the signal
2238 * @id: the session id
2240 * Request the related GstRtpSession as #GstElement related with session @id.
2244 gst_rtp_bin_signals[SIGNAL_GET_SESSION] =
2245 g_signal_new ("get-session", G_TYPE_FROM_CLASS (klass),
2246 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2247 get_session), NULL, NULL, NULL, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2250 * GstRtpBin::get-internal-session:
2251 * @rtpbin: the object which received the signal
2252 * @id: the session id
2254 * Request the internal RTPSession object as #GObject in session @id.
2256 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
2257 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
2258 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2259 get_internal_session), NULL, NULL, NULL, RTP_TYPE_SESSION, 1,
2263 * GstRtpBin::get-internal-storage:
2264 * @rtpbin: the object which received the signal
2265 * @id: the session id
2267 * Request the internal RTPStorage object as #GObject in session @id. This
2268 * is the internal storage used by the RTPStorage element, which is used to
2269 * keep a backlog of received RTP packets for the session @id.
2273 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_STORAGE] =
2274 g_signal_new ("get-internal-storage", G_TYPE_FROM_CLASS (klass),
2275 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2276 get_internal_storage), NULL, NULL, NULL, G_TYPE_OBJECT, 1,
2280 * GstRtpBin::get-storage:
2281 * @rtpbin: the object which received the signal
2282 * @id: the session id
2284 * Request the RTPStorage element as #GObject in session @id. This element
2285 * is used to keep a backlog of received RTP packets for the session @id.
2289 gst_rtp_bin_signals[SIGNAL_GET_STORAGE] =
2290 g_signal_new ("get-storage", G_TYPE_FROM_CLASS (klass),
2291 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2292 get_storage), NULL, NULL, NULL, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2295 * GstRtpBin::clear-ssrc:
2296 * @rtpbin: the object which received the signal
2297 * @id: the session id
2300 * Remove all pads from rtpssrcdemux element associated with the specified
2301 * ssrc. This delegate the action signal to the rtpssrcdemux element
2302 * associated with the specified session.
2306 gst_rtp_bin_signals[SIGNAL_CLEAR_SSRC] =
2307 g_signal_new ("clear-ssrc", G_TYPE_FROM_CLASS (klass),
2308 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2309 clear_ssrc), NULL, NULL, NULL, G_TYPE_NONE, 2,
2310 G_TYPE_UINT, G_TYPE_UINT);
2313 * GstRtpBin::on-new-ssrc:
2314 * @rtpbin: the object which received the signal
2315 * @session: the session
2318 * Notify of a new SSRC that entered @session.
2320 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
2321 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
2322 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
2323 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2325 * GstRtpBin::on-ssrc-collision:
2326 * @rtpbin: the object which received the signal
2327 * @session: the session
2330 * Notify when we have an SSRC collision
2332 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
2333 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
2334 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
2335 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2337 * GstRtpBin::on-ssrc-validated:
2338 * @rtpbin: the object which received the signal
2339 * @session: the session
2342 * Notify of a new SSRC that became validated.
2344 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
2345 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
2346 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
2347 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2349 * GstRtpBin::on-ssrc-active:
2350 * @rtpbin: the object which received the signal
2351 * @session: the session
2354 * Notify of a SSRC that is active, i.e., sending RTCP.
2356 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
2357 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
2358 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
2359 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2361 * GstRtpBin::on-ssrc-sdes:
2362 * @rtpbin: the object which received the signal
2363 * @session: the session
2366 * Notify of a SSRC that is active, i.e., sending RTCP.
2368 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
2369 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
2370 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
2371 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2374 * GstRtpBin::on-bye-ssrc:
2375 * @rtpbin: the object which received the signal
2376 * @session: the session
2379 * Notify of an SSRC that became inactive because of a BYE packet.
2381 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
2382 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
2383 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
2384 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2386 * GstRtpBin::on-bye-timeout:
2387 * @rtpbin: the object which received the signal
2388 * @session: the session
2391 * Notify of an SSRC that has timed out because of BYE
2393 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
2394 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
2395 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
2396 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2398 * GstRtpBin::on-timeout:
2399 * @rtpbin: the object which received the signal
2400 * @session: the session
2403 * Notify of an SSRC that has timed out
2405 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
2406 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
2407 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
2408 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2410 * GstRtpBin::on-sender-timeout:
2411 * @rtpbin: the object which received the signal
2412 * @session: the session
2415 * Notify of a sender SSRC that has timed out and became a receiver
2417 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
2418 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
2419 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
2420 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2423 * GstRtpBin::on-npt-stop:
2424 * @rtpbin: the object which received the signal
2425 * @session: the session
2428 * Notify that SSRC sender has sent data up to the configured NPT stop time.
2430 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
2431 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
2432 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
2433 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2436 * GstRtpBin::request-rtp-encoder:
2437 * @rtpbin: the object which received the signal
2438 * @session: the session
2440 * Request an RTP encoder element for the given @session. The encoder
2441 * element will be added to the bin if not previously added.
2443 * If no handler is connected, no encoder will be used.
2447 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_ENCODER] =
2448 g_signal_new ("request-rtp-encoder", G_TYPE_FROM_CLASS (klass),
2449 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2450 request_rtp_encoder), _gst_element_accumulator, NULL, NULL,
2451 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2454 * GstRtpBin::request-rtp-decoder:
2455 * @rtpbin: the object which received the signal
2456 * @session: the session
2458 * Request an RTP decoder element for the given @session. The decoder
2459 * element will be added to the bin if not previously added.
2461 * If no handler is connected, no encoder will be used.
2465 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_DECODER] =
2466 g_signal_new ("request-rtp-decoder", G_TYPE_FROM_CLASS (klass),
2467 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2468 request_rtp_decoder), _gst_element_accumulator, NULL,
2469 NULL, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2472 * GstRtpBin::request-rtcp-encoder:
2473 * @rtpbin: the object which received the signal
2474 * @session: the session
2476 * Request an RTCP encoder element for the given @session. The encoder
2477 * element will be added to the bin if not previously added.
2479 * If no handler is connected, no encoder will be used.
2483 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_ENCODER] =
2484 g_signal_new ("request-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
2485 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2486 request_rtcp_encoder), _gst_element_accumulator, NULL, NULL,
2487 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2490 * GstRtpBin::request-rtcp-decoder:
2491 * @rtpbin: the object which received the signal
2492 * @session: the session
2494 * Request an RTCP decoder element for the given @session. The decoder
2495 * element will be added to the bin if not previously added.
2497 * If no handler is connected, no encoder will be used.
2501 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_DECODER] =
2502 g_signal_new ("request-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
2503 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2504 request_rtcp_decoder), _gst_element_accumulator, NULL, NULL,
2505 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2508 * GstRtpBin::request-jitterbuffer:
2509 * @rtpbin: the object which received the signal
2510 * @session: the session
2512 * Request a jitterbuffer element for the given @session.
2514 * If no handler is connected, the default jitterbuffer will be used.
2516 * Note: The provided element is expected to conform to the API exposed
2517 * by the standard #GstRtpJitterBuffer. Runtime checks will be made to
2518 * determine whether it exposes properties and signals before attempting
2519 * to set, call or connect to them, and some functionalities of #GstRtpBin
2520 * may not be available when that is not the case.
2522 * This should be considered experimental API, as the standard jitterbuffer
2523 * API is susceptible to change, provided elements will have to update their
2524 * custom jitterbuffer's API to match the API of #GstRtpJitterBuffer if and
2529 gst_rtp_bin_signals[SIGNAL_REQUEST_JITTERBUFFER] =
2530 g_signal_new ("request-jitterbuffer", G_TYPE_FROM_CLASS (klass),
2531 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2532 request_jitterbuffer), _gst_element_accumulator, NULL,
2533 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2536 * GstRtpBin::new-jitterbuffer:
2537 * @rtpbin: the object which received the signal
2538 * @jitterbuffer: the new jitterbuffer
2539 * @session: the session
2542 * Notify that a new @jitterbuffer was created for @session and @ssrc.
2543 * This signal can, for example, be used to configure @jitterbuffer.
2547 gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER] =
2548 g_signal_new ("new-jitterbuffer", G_TYPE_FROM_CLASS (klass),
2549 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2550 new_jitterbuffer), NULL, NULL, NULL,
2551 G_TYPE_NONE, 3, GST_TYPE_ELEMENT, G_TYPE_UINT, G_TYPE_UINT);
2554 * GstRtpBin::new-storage:
2555 * @rtpbin: the object which received the signal
2556 * @storage: the new storage
2557 * @session: the session
2559 * Notify that a new @storage was created for @session.
2560 * This signal can, for example, be used to configure @storage.
2564 gst_rtp_bin_signals[SIGNAL_NEW_STORAGE] =
2565 g_signal_new ("new-storage", G_TYPE_FROM_CLASS (klass),
2566 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2567 new_storage), NULL, NULL, NULL,
2568 G_TYPE_NONE, 2, GST_TYPE_ELEMENT, G_TYPE_UINT);
2571 * GstRtpBin::request-aux-sender:
2572 * @rtpbin: the object which received the signal
2573 * @session: the session
2575 * Request an AUX sender element for the given @session. The AUX
2576 * element will be added to the bin.
2578 * If no handler is connected, no AUX element will be used.
2582 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_SENDER] =
2583 g_signal_new ("request-aux-sender", G_TYPE_FROM_CLASS (klass),
2584 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2585 request_aux_sender), _gst_element_accumulator, NULL, NULL,
2586 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2589 * GstRtpBin::request-aux-receiver:
2590 * @rtpbin: the object which received the signal
2591 * @session: the session
2593 * Request an AUX receiver element for the given @session. The AUX
2594 * element will be added to the bin.
2596 * If no handler is connected, no AUX element will be used.
2600 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_RECEIVER] =
2601 g_signal_new ("request-aux-receiver", G_TYPE_FROM_CLASS (klass),
2602 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2603 request_aux_receiver), _gst_element_accumulator, NULL, NULL,
2604 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2607 * GstRtpBin::request-fec-decoder:
2608 * @rtpbin: the object which received the signal
2609 * @session: the session index
2611 * Request a FEC decoder element for the given @session. The element
2612 * will be added to the bin after the pt demuxer. If there are multiple
2613 * ssrc's and pt's in @session, this signal may be called multiple times for
2614 * the same @session each corresponding to a newly discovered ssrc.
2616 * If no handler is connected, no FEC decoder will be used.
2618 * Warning: usage of this signal is not appropriate for the BUNDLE case,
2619 * connect to #GstRtpBin::request-fec-decoder-full instead.
2623 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_DECODER] =
2624 g_signal_new ("request-fec-decoder", G_TYPE_FROM_CLASS (klass),
2625 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2626 request_fec_decoder), _gst_element_accumulator, NULL, NULL,
2627 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2630 * GstRtpBin::request-fec-decoder-full:
2631 * @rtpbin: the object which received the signal
2632 * @session: the session index
2633 * @ssrc: the ssrc of the stream
2634 * @pt: the payload type
2636 * Request a FEC decoder element for the given @session. The element
2637 * will be added to the bin after the pt demuxer. If there are multiple
2638 * ssrc's and pt's in @session, this signal may be called multiple times for
2639 * the same @session each corresponding to a newly discovered ssrc and payload
2640 * type, those are provided as parameters.
2642 * If no handler is connected, no FEC decoder will be used.
2646 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_DECODER_FULL] =
2647 g_signal_new ("request-fec-decoder-full", G_TYPE_FROM_CLASS (klass),
2648 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2649 request_fec_decoder), _gst_element_accumulator, NULL, NULL,
2650 GST_TYPE_ELEMENT, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
2653 * GstRtpBin::request-fec-encoder:
2654 * @rtpbin: the object which received the signal
2655 * @session: the session index
2657 * Request a FEC encoder element for the given @session. The element
2658 * will be added to the bin after the RTPSession.
2660 * If no handler is connected, no FEC encoder will be used.
2664 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_ENCODER] =
2665 g_signal_new ("request-fec-encoder", G_TYPE_FROM_CLASS (klass),
2666 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2667 request_fec_encoder), _gst_element_accumulator, NULL, NULL,
2668 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2671 * GstRtpBin::on-new-sender-ssrc:
2672 * @rtpbin: the object which received the signal
2673 * @session: the session
2674 * @ssrc: the sender SSRC
2676 * Notify of a new sender SSRC that entered @session.
2680 gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
2681 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
2682 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_sender_ssrc),
2683 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2685 * GstRtpBin::on-sender-ssrc-active:
2686 * @rtpbin: the object which received the signal
2687 * @session: the session
2688 * @ssrc: the sender SSRC
2690 * Notify of a sender SSRC that is active, i.e., sending RTCP.
2694 gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
2695 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
2696 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2697 on_sender_ssrc_active), NULL, NULL, NULL,
2698 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2700 g_object_class_install_property (gobject_class, PROP_SDES,
2701 g_param_spec_boxed ("sdes", "SDES",
2702 "The SDES items of this session",
2703 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
2704 | GST_PARAM_DOC_SHOW_DEFAULT));
2706 g_object_class_install_property (gobject_class, PROP_DO_LOST,
2707 g_param_spec_boolean ("do-lost", "Do Lost",
2708 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
2709 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2711 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
2712 g_param_spec_boolean ("autoremove", "Auto Remove",
2713 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
2714 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2716 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
2717 g_param_spec_boolean ("ignore-pt", "Ignore PT",
2718 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
2719 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2721 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
2722 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
2723 "Use the pipeline running-time to set the NTP time in the RTCP SR messages "
2724 "(DEPRECATED: Use ntp-time-source property)",
2725 DEFAULT_USE_PIPELINE_CLOCK,
2726 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
2728 * GstRtpBin:buffer-mode:
2730 * Control the buffering and timestamping mode used by the jitterbuffer.
2732 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
2733 g_param_spec_enum ("buffer-mode", "Buffer Mode",
2734 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
2735 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2737 * GstRtpBin:ntp-sync:
2739 * Set the NTP time from the sender reports as the running-time on the
2740 * buffers. When both the sender and receiver have sychronized
2741 * running-time, i.e. when the clock and base-time is shared
2742 * between the receivers and the and the senders, this option can be
2743 * used to synchronize receivers on multiple machines.
2745 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
2746 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
2747 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
2748 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2751 * GstRtpBin:rtcp-sync:
2753 * If not synchronizing (directly) to the NTP clock, determines how to sync
2754 * the various streams.
2756 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
2757 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
2758 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
2759 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2762 * GstRtpBin:rtcp-sync-interval:
2764 * Determines how often to sync streams using RTCP data or inband NTP-64
2765 * header extensions.
2767 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
2768 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
2769 "RTCP SR / NTP-64 interval synchronization (ms) (0 = always)",
2770 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
2771 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2773 g_object_class_install_property (gobject_class, PROP_DO_SYNC_EVENT,
2774 g_param_spec_boolean ("do-sync-event", "Do Sync Event",
2775 "Send event downstream when a stream is synchronized to the sender",
2776 DEFAULT_DO_SYNC_EVENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2779 * GstRtpBin:do-retransmission:
2781 * Enables RTP retransmission on all streams. To control retransmission on
2782 * a per-SSRC basis, connect to the #GstRtpBin::new-jitterbuffer signal and
2783 * set the #GstRtpJitterBuffer:do-retransmission property on the
2784 * #GstRtpJitterBuffer object instead.
2786 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
2787 g_param_spec_boolean ("do-retransmission", "Do retransmission",
2788 "Enable retransmission on all streams",
2789 DEFAULT_DO_RETRANSMISSION,
2790 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2793 * GstRtpBin:rtp-profile:
2795 * Sets the default RTP profile of newly created RTP sessions. The
2796 * profile can be changed afterwards on a per-session basis.
2798 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
2799 g_param_spec_enum ("rtp-profile", "RTP Profile",
2800 "Default RTP profile of newly created sessions",
2801 GST_TYPE_RTP_PROFILE, DEFAULT_RTP_PROFILE,
2802 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2804 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
2805 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
2806 "NTP time source for RTCP packets",
2807 gst_rtp_ntp_time_source_get_type (), DEFAULT_NTP_TIME_SOURCE,
2808 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2810 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_SEND_TIME,
2811 g_param_spec_boolean ("rtcp-sync-send-time", "RTCP Sync Send Time",
2812 "Use send time or capture time for RTCP sync "
2813 "(TRUE = send time, FALSE = capture time)",
2814 DEFAULT_RTCP_SYNC_SEND_TIME,
2815 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2817 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
2818 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
2819 "Maximum amount of time in ms that the RTP time in RTCP SRs "
2820 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
2821 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
2822 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2824 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
2825 g_param_spec_uint ("max-dropout-time", "Max dropout time",
2826 "The maximum time (milliseconds) of missing packets tolerated.",
2827 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
2828 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2830 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
2831 g_param_spec_uint ("max-misorder-time", "Max misorder time",
2832 "The maximum time (milliseconds) of misordered packets tolerated.",
2833 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
2834 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2836 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
2837 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
2838 "Synchronize received streams to the RFC7273 clock "
2839 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
2840 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2843 * GstRtpBin:add-reference-timestamp-meta:
2845 * When syncing to a RFC7273 clock or after clock synchronization via RTCP or
2846 * inband NTP-64 header extensions has happened, add #GstReferenceTimestampMeta
2847 * to buffers with the original reconstructed reference clock timestamp.
2851 g_object_class_install_property (gobject_class,
2852 PROP_ADD_REFERENCE_TIMESTAMP_META,
2853 g_param_spec_boolean ("add-reference-timestamp-meta",
2854 "Add Reference Timestamp Meta",
2855 "Add Reference Timestamp Meta to buffers with the original clock timestamp "
2856 "before any adjustments when syncing to an RFC7273 clock or after clock "
2857 "synchronization via RTCP or inband NTP-64 header extensions has happened.",
2858 DEFAULT_ADD_REFERENCE_TIMESTAMP_META,
2859 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2861 g_object_class_install_property (gobject_class, PROP_MAX_STREAMS,
2862 g_param_spec_uint ("max-streams", "Max Streams",
2863 "The maximum number of streams to create for one session",
2864 0, G_MAXUINT, DEFAULT_MAX_STREAMS,
2865 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2868 * GstRtpBin:max-ts-offset-adjustment:
2870 * Syncing time stamps to NTP time adds a time offset. This parameter
2871 * specifies the maximum number of nanoseconds per frame that this time offset
2872 * may be adjusted with. This is used to avoid sudden large changes to time
2877 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
2878 g_param_spec_uint64 ("max-ts-offset-adjustment",
2879 "Max Timestamp Offset Adjustment",
2880 "The maximum number of nanoseconds per frame that time stamp offsets "
2881 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
2882 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
2883 G_PARAM_STATIC_STRINGS));
2886 * GstRtpBin:max-ts-offset:
2888 * Used to set an upper limit of how large a time offset may be. This
2889 * is used to protect against unrealistic values as a result of either
2890 * client,server or clock issues.
2894 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
2895 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
2896 "The maximum absolute value of the time offset in (nanoseconds). "
2897 "Note, if the ntp-sync parameter is set the default value is "
2898 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
2899 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2902 * GstRtpBin:min-ts-offset:
2904 * Used to set an lower limit for when a time offset is deemed large enough
2905 * to be useful for sync corrections.
2907 * When streaming for instance audio, even very small ts_offsets cause
2908 * audible glitches. This property is used for controlling how sensitive the
2909 * adjustments should be to small deviations in ts_offset, occurring for
2910 * instance due to jittery network conditions or system load.
2914 g_object_class_install_property (gobject_class, PROP_MIN_TS_OFFSET,
2915 g_param_spec_uint64 ("min-ts-offset", "Min TS Offset",
2916 "The minimum absolute value of the time offset in (nanoseconds). "
2917 "Used to set an lower limit for when a time offset is deemed large "
2918 "enough to be useful for sync corrections."
2919 "Note, if the ntp-sync parameter is set the default value is "
2920 "changed to 0 (no limit)", 0, G_MAXUINT64, DEFAULT_MIN_TS_OFFSET,
2921 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2924 * GstRtpBin:ts-offset-smoothing-factor:
2926 * Controls the weighting between previous and current timestamp offsets in
2927 * a running moving average (RMA):
2928 * ts_offset_average(n) =
2929 * ((ts-offset-smoothing-factor - 1) * ts_offset_average(n - 1) + ts_offset(n)) /
2930 * ts-offset-smoothing-factor
2932 * This can stabilize the timestamp offset and prevent unnecessary skew
2933 * corrections due to jitter introduced by network or system load.
2937 g_object_class_install_property (gobject_class,
2938 PROP_TS_OFFSET_SMOOTHING_FACTOR,
2939 g_param_spec_uint ("ts-offset-smoothing-factor",
2940 "Timestamp Offset Smoothing Factor",
2941 "Sets a smoothing factor for the timestamp offset in number of "
2942 "values for a calculated running moving average. "
2943 "(0 = no smoothing factor)", 0, G_MAXUINT,
2944 DEFAULT_TS_OFFSET_SMOOTHING_FACTOR,
2945 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2948 * GstRtpBin:fec-decoders:
2950 * Used to provide a factory used to build the FEC decoder for a
2951 * given session, as a command line alternative to
2952 * #GstRtpBin::request-fec-decoder.
2954 * Expects a GstStructure in the form session_id (gint) -> factory (string)
2958 g_object_class_install_property (gobject_class, PROP_FEC_DECODERS,
2959 g_param_spec_boxed ("fec-decoders", "Fec Decoders",
2960 "GstStructure mapping from session index to FEC decoder "
2962 "fec-decoders='fec,0=\"rtpst2022-1-fecdec\\ size-time\\=1000000000\";'",
2963 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2966 * GstRtpBin:fec-encoders:
2968 * Used to provide a factory used to build the FEC encoder for a
2969 * given session, as a command line alternative to
2970 * #GstRtpBin::request-fec-encoder.
2972 * Expects a GstStructure in the form session_id (gint) -> factory (string)
2976 g_object_class_install_property (gobject_class, PROP_FEC_ENCODERS,
2977 g_param_spec_boxed ("fec-encoders", "Fec Encoders",
2978 "GstStructure mapping from session index to FEC encoder "
2980 "fec-encoders='fec,0=\"rtpst2022-1-fecenc\\ rows\\=5\\ columns\\=5\";'",
2981 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2983 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
2984 gstelement_class->request_new_pad =
2985 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
2986 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
2989 gst_element_class_add_static_pad_template (gstelement_class,
2990 &rtpbin_recv_rtp_sink_template);
2991 gst_element_class_add_static_pad_template (gstelement_class,
2992 &rtpbin_recv_fec_sink_template);
2993 gst_element_class_add_static_pad_template (gstelement_class,
2994 &rtpbin_recv_rtcp_sink_template);
2995 gst_element_class_add_static_pad_template (gstelement_class,
2996 &rtpbin_send_rtp_sink_template);
2999 gst_element_class_add_static_pad_template (gstelement_class,
3000 &rtpbin_recv_rtp_src_template);
3001 gst_element_class_add_static_pad_template (gstelement_class,
3002 &rtpbin_send_rtcp_src_template);
3003 gst_element_class_add_static_pad_template (gstelement_class,
3004 &rtpbin_send_rtp_src_template);
3005 gst_element_class_add_static_pad_template (gstelement_class,
3006 &rtpbin_send_fec_src_template);
3008 gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
3009 "Filter/Network/RTP",
3010 "Real-Time Transport Protocol bin",
3011 "Wim Taymans <wim.taymans@gmail.com>");
3013 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
3015 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
3016 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
3017 klass->get_session = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_session);
3018 klass->get_internal_session =
3019 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
3020 klass->get_storage = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_storage);
3021 klass->get_internal_storage =
3022 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_storage);
3023 klass->clear_ssrc = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_ssrc);
3024 klass->request_rtp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
3025 klass->request_rtp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
3026 klass->request_rtcp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
3027 klass->request_rtcp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
3028 klass->request_jitterbuffer =
3029 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_jitterbuffer);
3031 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
3033 gst_type_mark_as_plugin_api (GST_RTP_BIN_RTCP_SYNC_TYPE, 0);
3037 gst_rtp_bin_init (GstRtpBin * rtpbin)
3041 rtpbin->priv = gst_rtp_bin_get_instance_private (rtpbin);
3042 g_mutex_init (&rtpbin->priv->bin_lock);
3043 g_mutex_init (&rtpbin->priv->dyn_lock);
3045 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
3046 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
3047 rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
3048 rtpbin->do_lost = DEFAULT_DO_LOST;
3049 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
3050 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
3051 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
3052 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
3053 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
3054 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
3055 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
3056 rtpbin->send_sync_event = DEFAULT_DO_SYNC_EVENT;
3057 rtpbin->do_retransmission = DEFAULT_DO_RETRANSMISSION;
3058 rtpbin->rtp_profile = DEFAULT_RTP_PROFILE;
3059 rtpbin->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
3060 rtpbin->rtcp_sync_send_time = DEFAULT_RTCP_SYNC_SEND_TIME;
3061 rtpbin->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
3062 rtpbin->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
3063 rtpbin->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
3064 rtpbin->rfc7273_sync = DEFAULT_RFC7273_SYNC;
3065 rtpbin->add_reference_timestamp_meta = DEFAULT_ADD_REFERENCE_TIMESTAMP_META;
3066 rtpbin->max_streams = DEFAULT_MAX_STREAMS;
3067 rtpbin->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
3068 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
3069 rtpbin->max_ts_offset_is_set = FALSE;
3070 rtpbin->min_ts_offset = DEFAULT_MIN_TS_OFFSET;
3071 rtpbin->min_ts_offset_is_set = FALSE;
3072 rtpbin->ts_offset_smoothing_factor = DEFAULT_TS_OFFSET_SMOOTHING_FACTOR;
3074 /* some default SDES entries */
3075 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
3076 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
3077 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
3078 rtpbin->fec_decoders =
3079 gst_structure_new_empty ("application/x-rtp-fec-decoders");
3080 rtpbin->fec_encoders =
3081 gst_structure_new_empty ("application/x-rtp-fec-encoders");
3086 gst_rtp_bin_dispose (GObject * object)
3090 rtpbin = GST_RTP_BIN (object);
3092 GST_RTP_BIN_LOCK (rtpbin);
3093 GST_DEBUG_OBJECT (object, "freeing sessions");
3094 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
3095 g_slist_free (rtpbin->sessions);
3096 rtpbin->sessions = NULL;
3097 GST_RTP_BIN_UNLOCK (rtpbin);
3099 G_OBJECT_CLASS (parent_class)->dispose (object);
3103 gst_rtp_bin_finalize (GObject * object)
3107 rtpbin = GST_RTP_BIN (object);
3110 gst_structure_free (rtpbin->sdes);
3112 if (rtpbin->fec_decoders)
3113 gst_structure_free (rtpbin->fec_decoders);
3115 if (rtpbin->fec_encoders)
3116 gst_structure_free (rtpbin->fec_encoders);
3118 g_mutex_clear (&rtpbin->priv->bin_lock);
3119 g_mutex_clear (&rtpbin->priv->dyn_lock);
3121 G_OBJECT_CLASS (parent_class)->finalize (object);
3126 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
3133 GST_RTP_BIN_LOCK (bin);
3135 GST_OBJECT_LOCK (bin);
3137 gst_structure_free (bin->sdes);
3138 bin->sdes = gst_structure_copy (sdes);
3139 GST_OBJECT_UNLOCK (bin);
3141 /* store in all sessions */
3142 for (item = bin->sessions; item; item = g_slist_next (item)) {
3143 GstRtpBinSession *session = item->data;
3144 g_object_set (session->session, "sdes", sdes, NULL);
3147 GST_RTP_BIN_UNLOCK (bin);
3151 gst_rtp_bin_set_fec_decoders_struct (GstRtpBin * bin,
3152 const GstStructure * decoders)
3154 if (decoders == NULL)
3157 GST_RTP_BIN_LOCK (bin);
3159 GST_OBJECT_LOCK (bin);
3160 if (bin->fec_decoders)
3161 gst_structure_free (bin->fec_decoders);
3162 bin->fec_decoders = gst_structure_copy (decoders);
3164 GST_OBJECT_UNLOCK (bin);
3166 GST_RTP_BIN_UNLOCK (bin);
3170 gst_rtp_bin_set_fec_encoders_struct (GstRtpBin * bin,
3171 const GstStructure * encoders)
3173 if (encoders == NULL)
3176 GST_RTP_BIN_LOCK (bin);
3178 GST_OBJECT_LOCK (bin);
3179 if (bin->fec_encoders)
3180 gst_structure_free (bin->fec_encoders);
3181 bin->fec_encoders = gst_structure_copy (encoders);
3183 GST_OBJECT_UNLOCK (bin);
3185 GST_RTP_BIN_UNLOCK (bin);
3188 static GstStructure *
3189 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
3191 GstStructure *result;
3193 GST_OBJECT_LOCK (bin);
3194 result = gst_structure_copy (bin->sdes);
3195 GST_OBJECT_UNLOCK (bin);
3200 static GstStructure *
3201 gst_rtp_bin_get_fec_decoders_struct (GstRtpBin * bin)
3203 GstStructure *result;
3205 GST_OBJECT_LOCK (bin);
3206 result = gst_structure_copy (bin->fec_decoders);
3207 GST_OBJECT_UNLOCK (bin);
3212 static GstStructure *
3213 gst_rtp_bin_get_fec_encoders_struct (GstRtpBin * bin)
3215 GstStructure *result;
3217 GST_OBJECT_LOCK (bin);
3218 result = gst_structure_copy (bin->fec_encoders);
3219 GST_OBJECT_UNLOCK (bin);
3225 gst_rtp_bin_set_property (GObject * object, guint prop_id,
3226 const GValue * value, GParamSpec * pspec)
3230 rtpbin = GST_RTP_BIN (object);
3234 GST_RTP_BIN_LOCK (rtpbin);
3235 rtpbin->latency_ms = g_value_get_uint (value);
3236 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
3237 GST_RTP_BIN_UNLOCK (rtpbin);
3238 /* propagate the property down to the jitterbuffer */
3239 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
3241 case PROP_DROP_ON_LATENCY:
3242 GST_RTP_BIN_LOCK (rtpbin);
3243 rtpbin->drop_on_latency = g_value_get_boolean (value);
3244 GST_RTP_BIN_UNLOCK (rtpbin);
3245 /* propagate the property down to the jitterbuffer */
3246 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3247 "drop-on-latency", value);
3250 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
3253 GST_RTP_BIN_LOCK (rtpbin);
3254 rtpbin->do_lost = g_value_get_boolean (value);
3255 GST_RTP_BIN_UNLOCK (rtpbin);
3256 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
3259 rtpbin->ntp_sync = g_value_get_boolean (value);
3260 /* The default value of max_ts_offset depends on ntp_sync. If user
3261 * hasn't set it then change default value */
3262 if (!rtpbin->max_ts_offset_is_set) {
3263 if (rtpbin->ntp_sync) {
3264 rtpbin->max_ts_offset = 0;
3266 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
3269 if (!rtpbin->min_ts_offset_is_set) {
3270 if (rtpbin->ntp_sync) {
3271 rtpbin->min_ts_offset = 0;
3273 rtpbin->min_ts_offset = DEFAULT_MIN_TS_OFFSET;
3277 case PROP_RTCP_SYNC:
3278 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
3280 case PROP_RTCP_SYNC_INTERVAL:
3281 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
3283 case PROP_IGNORE_PT:
3284 rtpbin->ignore_pt = g_value_get_boolean (value);
3286 case PROP_AUTOREMOVE:
3287 rtpbin->priv->autoremove = g_value_get_boolean (value);
3289 case PROP_USE_PIPELINE_CLOCK:
3292 GST_RTP_BIN_LOCK (rtpbin);
3293 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
3294 for (sessions = rtpbin->sessions; sessions;
3295 sessions = g_slist_next (sessions)) {
3296 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3298 g_object_set (G_OBJECT (session->session),
3299 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
3301 GST_RTP_BIN_UNLOCK (rtpbin);
3304 case PROP_DO_SYNC_EVENT:
3305 rtpbin->send_sync_event = g_value_get_boolean (value);
3307 case PROP_BUFFER_MODE:
3308 GST_RTP_BIN_LOCK (rtpbin);
3309 rtpbin->buffer_mode = g_value_get_enum (value);
3310 GST_RTP_BIN_UNLOCK (rtpbin);
3311 /* propagate the property down to the jitterbuffer */
3312 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
3314 case PROP_DO_RETRANSMISSION:
3315 GST_RTP_BIN_LOCK (rtpbin);
3316 rtpbin->do_retransmission = g_value_get_boolean (value);
3317 GST_RTP_BIN_UNLOCK (rtpbin);
3318 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3319 "do-retransmission", value);
3321 case PROP_RTP_PROFILE:
3322 rtpbin->rtp_profile = g_value_get_enum (value);
3324 case PROP_NTP_TIME_SOURCE:{
3326 GST_RTP_BIN_LOCK (rtpbin);
3327 rtpbin->ntp_time_source = g_value_get_enum (value);
3328 for (sessions = rtpbin->sessions; sessions;
3329 sessions = g_slist_next (sessions)) {
3330 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3332 g_object_set (G_OBJECT (session->session),
3333 "ntp-time-source", rtpbin->ntp_time_source, NULL);
3335 GST_RTP_BIN_UNLOCK (rtpbin);
3338 case PROP_RTCP_SYNC_SEND_TIME:{
3340 GST_RTP_BIN_LOCK (rtpbin);
3341 rtpbin->rtcp_sync_send_time = g_value_get_boolean (value);
3342 for (sessions = rtpbin->sessions; sessions;
3343 sessions = g_slist_next (sessions)) {
3344 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3346 g_object_set (G_OBJECT (session->session),
3347 "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time, NULL);
3349 GST_RTP_BIN_UNLOCK (rtpbin);
3352 case PROP_MAX_RTCP_RTP_TIME_DIFF:
3353 GST_RTP_BIN_LOCK (rtpbin);
3354 rtpbin->max_rtcp_rtp_time_diff = g_value_get_int (value);
3355 GST_RTP_BIN_UNLOCK (rtpbin);
3356 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3357 "max-rtcp-rtp-time-diff", value);
3359 case PROP_MAX_DROPOUT_TIME:
3360 GST_RTP_BIN_LOCK (rtpbin);
3361 rtpbin->max_dropout_time = g_value_get_uint (value);
3362 GST_RTP_BIN_UNLOCK (rtpbin);
3363 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3364 "max-dropout-time", value);
3365 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-dropout-time",
3368 case PROP_MAX_MISORDER_TIME:
3369 GST_RTP_BIN_LOCK (rtpbin);
3370 rtpbin->max_misorder_time = g_value_get_uint (value);
3371 GST_RTP_BIN_UNLOCK (rtpbin);
3372 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3373 "max-misorder-time", value);
3374 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-misorder-time",
3377 case PROP_RFC7273_SYNC:
3378 rtpbin->rfc7273_sync = g_value_get_boolean (value);
3379 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3380 "rfc7273-sync", value);
3382 case PROP_ADD_REFERENCE_TIMESTAMP_META:
3383 rtpbin->add_reference_timestamp_meta = g_value_get_boolean (value);
3384 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3385 "add-reference-timestamp-meta", value);
3387 case PROP_MAX_STREAMS:
3388 rtpbin->max_streams = g_value_get_uint (value);
3390 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
3391 rtpbin->max_ts_offset_adjustment = g_value_get_uint64 (value);
3392 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3393 "max-ts-offset-adjustment", value);
3395 case PROP_MAX_TS_OFFSET:
3396 rtpbin->max_ts_offset = g_value_get_int64 (value);
3397 rtpbin->max_ts_offset_is_set = TRUE;
3399 case PROP_MIN_TS_OFFSET:
3400 rtpbin->min_ts_offset = g_value_get_uint64 (value);
3401 rtpbin->min_ts_offset_is_set = TRUE;
3403 case PROP_TS_OFFSET_SMOOTHING_FACTOR:
3404 rtpbin->ts_offset_smoothing_factor = g_value_get_uint (value);
3406 case PROP_FEC_DECODERS:
3407 gst_rtp_bin_set_fec_decoders_struct (rtpbin, g_value_get_boxed (value));
3409 case PROP_FEC_ENCODERS:
3410 gst_rtp_bin_set_fec_encoders_struct (rtpbin, g_value_get_boxed (value));
3413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3419 gst_rtp_bin_get_property (GObject * object, guint prop_id,
3420 GValue * value, GParamSpec * pspec)
3424 rtpbin = GST_RTP_BIN (object);
3428 GST_RTP_BIN_LOCK (rtpbin);
3429 g_value_set_uint (value, rtpbin->latency_ms);
3430 GST_RTP_BIN_UNLOCK (rtpbin);
3432 case PROP_DROP_ON_LATENCY:
3433 GST_RTP_BIN_LOCK (rtpbin);
3434 g_value_set_boolean (value, rtpbin->drop_on_latency);
3435 GST_RTP_BIN_UNLOCK (rtpbin);
3438 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
3441 GST_RTP_BIN_LOCK (rtpbin);
3442 g_value_set_boolean (value, rtpbin->do_lost);
3443 GST_RTP_BIN_UNLOCK (rtpbin);
3445 case PROP_IGNORE_PT:
3446 g_value_set_boolean (value, rtpbin->ignore_pt);
3449 g_value_set_boolean (value, rtpbin->ntp_sync);
3451 case PROP_RTCP_SYNC:
3452 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
3454 case PROP_RTCP_SYNC_INTERVAL:
3455 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
3457 case PROP_AUTOREMOVE:
3458 g_value_set_boolean (value, rtpbin->priv->autoremove);
3460 case PROP_BUFFER_MODE:
3461 g_value_set_enum (value, rtpbin->buffer_mode);
3463 case PROP_USE_PIPELINE_CLOCK:
3464 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
3466 case PROP_DO_SYNC_EVENT:
3467 g_value_set_boolean (value, rtpbin->send_sync_event);
3469 case PROP_DO_RETRANSMISSION:
3470 GST_RTP_BIN_LOCK (rtpbin);
3471 g_value_set_boolean (value, rtpbin->do_retransmission);
3472 GST_RTP_BIN_UNLOCK (rtpbin);
3474 case PROP_RTP_PROFILE:
3475 g_value_set_enum (value, rtpbin->rtp_profile);
3477 case PROP_NTP_TIME_SOURCE:
3478 g_value_set_enum (value, rtpbin->ntp_time_source);
3480 case PROP_RTCP_SYNC_SEND_TIME:
3481 g_value_set_boolean (value, rtpbin->rtcp_sync_send_time);
3483 case PROP_MAX_RTCP_RTP_TIME_DIFF:
3484 GST_RTP_BIN_LOCK (rtpbin);
3485 g_value_set_int (value, rtpbin->max_rtcp_rtp_time_diff);
3486 GST_RTP_BIN_UNLOCK (rtpbin);
3488 case PROP_MAX_DROPOUT_TIME:
3489 g_value_set_uint (value, rtpbin->max_dropout_time);
3491 case PROP_MAX_MISORDER_TIME:
3492 g_value_set_uint (value, rtpbin->max_misorder_time);
3494 case PROP_RFC7273_SYNC:
3495 g_value_set_boolean (value, rtpbin->rfc7273_sync);
3497 case PROP_ADD_REFERENCE_TIMESTAMP_META:
3498 g_value_set_boolean (value, rtpbin->add_reference_timestamp_meta);
3500 case PROP_MAX_STREAMS:
3501 g_value_set_uint (value, rtpbin->max_streams);
3503 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
3504 g_value_set_uint64 (value, rtpbin->max_ts_offset_adjustment);
3506 case PROP_MAX_TS_OFFSET:
3507 g_value_set_int64 (value, rtpbin->max_ts_offset);
3509 case PROP_MIN_TS_OFFSET:
3510 g_value_set_uint64 (value, rtpbin->min_ts_offset);
3512 case PROP_TS_OFFSET_SMOOTHING_FACTOR:
3513 g_value_set_uint (value, rtpbin->ts_offset_smoothing_factor);
3515 case PROP_FEC_DECODERS:
3516 g_value_take_boxed (value, gst_rtp_bin_get_fec_decoders_struct (rtpbin));
3518 case PROP_FEC_ENCODERS:
3519 g_value_take_boxed (value, gst_rtp_bin_get_fec_encoders_struct (rtpbin));
3522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3528 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
3532 rtpbin = GST_RTP_BIN (bin);
3534 switch (GST_MESSAGE_TYPE (message)) {
3535 case GST_MESSAGE_ELEMENT:
3537 const GstStructure *s = gst_message_get_structure (message);
3539 /* we change the structure name and add the session ID to it */
3540 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
3541 GstRtpBinSession *sess;
3543 /* find the session we set it as object data */
3544 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3545 "GstRTPBin.session");
3547 if (G_LIKELY (sess)) {
3548 message = gst_message_make_writable (message);
3549 s = gst_message_get_structure (message);
3550 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
3554 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3557 case GST_MESSAGE_BUFFERING:
3560 gint min_percent = 100;
3561 GSList *sessions, *streams;
3562 GstRtpBinStream *stream;
3563 gboolean change = FALSE, active = FALSE;
3564 GstClockTime min_out_time;
3565 GstBufferingMode mode;
3566 gint avg_in, avg_out;
3567 gint64 buffering_left;
3569 gst_message_parse_buffering (message, &percent);
3570 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
3574 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3575 "GstRTPBin.stream");
3577 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
3579 /* get the stream */
3580 if (G_LIKELY (stream)) {
3581 GST_RTP_BIN_LOCK (rtpbin);
3582 /* fill in the percent */
3583 stream->percent = percent;
3585 /* calculate the min value for all streams */
3586 for (sessions = rtpbin->sessions; sessions;
3587 sessions = g_slist_next (sessions)) {
3588 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3590 GST_RTP_SESSION_LOCK (session);
3591 if (session->streams) {
3592 for (streams = session->streams; streams;
3593 streams = g_slist_next (streams)) {
3594 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3596 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
3599 /* find min percent */
3600 if (min_percent > stream->percent)
3601 min_percent = stream->percent;
3604 GST_INFO_OBJECT (bin,
3605 "session has no streams, setting min_percent to 0");
3608 GST_RTP_SESSION_UNLOCK (session);
3610 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
3612 if (rtpbin->buffering) {
3613 if (min_percent == 100) {
3614 rtpbin->buffering = FALSE;
3619 if (min_percent < 100) {
3620 /* pause the streams */
3621 rtpbin->buffering = TRUE;
3626 GST_RTP_BIN_UNLOCK (rtpbin);
3628 gst_message_unref (message);
3630 /* make a new buffering message with the min value */
3632 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
3633 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
3636 if (G_UNLIKELY (change)) {
3638 guint64 running_time = 0;
3641 /* figure out the running time when we have a clock */
3642 if (G_LIKELY ((clock =
3643 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
3644 guint64 now, base_time;
3646 now = gst_clock_get_time (clock);
3647 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
3648 running_time = now - base_time;
3649 gst_object_unref (clock);
3651 GST_DEBUG_OBJECT (bin,
3652 "running time now %" GST_TIME_FORMAT,
3653 GST_TIME_ARGS (running_time));
3655 GST_RTP_BIN_LOCK (rtpbin);
3657 /* when we reactivate, calculate the offsets so that all streams have
3658 * an output time that is at least as big as the running_time */
3661 if (running_time > rtpbin->buffer_start) {
3662 offset = running_time - rtpbin->buffer_start;
3663 if (offset >= rtpbin->latency_ns)
3664 offset -= rtpbin->latency_ns;
3670 /* pause all streams */
3672 for (sessions = rtpbin->sessions; sessions;
3673 sessions = g_slist_next (sessions)) {
3674 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3676 GST_RTP_SESSION_LOCK (session);
3677 for (streams = session->streams; streams;
3678 streams = g_slist_next (streams)) {
3679 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3680 GstElement *element = stream->buffer;
3681 guint64 last_out = -1;
3683 if (g_signal_lookup ("set-active", G_OBJECT_TYPE (element)) != 0) {
3684 g_signal_emit_by_name (element, "set-active", active, offset,
3689 g_object_get (element, "percent", &stream->percent, NULL);
3693 if (min_out_time == -1 || last_out < min_out_time)
3694 min_out_time = last_out;
3697 GST_DEBUG_OBJECT (bin,
3698 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
3699 GST_TIME_FORMAT ", percent %d", element, active,
3700 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
3703 GST_RTP_SESSION_UNLOCK (session);
3705 GST_DEBUG_OBJECT (bin,
3706 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
3708 /* the buffer_start is the min out time of all paused jitterbuffers */
3710 rtpbin->buffer_start = min_out_time;
3712 GST_RTP_BIN_UNLOCK (rtpbin);
3715 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3720 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3726 static GstStateChangeReturn
3727 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
3729 GstStateChangeReturn res;
3731 GstRtpBinPrivate *priv;
3733 rtpbin = GST_RTP_BIN (element);
3734 priv = rtpbin->priv;
3736 switch (transition) {
3737 case GST_STATE_CHANGE_NULL_TO_READY:
3739 case GST_STATE_CHANGE_READY_TO_PAUSED:
3740 priv->last_ntpnstime = 0;
3741 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
3742 g_atomic_int_set (&priv->shutdown, 0);
3744 case GST_STATE_CHANGE_PAUSED_TO_READY:
3745 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
3746 g_atomic_int_set (&priv->shutdown, 1);
3747 /* wait for all callbacks to end by taking the lock. No new callbacks will
3748 * be able to happen as we set the shutdown flag. */
3749 GST_RTP_BIN_DYN_LOCK (rtpbin);
3750 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
3751 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3757 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3759 switch (transition) {
3760 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3762 case GST_STATE_CHANGE_PAUSED_TO_READY:
3764 case GST_STATE_CHANGE_READY_TO_NULL:
3773 session_request_element_full (GstRtpBinSession * session, guint signal,
3774 guint ssrc, guint8 pt)
3776 GstElement *element = NULL;
3777 GstRtpBin *bin = session->bin;
3779 g_signal_emit (bin, gst_rtp_bin_signals[signal], 0, session->id, ssrc, pt,
3783 if (!bin_manage_element (bin, element))
3785 session->elements = g_slist_prepend (session->elements, element);
3792 GST_WARNING_OBJECT (bin, "unable to manage element");
3793 gst_object_unref (element);
3799 session_request_element (GstRtpBinSession * session, guint signal)
3801 GstElement *element = NULL;
3802 GstRtpBin *bin = session->bin;
3804 g_signal_emit (bin, gst_rtp_bin_signals[signal], 0, session->id, &element);
3807 if (!bin_manage_element (bin, element))
3809 session->elements = g_slist_prepend (session->elements, element);
3816 GST_WARNING_OBJECT (bin, "unable to manage element");
3817 gst_object_unref (element);
3823 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3825 GstPad *gpad = GST_PAD_CAST (user_data);
3827 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3828 gst_pad_store_sticky_event (gpad, *event);
3834 ensure_early_fec_decoder (GstRtpBin * rtpbin, GstRtpBinSession * session)
3836 const gchar *factory;
3839 if (session->early_fec_decoder)
3842 sess_id_str = g_strdup_printf ("%u", session->id);
3843 factory = gst_structure_get_string (rtpbin->fec_decoders, sess_id_str);
3844 g_free (sess_id_str);
3846 /* First try the property */
3850 session->early_fec_decoder =
3851 gst_parse_bin_from_description_full (factory, TRUE, NULL,
3852 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_FATAL_ERRORS,
3854 if (!session->early_fec_decoder) {
3855 GST_ERROR_OBJECT (rtpbin, "Failed to build decoder from factory: %s",
3859 bin_manage_element (session->bin, session->early_fec_decoder);
3861 g_slist_prepend (session->elements, session->early_fec_decoder);
3862 GST_INFO_OBJECT (rtpbin, "Built FEC decoder: %" GST_PTR_FORMAT
3863 " for session %u", session->early_fec_decoder, session->id);
3866 /* Do not fallback to the signal as the signal expects a fec decoder to
3867 * be placed at a different place in the pipeline */
3870 return session->early_fec_decoder != NULL;
3874 expose_recv_src_pad (GstRtpBin * rtpbin, GstPad * pad, GstRtpBinStream * stream,
3877 GstElementClass *klass;
3878 GstPadTemplate *templ;
3882 gst_object_ref (pad);
3884 if (stream->session->storage) {
3885 /* First try the legacy signal, with no ssrc and pt as parameters.
3886 * This will likely cause issues for the BUNDLE case. */
3887 GstElement *fec_decoder =
3888 session_request_element (stream->session, SIGNAL_REQUEST_FEC_DECODER);
3890 /* Now try the new signal, where the application can provide a FEC
3891 * decoder according to ssrc and pt. */
3894 session_request_element_full (stream->session,
3895 SIGNAL_REQUEST_FEC_DECODER_FULL, stream->ssrc, pt);
3899 GstPad *sinkpad, *srcpad;
3900 GstPadLinkReturn ret;
3902 sinkpad = gst_element_get_static_pad (fec_decoder, "sink");
3905 goto fec_decoder_sink_failed;
3907 ret = gst_pad_link (pad, sinkpad);
3908 gst_object_unref (sinkpad);
3910 if (ret != GST_PAD_LINK_OK)
3911 goto fec_decoder_link_failed;
3913 srcpad = gst_element_get_static_pad (fec_decoder, "src");
3916 goto fec_decoder_src_failed;
3918 gst_pad_sticky_events_foreach (pad, copy_sticky_events, srcpad);
3919 gst_object_unref (pad);
3924 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3926 /* ghost the pad to the parent */
3927 klass = GST_ELEMENT_GET_CLASS (rtpbin);
3928 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3929 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3930 stream->session->id, stream->ssrc, pt);
3931 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3933 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
3935 gst_pad_set_active (gpad, TRUE);
3936 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3938 gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3939 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3942 gst_object_unref (pad);
3948 GST_DEBUG ("ignoring, we are shutting down");
3951 fec_decoder_sink_failed:
3953 g_warning ("rtpbin: failed to get fec encoder sink pad for session %u",
3954 stream->session->id);
3957 fec_decoder_src_failed:
3959 g_warning ("rtpbin: failed to get fec encoder src pad for session %u",
3960 stream->session->id);
3963 fec_decoder_link_failed:
3965 g_warning ("rtpbin: failed to link fec decoder for session %u",
3966 stream->session->id);
3971 /* a new pad (SSRC) was created in @session. This signal is emitted from the
3972 * payload demuxer. */
3974 new_payload_found (GstElement * element, guint pt, GstPad * pad,
3975 GstRtpBinStream * stream)
3979 rtpbin = stream->bin;
3981 GST_DEBUG_OBJECT (rtpbin, "new payload pad %u", pt);
3983 expose_recv_src_pad (rtpbin, pad, stream, pt);
3987 payload_pad_removed (GstElement * element, GstPad * pad,
3988 GstRtpBinStream * stream)
3993 rtpbin = stream->bin;
3995 GST_DEBUG ("payload pad removed");
3997 GST_RTP_BIN_DYN_LOCK (rtpbin);
3998 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
3999 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
4001 gst_pad_set_active (gpad, FALSE);
4002 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
4004 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
4008 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
4013 rtpbin = session->bin;
4015 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %u in session %u", pt,
4018 caps = get_pt_map (session, pt);
4027 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
4033 ptdemux_pt_map_requested (GstElement * element, guint pt,
4034 GstRtpBinSession * session)
4036 GstCaps *ret = pt_map_requested (element, pt, session);
4038 if (ret && gst_caps_get_size (ret) == 1) {
4039 const GstStructure *s = gst_caps_get_structure (ret, 0);
4042 if (gst_structure_get_boolean (s, "is-fec", &is_fec) && is_fec) {
4043 GValue v = G_VALUE_INIT;
4044 GValue v2 = G_VALUE_INIT;
4046 GST_INFO_OBJECT (session->bin, "Will ignore FEC pt %u in session %u", pt,
4048 g_value_init (&v, GST_TYPE_ARRAY);
4049 g_value_init (&v2, G_TYPE_INT);
4050 g_object_get_property (G_OBJECT (element), "ignored-payload-types", &v);
4051 g_value_set_int (&v2, pt);
4052 gst_value_array_append_value (&v, &v2);
4053 g_value_unset (&v2);
4054 g_object_set_property (G_OBJECT (element), "ignored-payload-types", &v);
4063 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
4065 GST_DEBUG_OBJECT (session->bin,
4066 "emitting signal for pt type changed to %u in session %u", pt,
4069 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
4070 0, session->id, pt);
4073 /* emitted when caps changed for the session */
4075 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
4080 const GstStructure *s;
4084 g_object_get (pad, "caps", &caps, NULL);
4089 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
4091 s = gst_caps_get_structure (caps, 0);
4093 /* get payload, finish when it's not there */
4094 if (!gst_structure_get_int (s, "payload", &payload)) {
4095 gst_caps_unref (caps);
4099 GST_RTP_SESSION_LOCK (session);
4100 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
4101 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
4102 GST_RTP_SESSION_UNLOCK (session);
4105 /* a new pad (SSRC) was created in @session */
4107 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
4108 GstRtpBinSession * session)
4111 GstRtpBinStream *stream;
4112 GstPad *sinkpad, *srcpad;
4115 rtpbin = session->bin;
4117 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
4118 GST_DEBUG_PAD_NAME (pad));
4120 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
4122 GST_RTP_SESSION_LOCK (session);
4124 /* create new stream */
4125 stream = create_stream (session, ssrc);
4129 /* get pad and link */
4130 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
4131 padname = g_strdup_printf ("src_%u", ssrc);
4132 srcpad = gst_element_get_static_pad (element, padname);
4135 if (session->early_fec_decoder) {
4136 GST_DEBUG_OBJECT (rtpbin, "linking fec decoder");
4137 sinkpad = gst_element_get_static_pad (session->early_fec_decoder, "sink");
4138 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
4139 gst_object_unref (sinkpad);
4140 gst_object_unref (srcpad);
4141 srcpad = gst_element_get_static_pad (session->early_fec_decoder, "src");
4144 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
4145 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
4146 gst_object_unref (sinkpad);
4147 gst_object_unref (srcpad);
4149 sinkpad = gst_element_request_pad_simple (stream->buffer, "sink_rtcp");
4151 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
4152 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
4153 srcpad = gst_element_get_static_pad (element, padname);
4155 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
4156 gst_object_unref (sinkpad);
4157 gst_object_unref (srcpad);
4160 if (g_signal_lookup ("handle-sync", G_OBJECT_TYPE (stream->buffer)) != 0) {
4161 /* connect to the RTCP sync signal from the jitterbuffer */
4162 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
4163 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
4164 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
4167 if (stream->demux) {
4168 /* connect to the new-pad signal of the payload demuxer, this will expose the
4169 * new pad by ghosting it. */
4170 stream->demux_newpad_sig = g_signal_connect (stream->demux,
4171 "new-payload-type", (GCallback) new_payload_found, stream);
4172 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
4173 "pad-removed", (GCallback) payload_pad_removed, stream);
4175 /* connect to the request-pt-map signal. This signal will be emitted by the
4176 * demuxer so that it can apply a proper caps on the buffers for the
4178 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
4179 "request-pt-map", (GCallback) ptdemux_pt_map_requested, session);
4180 /* connect to the signal so it can be forwarded. */
4181 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
4182 "payload-type-change", (GCallback) payload_type_change, session);
4184 GST_RTP_SESSION_UNLOCK (session);
4185 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
4187 /* add rtpjitterbuffer src pad to pads */
4190 pad = gst_element_get_static_pad (stream->buffer, "src");
4192 GST_RTP_SESSION_UNLOCK (session);
4193 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
4195 expose_recv_src_pad (rtpbin, pad, stream, 255);
4197 gst_object_unref (pad);
4205 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
4210 GST_RTP_SESSION_UNLOCK (session);
4211 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
4212 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
4218 complete_session_sink (GstRtpBin * rtpbin, GstRtpBinSession * session)
4220 guint sessid = session->id;
4221 GstPad *recv_rtp_sink;
4222 GstElement *decoder;
4224 g_assert (!session->recv_rtp_sink);
4226 /* get recv_rtp pad and store */
4227 session->recv_rtp_sink =
4228 gst_element_request_pad_simple (session->session, "recv_rtp_sink");
4229 if (session->recv_rtp_sink == NULL)
4232 g_signal_connect (session->recv_rtp_sink, "notify::caps",
4233 (GCallback) caps_changed, session);
4235 GST_DEBUG_OBJECT (rtpbin, "requesting RTP decoder");
4236 decoder = session_request_element (session, SIGNAL_REQUEST_RTP_DECODER);
4238 GstPad *decsrc, *decsink;
4239 GstPadLinkReturn ret;
4241 GST_DEBUG_OBJECT (rtpbin, "linking RTP decoder");
4242 decsink = gst_element_get_static_pad (decoder, "rtp_sink");
4243 if (decsink == NULL)
4244 goto dec_sink_failed;
4246 recv_rtp_sink = decsink;
4248 decsrc = gst_element_get_static_pad (decoder, "rtp_src");
4250 goto dec_src_failed;
4252 ret = gst_pad_link (decsrc, session->recv_rtp_sink);
4254 gst_object_unref (decsrc);
4256 if (ret != GST_PAD_LINK_OK)
4257 goto dec_link_failed;
4260 GST_DEBUG_OBJECT (rtpbin, "no RTP decoder given");
4261 recv_rtp_sink = gst_object_ref (session->recv_rtp_sink);
4264 return recv_rtp_sink;
4269 g_warning ("rtpbin: failed to get session recv_rtp_sink pad");
4274 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
4279 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
4280 gst_object_unref (recv_rtp_sink);
4285 g_warning ("rtpbin: failed to link rtp decoder for session %u", sessid);
4286 gst_object_unref (recv_rtp_sink);
4292 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
4296 GstPad *recv_rtp_src;
4298 g_assert (!session->recv_rtp_src);
4300 session->recv_rtp_src =
4301 gst_element_get_static_pad (session->session, "recv_rtp_src");
4302 if (session->recv_rtp_src == NULL)
4305 /* find out if we need AUX elements */
4306 aux = session_request_element (session, SIGNAL_REQUEST_AUX_RECEIVER);
4310 GstPadLinkReturn ret;
4312 GST_DEBUG_OBJECT (rtpbin, "linking AUX receiver");
4314 pname = g_strdup_printf ("sink_%u", sessid);
4315 auxsink = gst_element_get_static_pad (aux, pname);
4317 if (auxsink == NULL)
4318 goto aux_sink_failed;
4320 ret = gst_pad_link (session->recv_rtp_src, auxsink);
4321 gst_object_unref (auxsink);
4322 if (ret != GST_PAD_LINK_OK)
4323 goto aux_link_failed;
4325 /* this can be NULL when this AUX element is not to be linked any further */
4326 pname = g_strdup_printf ("src_%u", sessid);
4327 recv_rtp_src = gst_element_get_static_pad (aux, pname);
4330 recv_rtp_src = gst_object_ref (session->recv_rtp_src);
4333 /* Add a storage element if needed */
4334 if (recv_rtp_src && session->storage) {
4335 GstPadLinkReturn ret;
4336 GstPad *sinkpad = gst_element_get_static_pad (session->storage, "sink");
4338 ret = gst_pad_link (recv_rtp_src, sinkpad);
4340 gst_object_unref (sinkpad);
4341 gst_object_unref (recv_rtp_src);
4343 if (ret != GST_PAD_LINK_OK)
4344 goto storage_link_failed;
4346 recv_rtp_src = gst_element_get_static_pad (session->storage, "src");
4352 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
4353 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
4354 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
4355 gst_pad_link_full (recv_rtp_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
4356 gst_object_unref (sinkdpad);
4357 gst_object_unref (recv_rtp_src);
4359 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
4360 session->demux_newpad_sig = g_signal_connect (session->demux,
4361 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
4362 session->demux_padremoved_sig = g_signal_connect (session->demux,
4363 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
4370 g_warning ("rtpbin: failed to get session recv_rtp_src pad");
4375 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4380 g_warning ("rtpbin: failed to link AUX pad to session %u", sessid);
4383 storage_link_failed:
4385 g_warning ("rtpbin: failed to link storage");
4390 /* Create a pad for receiving RTP for the session in @name. Must be called with
4394 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
4397 GstRtpBinSession *session;
4398 GstPad *recv_rtp_sink;
4400 /* first get the session number */
4401 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
4404 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
4406 /* get or create session */
4407 session = find_session_by_id (rtpbin, sessid);
4409 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4410 /* create session now */
4411 session = create_session (rtpbin, sessid);
4412 if (session == NULL)
4416 /* check if pad was requested */
4417 if (session->recv_rtp_sink_ghost != NULL)
4418 return session->recv_rtp_sink_ghost;
4420 /* setup the session sink pad */
4421 recv_rtp_sink = complete_session_sink (rtpbin, session);
4423 goto session_sink_failed;
4425 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
4426 session->recv_rtp_sink_ghost =
4427 gst_ghost_pad_new_from_template (name, recv_rtp_sink, templ);
4428 gst_object_unref (recv_rtp_sink);
4430 complete_session_receiver (rtpbin, session, sessid);
4432 return session->recv_rtp_sink_ghost;
4437 g_warning ("rtpbin: cannot find session id for pad: %s",
4438 GST_STR_NULL (name));
4443 /* create_session already warned */
4446 session_sink_failed:
4448 /* warning already done */
4454 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4456 if (session->demux_newpad_sig) {
4457 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
4458 session->demux_newpad_sig = 0;
4460 if (session->demux_padremoved_sig) {
4461 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
4462 session->demux_padremoved_sig = 0;
4464 if (session->recv_rtp_src) {
4465 gst_object_unref (session->recv_rtp_src);
4466 session->recv_rtp_src = NULL;
4468 if (session->recv_rtp_sink) {
4469 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
4470 gst_object_unref (session->recv_rtp_sink);
4471 session->recv_rtp_sink = NULL;
4473 if (session->recv_rtp_sink_ghost) {
4474 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
4475 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4476 session->recv_rtp_sink_ghost);
4477 session->recv_rtp_sink_ghost = NULL;
4482 fec_sinkpad_find (const GValue * item, gchar * padname)
4484 GstPad *pad = g_value_get_object (item);
4485 return g_strcmp0 (GST_PAD_NAME (pad), padname);
4489 complete_session_fec (GstRtpBin * rtpbin, GstRtpBinSession * session,
4492 gboolean have_static_pad;
4497 GValue item = { 0, };
4499 if (!ensure_early_fec_decoder (rtpbin, session))
4502 padname = g_strdup_printf ("fec_%u", fec_idx);
4504 GST_DEBUG_OBJECT (rtpbin, "getting FEC sink pad %s", padname);
4506 /* First try to find the decoder static pad that matches the padname */
4507 it = gst_element_iterate_sink_pads (session->early_fec_decoder);
4509 gst_iterator_find_custom (it, (GCompareFunc) fec_sinkpad_find, &item,
4512 if (have_static_pad) {
4513 ret = g_value_get_object (&item);
4514 gst_object_ref (ret);
4515 g_value_unset (&item);
4517 ret = gst_element_request_pad_simple (session->early_fec_decoder, padname);
4521 gst_iterator_free (it);
4526 session->recv_fec_sinks = g_slist_prepend (session->recv_fec_sinks, ret);
4532 g_warning ("rtpbin: failed to get decoder fec pad");
4537 g_warning ("rtpbin: failed to build FEC decoder for session %u",
4544 complete_session_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session,
4547 GstElement *decoder;
4549 GstPad *decsink = NULL;
4551 /* get recv_rtp pad and store */
4552 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
4553 session->recv_rtcp_sink =
4554 gst_element_request_pad_simple (session->session, "recv_rtcp_sink");
4555 if (session->recv_rtcp_sink == NULL)
4558 GST_DEBUG_OBJECT (rtpbin, "getting RTCP decoder");
4559 decoder = session_request_element (session, SIGNAL_REQUEST_RTCP_DECODER);
4562 GstPadLinkReturn ret;
4564 GST_DEBUG_OBJECT (rtpbin, "linking RTCP decoder");
4565 decsink = gst_element_get_static_pad (decoder, "rtcp_sink");
4566 decsrc = gst_element_get_static_pad (decoder, "rtcp_src");
4568 if (decsink == NULL)
4569 goto dec_sink_failed;
4572 goto dec_src_failed;
4574 ret = gst_pad_link (decsrc, session->recv_rtcp_sink);
4576 gst_object_unref (decsrc);
4578 if (ret != GST_PAD_LINK_OK)
4579 goto dec_link_failed;
4581 GST_DEBUG_OBJECT (rtpbin, "no RTCP decoder given");
4582 decsink = gst_object_ref (session->recv_rtcp_sink);
4585 /* get srcpad, link to SSRCDemux */
4586 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
4587 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
4588 if (session->sync_src == NULL)
4589 goto src_pad_failed;
4591 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
4592 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
4593 gst_pad_link_full (session->sync_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
4594 gst_object_unref (sinkdpad);
4600 g_warning ("rtpbin: failed to get session rtcp_sink pad");
4605 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
4610 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
4615 g_warning ("rtpbin: failed to link rtcp decoder for session %u", sessid);
4620 g_warning ("rtpbin: failed to get session sync_src pad");
4624 gst_object_unref (decsink);
4628 /* Create a pad for receiving RTCP for the session in @name. Must be called with
4632 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
4636 GstRtpBinSession *session;
4637 GstPad *decsink = NULL;
4639 /* first get the session number */
4640 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
4643 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
4645 /* get or create the session */
4646 session = find_session_by_id (rtpbin, sessid);
4648 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4649 /* create session now */
4650 session = create_session (rtpbin, sessid);
4651 if (session == NULL)
4655 /* check if pad was requested */
4656 if (session->recv_rtcp_sink_ghost != NULL)
4657 return session->recv_rtcp_sink_ghost;
4659 decsink = complete_session_rtcp (rtpbin, session, sessid);
4663 session->recv_rtcp_sink_ghost =
4664 gst_ghost_pad_new_from_template (name, decsink, templ);
4665 gst_object_unref (decsink);
4667 return session->recv_rtcp_sink_ghost;
4672 g_warning ("rtpbin: cannot find session id for pad: %s",
4673 GST_STR_NULL (name));
4678 /* create_session already warned */
4684 create_recv_fec (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
4686 guint sessid, fec_idx;
4687 GstRtpBinSession *session;
4688 GstPad *decsink = NULL;
4691 /* first get the session number */
4693 || sscanf (name, "recv_fec_sink_%u_%u", &sessid, &fec_idx) != 2)
4699 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
4701 /* get or create the session */
4702 session = find_session_by_id (rtpbin, sessid);
4704 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4705 /* create session now */
4706 session = create_session (rtpbin, sessid);
4707 if (session == NULL)
4711 decsink = complete_session_fec (rtpbin, session, fec_idx);
4715 ghost = gst_ghost_pad_new_from_template (name, decsink, templ);
4716 session->recv_fec_sink_ghosts =
4717 g_slist_prepend (session->recv_fec_sink_ghosts, ghost);
4718 gst_object_unref (decsink);
4725 g_warning ("rtpbin: cannot find session id for pad: %s",
4726 GST_STR_NULL (name));
4731 g_warning ("rtpbin: invalid FEC index: %s", GST_STR_NULL (name));
4736 /* create_session already warned */
4742 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4744 if (session->recv_rtcp_sink_ghost) {
4745 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
4746 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4747 session->recv_rtcp_sink_ghost);
4748 session->recv_rtcp_sink_ghost = NULL;
4750 if (session->sync_src) {
4751 /* releasing the request pad should also unref the sync pad */
4752 gst_object_unref (session->sync_src);
4753 session->sync_src = NULL;
4755 if (session->recv_rtcp_sink) {
4756 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
4757 gst_object_unref (session->recv_rtcp_sink);
4758 session->recv_rtcp_sink = NULL;
4763 remove_recv_fec_for_pad (GstRtpBin * rtpbin, GstRtpBinSession * session,
4769 target = gst_ghost_pad_get_target (GST_GHOST_PAD (ghost));
4772 item = g_slist_find (session->recv_fec_sinks, target);
4774 GstPadTemplate *templ;
4778 templ = gst_pad_get_pad_template (pad);
4780 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_REQUEST) {
4781 GST_DEBUG_OBJECT (rtpbin,
4782 "Releasing FEC decoder pad %" GST_PTR_FORMAT, pad);
4783 gst_element_release_request_pad (session->early_fec_decoder, pad);
4785 gst_object_unref (pad);
4788 session->recv_fec_sinks =
4789 g_slist_delete_link (session->recv_fec_sinks, item);
4791 gst_object_unref (templ);
4793 gst_object_unref (target);
4796 item = g_slist_find (session->recv_fec_sink_ghosts, ghost);
4798 session->recv_fec_sink_ghosts =
4799 g_slist_delete_link (session->recv_fec_sink_ghosts, item);
4801 gst_pad_set_active (ghost, FALSE);
4802 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), ghost);
4806 remove_recv_fec (GstRtpBin * rtpbin, GstRtpBinSession * session)
4811 copy = g_slist_copy (session->recv_fec_sink_ghosts);
4813 for (tmp = copy; tmp; tmp = tmp->next) {
4814 remove_recv_fec_for_pad (rtpbin, session, (GstPad *) tmp->data);
4817 g_slist_free (copy);
4821 complete_session_src (GstRtpBin * rtpbin, GstRtpBinSession * session)
4824 guint sessid = session->id;
4825 GstPad *send_rtp_src;
4826 GstElement *encoder;
4827 GstElementClass *klass;
4828 GstPadTemplate *templ;
4829 gboolean ret = FALSE;
4832 send_rtp_src = gst_element_get_static_pad (session->session, "send_rtp_src");
4834 if (send_rtp_src == NULL)
4837 GST_DEBUG_OBJECT (rtpbin, "getting RTP encoder");
4838 encoder = session_request_element (session, SIGNAL_REQUEST_RTP_ENCODER);
4841 GstPad *encsrc, *encsink;
4842 GstPadLinkReturn ret;
4844 GST_DEBUG_OBJECT (rtpbin, "linking RTP encoder");
4845 ename = g_strdup_printf ("rtp_src_%u", sessid);
4846 encsrc = gst_element_get_static_pad (encoder, ename);
4850 goto enc_src_failed;
4852 ename = g_strdup_printf ("rtp_sink_%u", sessid);
4853 encsink = gst_element_get_static_pad (encoder, ename);
4855 if (encsink == NULL)
4856 goto enc_sink_failed;
4858 ret = gst_pad_link (send_rtp_src, encsink);
4859 gst_object_unref (encsink);
4860 gst_object_unref (send_rtp_src);
4862 send_rtp_src = encsrc;
4864 if (ret != GST_PAD_LINK_OK)
4865 goto enc_link_failed;
4867 GST_DEBUG_OBJECT (rtpbin, "no RTP encoder given");
4870 /* ghost the new source pad */
4871 klass = GST_ELEMENT_GET_CLASS (rtpbin);
4872 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
4873 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
4874 session->send_rtp_src_ghost =
4875 gst_ghost_pad_new_from_template (gname, send_rtp_src, templ);
4876 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
4877 gst_pad_sticky_events_foreach (send_rtp_src, copy_sticky_events,
4878 session->send_rtp_src_ghost);
4879 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
4886 gst_object_unref (send_rtp_src);
4893 g_warning ("rtpbin: failed to get rtp source pad for session %u", sessid);
4898 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4899 " src pad for session %u", encoder, sessid);
4904 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4905 " sink pad for session %u", encoder, sessid);
4910 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4917 setup_aux_sender_fold (const GValue * item, GValue * result, gpointer user_data)
4922 GstRtpBinSession *session = user_data, *newsess;
4923 GstRtpBin *rtpbin = session->bin;
4924 GstPadLinkReturn ret;
4926 pad = g_value_get_object (item);
4927 name = gst_pad_get_name (pad);
4929 if (name == NULL || sscanf (name, "src_%u", &sessid) != 1)
4934 newsess = find_session_by_id (rtpbin, sessid);
4935 if (newsess == NULL) {
4936 /* create new session */
4937 newsess = create_session (rtpbin, sessid);
4938 if (newsess == NULL)
4940 } else if (newsess->send_rtp_sink != NULL)
4941 goto existing_session;
4943 /* get send_rtp pad and store */
4944 newsess->send_rtp_sink =
4945 gst_element_request_pad_simple (newsess->session, "send_rtp_sink");
4946 if (newsess->send_rtp_sink == NULL)
4949 ret = gst_pad_link (pad, newsess->send_rtp_sink);
4950 if (ret != GST_PAD_LINK_OK)
4951 goto aux_link_failed;
4953 if (!complete_session_src (rtpbin, newsess))
4954 goto session_src_failed;
4961 GST_WARNING ("ignoring invalid pad name %s", GST_STR_NULL (name));
4967 /* create_session already warned */
4972 GST_DEBUG_OBJECT (rtpbin,
4973 "skipping src_%i setup, since it is already configured.", sessid);
4978 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4983 g_warning ("rtpbin: failed to link AUX for session %u", sessid);
4988 g_warning ("rtpbin: failed to complete AUX for session %u", sessid);
4994 setup_aux_sender (GstRtpBin * rtpbin, GstRtpBinSession * session,
4998 GValue result = { 0, };
4999 GstIteratorResult res;
5001 it = gst_element_iterate_src_pads (aux);
5002 res = gst_iterator_fold (it, setup_aux_sender_fold, &result, session);
5003 gst_iterator_free (it);
5005 return res == GST_ITERATOR_DONE;
5009 fec_encoder_add_pad_unlocked (GstPad * pad, GstRtpBinSession * session)
5011 GstElementClass *klass;
5013 GstPadTemplate *templ;
5017 if (sscanf (GST_PAD_NAME (pad), "fec_%u", &fec_idx) != 1) {
5018 GST_WARNING_OBJECT (session->bin,
5019 "FEC encoder added pad with name not matching fec_%%u (%s)",
5020 GST_PAD_NAME (pad));
5024 GST_INFO_OBJECT (session->bin, "FEC encoder for session %u exposed new pad",
5027 klass = GST_ELEMENT_GET_CLASS (session->bin);
5028 gname = g_strdup_printf ("send_fec_src_%u_%u", session->id, fec_idx);
5029 templ = gst_element_class_get_pad_template (klass, "send_fec_src_%u_%u");
5030 ghost = gst_ghost_pad_new_from_template (gname, pad, templ);
5031 session->send_fec_src_ghosts =
5032 g_slist_prepend (session->send_fec_src_ghosts, ghost);
5033 gst_pad_set_active (ghost, TRUE);
5034 gst_pad_sticky_events_foreach (pad, copy_sticky_events, ghost);
5035 gst_element_add_pad (GST_ELEMENT (session->bin), ghost);
5043 fec_encoder_add_pad (GstPad * pad, GstRtpBinSession * session)
5045 GST_RTP_BIN_LOCK (session->bin);
5046 fec_encoder_add_pad_unlocked (pad, session);
5047 GST_RTP_BIN_UNLOCK (session->bin);
5051 fec_srcpad_iterator_filter (const GValue * item, GValue * unused)
5054 GstPad *pad = g_value_get_object (item);
5055 GstPadTemplate *templ = gst_pad_get_pad_template (pad);
5057 gint have_static_pad =
5058 (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_ALWAYS) &&
5059 (sscanf (GST_PAD_NAME (pad), "fec_%u", &fec_idx) == 1);
5061 gst_object_unref (templ);
5063 /* return 0 to retain pad in filtered iterator */
5064 return !have_static_pad;
5068 fec_srcpad_iterator_foreach (const GValue * item, GstRtpBinSession * session)
5070 GstPad *pad = g_value_get_object (item);
5071 fec_encoder_add_pad_unlocked (pad, session);
5075 fec_encoder_pad_added_cb (GstElement * encoder, GstPad * pad,
5076 GstRtpBinSession * session)
5078 fec_encoder_add_pad (pad, session);
5082 request_fec_encoder (GstRtpBin * rtpbin, GstRtpBinSession * session,
5085 GstElement *ret = NULL;
5086 const gchar *factory;
5089 sess_id_str = g_strdup_printf ("%u", sessid);
5090 factory = gst_structure_get_string (rtpbin->fec_encoders, sess_id_str);
5091 g_free (sess_id_str);
5093 /* First try the property */
5098 gst_parse_bin_from_description_full (factory, TRUE, NULL,
5099 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_FATAL_ERRORS,
5102 GST_ERROR_OBJECT (rtpbin, "Failed to build encoder from factory: %s",
5107 bin_manage_element (session->bin, ret);
5108 session->elements = g_slist_prepend (session->elements, ret);
5109 GST_INFO_OBJECT (rtpbin, "Built FEC encoder: %" GST_PTR_FORMAT
5110 " for session %u", ret, sessid);
5113 /* Fallback to the signal */
5115 ret = session_request_element (session, SIGNAL_REQUEST_FEC_ENCODER);
5118 /* First, add encoder pads that match fec_% template and are already present */
5119 GstIterator *it, *filter;
5120 GstIteratorResult it_ret = GST_ITERATOR_OK;
5122 it = gst_element_iterate_src_pads (ret);
5124 gst_iterator_filter (it, (GCompareFunc) fec_srcpad_iterator_filter,
5127 while (it_ret == GST_ITERATOR_OK || it_ret == GST_ITERATOR_RESYNC) {
5129 gst_iterator_foreach (filter,
5130 (GstIteratorForeachFunction) fec_srcpad_iterator_foreach, session);
5132 if (it_ret == GST_ITERATOR_RESYNC)
5133 gst_iterator_resync (filter);
5136 gst_iterator_free (filter);
5138 /* Finally, connect to pad-added signal if any of the encoder pads are
5140 g_signal_connect (ret, "pad-added", G_CALLBACK (fec_encoder_pad_added_cb),
5148 /* Create a pad for sending RTP for the session in @name. Must be called with
5152 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
5156 GstPad *send_rtp_sink;
5158 GstElement *encoder;
5159 GstElement *prev = NULL;
5160 GstRtpBinSession *session;
5162 /* first get the session number */
5163 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
5166 /* get or create session */
5167 session = find_session_by_id (rtpbin, sessid);
5169 /* create session now */
5170 session = create_session (rtpbin, sessid);
5171 if (session == NULL)
5175 /* check if pad was requested */
5176 if (session->send_rtp_sink_ghost != NULL)
5177 return session->send_rtp_sink_ghost;
5179 /* check if we are already using this session as a sender */
5180 if (session->send_rtp_sink != NULL)
5181 goto existing_session;
5183 encoder = request_fec_encoder (rtpbin, session, sessid);
5186 GST_DEBUG_OBJECT (rtpbin, "Linking FEC encoder");
5188 send_rtp_sink = gst_element_get_static_pad (encoder, "sink");
5191 goto enc_sink_failed;
5196 GST_DEBUG_OBJECT (rtpbin, "getting RTP AUX sender");
5197 aux = session_request_element (session, SIGNAL_REQUEST_AUX_SENDER);
5200 GST_DEBUG_OBJECT (rtpbin, "linking AUX sender");
5201 if (!setup_aux_sender (rtpbin, session, aux))
5202 goto aux_session_failed;
5204 pname = g_strdup_printf ("sink_%u", sessid);
5205 sinkpad = gst_element_get_static_pad (aux, pname);
5208 if (sinkpad == NULL)
5209 goto aux_sink_failed;
5212 send_rtp_sink = sinkpad;
5214 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
5215 GstPadLinkReturn ret;
5217 ret = gst_pad_link (srcpad, sinkpad);
5218 gst_object_unref (srcpad);
5219 if (ret != GST_PAD_LINK_OK) {
5220 goto aux_link_failed;
5222 gst_object_unref (sinkpad);
5226 /* get send_rtp pad and store */
5227 session->send_rtp_sink =
5228 gst_element_request_pad_simple (session->session, "send_rtp_sink");
5229 if (session->send_rtp_sink == NULL)
5232 if (!complete_session_src (rtpbin, session))
5233 goto session_src_failed;
5236 send_rtp_sink = gst_object_ref (session->send_rtp_sink);
5238 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
5239 GstPadLinkReturn ret;
5241 ret = gst_pad_link (srcpad, session->send_rtp_sink);
5242 gst_object_unref (srcpad);
5243 if (ret != GST_PAD_LINK_OK)
5244 goto session_link_failed;
5248 session->send_rtp_sink_ghost =
5249 gst_ghost_pad_new_from_template (name, send_rtp_sink, templ);
5250 gst_object_unref (send_rtp_sink);
5252 return session->send_rtp_sink_ghost;
5257 g_warning ("rtpbin: cannot find session id for pad: %s",
5258 GST_STR_NULL (name));
5263 /* create_session already warned */
5268 g_warning ("rtpbin: session %u is already in use", sessid);
5273 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
5278 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
5283 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
5289 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
5294 g_warning ("rtpbin: failed to setup source pads for session %u", sessid);
5297 session_link_failed:
5299 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
5305 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
5306 " sink pad for session %u", encoder, sessid);
5312 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
5314 if (session->send_rtp_src_ghost) {
5315 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
5316 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
5317 session->send_rtp_src_ghost);
5318 session->send_rtp_src_ghost = NULL;
5320 if (session->send_rtp_sink) {
5321 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
5322 session->send_rtp_sink);
5323 gst_object_unref (session->send_rtp_sink);
5324 session->send_rtp_sink = NULL;
5326 if (session->send_rtp_sink_ghost) {
5327 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
5328 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
5329 session->send_rtp_sink_ghost);
5330 session->send_rtp_sink_ghost = NULL;
5335 remove_send_fec (GstRtpBin * rtpbin, GstRtpBinSession * session)
5339 for (tmp = session->send_fec_src_ghosts; tmp; tmp = tmp->next) {
5340 GstPad *ghost = GST_PAD (tmp->data);
5341 gst_pad_set_active (ghost, FALSE);
5342 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), ghost);
5345 g_slist_free (session->send_fec_src_ghosts);
5346 session->send_fec_src_ghosts = NULL;
5349 /* Create a pad for sending RTCP for the session in @name. Must be called with
5353 create_send_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
5358 GstElement *encoder;
5359 GstRtpBinSession *session;
5361 /* first get the session number */
5362 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
5365 /* get or create session */
5366 session = find_session_by_id (rtpbin, sessid);
5368 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
5369 /* create session now */
5370 session = create_session (rtpbin, sessid);
5371 if (session == NULL)
5375 /* check if pad was requested */
5376 if (session->send_rtcp_src_ghost != NULL)
5377 return session->send_rtcp_src_ghost;
5379 /* get rtcp_src pad and store */
5380 session->send_rtcp_src =
5381 gst_element_request_pad_simple (session->session, "send_rtcp_src");
5382 if (session->send_rtcp_src == NULL)
5385 GST_DEBUG_OBJECT (rtpbin, "getting RTCP encoder");
5386 encoder = session_request_element (session, SIGNAL_REQUEST_RTCP_ENCODER);
5390 GstPadLinkReturn ret;
5392 GST_DEBUG_OBJECT (rtpbin, "linking RTCP encoder");
5394 ename = g_strdup_printf ("rtcp_src_%u", sessid);
5395 encsrc = gst_element_get_static_pad (encoder, ename);
5398 goto enc_src_failed;
5400 ename = g_strdup_printf ("rtcp_sink_%u", sessid);
5401 encsink = gst_element_get_static_pad (encoder, ename);
5403 if (encsink == NULL)
5404 goto enc_sink_failed;
5406 ret = gst_pad_link (session->send_rtcp_src, encsink);
5407 gst_object_unref (encsink);
5409 if (ret != GST_PAD_LINK_OK)
5410 goto enc_link_failed;
5412 GST_DEBUG_OBJECT (rtpbin, "no RTCP encoder given");
5413 encsrc = gst_object_ref (session->send_rtcp_src);
5416 session->send_rtcp_src_ghost =
5417 gst_ghost_pad_new_from_template (name, encsrc, templ);
5418 gst_object_unref (encsrc);
5420 return session->send_rtcp_src_ghost;
5425 g_warning ("rtpbin: cannot find session id for pad: %s",
5426 GST_STR_NULL (name));
5431 /* create_session already warned */
5436 g_warning ("rtpbin: failed to get rtcp pad for session %u", sessid);
5441 g_warning ("rtpbin: failed to get encoder src pad for session %u", sessid);
5446 g_warning ("rtpbin: failed to get encoder sink pad for session %u", sessid);
5447 gst_object_unref (encsrc);
5452 g_warning ("rtpbin: failed to link rtcp encoder for session %u", sessid);
5453 gst_object_unref (encsrc);
5459 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
5461 if (session->send_rtcp_src_ghost) {
5462 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
5463 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
5464 session->send_rtcp_src_ghost);
5465 session->send_rtcp_src_ghost = NULL;
5467 if (session->send_rtcp_src) {
5468 gst_element_release_request_pad (session->session, session->send_rtcp_src);
5469 gst_object_unref (session->send_rtcp_src);
5470 session->send_rtcp_src = NULL;
5474 /* If the requested name is NULL we should create a name with
5475 * the session number assuming we want the lowest possible session
5476 * with a free pad like the template */
5478 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
5480 gboolean name_found = FALSE;
5482 GstIterator *pad_it = NULL;
5483 gchar *pad_name = NULL;
5484 GValue data = { 0, };
5486 GST_DEBUG_OBJECT (element, "find a free pad name for template");
5487 while (!name_found) {
5488 gboolean done = FALSE;
5491 pad_name = g_strdup_printf (templ->name_template, session++);
5492 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
5495 switch (gst_iterator_next (pad_it, &data)) {
5496 case GST_ITERATOR_OK:
5501 pad = g_value_get_object (&data);
5502 name = gst_pad_get_name (pad);
5504 if (strcmp (name, pad_name) == 0) {
5509 g_value_reset (&data);
5512 case GST_ITERATOR_ERROR:
5513 case GST_ITERATOR_RESYNC:
5514 /* restart iteration */
5519 case GST_ITERATOR_DONE:
5524 g_value_unset (&data);
5525 gst_iterator_free (pad_it);
5528 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
5535 gst_rtp_bin_request_new_pad (GstElement * element,
5536 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
5539 GstElementClass *klass;
5542 gchar *pad_name = NULL;
5544 g_return_val_if_fail (templ != NULL, NULL);
5545 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
5547 rtpbin = GST_RTP_BIN (element);
5548 klass = GST_ELEMENT_GET_CLASS (element);
5550 GST_RTP_BIN_LOCK (rtpbin);
5553 /* use a free pad name */
5554 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
5556 /* use the provided name */
5557 pad_name = g_strdup (name);
5560 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
5562 /* figure out the template */
5563 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
5564 result = create_recv_rtp (rtpbin, templ, pad_name);
5565 } else if (templ == gst_element_class_get_pad_template (klass,
5566 "recv_rtcp_sink_%u")) {
5567 result = create_recv_rtcp (rtpbin, templ, pad_name);
5568 } else if (templ == gst_element_class_get_pad_template (klass,
5569 "send_rtp_sink_%u")) {
5570 result = create_send_rtp (rtpbin, templ, pad_name);
5571 } else if (templ == gst_element_class_get_pad_template (klass,
5572 "send_rtcp_src_%u")) {
5573 result = create_send_rtcp (rtpbin, templ, pad_name);
5574 } else if (templ == gst_element_class_get_pad_template (klass,
5575 "recv_fec_sink_%u_%u")) {
5576 result = create_recv_fec (rtpbin, templ, pad_name);
5578 goto wrong_template;
5581 GST_RTP_BIN_UNLOCK (rtpbin);
5584 gst_pad_set_active (result, TRUE);
5585 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
5594 GST_RTP_BIN_UNLOCK (rtpbin);
5595 g_warning ("rtpbin: this is not our template");
5601 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
5603 GstRtpBinSession *session;
5606 g_return_if_fail (GST_IS_GHOST_PAD (pad));
5607 g_return_if_fail (GST_IS_RTP_BIN (element));
5609 rtpbin = GST_RTP_BIN (element);
5611 GST_RTP_BIN_LOCK (rtpbin);
5612 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
5613 GST_DEBUG_PAD_NAME (pad));
5615 if (!(session = find_session_by_pad (rtpbin, pad)))
5618 if (session->recv_rtp_sink_ghost == pad) {
5619 remove_recv_rtp (rtpbin, session);
5620 } else if (session->recv_rtcp_sink_ghost == pad) {
5621 remove_recv_rtcp (rtpbin, session);
5622 } else if (session->send_rtp_sink_ghost == pad) {
5623 remove_send_rtp (rtpbin, session);
5624 } else if (session->send_rtcp_src_ghost == pad) {
5625 remove_rtcp (rtpbin, session);
5626 } else if (pad_is_recv_fec (session, pad)) {
5627 remove_recv_fec_for_pad (rtpbin, session, pad);
5630 /* no more request pads, free the complete session */
5631 if (session->recv_rtp_sink_ghost == NULL
5632 && session->recv_rtcp_sink_ghost == NULL
5633 && session->send_rtp_sink_ghost == NULL
5634 && session->send_rtcp_src_ghost == NULL
5635 && session->recv_fec_sink_ghosts == NULL) {
5636 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
5637 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
5638 free_session (session, rtpbin);
5640 GST_RTP_BIN_UNLOCK (rtpbin);
5647 GST_RTP_BIN_UNLOCK (rtpbin);
5648 g_warning ("rtpbin: %s:%s is not one of our request pads",
5649 GST_DEBUG_PAD_NAME (pad));