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
22 * @see_also: rtpjitterbuffer, rtpsession, rtpptdemux, rtpssrcdemux
24 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26 * RTP sessions that will be synchronized together using RTCP SR packets.
28 * #GstRtpBin is configured with a number of request pads that define the
29 * functionality that is activated, similar to the #GstRtpSession element.
31 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36 * the packets are released from the jitterbuffer, they will be forwarded to a
37 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39 * rtpbin with the session number, SSRC and payload type respectively as the pad
42 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43 * session number must be specified in the pad name.
45 * If you want the session manager to generate and send RTCP packets, request
46 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47 * on this pad contain SR/RR RTCP reports that should be sent to all participants
50 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52 * the pad from the lowest available session will be returned. The session manager will modify the
53 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54 * send_rtp_src_\%u pad after updating its internal state.
56 * The session manager needs the clock-rate of the payload types it is handling
57 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
61 * Access to the internal statistics of rtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
66 * #GstRtpBin also has signals (#GstRtpBin::request-rtp-encoder,
67 * #GstRtpBin::request-rtp-decoder, #GstRtpBin::request-rtcp-encoder and
68 * #GstRtpBin::request-rtp-decoder) to dynamically request for RTP and RTCP encoders
69 * and decoders in order to support SRTP. The encoders must provide the pads
70 * rtp_sink_\%u and rtp_src_\%u for RTP and rtcp_sink_\%u and rtcp_src_\%u for
71 * RTCP. The session number will be used in the pad name. The decoders must provide
72 * rtp_sink and rtp_src for RTP and rtcp_sink and rtcp_src for RTCP. The decoders will
73 * be placed before the #GstRtpSession element, thus they must support SSRC demuxing
76 * #GstRtpBin has signals (#GstRtpBin::request-aux-sender and
77 * #GstRtpBin::request-aux-receiver to dynamically request an element that can be
78 * used to create or merge additional RTP streams. AUX elements are needed to
79 * implement FEC or retransmission (such as RFC 4588). An AUX sender must have one
80 * sink_\%u pad that matches the sessionid in the signal and it should have 1 or
81 * more src_\%u pads. For each src_%\u pad, a session will be made (if needed)
82 * and the pad will be linked to the session send_rtp_sink pad. Each session will
83 * then expose its source pad as send_rtp_src_\%u on #GstRtpBin.
84 * An AUX receiver has 1 src_\%u pad that much match the sessionid in the signal
85 * and 1 or more sink_\%u pads. A session will be made for each sink_\%u pad
86 * when the corresponding recv_rtp_sink_\%u pad is requested on #GstRtpBin.
89 * <title>Example pipelines</title>
91 * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
92 * rtpbin ! rtptheoradepay ! theoradec ! xvimagesink
93 * ]| Receive RTP data from port 5000 and send to the session 0 in rtpbin.
95 * gst-launch-1.0 rtpbin name=rtpbin \
96 * v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
97 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
98 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
99 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
100 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
101 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
103 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
104 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
105 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
106 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
107 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
108 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
109 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
110 * is received on port 5007. Since RTCP packets from the sender should be sent
111 * as soon as possible and do not participate in preroll, sync=false and
112 * async=false is configured on udpsink
114 * gst-launch-1.0 -v rtpbin name=rtpbin \
115 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
116 * port=5000 ! rtpbin.recv_rtp_sink_0 \
117 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
118 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
119 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
120 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
121 * port=5002 ! rtpbin.recv_rtp_sink_1 \
122 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
123 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
124 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
125 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
126 * decode and display the video.
127 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
128 * decode and play the audio.
129 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
130 * session 1 on port 5003. These packets will be used for session management and
132 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
143 #include <gst/rtp/gstrtpbuffer.h>
144 #include <gst/rtp/gstrtcpbuffer.h>
146 #include "gstrtpbin.h"
147 #include "rtpsession.h"
148 #include "gstrtpsession.h"
149 #include "gstrtpjitterbuffer.h"
151 #include <gst/glib-compat-private.h>
153 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
154 #define GST_CAT_DEFAULT gst_rtp_bin_debug
157 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
158 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
161 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
164 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
168 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
171 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
172 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
175 GST_STATIC_CAPS ("application/x-rtp")
179 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
180 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
183 GST_STATIC_CAPS ("application/x-rtp")
186 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
190 GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
193 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
194 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
197 GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
200 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock (&(bin)->priv->bin_lock)
201 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
203 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
204 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock (&(bin)->priv->dyn_lock)
205 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->dyn_lock)
207 /* lock for shutdown */
208 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
210 if (g_atomic_int_get (&bin->priv->shutdown)) \
212 GST_RTP_BIN_DYN_LOCK (bin); \
213 if (g_atomic_int_get (&bin->priv->shutdown)) { \
214 GST_RTP_BIN_DYN_UNLOCK (bin); \
219 /* unlock for shutdown */
220 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
221 GST_RTP_BIN_DYN_UNLOCK (bin); \
223 /* Minimum time offset to apply. This compensates for rounding errors in NTP to
224 * RTP timestamp conversions */
225 #define MIN_TS_OFFSET (4 * GST_MSECOND)
227 struct _GstRtpBinPrivate
231 /* lock protecting dynamic adding/removing */
234 /* if we are shutting down or not */
239 /* NTP time in ns of last SR sync used */
240 guint64 last_ntpnstime;
242 /* list of extra elements */
246 /* signals and args */
249 SIGNAL_REQUEST_PT_MAP,
250 SIGNAL_PAYLOAD_TYPE_CHANGE,
254 SIGNAL_GET_INTERNAL_SESSION,
256 SIGNAL_GET_INTERNAL_STORAGE,
259 SIGNAL_ON_SSRC_COLLISION,
260 SIGNAL_ON_SSRC_VALIDATED,
261 SIGNAL_ON_SSRC_ACTIVE,
264 SIGNAL_ON_BYE_TIMEOUT,
266 SIGNAL_ON_SENDER_TIMEOUT,
269 SIGNAL_REQUEST_RTP_ENCODER,
270 SIGNAL_REQUEST_RTP_DECODER,
271 SIGNAL_REQUEST_RTCP_ENCODER,
272 SIGNAL_REQUEST_RTCP_DECODER,
274 SIGNAL_REQUEST_FEC_DECODER,
275 SIGNAL_REQUEST_FEC_ENCODER,
277 SIGNAL_NEW_JITTERBUFFER,
280 SIGNAL_REQUEST_AUX_SENDER,
281 SIGNAL_REQUEST_AUX_RECEIVER,
283 SIGNAL_ON_NEW_SENDER_SSRC,
284 SIGNAL_ON_SENDER_SSRC_ACTIVE,
286 SIGNAL_ON_BUNDLED_SSRC,
291 #define DEFAULT_LATENCY_MS 200
292 #define DEFAULT_DROP_ON_LATENCY FALSE
293 #define DEFAULT_SDES NULL
294 #define DEFAULT_DO_LOST FALSE
295 #define DEFAULT_IGNORE_PT FALSE
296 #define DEFAULT_NTP_SYNC FALSE
297 #define DEFAULT_AUTOREMOVE FALSE
298 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
299 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
300 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
301 #define DEFAULT_RTCP_SYNC_INTERVAL 0
302 #define DEFAULT_DO_SYNC_EVENT FALSE
303 #define DEFAULT_DO_RETRANSMISSION FALSE
304 #define DEFAULT_RTP_PROFILE GST_RTP_PROFILE_AVP
305 #define DEFAULT_NTP_TIME_SOURCE GST_RTP_NTP_TIME_SOURCE_NTP
306 #define DEFAULT_RTCP_SYNC_SEND_TIME TRUE
307 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
308 #define DEFAULT_MAX_DROPOUT_TIME 60000
309 #define DEFAULT_MAX_MISORDER_TIME 2000
310 #define DEFAULT_RFC7273_SYNC FALSE
311 #define DEFAULT_MAX_STREAMS G_MAXUINT
312 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
313 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
319 PROP_DROP_ON_LATENCY,
325 PROP_RTCP_SYNC_INTERVAL,
328 PROP_USE_PIPELINE_CLOCK,
330 PROP_DO_RETRANSMISSION,
332 PROP_NTP_TIME_SOURCE,
333 PROP_RTCP_SYNC_SEND_TIME,
334 PROP_MAX_RTCP_RTP_TIME_DIFF,
335 PROP_MAX_DROPOUT_TIME,
336 PROP_MAX_MISORDER_TIME,
339 PROP_MAX_TS_OFFSET_ADJUSTMENT,
343 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
345 gst_rtp_bin_rtcp_sync_get_type (void)
347 static GType rtcp_sync_type = 0;
348 static const GEnumValue rtcp_sync_types[] = {
349 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
350 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
351 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
355 if (!rtcp_sync_type) {
356 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
358 return rtcp_sync_type;
362 typedef struct _GstRtpBinSession GstRtpBinSession;
363 typedef struct _GstRtpBinStream GstRtpBinStream;
364 typedef struct _GstRtpBinClient GstRtpBinClient;
366 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
368 static GstCaps *pt_map_requested (GstElement * element, guint pt,
369 GstRtpBinSession * session);
370 static void payload_type_change (GstElement * element, guint pt,
371 GstRtpBinSession * session);
372 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
373 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
374 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
375 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
376 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
377 static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
378 static GstRtpBinSession *create_session (GstRtpBin * rtpbin, gint id);
379 static GstPad *complete_session_sink (GstRtpBin * rtpbin,
380 GstRtpBinSession * session);
382 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
384 static GstPad *complete_session_rtcp (GstRtpBin * rtpbin,
385 GstRtpBinSession * session, guint sessid);
387 /* Manages the RTP stream for one SSRC.
389 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
390 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
391 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
392 * together (see below).
394 struct _GstRtpBinStream
396 /* the SSRC of this stream */
402 /* the session this SSRC belongs to */
403 GstRtpBinSession *session;
405 /* the jitterbuffer of the SSRC */
407 gulong buffer_handlesync_sig;
408 gulong buffer_ptreq_sig;
409 gulong buffer_ntpstop_sig;
412 /* the PT demuxer of the SSRC */
414 gulong demux_newpad_sig;
415 gulong demux_padremoved_sig;
416 gulong demux_ptreq_sig;
417 gulong demux_ptchange_sig;
419 /* if we have calculated a valid rt_delta for this stream */
421 /* mapping to local RTP and NTP time */
424 /* base rtptime in gst time */
428 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->lock)
429 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
431 /* Manages the receiving end of the packets.
433 * There is one such structure for each RTP session (audio/video/...).
434 * We get the RTP/RTCP packets and stuff them into the session manager. From
435 * there they are pushed into an SSRC demuxer that splits the stream based on
436 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
437 * the GstRtpBinStream above).
439 * Before the SSRC demuxer, a storage element may be inserted for the purpose
440 * of Forward Error Correction.
442 struct _GstRtpBinSession
448 /* the session element */
450 /* the SSRC demuxer */
452 gulong demux_newpad_sig;
453 gulong demux_padremoved_sig;
460 /* list of GstRtpBinStream */
463 /* list of elements */
466 /* mapping of payload type to caps */
469 /* the pads of the session */
470 GstPad *recv_rtp_sink;
471 GstPad *recv_rtp_sink_ghost;
472 GstPad *recv_rtp_src;
473 GstPad *recv_rtcp_sink;
474 GstPad *recv_rtcp_sink_ghost;
476 GstPad *send_rtp_sink;
477 GstPad *send_rtp_sink_ghost;
478 GstPad *send_rtp_src_ghost;
479 GstPad *send_rtcp_src;
480 GstPad *send_rtcp_src_ghost;
483 /* Manages the RTP streams that come from one client and should therefore be
486 struct _GstRtpBinClient
488 /* the common CNAME for the streams */
497 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
498 static GstRtpBinSession *
499 find_session_by_id (GstRtpBin * rtpbin, gint id)
503 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
504 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
512 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
513 static GstRtpBinSession *
514 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
518 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
519 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
521 if ((sess->recv_rtp_sink_ghost == pad) ||
522 (sess->recv_rtcp_sink_ghost == pad) ||
523 (sess->send_rtp_sink_ghost == pad)
524 || (sess->send_rtcp_src_ghost == pad))
531 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
533 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
538 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
540 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
545 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
547 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
552 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
554 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
559 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
561 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
566 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
568 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
573 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
575 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
578 if (sess->bin->priv->autoremove)
579 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
583 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
585 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
588 if (sess->bin->priv->autoremove)
589 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
593 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
595 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
600 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
602 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
603 stream->session->id, stream->ssrc);
607 on_new_sender_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
609 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
614 on_sender_ssrc_active (GstElement * session, guint32 ssrc,
615 GstRtpBinSession * sess)
617 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE],
621 /* must be called with the SESSION lock */
622 static GstRtpBinStream *
623 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
627 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
628 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
630 if (stream->ssrc == ssrc)
637 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
638 GstRtpBinSession * session)
640 GstRtpBinStream *stream = NULL;
643 rtpbin = session->bin;
645 GST_RTP_BIN_LOCK (rtpbin);
647 GST_RTP_SESSION_LOCK (session);
648 if ((stream = find_stream_by_ssrc (session, ssrc)))
649 session->streams = g_slist_remove (session->streams, stream);
650 GST_RTP_SESSION_UNLOCK (session);
653 free_stream (stream, rtpbin);
655 GST_RTP_BIN_UNLOCK (rtpbin);
658 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
659 static GstRtpBinSession *
660 create_session (GstRtpBin * rtpbin, gint id)
662 GstRtpBinSession *sess;
663 GstElement *session, *demux;
664 GstElement *storage = NULL;
667 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
670 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
673 if (!(storage = gst_element_factory_make ("rtpstorage", NULL)))
676 /* need to sink the storage or otherwise signal handlers from bindings will
677 * take ownership of it and we don't own it anymore */
678 gst_object_ref_sink (storage);
679 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_STORAGE], 0, storage,
682 sess = g_new0 (GstRtpBinSession, 1);
683 g_mutex_init (&sess->lock);
686 sess->session = session;
688 sess->storage = storage;
690 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
691 (GDestroyNotify) gst_caps_unref);
692 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
694 /* configure SDES items */
695 GST_OBJECT_LOCK (rtpbin);
696 g_object_set (session, "sdes", rtpbin->sdes, "rtp-profile",
697 rtpbin->rtp_profile, "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time,
699 if (rtpbin->use_pipeline_clock)
700 g_object_set (session, "use-pipeline-clock", rtpbin->use_pipeline_clock,
703 g_object_set (session, "ntp-time-source", rtpbin->ntp_time_source, NULL);
705 g_object_set (session, "max-dropout-time", rtpbin->max_dropout_time,
706 "max-misorder-time", rtpbin->max_misorder_time, NULL);
707 GST_OBJECT_UNLOCK (rtpbin);
709 /* provide clock_rate to the session manager when needed */
710 g_signal_connect (session, "request-pt-map",
711 (GCallback) pt_map_requested, sess);
713 g_signal_connect (sess->session, "on-new-ssrc",
714 (GCallback) on_new_ssrc, sess);
715 g_signal_connect (sess->session, "on-ssrc-collision",
716 (GCallback) on_ssrc_collision, sess);
717 g_signal_connect (sess->session, "on-ssrc-validated",
718 (GCallback) on_ssrc_validated, sess);
719 g_signal_connect (sess->session, "on-ssrc-active",
720 (GCallback) on_ssrc_active, sess);
721 g_signal_connect (sess->session, "on-ssrc-sdes",
722 (GCallback) on_ssrc_sdes, sess);
723 g_signal_connect (sess->session, "on-bye-ssrc",
724 (GCallback) on_bye_ssrc, sess);
725 g_signal_connect (sess->session, "on-bye-timeout",
726 (GCallback) on_bye_timeout, sess);
727 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
728 g_signal_connect (sess->session, "on-sender-timeout",
729 (GCallback) on_sender_timeout, sess);
730 g_signal_connect (sess->session, "on-new-sender-ssrc",
731 (GCallback) on_new_sender_ssrc, sess);
732 g_signal_connect (sess->session, "on-sender-ssrc-active",
733 (GCallback) on_sender_ssrc_active, sess);
735 gst_bin_add (GST_BIN_CAST (rtpbin), session);
736 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
737 gst_bin_add (GST_BIN_CAST (rtpbin), storage);
739 /* unref the storage again, the bin has a reference now and
740 * we don't need it anymore */
741 gst_object_unref (storage);
743 GST_OBJECT_LOCK (rtpbin);
744 target = GST_STATE_TARGET (rtpbin);
745 GST_OBJECT_UNLOCK (rtpbin);
747 /* change state only to what's needed */
748 gst_element_set_state (demux, target);
749 gst_element_set_state (session, target);
750 gst_element_set_state (storage, target);
757 g_warning ("rtpbin: could not create rtpsession element");
762 gst_object_unref (session);
763 g_warning ("rtpbin: could not create rtpssrcdemux element");
768 gst_object_unref (session);
769 gst_object_unref (demux);
770 g_warning ("rtpbin: could not create rtpstorage element");
776 bin_manage_element (GstRtpBin * bin, GstElement * element)
778 GstRtpBinPrivate *priv = bin->priv;
780 if (g_list_find (priv->elements, element)) {
781 GST_DEBUG_OBJECT (bin, "requested element %p already in bin", element);
783 GST_DEBUG_OBJECT (bin, "adding requested element %p", element);
785 if (g_object_is_floating (element))
786 element = gst_object_ref_sink (element);
788 if (!gst_bin_add (GST_BIN_CAST (bin), element))
790 if (!gst_element_sync_state_with_parent (element))
791 GST_WARNING_OBJECT (bin, "unable to sync element state with rtpbin");
793 /* we add the element multiple times, each we need an equal number of
794 * removes to really remove the element from the bin */
795 priv->elements = g_list_prepend (priv->elements, element);
802 GST_WARNING_OBJECT (bin, "unable to add element");
803 gst_object_unref (element);
809 remove_bin_element (GstElement * element, GstRtpBin * bin)
811 GstRtpBinPrivate *priv = bin->priv;
814 find = g_list_find (priv->elements, element);
816 priv->elements = g_list_delete_link (priv->elements, find);
818 if (!g_list_find (priv->elements, element)) {
819 gst_element_set_locked_state (element, TRUE);
820 gst_bin_remove (GST_BIN_CAST (bin), element);
821 gst_element_set_state (element, GST_STATE_NULL);
824 gst_object_unref (element);
828 /* called with RTP_BIN_LOCK */
830 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
832 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
834 gst_element_set_locked_state (sess->demux, TRUE);
835 gst_element_set_locked_state (sess->session, TRUE);
837 gst_element_set_state (sess->demux, GST_STATE_NULL);
838 gst_element_set_state (sess->session, GST_STATE_NULL);
840 remove_recv_rtp (bin, sess);
841 remove_recv_rtcp (bin, sess);
842 remove_send_rtp (bin, sess);
843 remove_rtcp (bin, sess);
845 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
846 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
848 g_slist_foreach (sess->elements, (GFunc) remove_bin_element, bin);
849 g_slist_free (sess->elements);
851 g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
852 g_slist_free (sess->streams);
854 g_mutex_clear (&sess->lock);
855 g_hash_table_destroy (sess->ptmap);
860 /* get the payload type caps for the specific payload @pt in @session */
862 get_pt_map (GstRtpBinSession * session, guint pt)
864 GstCaps *caps = NULL;
867 GValue args[3] = { {0}, {0}, {0} };
869 GST_DEBUG ("searching pt %u in cache", pt);
871 GST_RTP_SESSION_LOCK (session);
873 /* first look in the cache */
874 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
882 GST_DEBUG ("emiting signal for pt %u in session %u", pt, session->id);
884 /* not in cache, send signal to request caps */
885 g_value_init (&args[0], GST_TYPE_ELEMENT);
886 g_value_set_object (&args[0], bin);
887 g_value_init (&args[1], G_TYPE_UINT);
888 g_value_set_uint (&args[1], session->id);
889 g_value_init (&args[2], G_TYPE_UINT);
890 g_value_set_uint (&args[2], pt);
892 g_value_init (&ret, GST_TYPE_CAPS);
893 g_value_set_boxed (&ret, NULL);
895 GST_RTP_SESSION_UNLOCK (session);
897 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
899 GST_RTP_SESSION_LOCK (session);
901 g_value_unset (&args[0]);
902 g_value_unset (&args[1]);
903 g_value_unset (&args[2]);
905 /* look in the cache again because we let the lock go */
906 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
909 g_value_unset (&ret);
913 caps = (GstCaps *) g_value_dup_boxed (&ret);
914 g_value_unset (&ret);
918 GST_DEBUG ("caching pt %u as %" GST_PTR_FORMAT, pt, caps);
920 /* store in cache, take additional ref */
921 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
922 gst_caps_ref (caps));
925 GST_RTP_SESSION_UNLOCK (session);
932 GST_RTP_SESSION_UNLOCK (session);
933 GST_DEBUG ("no pt map could be obtained");
939 return_true (gpointer key, gpointer value, gpointer user_data)
945 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
947 GSList *clients, *streams;
949 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
951 GST_RTP_BIN_LOCK (rtpbin);
952 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
953 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
955 /* reset sync on all streams for this client */
956 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
957 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
959 /* make use require a new SR packet for this stream before we attempt new
961 stream->have_sync = FALSE;
962 stream->rt_delta = 0;
963 stream->rtp_delta = 0;
964 stream->clock_base = -100 * GST_SECOND;
967 GST_RTP_BIN_UNLOCK (rtpbin);
971 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
973 GSList *sessions, *streams;
975 GST_RTP_BIN_LOCK (bin);
976 GST_DEBUG_OBJECT (bin, "clearing pt map");
977 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
978 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
980 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
981 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
983 GST_RTP_SESSION_LOCK (session);
984 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
986 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
987 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
989 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
990 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
992 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
994 GST_RTP_SESSION_UNLOCK (session);
996 GST_RTP_BIN_UNLOCK (bin);
999 gst_rtp_bin_reset_sync (bin);
1003 gst_rtp_bin_get_session (GstRtpBin * bin, guint session_id)
1005 GstRtpBinSession *session;
1006 GstElement *ret = NULL;
1008 GST_RTP_BIN_LOCK (bin);
1009 GST_DEBUG_OBJECT (bin, "retrieving GstRtpSession, index: %u", session_id);
1010 session = find_session_by_id (bin, (gint) session_id);
1012 ret = gst_object_ref (session->session);
1014 GST_RTP_BIN_UNLOCK (bin);
1020 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
1022 RTPSession *internal_session = NULL;
1023 GstRtpBinSession *session;
1025 GST_RTP_BIN_LOCK (bin);
1026 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %u",
1028 session = find_session_by_id (bin, (gint) session_id);
1030 g_object_get (session->session, "internal-session", &internal_session,
1033 GST_RTP_BIN_UNLOCK (bin);
1035 return internal_session;
1039 gst_rtp_bin_get_storage (GstRtpBin * bin, guint session_id)
1041 GstRtpBinSession *session;
1042 GstElement *res = NULL;
1044 GST_RTP_BIN_LOCK (bin);
1045 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1047 session = find_session_by_id (bin, (gint) session_id);
1048 if (session && session->storage) {
1049 res = gst_object_ref (session->storage);
1051 GST_RTP_BIN_UNLOCK (bin);
1057 gst_rtp_bin_get_internal_storage (GstRtpBin * bin, guint session_id)
1059 GObject *internal_storage = NULL;
1060 GstRtpBinSession *session;
1062 GST_RTP_BIN_LOCK (bin);
1063 GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1065 session = find_session_by_id (bin, (gint) session_id);
1066 if (session && session->storage) {
1067 g_object_get (session->storage, "internal-storage", &internal_storage,
1070 GST_RTP_BIN_UNLOCK (bin);
1072 return internal_storage;
1076 gst_rtp_bin_request_encoder (GstRtpBin * bin, guint session_id)
1078 GST_DEBUG_OBJECT (bin, "return NULL encoder");
1083 gst_rtp_bin_request_decoder (GstRtpBin * bin, guint session_id)
1085 GST_DEBUG_OBJECT (bin, "return NULL decoder");
1090 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
1091 const gchar * name, const GValue * value)
1093 GSList *sessions, *streams;
1095 GST_RTP_BIN_LOCK (bin);
1096 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1097 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1099 GST_RTP_SESSION_LOCK (session);
1100 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1101 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1103 g_object_set_property (G_OBJECT (stream->buffer), name, value);
1105 GST_RTP_SESSION_UNLOCK (session);
1107 GST_RTP_BIN_UNLOCK (bin);
1111 gst_rtp_bin_propagate_property_to_session (GstRtpBin * bin,
1112 const gchar * name, const GValue * value)
1116 GST_RTP_BIN_LOCK (bin);
1117 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1118 GstRtpBinSession *sess = (GstRtpBinSession *) sessions->data;
1120 g_object_set_property (G_OBJECT (sess->session), name, value);
1122 GST_RTP_BIN_UNLOCK (bin);
1125 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
1126 static GstRtpBinClient *
1127 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
1129 GstRtpBinClient *result = NULL;
1132 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
1133 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
1135 if (len != client->cname_len)
1138 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
1139 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
1146 /* nothing found, create one */
1147 if (result == NULL) {
1148 result = g_new0 (GstRtpBinClient, 1);
1149 result->cname = g_strndup ((gchar *) data, len);
1150 result->cname_len = len;
1151 bin->clients = g_slist_prepend (bin->clients, result);
1152 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
1159 free_client (GstRtpBinClient * client, GstRtpBin * bin)
1161 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
1162 g_slist_free (client->streams);
1163 g_free (client->cname);
1168 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
1169 guint64 * ntpnstime)
1173 GstClockTime base_time, rt, clock_time;
1175 GST_OBJECT_LOCK (bin);
1176 if ((clock = GST_ELEMENT_CLOCK (bin))) {
1177 base_time = GST_ELEMENT_CAST (bin)->base_time;
1178 gst_object_ref (clock);
1179 GST_OBJECT_UNLOCK (bin);
1181 /* get current clock time and convert to running time */
1182 clock_time = gst_clock_get_time (clock);
1183 rt = clock_time - base_time;
1185 if (bin->use_pipeline_clock) {
1187 /* add constant to convert from 1970 based time to 1900 based time */
1188 ntpns += (2208988800LL * GST_SECOND);
1190 switch (bin->ntp_time_source) {
1191 case GST_RTP_NTP_TIME_SOURCE_NTP:
1192 case GST_RTP_NTP_TIME_SOURCE_UNIX:{
1195 /* get current NTP time */
1196 g_get_current_time (¤t);
1197 ntpns = GST_TIMEVAL_TO_TIME (current);
1199 /* add constant to convert from 1970 based time to 1900 based time */
1200 if (bin->ntp_time_source == GST_RTP_NTP_TIME_SOURCE_NTP)
1201 ntpns += (2208988800LL * GST_SECOND);
1204 case GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME:
1207 case GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME:
1211 ntpns = -1; /* Fix uninited compiler warning */
1212 g_assert_not_reached ();
1217 gst_object_unref (clock);
1219 GST_OBJECT_UNLOCK (bin);
1230 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
1231 gint64 ts_offset, gint64 max_ts_offset, gint64 min_ts_offset,
1232 gboolean allow_positive_ts_offset)
1234 gint64 prev_ts_offset;
1236 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
1238 /* delta changed, see how much */
1239 if (prev_ts_offset != ts_offset) {
1242 diff = prev_ts_offset - ts_offset;
1244 GST_DEBUG_OBJECT (bin,
1245 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
1246 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
1248 /* ignore minor offsets */
1249 if (ABS (diff) < min_ts_offset) {
1250 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1254 /* sanity check offset */
1255 if (max_ts_offset > 0) {
1256 if (ts_offset > 0 && !allow_positive_ts_offset) {
1257 GST_DEBUG_OBJECT (bin,
1258 "offset is positive (clocks are out of sync), ignoring");
1261 if (ABS (ts_offset) > max_ts_offset) {
1262 GST_DEBUG_OBJECT (bin, "offset too large, ignoring");
1267 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1269 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1270 stream->ssrc, ts_offset);
1274 gst_rtp_bin_send_sync_event (GstRtpBinStream * stream)
1276 if (stream->bin->send_sync_event) {
1280 GST_DEBUG_OBJECT (stream->bin,
1281 "sending GstRTCPSRReceived event downstream");
1283 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1284 gst_structure_new_empty ("GstRTCPSRReceived"));
1286 srcpad = gst_element_get_static_pad (stream->buffer, "src");
1287 gst_pad_push_event (srcpad, event);
1288 gst_object_unref (srcpad);
1292 /* associate a stream to the given CNAME. This will make sure all streams for
1293 * that CNAME are synchronized together.
1294 * Must be called with GST_RTP_BIN_LOCK */
1296 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1297 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1298 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1299 gint64 rtp_clock_base)
1301 GstRtpBinClient *client;
1304 GstClockTime running_time, running_time_rtp;
1307 /* first find or create the CNAME */
1308 client = get_client (bin, len, data, &created);
1310 /* find stream in the client */
1311 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1312 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1314 if (ostream == stream)
1317 /* not found, add it to the list */
1319 GST_DEBUG_OBJECT (bin,
1320 "new association of SSRC %08x with client %p with CNAME %s",
1321 stream->ssrc, client, client->cname);
1322 client->streams = g_slist_prepend (client->streams, stream);
1325 GST_DEBUG_OBJECT (bin,
1326 "found association of SSRC %08x with client %p with CNAME %s",
1327 stream->ssrc, client, client->cname);
1330 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1331 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1332 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1333 /* we don't need that data, so carry on,
1334 * but make some values look saner */
1335 last_extrtptime = base_rtptime;
1337 /* nothing we can do with this data in this case */
1338 GST_DEBUG_OBJECT (bin, "bailing out");
1343 /* Take the extended rtptime we found in the SR packet and map it to the
1344 * local rtptime. The local rtp time is used to construct timestamps on the
1345 * buffers so we will calculate what running_time corresponds to the RTP
1346 * timestamp in the SR packet. */
1347 running_time_rtp = last_extrtptime - base_rtptime;
1349 GST_DEBUG_OBJECT (bin,
1350 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1351 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1352 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1353 last_extrtptime, running_time_rtp, clock_rate, rtp_clock_base);
1355 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1356 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1357 * into a corresponding gstreamer timestamp. Note that the base_time also
1358 * contains the drift between sender and receiver. */
1360 gst_util_uint64_scale_int (running_time_rtp, GST_SECOND, clock_rate);
1361 running_time += base_time;
1363 /* convert ntptime to nanoseconds */
1364 ntpnstime = gst_util_uint64_scale (ntptime, GST_SECOND,
1365 (G_GINT64_CONSTANT (1) << 32));
1367 stream->have_sync = TRUE;
1369 GST_DEBUG_OBJECT (bin,
1370 "SR RTP running time %" G_GUINT64_FORMAT ", SR NTP %" G_GUINT64_FORMAT,
1371 running_time, ntpnstime);
1373 /* recalc inter stream playout offset, but only if there is more than one
1374 * stream or we're doing NTP sync. */
1375 if (bin->ntp_sync) {
1376 gint64 ntpdiff, rtdiff;
1377 guint64 local_ntpnstime;
1378 GstClockTime local_running_time;
1380 /* For NTP sync we need to first get a snapshot of running_time and NTP
1381 * time. We know at what running_time we play a certain RTP time, we also
1382 * calculated when we would play the RTP time in the SR packet. Now we need
1383 * to know how the running_time and the NTP time relate to eachother. */
1384 get_current_times (bin, &local_running_time, &local_ntpnstime);
1386 /* see how far away the NTP time is. This is the difference between the
1387 * current NTP time and the NTP time in the last SR packet. */
1388 ntpdiff = local_ntpnstime - ntpnstime;
1389 /* see how far away the running_time is. This is the difference between the
1390 * current running_time and the running_time of the RTP timestamp in the
1391 * last SR packet. */
1392 rtdiff = local_running_time - running_time;
1394 GST_DEBUG_OBJECT (bin,
1395 "local NTP time %" G_GUINT64_FORMAT ", SR NTP time %" G_GUINT64_FORMAT,
1396 local_ntpnstime, ntpnstime);
1397 GST_DEBUG_OBJECT (bin,
1398 "local running time %" G_GUINT64_FORMAT ", SR RTP running time %"
1399 G_GUINT64_FORMAT, local_running_time, running_time);
1400 GST_DEBUG_OBJECT (bin,
1401 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1404 /* combine to get the final diff to apply to the running_time */
1405 stream->rt_delta = rtdiff - ntpdiff;
1407 stream_set_ts_offset (bin, stream, stream->rt_delta, bin->max_ts_offset,
1410 gint64 min, rtp_min, clock_base = stream->clock_base;
1411 gboolean all_sync, use_rtp;
1412 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1414 /* calculate delta between server and receiver. ntpnstime is created by
1415 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1416 * delta expresses the difference to our timeline and the server timeline. The
1417 * difference in itself doesn't mean much but we can combine the delta of
1418 * multiple streams to create a stream specific offset. */
1419 stream->rt_delta = ntpnstime - running_time;
1421 /* calculate the min of all deltas, ignoring streams that did not yet have a
1422 * valid rt_delta because we did not yet receive an SR packet for those
1424 * We calculate the mininum because we would like to only apply positive
1425 * offsets to streams, delaying their playback instead of trying to speed up
1426 * other streams (which might be imposible when we have to create negative
1428 * The stream that has the smallest diff is selected as the reference stream,
1429 * all other streams will have a positive offset to this difference. */
1431 /* some alternative setting allow ignoring RTCP as much as possible,
1432 * for servers generating bogus ntp timeline */
1433 min = rtp_min = G_MAXINT64;
1435 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1439 /* signed version for convienience */
1440 clock_base = base_rtptime;
1441 /* deal with possible wrap-around */
1442 ext_base = base_rtptime;
1443 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1444 /* sanity check; base rtp and provided clock_base should be close */
1445 if (rtp_clock_base >= clock_base) {
1446 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1447 rtp_clock_base = base_time +
1448 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1449 GST_SECOND, clock_rate);
1454 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1455 rtp_clock_base = base_time -
1456 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1457 GST_SECOND, clock_rate);
1462 /* warn and bail for clarity out if no sane values */
1464 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1467 /* store to track changes */
1468 clock_base = rtp_clock_base;
1469 /* generate a fake as before,
1470 * now equating rtptime obtained from RTP-Info,
1471 * where the large time represent the otherwise irrelevant npt/ntp time */
1472 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1474 clock_base = rtp_clock_base;
1478 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1479 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1481 if (!ostream->have_sync) {
1486 /* change in current stream's base from previously init'ed value
1487 * leads to reset of all stream's base */
1488 if (stream != ostream && stream->clock_base >= 0 &&
1489 (stream->clock_base != clock_base)) {
1490 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1491 ostream->clock_base = -100 * GST_SECOND;
1492 ostream->rtp_delta = 0;
1495 if (ostream->rt_delta < min)
1496 min = ostream->rt_delta;
1497 if (ostream->rtp_delta < rtp_min)
1498 rtp_min = ostream->rtp_delta;
1501 /* arrange to re-sync for each stream upon significant change,
1503 all_sync = all_sync && (stream->clock_base == clock_base);
1504 stream->clock_base = clock_base;
1506 /* may need init performed above later on, but nothing more to do now */
1507 if (client->nstreams <= 1)
1510 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1511 " all sync %d", client, min, all_sync);
1512 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1514 switch (rtcp_sync) {
1515 case GST_RTP_BIN_RTCP_SYNC_RTP:
1518 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1519 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1521 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1522 /* if all have been synced already, do not bother further */
1524 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1532 /* bail out if we adjusted recently enough */
1533 if (all_sync && (ntpnstime - bin->priv->last_ntpnstime) <
1534 bin->rtcp_sync_interval * GST_MSECOND) {
1535 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1536 "previous sender info too recent "
1537 "(previous NTP %" G_GUINT64_FORMAT ")", bin->priv->last_ntpnstime);
1540 bin->priv->last_ntpnstime = ntpnstime;
1542 /* calculate offsets for each stream */
1543 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1544 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1547 /* ignore streams for which we didn't receive an SR packet yet, we
1548 * can't synchronize them yet. We can however sync other streams just
1550 if (!ostream->have_sync)
1553 /* calculate offset to our reference stream, this should always give a
1554 * positive number. */
1556 ts_offset = ostream->rtp_delta - rtp_min;
1558 ts_offset = ostream->rt_delta - min;
1560 stream_set_ts_offset (bin, ostream, ts_offset, bin->max_ts_offset,
1561 MIN_TS_OFFSET, TRUE);
1564 gst_rtp_bin_send_sync_event (stream);
1569 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1570 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1571 (b) = gst_rtcp_packet_move_to_next ((packet)))
1573 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1574 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1575 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1577 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1578 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1579 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1582 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1583 GstRtpBinStream * stream)
1586 GstRTCPPacket packet;
1589 gboolean have_sr, have_sdes;
1591 guint64 base_rtptime;
1597 GstRTCPBuffer rtcp = { NULL, };
1601 GST_DEBUG_OBJECT (bin, "sync handler called");
1603 /* get the last relation between the rtp timestamps and the gstreamer
1604 * timestamps. We get this info directly from the jitterbuffer which
1605 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1606 * what the current situation is. */
1608 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1609 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1610 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1611 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1613 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1614 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1619 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1621 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1622 /* first packet must be SR or RR or else the validate would have failed */
1623 switch (gst_rtcp_packet_get_type (&packet)) {
1624 case GST_RTCP_TYPE_SR:
1625 /* only parse first. There is only supposed to be one SR in the packet
1626 * but we will deal with malformed packets gracefully */
1629 /* get NTP and RTP times */
1630 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1633 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1634 /* ignore SR that is not ours */
1635 if (ssrc != stream->ssrc)
1640 case GST_RTCP_TYPE_SDES:
1642 gboolean more_items, more_entries;
1644 /* only deal with first SDES, there is only supposed to be one SDES in
1645 * the RTCP packet but we deal with bad packets gracefully. Also bail
1646 * out if we have not seen an SR item yet. */
1647 if (have_sdes || !have_sr)
1650 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1651 /* skip items that are not about the SSRC of the sender */
1652 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1655 /* find the CNAME entry */
1656 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1657 GstRTCPSDESType type;
1661 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1663 if (type == GST_RTCP_SDES_CNAME) {
1664 GST_RTP_BIN_LOCK (bin);
1665 /* associate the stream to CNAME */
1666 gst_rtp_bin_associate (bin, stream, len, data,
1667 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1669 GST_RTP_BIN_UNLOCK (bin);
1677 /* we can ignore these packets */
1681 gst_rtcp_buffer_unmap (&rtcp);
1684 /* create a new stream with @ssrc in @session. Must be called with
1685 * RTP_SESSION_LOCK. */
1686 static GstRtpBinStream *
1687 create_stream (GstRtpBinSession * session, guint32 ssrc)
1689 GstElement *buffer, *demux = NULL;
1690 GstRtpBinStream *stream;
1694 rtpbin = session->bin;
1696 if (g_slist_length (session->streams) >= rtpbin->max_streams)
1699 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1700 goto no_jitterbuffer;
1702 if (!rtpbin->ignore_pt) {
1703 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1707 stream = g_new0 (GstRtpBinStream, 1);
1708 stream->ssrc = ssrc;
1709 stream->bin = rtpbin;
1710 stream->session = session;
1711 stream->buffer = buffer;
1712 stream->demux = demux;
1714 stream->have_sync = FALSE;
1715 stream->rt_delta = 0;
1716 stream->rtp_delta = 0;
1717 stream->percent = 100;
1718 stream->clock_base = -100 * GST_SECOND;
1719 session->streams = g_slist_prepend (session->streams, stream);
1721 /* provide clock_rate to the jitterbuffer when needed */
1722 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1723 (GCallback) pt_map_requested, session);
1724 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1725 (GCallback) on_npt_stop, stream);
1727 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1728 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1730 /* configure latency and packet lost */
1731 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1732 g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1733 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1734 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1735 g_object_set (buffer, "do-retransmission", rtpbin->do_retransmission, NULL);
1736 g_object_set (buffer, "max-rtcp-rtp-time-diff",
1737 rtpbin->max_rtcp_rtp_time_diff, NULL);
1738 g_object_set (buffer, "max-dropout-time", rtpbin->max_dropout_time,
1739 "max-misorder-time", rtpbin->max_misorder_time, NULL);
1740 g_object_set (buffer, "rfc7273-sync", rtpbin->rfc7273_sync, NULL);
1741 g_object_set (buffer, "max-ts-offset-adjustment",
1742 rtpbin->max_ts_offset_adjustment, NULL);
1744 /* need to sink the jitterbufer or otherwise signal handlers from bindings will
1745 * take ownership of it and we don't own it anymore */
1746 gst_object_ref_sink (buffer);
1747 g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER], 0,
1748 buffer, session->id, ssrc);
1750 if (!rtpbin->ignore_pt)
1751 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1752 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1754 /* unref the jitterbuffer again, the bin has a reference now and
1755 * we don't need it anymore */
1756 gst_object_unref (buffer);
1760 gst_element_link_pads_full (buffer, "src", demux, "sink",
1761 GST_PAD_LINK_CHECK_NOTHING);
1763 if (rtpbin->buffering) {
1766 GST_INFO_OBJECT (rtpbin,
1767 "bin is buffering, set jitterbuffer as not active");
1768 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1772 GST_OBJECT_LOCK (rtpbin);
1773 target = GST_STATE_TARGET (rtpbin);
1774 GST_OBJECT_UNLOCK (rtpbin);
1776 /* from sink to source */
1778 gst_element_set_state (demux, target);
1780 gst_element_set_state (buffer, target);
1787 GST_WARNING_OBJECT (rtpbin, "stream exeeds maximum (%d)",
1788 rtpbin->max_streams);
1793 g_warning ("rtpbin: could not create rtpjitterbuffer element");
1798 gst_object_unref (buffer);
1799 g_warning ("rtpbin: could not create rtpptdemux element");
1804 /* called with RTP_BIN_LOCK */
1806 free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
1808 GSList *clients, *next_client;
1810 GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
1812 if (stream->demux) {
1813 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1814 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1815 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1817 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1818 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1819 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1822 gst_element_set_locked_state (stream->demux, TRUE);
1823 gst_element_set_locked_state (stream->buffer, TRUE);
1826 gst_element_set_state (stream->demux, GST_STATE_NULL);
1827 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1829 /* now remove this signal, we need this while going to NULL because it to
1830 * do some cleanups */
1832 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1834 gst_bin_remove (GST_BIN_CAST (bin), stream->buffer);
1836 gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
1838 for (clients = bin->clients; clients; clients = next_client) {
1839 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1840 GSList *streams, *next_stream;
1842 next_client = g_slist_next (clients);
1844 for (streams = client->streams; streams; streams = next_stream) {
1845 GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
1847 next_stream = g_slist_next (streams);
1849 if (ostream == stream) {
1850 client->streams = g_slist_delete_link (client->streams, streams);
1851 /* If this was the last stream belonging to this client,
1852 * clean up the client. */
1853 if (--client->nstreams == 0) {
1854 bin->clients = g_slist_delete_link (bin->clients, clients);
1855 free_client (client, bin);
1864 /* GObject vmethods */
1865 static void gst_rtp_bin_dispose (GObject * object);
1866 static void gst_rtp_bin_finalize (GObject * object);
1867 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1868 const GValue * value, GParamSpec * pspec);
1869 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1870 GValue * value, GParamSpec * pspec);
1872 /* GstElement vmethods */
1873 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1874 GstStateChange transition);
1875 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1876 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1877 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1878 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1880 #define gst_rtp_bin_parent_class parent_class
1881 G_DEFINE_TYPE_WITH_PRIVATE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1884 _gst_element_accumulator (GSignalInvocationHint * ihint,
1885 GValue * return_accu, const GValue * handler_return, gpointer dummy)
1887 GstElement *element;
1889 element = g_value_get_object (handler_return);
1890 GST_DEBUG ("got element %" GST_PTR_FORMAT, element);
1892 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
1893 g_value_set_object (return_accu, element);
1895 /* stop emission if we have an element */
1896 return (element == NULL);
1900 _gst_caps_accumulator (GSignalInvocationHint * ihint,
1901 GValue * return_accu, const GValue * handler_return, gpointer dummy)
1905 caps = g_value_get_boxed (handler_return);
1906 GST_DEBUG ("got caps %" GST_PTR_FORMAT, caps);
1908 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
1909 g_value_set_boxed (return_accu, caps);
1911 /* stop emission if we have a caps */
1912 return (caps == NULL);
1916 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1918 GObjectClass *gobject_class;
1919 GstElementClass *gstelement_class;
1920 GstBinClass *gstbin_class;
1922 gobject_class = (GObjectClass *) klass;
1923 gstelement_class = (GstElementClass *) klass;
1924 gstbin_class = (GstBinClass *) klass;
1926 gobject_class->dispose = gst_rtp_bin_dispose;
1927 gobject_class->finalize = gst_rtp_bin_finalize;
1928 gobject_class->set_property = gst_rtp_bin_set_property;
1929 gobject_class->get_property = gst_rtp_bin_get_property;
1931 g_object_class_install_property (gobject_class, PROP_LATENCY,
1932 g_param_spec_uint ("latency", "Buffer latency in ms",
1933 "Default amount of ms to buffer in the jitterbuffers", 0,
1934 G_MAXUINT, DEFAULT_LATENCY_MS,
1935 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1937 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
1938 g_param_spec_boolean ("drop-on-latency",
1939 "Drop buffers when maximum latency is reached",
1940 "Tells the jitterbuffer to never exceed the given latency in size",
1941 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1944 * GstRtpBin::request-pt-map:
1945 * @rtpbin: the object which received the signal
1946 * @session: the session
1949 * Request the payload type as #GstCaps for @pt in @session.
1951 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1952 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1953 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1954 _gst_caps_accumulator, NULL, g_cclosure_marshal_generic, GST_TYPE_CAPS,
1955 2, G_TYPE_UINT, G_TYPE_UINT);
1958 * GstRtpBin::payload-type-change:
1959 * @rtpbin: the object which received the signal
1960 * @session: the session
1963 * Signal that the current payload type changed to @pt in @session.
1965 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1966 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1967 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1968 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
1972 * GstRtpBin::clear-pt-map:
1973 * @rtpbin: the object which received the signal
1975 * Clear all previously cached pt-mapping obtained with
1976 * #GstRtpBin::request-pt-map.
1978 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1979 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1980 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1981 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1985 * GstRtpBin::reset-sync:
1986 * @rtpbin: the object which received the signal
1988 * Reset all currently configured lip-sync parameters and require new SR
1989 * packets for all streams before lip-sync is attempted again.
1991 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1992 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1993 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1994 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1998 * GstRtpBin::get-session:
1999 * @rtpbin: the object which received the signal
2000 * @id: the session id
2002 * Request the related GstRtpSession as #GstElement related with session @id.
2006 gst_rtp_bin_signals[SIGNAL_GET_SESSION] =
2007 g_signal_new ("get-session", G_TYPE_FROM_CLASS (klass),
2008 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2009 get_session), NULL, NULL, g_cclosure_marshal_generic,
2010 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2013 * GstRtpBin::get-internal-session:
2014 * @rtpbin: the object which received the signal
2015 * @id: the session id
2017 * Request the internal RTPSession object as #GObject in session @id.
2019 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
2020 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
2021 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2022 get_internal_session), NULL, NULL, g_cclosure_marshal_generic,
2023 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
2026 * GstRtpBin::get-internal-storage:
2027 * @rtpbin: the object which received the signal
2028 * @id: the session id
2030 * Request the internal RTPStorage object as #GObject in session @id.
2034 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_STORAGE] =
2035 g_signal_new ("get-internal-storage", G_TYPE_FROM_CLASS (klass),
2036 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2037 get_internal_storage), NULL, NULL, g_cclosure_marshal_generic,
2038 G_TYPE_OBJECT, 1, G_TYPE_UINT);
2041 * GstRtpBin::get-storage:
2042 * @rtpbin: the object which received the signal
2043 * @id: the session id
2045 * Request the RTPStorage element as #GObject in session @id.
2049 gst_rtp_bin_signals[SIGNAL_GET_STORAGE] =
2050 g_signal_new ("get-storage", G_TYPE_FROM_CLASS (klass),
2051 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2052 get_storage), NULL, NULL, g_cclosure_marshal_generic,
2053 GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2056 * GstRtpBin::on-new-ssrc:
2057 * @rtpbin: the object which received the signal
2058 * @session: the session
2061 * Notify of a new SSRC that entered @session.
2063 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
2064 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
2065 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
2066 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2069 * GstRtpBin::on-ssrc-collision:
2070 * @rtpbin: the object which received the signal
2071 * @session: the session
2074 * Notify when we have an SSRC collision
2076 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
2077 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
2078 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
2079 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2082 * GstRtpBin::on-ssrc-validated:
2083 * @rtpbin: the object which received the signal
2084 * @session: the session
2087 * Notify of a new SSRC that became validated.
2089 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
2090 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
2091 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
2092 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2095 * GstRtpBin::on-ssrc-active:
2096 * @rtpbin: the object which received the signal
2097 * @session: the session
2100 * Notify of a SSRC that is active, i.e., sending RTCP.
2102 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
2103 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
2104 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
2105 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2108 * GstRtpBin::on-ssrc-sdes:
2109 * @rtpbin: the object which received the signal
2110 * @session: the session
2113 * Notify of a SSRC that is active, i.e., sending RTCP.
2115 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
2116 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
2117 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
2118 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2122 * GstRtpBin::on-bye-ssrc:
2123 * @rtpbin: the object which received the signal
2124 * @session: the session
2127 * Notify of an SSRC that became inactive because of a BYE packet.
2129 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
2130 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
2131 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
2132 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2135 * GstRtpBin::on-bye-timeout:
2136 * @rtpbin: the object which received the signal
2137 * @session: the session
2140 * Notify of an SSRC that has timed out because of BYE
2142 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
2143 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
2144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
2145 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2148 * GstRtpBin::on-timeout:
2149 * @rtpbin: the object which received the signal
2150 * @session: the session
2153 * Notify of an SSRC that has timed out
2155 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
2156 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
2157 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
2158 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2161 * GstRtpBin::on-sender-timeout:
2162 * @rtpbin: the object which received the signal
2163 * @session: the session
2166 * Notify of a sender SSRC that has timed out and became a receiver
2168 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
2169 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
2170 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
2171 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2175 * GstRtpBin::on-npt-stop:
2176 * @rtpbin: the object which received the signal
2177 * @session: the session
2180 * Notify that SSRC sender has sent data up to the configured NPT stop time.
2182 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
2183 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
2184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
2185 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2189 * GstRtpBin::request-rtp-encoder:
2190 * @rtpbin: the object which received the signal
2191 * @session: the session
2193 * Request an RTP encoder element for the given @session. The encoder
2194 * element will be added to the bin if not previously added.
2196 * If no handler is connected, no encoder will be used.
2200 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_ENCODER] =
2201 g_signal_new ("request-rtp-encoder", G_TYPE_FROM_CLASS (klass),
2202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2203 request_rtp_encoder), _gst_element_accumulator, NULL,
2204 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2207 * GstRtpBin::request-rtp-decoder:
2208 * @rtpbin: the object which received the signal
2209 * @session: the session
2211 * Request an RTP decoder element for the given @session. The decoder
2212 * element will be added to the bin if not previously added.
2214 * If no handler is connected, no encoder will be used.
2218 gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_DECODER] =
2219 g_signal_new ("request-rtp-decoder", G_TYPE_FROM_CLASS (klass),
2220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2221 request_rtp_decoder), _gst_element_accumulator, NULL,
2222 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2225 * GstRtpBin::request-rtcp-encoder:
2226 * @rtpbin: the object which received the signal
2227 * @session: the session
2229 * Request an RTCP encoder element for the given @session. The encoder
2230 * element will be added to the bin if not previously added.
2232 * If no handler is connected, no encoder will be used.
2236 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_ENCODER] =
2237 g_signal_new ("request-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
2238 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2239 request_rtcp_encoder), _gst_element_accumulator, NULL,
2240 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2243 * GstRtpBin::request-rtcp-decoder:
2244 * @rtpbin: the object which received the signal
2245 * @session: the session
2247 * Request an RTCP decoder element for the given @session. The decoder
2248 * element will be added to the bin if not previously added.
2250 * If no handler is connected, no encoder will be used.
2254 gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_DECODER] =
2255 g_signal_new ("request-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
2256 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2257 request_rtcp_decoder), _gst_element_accumulator, NULL,
2258 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2261 * GstRtpBin::new-jitterbuffer:
2262 * @rtpbin: the object which received the signal
2263 * @jitterbuffer: the new jitterbuffer
2264 * @session: the session
2267 * Notify that a new @jitterbuffer was created for @session and @ssrc.
2268 * This signal can, for example, be used to configure @jitterbuffer.
2272 gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER] =
2273 g_signal_new ("new-jitterbuffer", G_TYPE_FROM_CLASS (klass),
2274 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2275 new_jitterbuffer), NULL, NULL, g_cclosure_marshal_generic,
2276 G_TYPE_NONE, 3, GST_TYPE_ELEMENT, G_TYPE_UINT, G_TYPE_UINT);
2279 * GstRtpBin::new-storage:
2280 * @rtpbin: the object which received the signal
2281 * @storage: the new storage
2282 * @session: the session
2284 * Notify that a new @storage was created for @session.
2285 * This signal can, for example, be used to configure @storage.
2289 gst_rtp_bin_signals[SIGNAL_NEW_STORAGE] =
2290 g_signal_new ("new-storage", G_TYPE_FROM_CLASS (klass),
2291 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2292 new_storage), NULL, NULL, g_cclosure_marshal_generic,
2293 G_TYPE_NONE, 2, GST_TYPE_ELEMENT, G_TYPE_UINT);
2296 * GstRtpBin::request-aux-sender:
2297 * @rtpbin: the object which received the signal
2298 * @session: the session
2300 * Request an AUX sender element for the given @session. The AUX
2301 * element will be added to the bin.
2303 * If no handler is connected, no AUX element will be used.
2307 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_SENDER] =
2308 g_signal_new ("request-aux-sender", G_TYPE_FROM_CLASS (klass),
2309 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2310 request_aux_sender), _gst_element_accumulator, NULL,
2311 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2314 * GstRtpBin::request-aux-receiver:
2315 * @rtpbin: the object which received the signal
2316 * @session: the session
2318 * Request an AUX receiver element for the given @session. The AUX
2319 * element will be added to the bin.
2321 * If no handler is connected, no AUX element will be used.
2325 gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_RECEIVER] =
2326 g_signal_new ("request-aux-receiver", G_TYPE_FROM_CLASS (klass),
2327 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2328 request_aux_receiver), _gst_element_accumulator, NULL,
2329 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2332 * GstRtpBin::request-fec-decoder:
2333 * @rtpbin: the object which received the signal
2334 * @session: the session index
2336 * Request a FEC decoder element for the given @session. The element
2337 * will be added to the bin after the pt demuxer.
2339 * If no handler is connected, no FEC decoder will be used.
2343 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_DECODER] =
2344 g_signal_new ("request-fec-decoder", G_TYPE_FROM_CLASS (klass),
2345 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2346 request_fec_decoder), _gst_element_accumulator, NULL,
2347 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2350 * GstRtpBin::request-fec-encoder:
2351 * @rtpbin: the object which received the signal
2352 * @session: the session index
2354 * Request a FEC encoder element for the given @session. The element
2355 * will be added to the bin after the RTPSession.
2357 * If no handler is connected, no FEC encoder will be used.
2361 gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_ENCODER] =
2362 g_signal_new ("request-fec-encoder", G_TYPE_FROM_CLASS (klass),
2363 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2364 request_fec_encoder), _gst_element_accumulator, NULL,
2365 g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2368 * GstRtpBin::on-new-sender-ssrc:
2369 * @rtpbin: the object which received the signal
2370 * @session: the session
2371 * @ssrc: the sender SSRC
2373 * Notify of a new sender SSRC that entered @session.
2377 gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
2378 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
2379 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_sender_ssrc),
2380 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2383 * GstRtpBin::on-sender-ssrc-active:
2384 * @rtpbin: the object which received the signal
2385 * @session: the session
2386 * @ssrc: the sender SSRC
2388 * Notify of a sender SSRC that is active, i.e., sending RTCP.
2392 gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
2393 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
2394 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2395 on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_generic,
2396 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2398 g_object_class_install_property (gobject_class, PROP_SDES,
2399 g_param_spec_boxed ("sdes", "SDES",
2400 "The SDES items of this session",
2401 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2403 g_object_class_install_property (gobject_class, PROP_DO_LOST,
2404 g_param_spec_boolean ("do-lost", "Do Lost",
2405 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
2406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2408 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
2409 g_param_spec_boolean ("autoremove", "Auto Remove",
2410 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
2411 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2413 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
2414 g_param_spec_boolean ("ignore-pt", "Ignore PT",
2415 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
2416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2418 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
2419 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
2420 "Use the pipeline running-time to set the NTP time in the RTCP SR messages "
2421 "(DEPRECATED: Use ntp-time-source property)",
2422 DEFAULT_USE_PIPELINE_CLOCK,
2423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
2425 * GstRtpBin:buffer-mode:
2427 * Control the buffering and timestamping mode used by the jitterbuffer.
2429 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
2430 g_param_spec_enum ("buffer-mode", "Buffer Mode",
2431 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
2432 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2434 * GstRtpBin:ntp-sync:
2436 * Set the NTP time from the sender reports as the running-time on the
2437 * buffers. When both the sender and receiver have sychronized
2438 * running-time, i.e. when the clock and base-time is shared
2439 * between the receivers and the and the senders, this option can be
2440 * used to synchronize receivers on multiple machines.
2442 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
2443 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
2444 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
2445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2448 * GstRtpBin:rtcp-sync:
2450 * If not synchronizing (directly) to the NTP clock, determines how to sync
2451 * the various streams.
2453 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
2454 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
2455 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
2456 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2459 * GstRtpBin:rtcp-sync-interval:
2461 * Determines how often to sync streams using RTCP data.
2463 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
2464 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
2465 "RTCP SR interval synchronization (ms) (0 = always)",
2466 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
2467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2469 g_object_class_install_property (gobject_class, PROP_DO_SYNC_EVENT,
2470 g_param_spec_boolean ("do-sync-event", "Do Sync Event",
2471 "Send event downstream when a stream is synchronized to the sender",
2472 DEFAULT_DO_SYNC_EVENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2475 * GstRtpBin:do-retransmission:
2477 * Enables RTP retransmission on all streams. To control retransmission on
2478 * a per-SSRC basis, connect to the #GstRtpBin::new-jitterbuffer signal and
2479 * set the #GstRtpJitterBuffer::do-retransmission property on the
2480 * #GstRtpJitterBuffer object instead.
2482 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
2483 g_param_spec_boolean ("do-retransmission", "Do retransmission",
2484 "Enable retransmission on all streams",
2485 DEFAULT_DO_RETRANSMISSION,
2486 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2489 * GstRtpBin:rtp-profile:
2491 * Sets the default RTP profile of newly created RTP sessions. The
2492 * profile can be changed afterwards on a per-session basis.
2494 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
2495 g_param_spec_enum ("rtp-profile", "RTP Profile",
2496 "Default RTP profile of newly created sessions",
2497 GST_TYPE_RTP_PROFILE, DEFAULT_RTP_PROFILE,
2498 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2500 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
2501 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
2502 "NTP time source for RTCP packets",
2503 gst_rtp_ntp_time_source_get_type (), DEFAULT_NTP_TIME_SOURCE,
2504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2506 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_SEND_TIME,
2507 g_param_spec_boolean ("rtcp-sync-send-time", "RTCP Sync Send Time",
2508 "Use send time or capture time for RTCP sync "
2509 "(TRUE = send time, FALSE = capture time)",
2510 DEFAULT_RTCP_SYNC_SEND_TIME,
2511 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2513 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
2514 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
2515 "Maximum amount of time in ms that the RTP time in RTCP SRs "
2516 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
2517 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
2518 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2520 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
2521 g_param_spec_uint ("max-dropout-time", "Max dropout time",
2522 "The maximum time (milliseconds) of missing packets tolerated.",
2523 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
2524 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2526 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
2527 g_param_spec_uint ("max-misorder-time", "Max misorder time",
2528 "The maximum time (milliseconds) of misordered packets tolerated.",
2529 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
2530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2532 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
2533 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
2534 "Synchronize received streams to the RFC7273 clock "
2535 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
2536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2538 g_object_class_install_property (gobject_class, PROP_MAX_STREAMS,
2539 g_param_spec_uint ("max-streams", "Max Streams",
2540 "The maximum number of streams to create for one session",
2541 0, G_MAXUINT, DEFAULT_MAX_STREAMS,
2542 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2545 * GstRtpBin:max-ts-offset-adjustment:
2547 * Syncing time stamps to NTP time adds a time offset. This parameter
2548 * specifies the maximum number of nanoseconds per frame that this time offset
2549 * may be adjusted with. This is used to avoid sudden large changes to time
2554 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
2555 g_param_spec_uint64 ("max-ts-offset-adjustment",
2556 "Max Timestamp Offset Adjustment",
2557 "The maximum number of nanoseconds per frame that time stamp offsets "
2558 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
2559 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
2560 G_PARAM_STATIC_STRINGS));
2563 * GstRtpBin:max-ts-offset:
2565 * Used to set an upper limit of how large a time offset may be. This
2566 * is used to protect against unrealistic values as a result of either
2567 * client,server or clock issues.
2571 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
2572 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
2573 "The maximum absolute value of the time offset in (nanoseconds). "
2574 "Note, if the ntp-sync parameter is set the default value is "
2575 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
2576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2578 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
2579 gstelement_class->request_new_pad =
2580 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
2581 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
2584 gst_element_class_add_static_pad_template (gstelement_class,
2585 &rtpbin_recv_rtp_sink_template);
2586 gst_element_class_add_static_pad_template (gstelement_class,
2587 &rtpbin_recv_rtcp_sink_template);
2588 gst_element_class_add_static_pad_template (gstelement_class,
2589 &rtpbin_send_rtp_sink_template);
2592 gst_element_class_add_static_pad_template (gstelement_class,
2593 &rtpbin_recv_rtp_src_template);
2594 gst_element_class_add_static_pad_template (gstelement_class,
2595 &rtpbin_send_rtcp_src_template);
2596 gst_element_class_add_static_pad_template (gstelement_class,
2597 &rtpbin_send_rtp_src_template);
2599 gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
2600 "Filter/Network/RTP",
2601 "Real-Time Transport Protocol bin",
2602 "Wim Taymans <wim.taymans@gmail.com>");
2604 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
2606 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
2607 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
2608 klass->get_session = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_session);
2609 klass->get_internal_session =
2610 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
2611 klass->get_storage = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_storage);
2612 klass->get_internal_storage =
2613 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_storage);
2614 klass->request_rtp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2615 klass->request_rtp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2616 klass->request_rtcp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2617 klass->request_rtcp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2619 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
2623 gst_rtp_bin_init (GstRtpBin * rtpbin)
2627 rtpbin->priv = gst_rtp_bin_get_instance_private (rtpbin);
2628 g_mutex_init (&rtpbin->priv->bin_lock);
2629 g_mutex_init (&rtpbin->priv->dyn_lock);
2631 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
2632 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
2633 rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
2634 rtpbin->do_lost = DEFAULT_DO_LOST;
2635 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
2636 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
2637 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
2638 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
2639 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
2640 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
2641 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
2642 rtpbin->send_sync_event = DEFAULT_DO_SYNC_EVENT;
2643 rtpbin->do_retransmission = DEFAULT_DO_RETRANSMISSION;
2644 rtpbin->rtp_profile = DEFAULT_RTP_PROFILE;
2645 rtpbin->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
2646 rtpbin->rtcp_sync_send_time = DEFAULT_RTCP_SYNC_SEND_TIME;
2647 rtpbin->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
2648 rtpbin->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
2649 rtpbin->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
2650 rtpbin->rfc7273_sync = DEFAULT_RFC7273_SYNC;
2651 rtpbin->max_streams = DEFAULT_MAX_STREAMS;
2652 rtpbin->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
2653 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2654 rtpbin->max_ts_offset_is_set = FALSE;
2656 /* some default SDES entries */
2657 cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
2658 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
2659 "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
2664 gst_rtp_bin_dispose (GObject * object)
2668 rtpbin = GST_RTP_BIN (object);
2670 GST_RTP_BIN_LOCK (rtpbin);
2671 GST_DEBUG_OBJECT (object, "freeing sessions");
2672 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
2673 g_slist_free (rtpbin->sessions);
2674 rtpbin->sessions = NULL;
2675 GST_RTP_BIN_UNLOCK (rtpbin);
2677 G_OBJECT_CLASS (parent_class)->dispose (object);
2681 gst_rtp_bin_finalize (GObject * object)
2685 rtpbin = GST_RTP_BIN (object);
2688 gst_structure_free (rtpbin->sdes);
2690 g_mutex_clear (&rtpbin->priv->bin_lock);
2691 g_mutex_clear (&rtpbin->priv->dyn_lock);
2693 G_OBJECT_CLASS (parent_class)->finalize (object);
2698 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
2705 GST_RTP_BIN_LOCK (bin);
2707 GST_OBJECT_LOCK (bin);
2709 gst_structure_free (bin->sdes);
2710 bin->sdes = gst_structure_copy (sdes);
2711 GST_OBJECT_UNLOCK (bin);
2713 /* store in all sessions */
2714 for (item = bin->sessions; item; item = g_slist_next (item)) {
2715 GstRtpBinSession *session = item->data;
2716 g_object_set (session->session, "sdes", sdes, NULL);
2719 GST_RTP_BIN_UNLOCK (bin);
2722 static GstStructure *
2723 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
2725 GstStructure *result;
2727 GST_OBJECT_LOCK (bin);
2728 result = gst_structure_copy (bin->sdes);
2729 GST_OBJECT_UNLOCK (bin);
2735 gst_rtp_bin_set_property (GObject * object, guint prop_id,
2736 const GValue * value, GParamSpec * pspec)
2740 rtpbin = GST_RTP_BIN (object);
2744 GST_RTP_BIN_LOCK (rtpbin);
2745 rtpbin->latency_ms = g_value_get_uint (value);
2746 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
2747 GST_RTP_BIN_UNLOCK (rtpbin);
2748 /* propagate the property down to the jitterbuffer */
2749 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2751 case PROP_DROP_ON_LATENCY:
2752 GST_RTP_BIN_LOCK (rtpbin);
2753 rtpbin->drop_on_latency = g_value_get_boolean (value);
2754 GST_RTP_BIN_UNLOCK (rtpbin);
2755 /* propagate the property down to the jitterbuffer */
2756 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2757 "drop-on-latency", value);
2760 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2763 GST_RTP_BIN_LOCK (rtpbin);
2764 rtpbin->do_lost = g_value_get_boolean (value);
2765 GST_RTP_BIN_UNLOCK (rtpbin);
2766 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2769 rtpbin->ntp_sync = g_value_get_boolean (value);
2770 /* The default value of max_ts_offset depends on ntp_sync. If user
2771 * hasn't set it then change default value */
2772 if (!rtpbin->max_ts_offset_is_set) {
2773 if (rtpbin->ntp_sync) {
2774 rtpbin->max_ts_offset = 0;
2776 rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2780 case PROP_RTCP_SYNC:
2781 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2783 case PROP_RTCP_SYNC_INTERVAL:
2784 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2786 case PROP_IGNORE_PT:
2787 rtpbin->ignore_pt = g_value_get_boolean (value);
2789 case PROP_AUTOREMOVE:
2790 rtpbin->priv->autoremove = g_value_get_boolean (value);
2792 case PROP_USE_PIPELINE_CLOCK:
2795 GST_RTP_BIN_LOCK (rtpbin);
2796 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2797 for (sessions = rtpbin->sessions; sessions;
2798 sessions = g_slist_next (sessions)) {
2799 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2801 g_object_set (G_OBJECT (session->session),
2802 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2804 GST_RTP_BIN_UNLOCK (rtpbin);
2807 case PROP_DO_SYNC_EVENT:
2808 rtpbin->send_sync_event = g_value_get_boolean (value);
2810 case PROP_BUFFER_MODE:
2811 GST_RTP_BIN_LOCK (rtpbin);
2812 rtpbin->buffer_mode = g_value_get_enum (value);
2813 GST_RTP_BIN_UNLOCK (rtpbin);
2814 /* propagate the property down to the jitterbuffer */
2815 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2817 case PROP_DO_RETRANSMISSION:
2818 GST_RTP_BIN_LOCK (rtpbin);
2819 rtpbin->do_retransmission = g_value_get_boolean (value);
2820 GST_RTP_BIN_UNLOCK (rtpbin);
2821 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2822 "do-retransmission", value);
2824 case PROP_RTP_PROFILE:
2825 rtpbin->rtp_profile = g_value_get_enum (value);
2827 case PROP_NTP_TIME_SOURCE:{
2829 GST_RTP_BIN_LOCK (rtpbin);
2830 rtpbin->ntp_time_source = g_value_get_enum (value);
2831 for (sessions = rtpbin->sessions; sessions;
2832 sessions = g_slist_next (sessions)) {
2833 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2835 g_object_set (G_OBJECT (session->session),
2836 "ntp-time-source", rtpbin->ntp_time_source, NULL);
2838 GST_RTP_BIN_UNLOCK (rtpbin);
2841 case PROP_RTCP_SYNC_SEND_TIME:{
2843 GST_RTP_BIN_LOCK (rtpbin);
2844 rtpbin->rtcp_sync_send_time = g_value_get_boolean (value);
2845 for (sessions = rtpbin->sessions; sessions;
2846 sessions = g_slist_next (sessions)) {
2847 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2849 g_object_set (G_OBJECT (session->session),
2850 "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time, NULL);
2852 GST_RTP_BIN_UNLOCK (rtpbin);
2855 case PROP_MAX_RTCP_RTP_TIME_DIFF:
2856 GST_RTP_BIN_LOCK (rtpbin);
2857 rtpbin->max_rtcp_rtp_time_diff = g_value_get_int (value);
2858 GST_RTP_BIN_UNLOCK (rtpbin);
2859 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2860 "max-rtcp-rtp-time-diff", value);
2862 case PROP_MAX_DROPOUT_TIME:
2863 GST_RTP_BIN_LOCK (rtpbin);
2864 rtpbin->max_dropout_time = g_value_get_uint (value);
2865 GST_RTP_BIN_UNLOCK (rtpbin);
2866 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2867 "max-dropout-time", value);
2868 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-dropout-time",
2871 case PROP_MAX_MISORDER_TIME:
2872 GST_RTP_BIN_LOCK (rtpbin);
2873 rtpbin->max_misorder_time = g_value_get_uint (value);
2874 GST_RTP_BIN_UNLOCK (rtpbin);
2875 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2876 "max-misorder-time", value);
2877 gst_rtp_bin_propagate_property_to_session (rtpbin, "max-misorder-time",
2880 case PROP_RFC7273_SYNC:
2881 rtpbin->rfc7273_sync = g_value_get_boolean (value);
2882 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2883 "rfc7273-sync", value);
2885 case PROP_MAX_STREAMS:
2886 rtpbin->max_streams = g_value_get_uint (value);
2888 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
2889 rtpbin->max_ts_offset_adjustment = g_value_get_uint64 (value);
2890 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2891 "max-ts-offset-adjustment", value);
2893 case PROP_MAX_TS_OFFSET:
2894 rtpbin->max_ts_offset = g_value_get_int64 (value);
2895 rtpbin->max_ts_offset_is_set = TRUE;
2898 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2904 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2905 GValue * value, GParamSpec * pspec)
2909 rtpbin = GST_RTP_BIN (object);
2913 GST_RTP_BIN_LOCK (rtpbin);
2914 g_value_set_uint (value, rtpbin->latency_ms);
2915 GST_RTP_BIN_UNLOCK (rtpbin);
2917 case PROP_DROP_ON_LATENCY:
2918 GST_RTP_BIN_LOCK (rtpbin);
2919 g_value_set_boolean (value, rtpbin->drop_on_latency);
2920 GST_RTP_BIN_UNLOCK (rtpbin);
2923 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2926 GST_RTP_BIN_LOCK (rtpbin);
2927 g_value_set_boolean (value, rtpbin->do_lost);
2928 GST_RTP_BIN_UNLOCK (rtpbin);
2930 case PROP_IGNORE_PT:
2931 g_value_set_boolean (value, rtpbin->ignore_pt);
2934 g_value_set_boolean (value, rtpbin->ntp_sync);
2936 case PROP_RTCP_SYNC:
2937 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2939 case PROP_RTCP_SYNC_INTERVAL:
2940 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2942 case PROP_AUTOREMOVE:
2943 g_value_set_boolean (value, rtpbin->priv->autoremove);
2945 case PROP_BUFFER_MODE:
2946 g_value_set_enum (value, rtpbin->buffer_mode);
2948 case PROP_USE_PIPELINE_CLOCK:
2949 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2951 case PROP_DO_SYNC_EVENT:
2952 g_value_set_boolean (value, rtpbin->send_sync_event);
2954 case PROP_DO_RETRANSMISSION:
2955 GST_RTP_BIN_LOCK (rtpbin);
2956 g_value_set_boolean (value, rtpbin->do_retransmission);
2957 GST_RTP_BIN_UNLOCK (rtpbin);
2959 case PROP_RTP_PROFILE:
2960 g_value_set_enum (value, rtpbin->rtp_profile);
2962 case PROP_NTP_TIME_SOURCE:
2963 g_value_set_enum (value, rtpbin->ntp_time_source);
2965 case PROP_RTCP_SYNC_SEND_TIME:
2966 g_value_set_boolean (value, rtpbin->rtcp_sync_send_time);
2968 case PROP_MAX_RTCP_RTP_TIME_DIFF:
2969 GST_RTP_BIN_LOCK (rtpbin);
2970 g_value_set_int (value, rtpbin->max_rtcp_rtp_time_diff);
2971 GST_RTP_BIN_UNLOCK (rtpbin);
2973 case PROP_MAX_DROPOUT_TIME:
2974 g_value_set_uint (value, rtpbin->max_dropout_time);
2976 case PROP_MAX_MISORDER_TIME:
2977 g_value_set_uint (value, rtpbin->max_misorder_time);
2979 case PROP_RFC7273_SYNC:
2980 g_value_set_boolean (value, rtpbin->rfc7273_sync);
2982 case PROP_MAX_STREAMS:
2983 g_value_set_uint (value, rtpbin->max_streams);
2985 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
2986 g_value_set_uint64 (value, rtpbin->max_ts_offset_adjustment);
2988 case PROP_MAX_TS_OFFSET:
2989 g_value_set_int64 (value, rtpbin->max_ts_offset);
2992 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2998 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
3002 rtpbin = GST_RTP_BIN (bin);
3004 switch (GST_MESSAGE_TYPE (message)) {
3005 case GST_MESSAGE_ELEMENT:
3007 const GstStructure *s = gst_message_get_structure (message);
3009 /* we change the structure name and add the session ID to it */
3010 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
3011 GstRtpBinSession *sess;
3013 /* find the session we set it as object data */
3014 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3015 "GstRTPBin.session");
3017 if (G_LIKELY (sess)) {
3018 message = gst_message_make_writable (message);
3019 s = gst_message_get_structure (message);
3020 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
3024 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3027 case GST_MESSAGE_BUFFERING:
3030 gint min_percent = 100;
3031 GSList *sessions, *streams;
3032 GstRtpBinStream *stream;
3033 gboolean change = FALSE, active = FALSE;
3034 GstClockTime min_out_time;
3035 GstBufferingMode mode;
3036 gint avg_in, avg_out;
3037 gint64 buffering_left;
3039 gst_message_parse_buffering (message, &percent);
3040 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
3044 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3045 "GstRTPBin.stream");
3047 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
3049 /* get the stream */
3050 if (G_LIKELY (stream)) {
3051 GST_RTP_BIN_LOCK (rtpbin);
3052 /* fill in the percent */
3053 stream->percent = percent;
3055 /* calculate the min value for all streams */
3056 for (sessions = rtpbin->sessions; sessions;
3057 sessions = g_slist_next (sessions)) {
3058 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3060 GST_RTP_SESSION_LOCK (session);
3061 if (session->streams) {
3062 for (streams = session->streams; streams;
3063 streams = g_slist_next (streams)) {
3064 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3066 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
3069 /* find min percent */
3070 if (min_percent > stream->percent)
3071 min_percent = stream->percent;
3074 GST_INFO_OBJECT (bin,
3075 "session has no streams, setting min_percent to 0");
3078 GST_RTP_SESSION_UNLOCK (session);
3080 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
3082 if (rtpbin->buffering) {
3083 if (min_percent == 100) {
3084 rtpbin->buffering = FALSE;
3089 if (min_percent < 100) {
3090 /* pause the streams */
3091 rtpbin->buffering = TRUE;
3096 GST_RTP_BIN_UNLOCK (rtpbin);
3098 gst_message_unref (message);
3100 /* make a new buffering message with the min value */
3102 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
3103 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
3106 if (G_UNLIKELY (change)) {
3108 guint64 running_time = 0;
3111 /* figure out the running time when we have a clock */
3112 if (G_LIKELY ((clock =
3113 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
3114 guint64 now, base_time;
3116 now = gst_clock_get_time (clock);
3117 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
3118 running_time = now - base_time;
3119 gst_object_unref (clock);
3121 GST_DEBUG_OBJECT (bin,
3122 "running time now %" GST_TIME_FORMAT,
3123 GST_TIME_ARGS (running_time));
3125 GST_RTP_BIN_LOCK (rtpbin);
3127 /* when we reactivate, calculate the offsets so that all streams have
3128 * an output time that is at least as big as the running_time */
3131 if (running_time > rtpbin->buffer_start) {
3132 offset = running_time - rtpbin->buffer_start;
3133 if (offset >= rtpbin->latency_ns)
3134 offset -= rtpbin->latency_ns;
3140 /* pause all streams */
3142 for (sessions = rtpbin->sessions; sessions;
3143 sessions = g_slist_next (sessions)) {
3144 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3146 GST_RTP_SESSION_LOCK (session);
3147 for (streams = session->streams; streams;
3148 streams = g_slist_next (streams)) {
3149 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3150 GstElement *element = stream->buffer;
3153 g_signal_emit_by_name (element, "set-active", active, offset,
3157 g_object_get (element, "percent", &stream->percent, NULL);
3161 if (min_out_time == -1 || last_out < min_out_time)
3162 min_out_time = last_out;
3165 GST_DEBUG_OBJECT (bin,
3166 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
3167 GST_TIME_FORMAT ", percent %d", element, active,
3168 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
3171 GST_RTP_SESSION_UNLOCK (session);
3173 GST_DEBUG_OBJECT (bin,
3174 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
3176 /* the buffer_start is the min out time of all paused jitterbuffers */
3178 rtpbin->buffer_start = min_out_time;
3180 GST_RTP_BIN_UNLOCK (rtpbin);
3183 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3188 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3194 static GstStateChangeReturn
3195 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
3197 GstStateChangeReturn res;
3199 GstRtpBinPrivate *priv;
3201 rtpbin = GST_RTP_BIN (element);
3202 priv = rtpbin->priv;
3204 switch (transition) {
3205 case GST_STATE_CHANGE_NULL_TO_READY:
3207 case GST_STATE_CHANGE_READY_TO_PAUSED:
3208 priv->last_ntpnstime = 0;
3209 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
3210 g_atomic_int_set (&priv->shutdown, 0);
3212 case GST_STATE_CHANGE_PAUSED_TO_READY:
3213 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
3214 g_atomic_int_set (&priv->shutdown, 1);
3215 /* wait for all callbacks to end by taking the lock. No new callbacks will
3216 * be able to happen as we set the shutdown flag. */
3217 GST_RTP_BIN_DYN_LOCK (rtpbin);
3218 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
3219 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3225 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3227 switch (transition) {
3228 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3230 case GST_STATE_CHANGE_PAUSED_TO_READY:
3232 case GST_STATE_CHANGE_READY_TO_NULL:
3241 session_request_element (GstRtpBinSession * session, guint signal)
3243 GstElement *element = NULL;
3244 GstRtpBin *bin = session->bin;
3246 g_signal_emit (bin, gst_rtp_bin_signals[signal], 0, session->id, &element);
3249 if (!bin_manage_element (bin, element))
3251 session->elements = g_slist_prepend (session->elements, element);
3258 GST_WARNING_OBJECT (bin, "unable to manage element");
3259 gst_object_unref (element);
3265 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3267 GstPad *gpad = GST_PAD_CAST (user_data);
3269 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3270 gst_pad_store_sticky_event (gpad, *event);
3275 /* a new pad (SSRC) was created in @session. This signal is emitted from the
3276 * payload demuxer. */
3278 new_payload_found (GstElement * element, guint pt, GstPad * pad,
3279 GstRtpBinStream * stream)
3282 GstElementClass *klass;
3283 GstPadTemplate *templ;
3287 rtpbin = stream->bin;
3289 GST_DEBUG_OBJECT (rtpbin, "new payload pad %u", pt);
3291 pad = gst_object_ref (pad);
3293 if (stream->session->storage) {
3294 GstElement *fec_decoder =
3295 session_request_element (stream->session, SIGNAL_REQUEST_FEC_DECODER);
3298 GstPad *sinkpad, *srcpad;
3299 GstPadLinkReturn ret;
3301 sinkpad = gst_element_get_static_pad (fec_decoder, "sink");
3304 goto fec_decoder_sink_failed;
3306 ret = gst_pad_link (pad, sinkpad);
3307 gst_object_unref (sinkpad);
3309 if (ret != GST_PAD_LINK_OK)
3310 goto fec_decoder_link_failed;
3312 srcpad = gst_element_get_static_pad (fec_decoder, "src");
3315 goto fec_decoder_src_failed;
3317 gst_pad_sticky_events_foreach (pad, copy_sticky_events, srcpad);
3318 gst_object_unref (pad);
3323 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3325 /* ghost the pad to the parent */
3326 klass = GST_ELEMENT_GET_CLASS (rtpbin);
3327 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3328 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3329 stream->session->id, stream->ssrc, pt);
3330 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3332 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
3334 gst_pad_set_active (gpad, TRUE);
3335 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3337 gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3338 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3341 gst_object_unref (pad);
3347 GST_DEBUG ("ignoring, we are shutting down");
3350 fec_decoder_sink_failed:
3352 g_warning ("rtpbin: failed to get fec encoder sink pad for session %u",
3353 stream->session->id);
3356 fec_decoder_src_failed:
3358 g_warning ("rtpbin: failed to get fec encoder src pad for session %u",
3359 stream->session->id);
3362 fec_decoder_link_failed:
3364 g_warning ("rtpbin: failed to link fec decoder for session %u",
3365 stream->session->id);
3371 payload_pad_removed (GstElement * element, GstPad * pad,
3372 GstRtpBinStream * stream)
3377 rtpbin = stream->bin;
3379 GST_DEBUG ("payload pad removed");
3381 GST_RTP_BIN_DYN_LOCK (rtpbin);
3382 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
3383 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
3385 gst_pad_set_active (gpad, FALSE);
3386 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3388 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3392 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
3397 rtpbin = session->bin;
3399 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %u in session %u", pt,
3402 caps = get_pt_map (session, pt);
3411 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
3417 ptdemux_pt_map_requested (GstElement * element, guint pt,
3418 GstRtpBinSession * session)
3420 GstCaps *ret = pt_map_requested (element, pt, session);
3422 if (ret && gst_caps_get_size (ret) == 1) {
3423 const GstStructure *s = gst_caps_get_structure (ret, 0);
3426 if (gst_structure_get_boolean (s, "is-fec", &is_fec) && is_fec) {
3427 GValue v = G_VALUE_INIT;
3428 GValue v2 = G_VALUE_INIT;
3430 GST_INFO_OBJECT (session->bin, "Will ignore FEC pt %u in session %u", pt,
3432 g_value_init (&v, GST_TYPE_ARRAY);
3433 g_value_init (&v2, G_TYPE_INT);
3434 g_object_get_property (G_OBJECT (element), "ignored-payload-types", &v);
3435 g_value_set_int (&v2, pt);
3436 gst_value_array_append_value (&v, &v2);
3437 g_value_unset (&v2);
3438 g_object_set_property (G_OBJECT (element), "ignored-payload-types", &v);
3447 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
3449 GST_DEBUG_OBJECT (session->bin,
3450 "emiting signal for pt type changed to %u in session %u", pt,
3453 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
3454 0, session->id, pt);
3457 /* emitted when caps changed for the session */
3459 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
3464 const GstStructure *s;
3468 g_object_get (pad, "caps", &caps, NULL);
3473 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
3475 s = gst_caps_get_structure (caps, 0);
3477 /* get payload, finish when it's not there */
3478 if (!gst_structure_get_int (s, "payload", &payload)) {
3479 gst_caps_unref (caps);
3483 GST_RTP_SESSION_LOCK (session);
3484 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
3485 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
3486 GST_RTP_SESSION_UNLOCK (session);
3489 /* a new pad (SSRC) was created in @session */
3491 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
3492 GstRtpBinSession * session)
3495 GstRtpBinStream *stream;
3496 GstPad *sinkpad, *srcpad;
3499 rtpbin = session->bin;
3501 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
3502 GST_DEBUG_PAD_NAME (pad));
3504 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3506 GST_RTP_SESSION_LOCK (session);
3508 /* create new stream */
3509 stream = create_stream (session, ssrc);
3513 /* get pad and link */
3514 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
3515 padname = g_strdup_printf ("src_%u", ssrc);
3516 srcpad = gst_element_get_static_pad (element, padname);
3518 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
3519 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3520 gst_object_unref (sinkpad);
3521 gst_object_unref (srcpad);
3523 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
3524 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
3525 srcpad = gst_element_get_static_pad (element, padname);
3527 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
3528 gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3529 gst_object_unref (sinkpad);
3530 gst_object_unref (srcpad);
3532 /* connect to the RTCP sync signal from the jitterbuffer */
3533 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
3534 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
3535 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
3537 if (stream->demux) {
3538 /* connect to the new-pad signal of the payload demuxer, this will expose the
3539 * new pad by ghosting it. */
3540 stream->demux_newpad_sig = g_signal_connect (stream->demux,
3541 "new-payload-type", (GCallback) new_payload_found, stream);
3542 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
3543 "pad-removed", (GCallback) payload_pad_removed, stream);
3545 /* connect to the request-pt-map signal. This signal will be emitted by the
3546 * demuxer so that it can apply a proper caps on the buffers for the
3548 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
3549 "request-pt-map", (GCallback) ptdemux_pt_map_requested, session);
3550 /* connect to the signal so it can be forwarded. */
3551 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
3552 "payload-type-change", (GCallback) payload_type_change, session);
3554 /* add rtpjitterbuffer src pad to pads */
3555 GstElementClass *klass;
3556 GstPadTemplate *templ;
3560 pad = gst_element_get_static_pad (stream->buffer, "src");
3562 /* ghost the pad to the parent */
3563 klass = GST_ELEMENT_GET_CLASS (rtpbin);
3564 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3565 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3566 stream->session->id, stream->ssrc, 255);
3567 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3570 gst_pad_set_active (gpad, TRUE);
3571 gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3572 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3574 gst_object_unref (pad);
3577 GST_RTP_SESSION_UNLOCK (session);
3578 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3585 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
3590 GST_RTP_SESSION_UNLOCK (session);
3591 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3592 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
3598 complete_session_sink (GstRtpBin * rtpbin, GstRtpBinSession * session)
3600 guint sessid = session->id;
3601 GstPad *recv_rtp_sink;
3602 GstElement *decoder;
3604 g_assert (!session->recv_rtp_sink);
3606 /* get recv_rtp pad and store */
3607 session->recv_rtp_sink =
3608 gst_element_get_request_pad (session->session, "recv_rtp_sink");
3609 if (session->recv_rtp_sink == NULL)
3612 g_signal_connect (session->recv_rtp_sink, "notify::caps",
3613 (GCallback) caps_changed, session);
3615 GST_DEBUG_OBJECT (rtpbin, "requesting RTP decoder");
3616 decoder = session_request_element (session, SIGNAL_REQUEST_RTP_DECODER);
3618 GstPad *decsrc, *decsink;
3619 GstPadLinkReturn ret;
3621 GST_DEBUG_OBJECT (rtpbin, "linking RTP decoder");
3622 decsink = gst_element_get_static_pad (decoder, "rtp_sink");
3623 if (decsink == NULL)
3624 goto dec_sink_failed;
3626 recv_rtp_sink = decsink;
3628 decsrc = gst_element_get_static_pad (decoder, "rtp_src");
3630 goto dec_src_failed;
3632 ret = gst_pad_link (decsrc, session->recv_rtp_sink);
3634 gst_object_unref (decsrc);
3636 if (ret != GST_PAD_LINK_OK)
3637 goto dec_link_failed;
3640 GST_DEBUG_OBJECT (rtpbin, "no RTP decoder given");
3641 recv_rtp_sink = gst_object_ref (session->recv_rtp_sink);
3644 return recv_rtp_sink;
3649 g_warning ("rtpbin: failed to get session recv_rtp_sink pad");
3654 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
3659 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
3660 gst_object_unref (recv_rtp_sink);
3665 g_warning ("rtpbin: failed to link rtp decoder for session %u", sessid);
3666 gst_object_unref (recv_rtp_sink);
3672 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
3676 GstPad *recv_rtp_src;
3678 g_assert (!session->recv_rtp_src);
3680 session->recv_rtp_src =
3681 gst_element_get_static_pad (session->session, "recv_rtp_src");
3682 if (session->recv_rtp_src == NULL)
3685 /* find out if we need AUX elements */
3686 aux = session_request_element (session, SIGNAL_REQUEST_AUX_RECEIVER);
3690 GstPadLinkReturn ret;
3692 GST_DEBUG_OBJECT (rtpbin, "linking AUX receiver");
3694 pname = g_strdup_printf ("sink_%u", sessid);
3695 auxsink = gst_element_get_static_pad (aux, pname);
3697 if (auxsink == NULL)
3698 goto aux_sink_failed;
3700 ret = gst_pad_link (session->recv_rtp_src, auxsink);
3701 gst_object_unref (auxsink);
3702 if (ret != GST_PAD_LINK_OK)
3703 goto aux_link_failed;
3705 /* this can be NULL when this AUX element is not to be linked any further */
3706 pname = g_strdup_printf ("src_%u", sessid);
3707 recv_rtp_src = gst_element_get_static_pad (aux, pname);
3710 recv_rtp_src = gst_object_ref (session->recv_rtp_src);
3713 /* Add a storage element if needed */
3714 if (recv_rtp_src && session->storage) {
3715 GstPadLinkReturn ret;
3716 GstPad *sinkpad = gst_element_get_static_pad (session->storage, "sink");
3718 ret = gst_pad_link (recv_rtp_src, sinkpad);
3720 gst_object_unref (sinkpad);
3721 gst_object_unref (recv_rtp_src);
3723 if (ret != GST_PAD_LINK_OK)
3724 goto storage_link_failed;
3726 recv_rtp_src = gst_element_get_static_pad (session->storage, "src");
3732 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
3733 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
3734 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
3735 gst_pad_link_full (recv_rtp_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
3736 gst_object_unref (sinkdpad);
3737 gst_object_unref (recv_rtp_src);
3739 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
3740 session->demux_newpad_sig = g_signal_connect (session->demux,
3741 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
3742 session->demux_padremoved_sig = g_signal_connect (session->demux,
3743 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
3750 g_warning ("rtpbin: failed to get session recv_rtp_src pad");
3755 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
3760 g_warning ("rtpbin: failed to link AUX pad to session %u", sessid);
3763 storage_link_failed:
3765 g_warning ("rtpbin: failed to link storage");
3770 /* Create a pad for receiving RTP for the session in @name. Must be called with
3774 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
3777 GstRtpBinSession *session;
3778 GstPad *recv_rtp_sink;
3780 /* first get the session number */
3781 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
3784 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
3786 /* get or create session */
3787 session = find_session_by_id (rtpbin, sessid);
3789 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
3790 /* create session now */
3791 session = create_session (rtpbin, sessid);
3792 if (session == NULL)
3796 /* check if pad was requested */
3797 if (session->recv_rtp_sink_ghost != NULL)
3798 return session->recv_rtp_sink_ghost;
3800 /* setup the session sink pad */
3801 recv_rtp_sink = complete_session_sink (rtpbin, session);
3803 goto session_sink_failed;
3805 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
3806 session->recv_rtp_sink_ghost =
3807 gst_ghost_pad_new_from_template (name, recv_rtp_sink, templ);
3808 gst_object_unref (recv_rtp_sink);
3809 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
3810 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
3812 complete_session_receiver (rtpbin, session, sessid);
3814 return session->recv_rtp_sink_ghost;
3819 g_warning ("rtpbin: invalid name given");
3824 /* create_session already warned */
3827 session_sink_failed:
3829 /* warning already done */
3835 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
3837 if (session->demux_newpad_sig) {
3838 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
3839 session->demux_newpad_sig = 0;
3841 if (session->demux_padremoved_sig) {
3842 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
3843 session->demux_padremoved_sig = 0;
3845 if (session->recv_rtp_src) {
3846 gst_object_unref (session->recv_rtp_src);
3847 session->recv_rtp_src = NULL;
3849 if (session->recv_rtp_sink) {
3850 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
3851 gst_object_unref (session->recv_rtp_sink);
3852 session->recv_rtp_sink = NULL;
3854 if (session->recv_rtp_sink_ghost) {
3855 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
3856 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
3857 session->recv_rtp_sink_ghost);
3858 session->recv_rtp_sink_ghost = NULL;
3863 complete_session_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session,
3866 GstElement *decoder;
3868 GstPad *decsink = NULL;
3870 /* get recv_rtp pad and store */
3871 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
3872 session->recv_rtcp_sink =
3873 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
3874 if (session->recv_rtcp_sink == NULL)
3877 GST_DEBUG_OBJECT (rtpbin, "getting RTCP decoder");
3878 decoder = session_request_element (session, SIGNAL_REQUEST_RTCP_DECODER);
3881 GstPadLinkReturn ret;
3883 GST_DEBUG_OBJECT (rtpbin, "linking RTCP decoder");
3884 decsink = gst_element_get_static_pad (decoder, "rtcp_sink");
3885 decsrc = gst_element_get_static_pad (decoder, "rtcp_src");
3887 if (decsink == NULL)
3888 goto dec_sink_failed;
3891 goto dec_src_failed;
3893 ret = gst_pad_link (decsrc, session->recv_rtcp_sink);
3895 gst_object_unref (decsrc);
3897 if (ret != GST_PAD_LINK_OK)
3898 goto dec_link_failed;
3900 GST_DEBUG_OBJECT (rtpbin, "no RTCP decoder given");
3901 decsink = gst_object_ref (session->recv_rtcp_sink);
3904 /* get srcpad, link to SSRCDemux */
3905 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
3906 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
3907 if (session->sync_src == NULL)
3908 goto src_pad_failed;
3910 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
3911 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
3912 gst_pad_link_full (session->sync_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
3913 gst_object_unref (sinkdpad);
3919 g_warning ("rtpbin: failed to get session rtcp_sink pad");
3924 g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
3929 g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
3934 g_warning ("rtpbin: failed to link rtcp decoder for session %u", sessid);
3939 g_warning ("rtpbin: failed to get session sync_src pad");
3943 gst_object_unref (decsink);
3947 /* Create a pad for receiving RTCP for the session in @name. Must be called with
3951 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
3955 GstRtpBinSession *session;
3956 GstPad *decsink = NULL;
3958 /* first get the session number */
3959 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
3962 GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
3964 /* get or create the session */
3965 session = find_session_by_id (rtpbin, sessid);
3967 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
3968 /* create session now */
3969 session = create_session (rtpbin, sessid);
3970 if (session == NULL)
3974 /* check if pad was requested */
3975 if (session->recv_rtcp_sink_ghost != NULL)
3976 return session->recv_rtcp_sink_ghost;
3978 decsink = complete_session_rtcp (rtpbin, session, sessid);
3982 session->recv_rtcp_sink_ghost =
3983 gst_ghost_pad_new_from_template (name, decsink, templ);
3984 gst_object_unref (decsink);
3985 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
3986 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
3987 session->recv_rtcp_sink_ghost);
3989 return session->recv_rtcp_sink_ghost;
3994 g_warning ("rtpbin: invalid name given");
3999 /* create_session already warned */
4005 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4007 if (session->recv_rtcp_sink_ghost) {
4008 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
4009 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4010 session->recv_rtcp_sink_ghost);
4011 session->recv_rtcp_sink_ghost = NULL;
4013 if (session->sync_src) {
4014 /* releasing the request pad should also unref the sync pad */
4015 gst_object_unref (session->sync_src);
4016 session->sync_src = NULL;
4018 if (session->recv_rtcp_sink) {
4019 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
4020 gst_object_unref (session->recv_rtcp_sink);
4021 session->recv_rtcp_sink = NULL;
4026 complete_session_src (GstRtpBin * rtpbin, GstRtpBinSession * session)
4029 guint sessid = session->id;
4030 GstPad *send_rtp_src;
4031 GstElement *encoder;
4032 GstElementClass *klass;
4033 GstPadTemplate *templ;
4034 gboolean ret = FALSE;
4037 send_rtp_src = gst_element_get_static_pad (session->session, "send_rtp_src");
4039 if (send_rtp_src == NULL)
4042 GST_DEBUG_OBJECT (rtpbin, "getting RTP encoder");
4043 encoder = session_request_element (session, SIGNAL_REQUEST_RTP_ENCODER);
4046 GstPad *encsrc, *encsink;
4047 GstPadLinkReturn ret;
4049 GST_DEBUG_OBJECT (rtpbin, "linking RTP encoder");
4050 ename = g_strdup_printf ("rtp_src_%u", sessid);
4051 encsrc = gst_element_get_static_pad (encoder, ename);
4055 goto enc_src_failed;
4057 ename = g_strdup_printf ("rtp_sink_%u", sessid);
4058 encsink = gst_element_get_static_pad (encoder, ename);
4060 if (encsink == NULL)
4061 goto enc_sink_failed;
4063 ret = gst_pad_link (send_rtp_src, encsink);
4064 gst_object_unref (encsink);
4065 gst_object_unref (send_rtp_src);
4067 send_rtp_src = encsrc;
4069 if (ret != GST_PAD_LINK_OK)
4070 goto enc_link_failed;
4072 GST_DEBUG_OBJECT (rtpbin, "no RTP encoder given");
4075 /* ghost the new source pad */
4076 klass = GST_ELEMENT_GET_CLASS (rtpbin);
4077 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
4078 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
4079 session->send_rtp_src_ghost =
4080 gst_ghost_pad_new_from_template (gname, send_rtp_src, templ);
4081 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
4082 gst_pad_sticky_events_foreach (send_rtp_src, copy_sticky_events,
4083 session->send_rtp_src_ghost);
4084 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
4091 gst_object_unref (send_rtp_src);
4098 g_warning ("rtpbin: failed to get rtp source pad for session %u", sessid);
4103 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4104 " src pad for session %u", encoder, sessid);
4109 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4110 " sink pad for session %u", encoder, sessid);
4115 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4122 setup_aux_sender_fold (const GValue * item, GValue * result, gpointer user_data)
4127 GstRtpBinSession *session = user_data, *newsess;
4128 GstRtpBin *rtpbin = session->bin;
4129 GstPadLinkReturn ret;
4131 pad = g_value_get_object (item);
4132 name = gst_pad_get_name (pad);
4134 if (name == NULL || sscanf (name, "src_%u", &sessid) != 1)
4139 newsess = find_session_by_id (rtpbin, sessid);
4140 if (newsess == NULL) {
4141 /* create new session */
4142 newsess = create_session (rtpbin, sessid);
4143 if (newsess == NULL)
4145 } else if (newsess->send_rtp_sink != NULL)
4146 goto existing_session;
4148 /* get send_rtp pad and store */
4149 newsess->send_rtp_sink =
4150 gst_element_get_request_pad (newsess->session, "send_rtp_sink");
4151 if (newsess->send_rtp_sink == NULL)
4154 ret = gst_pad_link (pad, newsess->send_rtp_sink);
4155 if (ret != GST_PAD_LINK_OK)
4156 goto aux_link_failed;
4158 if (!complete_session_src (rtpbin, newsess))
4159 goto session_src_failed;
4166 GST_WARNING ("ignoring invalid pad name %s", GST_STR_NULL (name));
4172 /* create_session already warned */
4177 g_warning ("rtpbin: session %u is already a sender", sessid);
4182 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4187 g_warning ("rtpbin: failed to link AUX for session %u", sessid);
4192 g_warning ("rtpbin: failed to complete AUX for session %u", sessid);
4198 setup_aux_sender (GstRtpBin * rtpbin, GstRtpBinSession * session,
4202 GValue result = { 0, };
4203 GstIteratorResult res;
4205 it = gst_element_iterate_src_pads (aux);
4206 res = gst_iterator_fold (it, setup_aux_sender_fold, &result, session);
4207 gst_iterator_free (it);
4209 return res == GST_ITERATOR_DONE;
4212 /* Create a pad for sending RTP for the session in @name. Must be called with
4216 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
4220 GstPad *send_rtp_sink;
4222 GstElement *encoder;
4223 GstElement *prev = NULL;
4224 GstRtpBinSession *session;
4226 /* first get the session number */
4227 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
4230 /* get or create session */
4231 session = find_session_by_id (rtpbin, sessid);
4233 /* create session now */
4234 session = create_session (rtpbin, sessid);
4235 if (session == NULL)
4239 /* check if pad was requested */
4240 if (session->send_rtp_sink_ghost != NULL)
4241 return session->send_rtp_sink_ghost;
4243 /* check if we are already using this session as a sender */
4244 if (session->send_rtp_sink != NULL)
4245 goto existing_session;
4247 encoder = session_request_element (session, SIGNAL_REQUEST_FEC_ENCODER);
4250 GST_DEBUG_OBJECT (rtpbin, "Linking FEC encoder");
4252 send_rtp_sink = gst_element_get_static_pad (encoder, "sink");
4255 goto enc_sink_failed;
4260 GST_DEBUG_OBJECT (rtpbin, "getting RTP AUX sender");
4261 aux = session_request_element (session, SIGNAL_REQUEST_AUX_SENDER);
4264 GST_DEBUG_OBJECT (rtpbin, "linking AUX sender");
4265 if (!setup_aux_sender (rtpbin, session, aux))
4266 goto aux_session_failed;
4268 pname = g_strdup_printf ("sink_%u", sessid);
4269 sinkpad = gst_element_get_static_pad (aux, pname);
4272 if (sinkpad == NULL)
4273 goto aux_sink_failed;
4276 send_rtp_sink = sinkpad;
4278 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4279 GstPadLinkReturn ret;
4281 ret = gst_pad_link (srcpad, sinkpad);
4282 gst_object_unref (srcpad);
4283 if (ret != GST_PAD_LINK_OK) {
4284 goto aux_link_failed;
4289 /* get send_rtp pad and store */
4290 session->send_rtp_sink =
4291 gst_element_get_request_pad (session->session, "send_rtp_sink");
4292 if (session->send_rtp_sink == NULL)
4295 if (!complete_session_src (rtpbin, session))
4296 goto session_src_failed;
4299 send_rtp_sink = gst_object_ref (session->send_rtp_sink);
4301 GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4302 GstPadLinkReturn ret;
4304 ret = gst_pad_link (srcpad, session->send_rtp_sink);
4305 gst_object_unref (srcpad);
4306 if (ret != GST_PAD_LINK_OK)
4307 goto session_link_failed;
4311 session->send_rtp_sink_ghost =
4312 gst_ghost_pad_new_from_template (name, send_rtp_sink, templ);
4313 gst_object_unref (send_rtp_sink);
4314 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
4315 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
4317 return session->send_rtp_sink_ghost;
4322 g_warning ("rtpbin: invalid name given");
4327 /* create_session already warned */
4332 g_warning ("rtpbin: session %u is already in use", sessid);
4337 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4342 g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4347 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4353 g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4358 g_warning ("rtpbin: failed to setup source pads for session %u", sessid);
4361 session_link_failed:
4363 g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4369 g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4370 " sink pad for session %u", encoder, sessid);
4376 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4378 if (session->send_rtp_src_ghost) {
4379 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
4380 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4381 session->send_rtp_src_ghost);
4382 session->send_rtp_src_ghost = NULL;
4384 if (session->send_rtp_sink) {
4385 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
4386 session->send_rtp_sink);
4387 gst_object_unref (session->send_rtp_sink);
4388 session->send_rtp_sink = NULL;
4390 if (session->send_rtp_sink_ghost) {
4391 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
4392 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4393 session->send_rtp_sink_ghost);
4394 session->send_rtp_sink_ghost = NULL;
4398 /* Create a pad for sending RTCP for the session in @name. Must be called with
4402 create_send_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
4407 GstElement *encoder;
4408 GstRtpBinSession *session;
4410 /* first get the session number */
4411 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
4414 /* get or create session */
4415 session = find_session_by_id (rtpbin, sessid);
4417 GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4418 /* create session now */
4419 session = create_session (rtpbin, sessid);
4420 if (session == NULL)
4424 /* check if pad was requested */
4425 if (session->send_rtcp_src_ghost != NULL)
4426 return session->send_rtcp_src_ghost;
4428 /* get rtcp_src pad and store */
4429 session->send_rtcp_src =
4430 gst_element_get_request_pad (session->session, "send_rtcp_src");
4431 if (session->send_rtcp_src == NULL)
4434 GST_DEBUG_OBJECT (rtpbin, "getting RTCP encoder");
4435 encoder = session_request_element (session, SIGNAL_REQUEST_RTCP_ENCODER);
4439 GstPadLinkReturn ret;
4441 GST_DEBUG_OBJECT (rtpbin, "linking RTCP encoder");
4443 ename = g_strdup_printf ("rtcp_src_%u", sessid);
4444 encsrc = gst_element_get_static_pad (encoder, ename);
4447 goto enc_src_failed;
4449 ename = g_strdup_printf ("rtcp_sink_%u", sessid);
4450 encsink = gst_element_get_static_pad (encoder, ename);
4452 if (encsink == NULL)
4453 goto enc_sink_failed;
4455 ret = gst_pad_link (session->send_rtcp_src, encsink);
4456 gst_object_unref (encsink);
4458 if (ret != GST_PAD_LINK_OK)
4459 goto enc_link_failed;
4461 GST_DEBUG_OBJECT (rtpbin, "no RTCP encoder given");
4462 encsrc = gst_object_ref (session->send_rtcp_src);
4465 session->send_rtcp_src_ghost =
4466 gst_ghost_pad_new_from_template (name, encsrc, templ);
4467 gst_object_unref (encsrc);
4468 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
4469 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
4471 return session->send_rtcp_src_ghost;
4476 g_warning ("rtpbin: invalid name given");
4481 /* create_session already warned */
4486 g_warning ("rtpbin: failed to get rtcp pad for session %u", sessid);
4491 g_warning ("rtpbin: failed to get encoder src pad for session %u", sessid);
4496 g_warning ("rtpbin: failed to get encoder sink pad for session %u", sessid);
4497 gst_object_unref (encsrc);
4502 g_warning ("rtpbin: failed to link rtcp encoder for session %u", sessid);
4503 gst_object_unref (encsrc);
4509 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4511 if (session->send_rtcp_src_ghost) {
4512 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
4513 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4514 session->send_rtcp_src_ghost);
4515 session->send_rtcp_src_ghost = NULL;
4517 if (session->send_rtcp_src) {
4518 gst_element_release_request_pad (session->session, session->send_rtcp_src);
4519 gst_object_unref (session->send_rtcp_src);
4520 session->send_rtcp_src = NULL;
4524 /* If the requested name is NULL we should create a name with
4525 * the session number assuming we want the lowest posible session
4526 * with a free pad like the template */
4528 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
4530 gboolean name_found = FALSE;
4532 GstIterator *pad_it = NULL;
4533 gchar *pad_name = NULL;
4534 GValue data = { 0, };
4536 GST_DEBUG_OBJECT (element, "find a free pad name for template");
4537 while (!name_found) {
4538 gboolean done = FALSE;
4541 pad_name = g_strdup_printf (templ->name_template, session++);
4542 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
4545 switch (gst_iterator_next (pad_it, &data)) {
4546 case GST_ITERATOR_OK:
4551 pad = g_value_get_object (&data);
4552 name = gst_pad_get_name (pad);
4554 if (strcmp (name, pad_name) == 0) {
4559 g_value_reset (&data);
4562 case GST_ITERATOR_ERROR:
4563 case GST_ITERATOR_RESYNC:
4564 /* restart iteration */
4569 case GST_ITERATOR_DONE:
4574 g_value_unset (&data);
4575 gst_iterator_free (pad_it);
4578 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
4585 gst_rtp_bin_request_new_pad (GstElement * element,
4586 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
4589 GstElementClass *klass;
4592 gchar *pad_name = NULL;
4594 g_return_val_if_fail (templ != NULL, NULL);
4595 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
4597 rtpbin = GST_RTP_BIN (element);
4598 klass = GST_ELEMENT_GET_CLASS (element);
4600 GST_RTP_BIN_LOCK (rtpbin);
4603 /* use a free pad name */
4604 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
4606 /* use the provided name */
4607 pad_name = g_strdup (name);
4610 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
4612 /* figure out the template */
4613 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
4614 result = create_recv_rtp (rtpbin, templ, pad_name);
4615 } else if (templ == gst_element_class_get_pad_template (klass,
4616 "recv_rtcp_sink_%u")) {
4617 result = create_recv_rtcp (rtpbin, templ, pad_name);
4618 } else if (templ == gst_element_class_get_pad_template (klass,
4619 "send_rtp_sink_%u")) {
4620 result = create_send_rtp (rtpbin, templ, pad_name);
4621 } else if (templ == gst_element_class_get_pad_template (klass,
4622 "send_rtcp_src_%u")) {
4623 result = create_send_rtcp (rtpbin, templ, pad_name);
4625 goto wrong_template;
4628 GST_RTP_BIN_UNLOCK (rtpbin);
4636 GST_RTP_BIN_UNLOCK (rtpbin);
4637 g_warning ("rtpbin: this is not our template");
4643 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
4645 GstRtpBinSession *session;
4648 g_return_if_fail (GST_IS_GHOST_PAD (pad));
4649 g_return_if_fail (GST_IS_RTP_BIN (element));
4651 rtpbin = GST_RTP_BIN (element);
4653 GST_RTP_BIN_LOCK (rtpbin);
4654 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
4655 GST_DEBUG_PAD_NAME (pad));
4657 if (!(session = find_session_by_pad (rtpbin, pad)))
4660 if (session->recv_rtp_sink_ghost == pad) {
4661 remove_recv_rtp (rtpbin, session);
4662 } else if (session->recv_rtcp_sink_ghost == pad) {
4663 remove_recv_rtcp (rtpbin, session);
4664 } else if (session->send_rtp_sink_ghost == pad) {
4665 remove_send_rtp (rtpbin, session);
4666 } else if (session->send_rtcp_src_ghost == pad) {
4667 remove_rtcp (rtpbin, session);
4670 /* no more request pads, free the complete session */
4671 if (session->recv_rtp_sink_ghost == NULL
4672 && session->recv_rtcp_sink_ghost == NULL
4673 && session->send_rtp_sink_ghost == NULL
4674 && session->send_rtcp_src_ghost == NULL) {
4675 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
4676 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
4677 free_session (session, rtpbin);
4679 GST_RTP_BIN_UNLOCK (rtpbin);
4686 GST_RTP_BIN_UNLOCK (rtpbin);
4687 g_warning ("rtpbin: %s:%s is not one of our request pads",
4688 GST_DEBUG_PAD_NAME (pad));