2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-gstrtpbin
22 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
24 * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25 * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26 * RTP sessions that will be synchronized together using RTCP SR packets.
28 * #GstRtpBin is configured with a number of request pads that define the
29 * functionality that is activated, similar to the #GstRtpSession element.
31 * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34 * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35 * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36 * the packets are released from the jitterbuffer, they will be forwarded to a
37 * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38 * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39 * gstrtpbin with the session number, SSRC and payload type respectively as the pad
42 * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43 * session number must be specified in the pad name.
45 * If you want the session manager to generate and send RTCP packets, request
46 * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47 * on this pad contain SR/RR RTCP reports that should be sent to all participants
50 * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51 * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52 * the pad from the lowest available session will be returned. The session manager will modify the
53 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54 * send_rtp_src_\%u pad after updating its internal state.
56 * The session manager needs the clock-rate of the payload types it is handling
57 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
61 * Access to the internal statistics of gstrtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
67 * <title>Example pipelines</title>
69 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
70 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
71 * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
73 * gst-launch gstrtpbin name=rtpbin \
74 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
75 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
76 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
77 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
78 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
79 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
80 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
81 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
82 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
83 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
84 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
85 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
86 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
87 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
88 * is received on port 5007. Since RTCP packets from the sender should be sent
89 * as soon as possible and do not participate in preroll, sync=false and
90 * async=false is configured on udpsink
92 * gst-launch -v gstrtpbin name=rtpbin \
93 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
94 * port=5000 ! rtpbin.recv_rtp_sink_0 \
95 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
96 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
97 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
98 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
99 * port=5002 ! rtpbin.recv_rtp_sink_1 \
100 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
101 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
103 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
104 * decode and display the video.
105 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
106 * decode and play the audio.
107 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
108 * session 1 on port 5003. These packets will be used for session management and
110 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
114 * Last reviewed on 2007-08-30 (0.10.6)
123 #include <gst/rtp/gstrtpbuffer.h>
124 #include <gst/rtp/gstrtcpbuffer.h>
126 #include "gstrtpbin-marshal.h"
127 #include "gstrtpbin.h"
128 #include "rtpsession.h"
129 #include "gstrtpsession.h"
130 #include "gstrtpjitterbuffer.h"
132 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
133 #define GST_CAT_DEFAULT gst_rtp_bin_debug
136 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
137 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
140 GST_STATIC_CAPS ("application/x-rtp")
143 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
144 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
147 GST_STATIC_CAPS ("application/x-rtcp")
150 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
154 GST_STATIC_CAPS ("application/x-rtp")
158 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
159 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
162 GST_STATIC_CAPS ("application/x-rtp")
165 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
166 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
169 GST_STATIC_CAPS ("application/x-rtcp")
172 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
176 GST_STATIC_CAPS ("application/x-rtp")
179 #define GST_RTP_BIN_GET_PRIVATE(obj) \
180 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
182 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
183 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
185 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
186 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock ((bin)->priv->dyn_lock)
187 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->dyn_lock)
189 /* lock for shutdown */
190 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
192 if (g_atomic_int_get (&bin->priv->shutdown)) \
194 GST_RTP_BIN_DYN_LOCK (bin); \
195 if (g_atomic_int_get (&bin->priv->shutdown)) { \
196 GST_RTP_BIN_DYN_UNLOCK (bin); \
201 /* unlock for shutdown */
202 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
203 GST_RTP_BIN_DYN_UNLOCK (bin); \
205 struct _GstRtpBinPrivate
209 /* lock protecting dynamic adding/removing */
212 /* if we are shutting down or not */
217 /* UNIX (ntp) time of last SR sync used */
221 /* signals and args */
224 SIGNAL_REQUEST_PT_MAP,
225 SIGNAL_PAYLOAD_TYPE_CHANGE,
228 SIGNAL_GET_INTERNAL_SESSION,
231 SIGNAL_ON_SSRC_COLLISION,
232 SIGNAL_ON_SSRC_VALIDATED,
233 SIGNAL_ON_SSRC_ACTIVE,
236 SIGNAL_ON_BYE_TIMEOUT,
238 SIGNAL_ON_SENDER_TIMEOUT,
243 #define DEFAULT_LATENCY_MS 200
244 #define DEFAULT_SDES NULL
245 #define DEFAULT_DO_LOST FALSE
246 #define DEFAULT_IGNORE_PT FALSE
247 #define DEFAULT_NTP_SYNC FALSE
248 #define DEFAULT_AUTOREMOVE FALSE
249 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
250 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
251 #define DEFAULT_RTCP_SYNC GST_RTP_BIN_RTCP_SYNC_ALWAYS
252 #define DEFAULT_RTCP_SYNC_INTERVAL 0
263 PROP_RTCP_SYNC_INTERVAL,
266 PROP_USE_PIPELINE_CLOCK,
272 GST_RTP_BIN_RTCP_SYNC_ALWAYS,
273 GST_RTP_BIN_RTCP_SYNC_INITIAL,
274 GST_RTP_BIN_RTCP_SYNC_RTP
277 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
279 gst_rtp_bin_rtcp_sync_get_type (void)
281 static GType rtcp_sync_type = 0;
282 static const GEnumValue rtcp_sync_types[] = {
283 {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
284 {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
285 {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
289 if (!rtcp_sync_type) {
290 rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
292 return rtcp_sync_type;
296 typedef struct _GstRtpBinSession GstRtpBinSession;
297 typedef struct _GstRtpBinStream GstRtpBinStream;
298 typedef struct _GstRtpBinClient GstRtpBinClient;
300 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
302 static GstCaps *pt_map_requested (GstElement * element, guint pt,
303 GstRtpBinSession * session);
304 static void payload_type_change (GstElement * element, guint pt,
305 GstRtpBinSession * session);
306 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
307 static void free_stream (GstRtpBinStream * stream);
309 /* Manages the RTP stream for one SSRC.
311 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
312 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
313 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
314 * together (see below).
316 struct _GstRtpBinStream
318 /* the SSRC of this stream */
324 /* the session this SSRC belongs to */
325 GstRtpBinSession *session;
327 /* the jitterbuffer of the SSRC */
329 gulong buffer_handlesync_sig;
330 gulong buffer_ptreq_sig;
331 gulong buffer_ntpstop_sig;
334 /* the PT demuxer of the SSRC */
336 gulong demux_newpad_sig;
337 gulong demux_padremoved_sig;
338 gulong demux_ptreq_sig;
339 gulong demux_ptchange_sig;
341 /* if we have calculated a valid rt_delta for this stream */
343 /* mapping to local RTP and NTP time */
346 /* base rtptime in gst time */
350 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
351 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
353 /* Manages the receiving end of the packets.
355 * There is one such structure for each RTP session (audio/video/...).
356 * We get the RTP/RTCP packets and stuff them into the session manager. From
357 * there they are pushed into an SSRC demuxer that splits the stream based on
358 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
359 * the GstRtpBinStream above).
361 struct _GstRtpBinSession
367 /* the session element */
369 /* the SSRC demuxer */
371 gulong demux_newpad_sig;
372 gulong demux_padremoved_sig;
376 /* list of GstRtpBinStream */
379 /* mapping of payload type to caps */
382 /* the pads of the session */
383 GstPad *recv_rtp_sink;
384 GstPad *recv_rtp_sink_ghost;
385 GstPad *recv_rtp_src;
386 GstPad *recv_rtcp_sink;
387 GstPad *recv_rtcp_sink_ghost;
389 GstPad *send_rtp_sink;
390 GstPad *send_rtp_sink_ghost;
391 GstPad *send_rtp_src;
392 GstPad *send_rtp_src_ghost;
393 GstPad *send_rtcp_src;
394 GstPad *send_rtcp_src_ghost;
397 /* Manages the RTP streams that come from one client and should therefore be
400 struct _GstRtpBinClient
402 /* the common CNAME for the streams */
411 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
412 static GstRtpBinSession *
413 find_session_by_id (GstRtpBin * rtpbin, gint id)
417 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
418 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
426 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
427 static GstRtpBinSession *
428 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
432 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
433 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
435 if ((sess->recv_rtp_sink_ghost == pad) ||
436 (sess->recv_rtcp_sink_ghost == pad) ||
437 (sess->send_rtp_sink_ghost == pad)
438 || (sess->send_rtcp_src_ghost == pad))
445 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
447 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
452 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
454 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
459 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
461 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
466 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
468 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
473 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
475 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
480 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
482 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
487 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
489 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
492 if (sess->bin->priv->autoremove)
493 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
497 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
499 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
502 if (sess->bin->priv->autoremove)
503 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
507 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
509 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
514 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
516 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
517 stream->session->id, stream->ssrc);
520 /* must be called with the SESSION lock */
521 static GstRtpBinStream *
522 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
526 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
527 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
529 if (stream->ssrc == ssrc)
536 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
537 GstRtpBinSession * session)
539 GstRtpBinStream *stream = NULL;
541 GST_RTP_SESSION_LOCK (session);
542 if ((stream = find_stream_by_ssrc (session, ssrc)))
543 session->streams = g_slist_remove (session->streams, stream);
544 GST_RTP_SESSION_UNLOCK (session);
547 free_stream (stream);
550 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
551 static GstRtpBinSession *
552 create_session (GstRtpBin * rtpbin, gint id)
554 GstRtpBinSession *sess;
555 GstElement *session, *demux;
558 if (!(session = gst_element_factory_make ("rtpsession", NULL)))
561 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
564 sess = g_new0 (GstRtpBinSession, 1);
565 sess->lock = g_mutex_new ();
568 sess->session = session;
570 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
571 (GDestroyNotify) gst_caps_unref);
572 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
574 /* configure SDES items */
575 GST_OBJECT_LOCK (rtpbin);
576 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
577 rtpbin->use_pipeline_clock, NULL);
578 GST_OBJECT_UNLOCK (rtpbin);
580 /* provide clock_rate to the session manager when needed */
581 g_signal_connect (session, "request-pt-map",
582 (GCallback) pt_map_requested, sess);
584 g_signal_connect (sess->session, "on-new-ssrc",
585 (GCallback) on_new_ssrc, sess);
586 g_signal_connect (sess->session, "on-ssrc-collision",
587 (GCallback) on_ssrc_collision, sess);
588 g_signal_connect (sess->session, "on-ssrc-validated",
589 (GCallback) on_ssrc_validated, sess);
590 g_signal_connect (sess->session, "on-ssrc-active",
591 (GCallback) on_ssrc_active, sess);
592 g_signal_connect (sess->session, "on-ssrc-sdes",
593 (GCallback) on_ssrc_sdes, sess);
594 g_signal_connect (sess->session, "on-bye-ssrc",
595 (GCallback) on_bye_ssrc, sess);
596 g_signal_connect (sess->session, "on-bye-timeout",
597 (GCallback) on_bye_timeout, sess);
598 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
599 g_signal_connect (sess->session, "on-sender-timeout",
600 (GCallback) on_sender_timeout, sess);
602 gst_bin_add (GST_BIN_CAST (rtpbin), session);
603 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
605 GST_OBJECT_LOCK (rtpbin);
606 target = GST_STATE_TARGET (rtpbin);
607 GST_OBJECT_UNLOCK (rtpbin);
609 /* change state only to what's needed */
610 gst_element_set_state (demux, target);
611 gst_element_set_state (session, target);
618 g_warning ("rtpbin: could not create gstrtpsession element");
623 gst_object_unref (session);
624 g_warning ("rtpbin: could not create gstrtpssrcdemux element");
630 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
634 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
636 gst_element_set_locked_state (sess->demux, TRUE);
637 gst_element_set_locked_state (sess->session, TRUE);
639 gst_element_set_state (sess->demux, GST_STATE_NULL);
640 gst_element_set_state (sess->session, GST_STATE_NULL);
642 if (sess->recv_rtp_sink != NULL) {
643 gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
644 gst_object_unref (sess->recv_rtp_sink);
646 if (sess->recv_rtp_src != NULL)
647 gst_object_unref (sess->recv_rtp_src);
648 if (sess->recv_rtcp_sink != NULL) {
649 gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
650 gst_object_unref (sess->recv_rtcp_sink);
652 if (sess->sync_src != NULL)
653 gst_object_unref (sess->sync_src);
654 if (sess->send_rtp_sink != NULL) {
655 gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
656 gst_object_unref (sess->send_rtp_sink);
658 if (sess->send_rtp_src != NULL)
659 gst_object_unref (sess->send_rtp_src);
660 if (sess->send_rtcp_src != NULL) {
661 gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
662 gst_object_unref (sess->send_rtcp_src);
665 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
666 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
668 /* remove any references in bin->clients to the streams in sess->streams */
669 client_walk = bin->clients;
670 while (client_walk) {
671 GSList *client_node = client_walk;
672 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
673 GSList *stream_walk = client->streams;
675 while (stream_walk) {
676 GSList *stream_node = stream_walk;
677 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
680 stream_walk = g_slist_next (stream_walk);
682 for (inner_walk = sess->streams; inner_walk;
683 inner_walk = g_slist_next (inner_walk)) {
684 if ((GstRtpBinStream *) inner_walk->data == stream) {
685 client->streams = g_slist_delete_link (client->streams, stream_node);
691 client_walk = g_slist_next (client_walk);
693 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
694 && client->streams == 0));
695 if (client->nstreams == 0) {
696 free_client (client, bin);
697 bin->clients = g_slist_delete_link (bin->clients, client_node);
701 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
702 g_slist_free (sess->streams);
704 g_mutex_free (sess->lock);
705 g_hash_table_destroy (sess->ptmap);
710 /* get the payload type caps for the specific payload @pt in @session */
712 get_pt_map (GstRtpBinSession * session, guint pt)
714 GstCaps *caps = NULL;
717 GValue args[3] = { {0}, {0}, {0} };
719 GST_DEBUG ("searching pt %d in cache", pt);
721 GST_RTP_SESSION_LOCK (session);
723 /* first look in the cache */
724 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
732 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
734 /* not in cache, send signal to request caps */
735 g_value_init (&args[0], GST_TYPE_ELEMENT);
736 g_value_set_object (&args[0], bin);
737 g_value_init (&args[1], G_TYPE_UINT);
738 g_value_set_uint (&args[1], session->id);
739 g_value_init (&args[2], G_TYPE_UINT);
740 g_value_set_uint (&args[2], pt);
742 g_value_init (&ret, GST_TYPE_CAPS);
743 g_value_set_boxed (&ret, NULL);
745 GST_RTP_SESSION_UNLOCK (session);
747 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
749 GST_RTP_SESSION_LOCK (session);
751 g_value_unset (&args[0]);
752 g_value_unset (&args[1]);
753 g_value_unset (&args[2]);
755 /* look in the cache again because we let the lock go */
756 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
759 g_value_unset (&ret);
763 caps = (GstCaps *) g_value_dup_boxed (&ret);
764 g_value_unset (&ret);
768 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
770 /* store in cache, take additional ref */
771 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
772 gst_caps_ref (caps));
775 GST_RTP_SESSION_UNLOCK (session);
782 GST_RTP_SESSION_UNLOCK (session);
783 GST_DEBUG ("no pt map could be obtained");
789 return_true (gpointer key, gpointer value, gpointer user_data)
795 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
797 GSList *clients, *streams;
799 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
801 GST_RTP_BIN_LOCK (rtpbin);
802 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
803 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
805 /* reset sync on all streams for this client */
806 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
807 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
809 /* make use require a new SR packet for this stream before we attempt new
811 stream->have_sync = FALSE;
812 stream->rt_delta = 0;
813 stream->rtp_delta = 0;
814 stream->clock_base = -100 * GST_SECOND;
817 GST_RTP_BIN_UNLOCK (rtpbin);
821 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
823 GSList *sessions, *streams;
825 GST_RTP_BIN_LOCK (bin);
826 GST_DEBUG_OBJECT (bin, "clearing pt map");
827 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
828 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
830 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
831 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
833 GST_RTP_SESSION_LOCK (session);
834 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
836 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
837 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
839 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
840 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
842 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
844 GST_RTP_SESSION_UNLOCK (session);
846 GST_RTP_BIN_UNLOCK (bin);
849 gst_rtp_bin_reset_sync (bin);
853 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
855 RTPSession *internal_session = NULL;
856 GstRtpBinSession *session;
858 GST_RTP_BIN_LOCK (bin);
859 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
861 session = find_session_by_id (bin, (gint) session_id);
863 g_object_get (session->session, "internal-session", &internal_session,
866 GST_RTP_BIN_UNLOCK (bin);
868 return internal_session;
872 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
873 const gchar * name, const GValue * value)
875 GSList *sessions, *streams;
877 GST_RTP_BIN_LOCK (bin);
878 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
879 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
881 GST_RTP_SESSION_LOCK (session);
882 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
883 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
885 g_object_set_property (G_OBJECT (stream->buffer), name, value);
887 GST_RTP_SESSION_UNLOCK (session);
889 GST_RTP_BIN_UNLOCK (bin);
892 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
893 static GstRtpBinClient *
894 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
896 GstRtpBinClient *result = NULL;
899 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
900 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
902 if (len != client->cname_len)
905 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
906 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
913 /* nothing found, create one */
914 if (result == NULL) {
915 result = g_new0 (GstRtpBinClient, 1);
916 result->cname = g_strndup ((gchar *) data, len);
917 result->cname_len = len;
918 bin->clients = g_slist_prepend (bin->clients, result);
919 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
926 free_client (GstRtpBinClient * client, GstRtpBin * bin)
928 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
929 g_slist_free (client->streams);
930 g_free (client->cname);
935 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
940 GstClockTime base_time, rt, clock_time;
942 GST_OBJECT_LOCK (bin);
943 if ((clock = GST_ELEMENT_CLOCK (bin))) {
944 base_time = GST_ELEMENT_CAST (bin)->base_time;
945 gst_object_ref (clock);
946 GST_OBJECT_UNLOCK (bin);
948 clock_time = gst_clock_get_time (clock);
950 if (bin->use_pipeline_clock) {
955 /* get current NTP time */
956 g_get_current_time (¤t);
957 ntpns = GST_TIMEVAL_TO_TIME (current);
960 /* add constant to convert from 1970 based time to 1900 based time */
961 ntpns += (2208988800LL * GST_SECOND);
963 /* get current clock time and convert to running time */
964 rt = clock_time - base_time;
966 gst_object_unref (clock);
968 GST_OBJECT_UNLOCK (bin);
979 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
982 gint64 prev_ts_offset;
984 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
986 /* delta changed, see how much */
987 if (prev_ts_offset != ts_offset) {
990 diff = prev_ts_offset - ts_offset;
992 GST_DEBUG_OBJECT (bin,
993 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
994 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
996 /* only change diff when it changed more than 4 milliseconds. This
997 * compensates for rounding errors in NTP to RTP timestamp
999 if (ABS (diff) > 4 * GST_MSECOND) {
1000 if (ABS (diff) < (3 * GST_SECOND)) {
1001 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1003 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
1006 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1009 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1010 stream->ssrc, ts_offset);
1013 /* associate a stream to the given CNAME. This will make sure all streams for
1014 * that CNAME are synchronized together.
1015 * Must be called with GST_RTP_BIN_LOCK */
1017 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1018 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1019 guint64 base_rtptime, guint64 base_time, guint clock_rate,
1020 gint64 rtp_clock_base)
1022 GstRtpBinClient *client;
1027 GstClockTime running_time;
1029 gint64 ntpdiff, rtdiff;
1032 /* first find or create the CNAME */
1033 client = get_client (bin, len, data, &created);
1035 /* find stream in the client */
1036 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1037 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1039 if (ostream == stream)
1042 /* not found, add it to the list */
1044 GST_DEBUG_OBJECT (bin,
1045 "new association of SSRC %08x with client %p with CNAME %s",
1046 stream->ssrc, client, client->cname);
1047 client->streams = g_slist_prepend (client->streams, stream);
1050 GST_DEBUG_OBJECT (bin,
1051 "found association of SSRC %08x with client %p with CNAME %s",
1052 stream->ssrc, client, client->cname);
1055 if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1056 GST_DEBUG_OBJECT (bin, "invalidated sync data");
1057 if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1058 /* we don't need that data, so carry on,
1059 * but make some values look saner */
1060 last_extrtptime = base_rtptime;
1062 /* nothing we can do with this data in this case */
1063 GST_DEBUG_OBJECT (bin, "bailing out");
1068 /* Take the extended rtptime we found in the SR packet and map it to the
1069 * local rtptime. The local rtp time is used to construct timestamps on the
1070 * buffers so we will calculate what running_time corresponds to the RTP
1071 * timestamp in the SR packet. */
1072 local_rtp = last_extrtptime - base_rtptime;
1074 GST_DEBUG_OBJECT (bin,
1075 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1076 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1077 "clock-base %" G_GINT64_FORMAT, base_rtptime,
1078 last_extrtptime, local_rtp, clock_rate, rtp_clock_base);
1080 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1081 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1082 * into a corresponding gstreamer timestamp. Note that the base_time also
1083 * contains the drift between sender and receiver. */
1084 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1085 local_rt += base_time;
1087 /* convert ntptime to unix time since 1900 */
1088 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1089 (G_GINT64_CONSTANT (1) << 32));
1091 stream->have_sync = TRUE;
1093 GST_DEBUG_OBJECT (bin,
1094 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1095 local_rt, last_unix);
1097 /* recalc inter stream playout offset, but only if there is more than one
1098 * stream or we're doing NTP sync. */
1099 if (bin->ntp_sync) {
1100 /* For NTP sync we need to first get a snapshot of running_time and NTP
1101 * time. We know at what running_time we play a certain RTP time, we also
1102 * calculated when we would play the RTP time in the SR packet. Now we need
1103 * to know how the running_time and the NTP time relate to eachother. */
1104 get_current_times (bin, &running_time, &ntpnstime);
1106 /* see how far away the NTP time is. This is the difference between the
1107 * current NTP time and the NTP time in the last SR packet. */
1108 ntpdiff = ntpnstime - last_unix;
1109 /* see how far away the running_time is. This is the difference between the
1110 * current running_time and the running_time of the RTP timestamp in the
1111 * last SR packet. */
1112 rtdiff = running_time - local_rt;
1114 GST_DEBUG_OBJECT (bin,
1115 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1116 ntpnstime, last_unix);
1117 GST_DEBUG_OBJECT (bin,
1118 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1121 /* combine to get the final diff to apply to the running_time */
1122 stream->rt_delta = rtdiff - ntpdiff;
1124 stream_set_ts_offset (bin, stream, stream->rt_delta);
1126 gint64 min, rtp_min, clock_base = stream->clock_base;
1127 gboolean all_sync, use_rtp;
1128 gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1130 /* calculate delta between server and receiver. last_unix is created by
1131 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1132 * delta expresses the difference to our timeline and the server timeline. The
1133 * difference in itself doesn't mean much but we can combine the delta of
1134 * multiple streams to create a stream specific offset. */
1135 stream->rt_delta = last_unix - local_rt;
1137 /* calculate the min of all deltas, ignoring streams that did not yet have a
1138 * valid rt_delta because we did not yet receive an SR packet for those
1140 * We calculate the mininum because we would like to only apply positive
1141 * offsets to streams, delaying their playback instead of trying to speed up
1142 * other streams (which might be imposible when we have to create negative
1144 * The stream that has the smallest diff is selected as the reference stream,
1145 * all other streams will have a positive offset to this difference. */
1147 /* some alternative setting allow ignoring RTCP as much as possible,
1148 * for servers generating bogus ntp timeline */
1149 min = rtp_min = G_MAXINT64;
1151 if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1155 /* signed version for convienience */
1156 clock_base = base_rtptime;
1157 /* deal with possible wrap-around */
1158 ext_base = base_rtptime;
1159 rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1160 /* sanity check; base rtp and provided clock_base should be close */
1161 if (rtp_clock_base >= clock_base) {
1162 if (rtp_clock_base - clock_base < 10 * clock_rate) {
1163 rtp_clock_base = base_time +
1164 gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1165 GST_SECOND, clock_rate);
1170 if (clock_base - rtp_clock_base < 10 * clock_rate) {
1171 rtp_clock_base = base_time -
1172 gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1173 GST_SECOND, clock_rate);
1178 /* warn and bail for clarity out if no sane values */
1180 GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1183 /* store to track changes */
1184 clock_base = rtp_clock_base;
1185 /* generate a fake as before,
1186 * now equating rtptime obtained from RTP-Info,
1187 * where the large time represent the otherwise irrelevant npt/ntp time */
1188 stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1191 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1192 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1194 if (!ostream->have_sync) {
1199 /* change in current stream's base from previously init'ed value
1200 * leads to reset of all stream's base */
1201 if (stream != ostream && stream->clock_base >= 0 &&
1202 (stream->clock_base != clock_base)) {
1203 GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1204 ostream->clock_base = -100 * GST_SECOND;
1205 ostream->rtp_delta = 0;
1208 if (ostream->rt_delta < min)
1209 min = ostream->rt_delta;
1210 if (ostream->rtp_delta < rtp_min)
1211 rtp_min = ostream->rtp_delta;
1214 /* arrange to re-sync for each stream upon significant change,
1216 all_sync = (stream->clock_base == clock_base);
1217 stream->clock_base = clock_base;
1219 /* may need init performed above later on, but nothing more to do now */
1220 if (client->nstreams <= 1)
1223 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1224 " all sync %d", client, min, all_sync);
1225 GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1227 switch (rtcp_sync) {
1228 case GST_RTP_BIN_RTCP_SYNC_RTP:
1231 GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1232 "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1234 case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1235 /* if all have been synced already, do not bother further */
1237 GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1245 /* bail out if we adjusted recently enough */
1246 if (all_sync && (last_unix - bin->priv->last_unix) <
1247 bin->rtcp_sync_interval * GST_MSECOND) {
1248 GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1249 "previous sender info too recent "
1250 "(previous UNIX %" G_GUINT64_FORMAT ")", bin->priv->last_unix);
1253 bin->priv->last_unix = last_unix;
1255 /* calculate offsets for each stream */
1256 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1257 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1260 /* ignore streams for which we didn't receive an SR packet yet, we
1261 * can't synchronize them yet. We can however sync other streams just
1263 if (!ostream->have_sync)
1266 /* calculate offset to our reference stream, this should always give a
1267 * positive number. */
1269 ts_offset = ostream->rtp_delta - rtp_min;
1271 ts_offset = ostream->rt_delta - min;
1273 stream_set_ts_offset (bin, ostream, ts_offset);
1279 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1280 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1281 (b) = gst_rtcp_packet_move_to_next ((packet)))
1283 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1284 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1285 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1287 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1288 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1289 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1292 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1293 GstRtpBinStream * stream)
1296 GstRTCPPacket packet;
1299 gboolean have_sr, have_sdes;
1301 guint64 base_rtptime;
1307 GstRTCPBuffer rtcp = { NULL };
1311 GST_DEBUG_OBJECT (bin, "sync handler called");
1313 /* get the last relation between the rtp timestamps and the gstreamer
1314 * timestamps. We get this info directly from the jitterbuffer which
1315 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1316 * what the current situation is. */
1318 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1319 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1320 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1321 clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1323 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1324 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1329 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1331 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1332 /* first packet must be SR or RR or else the validate would have failed */
1333 switch (gst_rtcp_packet_get_type (&packet)) {
1334 case GST_RTCP_TYPE_SR:
1335 /* only parse first. There is only supposed to be one SR in the packet
1336 * but we will deal with malformed packets gracefully */
1339 /* get NTP and RTP times */
1340 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1343 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1344 /* ignore SR that is not ours */
1345 if (ssrc != stream->ssrc)
1350 case GST_RTCP_TYPE_SDES:
1352 gboolean more_items, more_entries;
1354 /* only deal with first SDES, there is only supposed to be one SDES in
1355 * the RTCP packet but we deal with bad packets gracefully. Also bail
1356 * out if we have not seen an SR item yet. */
1357 if (have_sdes || !have_sr)
1360 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1361 /* skip items that are not about the SSRC of the sender */
1362 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1365 /* find the CNAME entry */
1366 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1367 GstRTCPSDESType type;
1371 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1373 if (type == GST_RTCP_SDES_CNAME) {
1374 GST_RTP_BIN_LOCK (bin);
1375 /* associate the stream to CNAME */
1376 gst_rtp_bin_associate (bin, stream, len, data,
1377 ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1379 GST_RTP_BIN_UNLOCK (bin);
1387 /* we can ignore these packets */
1391 gst_rtcp_buffer_unmap (&rtcp);
1394 /* create a new stream with @ssrc in @session. Must be called with
1395 * RTP_SESSION_LOCK. */
1396 static GstRtpBinStream *
1397 create_stream (GstRtpBinSession * session, guint32 ssrc)
1399 GstElement *buffer, *demux = NULL;
1400 GstRtpBinStream *stream;
1404 rtpbin = session->bin;
1406 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1407 goto no_jitterbuffer;
1409 if (!rtpbin->ignore_pt)
1410 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1414 stream = g_new0 (GstRtpBinStream, 1);
1415 stream->ssrc = ssrc;
1416 stream->bin = rtpbin;
1417 stream->session = session;
1418 stream->buffer = buffer;
1419 stream->demux = demux;
1421 stream->have_sync = FALSE;
1422 stream->rt_delta = 0;
1423 stream->rtp_delta = 0;
1424 stream->percent = 100;
1425 stream->clock_base = -100 * GST_SECOND;
1426 session->streams = g_slist_prepend (session->streams, stream);
1428 /* provide clock_rate to the jitterbuffer when needed */
1429 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1430 (GCallback) pt_map_requested, session);
1431 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1432 (GCallback) on_npt_stop, stream);
1434 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1435 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1437 /* configure latency and packet lost */
1438 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1439 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1440 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1442 if (!rtpbin->ignore_pt)
1443 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1444 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1448 gst_element_link (buffer, demux);
1450 if (rtpbin->buffering) {
1453 GST_INFO_OBJECT (rtpbin,
1454 "bin is buffering, set jitterbuffer as not active");
1455 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1459 GST_OBJECT_LOCK (rtpbin);
1460 target = GST_STATE_TARGET (rtpbin);
1461 GST_OBJECT_UNLOCK (rtpbin);
1463 /* from sink to source */
1465 gst_element_set_state (demux, target);
1467 gst_element_set_state (buffer, target);
1474 g_warning ("rtpbin: could not create gstrtpjitterbuffer element");
1479 gst_object_unref (buffer);
1480 g_warning ("rtpbin: could not create gstrtpptdemux element");
1486 free_stream (GstRtpBinStream * stream)
1488 GstRtpBinSession *session;
1490 session = stream->session;
1492 if (stream->demux) {
1493 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1494 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1495 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1497 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1498 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1499 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1502 gst_element_set_locked_state (stream->demux, TRUE);
1503 gst_element_set_locked_state (stream->buffer, TRUE);
1506 gst_element_set_state (stream->demux, GST_STATE_NULL);
1507 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1509 /* now remove this signal, we need this while going to NULL because it to
1510 * do some cleanups */
1512 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1514 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1516 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1521 /* GObject vmethods */
1522 static void gst_rtp_bin_dispose (GObject * object);
1523 static void gst_rtp_bin_finalize (GObject * object);
1524 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1525 const GValue * value, GParamSpec * pspec);
1526 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1527 GValue * value, GParamSpec * pspec);
1529 /* GstElement vmethods */
1530 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1531 GstStateChange transition);
1532 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1533 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1534 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1535 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1537 #define gst_rtp_bin_parent_class parent_class
1538 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1541 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1543 GObjectClass *gobject_class;
1544 GstElementClass *gstelement_class;
1545 GstBinClass *gstbin_class;
1547 gobject_class = (GObjectClass *) klass;
1548 gstelement_class = (GstElementClass *) klass;
1549 gstbin_class = (GstBinClass *) klass;
1551 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1553 gobject_class->dispose = gst_rtp_bin_dispose;
1554 gobject_class->finalize = gst_rtp_bin_finalize;
1555 gobject_class->set_property = gst_rtp_bin_set_property;
1556 gobject_class->get_property = gst_rtp_bin_get_property;
1558 g_object_class_install_property (gobject_class, PROP_LATENCY,
1559 g_param_spec_uint ("latency", "Buffer latency in ms",
1560 "Default amount of ms to buffer in the jitterbuffers", 0,
1561 G_MAXUINT, DEFAULT_LATENCY_MS,
1562 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1565 * GstRtpBin::request-pt-map:
1566 * @rtpbin: the object which received the signal
1567 * @session: the session
1570 * Request the payload type as #GstCaps for @pt in @session.
1572 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1573 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1574 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1575 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1576 G_TYPE_UINT, G_TYPE_UINT);
1579 * GstRtpBin::payload-type-change:
1580 * @rtpbin: the object which received the signal
1581 * @session: the session
1584 * Signal that the current payload type changed to @pt in @session.
1588 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1589 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1590 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1591 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1592 G_TYPE_UINT, G_TYPE_UINT);
1595 * GstRtpBin::clear-pt-map:
1596 * @rtpbin: the object which received the signal
1598 * Clear all previously cached pt-mapping obtained with
1599 * #GstRtpBin::request-pt-map.
1601 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1602 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1603 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1604 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1608 * GstRtpBin::reset-sync:
1609 * @rtpbin: the object which received the signal
1611 * Reset all currently configured lip-sync parameters and require new SR
1612 * packets for all streams before lip-sync is attempted again.
1614 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1615 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1616 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1617 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1621 * GstRtpBin::get-internal-session:
1622 * @rtpbin: the object which received the signal
1623 * @id: the session id
1625 * Request the internal RTPSession object as #GObject in session @id.
1627 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1628 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1629 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1630 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1631 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1634 * GstRtpBin::on-new-ssrc:
1635 * @rtpbin: the object which received the signal
1636 * @session: the session
1639 * Notify of a new SSRC that entered @session.
1641 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1642 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1643 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1644 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1645 G_TYPE_UINT, G_TYPE_UINT);
1647 * GstRtpBin::on-ssrc-collision:
1648 * @rtpbin: the object which received the signal
1649 * @session: the session
1652 * Notify when we have an SSRC collision
1654 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1655 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1656 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1657 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1658 G_TYPE_UINT, G_TYPE_UINT);
1660 * GstRtpBin::on-ssrc-validated:
1661 * @rtpbin: the object which received the signal
1662 * @session: the session
1665 * Notify of a new SSRC that became validated.
1667 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1668 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1669 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1670 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1671 G_TYPE_UINT, G_TYPE_UINT);
1673 * GstRtpBin::on-ssrc-active:
1674 * @rtpbin: the object which received the signal
1675 * @session: the session
1678 * Notify of a SSRC that is active, i.e., sending RTCP.
1680 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1681 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1682 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1683 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1684 G_TYPE_UINT, G_TYPE_UINT);
1686 * GstRtpBin::on-ssrc-sdes:
1687 * @rtpbin: the object which received the signal
1688 * @session: the session
1691 * Notify of a SSRC that is active, i.e., sending RTCP.
1693 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1694 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1695 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1696 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1697 G_TYPE_UINT, G_TYPE_UINT);
1700 * GstRtpBin::on-bye-ssrc:
1701 * @rtpbin: the object which received the signal
1702 * @session: the session
1705 * Notify of an SSRC that became inactive because of a BYE packet.
1707 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1708 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1709 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1710 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1711 G_TYPE_UINT, G_TYPE_UINT);
1713 * GstRtpBin::on-bye-timeout:
1714 * @rtpbin: the object which received the signal
1715 * @session: the session
1718 * Notify of an SSRC that has timed out because of BYE
1720 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1721 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1722 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1723 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1724 G_TYPE_UINT, G_TYPE_UINT);
1726 * GstRtpBin::on-timeout:
1727 * @rtpbin: the object which received the signal
1728 * @session: the session
1731 * Notify of an SSRC that has timed out
1733 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1734 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1735 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1736 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1737 G_TYPE_UINT, G_TYPE_UINT);
1739 * GstRtpBin::on-sender-timeout:
1740 * @rtpbin: the object which received the signal
1741 * @session: the session
1744 * Notify of a sender SSRC that has timed out and became a receiver
1746 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1747 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1748 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1749 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1750 G_TYPE_UINT, G_TYPE_UINT);
1753 * GstRtpBin::on-npt-stop:
1754 * @rtpbin: the object which received the signal
1755 * @session: the session
1758 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1760 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1761 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1762 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1763 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1764 G_TYPE_UINT, G_TYPE_UINT);
1766 g_object_class_install_property (gobject_class, PROP_SDES,
1767 g_param_spec_boxed ("sdes", "SDES",
1768 "The SDES items of this session",
1769 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1771 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1772 g_param_spec_boolean ("do-lost", "Do Lost",
1773 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1774 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1776 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1777 g_param_spec_boolean ("autoremove", "Auto Remove",
1778 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1779 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1781 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1782 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1783 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1784 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1786 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1787 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1788 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1789 DEFAULT_USE_PIPELINE_CLOCK,
1790 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1792 * GstRtpBin::buffer-mode:
1794 * Control the buffering and timestamping mode used by the jitterbuffer.
1798 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1799 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1800 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1801 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1803 * GstRtpBin::ntp-sync:
1805 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1806 * between the receivers and the senders (such as when using ntpd) this option
1807 * can be used to synchronize receivers on multiple machines.
1811 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1812 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1813 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1814 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1817 * GstRtpBin::rtcp-sync:
1819 * If not synchronizing (directly) to the NTP clock, determines how to sync
1820 * the various streams.
1824 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
1825 g_param_spec_enum ("rtcp-sync", "RTCP Sync",
1826 "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
1827 DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1830 * GstRtpBin::rtcp-sync-interval:
1832 * Determines how often to sync streams using RTCP data.
1836 g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
1837 g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
1838 "RTCP SR interval synchronization (ms) (0 = always)",
1839 0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
1840 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1842 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1843 gstelement_class->request_new_pad =
1844 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1845 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1848 gst_element_class_add_pad_template (gstelement_class,
1849 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1850 gst_element_class_add_pad_template (gstelement_class,
1851 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1852 gst_element_class_add_pad_template (gstelement_class,
1853 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1856 gst_element_class_add_pad_template (gstelement_class,
1857 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1858 gst_element_class_add_pad_template (gstelement_class,
1859 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1860 gst_element_class_add_pad_template (gstelement_class,
1861 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1863 gst_element_class_set_details_simple (gstelement_class, "RTP Bin",
1864 "Filter/Network/RTP",
1865 "Real-Time Transport Protocol bin",
1866 "Wim Taymans <wim.taymans@gmail.com>");
1868 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1870 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1871 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1872 klass->get_internal_session =
1873 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1875 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1879 gst_rtp_bin_init (GstRtpBin * rtpbin)
1883 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1884 rtpbin->priv->bin_lock = g_mutex_new ();
1885 rtpbin->priv->dyn_lock = g_mutex_new ();
1887 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1888 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1889 rtpbin->do_lost = DEFAULT_DO_LOST;
1890 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1891 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1892 rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
1893 rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
1894 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1895 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1896 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1898 /* some default SDES entries */
1899 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1900 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1901 "cname", G_TYPE_STRING, str,
1902 "name", G_TYPE_STRING, g_get_real_name (),
1903 "tool", G_TYPE_STRING, "GStreamer", NULL);
1908 gst_rtp_bin_dispose (GObject * object)
1912 rtpbin = GST_RTP_BIN (object);
1914 GST_DEBUG_OBJECT (object, "freeing sessions");
1915 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1916 g_slist_free (rtpbin->sessions);
1917 rtpbin->sessions = NULL;
1918 GST_DEBUG_OBJECT (object, "freeing clients");
1919 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1920 g_slist_free (rtpbin->clients);
1921 rtpbin->clients = NULL;
1923 G_OBJECT_CLASS (parent_class)->dispose (object);
1927 gst_rtp_bin_finalize (GObject * object)
1931 rtpbin = GST_RTP_BIN (object);
1934 gst_structure_free (rtpbin->sdes);
1936 g_mutex_free (rtpbin->priv->bin_lock);
1937 g_mutex_free (rtpbin->priv->dyn_lock);
1939 G_OBJECT_CLASS (parent_class)->finalize (object);
1944 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1951 GST_RTP_BIN_LOCK (bin);
1953 GST_OBJECT_LOCK (bin);
1955 gst_structure_free (bin->sdes);
1956 bin->sdes = gst_structure_copy (sdes);
1957 GST_OBJECT_UNLOCK (bin);
1959 /* store in all sessions */
1960 for (item = bin->sessions; item; item = g_slist_next (item)) {
1961 GstRtpBinSession *session = item->data;
1962 g_object_set (session->session, "sdes", sdes, NULL);
1965 GST_RTP_BIN_UNLOCK (bin);
1968 static GstStructure *
1969 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1971 GstStructure *result;
1973 GST_OBJECT_LOCK (bin);
1974 result = gst_structure_copy (bin->sdes);
1975 GST_OBJECT_UNLOCK (bin);
1981 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1982 const GValue * value, GParamSpec * pspec)
1986 rtpbin = GST_RTP_BIN (object);
1990 GST_RTP_BIN_LOCK (rtpbin);
1991 rtpbin->latency_ms = g_value_get_uint (value);
1992 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1993 GST_RTP_BIN_UNLOCK (rtpbin);
1994 /* propagate the property down to the jitterbuffer */
1995 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1998 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2001 GST_RTP_BIN_LOCK (rtpbin);
2002 rtpbin->do_lost = g_value_get_boolean (value);
2003 GST_RTP_BIN_UNLOCK (rtpbin);
2004 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2007 rtpbin->ntp_sync = g_value_get_boolean (value);
2009 case PROP_RTCP_SYNC:
2010 g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2012 case PROP_RTCP_SYNC_INTERVAL:
2013 rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2015 case PROP_IGNORE_PT:
2016 rtpbin->ignore_pt = g_value_get_boolean (value);
2018 case PROP_AUTOREMOVE:
2019 rtpbin->priv->autoremove = g_value_get_boolean (value);
2021 case PROP_USE_PIPELINE_CLOCK:
2024 GST_RTP_BIN_LOCK (rtpbin);
2025 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2026 for (sessions = rtpbin->sessions; sessions;
2027 sessions = g_slist_next (sessions)) {
2028 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2030 g_object_set (G_OBJECT (session->session),
2031 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2033 GST_RTP_BIN_UNLOCK (rtpbin);
2036 case PROP_BUFFER_MODE:
2037 GST_RTP_BIN_LOCK (rtpbin);
2038 rtpbin->buffer_mode = g_value_get_enum (value);
2039 GST_RTP_BIN_UNLOCK (rtpbin);
2040 /* propagate the property down to the jitterbuffer */
2041 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2044 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2050 gst_rtp_bin_get_property (GObject * object, guint prop_id,
2051 GValue * value, GParamSpec * pspec)
2055 rtpbin = GST_RTP_BIN (object);
2059 GST_RTP_BIN_LOCK (rtpbin);
2060 g_value_set_uint (value, rtpbin->latency_ms);
2061 GST_RTP_BIN_UNLOCK (rtpbin);
2064 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
2067 GST_RTP_BIN_LOCK (rtpbin);
2068 g_value_set_boolean (value, rtpbin->do_lost);
2069 GST_RTP_BIN_UNLOCK (rtpbin);
2071 case PROP_IGNORE_PT:
2072 g_value_set_boolean (value, rtpbin->ignore_pt);
2075 g_value_set_boolean (value, rtpbin->ntp_sync);
2077 case PROP_RTCP_SYNC:
2078 g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
2080 case PROP_RTCP_SYNC_INTERVAL:
2081 g_value_set_uint (value, rtpbin->rtcp_sync_interval);
2083 case PROP_AUTOREMOVE:
2084 g_value_set_boolean (value, rtpbin->priv->autoremove);
2086 case PROP_BUFFER_MODE:
2087 g_value_set_enum (value, rtpbin->buffer_mode);
2089 case PROP_USE_PIPELINE_CLOCK:
2090 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
2093 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2099 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
2103 rtpbin = GST_RTP_BIN (bin);
2105 switch (GST_MESSAGE_TYPE (message)) {
2106 case GST_MESSAGE_ELEMENT:
2108 const GstStructure *s = gst_message_get_structure (message);
2110 /* we change the structure name and add the session ID to it */
2111 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
2112 GstRtpBinSession *sess;
2114 /* find the session we set it as object data */
2115 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2116 "GstRTPBin.session");
2118 if (G_LIKELY (sess)) {
2119 message = gst_message_make_writable (message);
2120 s = gst_message_get_structure (message);
2121 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
2125 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2128 case GST_MESSAGE_BUFFERING:
2131 gint min_percent = 100;
2132 GSList *sessions, *streams;
2133 GstRtpBinStream *stream;
2134 gboolean change = FALSE, active = FALSE;
2135 GstClockTime min_out_time;
2136 GstBufferingMode mode;
2137 gint avg_in, avg_out;
2138 gint64 buffering_left;
2140 gst_message_parse_buffering (message, &percent);
2141 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
2145 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
2146 "GstRTPBin.stream");
2148 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
2150 /* get the stream */
2151 if (G_LIKELY (stream)) {
2152 GST_RTP_BIN_LOCK (rtpbin);
2153 /* fill in the percent */
2154 stream->percent = percent;
2156 /* calculate the min value for all streams */
2157 for (sessions = rtpbin->sessions; sessions;
2158 sessions = g_slist_next (sessions)) {
2159 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2161 GST_RTP_SESSION_LOCK (session);
2162 if (session->streams) {
2163 for (streams = session->streams; streams;
2164 streams = g_slist_next (streams)) {
2165 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2167 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
2170 /* find min percent */
2171 if (min_percent > stream->percent)
2172 min_percent = stream->percent;
2175 GST_INFO_OBJECT (bin,
2176 "session has no streams, setting min_percent to 0");
2179 GST_RTP_SESSION_UNLOCK (session);
2181 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
2183 if (rtpbin->buffering) {
2184 if (min_percent == 100) {
2185 rtpbin->buffering = FALSE;
2190 if (min_percent < 100) {
2191 /* pause the streams */
2192 rtpbin->buffering = TRUE;
2197 GST_RTP_BIN_UNLOCK (rtpbin);
2199 gst_message_unref (message);
2201 /* make a new buffering message with the min value */
2203 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2204 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2207 if (G_UNLIKELY (change)) {
2209 guint64 running_time = 0;
2212 /* figure out the running time when we have a clock */
2213 if (G_LIKELY ((clock =
2214 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2215 guint64 now, base_time;
2217 now = gst_clock_get_time (clock);
2218 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2219 running_time = now - base_time;
2220 gst_object_unref (clock);
2222 GST_DEBUG_OBJECT (bin,
2223 "running time now %" GST_TIME_FORMAT,
2224 GST_TIME_ARGS (running_time));
2226 GST_RTP_BIN_LOCK (rtpbin);
2228 /* when we reactivate, calculate the offsets so that all streams have
2229 * an output time that is at least as big as the running_time */
2232 if (running_time > rtpbin->buffer_start) {
2233 offset = running_time - rtpbin->buffer_start;
2234 if (offset >= rtpbin->latency_ns)
2235 offset -= rtpbin->latency_ns;
2241 /* pause all streams */
2243 for (sessions = rtpbin->sessions; sessions;
2244 sessions = g_slist_next (sessions)) {
2245 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2247 GST_RTP_SESSION_LOCK (session);
2248 for (streams = session->streams; streams;
2249 streams = g_slist_next (streams)) {
2250 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2251 GstElement *element = stream->buffer;
2254 g_signal_emit_by_name (element, "set-active", active, offset,
2258 g_object_get (element, "percent", &stream->percent, NULL);
2262 if (min_out_time == -1 || last_out < min_out_time)
2263 min_out_time = last_out;
2266 GST_DEBUG_OBJECT (bin,
2267 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2268 GST_TIME_FORMAT ", percent %d", element, active,
2269 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2272 GST_RTP_SESSION_UNLOCK (session);
2274 GST_DEBUG_OBJECT (bin,
2275 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2277 /* the buffer_start is the min out time of all paused jitterbuffers */
2279 rtpbin->buffer_start = min_out_time;
2281 GST_RTP_BIN_UNLOCK (rtpbin);
2284 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2289 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2295 static GstStateChangeReturn
2296 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2298 GstStateChangeReturn res;
2300 GstRtpBinPrivate *priv;
2302 rtpbin = GST_RTP_BIN (element);
2303 priv = rtpbin->priv;
2305 switch (transition) {
2306 case GST_STATE_CHANGE_NULL_TO_READY:
2308 case GST_STATE_CHANGE_READY_TO_PAUSED:
2309 priv->last_unix = 0;
2310 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2311 g_atomic_int_set (&priv->shutdown, 0);
2313 case GST_STATE_CHANGE_PAUSED_TO_READY:
2314 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2315 g_atomic_int_set (&priv->shutdown, 1);
2316 /* wait for all callbacks to end by taking the lock. No new callbacks will
2317 * be able to happen as we set the shutdown flag. */
2318 GST_RTP_BIN_DYN_LOCK (rtpbin);
2319 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2320 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2326 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2328 switch (transition) {
2329 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2331 case GST_STATE_CHANGE_PAUSED_TO_READY:
2333 case GST_STATE_CHANGE_READY_TO_NULL:
2341 /* a new pad (SSRC) was created in @session. This signal is emited from the
2342 * payload demuxer. */
2344 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2345 GstRtpBinStream * stream)
2348 GstElementClass *klass;
2349 GstPadTemplate *templ;
2353 rtpbin = stream->bin;
2355 GST_DEBUG ("new payload pad %d", pt);
2357 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2359 /* ghost the pad to the parent */
2360 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2361 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2362 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2363 stream->session->id, stream->ssrc, pt);
2364 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2366 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2368 gst_pad_set_active (gpad, TRUE);
2369 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2371 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2377 GST_DEBUG ("ignoring, we are shutting down");
2383 payload_pad_removed (GstElement * element, GstPad * pad,
2384 GstRtpBinStream * stream)
2389 rtpbin = stream->bin;
2391 GST_DEBUG ("payload pad removed");
2393 GST_RTP_BIN_DYN_LOCK (rtpbin);
2394 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2395 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2397 gst_pad_set_active (gpad, FALSE);
2398 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2400 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2404 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2409 rtpbin = session->bin;
2411 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2414 caps = get_pt_map (session, pt);
2423 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2429 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2431 GST_DEBUG_OBJECT (session->bin,
2432 "emiting signal for pt type changed to %d in session %d", pt,
2435 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2436 0, session->id, pt);
2439 /* emited when caps changed for the session */
2441 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2446 const GstStructure *s;
2450 g_object_get (pad, "caps", &caps, NULL);
2455 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2457 s = gst_caps_get_structure (caps, 0);
2459 /* get payload, finish when it's not there */
2460 if (!gst_structure_get_int (s, "payload", &payload))
2463 GST_RTP_SESSION_LOCK (session);
2464 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2465 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2466 GST_RTP_SESSION_UNLOCK (session);
2469 /* a new pad (SSRC) was created in @session */
2471 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2472 GstRtpBinSession * session)
2475 GstRtpBinStream *stream;
2476 GstPad *sinkpad, *srcpad;
2479 rtpbin = session->bin;
2481 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2482 GST_DEBUG_PAD_NAME (pad));
2484 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2486 GST_RTP_SESSION_LOCK (session);
2488 /* create new stream */
2489 stream = create_stream (session, ssrc);
2493 /* get pad and link */
2494 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2495 padname = g_strdup_printf ("src_%u", ssrc);
2496 srcpad = gst_element_get_static_pad (element, padname);
2498 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2499 gst_pad_link (srcpad, sinkpad);
2500 gst_object_unref (sinkpad);
2501 gst_object_unref (srcpad);
2503 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2504 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
2505 srcpad = gst_element_get_static_pad (element, padname);
2507 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2508 gst_pad_link (srcpad, sinkpad);
2509 gst_object_unref (sinkpad);
2510 gst_object_unref (srcpad);
2512 /* connect to the RTCP sync signal from the jitterbuffer */
2513 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2514 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2515 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2517 if (stream->demux) {
2518 /* connect to the new-pad signal of the payload demuxer, this will expose the
2519 * new pad by ghosting it. */
2520 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2521 "new-payload-type", (GCallback) new_payload_found, stream);
2522 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2523 "pad-removed", (GCallback) payload_pad_removed, stream);
2525 /* connect to the request-pt-map signal. This signal will be emited by the
2526 * demuxer so that it can apply a proper caps on the buffers for the
2528 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2529 "request-pt-map", (GCallback) pt_map_requested, session);
2530 /* connect to the signal so it can be forwarded. */
2531 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2532 "payload-type-change", (GCallback) payload_type_change, session);
2534 /* add gstrtpjitterbuffer src pad to pads */
2535 GstElementClass *klass;
2536 GstPadTemplate *templ;
2540 pad = gst_element_get_static_pad (stream->buffer, "src");
2542 /* ghost the pad to the parent */
2543 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2544 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
2545 padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
2546 stream->session->id, stream->ssrc, 255);
2547 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2550 gst_pad_set_active (gpad, TRUE);
2551 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2553 gst_object_unref (pad);
2556 GST_RTP_SESSION_UNLOCK (session);
2557 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2564 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2569 GST_RTP_SESSION_UNLOCK (session);
2570 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2571 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2576 /* Create a pad for receiving RTP for the session in @name. Must be called with
2580 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2584 GstRtpBinSession *session;
2585 GstPadLinkReturn lres;
2587 /* first get the session number */
2588 if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
2591 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2593 /* get or create session */
2594 session = find_session_by_id (rtpbin, sessid);
2596 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2597 /* create session now */
2598 session = create_session (rtpbin, sessid);
2599 if (session == NULL)
2603 /* check if pad was requested */
2604 if (session->recv_rtp_sink_ghost != NULL)
2605 return session->recv_rtp_sink_ghost;
2607 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2608 /* get recv_rtp pad and store */
2609 session->recv_rtp_sink =
2610 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2611 if (session->recv_rtp_sink == NULL)
2614 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2615 (GCallback) caps_changed, session);
2617 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2618 /* get srcpad, link to SSRCDemux */
2619 session->recv_rtp_src =
2620 gst_element_get_static_pad (session->session, "recv_rtp_src");
2621 if (session->recv_rtp_src == NULL)
2624 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2625 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2626 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2627 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2628 gst_object_unref (sinkdpad);
2629 if (lres != GST_PAD_LINK_OK)
2632 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2633 session->demux_newpad_sig = g_signal_connect (session->demux,
2634 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2635 session->demux_padremoved_sig = g_signal_connect (session->demux,
2636 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2638 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2639 session->recv_rtp_sink_ghost =
2640 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2641 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2642 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2644 return session->recv_rtp_sink_ghost;
2649 g_warning ("rtpbin: invalid name given");
2654 /* create_session already warned */
2659 g_warning ("rtpbin: failed to get session pad");
2664 g_warning ("rtpbin: failed to link pads");
2670 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2672 if (session->demux_newpad_sig) {
2673 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2674 session->demux_newpad_sig = 0;
2676 if (session->demux_padremoved_sig) {
2677 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2678 session->demux_padremoved_sig = 0;
2680 if (session->recv_rtp_src) {
2681 gst_object_unref (session->recv_rtp_src);
2682 session->recv_rtp_src = NULL;
2684 if (session->recv_rtp_sink) {
2685 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2686 gst_object_unref (session->recv_rtp_sink);
2687 session->recv_rtp_sink = NULL;
2689 if (session->recv_rtp_sink_ghost) {
2690 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2691 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2692 session->recv_rtp_sink_ghost);
2693 session->recv_rtp_sink_ghost = NULL;
2697 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2701 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2705 GstRtpBinSession *session;
2707 GstPadLinkReturn lres;
2709 /* first get the session number */
2710 if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
2713 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2715 /* get or create the session */
2716 session = find_session_by_id (rtpbin, sessid);
2718 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2719 /* create session now */
2720 session = create_session (rtpbin, sessid);
2721 if (session == NULL)
2725 /* check if pad was requested */
2726 if (session->recv_rtcp_sink_ghost != NULL)
2727 return session->recv_rtcp_sink_ghost;
2729 /* get recv_rtp pad and store */
2730 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2731 session->recv_rtcp_sink =
2732 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2733 if (session->recv_rtcp_sink == NULL)
2736 /* get srcpad, link to SSRCDemux */
2737 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2738 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2739 if (session->sync_src == NULL)
2742 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2743 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2744 lres = gst_pad_link (session->sync_src, sinkdpad);
2745 gst_object_unref (sinkdpad);
2746 if (lres != GST_PAD_LINK_OK)
2749 session->recv_rtcp_sink_ghost =
2750 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2751 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2752 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2753 session->recv_rtcp_sink_ghost);
2755 return session->recv_rtcp_sink_ghost;
2760 g_warning ("rtpbin: invalid name given");
2765 /* create_session already warned */
2770 g_warning ("rtpbin: failed to get session pad");
2775 g_warning ("rtpbin: failed to link pads");
2781 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2783 if (session->recv_rtcp_sink_ghost) {
2784 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2785 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2786 session->recv_rtcp_sink_ghost);
2787 session->recv_rtcp_sink_ghost = NULL;
2789 if (session->sync_src) {
2790 /* releasing the request pad should also unref the sync pad */
2791 gst_object_unref (session->sync_src);
2792 session->sync_src = NULL;
2794 if (session->recv_rtcp_sink) {
2795 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2796 gst_object_unref (session->recv_rtcp_sink);
2797 session->recv_rtcp_sink = NULL;
2801 /* Create a pad for sending RTP for the session in @name. Must be called with
2805 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2809 GstRtpBinSession *session;
2810 GstElementClass *klass;
2812 /* first get the session number */
2813 if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
2816 /* get or create session */
2817 session = find_session_by_id (rtpbin, sessid);
2819 /* create session now */
2820 session = create_session (rtpbin, sessid);
2821 if (session == NULL)
2825 /* check if pad was requested */
2826 if (session->send_rtp_sink_ghost != NULL)
2827 return session->send_rtp_sink_ghost;
2829 /* get send_rtp pad and store */
2830 session->send_rtp_sink =
2831 gst_element_get_request_pad (session->session, "send_rtp_sink");
2832 if (session->send_rtp_sink == NULL)
2835 session->send_rtp_sink_ghost =
2836 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2837 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2838 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2841 session->send_rtp_src =
2842 gst_element_get_static_pad (session->session, "send_rtp_src");
2843 if (session->send_rtp_src == NULL)
2846 /* ghost the new source pad */
2847 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2848 gname = g_strdup_printf ("send_rtp_src_%u", sessid);
2849 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
2850 session->send_rtp_src_ghost =
2851 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2852 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2853 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2856 return session->send_rtp_sink_ghost;
2861 g_warning ("rtpbin: invalid name given");
2866 /* create_session already warned */
2871 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
2876 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
2882 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2884 if (session->send_rtp_src_ghost) {
2885 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2886 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2887 session->send_rtp_src_ghost);
2888 session->send_rtp_src_ghost = NULL;
2890 if (session->send_rtp_src) {
2891 gst_object_unref (session->send_rtp_src);
2892 session->send_rtp_src = NULL;
2894 if (session->send_rtp_sink) {
2895 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2896 session->send_rtp_sink);
2897 gst_object_unref (session->send_rtp_sink);
2898 session->send_rtp_sink = NULL;
2900 if (session->send_rtp_sink_ghost) {
2901 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2902 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2903 session->send_rtp_sink_ghost);
2904 session->send_rtp_sink_ghost = NULL;
2908 /* Create a pad for sending RTCP for the session in @name. Must be called with
2912 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2915 GstRtpBinSession *session;
2917 /* first get the session number */
2918 if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
2921 /* get or create session */
2922 session = find_session_by_id (rtpbin, sessid);
2926 /* check if pad was requested */
2927 if (session->send_rtcp_src_ghost != NULL)
2928 return session->send_rtcp_src_ghost;
2930 /* get rtcp_src pad and store */
2931 session->send_rtcp_src =
2932 gst_element_get_request_pad (session->session, "send_rtcp_src");
2933 if (session->send_rtcp_src == NULL)
2936 session->send_rtcp_src_ghost =
2937 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2938 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2939 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2941 return session->send_rtcp_src_ghost;
2946 g_warning ("rtpbin: invalid name given");
2951 g_warning ("rtpbin: session with id %d does not exist", sessid);
2956 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
2962 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2964 if (session->send_rtcp_src_ghost) {
2965 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2966 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2967 session->send_rtcp_src_ghost);
2968 session->send_rtcp_src_ghost = NULL;
2970 if (session->send_rtcp_src) {
2971 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2972 gst_object_unref (session->send_rtcp_src);
2973 session->send_rtcp_src = NULL;
2977 /* If the requested name is NULL we should create a name with
2978 * the session number assuming we want the lowest posible session
2979 * with a free pad like the template */
2981 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2983 gboolean name_found = FALSE;
2985 GstIterator *pad_it = NULL;
2986 gchar *pad_name = NULL;
2987 GValue data = { 0, };
2989 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2990 while (!name_found) {
2991 gboolean done = FALSE;
2994 pad_name = g_strdup_printf (templ->name_template, session++);
2995 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2998 switch (gst_iterator_next (pad_it, &data)) {
2999 case GST_ITERATOR_OK:
3004 pad = g_value_get_object (&data);
3005 name = gst_pad_get_name (pad);
3007 if (strcmp (name, pad_name) == 0) {
3012 g_value_reset (&data);
3015 case GST_ITERATOR_ERROR:
3016 case GST_ITERATOR_RESYNC:
3017 /* restart iteration */
3022 case GST_ITERATOR_DONE:
3027 g_value_unset (&data);
3028 gst_iterator_free (pad_it);
3031 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
3038 gst_rtp_bin_request_new_pad (GstElement * element,
3039 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
3042 GstElementClass *klass;
3045 gchar *pad_name = NULL;
3047 g_return_val_if_fail (templ != NULL, NULL);
3048 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
3050 rtpbin = GST_RTP_BIN (element);
3051 klass = GST_ELEMENT_GET_CLASS (element);
3053 GST_RTP_BIN_LOCK (rtpbin);
3056 /* use a free pad name */
3057 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
3059 /* use the provided name */
3060 pad_name = g_strdup (name);
3063 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
3065 /* figure out the template */
3066 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
3067 result = create_recv_rtp (rtpbin, templ, pad_name);
3068 } else if (templ == gst_element_class_get_pad_template (klass,
3069 "recv_rtcp_sink_%u")) {
3070 result = create_recv_rtcp (rtpbin, templ, pad_name);
3071 } else if (templ == gst_element_class_get_pad_template (klass,
3072 "send_rtp_sink_%u")) {
3073 result = create_send_rtp (rtpbin, templ, pad_name);
3074 } else if (templ == gst_element_class_get_pad_template (klass,
3075 "send_rtcp_src_%u")) {
3076 result = create_rtcp (rtpbin, templ, pad_name);
3078 goto wrong_template;
3081 GST_RTP_BIN_UNLOCK (rtpbin);
3089 GST_RTP_BIN_UNLOCK (rtpbin);
3090 g_warning ("rtpbin: this is not our template");
3096 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
3098 GstRtpBinSession *session;
3101 g_return_if_fail (GST_IS_GHOST_PAD (pad));
3102 g_return_if_fail (GST_IS_RTP_BIN (element));
3104 rtpbin = GST_RTP_BIN (element);
3106 GST_RTP_BIN_LOCK (rtpbin);
3107 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
3108 GST_DEBUG_PAD_NAME (pad));
3110 if (!(session = find_session_by_pad (rtpbin, pad)))
3113 if (session->recv_rtp_sink_ghost == pad) {
3114 remove_recv_rtp (rtpbin, session);
3115 } else if (session->recv_rtcp_sink_ghost == pad) {
3116 remove_recv_rtcp (rtpbin, session);
3117 } else if (session->send_rtp_sink_ghost == pad) {
3118 remove_send_rtp (rtpbin, session);
3119 } else if (session->send_rtcp_src_ghost == pad) {
3120 remove_rtcp (rtpbin, session);
3123 /* no more request pads, free the complete session */
3124 if (session->recv_rtp_sink_ghost == NULL
3125 && session->recv_rtcp_sink_ghost == NULL
3126 && session->send_rtp_sink_ghost == NULL
3127 && session->send_rtcp_src_ghost == NULL) {
3128 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
3129 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
3130 free_session (session, rtpbin);
3132 GST_RTP_BIN_UNLOCK (rtpbin);
3139 GST_RTP_BIN_UNLOCK (rtpbin);
3140 g_warning ("rtpbin: %s:%s is not one of our request pads",
3141 GST_DEBUG_PAD_NAME (pad));