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, G_PARAM_READWRITE));
1421 * GstRtpBin::request-pt-map:
1422 * @rtpbin: the object which received the signal
1423 * @session: the session
1426 * Request the payload type as #GstCaps for @pt in @session.
1428 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1429 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1430 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1431 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1432 G_TYPE_UINT, G_TYPE_UINT);
1435 * GstRtpBin::payload-type-change:
1436 * @rtpbin: the object which received the signal
1437 * @session: the session
1440 * Signal that the current payload type changed to @pt in @session.
1444 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1445 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1446 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1447 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1448 G_TYPE_UINT, G_TYPE_UINT);
1451 * GstRtpBin::clear-pt-map:
1452 * @rtpbin: the object which received the signal
1454 * Clear all previously cached pt-mapping obtained with
1455 * #GstRtpBin::request-pt-map.
1457 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1458 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1459 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1460 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1464 * GstRtpBin::reset-sync:
1465 * @rtpbin: the object which received the signal
1467 * Reset all currently configured lip-sync parameters and require new SR
1468 * packets for all streams before lip-sync is attempted again.
1470 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1471 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1472 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1473 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1477 * GstRtpBin::get-internal-session:
1478 * @rtpbin: the object which received the signal
1479 * @id: the session id
1481 * Request the internal RTPSession object as #GObject in session @id.
1483 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1484 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1485 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1486 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1487 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1490 * GstRtpBin::on-new-ssrc:
1491 * @rtpbin: the object which received the signal
1492 * @session: the session
1495 * Notify of a new SSRC that entered @session.
1497 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1498 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1499 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1500 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1501 G_TYPE_UINT, G_TYPE_UINT);
1503 * GstRtpBin::on-ssrc-collision:
1504 * @rtpbin: the object which received the signal
1505 * @session: the session
1508 * Notify when we have an SSRC collision
1510 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1511 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1512 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1513 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1514 G_TYPE_UINT, G_TYPE_UINT);
1516 * GstRtpBin::on-ssrc-validated:
1517 * @rtpbin: the object which received the signal
1518 * @session: the session
1521 * Notify of a new SSRC that became validated.
1523 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1524 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1525 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1526 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1527 G_TYPE_UINT, G_TYPE_UINT);
1529 * GstRtpBin::on-ssrc-active:
1530 * @rtpbin: the object which received the signal
1531 * @session: the session
1534 * Notify of a SSRC that is active, i.e., sending RTCP.
1536 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1537 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1538 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1539 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1540 G_TYPE_UINT, G_TYPE_UINT);
1542 * GstRtpBin::on-ssrc-sdes:
1543 * @rtpbin: the object which received the signal
1544 * @session: the session
1547 * Notify of a SSRC that is active, i.e., sending RTCP.
1549 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1550 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1551 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1552 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1553 G_TYPE_UINT, G_TYPE_UINT);
1556 * GstRtpBin::on-bye-ssrc:
1557 * @rtpbin: the object which received the signal
1558 * @session: the session
1561 * Notify of an SSRC that became inactive because of a BYE packet.
1563 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1564 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1565 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1566 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1567 G_TYPE_UINT, G_TYPE_UINT);
1569 * GstRtpBin::on-bye-timeout:
1570 * @rtpbin: the object which received the signal
1571 * @session: the session
1574 * Notify of an SSRC that has timed out because of BYE
1576 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1577 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1578 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1579 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1580 G_TYPE_UINT, G_TYPE_UINT);
1582 * GstRtpBin::on-timeout:
1583 * @rtpbin: the object which received the signal
1584 * @session: the session
1587 * Notify of an SSRC that has timed out
1589 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1590 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1591 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1592 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1593 G_TYPE_UINT, G_TYPE_UINT);
1595 * GstRtpBin::on-sender-timeout:
1596 * @rtpbin: the object which received the signal
1597 * @session: the session
1600 * Notify of a sender SSRC that has timed out and became a receiver
1602 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1603 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1604 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1605 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1606 G_TYPE_UINT, G_TYPE_UINT);
1609 * GstRtpBin::on-npt-stop:
1610 * @rtpbin: the object which received the signal
1611 * @session: the session
1614 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1616 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1617 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1618 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1619 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1620 G_TYPE_UINT, G_TYPE_UINT);
1622 g_object_class_install_property (gobject_class, PROP_SDES,
1623 g_param_spec_boxed ("sdes", "SDES",
1624 "The SDES items of this session",
1625 GST_TYPE_STRUCTURE, G_PARAM_READWRITE));
1627 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1628 g_param_spec_boolean ("do-lost", "Do Lost",
1629 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1630 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1632 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1633 g_param_spec_boolean ("autoremove", "Auto Remove",
1634 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1635 G_PARAM_READWRITE));
1637 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1638 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1639 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1640 G_PARAM_READWRITE));
1642 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1643 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1644 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1645 DEFAULT_AUTOREMOVE, G_PARAM_READWRITE));
1647 * GstRtpBin::buffer-mode:
1649 * Control the buffering and timestamping mode used by the jitterbuffer.
1653 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1654 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1655 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1656 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1658 * GstRtpBin::ntp-sync:
1660 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1661 * between the receivers and the senders (such as when using ntpd) this option
1662 * can be used to synchronize receivers on multiple machines.
1666 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1667 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1668 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1669 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1671 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1672 gstelement_class->request_new_pad =
1673 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1674 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1676 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1678 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1679 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1680 klass->get_internal_session =
1681 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1683 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1687 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1691 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1692 rtpbin->priv->bin_lock = g_mutex_new ();
1693 rtpbin->priv->dyn_lock = g_mutex_new ();
1695 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1696 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1697 rtpbin->do_lost = DEFAULT_DO_LOST;
1698 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1699 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1700 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1701 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1702 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1704 /* some default SDES entries */
1705 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1706 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1707 "cname", G_TYPE_STRING, str,
1708 "name", G_TYPE_STRING, g_get_real_name (),
1709 "tool", G_TYPE_STRING, "GStreamer", NULL);
1714 gst_rtp_bin_dispose (GObject * object)
1718 rtpbin = GST_RTP_BIN (object);
1720 GST_DEBUG_OBJECT (object, "freeing sessions");
1721 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1722 g_slist_free (rtpbin->sessions);
1723 rtpbin->sessions = NULL;
1724 GST_DEBUG_OBJECT (object, "freeing clients");
1725 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1726 g_slist_free (rtpbin->clients);
1727 rtpbin->clients = NULL;
1729 G_OBJECT_CLASS (parent_class)->dispose (object);
1733 gst_rtp_bin_finalize (GObject * object)
1737 rtpbin = GST_RTP_BIN (object);
1740 gst_structure_free (rtpbin->sdes);
1742 g_mutex_free (rtpbin->priv->bin_lock);
1743 g_mutex_free (rtpbin->priv->dyn_lock);
1745 G_OBJECT_CLASS (parent_class)->finalize (object);
1750 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1757 GST_RTP_BIN_LOCK (bin);
1759 GST_OBJECT_LOCK (bin);
1761 gst_structure_free (bin->sdes);
1762 bin->sdes = gst_structure_copy (sdes);
1764 /* store in all sessions */
1765 for (item = bin->sessions; item; item = g_slist_next (item))
1766 g_object_set (item->data, "sdes", sdes, NULL);
1767 GST_OBJECT_UNLOCK (bin);
1769 GST_RTP_BIN_UNLOCK (bin);
1772 static GstStructure *
1773 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1775 GstStructure *result;
1777 GST_OBJECT_LOCK (bin);
1778 result = gst_structure_copy (bin->sdes);
1779 GST_OBJECT_UNLOCK (bin);
1785 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1786 const GValue * value, GParamSpec * pspec)
1790 rtpbin = GST_RTP_BIN (object);
1794 GST_RTP_BIN_LOCK (rtpbin);
1795 rtpbin->latency_ms = g_value_get_uint (value);
1796 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1797 GST_RTP_BIN_UNLOCK (rtpbin);
1798 /* propagate the property down to the jitterbuffer */
1799 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1802 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1805 GST_RTP_BIN_LOCK (rtpbin);
1806 rtpbin->do_lost = g_value_get_boolean (value);
1807 GST_RTP_BIN_UNLOCK (rtpbin);
1808 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1811 rtpbin->ntp_sync = g_value_get_boolean (value);
1813 case PROP_IGNORE_PT:
1814 rtpbin->ignore_pt = g_value_get_boolean (value);
1816 case PROP_AUTOREMOVE:
1817 rtpbin->priv->autoremove = g_value_get_boolean (value);
1819 case PROP_USE_PIPELINE_CLOCK:
1822 GST_RTP_BIN_LOCK (rtpbin);
1823 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
1824 for (sessions = rtpbin->sessions; sessions;
1825 sessions = g_slist_next (sessions)) {
1826 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1828 g_object_set (G_OBJECT (session->session),
1829 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
1831 GST_RTP_BIN_UNLOCK (rtpbin);
1834 case PROP_BUFFER_MODE:
1835 GST_RTP_BIN_LOCK (rtpbin);
1836 rtpbin->buffer_mode = g_value_get_enum (value);
1837 GST_RTP_BIN_UNLOCK (rtpbin);
1838 /* propagate the property down to the jitterbuffer */
1839 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
1842 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1848 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1849 GValue * value, GParamSpec * pspec)
1853 rtpbin = GST_RTP_BIN (object);
1857 GST_RTP_BIN_LOCK (rtpbin);
1858 g_value_set_uint (value, rtpbin->latency_ms);
1859 GST_RTP_BIN_UNLOCK (rtpbin);
1862 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
1865 GST_RTP_BIN_LOCK (rtpbin);
1866 g_value_set_boolean (value, rtpbin->do_lost);
1867 GST_RTP_BIN_UNLOCK (rtpbin);
1869 case PROP_IGNORE_PT:
1870 g_value_set_boolean (value, rtpbin->ignore_pt);
1873 g_value_set_boolean (value, rtpbin->ntp_sync);
1875 case PROP_AUTOREMOVE:
1876 g_value_set_boolean (value, rtpbin->priv->autoremove);
1878 case PROP_BUFFER_MODE:
1879 g_value_set_enum (value, rtpbin->buffer_mode);
1881 case PROP_USE_PIPELINE_CLOCK:
1882 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
1885 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1891 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1895 rtpbin = GST_RTP_BIN (bin);
1897 switch (GST_MESSAGE_TYPE (message)) {
1898 case GST_MESSAGE_ELEMENT:
1900 const GstStructure *s = gst_message_get_structure (message);
1902 /* we change the structure name and add the session ID to it */
1903 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
1904 GstRtpBinSession *sess;
1906 /* find the session we set it as object data */
1907 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1908 "GstRTPBin.session");
1910 if (G_LIKELY (sess)) {
1911 message = gst_message_make_writable (message);
1912 s = gst_message_get_structure (message);
1913 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1917 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1920 case GST_MESSAGE_BUFFERING:
1923 gint min_percent = 100;
1924 GSList *sessions, *streams;
1925 GstRtpBinStream *stream;
1926 gboolean change = FALSE, active = FALSE;
1927 GstClockTime min_out_time;
1929 gst_message_parse_buffering (message, &percent);
1932 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1933 "GstRTPBin.stream");
1935 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
1937 /* get the stream */
1938 if (G_LIKELY (stream)) {
1939 GST_RTP_BIN_LOCK (rtpbin);
1940 /* fill in the percent */
1941 stream->percent = percent;
1943 /* calculate the min value for all streams */
1944 for (sessions = rtpbin->sessions; sessions;
1945 sessions = g_slist_next (sessions)) {
1946 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1948 GST_RTP_SESSION_LOCK (session);
1949 if (session->streams) {
1950 for (streams = session->streams; streams;
1951 streams = g_slist_next (streams)) {
1952 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1954 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
1957 /* find min percent */
1958 if (min_percent > stream->percent)
1959 min_percent = stream->percent;
1962 GST_INFO_OBJECT (bin,
1963 "session has no streams, setting min_percent to 0");
1966 GST_RTP_SESSION_UNLOCK (session);
1968 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
1970 if (rtpbin->buffering) {
1971 if (min_percent == 100) {
1972 rtpbin->buffering = FALSE;
1977 if (min_percent < 100) {
1978 /* pause the streams */
1979 rtpbin->buffering = TRUE;
1984 GST_RTP_BIN_UNLOCK (rtpbin);
1986 gst_message_unref (message);
1988 /* make a new buffering message with the min value */
1990 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
1992 if (G_UNLIKELY (change)) {
1994 guint64 running_time = 0;
1997 /* figure out the running time when we have a clock */
1998 if (G_LIKELY ((clock =
1999 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2000 guint64 now, base_time;
2002 now = gst_clock_get_time (clock);
2003 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2004 running_time = now - base_time;
2006 GST_DEBUG_OBJECT (bin,
2007 "running time now %" GST_TIME_FORMAT,
2008 GST_TIME_ARGS (running_time));
2010 GST_RTP_BIN_LOCK (rtpbin);
2012 /* when we reactivate, calculate the offsets so that all streams have
2013 * an output time that is at least as big as the running_time */
2016 if (running_time > rtpbin->buffer_start) {
2017 offset = running_time - rtpbin->buffer_start;
2018 if (offset >= rtpbin->latency_ns)
2019 offset -= rtpbin->latency_ns;
2025 /* pause all streams */
2027 for (sessions = rtpbin->sessions; sessions;
2028 sessions = g_slist_next (sessions)) {
2029 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2031 GST_RTP_SESSION_LOCK (session);
2032 for (streams = session->streams; streams;
2033 streams = g_slist_next (streams)) {
2034 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2035 GstElement *element = stream->buffer;
2038 g_signal_emit_by_name (element, "set-active", active, offset,
2042 g_object_get (element, "percent", &stream->percent, NULL);
2046 if (min_out_time == -1 || last_out < min_out_time)
2047 min_out_time = last_out;
2050 GST_DEBUG_OBJECT (bin,
2051 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2052 GST_TIME_FORMAT ", percent %d", element, active,
2053 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2056 GST_RTP_SESSION_UNLOCK (session);
2058 GST_DEBUG_OBJECT (bin,
2059 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2061 /* the buffer_start is the min out time of all paused jitterbuffers */
2063 rtpbin->buffer_start = min_out_time;
2065 GST_RTP_BIN_UNLOCK (rtpbin);
2068 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2073 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2079 static GstStateChangeReturn
2080 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2082 GstStateChangeReturn res;
2084 GstRtpBinPrivate *priv;
2086 rtpbin = GST_RTP_BIN (element);
2087 priv = rtpbin->priv;
2089 switch (transition) {
2090 case GST_STATE_CHANGE_NULL_TO_READY:
2092 case GST_STATE_CHANGE_READY_TO_PAUSED:
2093 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2094 g_atomic_int_set (&priv->shutdown, 0);
2096 case GST_STATE_CHANGE_PAUSED_TO_READY:
2097 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2098 g_atomic_int_set (&priv->shutdown, 1);
2099 /* wait for all callbacks to end by taking the lock. No new callbacks will
2100 * be able to happen as we set the shutdown flag. */
2101 GST_RTP_BIN_DYN_LOCK (rtpbin);
2102 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2103 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2109 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2111 switch (transition) {
2112 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2114 case GST_STATE_CHANGE_PAUSED_TO_READY:
2116 case GST_STATE_CHANGE_READY_TO_NULL:
2124 /* a new pad (SSRC) was created in @session. This signal is emited from the
2125 * payload demuxer. */
2127 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2128 GstRtpBinStream * stream)
2131 GstElementClass *klass;
2132 GstPadTemplate *templ;
2136 rtpbin = stream->bin;
2138 GST_DEBUG ("new payload pad %d", pt);
2140 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2142 /* ghost the pad to the parent */
2143 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2144 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2145 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2146 stream->session->id, stream->ssrc, pt);
2147 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2149 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2151 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2152 gst_pad_set_active (gpad, TRUE);
2153 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2155 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2161 GST_DEBUG ("ignoring, we are shutting down");
2167 payload_pad_removed (GstElement * element, GstPad * pad,
2168 GstRtpBinStream * stream)
2173 rtpbin = stream->bin;
2175 GST_DEBUG ("payload pad removed");
2177 GST_RTP_BIN_DYN_LOCK (rtpbin);
2178 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2179 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2181 gst_pad_set_active (gpad, FALSE);
2182 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2184 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2188 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2193 rtpbin = session->bin;
2195 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2198 caps = get_pt_map (session, pt);
2207 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2213 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2215 GST_DEBUG_OBJECT (session->bin,
2216 "emiting signal for pt type changed to %d in session %d", pt,
2219 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2220 0, session->id, pt);
2223 /* emited when caps changed for the session */
2225 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2230 const GstStructure *s;
2234 g_object_get (pad, "caps", &caps, NULL);
2239 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2241 s = gst_caps_get_structure (caps, 0);
2243 /* get payload, finish when it's not there */
2244 if (!gst_structure_get_int (s, "payload", &payload))
2247 GST_RTP_SESSION_LOCK (session);
2248 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2249 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2250 GST_RTP_SESSION_UNLOCK (session);
2253 /* a new pad (SSRC) was created in @session */
2255 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2256 GstRtpBinSession * session)
2259 GstRtpBinStream *stream;
2260 GstPad *sinkpad, *srcpad;
2263 rtpbin = session->bin;
2265 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2266 GST_DEBUG_PAD_NAME (pad));
2268 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2270 GST_RTP_SESSION_LOCK (session);
2272 /* create new stream */
2273 stream = create_stream (session, ssrc);
2277 /* get pad and link */
2278 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2279 padname = g_strdup_printf ("src_%d", ssrc);
2280 srcpad = gst_element_get_static_pad (element, padname);
2282 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2283 gst_pad_link (srcpad, sinkpad);
2284 gst_object_unref (sinkpad);
2285 gst_object_unref (srcpad);
2287 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2288 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2289 srcpad = gst_element_get_static_pad (element, padname);
2291 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2292 gst_pad_link (srcpad, sinkpad);
2293 gst_object_unref (sinkpad);
2294 gst_object_unref (srcpad);
2296 /* connect to the RTCP sync signal from the jitterbuffer */
2297 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2298 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2299 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2301 if (stream->demux) {
2302 /* connect to the new-pad signal of the payload demuxer, this will expose the
2303 * new pad by ghosting it. */
2304 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2305 "new-payload-type", (GCallback) new_payload_found, stream);
2306 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2307 "pad-removed", (GCallback) payload_pad_removed, stream);
2309 /* connect to the request-pt-map signal. This signal will be emited by the
2310 * demuxer so that it can apply a proper caps on the buffers for the
2312 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2313 "request-pt-map", (GCallback) pt_map_requested, session);
2314 /* connect to the signal so it can be forwarded. */
2315 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2316 "payload-type-change", (GCallback) payload_type_change, session);
2318 /* add gstrtpjitterbuffer src pad to pads */
2319 GstElementClass *klass;
2320 GstPadTemplate *templ;
2324 pad = gst_element_get_static_pad (stream->buffer, "src");
2326 /* ghost the pad to the parent */
2327 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2328 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2329 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2330 stream->session->id, stream->ssrc, 255);
2331 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2334 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2335 gst_pad_set_active (gpad, TRUE);
2336 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2338 gst_object_unref (pad);
2341 GST_RTP_SESSION_UNLOCK (session);
2342 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2349 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2354 GST_RTP_SESSION_UNLOCK (session);
2355 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2356 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2361 /* Create a pad for receiving RTP for the session in @name. Must be called with
2365 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2369 GstRtpBinSession *session;
2370 GstPadLinkReturn lres;
2372 /* first get the session number */
2373 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2376 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2378 /* get or create session */
2379 session = find_session_by_id (rtpbin, sessid);
2381 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2382 /* create session now */
2383 session = create_session (rtpbin, sessid);
2384 if (session == NULL)
2388 /* check if pad was requested */
2389 if (session->recv_rtp_sink_ghost != NULL)
2390 return session->recv_rtp_sink_ghost;
2392 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2393 /* get recv_rtp pad and store */
2394 session->recv_rtp_sink =
2395 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2396 if (session->recv_rtp_sink == NULL)
2399 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2400 (GCallback) caps_changed, session);
2402 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2403 /* get srcpad, link to SSRCDemux */
2404 session->recv_rtp_src =
2405 gst_element_get_static_pad (session->session, "recv_rtp_src");
2406 if (session->recv_rtp_src == NULL)
2409 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2410 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2411 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2412 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2413 gst_object_unref (sinkdpad);
2414 if (lres != GST_PAD_LINK_OK)
2417 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2418 session->demux_newpad_sig = g_signal_connect (session->demux,
2419 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2420 session->demux_padremoved_sig = g_signal_connect (session->demux,
2421 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2423 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2424 session->recv_rtp_sink_ghost =
2425 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2426 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2427 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2429 return session->recv_rtp_sink_ghost;
2434 g_warning ("gstrtpbin: invalid name given");
2439 /* create_session already warned */
2444 g_warning ("gstrtpbin: failed to get session pad");
2449 g_warning ("gstrtpbin: failed to link pads");
2455 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2457 if (session->demux_newpad_sig) {
2458 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2459 session->demux_newpad_sig = 0;
2461 if (session->demux_padremoved_sig) {
2462 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2463 session->demux_padremoved_sig = 0;
2465 if (session->recv_rtp_src) {
2466 gst_object_unref (session->recv_rtp_src);
2467 session->recv_rtp_src = NULL;
2469 if (session->recv_rtp_sink) {
2470 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2471 gst_object_unref (session->recv_rtp_sink);
2472 session->recv_rtp_sink = NULL;
2474 if (session->recv_rtp_sink_ghost) {
2475 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2476 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2477 session->recv_rtp_sink_ghost);
2478 session->recv_rtp_sink_ghost = NULL;
2482 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2486 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2490 GstRtpBinSession *session;
2492 GstPadLinkReturn lres;
2494 /* first get the session number */
2495 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2498 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2500 /* get or create the session */
2501 session = find_session_by_id (rtpbin, sessid);
2503 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2504 /* create session now */
2505 session = create_session (rtpbin, sessid);
2506 if (session == NULL)
2510 /* check if pad was requested */
2511 if (session->recv_rtcp_sink_ghost != NULL)
2512 return session->recv_rtcp_sink_ghost;
2514 /* get recv_rtp pad and store */
2515 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2516 session->recv_rtcp_sink =
2517 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2518 if (session->recv_rtcp_sink == NULL)
2521 /* get srcpad, link to SSRCDemux */
2522 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2523 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2524 if (session->sync_src == NULL)
2527 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2528 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2529 lres = gst_pad_link (session->sync_src, sinkdpad);
2530 gst_object_unref (sinkdpad);
2531 if (lres != GST_PAD_LINK_OK)
2534 session->recv_rtcp_sink_ghost =
2535 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2536 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2537 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2538 session->recv_rtcp_sink_ghost);
2540 return session->recv_rtcp_sink_ghost;
2545 g_warning ("gstrtpbin: invalid name given");
2550 /* create_session already warned */
2555 g_warning ("gstrtpbin: failed to get session pad");
2560 g_warning ("gstrtpbin: failed to link pads");
2566 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2568 if (session->recv_rtcp_sink_ghost) {
2569 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2570 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2571 session->recv_rtcp_sink_ghost);
2572 session->recv_rtcp_sink_ghost = NULL;
2574 if (session->sync_src) {
2575 /* releasing the request pad should also unref the sync pad */
2576 gst_object_unref (session->sync_src);
2577 session->sync_src = NULL;
2579 if (session->recv_rtcp_sink) {
2580 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2581 gst_object_unref (session->recv_rtcp_sink);
2582 session->recv_rtcp_sink = NULL;
2586 /* Create a pad for sending RTP for the session in @name. Must be called with
2590 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2594 GstRtpBinSession *session;
2595 GstElementClass *klass;
2597 /* first get the session number */
2598 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2601 /* get or create session */
2602 session = find_session_by_id (rtpbin, sessid);
2604 /* create session now */
2605 session = create_session (rtpbin, sessid);
2606 if (session == NULL)
2610 /* check if pad was requested */
2611 if (session->send_rtp_sink_ghost != NULL)
2612 return session->send_rtp_sink_ghost;
2614 /* get send_rtp pad and store */
2615 session->send_rtp_sink =
2616 gst_element_get_request_pad (session->session, "send_rtp_sink");
2617 if (session->send_rtp_sink == NULL)
2620 session->send_rtp_sink_ghost =
2621 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2622 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2623 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2626 session->send_rtp_src =
2627 gst_element_get_static_pad (session->session, "send_rtp_src");
2628 if (session->send_rtp_src == NULL)
2631 /* ghost the new source pad */
2632 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2633 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2634 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2635 session->send_rtp_src_ghost =
2636 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2637 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2638 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2641 return session->send_rtp_sink_ghost;
2646 g_warning ("gstrtpbin: invalid name given");
2651 /* create_session already warned */
2656 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2661 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2668 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2670 if (session->send_rtp_src_ghost) {
2671 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2672 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2673 session->send_rtp_src_ghost);
2674 session->send_rtp_src_ghost = NULL;
2676 if (session->send_rtp_src) {
2677 gst_object_unref (session->send_rtp_src);
2678 session->send_rtp_src = NULL;
2680 if (session->send_rtp_sink) {
2681 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2682 session->send_rtp_sink);
2683 gst_object_unref (session->send_rtp_sink);
2684 session->send_rtp_sink = NULL;
2686 if (session->send_rtp_sink_ghost) {
2687 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2688 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2689 session->send_rtp_sink_ghost);
2690 session->send_rtp_sink_ghost = NULL;
2694 /* Create a pad for sending RTCP for the session in @name. Must be called with
2698 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2701 GstRtpBinSession *session;
2703 /* first get the session number */
2704 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2707 /* get or create session */
2708 session = find_session_by_id (rtpbin, sessid);
2712 /* check if pad was requested */
2713 if (session->send_rtcp_src_ghost != NULL)
2714 return session->send_rtcp_src_ghost;
2716 /* get rtcp_src pad and store */
2717 session->send_rtcp_src =
2718 gst_element_get_request_pad (session->session, "send_rtcp_src");
2719 if (session->send_rtcp_src == NULL)
2722 session->send_rtcp_src_ghost =
2723 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2724 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2725 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2727 return session->send_rtcp_src_ghost;
2732 g_warning ("gstrtpbin: invalid name given");
2737 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2742 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2748 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2750 if (session->send_rtcp_src_ghost) {
2751 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2752 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2753 session->send_rtcp_src_ghost);
2754 session->send_rtcp_src_ghost = NULL;
2756 if (session->send_rtcp_src) {
2757 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2758 gst_object_unref (session->send_rtcp_src);
2759 session->send_rtcp_src = NULL;
2763 /* If the requested name is NULL we should create a name with
2764 * the session number assuming we want the lowest posible session
2765 * with a free pad like the template */
2767 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2769 gboolean name_found = FALSE;
2771 GstIterator *pad_it = NULL;
2772 gchar *pad_name = NULL;
2774 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2775 while (!name_found) {
2776 gboolean done = FALSE;
2778 pad_name = g_strdup_printf (templ->name_template, session++);
2779 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2784 switch (gst_iterator_next (pad_it, &data)) {
2785 case GST_ITERATOR_OK:
2790 pad = GST_PAD_CAST (data);
2791 name = gst_pad_get_name (pad);
2793 if (strcmp (name, pad_name) == 0) {
2798 gst_object_unref (pad);
2801 case GST_ITERATOR_ERROR:
2802 case GST_ITERATOR_RESYNC:
2803 /* restart iteration */
2808 case GST_ITERATOR_DONE:
2813 gst_iterator_free (pad_it);
2816 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2823 gst_rtp_bin_request_new_pad (GstElement * element,
2824 GstPadTemplate * templ, const gchar * name)
2827 GstElementClass *klass;
2830 gchar *pad_name = NULL;
2832 g_return_val_if_fail (templ != NULL, NULL);
2833 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2835 rtpbin = GST_RTP_BIN (element);
2836 klass = GST_ELEMENT_GET_CLASS (element);
2838 GST_RTP_BIN_LOCK (rtpbin);
2841 /* use a free pad name */
2842 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2844 /* use the provided name */
2845 pad_name = g_strdup (name);
2848 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2850 /* figure out the template */
2851 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2852 result = create_recv_rtp (rtpbin, templ, pad_name);
2853 } else if (templ == gst_element_class_get_pad_template (klass,
2854 "recv_rtcp_sink_%d")) {
2855 result = create_recv_rtcp (rtpbin, templ, pad_name);
2856 } else if (templ == gst_element_class_get_pad_template (klass,
2857 "send_rtp_sink_%d")) {
2858 result = create_send_rtp (rtpbin, templ, pad_name);
2859 } else if (templ == gst_element_class_get_pad_template (klass,
2860 "send_rtcp_src_%d")) {
2861 result = create_rtcp (rtpbin, templ, pad_name);
2863 goto wrong_template;
2866 GST_RTP_BIN_UNLOCK (rtpbin);
2874 GST_RTP_BIN_UNLOCK (rtpbin);
2875 g_warning ("gstrtpbin: this is not our template");
2881 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2883 GstRtpBinSession *session;
2886 g_return_if_fail (GST_IS_GHOST_PAD (pad));
2887 g_return_if_fail (GST_IS_RTP_BIN (element));
2889 rtpbin = GST_RTP_BIN (element);
2891 GST_RTP_BIN_LOCK (rtpbin);
2892 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2893 GST_DEBUG_PAD_NAME (pad));
2895 if (!(session = find_session_by_pad (rtpbin, pad)))
2898 if (session->recv_rtp_sink_ghost == pad) {
2899 remove_recv_rtp (rtpbin, session);
2900 } else if (session->recv_rtcp_sink_ghost == pad) {
2901 remove_recv_rtcp (rtpbin, session);
2902 } else if (session->send_rtp_sink_ghost == pad) {
2903 remove_send_rtp (rtpbin, session);
2904 } else if (session->send_rtcp_src_ghost == pad) {
2905 remove_rtcp (rtpbin, session);
2908 /* no more request pads, free the complete session */
2909 if (session->recv_rtp_sink_ghost == NULL
2910 && session->recv_rtcp_sink_ghost == NULL
2911 && session->send_rtp_sink_ghost == NULL
2912 && session->send_rtcp_src_ghost == NULL) {
2913 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2914 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2915 free_session (session, rtpbin);
2917 GST_RTP_BIN_UNLOCK (rtpbin);
2924 GST_RTP_BIN_UNLOCK (rtpbin);
2925 g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2926 GST_DEBUG_PAD_NAME (pad));