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_\%d pad. The session
32 * number must be specified in the pad name.
33 * Data received on the recv_rtp_sink_\%d 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_\%d_\%d_\%d 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_\%d 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_\%d 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_\%d pad, which will
51 * automatically create a send_rtp_src_\%d 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_\%d 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
62 * <title>Example pipelines</title>
64 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
65 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
66 * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
68 * gst-launch gstrtpbin name=rtpbin \
69 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
70 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
71 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
72 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
73 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
74 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
75 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
76 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
77 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
78 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
79 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
80 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
81 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
82 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
83 * is received on port 5007. Since RTCP packets from the sender should be sent
84 * as soon as possible and do not participate in preroll, sync=false and
85 * async=false is configured on udpsink
87 * gst-launch -v gstrtpbin name=rtpbin \
88 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
89 * port=5000 ! rtpbin.recv_rtp_sink_0 \
90 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
91 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
92 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
93 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
94 * port=5002 ! rtpbin.recv_rtp_sink_1 \
95 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
96 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
97 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
98 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
99 * decode and display the video.
100 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
101 * decode and play the audio.
102 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
103 * session 1 on port 5003. These packets will be used for session management and
105 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
109 * Last reviewed on 2007-08-30 (0.10.6)
118 #include <gst/rtp/gstrtpbuffer.h>
119 #include <gst/rtp/gstrtcpbuffer.h>
121 #include "gstrtpbin-marshal.h"
122 #include "gstrtpbin.h"
123 #include "rtpsession.h"
124 #include "gstrtpsession.h"
125 #include "gstrtpjitterbuffer.h"
127 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
128 #define GST_CAT_DEFAULT gst_rtp_bin_debug
131 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
132 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
135 GST_STATIC_CAPS ("application/x-rtp")
138 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
139 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
142 GST_STATIC_CAPS ("application/x-rtcp")
145 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
146 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
149 GST_STATIC_CAPS ("application/x-rtp")
153 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
154 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
157 GST_STATIC_CAPS ("application/x-rtp")
160 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
161 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
164 GST_STATIC_CAPS ("application/x-rtcp")
167 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
168 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
171 GST_STATIC_CAPS ("application/x-rtp")
174 #define GST_RTP_BIN_GET_PRIVATE(obj) \
175 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
177 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
178 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
180 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
181 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock ((bin)->priv->dyn_lock)
182 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->dyn_lock)
184 /* lock for shutdown */
185 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
187 if (g_atomic_int_get (&bin->priv->shutdown)) \
189 GST_RTP_BIN_DYN_LOCK (bin); \
190 if (g_atomic_int_get (&bin->priv->shutdown)) { \
191 GST_RTP_BIN_DYN_UNLOCK (bin); \
196 /* unlock for shutdown */
197 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
198 GST_RTP_BIN_DYN_UNLOCK (bin); \
200 struct _GstRtpBinPrivate
204 /* lock protecting dynamic adding/removing */
207 /* if we are shutting down or not */
213 /* signals and args */
216 SIGNAL_REQUEST_PT_MAP,
217 SIGNAL_PAYLOAD_TYPE_CHANGE,
220 SIGNAL_GET_INTERNAL_SESSION,
223 SIGNAL_ON_SSRC_COLLISION,
224 SIGNAL_ON_SSRC_VALIDATED,
225 SIGNAL_ON_SSRC_ACTIVE,
228 SIGNAL_ON_BYE_TIMEOUT,
230 SIGNAL_ON_SENDER_TIMEOUT,
235 #define DEFAULT_LATENCY_MS 200
236 #define DEFAULT_SDES NULL
237 #define DEFAULT_DO_LOST FALSE
238 #define DEFAULT_IGNORE_PT FALSE
239 #define DEFAULT_NTP_SYNC FALSE
240 #define DEFAULT_AUTOREMOVE FALSE
241 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
242 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
254 PROP_USE_PIPELINE_CLOCK,
259 typedef struct _GstRtpBinSession GstRtpBinSession;
260 typedef struct _GstRtpBinStream GstRtpBinStream;
261 typedef struct _GstRtpBinClient GstRtpBinClient;
263 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
265 static GstCaps *pt_map_requested (GstElement * element, guint pt,
266 GstRtpBinSession * session);
267 static void payload_type_change (GstElement * element, guint pt,
268 GstRtpBinSession * session);
269 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
270 static void free_stream (GstRtpBinStream * stream);
272 /* Manages the RTP stream for one SSRC.
274 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
275 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
276 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
277 * together (see below).
279 struct _GstRtpBinStream
281 /* the SSRC of this stream */
287 /* the session this SSRC belongs to */
288 GstRtpBinSession *session;
290 /* the jitterbuffer of the SSRC */
292 gulong buffer_handlesync_sig;
293 gulong buffer_ptreq_sig;
294 gulong buffer_ntpstop_sig;
297 /* the PT demuxer of the SSRC */
299 gulong demux_newpad_sig;
300 gulong demux_padremoved_sig;
301 gulong demux_ptreq_sig;
302 gulong demux_ptchange_sig;
304 /* if we have calculated a valid rt_delta for this stream */
306 /* mapping to local RTP and NTP time */
310 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
311 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
313 /* Manages the receiving end of the packets.
315 * There is one such structure for each RTP session (audio/video/...).
316 * We get the RTP/RTCP packets and stuff them into the session manager. From
317 * there they are pushed into an SSRC demuxer that splits the stream based on
318 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
319 * the GstRtpBinStream above).
321 struct _GstRtpBinSession
327 /* the session element */
329 /* the SSRC demuxer */
331 gulong demux_newpad_sig;
332 gulong demux_padremoved_sig;
336 /* list of GstRtpBinStream */
339 /* mapping of payload type to caps */
342 /* the pads of the session */
343 GstPad *recv_rtp_sink;
344 GstPad *recv_rtp_sink_ghost;
345 GstPad *recv_rtp_src;
346 GstPad *recv_rtcp_sink;
347 GstPad *recv_rtcp_sink_ghost;
349 GstPad *send_rtp_sink;
350 GstPad *send_rtp_sink_ghost;
351 GstPad *send_rtp_src;
352 GstPad *send_rtp_src_ghost;
353 GstPad *send_rtcp_src;
354 GstPad *send_rtcp_src_ghost;
357 /* Manages the RTP streams that come from one client and should therefore be
360 struct _GstRtpBinClient
362 /* the common CNAME for the streams */
371 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
372 static GstRtpBinSession *
373 find_session_by_id (GstRtpBin * rtpbin, gint id)
377 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
378 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
386 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
387 static GstRtpBinSession *
388 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
392 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
393 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
395 if ((sess->recv_rtp_sink_ghost == pad) ||
396 (sess->recv_rtcp_sink_ghost == pad) ||
397 (sess->send_rtp_sink_ghost == pad)
398 || (sess->send_rtcp_src_ghost == pad))
405 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
407 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
412 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
414 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
419 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
421 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
426 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
428 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
433 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
435 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
440 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
442 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
447 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
449 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
452 if (sess->bin->priv->autoremove)
453 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
457 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
459 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
462 if (sess->bin->priv->autoremove)
463 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
467 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
469 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
474 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
476 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
477 stream->session->id, stream->ssrc);
480 /* must be called with the SESSION lock */
481 static GstRtpBinStream *
482 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
486 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
487 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
489 if (stream->ssrc == ssrc)
496 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
497 GstRtpBinSession * session)
499 GstRtpBinStream *stream = NULL;
501 GST_RTP_SESSION_LOCK (session);
502 if ((stream = find_stream_by_ssrc (session, ssrc)))
503 session->streams = g_slist_remove (session->streams, stream);
504 GST_RTP_SESSION_UNLOCK (session);
507 free_stream (stream);
510 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
511 static GstRtpBinSession *
512 create_session (GstRtpBin * rtpbin, gint id)
514 GstRtpBinSession *sess;
515 GstElement *session, *demux;
518 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
521 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
524 sess = g_new0 (GstRtpBinSession, 1);
525 sess->lock = g_mutex_new ();
528 sess->session = session;
530 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
531 (GDestroyNotify) gst_caps_unref);
532 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
534 /* configure SDES items */
535 GST_OBJECT_LOCK (rtpbin);
536 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
537 rtpbin->use_pipeline_clock, NULL);
538 GST_OBJECT_UNLOCK (rtpbin);
540 /* provide clock_rate to the session manager when needed */
541 g_signal_connect (session, "request-pt-map",
542 (GCallback) pt_map_requested, sess);
544 g_signal_connect (sess->session, "on-new-ssrc",
545 (GCallback) on_new_ssrc, sess);
546 g_signal_connect (sess->session, "on-ssrc-collision",
547 (GCallback) on_ssrc_collision, sess);
548 g_signal_connect (sess->session, "on-ssrc-validated",
549 (GCallback) on_ssrc_validated, sess);
550 g_signal_connect (sess->session, "on-ssrc-active",
551 (GCallback) on_ssrc_active, sess);
552 g_signal_connect (sess->session, "on-ssrc-sdes",
553 (GCallback) on_ssrc_sdes, sess);
554 g_signal_connect (sess->session, "on-bye-ssrc",
555 (GCallback) on_bye_ssrc, sess);
556 g_signal_connect (sess->session, "on-bye-timeout",
557 (GCallback) on_bye_timeout, sess);
558 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
559 g_signal_connect (sess->session, "on-sender-timeout",
560 (GCallback) on_sender_timeout, sess);
562 gst_bin_add (GST_BIN_CAST (rtpbin), session);
563 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
565 GST_OBJECT_LOCK (rtpbin);
566 target = GST_STATE_TARGET (rtpbin);
567 GST_OBJECT_UNLOCK (rtpbin);
569 /* change state only to what's needed */
570 gst_element_set_state (demux, target);
571 gst_element_set_state (session, target);
578 g_warning ("gstrtpbin: could not create gstrtpsession element");
583 gst_object_unref (session);
584 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
590 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
594 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
596 gst_element_set_locked_state (sess->demux, TRUE);
597 gst_element_set_locked_state (sess->session, TRUE);
599 gst_element_set_state (sess->demux, GST_STATE_NULL);
600 gst_element_set_state (sess->session, GST_STATE_NULL);
602 if (sess->recv_rtp_sink != NULL) {
603 gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
604 gst_object_unref (sess->recv_rtp_sink);
606 if (sess->recv_rtp_src != NULL)
607 gst_object_unref (sess->recv_rtp_src);
608 if (sess->recv_rtcp_sink != NULL) {
609 gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
610 gst_object_unref (sess->recv_rtcp_sink);
612 if (sess->sync_src != NULL)
613 gst_object_unref (sess->sync_src);
614 if (sess->send_rtp_sink != NULL) {
615 gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
616 gst_object_unref (sess->send_rtp_sink);
618 if (sess->send_rtp_src != NULL)
619 gst_object_unref (sess->send_rtp_src);
620 if (sess->send_rtcp_src != NULL) {
621 gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
622 gst_object_unref (sess->send_rtcp_src);
625 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
626 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
628 /* remove any references in bin->clients to the streams in sess->streams */
629 client_walk = bin->clients;
630 while (client_walk) {
631 GSList *client_node = client_walk;
632 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
633 GSList *stream_walk = client->streams;
635 while (stream_walk) {
636 GSList *stream_node = stream_walk;
637 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
640 stream_walk = g_slist_next (stream_walk);
642 for (inner_walk = sess->streams; inner_walk;
643 inner_walk = g_slist_next (inner_walk)) {
644 if ((GstRtpBinStream *) inner_walk->data == stream) {
645 client->streams = g_slist_delete_link (client->streams, stream_node);
651 client_walk = g_slist_next (client_walk);
653 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
654 && client->streams == 0));
655 if (client->nstreams == 0) {
656 free_client (client, bin);
657 bin->clients = g_slist_delete_link (bin->clients, client_node);
661 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
662 g_slist_free (sess->streams);
664 g_mutex_free (sess->lock);
665 g_hash_table_destroy (sess->ptmap);
670 /* get the payload type caps for the specific payload @pt in @session */
672 get_pt_map (GstRtpBinSession * session, guint pt)
674 GstCaps *caps = NULL;
677 GValue args[3] = { {0}, {0}, {0} };
679 GST_DEBUG ("searching pt %d in cache", pt);
681 GST_RTP_SESSION_LOCK (session);
683 /* first look in the cache */
684 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
692 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
694 /* not in cache, send signal to request caps */
695 g_value_init (&args[0], GST_TYPE_ELEMENT);
696 g_value_set_object (&args[0], bin);
697 g_value_init (&args[1], G_TYPE_UINT);
698 g_value_set_uint (&args[1], session->id);
699 g_value_init (&args[2], G_TYPE_UINT);
700 g_value_set_uint (&args[2], pt);
702 g_value_init (&ret, GST_TYPE_CAPS);
703 g_value_set_boxed (&ret, NULL);
705 GST_RTP_SESSION_UNLOCK (session);
707 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
709 GST_RTP_SESSION_LOCK (session);
711 g_value_unset (&args[0]);
712 g_value_unset (&args[1]);
713 g_value_unset (&args[2]);
715 /* look in the cache again because we let the lock go */
716 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
719 g_value_unset (&ret);
723 caps = (GstCaps *) g_value_dup_boxed (&ret);
724 g_value_unset (&ret);
728 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
730 /* store in cache, take additional ref */
731 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
732 gst_caps_ref (caps));
735 GST_RTP_SESSION_UNLOCK (session);
742 GST_RTP_SESSION_UNLOCK (session);
743 GST_DEBUG ("no pt map could be obtained");
749 return_true (gpointer key, gpointer value, gpointer user_data)
755 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
757 GSList *clients, *streams;
759 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
761 GST_RTP_BIN_LOCK (rtpbin);
762 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
763 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
765 /* reset sync on all streams for this client */
766 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
767 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
769 /* make use require a new SR packet for this stream before we attempt new
771 stream->have_sync = FALSE;
772 stream->rt_delta = 0;
775 GST_RTP_BIN_UNLOCK (rtpbin);
779 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
781 GSList *sessions, *streams;
783 GST_RTP_BIN_LOCK (bin);
784 GST_DEBUG_OBJECT (bin, "clearing pt map");
785 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
786 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
788 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
789 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
791 GST_RTP_SESSION_LOCK (session);
792 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
794 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
795 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
797 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
798 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
800 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
802 GST_RTP_SESSION_UNLOCK (session);
804 GST_RTP_BIN_UNLOCK (bin);
807 gst_rtp_bin_reset_sync (bin);
811 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
813 RTPSession *internal_session = NULL;
814 GstRtpBinSession *session;
816 GST_RTP_BIN_LOCK (bin);
817 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
819 session = find_session_by_id (bin, (gint) session_id);
821 g_object_get (session->session, "internal-session", &internal_session,
824 GST_RTP_BIN_UNLOCK (bin);
826 return internal_session;
830 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
831 const gchar * name, const GValue * value)
833 GSList *sessions, *streams;
835 GST_RTP_BIN_LOCK (bin);
836 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
837 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
839 GST_RTP_SESSION_LOCK (session);
840 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
841 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
843 g_object_set_property (G_OBJECT (stream->buffer), name, value);
845 GST_RTP_SESSION_UNLOCK (session);
847 GST_RTP_BIN_UNLOCK (bin);
850 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
851 static GstRtpBinClient *
852 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
854 GstRtpBinClient *result = NULL;
857 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
858 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
860 if (len != client->cname_len)
863 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
864 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
871 /* nothing found, create one */
872 if (result == NULL) {
873 result = g_new0 (GstRtpBinClient, 1);
874 result->cname = g_strndup ((gchar *) data, len);
875 result->cname_len = len;
876 bin->clients = g_slist_prepend (bin->clients, result);
877 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
884 free_client (GstRtpBinClient * client, GstRtpBin * bin)
886 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
887 g_slist_free (client->streams);
888 g_free (client->cname);
893 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
898 GstClockTime base_time, rt, clock_time;
900 GST_OBJECT_LOCK (bin);
901 if ((clock = GST_ELEMENT_CLOCK (bin))) {
902 base_time = GST_ELEMENT_CAST (bin)->base_time;
903 gst_object_ref (clock);
904 GST_OBJECT_UNLOCK (bin);
906 clock_time = gst_clock_get_time (clock);
908 if (bin->use_pipeline_clock) {
913 /* get current NTP time */
914 g_get_current_time (¤t);
915 ntpns = GST_TIMEVAL_TO_TIME (current);
918 /* add constant to convert from 1970 based time to 1900 based time */
919 ntpns += (2208988800LL * GST_SECOND);
921 /* get current clock time and convert to running time */
922 rt = clock_time - base_time;
924 gst_object_unref (clock);
926 GST_OBJECT_UNLOCK (bin);
937 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
940 gint64 prev_ts_offset;
942 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
944 /* delta changed, see how much */
945 if (prev_ts_offset != ts_offset) {
948 diff = prev_ts_offset - ts_offset;
950 GST_DEBUG_OBJECT (bin,
951 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
952 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
954 /* only change diff when it changed more than 4 milliseconds. This
955 * compensates for rounding errors in NTP to RTP timestamp
957 if (ABS (diff) > 4 * GST_MSECOND) {
958 if (ABS (diff) < (3 * GST_SECOND)) {
959 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
961 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
964 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
967 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
968 stream->ssrc, ts_offset);
971 /* associate a stream to the given CNAME. This will make sure all streams for
972 * that CNAME are synchronized together.
973 * Must be called with GST_RTP_BIN_LOCK */
975 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
976 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
977 guint64 base_rtptime, guint64 base_time, guint clock_rate)
979 GstRtpBinClient *client;
984 GstClockTime running_time;
986 gint64 ntpdiff, rtdiff;
989 /* first find or create the CNAME */
990 client = get_client (bin, len, data, &created);
992 /* find stream in the client */
993 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
994 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
996 if (ostream == stream)
999 /* not found, add it to the list */
1001 GST_DEBUG_OBJECT (bin,
1002 "new association of SSRC %08x with client %p with CNAME %s",
1003 stream->ssrc, client, client->cname);
1004 client->streams = g_slist_prepend (client->streams, stream);
1007 GST_DEBUG_OBJECT (bin,
1008 "found association of SSRC %08x with client %p with CNAME %s",
1009 stream->ssrc, client, client->cname);
1012 /* Take the extended rtptime we found in the SR packet and map it to the
1013 * local rtptime. The local rtp time is used to construct timestamps on the
1014 * buffers so we will calculate what running_time corresponds to the RTP
1015 * timestamp in the SR packet. */
1016 local_rtp = last_extrtptime - base_rtptime;
1018 GST_DEBUG_OBJECT (bin,
1019 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1020 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", base_rtptime,
1021 last_extrtptime, local_rtp, clock_rate);
1023 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1024 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1025 * into a corresponding gstreamer timestamp. Note that the base_time also
1026 * contains the drift between sender and receiver. */
1027 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1028 local_rt += base_time;
1030 /* convert ntptime to unix time since 1900 */
1031 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1032 (G_GINT64_CONSTANT (1) << 32));
1034 stream->have_sync = TRUE;
1036 GST_DEBUG_OBJECT (bin,
1037 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1038 local_rt, last_unix);
1040 /* recalc inter stream playout offset, but only if there is more than one
1041 * stream or we're doing NTP sync. */
1042 if (bin->ntp_sync) {
1043 /* For NTP sync we need to first get a snapshot of running_time and NTP
1044 * time. We know at what running_time we play a certain RTP time, we also
1045 * calculated when we would play the RTP time in the SR packet. Now we need
1046 * to know how the running_time and the NTP time relate to eachother. */
1047 get_current_times (bin, &running_time, &ntpnstime);
1049 /* see how far away the NTP time is. This is the difference between the
1050 * current NTP time and the NTP time in the last SR packet. */
1051 ntpdiff = ntpnstime - last_unix;
1052 /* see how far away the running_time is. This is the difference between the
1053 * current running_time and the running_time of the RTP timestamp in the
1054 * last SR packet. */
1055 rtdiff = running_time - local_rt;
1057 GST_DEBUG_OBJECT (bin,
1058 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1059 ntpnstime, last_unix);
1060 GST_DEBUG_OBJECT (bin,
1061 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1064 /* combine to get the final diff to apply to the running_time */
1065 stream->rt_delta = rtdiff - ntpdiff;
1067 stream_set_ts_offset (bin, stream, stream->rt_delta);
1068 } else if (client->nstreams > 1) {
1071 /* calculate delta between server and receiver. last_unix is created by
1072 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1073 * delta expresses the difference to our timeline and the server timeline. The
1074 * difference in itself doesn't mean much but we can combine the delta of
1075 * multiple streams to create a stream specific offset. */
1076 stream->rt_delta = last_unix - local_rt;
1078 /* calculate the min of all deltas, ignoring streams that did not yet have a
1079 * valid rt_delta because we did not yet receive an SR packet for those
1081 * We calculate the mininum because we would like to only apply positive
1082 * offsets to streams, delaying their playback instead of trying to speed up
1083 * other streams (which might be imposible when we have to create negative
1085 * The stream that has the smallest diff is selected as the reference stream,
1086 * all other streams will have a positive offset to this difference. */
1088 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1089 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1091 if (!ostream->have_sync)
1094 if (ostream->rt_delta < min)
1095 min = ostream->rt_delta;
1098 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
1101 /* calculate offsets for each stream */
1102 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1103 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1106 /* ignore streams for which we didn't receive an SR packet yet, we
1107 * can't synchronize them yet. We can however sync other streams just
1109 if (!ostream->have_sync)
1112 /* calculate offset to our reference stream, this should always give a
1113 * positive number. */
1114 ts_offset = ostream->rt_delta - min;
1116 stream_set_ts_offset (bin, ostream, ts_offset);
1122 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1123 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1124 (b) = gst_rtcp_packet_move_to_next ((packet)))
1126 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1127 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1128 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1130 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1131 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1132 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1135 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1136 GstRtpBinStream * stream)
1139 GstRTCPPacket packet;
1142 gboolean have_sr, have_sdes;
1144 guint64 base_rtptime;
1152 GST_DEBUG_OBJECT (bin, "sync handler called");
1154 /* get the last relation between the rtp timestamps and the gstreamer
1155 * timestamps. We get this info directly from the jitterbuffer which
1156 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1157 * what the current situation is. */
1159 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1160 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1161 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1163 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1164 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1168 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
1169 /* first packet must be SR or RR or else the validate would have failed */
1170 switch (gst_rtcp_packet_get_type (&packet)) {
1171 case GST_RTCP_TYPE_SR:
1172 /* only parse first. There is only supposed to be one SR in the packet
1173 * but we will deal with malformed packets gracefully */
1176 /* get NTP and RTP times */
1177 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1180 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1181 /* ignore SR that is not ours */
1182 if (ssrc != stream->ssrc)
1187 case GST_RTCP_TYPE_SDES:
1189 gboolean more_items, more_entries;
1191 /* only deal with first SDES, there is only supposed to be one SDES in
1192 * the RTCP packet but we deal with bad packets gracefully. Also bail
1193 * out if we have not seen an SR item yet. */
1194 if (have_sdes || !have_sr)
1197 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1198 /* skip items that are not about the SSRC of the sender */
1199 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1202 /* find the CNAME entry */
1203 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1204 GstRTCPSDESType type;
1208 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1210 if (type == GST_RTCP_SDES_CNAME) {
1211 GST_RTP_BIN_LOCK (bin);
1212 /* associate the stream to CNAME */
1213 gst_rtp_bin_associate (bin, stream, len, data,
1214 ntptime, extrtptime, base_rtptime, base_time, clock_rate);
1215 GST_RTP_BIN_UNLOCK (bin);
1223 /* we can ignore these packets */
1229 /* create a new stream with @ssrc in @session. Must be called with
1230 * RTP_SESSION_LOCK. */
1231 static GstRtpBinStream *
1232 create_stream (GstRtpBinSession * session, guint32 ssrc)
1234 GstElement *buffer, *demux = NULL;
1235 GstRtpBinStream *stream;
1239 rtpbin = session->bin;
1241 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
1242 goto no_jitterbuffer;
1244 if (!rtpbin->ignore_pt)
1245 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
1249 stream = g_new0 (GstRtpBinStream, 1);
1250 stream->ssrc = ssrc;
1251 stream->bin = rtpbin;
1252 stream->session = session;
1253 stream->buffer = buffer;
1254 stream->demux = demux;
1256 stream->have_sync = FALSE;
1257 stream->rt_delta = 0;
1258 stream->percent = 100;
1259 session->streams = g_slist_prepend (session->streams, stream);
1261 /* provide clock_rate to the jitterbuffer when needed */
1262 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1263 (GCallback) pt_map_requested, session);
1264 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1265 (GCallback) on_npt_stop, stream);
1267 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1268 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1270 /* configure latency and packet lost */
1271 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1272 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1273 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1275 if (!rtpbin->ignore_pt)
1276 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1277 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1281 gst_element_link (buffer, demux);
1283 if (rtpbin->buffering) {
1286 GST_INFO_OBJECT (rtpbin,
1287 "bin is buffering, set jitterbuffer as not active");
1288 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1292 GST_OBJECT_LOCK (rtpbin);
1293 target = GST_STATE_TARGET (rtpbin);
1294 GST_OBJECT_UNLOCK (rtpbin);
1296 /* from sink to source */
1298 gst_element_set_state (demux, target);
1300 gst_element_set_state (buffer, target);
1307 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1312 gst_object_unref (buffer);
1313 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1319 free_stream (GstRtpBinStream * stream)
1321 GstRtpBinSession *session;
1323 session = stream->session;
1325 if (stream->demux) {
1326 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1327 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1328 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1330 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1331 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1332 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1334 gst_element_set_locked_state (stream->demux, TRUE);
1335 gst_element_set_locked_state (stream->buffer, TRUE);
1337 gst_element_set_state (stream->demux, GST_STATE_NULL);
1338 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1340 /* now remove this signal, we need this while going to NULL because it to
1341 * do some cleanups */
1343 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1345 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1347 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1352 /* GObject vmethods */
1353 static void gst_rtp_bin_dispose (GObject * object);
1354 static void gst_rtp_bin_finalize (GObject * object);
1355 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1356 const GValue * value, GParamSpec * pspec);
1357 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1358 GValue * value, GParamSpec * pspec);
1360 /* GstElement vmethods */
1361 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1362 GstStateChange transition);
1363 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1364 GstPadTemplate * templ, const gchar * name);
1365 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1366 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1368 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1371 gst_rtp_bin_base_init (gpointer klass)
1373 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1376 gst_element_class_add_pad_template (element_class,
1377 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1378 gst_element_class_add_pad_template (element_class,
1379 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1380 gst_element_class_add_pad_template (element_class,
1381 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1384 gst_element_class_add_pad_template (element_class,
1385 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1386 gst_element_class_add_pad_template (element_class,
1387 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1388 gst_element_class_add_pad_template (element_class,
1389 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1391 gst_element_class_set_details_simple (element_class, "RTP Bin",
1392 "Filter/Network/RTP",
1393 "Real-Time Transport Protocol bin",
1394 "Wim Taymans <wim.taymans@gmail.com>");
1398 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1400 GObjectClass *gobject_class;
1401 GstElementClass *gstelement_class;
1402 GstBinClass *gstbin_class;
1404 gobject_class = (GObjectClass *) klass;
1405 gstelement_class = (GstElementClass *) klass;
1406 gstbin_class = (GstBinClass *) klass;
1408 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1410 gobject_class->dispose = gst_rtp_bin_dispose;
1411 gobject_class->finalize = gst_rtp_bin_finalize;
1412 gobject_class->set_property = gst_rtp_bin_set_property;
1413 gobject_class->get_property = gst_rtp_bin_get_property;
1415 g_object_class_install_property (gobject_class, PROP_LATENCY,
1416 g_param_spec_uint ("latency", "Buffer latency in ms",
1417 "Default amount of ms to buffer in the jitterbuffers", 0,
1418 G_MAXUINT, DEFAULT_LATENCY_MS,
1419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1422 * GstRtpBin::request-pt-map:
1423 * @rtpbin: the object which received the signal
1424 * @session: the session
1427 * Request the payload type as #GstCaps for @pt in @session.
1429 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1430 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1431 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1432 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1433 G_TYPE_UINT, G_TYPE_UINT);
1436 * GstRtpBin::payload-type-change:
1437 * @rtpbin: the object which received the signal
1438 * @session: the session
1441 * Signal that the current payload type changed to @pt in @session.
1445 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1446 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1447 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1448 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1449 G_TYPE_UINT, G_TYPE_UINT);
1452 * GstRtpBin::clear-pt-map:
1453 * @rtpbin: the object which received the signal
1455 * Clear all previously cached pt-mapping obtained with
1456 * #GstRtpBin::request-pt-map.
1458 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1459 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1460 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1461 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1465 * GstRtpBin::reset-sync:
1466 * @rtpbin: the object which received the signal
1468 * Reset all currently configured lip-sync parameters and require new SR
1469 * packets for all streams before lip-sync is attempted again.
1471 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1472 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1473 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1474 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1478 * GstRtpBin::get-internal-session:
1479 * @rtpbin: the object which received the signal
1480 * @id: the session id
1482 * Request the internal RTPSession object as #GObject in session @id.
1484 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1485 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1486 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1487 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1488 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1491 * GstRtpBin::on-new-ssrc:
1492 * @rtpbin: the object which received the signal
1493 * @session: the session
1496 * Notify of a new SSRC that entered @session.
1498 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1499 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1500 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1501 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1502 G_TYPE_UINT, G_TYPE_UINT);
1504 * GstRtpBin::on-ssrc-collision:
1505 * @rtpbin: the object which received the signal
1506 * @session: the session
1509 * Notify when we have an SSRC collision
1511 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1512 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1513 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1514 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1515 G_TYPE_UINT, G_TYPE_UINT);
1517 * GstRtpBin::on-ssrc-validated:
1518 * @rtpbin: the object which received the signal
1519 * @session: the session
1522 * Notify of a new SSRC that became validated.
1524 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1525 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1526 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1527 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1528 G_TYPE_UINT, G_TYPE_UINT);
1530 * GstRtpBin::on-ssrc-active:
1531 * @rtpbin: the object which received the signal
1532 * @session: the session
1535 * Notify of a SSRC that is active, i.e., sending RTCP.
1537 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1538 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1539 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1540 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1541 G_TYPE_UINT, G_TYPE_UINT);
1543 * GstRtpBin::on-ssrc-sdes:
1544 * @rtpbin: the object which received the signal
1545 * @session: the session
1548 * Notify of a SSRC that is active, i.e., sending RTCP.
1550 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1551 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1552 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1553 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1554 G_TYPE_UINT, G_TYPE_UINT);
1557 * GstRtpBin::on-bye-ssrc:
1558 * @rtpbin: the object which received the signal
1559 * @session: the session
1562 * Notify of an SSRC that became inactive because of a BYE packet.
1564 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1565 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1566 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1567 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1568 G_TYPE_UINT, G_TYPE_UINT);
1570 * GstRtpBin::on-bye-timeout:
1571 * @rtpbin: the object which received the signal
1572 * @session: the session
1575 * Notify of an SSRC that has timed out because of BYE
1577 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1578 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1579 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1580 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1581 G_TYPE_UINT, G_TYPE_UINT);
1583 * GstRtpBin::on-timeout:
1584 * @rtpbin: the object which received the signal
1585 * @session: the session
1588 * Notify of an SSRC that has timed out
1590 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1591 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1592 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1593 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1594 G_TYPE_UINT, G_TYPE_UINT);
1596 * GstRtpBin::on-sender-timeout:
1597 * @rtpbin: the object which received the signal
1598 * @session: the session
1601 * Notify of a sender SSRC that has timed out and became a receiver
1603 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1604 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1605 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1606 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1607 G_TYPE_UINT, G_TYPE_UINT);
1610 * GstRtpBin::on-npt-stop:
1611 * @rtpbin: the object which received the signal
1612 * @session: the session
1615 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1617 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1618 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1619 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1620 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1621 G_TYPE_UINT, G_TYPE_UINT);
1623 g_object_class_install_property (gobject_class, PROP_SDES,
1624 g_param_spec_boxed ("sdes", "SDES",
1625 "The SDES items of this session",
1626 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1628 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1629 g_param_spec_boolean ("do-lost", "Do Lost",
1630 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1631 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1633 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1634 g_param_spec_boolean ("autoremove", "Auto Remove",
1635 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1638 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1639 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1640 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1641 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1643 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1644 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1645 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1646 DEFAULT_USE_PIPELINE_CLOCK,
1647 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1649 * GstRtpBin::buffer-mode:
1651 * Control the buffering and timestamping mode used by the jitterbuffer.
1655 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1656 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1657 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1658 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1660 * GstRtpBin::ntp-sync:
1662 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1663 * between the receivers and the senders (such as when using ntpd) this option
1664 * can be used to synchronize receivers on multiple machines.
1668 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1669 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1670 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1671 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1673 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1674 gstelement_class->request_new_pad =
1675 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1676 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1678 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1680 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1681 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1682 klass->get_internal_session =
1683 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1685 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1689 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1693 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1694 rtpbin->priv->bin_lock = g_mutex_new ();
1695 rtpbin->priv->dyn_lock = g_mutex_new ();
1697 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1698 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1699 rtpbin->do_lost = DEFAULT_DO_LOST;
1700 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1701 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1702 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1703 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1704 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1706 /* some default SDES entries */
1707 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1708 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1709 "cname", G_TYPE_STRING, str,
1710 "name", G_TYPE_STRING, g_get_real_name (),
1711 "tool", G_TYPE_STRING, "GStreamer", NULL);
1716 gst_rtp_bin_dispose (GObject * object)
1720 rtpbin = GST_RTP_BIN (object);
1722 GST_DEBUG_OBJECT (object, "freeing sessions");
1723 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1724 g_slist_free (rtpbin->sessions);
1725 rtpbin->sessions = NULL;
1726 GST_DEBUG_OBJECT (object, "freeing clients");
1727 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1728 g_slist_free (rtpbin->clients);
1729 rtpbin->clients = NULL;
1731 G_OBJECT_CLASS (parent_class)->dispose (object);
1735 gst_rtp_bin_finalize (GObject * object)
1739 rtpbin = GST_RTP_BIN (object);
1742 gst_structure_free (rtpbin->sdes);
1744 g_mutex_free (rtpbin->priv->bin_lock);
1745 g_mutex_free (rtpbin->priv->dyn_lock);
1747 G_OBJECT_CLASS (parent_class)->finalize (object);
1752 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1759 GST_RTP_BIN_LOCK (bin);
1761 GST_OBJECT_LOCK (bin);
1763 gst_structure_free (bin->sdes);
1764 bin->sdes = gst_structure_copy (sdes);
1766 /* store in all sessions */
1767 for (item = bin->sessions; item; item = g_slist_next (item))
1768 g_object_set (item->data, "sdes", sdes, NULL);
1769 GST_OBJECT_UNLOCK (bin);
1771 GST_RTP_BIN_UNLOCK (bin);
1774 static GstStructure *
1775 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1777 GstStructure *result;
1779 GST_OBJECT_LOCK (bin);
1780 result = gst_structure_copy (bin->sdes);
1781 GST_OBJECT_UNLOCK (bin);
1787 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1788 const GValue * value, GParamSpec * pspec)
1792 rtpbin = GST_RTP_BIN (object);
1796 GST_RTP_BIN_LOCK (rtpbin);
1797 rtpbin->latency_ms = g_value_get_uint (value);
1798 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1799 GST_RTP_BIN_UNLOCK (rtpbin);
1800 /* propagate the property down to the jitterbuffer */
1801 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1804 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1807 GST_RTP_BIN_LOCK (rtpbin);
1808 rtpbin->do_lost = g_value_get_boolean (value);
1809 GST_RTP_BIN_UNLOCK (rtpbin);
1810 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1813 rtpbin->ntp_sync = g_value_get_boolean (value);
1815 case PROP_IGNORE_PT:
1816 rtpbin->ignore_pt = g_value_get_boolean (value);
1818 case PROP_AUTOREMOVE:
1819 rtpbin->priv->autoremove = g_value_get_boolean (value);
1821 case PROP_USE_PIPELINE_CLOCK:
1824 GST_RTP_BIN_LOCK (rtpbin);
1825 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
1826 for (sessions = rtpbin->sessions; sessions;
1827 sessions = g_slist_next (sessions)) {
1828 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1830 g_object_set (G_OBJECT (session->session),
1831 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
1833 GST_RTP_BIN_UNLOCK (rtpbin);
1836 case PROP_BUFFER_MODE:
1837 GST_RTP_BIN_LOCK (rtpbin);
1838 rtpbin->buffer_mode = g_value_get_enum (value);
1839 GST_RTP_BIN_UNLOCK (rtpbin);
1840 /* propagate the property down to the jitterbuffer */
1841 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
1844 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1850 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1851 GValue * value, GParamSpec * pspec)
1855 rtpbin = GST_RTP_BIN (object);
1859 GST_RTP_BIN_LOCK (rtpbin);
1860 g_value_set_uint (value, rtpbin->latency_ms);
1861 GST_RTP_BIN_UNLOCK (rtpbin);
1864 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
1867 GST_RTP_BIN_LOCK (rtpbin);
1868 g_value_set_boolean (value, rtpbin->do_lost);
1869 GST_RTP_BIN_UNLOCK (rtpbin);
1871 case PROP_IGNORE_PT:
1872 g_value_set_boolean (value, rtpbin->ignore_pt);
1875 g_value_set_boolean (value, rtpbin->ntp_sync);
1877 case PROP_AUTOREMOVE:
1878 g_value_set_boolean (value, rtpbin->priv->autoremove);
1880 case PROP_BUFFER_MODE:
1881 g_value_set_enum (value, rtpbin->buffer_mode);
1883 case PROP_USE_PIPELINE_CLOCK:
1884 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
1887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1893 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1897 rtpbin = GST_RTP_BIN (bin);
1899 switch (GST_MESSAGE_TYPE (message)) {
1900 case GST_MESSAGE_ELEMENT:
1902 const GstStructure *s = gst_message_get_structure (message);
1904 /* we change the structure name and add the session ID to it */
1905 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
1906 GstRtpBinSession *sess;
1908 /* find the session we set it as object data */
1909 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1910 "GstRTPBin.session");
1912 if (G_LIKELY (sess)) {
1913 message = gst_message_make_writable (message);
1914 s = gst_message_get_structure (message);
1915 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1919 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1922 case GST_MESSAGE_BUFFERING:
1925 gint min_percent = 100;
1926 GSList *sessions, *streams;
1927 GstRtpBinStream *stream;
1928 gboolean change = FALSE, active = FALSE;
1929 GstClockTime min_out_time;
1931 gst_message_parse_buffering (message, &percent);
1934 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1935 "GstRTPBin.stream");
1937 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
1939 /* get the stream */
1940 if (G_LIKELY (stream)) {
1941 GST_RTP_BIN_LOCK (rtpbin);
1942 /* fill in the percent */
1943 stream->percent = percent;
1945 /* calculate the min value for all streams */
1946 for (sessions = rtpbin->sessions; sessions;
1947 sessions = g_slist_next (sessions)) {
1948 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1950 GST_RTP_SESSION_LOCK (session);
1951 if (session->streams) {
1952 for (streams = session->streams; streams;
1953 streams = g_slist_next (streams)) {
1954 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1956 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
1959 /* find min percent */
1960 if (min_percent > stream->percent)
1961 min_percent = stream->percent;
1964 GST_INFO_OBJECT (bin,
1965 "session has no streams, setting min_percent to 0");
1968 GST_RTP_SESSION_UNLOCK (session);
1970 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
1972 if (rtpbin->buffering) {
1973 if (min_percent == 100) {
1974 rtpbin->buffering = FALSE;
1979 if (min_percent < 100) {
1980 /* pause the streams */
1981 rtpbin->buffering = TRUE;
1986 GST_RTP_BIN_UNLOCK (rtpbin);
1988 gst_message_unref (message);
1990 /* make a new buffering message with the min value */
1992 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
1994 if (G_UNLIKELY (change)) {
1996 guint64 running_time = 0;
1999 /* figure out the running time when we have a clock */
2000 if (G_LIKELY ((clock =
2001 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2002 guint64 now, base_time;
2004 now = gst_clock_get_time (clock);
2005 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2006 running_time = now - base_time;
2008 GST_DEBUG_OBJECT (bin,
2009 "running time now %" GST_TIME_FORMAT,
2010 GST_TIME_ARGS (running_time));
2012 GST_RTP_BIN_LOCK (rtpbin);
2014 /* when we reactivate, calculate the offsets so that all streams have
2015 * an output time that is at least as big as the running_time */
2018 if (running_time > rtpbin->buffer_start) {
2019 offset = running_time - rtpbin->buffer_start;
2020 if (offset >= rtpbin->latency_ns)
2021 offset -= rtpbin->latency_ns;
2027 /* pause all streams */
2029 for (sessions = rtpbin->sessions; sessions;
2030 sessions = g_slist_next (sessions)) {
2031 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2033 GST_RTP_SESSION_LOCK (session);
2034 for (streams = session->streams; streams;
2035 streams = g_slist_next (streams)) {
2036 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2037 GstElement *element = stream->buffer;
2040 g_signal_emit_by_name (element, "set-active", active, offset,
2044 g_object_get (element, "percent", &stream->percent, NULL);
2048 if (min_out_time == -1 || last_out < min_out_time)
2049 min_out_time = last_out;
2052 GST_DEBUG_OBJECT (bin,
2053 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2054 GST_TIME_FORMAT ", percent %d", element, active,
2055 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2058 GST_RTP_SESSION_UNLOCK (session);
2060 GST_DEBUG_OBJECT (bin,
2061 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2063 /* the buffer_start is the min out time of all paused jitterbuffers */
2065 rtpbin->buffer_start = min_out_time;
2067 GST_RTP_BIN_UNLOCK (rtpbin);
2070 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2075 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2081 static GstStateChangeReturn
2082 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2084 GstStateChangeReturn res;
2086 GstRtpBinPrivate *priv;
2088 rtpbin = GST_RTP_BIN (element);
2089 priv = rtpbin->priv;
2091 switch (transition) {
2092 case GST_STATE_CHANGE_NULL_TO_READY:
2094 case GST_STATE_CHANGE_READY_TO_PAUSED:
2095 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2096 g_atomic_int_set (&priv->shutdown, 0);
2098 case GST_STATE_CHANGE_PAUSED_TO_READY:
2099 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2100 g_atomic_int_set (&priv->shutdown, 1);
2101 /* wait for all callbacks to end by taking the lock. No new callbacks will
2102 * be able to happen as we set the shutdown flag. */
2103 GST_RTP_BIN_DYN_LOCK (rtpbin);
2104 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2105 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2111 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2113 switch (transition) {
2114 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2116 case GST_STATE_CHANGE_PAUSED_TO_READY:
2118 case GST_STATE_CHANGE_READY_TO_NULL:
2126 /* a new pad (SSRC) was created in @session. This signal is emited from the
2127 * payload demuxer. */
2129 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2130 GstRtpBinStream * stream)
2133 GstElementClass *klass;
2134 GstPadTemplate *templ;
2138 rtpbin = stream->bin;
2140 GST_DEBUG ("new payload pad %d", pt);
2142 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2144 /* ghost the pad to the parent */
2145 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2146 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2147 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2148 stream->session->id, stream->ssrc, pt);
2149 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2151 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2153 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2154 gst_pad_set_active (gpad, TRUE);
2155 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2157 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2163 GST_DEBUG ("ignoring, we are shutting down");
2169 payload_pad_removed (GstElement * element, GstPad * pad,
2170 GstRtpBinStream * stream)
2175 rtpbin = stream->bin;
2177 GST_DEBUG ("payload pad removed");
2179 GST_RTP_BIN_DYN_LOCK (rtpbin);
2180 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2181 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2183 gst_pad_set_active (gpad, FALSE);
2184 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2186 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2190 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2195 rtpbin = session->bin;
2197 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2200 caps = get_pt_map (session, pt);
2209 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2215 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2217 GST_DEBUG_OBJECT (session->bin,
2218 "emiting signal for pt type changed to %d in session %d", pt,
2221 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2222 0, session->id, pt);
2225 /* emited when caps changed for the session */
2227 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2232 const GstStructure *s;
2236 g_object_get (pad, "caps", &caps, NULL);
2241 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2243 s = gst_caps_get_structure (caps, 0);
2245 /* get payload, finish when it's not there */
2246 if (!gst_structure_get_int (s, "payload", &payload))
2249 GST_RTP_SESSION_LOCK (session);
2250 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2251 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2252 GST_RTP_SESSION_UNLOCK (session);
2255 /* a new pad (SSRC) was created in @session */
2257 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2258 GstRtpBinSession * session)
2261 GstRtpBinStream *stream;
2262 GstPad *sinkpad, *srcpad;
2265 rtpbin = session->bin;
2267 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2268 GST_DEBUG_PAD_NAME (pad));
2270 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2272 GST_RTP_SESSION_LOCK (session);
2274 /* create new stream */
2275 stream = create_stream (session, ssrc);
2279 /* get pad and link */
2280 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2281 padname = g_strdup_printf ("src_%d", ssrc);
2282 srcpad = gst_element_get_static_pad (element, padname);
2284 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2285 gst_pad_link (srcpad, sinkpad);
2286 gst_object_unref (sinkpad);
2287 gst_object_unref (srcpad);
2289 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2290 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2291 srcpad = gst_element_get_static_pad (element, padname);
2293 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2294 gst_pad_link (srcpad, sinkpad);
2295 gst_object_unref (sinkpad);
2296 gst_object_unref (srcpad);
2298 /* connect to the RTCP sync signal from the jitterbuffer */
2299 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2300 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2301 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2303 if (stream->demux) {
2304 /* connect to the new-pad signal of the payload demuxer, this will expose the
2305 * new pad by ghosting it. */
2306 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2307 "new-payload-type", (GCallback) new_payload_found, stream);
2308 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2309 "pad-removed", (GCallback) payload_pad_removed, stream);
2311 /* connect to the request-pt-map signal. This signal will be emited by the
2312 * demuxer so that it can apply a proper caps on the buffers for the
2314 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2315 "request-pt-map", (GCallback) pt_map_requested, session);
2316 /* connect to the signal so it can be forwarded. */
2317 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2318 "payload-type-change", (GCallback) payload_type_change, session);
2320 /* add gstrtpjitterbuffer src pad to pads */
2321 GstElementClass *klass;
2322 GstPadTemplate *templ;
2326 pad = gst_element_get_static_pad (stream->buffer, "src");
2328 /* ghost the pad to the parent */
2329 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2330 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2331 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2332 stream->session->id, stream->ssrc, 255);
2333 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2336 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2337 gst_pad_set_active (gpad, TRUE);
2338 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2340 gst_object_unref (pad);
2343 GST_RTP_SESSION_UNLOCK (session);
2344 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2351 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2356 GST_RTP_SESSION_UNLOCK (session);
2357 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2358 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2363 /* Create a pad for receiving RTP for the session in @name. Must be called with
2367 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2371 GstRtpBinSession *session;
2372 GstPadLinkReturn lres;
2374 /* first get the session number */
2375 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2378 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2380 /* get or create session */
2381 session = find_session_by_id (rtpbin, sessid);
2383 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2384 /* create session now */
2385 session = create_session (rtpbin, sessid);
2386 if (session == NULL)
2390 /* check if pad was requested */
2391 if (session->recv_rtp_sink_ghost != NULL)
2392 return session->recv_rtp_sink_ghost;
2394 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2395 /* get recv_rtp pad and store */
2396 session->recv_rtp_sink =
2397 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2398 if (session->recv_rtp_sink == NULL)
2401 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2402 (GCallback) caps_changed, session);
2404 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2405 /* get srcpad, link to SSRCDemux */
2406 session->recv_rtp_src =
2407 gst_element_get_static_pad (session->session, "recv_rtp_src");
2408 if (session->recv_rtp_src == NULL)
2411 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2412 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2413 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2414 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2415 gst_object_unref (sinkdpad);
2416 if (lres != GST_PAD_LINK_OK)
2419 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2420 session->demux_newpad_sig = g_signal_connect (session->demux,
2421 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2422 session->demux_padremoved_sig = g_signal_connect (session->demux,
2423 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2425 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2426 session->recv_rtp_sink_ghost =
2427 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2428 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2429 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2431 return session->recv_rtp_sink_ghost;
2436 g_warning ("gstrtpbin: invalid name given");
2441 /* create_session already warned */
2446 g_warning ("gstrtpbin: failed to get session pad");
2451 g_warning ("gstrtpbin: failed to link pads");
2457 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2459 if (session->demux_newpad_sig) {
2460 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2461 session->demux_newpad_sig = 0;
2463 if (session->demux_padremoved_sig) {
2464 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2465 session->demux_padremoved_sig = 0;
2467 if (session->recv_rtp_src) {
2468 gst_object_unref (session->recv_rtp_src);
2469 session->recv_rtp_src = NULL;
2471 if (session->recv_rtp_sink) {
2472 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2473 gst_object_unref (session->recv_rtp_sink);
2474 session->recv_rtp_sink = NULL;
2476 if (session->recv_rtp_sink_ghost) {
2477 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2478 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2479 session->recv_rtp_sink_ghost);
2480 session->recv_rtp_sink_ghost = NULL;
2484 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2488 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2492 GstRtpBinSession *session;
2494 GstPadLinkReturn lres;
2496 /* first get the session number */
2497 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2500 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2502 /* get or create the session */
2503 session = find_session_by_id (rtpbin, sessid);
2505 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2506 /* create session now */
2507 session = create_session (rtpbin, sessid);
2508 if (session == NULL)
2512 /* check if pad was requested */
2513 if (session->recv_rtcp_sink_ghost != NULL)
2514 return session->recv_rtcp_sink_ghost;
2516 /* get recv_rtp pad and store */
2517 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2518 session->recv_rtcp_sink =
2519 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2520 if (session->recv_rtcp_sink == NULL)
2523 /* get srcpad, link to SSRCDemux */
2524 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2525 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2526 if (session->sync_src == NULL)
2529 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2530 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2531 lres = gst_pad_link (session->sync_src, sinkdpad);
2532 gst_object_unref (sinkdpad);
2533 if (lres != GST_PAD_LINK_OK)
2536 session->recv_rtcp_sink_ghost =
2537 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2538 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2539 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2540 session->recv_rtcp_sink_ghost);
2542 return session->recv_rtcp_sink_ghost;
2547 g_warning ("gstrtpbin: invalid name given");
2552 /* create_session already warned */
2557 g_warning ("gstrtpbin: failed to get session pad");
2562 g_warning ("gstrtpbin: failed to link pads");
2568 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2570 if (session->recv_rtcp_sink_ghost) {
2571 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2572 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2573 session->recv_rtcp_sink_ghost);
2574 session->recv_rtcp_sink_ghost = NULL;
2576 if (session->sync_src) {
2577 /* releasing the request pad should also unref the sync pad */
2578 gst_object_unref (session->sync_src);
2579 session->sync_src = NULL;
2581 if (session->recv_rtcp_sink) {
2582 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2583 gst_object_unref (session->recv_rtcp_sink);
2584 session->recv_rtcp_sink = NULL;
2588 /* Create a pad for sending RTP for the session in @name. Must be called with
2592 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2596 GstRtpBinSession *session;
2597 GstElementClass *klass;
2599 /* first get the session number */
2600 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2603 /* get or create session */
2604 session = find_session_by_id (rtpbin, sessid);
2606 /* create session now */
2607 session = create_session (rtpbin, sessid);
2608 if (session == NULL)
2612 /* check if pad was requested */
2613 if (session->send_rtp_sink_ghost != NULL)
2614 return session->send_rtp_sink_ghost;
2616 /* get send_rtp pad and store */
2617 session->send_rtp_sink =
2618 gst_element_get_request_pad (session->session, "send_rtp_sink");
2619 if (session->send_rtp_sink == NULL)
2622 session->send_rtp_sink_ghost =
2623 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2624 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2625 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2628 session->send_rtp_src =
2629 gst_element_get_static_pad (session->session, "send_rtp_src");
2630 if (session->send_rtp_src == NULL)
2633 /* ghost the new source pad */
2634 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2635 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2636 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2637 session->send_rtp_src_ghost =
2638 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2639 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2640 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2643 return session->send_rtp_sink_ghost;
2648 g_warning ("gstrtpbin: invalid name given");
2653 /* create_session already warned */
2658 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2663 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2670 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2672 if (session->send_rtp_src_ghost) {
2673 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2674 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2675 session->send_rtp_src_ghost);
2676 session->send_rtp_src_ghost = NULL;
2678 if (session->send_rtp_src) {
2679 gst_object_unref (session->send_rtp_src);
2680 session->send_rtp_src = NULL;
2682 if (session->send_rtp_sink) {
2683 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2684 session->send_rtp_sink);
2685 gst_object_unref (session->send_rtp_sink);
2686 session->send_rtp_sink = NULL;
2688 if (session->send_rtp_sink_ghost) {
2689 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2690 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2691 session->send_rtp_sink_ghost);
2692 session->send_rtp_sink_ghost = NULL;
2696 /* Create a pad for sending RTCP for the session in @name. Must be called with
2700 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2703 GstRtpBinSession *session;
2705 /* first get the session number */
2706 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2709 /* get or create session */
2710 session = find_session_by_id (rtpbin, sessid);
2714 /* check if pad was requested */
2715 if (session->send_rtcp_src_ghost != NULL)
2716 return session->send_rtcp_src_ghost;
2718 /* get rtcp_src pad and store */
2719 session->send_rtcp_src =
2720 gst_element_get_request_pad (session->session, "send_rtcp_src");
2721 if (session->send_rtcp_src == NULL)
2724 session->send_rtcp_src_ghost =
2725 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2726 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2727 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2729 return session->send_rtcp_src_ghost;
2734 g_warning ("gstrtpbin: invalid name given");
2739 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2744 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2750 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2752 if (session->send_rtcp_src_ghost) {
2753 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2754 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2755 session->send_rtcp_src_ghost);
2756 session->send_rtcp_src_ghost = NULL;
2758 if (session->send_rtcp_src) {
2759 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2760 gst_object_unref (session->send_rtcp_src);
2761 session->send_rtcp_src = NULL;
2765 /* If the requested name is NULL we should create a name with
2766 * the session number assuming we want the lowest posible session
2767 * with a free pad like the template */
2769 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2771 gboolean name_found = FALSE;
2773 GstIterator *pad_it = NULL;
2774 gchar *pad_name = NULL;
2776 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2777 while (!name_found) {
2778 gboolean done = FALSE;
2780 pad_name = g_strdup_printf (templ->name_template, session++);
2781 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2786 switch (gst_iterator_next (pad_it, &data)) {
2787 case GST_ITERATOR_OK:
2792 pad = GST_PAD_CAST (data);
2793 name = gst_pad_get_name (pad);
2795 if (strcmp (name, pad_name) == 0) {
2800 gst_object_unref (pad);
2803 case GST_ITERATOR_ERROR:
2804 case GST_ITERATOR_RESYNC:
2805 /* restart iteration */
2810 case GST_ITERATOR_DONE:
2815 gst_iterator_free (pad_it);
2818 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2825 gst_rtp_bin_request_new_pad (GstElement * element,
2826 GstPadTemplate * templ, const gchar * name)
2829 GstElementClass *klass;
2832 gchar *pad_name = NULL;
2834 g_return_val_if_fail (templ != NULL, NULL);
2835 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2837 rtpbin = GST_RTP_BIN (element);
2838 klass = GST_ELEMENT_GET_CLASS (element);
2840 GST_RTP_BIN_LOCK (rtpbin);
2843 /* use a free pad name */
2844 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2846 /* use the provided name */
2847 pad_name = g_strdup (name);
2850 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2852 /* figure out the template */
2853 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2854 result = create_recv_rtp (rtpbin, templ, pad_name);
2855 } else if (templ == gst_element_class_get_pad_template (klass,
2856 "recv_rtcp_sink_%d")) {
2857 result = create_recv_rtcp (rtpbin, templ, pad_name);
2858 } else if (templ == gst_element_class_get_pad_template (klass,
2859 "send_rtp_sink_%d")) {
2860 result = create_send_rtp (rtpbin, templ, pad_name);
2861 } else if (templ == gst_element_class_get_pad_template (klass,
2862 "send_rtcp_src_%d")) {
2863 result = create_rtcp (rtpbin, templ, pad_name);
2865 goto wrong_template;
2868 GST_RTP_BIN_UNLOCK (rtpbin);
2876 GST_RTP_BIN_UNLOCK (rtpbin);
2877 g_warning ("gstrtpbin: this is not our template");
2883 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2885 GstRtpBinSession *session;
2888 g_return_if_fail (GST_IS_GHOST_PAD (pad));
2889 g_return_if_fail (GST_IS_RTP_BIN (element));
2891 rtpbin = GST_RTP_BIN (element);
2893 GST_RTP_BIN_LOCK (rtpbin);
2894 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2895 GST_DEBUG_PAD_NAME (pad));
2897 if (!(session = find_session_by_pad (rtpbin, pad)))
2900 if (session->recv_rtp_sink_ghost == pad) {
2901 remove_recv_rtp (rtpbin, session);
2902 } else if (session->recv_rtcp_sink_ghost == pad) {
2903 remove_recv_rtcp (rtpbin, session);
2904 } else if (session->send_rtp_sink_ghost == pad) {
2905 remove_send_rtp (rtpbin, session);
2906 } else if (session->send_rtcp_src_ghost == pad) {
2907 remove_rtcp (rtpbin, session);
2910 /* no more request pads, free the complete session */
2911 if (session->recv_rtp_sink_ghost == NULL
2912 && session->recv_rtcp_sink_ghost == NULL
2913 && session->send_rtp_sink_ghost == NULL
2914 && session->send_rtcp_src_ghost == NULL) {
2915 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2916 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2917 free_session (session, rtpbin);
2919 GST_RTP_BIN_UNLOCK (rtpbin);
2926 GST_RTP_BIN_UNLOCK (rtpbin);
2927 g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2928 GST_DEBUG_PAD_NAME (pad));