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
61 * Access to the internal statistics of gstrtpbin is provided with the
62 * get-internal-session property. This action signal gives access to the
63 * RTPSession object which further provides action signals to retrieve the
64 * internal source and other sources.
67 * <title>Example pipelines</title>
69 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
70 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
71 * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
73 * gst-launch gstrtpbin name=rtpbin \
74 * v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
75 * rtpbin.send_rtp_src_0 ! udpsink port=5000 \
76 * rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false \
77 * udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0 \
78 * audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1 \
79 * rtpbin.send_rtp_src_1 ! udpsink port=5002 \
80 * rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false \
81 * udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
82 * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
83 * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
84 * and the audio is sent to session 1. Video packets are sent on UDP port 5000
85 * and audio packets on port 5002. The video RTCP packets for session 0 are sent
86 * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
87 * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
88 * is received on port 5007. Since RTCP packets from the sender should be sent
89 * as soon as possible and do not participate in preroll, sync=false and
90 * async=false is configured on udpsink
92 * gst-launch -v gstrtpbin name=rtpbin \
93 * udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
94 * port=5000 ! rtpbin.recv_rtp_sink_0 \
95 * rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink \
96 * udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0 \
97 * rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false \
98 * udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
99 * port=5002 ! rtpbin.recv_rtp_sink_1 \
100 * rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink \
101 * udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1 \
102 * rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
103 * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
104 * decode and display the video.
105 * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
106 * decode and play the audio.
107 * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
108 * session 1 on port 5003. These packets will be used for session management and
110 * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
114 * Last reviewed on 2007-08-30 (0.10.6)
123 #include <gst/rtp/gstrtpbuffer.h>
124 #include <gst/rtp/gstrtcpbuffer.h>
126 #include "gstrtpbin-marshal.h"
127 #include "gstrtpbin.h"
128 #include "rtpsession.h"
129 #include "gstrtpsession.h"
130 #include "gstrtpjitterbuffer.h"
132 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
133 #define GST_CAT_DEFAULT gst_rtp_bin_debug
136 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
137 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
140 GST_STATIC_CAPS ("application/x-rtp")
143 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
144 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
147 GST_STATIC_CAPS ("application/x-rtcp")
150 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
154 GST_STATIC_CAPS ("application/x-rtp")
158 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
159 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
162 GST_STATIC_CAPS ("application/x-rtp")
165 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
166 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
169 GST_STATIC_CAPS ("application/x-rtcp")
172 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
176 GST_STATIC_CAPS ("application/x-rtp")
179 #define GST_RTP_BIN_GET_PRIVATE(obj) \
180 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
182 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
183 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
185 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
186 #define GST_RTP_BIN_DYN_LOCK(bin) g_mutex_lock ((bin)->priv->dyn_lock)
187 #define GST_RTP_BIN_DYN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->dyn_lock)
189 /* lock for shutdown */
190 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label) \
192 if (g_atomic_int_get (&bin->priv->shutdown)) \
194 GST_RTP_BIN_DYN_LOCK (bin); \
195 if (g_atomic_int_get (&bin->priv->shutdown)) { \
196 GST_RTP_BIN_DYN_UNLOCK (bin); \
201 /* unlock for shutdown */
202 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin) \
203 GST_RTP_BIN_DYN_UNLOCK (bin); \
205 struct _GstRtpBinPrivate
209 /* lock protecting dynamic adding/removing */
212 /* if we are shutting down or not */
218 /* signals and args */
221 SIGNAL_REQUEST_PT_MAP,
222 SIGNAL_PAYLOAD_TYPE_CHANGE,
225 SIGNAL_GET_INTERNAL_SESSION,
228 SIGNAL_ON_SSRC_COLLISION,
229 SIGNAL_ON_SSRC_VALIDATED,
230 SIGNAL_ON_SSRC_ACTIVE,
233 SIGNAL_ON_BYE_TIMEOUT,
235 SIGNAL_ON_SENDER_TIMEOUT,
240 #define DEFAULT_LATENCY_MS 200
241 #define DEFAULT_SDES NULL
242 #define DEFAULT_DO_LOST FALSE
243 #define DEFAULT_IGNORE_PT FALSE
244 #define DEFAULT_NTP_SYNC FALSE
245 #define DEFAULT_AUTOREMOVE FALSE
246 #define DEFAULT_BUFFER_MODE RTP_JITTER_BUFFER_MODE_SLAVE
247 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
259 PROP_USE_PIPELINE_CLOCK,
264 typedef struct _GstRtpBinSession GstRtpBinSession;
265 typedef struct _GstRtpBinStream GstRtpBinStream;
266 typedef struct _GstRtpBinClient GstRtpBinClient;
268 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
270 static GstCaps *pt_map_requested (GstElement * element, guint pt,
271 GstRtpBinSession * session);
272 static void payload_type_change (GstElement * element, guint pt,
273 GstRtpBinSession * session);
274 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
275 static void free_stream (GstRtpBinStream * stream);
277 /* Manages the RTP stream for one SSRC.
279 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
280 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
281 * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
282 * together (see below).
284 struct _GstRtpBinStream
286 /* the SSRC of this stream */
292 /* the session this SSRC belongs to */
293 GstRtpBinSession *session;
295 /* the jitterbuffer of the SSRC */
297 gulong buffer_handlesync_sig;
298 gulong buffer_ptreq_sig;
299 gulong buffer_ntpstop_sig;
302 /* the PT demuxer of the SSRC */
304 gulong demux_newpad_sig;
305 gulong demux_padremoved_sig;
306 gulong demux_ptreq_sig;
307 gulong demux_ptchange_sig;
309 /* if we have calculated a valid rt_delta for this stream */
311 /* mapping to local RTP and NTP time */
315 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
316 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
318 /* Manages the receiving end of the packets.
320 * There is one such structure for each RTP session (audio/video/...).
321 * We get the RTP/RTCP packets and stuff them into the session manager. From
322 * there they are pushed into an SSRC demuxer that splits the stream based on
323 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
324 * the GstRtpBinStream above).
326 struct _GstRtpBinSession
332 /* the session element */
334 /* the SSRC demuxer */
336 gulong demux_newpad_sig;
337 gulong demux_padremoved_sig;
341 /* list of GstRtpBinStream */
344 /* mapping of payload type to caps */
347 /* the pads of the session */
348 GstPad *recv_rtp_sink;
349 GstPad *recv_rtp_sink_ghost;
350 GstPad *recv_rtp_src;
351 GstPad *recv_rtcp_sink;
352 GstPad *recv_rtcp_sink_ghost;
354 GstPad *send_rtp_sink;
355 GstPad *send_rtp_sink_ghost;
356 GstPad *send_rtp_src;
357 GstPad *send_rtp_src_ghost;
358 GstPad *send_rtcp_src;
359 GstPad *send_rtcp_src_ghost;
362 /* Manages the RTP streams that come from one client and should therefore be
365 struct _GstRtpBinClient
367 /* the common CNAME for the streams */
376 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
377 static GstRtpBinSession *
378 find_session_by_id (GstRtpBin * rtpbin, gint id)
382 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
383 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
391 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
392 static GstRtpBinSession *
393 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
397 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
398 GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
400 if ((sess->recv_rtp_sink_ghost == pad) ||
401 (sess->recv_rtcp_sink_ghost == pad) ||
402 (sess->send_rtp_sink_ghost == pad)
403 || (sess->send_rtcp_src_ghost == pad))
410 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
412 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
417 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
419 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
424 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
426 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
431 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
433 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
438 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
440 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
445 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
447 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
452 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
454 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
457 if (sess->bin->priv->autoremove)
458 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
462 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
464 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
467 if (sess->bin->priv->autoremove)
468 g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
472 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
474 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
479 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
481 g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
482 stream->session->id, stream->ssrc);
485 /* must be called with the SESSION lock */
486 static GstRtpBinStream *
487 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
491 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
492 GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
494 if (stream->ssrc == ssrc)
501 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
502 GstRtpBinSession * session)
504 GstRtpBinStream *stream = NULL;
506 GST_RTP_SESSION_LOCK (session);
507 if ((stream = find_stream_by_ssrc (session, ssrc)))
508 session->streams = g_slist_remove (session->streams, stream);
509 GST_RTP_SESSION_UNLOCK (session);
512 free_stream (stream);
515 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
516 static GstRtpBinSession *
517 create_session (GstRtpBin * rtpbin, gint id)
519 GstRtpBinSession *sess;
520 GstElement *session, *demux;
523 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
526 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
529 sess = g_new0 (GstRtpBinSession, 1);
530 sess->lock = g_mutex_new ();
533 sess->session = session;
535 sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
536 (GDestroyNotify) gst_caps_unref);
537 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
539 /* configure SDES items */
540 GST_OBJECT_LOCK (rtpbin);
541 g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
542 rtpbin->use_pipeline_clock, NULL);
543 GST_OBJECT_UNLOCK (rtpbin);
545 /* provide clock_rate to the session manager when needed */
546 g_signal_connect (session, "request-pt-map",
547 (GCallback) pt_map_requested, sess);
549 g_signal_connect (sess->session, "on-new-ssrc",
550 (GCallback) on_new_ssrc, sess);
551 g_signal_connect (sess->session, "on-ssrc-collision",
552 (GCallback) on_ssrc_collision, sess);
553 g_signal_connect (sess->session, "on-ssrc-validated",
554 (GCallback) on_ssrc_validated, sess);
555 g_signal_connect (sess->session, "on-ssrc-active",
556 (GCallback) on_ssrc_active, sess);
557 g_signal_connect (sess->session, "on-ssrc-sdes",
558 (GCallback) on_ssrc_sdes, sess);
559 g_signal_connect (sess->session, "on-bye-ssrc",
560 (GCallback) on_bye_ssrc, sess);
561 g_signal_connect (sess->session, "on-bye-timeout",
562 (GCallback) on_bye_timeout, sess);
563 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
564 g_signal_connect (sess->session, "on-sender-timeout",
565 (GCallback) on_sender_timeout, sess);
567 gst_bin_add (GST_BIN_CAST (rtpbin), session);
568 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
570 GST_OBJECT_LOCK (rtpbin);
571 target = GST_STATE_TARGET (rtpbin);
572 GST_OBJECT_UNLOCK (rtpbin);
574 /* change state only to what's needed */
575 gst_element_set_state (demux, target);
576 gst_element_set_state (session, target);
583 g_warning ("gstrtpbin: could not create gstrtpsession element");
588 gst_object_unref (session);
589 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
595 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
599 GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
601 gst_element_set_locked_state (sess->demux, TRUE);
602 gst_element_set_locked_state (sess->session, TRUE);
604 gst_element_set_state (sess->demux, GST_STATE_NULL);
605 gst_element_set_state (sess->session, GST_STATE_NULL);
607 if (sess->recv_rtp_sink != NULL) {
608 gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
609 gst_object_unref (sess->recv_rtp_sink);
611 if (sess->recv_rtp_src != NULL)
612 gst_object_unref (sess->recv_rtp_src);
613 if (sess->recv_rtcp_sink != NULL) {
614 gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
615 gst_object_unref (sess->recv_rtcp_sink);
617 if (sess->sync_src != NULL)
618 gst_object_unref (sess->sync_src);
619 if (sess->send_rtp_sink != NULL) {
620 gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
621 gst_object_unref (sess->send_rtp_sink);
623 if (sess->send_rtp_src != NULL)
624 gst_object_unref (sess->send_rtp_src);
625 if (sess->send_rtcp_src != NULL) {
626 gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
627 gst_object_unref (sess->send_rtcp_src);
630 gst_bin_remove (GST_BIN_CAST (bin), sess->session);
631 gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
633 /* remove any references in bin->clients to the streams in sess->streams */
634 client_walk = bin->clients;
635 while (client_walk) {
636 GSList *client_node = client_walk;
637 GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
638 GSList *stream_walk = client->streams;
640 while (stream_walk) {
641 GSList *stream_node = stream_walk;
642 GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
645 stream_walk = g_slist_next (stream_walk);
647 for (inner_walk = sess->streams; inner_walk;
648 inner_walk = g_slist_next (inner_walk)) {
649 if ((GstRtpBinStream *) inner_walk->data == stream) {
650 client->streams = g_slist_delete_link (client->streams, stream_node);
656 client_walk = g_slist_next (client_walk);
658 g_assert ((client->streams && client->nstreams > 0) || (!client->streams
659 && client->streams == 0));
660 if (client->nstreams == 0) {
661 free_client (client, bin);
662 bin->clients = g_slist_delete_link (bin->clients, client_node);
666 g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
667 g_slist_free (sess->streams);
669 g_mutex_free (sess->lock);
670 g_hash_table_destroy (sess->ptmap);
675 /* get the payload type caps for the specific payload @pt in @session */
677 get_pt_map (GstRtpBinSession * session, guint pt)
679 GstCaps *caps = NULL;
682 GValue args[3] = { {0}, {0}, {0} };
684 GST_DEBUG ("searching pt %d in cache", pt);
686 GST_RTP_SESSION_LOCK (session);
688 /* first look in the cache */
689 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
697 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
699 /* not in cache, send signal to request caps */
700 g_value_init (&args[0], GST_TYPE_ELEMENT);
701 g_value_set_object (&args[0], bin);
702 g_value_init (&args[1], G_TYPE_UINT);
703 g_value_set_uint (&args[1], session->id);
704 g_value_init (&args[2], G_TYPE_UINT);
705 g_value_set_uint (&args[2], pt);
707 g_value_init (&ret, GST_TYPE_CAPS);
708 g_value_set_boxed (&ret, NULL);
710 GST_RTP_SESSION_UNLOCK (session);
712 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
714 GST_RTP_SESSION_LOCK (session);
716 g_value_unset (&args[0]);
717 g_value_unset (&args[1]);
718 g_value_unset (&args[2]);
720 /* look in the cache again because we let the lock go */
721 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
724 g_value_unset (&ret);
728 caps = (GstCaps *) g_value_dup_boxed (&ret);
729 g_value_unset (&ret);
733 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
735 /* store in cache, take additional ref */
736 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
737 gst_caps_ref (caps));
740 GST_RTP_SESSION_UNLOCK (session);
747 GST_RTP_SESSION_UNLOCK (session);
748 GST_DEBUG ("no pt map could be obtained");
754 return_true (gpointer key, gpointer value, gpointer user_data)
760 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
762 GSList *clients, *streams;
764 GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
766 GST_RTP_BIN_LOCK (rtpbin);
767 for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
768 GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
770 /* reset sync on all streams for this client */
771 for (streams = client->streams; streams; streams = g_slist_next (streams)) {
772 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
774 /* make use require a new SR packet for this stream before we attempt new
776 stream->have_sync = FALSE;
777 stream->rt_delta = 0;
780 GST_RTP_BIN_UNLOCK (rtpbin);
784 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
786 GSList *sessions, *streams;
788 GST_RTP_BIN_LOCK (bin);
789 GST_DEBUG_OBJECT (bin, "clearing pt map");
790 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
791 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
793 GST_DEBUG_OBJECT (bin, "clearing session %p", session);
794 g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
796 GST_RTP_SESSION_LOCK (session);
797 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
799 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
800 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
802 GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
803 g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
805 g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
807 GST_RTP_SESSION_UNLOCK (session);
809 GST_RTP_BIN_UNLOCK (bin);
812 gst_rtp_bin_reset_sync (bin);
816 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
818 RTPSession *internal_session = NULL;
819 GstRtpBinSession *session;
821 GST_RTP_BIN_LOCK (bin);
822 GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
824 session = find_session_by_id (bin, (gint) session_id);
826 g_object_get (session->session, "internal-session", &internal_session,
829 GST_RTP_BIN_UNLOCK (bin);
831 return internal_session;
835 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
836 const gchar * name, const GValue * value)
838 GSList *sessions, *streams;
840 GST_RTP_BIN_LOCK (bin);
841 for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
842 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
844 GST_RTP_SESSION_LOCK (session);
845 for (streams = session->streams; streams; streams = g_slist_next (streams)) {
846 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
848 g_object_set_property (G_OBJECT (stream->buffer), name, value);
850 GST_RTP_SESSION_UNLOCK (session);
852 GST_RTP_BIN_UNLOCK (bin);
855 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
856 static GstRtpBinClient *
857 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
859 GstRtpBinClient *result = NULL;
862 for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
863 GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
865 if (len != client->cname_len)
868 if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
869 GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
876 /* nothing found, create one */
877 if (result == NULL) {
878 result = g_new0 (GstRtpBinClient, 1);
879 result->cname = g_strndup ((gchar *) data, len);
880 result->cname_len = len;
881 bin->clients = g_slist_prepend (bin->clients, result);
882 GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
889 free_client (GstRtpBinClient * client, GstRtpBin * bin)
891 GST_DEBUG_OBJECT (bin, "freeing client %p", client);
892 g_slist_free (client->streams);
893 g_free (client->cname);
898 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
903 GstClockTime base_time, rt, clock_time;
905 GST_OBJECT_LOCK (bin);
906 if ((clock = GST_ELEMENT_CLOCK (bin))) {
907 base_time = GST_ELEMENT_CAST (bin)->base_time;
908 gst_object_ref (clock);
909 GST_OBJECT_UNLOCK (bin);
911 clock_time = gst_clock_get_time (clock);
913 if (bin->use_pipeline_clock) {
918 /* get current NTP time */
919 g_get_current_time (¤t);
920 ntpns = GST_TIMEVAL_TO_TIME (current);
923 /* add constant to convert from 1970 based time to 1900 based time */
924 ntpns += (2208988800LL * GST_SECOND);
926 /* get current clock time and convert to running time */
927 rt = clock_time - base_time;
929 gst_object_unref (clock);
931 GST_OBJECT_UNLOCK (bin);
942 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
945 gint64 prev_ts_offset;
947 g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
949 /* delta changed, see how much */
950 if (prev_ts_offset != ts_offset) {
953 diff = prev_ts_offset - ts_offset;
955 GST_DEBUG_OBJECT (bin,
956 "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
957 ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
959 /* only change diff when it changed more than 4 milliseconds. This
960 * compensates for rounding errors in NTP to RTP timestamp
962 if (ABS (diff) > 4 * GST_MSECOND) {
963 if (ABS (diff) < (3 * GST_SECOND)) {
964 g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
966 GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
969 GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
972 GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
973 stream->ssrc, ts_offset);
976 /* associate a stream to the given CNAME. This will make sure all streams for
977 * that CNAME are synchronized together.
978 * Must be called with GST_RTP_BIN_LOCK */
980 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
981 guint8 * data, guint64 ntptime, guint64 last_extrtptime,
982 guint64 base_rtptime, guint64 base_time, guint clock_rate)
984 GstRtpBinClient *client;
989 GstClockTime running_time;
991 gint64 ntpdiff, rtdiff;
994 /* first find or create the CNAME */
995 client = get_client (bin, len, data, &created);
997 /* find stream in the client */
998 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
999 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1001 if (ostream == stream)
1004 /* not found, add it to the list */
1006 GST_DEBUG_OBJECT (bin,
1007 "new association of SSRC %08x with client %p with CNAME %s",
1008 stream->ssrc, client, client->cname);
1009 client->streams = g_slist_prepend (client->streams, stream);
1012 GST_DEBUG_OBJECT (bin,
1013 "found association of SSRC %08x with client %p with CNAME %s",
1014 stream->ssrc, client, client->cname);
1017 /* Take the extended rtptime we found in the SR packet and map it to the
1018 * local rtptime. The local rtp time is used to construct timestamps on the
1019 * buffers so we will calculate what running_time corresponds to the RTP
1020 * timestamp in the SR packet. */
1021 local_rtp = last_extrtptime - base_rtptime;
1023 GST_DEBUG_OBJECT (bin,
1024 "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1025 ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", base_rtptime,
1026 last_extrtptime, local_rtp, clock_rate);
1028 /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1029 * same conversion that a jitterbuffer would use to convert an rtp timestamp
1030 * into a corresponding gstreamer timestamp. Note that the base_time also
1031 * contains the drift between sender and receiver. */
1032 local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1033 local_rt += base_time;
1035 /* convert ntptime to unix time since 1900 */
1036 last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1037 (G_GINT64_CONSTANT (1) << 32));
1039 stream->have_sync = TRUE;
1041 GST_DEBUG_OBJECT (bin,
1042 "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1043 local_rt, last_unix);
1045 /* recalc inter stream playout offset, but only if there is more than one
1046 * stream or we're doing NTP sync. */
1047 if (bin->ntp_sync) {
1048 /* For NTP sync we need to first get a snapshot of running_time and NTP
1049 * time. We know at what running_time we play a certain RTP time, we also
1050 * calculated when we would play the RTP time in the SR packet. Now we need
1051 * to know how the running_time and the NTP time relate to eachother. */
1052 get_current_times (bin, &running_time, &ntpnstime);
1054 /* see how far away the NTP time is. This is the difference between the
1055 * current NTP time and the NTP time in the last SR packet. */
1056 ntpdiff = ntpnstime - last_unix;
1057 /* see how far away the running_time is. This is the difference between the
1058 * current running_time and the running_time of the RTP timestamp in the
1059 * last SR packet. */
1060 rtdiff = running_time - local_rt;
1062 GST_DEBUG_OBJECT (bin,
1063 "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1064 ntpnstime, last_unix);
1065 GST_DEBUG_OBJECT (bin,
1066 "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1069 /* combine to get the final diff to apply to the running_time */
1070 stream->rt_delta = rtdiff - ntpdiff;
1072 stream_set_ts_offset (bin, stream, stream->rt_delta);
1073 } else if (client->nstreams > 1) {
1076 /* calculate delta between server and receiver. last_unix is created by
1077 * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1078 * delta expresses the difference to our timeline and the server timeline. The
1079 * difference in itself doesn't mean much but we can combine the delta of
1080 * multiple streams to create a stream specific offset. */
1081 stream->rt_delta = last_unix - local_rt;
1083 /* calculate the min of all deltas, ignoring streams that did not yet have a
1084 * valid rt_delta because we did not yet receive an SR packet for those
1086 * We calculate the mininum because we would like to only apply positive
1087 * offsets to streams, delaying their playback instead of trying to speed up
1088 * other streams (which might be imposible when we have to create negative
1090 * The stream that has the smallest diff is selected as the reference stream,
1091 * all other streams will have a positive offset to this difference. */
1093 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1094 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1096 if (!ostream->have_sync)
1099 if (ostream->rt_delta < min)
1100 min = ostream->rt_delta;
1103 GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
1106 /* calculate offsets for each stream */
1107 for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1108 GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1111 /* ignore streams for which we didn't receive an SR packet yet, we
1112 * can't synchronize them yet. We can however sync other streams just
1114 if (!ostream->have_sync)
1117 /* calculate offset to our reference stream, this should always give a
1118 * positive number. */
1119 ts_offset = ostream->rt_delta - min;
1121 stream_set_ts_offset (bin, ostream, ts_offset);
1127 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1128 for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1129 (b) = gst_rtcp_packet_move_to_next ((packet)))
1131 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1132 for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1133 (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1135 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1136 for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1137 (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1140 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1141 GstRtpBinStream * stream)
1144 GstRTCPPacket packet;
1147 gboolean have_sr, have_sdes;
1149 guint64 base_rtptime;
1154 GstRTCPBuffer rtcp = { NULL };
1158 GST_DEBUG_OBJECT (bin, "sync handler called");
1160 /* get the last relation between the rtp timestamps and the gstreamer
1161 * timestamps. We get this info directly from the jitterbuffer which
1162 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1163 * what the current situation is. */
1165 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1166 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1167 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1169 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1170 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1175 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1177 GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1178 /* first packet must be SR or RR or else the validate would have failed */
1179 switch (gst_rtcp_packet_get_type (&packet)) {
1180 case GST_RTCP_TYPE_SR:
1181 /* only parse first. There is only supposed to be one SR in the packet
1182 * but we will deal with malformed packets gracefully */
1185 /* get NTP and RTP times */
1186 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1189 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1190 /* ignore SR that is not ours */
1191 if (ssrc != stream->ssrc)
1196 case GST_RTCP_TYPE_SDES:
1198 gboolean more_items, more_entries;
1200 /* only deal with first SDES, there is only supposed to be one SDES in
1201 * the RTCP packet but we deal with bad packets gracefully. Also bail
1202 * out if we have not seen an SR item yet. */
1203 if (have_sdes || !have_sr)
1206 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1207 /* skip items that are not about the SSRC of the sender */
1208 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1211 /* find the CNAME entry */
1212 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1213 GstRTCPSDESType type;
1217 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1219 if (type == GST_RTCP_SDES_CNAME) {
1220 GST_RTP_BIN_LOCK (bin);
1221 /* associate the stream to CNAME */
1222 gst_rtp_bin_associate (bin, stream, len, data,
1223 ntptime, extrtptime, base_rtptime, base_time, clock_rate);
1224 GST_RTP_BIN_UNLOCK (bin);
1232 /* we can ignore these packets */
1236 gst_rtcp_buffer_unmap (&rtcp);
1239 /* create a new stream with @ssrc in @session. Must be called with
1240 * RTP_SESSION_LOCK. */
1241 static GstRtpBinStream *
1242 create_stream (GstRtpBinSession * session, guint32 ssrc)
1244 GstElement *buffer, *demux = NULL;
1245 GstRtpBinStream *stream;
1249 rtpbin = session->bin;
1251 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
1252 goto no_jitterbuffer;
1254 if (!rtpbin->ignore_pt)
1255 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
1259 stream = g_new0 (GstRtpBinStream, 1);
1260 stream->ssrc = ssrc;
1261 stream->bin = rtpbin;
1262 stream->session = session;
1263 stream->buffer = buffer;
1264 stream->demux = demux;
1266 stream->have_sync = FALSE;
1267 stream->rt_delta = 0;
1268 stream->percent = 100;
1269 session->streams = g_slist_prepend (session->streams, stream);
1271 /* provide clock_rate to the jitterbuffer when needed */
1272 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1273 (GCallback) pt_map_requested, session);
1274 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1275 (GCallback) on_npt_stop, stream);
1277 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1278 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1280 /* configure latency and packet lost */
1281 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1282 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1283 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1285 if (!rtpbin->ignore_pt)
1286 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1287 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1291 gst_element_link (buffer, demux);
1293 if (rtpbin->buffering) {
1296 GST_INFO_OBJECT (rtpbin,
1297 "bin is buffering, set jitterbuffer as not active");
1298 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1302 GST_OBJECT_LOCK (rtpbin);
1303 target = GST_STATE_TARGET (rtpbin);
1304 GST_OBJECT_UNLOCK (rtpbin);
1306 /* from sink to source */
1308 gst_element_set_state (demux, target);
1310 gst_element_set_state (buffer, target);
1317 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1322 gst_object_unref (buffer);
1323 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1329 free_stream (GstRtpBinStream * stream)
1331 GstRtpBinSession *session;
1333 session = stream->session;
1335 if (stream->demux) {
1336 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1337 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1338 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1340 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1341 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1342 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1345 gst_element_set_locked_state (stream->demux, TRUE);
1346 gst_element_set_locked_state (stream->buffer, TRUE);
1349 gst_element_set_state (stream->demux, GST_STATE_NULL);
1350 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1352 /* now remove this signal, we need this while going to NULL because it to
1353 * do some cleanups */
1355 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1357 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1359 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1364 /* GObject vmethods */
1365 static void gst_rtp_bin_dispose (GObject * object);
1366 static void gst_rtp_bin_finalize (GObject * object);
1367 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1368 const GValue * value, GParamSpec * pspec);
1369 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1370 GValue * value, GParamSpec * pspec);
1372 /* GstElement vmethods */
1373 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1374 GstStateChange transition);
1375 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1376 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1377 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1378 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1380 #define gst_rtp_bin_parent_class parent_class
1381 G_DEFINE_TYPE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1384 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1386 GObjectClass *gobject_class;
1387 GstElementClass *gstelement_class;
1388 GstBinClass *gstbin_class;
1390 gobject_class = (GObjectClass *) klass;
1391 gstelement_class = (GstElementClass *) klass;
1392 gstbin_class = (GstBinClass *) klass;
1394 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1396 gobject_class->dispose = gst_rtp_bin_dispose;
1397 gobject_class->finalize = gst_rtp_bin_finalize;
1398 gobject_class->set_property = gst_rtp_bin_set_property;
1399 gobject_class->get_property = gst_rtp_bin_get_property;
1401 g_object_class_install_property (gobject_class, PROP_LATENCY,
1402 g_param_spec_uint ("latency", "Buffer latency in ms",
1403 "Default amount of ms to buffer in the jitterbuffers", 0,
1404 G_MAXUINT, DEFAULT_LATENCY_MS,
1405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1408 * GstRtpBin::request-pt-map:
1409 * @rtpbin: the object which received the signal
1410 * @session: the session
1413 * Request the payload type as #GstCaps for @pt in @session.
1415 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1416 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1417 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1418 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1419 G_TYPE_UINT, G_TYPE_UINT);
1422 * GstRtpBin::payload-type-change:
1423 * @rtpbin: the object which received the signal
1424 * @session: the session
1427 * Signal that the current payload type changed to @pt in @session.
1431 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1432 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1433 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1434 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1435 G_TYPE_UINT, G_TYPE_UINT);
1438 * GstRtpBin::clear-pt-map:
1439 * @rtpbin: the object which received the signal
1441 * Clear all previously cached pt-mapping obtained with
1442 * #GstRtpBin::request-pt-map.
1444 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1445 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1446 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1447 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1451 * GstRtpBin::reset-sync:
1452 * @rtpbin: the object which received the signal
1454 * Reset all currently configured lip-sync parameters and require new SR
1455 * packets for all streams before lip-sync is attempted again.
1457 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1458 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1459 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1460 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1464 * GstRtpBin::get-internal-session:
1465 * @rtpbin: the object which received the signal
1466 * @id: the session id
1468 * Request the internal RTPSession object as #GObject in session @id.
1470 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1471 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1472 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1473 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1474 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1477 * GstRtpBin::on-new-ssrc:
1478 * @rtpbin: the object which received the signal
1479 * @session: the session
1482 * Notify of a new SSRC that entered @session.
1484 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1485 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1486 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1487 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1488 G_TYPE_UINT, G_TYPE_UINT);
1490 * GstRtpBin::on-ssrc-collision:
1491 * @rtpbin: the object which received the signal
1492 * @session: the session
1495 * Notify when we have an SSRC collision
1497 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1498 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1499 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
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-validated:
1504 * @rtpbin: the object which received the signal
1505 * @session: the session
1508 * Notify of a new SSRC that became validated.
1510 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1511 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1512 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
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-active:
1517 * @rtpbin: the object which received the signal
1518 * @session: the session
1521 * Notify of a SSRC that is active, i.e., sending RTCP.
1523 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1524 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1525 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
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-sdes:
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_SDES] =
1537 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1538 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1539 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1540 G_TYPE_UINT, G_TYPE_UINT);
1543 * GstRtpBin::on-bye-ssrc:
1544 * @rtpbin: the object which received the signal
1545 * @session: the session
1548 * Notify of an SSRC that became inactive because of a BYE packet.
1550 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1551 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1552 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1553 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1554 G_TYPE_UINT, G_TYPE_UINT);
1556 * GstRtpBin::on-bye-timeout:
1557 * @rtpbin: the object which received the signal
1558 * @session: the session
1561 * Notify of an SSRC that has timed out because of BYE
1563 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1564 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1565 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1566 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1567 G_TYPE_UINT, G_TYPE_UINT);
1569 * GstRtpBin::on-timeout:
1570 * @rtpbin: the object which received the signal
1571 * @session: the session
1574 * Notify of an SSRC that has timed out
1576 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1577 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1578 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_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-sender-timeout:
1583 * @rtpbin: the object which received the signal
1584 * @session: the session
1587 * Notify of a sender SSRC that has timed out and became a receiver
1589 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1590 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1591 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1592 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1593 G_TYPE_UINT, G_TYPE_UINT);
1596 * GstRtpBin::on-npt-stop:
1597 * @rtpbin: the object which received the signal
1598 * @session: the session
1601 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1603 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1604 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1605 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1606 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1607 G_TYPE_UINT, G_TYPE_UINT);
1609 g_object_class_install_property (gobject_class, PROP_SDES,
1610 g_param_spec_boxed ("sdes", "SDES",
1611 "The SDES items of this session",
1612 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1614 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1615 g_param_spec_boolean ("do-lost", "Do Lost",
1616 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1617 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1619 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1620 g_param_spec_boolean ("autoremove", "Auto Remove",
1621 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1622 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1624 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1625 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1626 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1627 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1629 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1630 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1631 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1632 DEFAULT_USE_PIPELINE_CLOCK,
1633 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1635 * GstRtpBin::buffer-mode:
1637 * Control the buffering and timestamping mode used by the jitterbuffer.
1641 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1642 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1643 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1644 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1646 * GstRtpBin::ntp-sync:
1648 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1649 * between the receivers and the senders (such as when using ntpd) this option
1650 * can be used to synchronize receivers on multiple machines.
1654 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1655 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1656 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1657 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1659 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1660 gstelement_class->request_new_pad =
1661 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1662 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1665 gst_element_class_add_pad_template (gstelement_class,
1666 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1667 gst_element_class_add_pad_template (gstelement_class,
1668 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1669 gst_element_class_add_pad_template (gstelement_class,
1670 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1673 gst_element_class_add_pad_template (gstelement_class,
1674 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1675 gst_element_class_add_pad_template (gstelement_class,
1676 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1677 gst_element_class_add_pad_template (gstelement_class,
1678 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1680 gst_element_class_set_details_simple (gstelement_class, "RTP Bin",
1681 "Filter/Network/RTP",
1682 "Real-Time Transport Protocol bin",
1683 "Wim Taymans <wim.taymans@gmail.com>");
1685 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1687 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1688 klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1689 klass->get_internal_session =
1690 GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1692 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1696 gst_rtp_bin_init (GstRtpBin * rtpbin)
1700 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1701 rtpbin->priv->bin_lock = g_mutex_new ();
1702 rtpbin->priv->dyn_lock = g_mutex_new ();
1704 rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1705 rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1706 rtpbin->do_lost = DEFAULT_DO_LOST;
1707 rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1708 rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1709 rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1710 rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1711 rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1713 /* some default SDES entries */
1714 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1715 rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1716 "cname", G_TYPE_STRING, str,
1717 "name", G_TYPE_STRING, g_get_real_name (),
1718 "tool", G_TYPE_STRING, "GStreamer", NULL);
1723 gst_rtp_bin_dispose (GObject * object)
1727 rtpbin = GST_RTP_BIN (object);
1729 GST_DEBUG_OBJECT (object, "freeing sessions");
1730 g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1731 g_slist_free (rtpbin->sessions);
1732 rtpbin->sessions = NULL;
1733 GST_DEBUG_OBJECT (object, "freeing clients");
1734 g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1735 g_slist_free (rtpbin->clients);
1736 rtpbin->clients = NULL;
1738 G_OBJECT_CLASS (parent_class)->dispose (object);
1742 gst_rtp_bin_finalize (GObject * object)
1746 rtpbin = GST_RTP_BIN (object);
1749 gst_structure_free (rtpbin->sdes);
1751 g_mutex_free (rtpbin->priv->bin_lock);
1752 g_mutex_free (rtpbin->priv->dyn_lock);
1754 G_OBJECT_CLASS (parent_class)->finalize (object);
1759 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1766 GST_RTP_BIN_LOCK (bin);
1768 GST_OBJECT_LOCK (bin);
1770 gst_structure_free (bin->sdes);
1771 bin->sdes = gst_structure_copy (sdes);
1772 GST_OBJECT_UNLOCK (bin);
1774 /* store in all sessions */
1775 for (item = bin->sessions; item; item = g_slist_next (item)) {
1776 GstRtpBinSession *session = item->data;
1777 g_object_set (session->session, "sdes", sdes, NULL);
1780 GST_RTP_BIN_UNLOCK (bin);
1783 static GstStructure *
1784 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1786 GstStructure *result;
1788 GST_OBJECT_LOCK (bin);
1789 result = gst_structure_copy (bin->sdes);
1790 GST_OBJECT_UNLOCK (bin);
1796 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1797 const GValue * value, GParamSpec * pspec)
1801 rtpbin = GST_RTP_BIN (object);
1805 GST_RTP_BIN_LOCK (rtpbin);
1806 rtpbin->latency_ms = g_value_get_uint (value);
1807 rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1808 GST_RTP_BIN_UNLOCK (rtpbin);
1809 /* propagate the property down to the jitterbuffer */
1810 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1813 gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1816 GST_RTP_BIN_LOCK (rtpbin);
1817 rtpbin->do_lost = g_value_get_boolean (value);
1818 GST_RTP_BIN_UNLOCK (rtpbin);
1819 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1822 rtpbin->ntp_sync = g_value_get_boolean (value);
1824 case PROP_IGNORE_PT:
1825 rtpbin->ignore_pt = g_value_get_boolean (value);
1827 case PROP_AUTOREMOVE:
1828 rtpbin->priv->autoremove = g_value_get_boolean (value);
1830 case PROP_USE_PIPELINE_CLOCK:
1833 GST_RTP_BIN_LOCK (rtpbin);
1834 rtpbin->use_pipeline_clock = g_value_get_boolean (value);
1835 for (sessions = rtpbin->sessions; sessions;
1836 sessions = g_slist_next (sessions)) {
1837 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1839 g_object_set (G_OBJECT (session->session),
1840 "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
1842 GST_RTP_BIN_UNLOCK (rtpbin);
1845 case PROP_BUFFER_MODE:
1846 GST_RTP_BIN_LOCK (rtpbin);
1847 rtpbin->buffer_mode = g_value_get_enum (value);
1848 GST_RTP_BIN_UNLOCK (rtpbin);
1849 /* propagate the property down to the jitterbuffer */
1850 gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
1853 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1859 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1860 GValue * value, GParamSpec * pspec)
1864 rtpbin = GST_RTP_BIN (object);
1868 GST_RTP_BIN_LOCK (rtpbin);
1869 g_value_set_uint (value, rtpbin->latency_ms);
1870 GST_RTP_BIN_UNLOCK (rtpbin);
1873 g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
1876 GST_RTP_BIN_LOCK (rtpbin);
1877 g_value_set_boolean (value, rtpbin->do_lost);
1878 GST_RTP_BIN_UNLOCK (rtpbin);
1880 case PROP_IGNORE_PT:
1881 g_value_set_boolean (value, rtpbin->ignore_pt);
1884 g_value_set_boolean (value, rtpbin->ntp_sync);
1886 case PROP_AUTOREMOVE:
1887 g_value_set_boolean (value, rtpbin->priv->autoremove);
1889 case PROP_BUFFER_MODE:
1890 g_value_set_enum (value, rtpbin->buffer_mode);
1892 case PROP_USE_PIPELINE_CLOCK:
1893 g_value_set_boolean (value, rtpbin->use_pipeline_clock);
1896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1902 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1906 rtpbin = GST_RTP_BIN (bin);
1908 switch (GST_MESSAGE_TYPE (message)) {
1909 case GST_MESSAGE_ELEMENT:
1911 const GstStructure *s = gst_message_get_structure (message);
1913 /* we change the structure name and add the session ID to it */
1914 if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
1915 GstRtpBinSession *sess;
1917 /* find the session we set it as object data */
1918 sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1919 "GstRTPBin.session");
1921 if (G_LIKELY (sess)) {
1922 message = gst_message_make_writable (message);
1923 s = gst_message_get_structure (message);
1924 gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1928 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1931 case GST_MESSAGE_BUFFERING:
1934 gint min_percent = 100;
1935 GSList *sessions, *streams;
1936 GstRtpBinStream *stream;
1937 gboolean change = FALSE, active = FALSE;
1938 GstClockTime min_out_time;
1939 GstBufferingMode mode;
1940 gint avg_in, avg_out;
1941 gint64 buffering_left;
1943 gst_message_parse_buffering (message, &percent);
1944 gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
1948 g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1949 "GstRTPBin.stream");
1951 GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
1953 /* get the stream */
1954 if (G_LIKELY (stream)) {
1955 GST_RTP_BIN_LOCK (rtpbin);
1956 /* fill in the percent */
1957 stream->percent = percent;
1959 /* calculate the min value for all streams */
1960 for (sessions = rtpbin->sessions; sessions;
1961 sessions = g_slist_next (sessions)) {
1962 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1964 GST_RTP_SESSION_LOCK (session);
1965 if (session->streams) {
1966 for (streams = session->streams; streams;
1967 streams = g_slist_next (streams)) {
1968 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1970 GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
1973 /* find min percent */
1974 if (min_percent > stream->percent)
1975 min_percent = stream->percent;
1978 GST_INFO_OBJECT (bin,
1979 "session has no streams, setting min_percent to 0");
1982 GST_RTP_SESSION_UNLOCK (session);
1984 GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
1986 if (rtpbin->buffering) {
1987 if (min_percent == 100) {
1988 rtpbin->buffering = FALSE;
1993 if (min_percent < 100) {
1994 /* pause the streams */
1995 rtpbin->buffering = TRUE;
2000 GST_RTP_BIN_UNLOCK (rtpbin);
2002 gst_message_unref (message);
2004 /* make a new buffering message with the min value */
2006 gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2007 gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2010 if (G_UNLIKELY (change)) {
2012 guint64 running_time = 0;
2015 /* figure out the running time when we have a clock */
2016 if (G_LIKELY ((clock =
2017 gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2018 guint64 now, base_time;
2020 now = gst_clock_get_time (clock);
2021 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2022 running_time = now - base_time;
2024 GST_DEBUG_OBJECT (bin,
2025 "running time now %" GST_TIME_FORMAT,
2026 GST_TIME_ARGS (running_time));
2028 GST_RTP_BIN_LOCK (rtpbin);
2030 /* when we reactivate, calculate the offsets so that all streams have
2031 * an output time that is at least as big as the running_time */
2034 if (running_time > rtpbin->buffer_start) {
2035 offset = running_time - rtpbin->buffer_start;
2036 if (offset >= rtpbin->latency_ns)
2037 offset -= rtpbin->latency_ns;
2043 /* pause all streams */
2045 for (sessions = rtpbin->sessions; sessions;
2046 sessions = g_slist_next (sessions)) {
2047 GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2049 GST_RTP_SESSION_LOCK (session);
2050 for (streams = session->streams; streams;
2051 streams = g_slist_next (streams)) {
2052 GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2053 GstElement *element = stream->buffer;
2056 g_signal_emit_by_name (element, "set-active", active, offset,
2060 g_object_get (element, "percent", &stream->percent, NULL);
2064 if (min_out_time == -1 || last_out < min_out_time)
2065 min_out_time = last_out;
2068 GST_DEBUG_OBJECT (bin,
2069 "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2070 GST_TIME_FORMAT ", percent %d", element, active,
2071 GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2074 GST_RTP_SESSION_UNLOCK (session);
2076 GST_DEBUG_OBJECT (bin,
2077 "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2079 /* the buffer_start is the min out time of all paused jitterbuffers */
2081 rtpbin->buffer_start = min_out_time;
2083 GST_RTP_BIN_UNLOCK (rtpbin);
2086 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2091 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2097 static GstStateChangeReturn
2098 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2100 GstStateChangeReturn res;
2102 GstRtpBinPrivate *priv;
2104 rtpbin = GST_RTP_BIN (element);
2105 priv = rtpbin->priv;
2107 switch (transition) {
2108 case GST_STATE_CHANGE_NULL_TO_READY:
2110 case GST_STATE_CHANGE_READY_TO_PAUSED:
2111 GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2112 g_atomic_int_set (&priv->shutdown, 0);
2114 case GST_STATE_CHANGE_PAUSED_TO_READY:
2115 GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2116 g_atomic_int_set (&priv->shutdown, 1);
2117 /* wait for all callbacks to end by taking the lock. No new callbacks will
2118 * be able to happen as we set the shutdown flag. */
2119 GST_RTP_BIN_DYN_LOCK (rtpbin);
2120 GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2121 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2127 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2129 switch (transition) {
2130 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2132 case GST_STATE_CHANGE_PAUSED_TO_READY:
2134 case GST_STATE_CHANGE_READY_TO_NULL:
2142 /* a new pad (SSRC) was created in @session. This signal is emited from the
2143 * payload demuxer. */
2145 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2146 GstRtpBinStream * stream)
2149 GstElementClass *klass;
2150 GstPadTemplate *templ;
2154 rtpbin = stream->bin;
2156 GST_DEBUG ("new payload pad %d", pt);
2158 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2160 /* ghost the pad to the parent */
2161 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2162 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2163 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2164 stream->session->id, stream->ssrc, pt);
2165 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2167 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2169 gst_pad_set_active (gpad, TRUE);
2170 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2172 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2178 GST_DEBUG ("ignoring, we are shutting down");
2184 payload_pad_removed (GstElement * element, GstPad * pad,
2185 GstRtpBinStream * stream)
2190 rtpbin = stream->bin;
2192 GST_DEBUG ("payload pad removed");
2194 GST_RTP_BIN_DYN_LOCK (rtpbin);
2195 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2196 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2198 gst_pad_set_active (gpad, FALSE);
2199 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2201 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2205 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2210 rtpbin = session->bin;
2212 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2215 caps = get_pt_map (session, pt);
2224 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2230 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2232 GST_DEBUG_OBJECT (session->bin,
2233 "emiting signal for pt type changed to %d in session %d", pt,
2236 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2237 0, session->id, pt);
2240 /* emited when caps changed for the session */
2242 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2247 const GstStructure *s;
2251 g_object_get (pad, "caps", &caps, NULL);
2256 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2258 s = gst_caps_get_structure (caps, 0);
2260 /* get payload, finish when it's not there */
2261 if (!gst_structure_get_int (s, "payload", &payload))
2264 GST_RTP_SESSION_LOCK (session);
2265 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2266 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2267 GST_RTP_SESSION_UNLOCK (session);
2270 /* a new pad (SSRC) was created in @session */
2272 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2273 GstRtpBinSession * session)
2276 GstRtpBinStream *stream;
2277 GstPad *sinkpad, *srcpad;
2280 rtpbin = session->bin;
2282 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2283 GST_DEBUG_PAD_NAME (pad));
2285 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2287 GST_RTP_SESSION_LOCK (session);
2289 /* create new stream */
2290 stream = create_stream (session, ssrc);
2294 /* get pad and link */
2295 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2296 padname = g_strdup_printf ("src_%d", ssrc);
2297 srcpad = gst_element_get_static_pad (element, padname);
2299 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2300 gst_pad_link (srcpad, sinkpad);
2301 gst_object_unref (sinkpad);
2302 gst_object_unref (srcpad);
2304 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2305 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2306 srcpad = gst_element_get_static_pad (element, padname);
2308 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2309 gst_pad_link (srcpad, sinkpad);
2310 gst_object_unref (sinkpad);
2311 gst_object_unref (srcpad);
2313 /* connect to the RTCP sync signal from the jitterbuffer */
2314 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2315 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2316 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2318 if (stream->demux) {
2319 /* connect to the new-pad signal of the payload demuxer, this will expose the
2320 * new pad by ghosting it. */
2321 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2322 "new-payload-type", (GCallback) new_payload_found, stream);
2323 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2324 "pad-removed", (GCallback) payload_pad_removed, stream);
2326 /* connect to the request-pt-map signal. This signal will be emited by the
2327 * demuxer so that it can apply a proper caps on the buffers for the
2329 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2330 "request-pt-map", (GCallback) pt_map_requested, session);
2331 /* connect to the signal so it can be forwarded. */
2332 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2333 "payload-type-change", (GCallback) payload_type_change, session);
2335 /* add gstrtpjitterbuffer src pad to pads */
2336 GstElementClass *klass;
2337 GstPadTemplate *templ;
2341 pad = gst_element_get_static_pad (stream->buffer, "src");
2343 /* ghost the pad to the parent */
2344 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2345 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2346 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2347 stream->session->id, stream->ssrc, 255);
2348 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2351 gst_pad_set_active (gpad, TRUE);
2352 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2354 gst_object_unref (pad);
2357 GST_RTP_SESSION_UNLOCK (session);
2358 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2365 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2370 GST_RTP_SESSION_UNLOCK (session);
2371 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2372 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2377 /* Create a pad for receiving RTP for the session in @name. Must be called with
2381 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2385 GstRtpBinSession *session;
2386 GstPadLinkReturn lres;
2388 /* first get the session number */
2389 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2392 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2394 /* get or create session */
2395 session = find_session_by_id (rtpbin, sessid);
2397 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2398 /* create session now */
2399 session = create_session (rtpbin, sessid);
2400 if (session == NULL)
2404 /* check if pad was requested */
2405 if (session->recv_rtp_sink_ghost != NULL)
2406 return session->recv_rtp_sink_ghost;
2408 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2409 /* get recv_rtp pad and store */
2410 session->recv_rtp_sink =
2411 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2412 if (session->recv_rtp_sink == NULL)
2415 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2416 (GCallback) caps_changed, session);
2418 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2419 /* get srcpad, link to SSRCDemux */
2420 session->recv_rtp_src =
2421 gst_element_get_static_pad (session->session, "recv_rtp_src");
2422 if (session->recv_rtp_src == NULL)
2425 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2426 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2427 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2428 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2429 gst_object_unref (sinkdpad);
2430 if (lres != GST_PAD_LINK_OK)
2433 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2434 session->demux_newpad_sig = g_signal_connect (session->demux,
2435 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2436 session->demux_padremoved_sig = g_signal_connect (session->demux,
2437 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2439 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2440 session->recv_rtp_sink_ghost =
2441 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2442 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2443 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2445 return session->recv_rtp_sink_ghost;
2450 g_warning ("gstrtpbin: invalid name given");
2455 /* create_session already warned */
2460 g_warning ("gstrtpbin: failed to get session pad");
2465 g_warning ("gstrtpbin: failed to link pads");
2471 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2473 if (session->demux_newpad_sig) {
2474 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2475 session->demux_newpad_sig = 0;
2477 if (session->demux_padremoved_sig) {
2478 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2479 session->demux_padremoved_sig = 0;
2481 if (session->recv_rtp_src) {
2482 gst_object_unref (session->recv_rtp_src);
2483 session->recv_rtp_src = NULL;
2485 if (session->recv_rtp_sink) {
2486 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2487 gst_object_unref (session->recv_rtp_sink);
2488 session->recv_rtp_sink = NULL;
2490 if (session->recv_rtp_sink_ghost) {
2491 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2492 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2493 session->recv_rtp_sink_ghost);
2494 session->recv_rtp_sink_ghost = NULL;
2498 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2502 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2506 GstRtpBinSession *session;
2508 GstPadLinkReturn lres;
2510 /* first get the session number */
2511 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2514 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2516 /* get or create the session */
2517 session = find_session_by_id (rtpbin, sessid);
2519 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2520 /* create session now */
2521 session = create_session (rtpbin, sessid);
2522 if (session == NULL)
2526 /* check if pad was requested */
2527 if (session->recv_rtcp_sink_ghost != NULL)
2528 return session->recv_rtcp_sink_ghost;
2530 /* get recv_rtp pad and store */
2531 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2532 session->recv_rtcp_sink =
2533 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2534 if (session->recv_rtcp_sink == NULL)
2537 /* get srcpad, link to SSRCDemux */
2538 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2539 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2540 if (session->sync_src == NULL)
2543 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2544 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2545 lres = gst_pad_link (session->sync_src, sinkdpad);
2546 gst_object_unref (sinkdpad);
2547 if (lres != GST_PAD_LINK_OK)
2550 session->recv_rtcp_sink_ghost =
2551 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2552 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2553 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2554 session->recv_rtcp_sink_ghost);
2556 return session->recv_rtcp_sink_ghost;
2561 g_warning ("gstrtpbin: invalid name given");
2566 /* create_session already warned */
2571 g_warning ("gstrtpbin: failed to get session pad");
2576 g_warning ("gstrtpbin: failed to link pads");
2582 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2584 if (session->recv_rtcp_sink_ghost) {
2585 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2586 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2587 session->recv_rtcp_sink_ghost);
2588 session->recv_rtcp_sink_ghost = NULL;
2590 if (session->sync_src) {
2591 /* releasing the request pad should also unref the sync pad */
2592 gst_object_unref (session->sync_src);
2593 session->sync_src = NULL;
2595 if (session->recv_rtcp_sink) {
2596 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2597 gst_object_unref (session->recv_rtcp_sink);
2598 session->recv_rtcp_sink = NULL;
2602 /* Create a pad for sending RTP for the session in @name. Must be called with
2606 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2610 GstRtpBinSession *session;
2611 GstElementClass *klass;
2613 /* first get the session number */
2614 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2617 /* get or create session */
2618 session = find_session_by_id (rtpbin, sessid);
2620 /* create session now */
2621 session = create_session (rtpbin, sessid);
2622 if (session == NULL)
2626 /* check if pad was requested */
2627 if (session->send_rtp_sink_ghost != NULL)
2628 return session->send_rtp_sink_ghost;
2630 /* get send_rtp pad and store */
2631 session->send_rtp_sink =
2632 gst_element_get_request_pad (session->session, "send_rtp_sink");
2633 if (session->send_rtp_sink == NULL)
2636 session->send_rtp_sink_ghost =
2637 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2638 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2639 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2642 session->send_rtp_src =
2643 gst_element_get_static_pad (session->session, "send_rtp_src");
2644 if (session->send_rtp_src == NULL)
2647 /* ghost the new source pad */
2648 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2649 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2650 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2651 session->send_rtp_src_ghost =
2652 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2653 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2654 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2657 return session->send_rtp_sink_ghost;
2662 g_warning ("gstrtpbin: invalid name given");
2667 /* create_session already warned */
2672 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2677 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2684 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2686 if (session->send_rtp_src_ghost) {
2687 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2688 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2689 session->send_rtp_src_ghost);
2690 session->send_rtp_src_ghost = NULL;
2692 if (session->send_rtp_src) {
2693 gst_object_unref (session->send_rtp_src);
2694 session->send_rtp_src = NULL;
2696 if (session->send_rtp_sink) {
2697 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2698 session->send_rtp_sink);
2699 gst_object_unref (session->send_rtp_sink);
2700 session->send_rtp_sink = NULL;
2702 if (session->send_rtp_sink_ghost) {
2703 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2704 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2705 session->send_rtp_sink_ghost);
2706 session->send_rtp_sink_ghost = NULL;
2710 /* Create a pad for sending RTCP for the session in @name. Must be called with
2714 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2717 GstRtpBinSession *session;
2719 /* first get the session number */
2720 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2723 /* get or create session */
2724 session = find_session_by_id (rtpbin, sessid);
2728 /* check if pad was requested */
2729 if (session->send_rtcp_src_ghost != NULL)
2730 return session->send_rtcp_src_ghost;
2732 /* get rtcp_src pad and store */
2733 session->send_rtcp_src =
2734 gst_element_get_request_pad (session->session, "send_rtcp_src");
2735 if (session->send_rtcp_src == NULL)
2738 session->send_rtcp_src_ghost =
2739 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2740 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2741 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2743 return session->send_rtcp_src_ghost;
2748 g_warning ("gstrtpbin: invalid name given");
2753 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2758 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2764 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2766 if (session->send_rtcp_src_ghost) {
2767 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2768 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2769 session->send_rtcp_src_ghost);
2770 session->send_rtcp_src_ghost = NULL;
2772 if (session->send_rtcp_src) {
2773 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2774 gst_object_unref (session->send_rtcp_src);
2775 session->send_rtcp_src = NULL;
2779 /* If the requested name is NULL we should create a name with
2780 * the session number assuming we want the lowest posible session
2781 * with a free pad like the template */
2783 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2785 gboolean name_found = FALSE;
2787 GstIterator *pad_it = NULL;
2788 gchar *pad_name = NULL;
2789 GValue data = { 0, };
2791 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2792 while (!name_found) {
2793 gboolean done = FALSE;
2796 pad_name = g_strdup_printf (templ->name_template, session++);
2797 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2800 switch (gst_iterator_next (pad_it, &data)) {
2801 case GST_ITERATOR_OK:
2806 pad = g_value_get_object (&data);
2807 name = gst_pad_get_name (pad);
2809 if (strcmp (name, pad_name) == 0) {
2814 g_value_reset (&data);
2817 case GST_ITERATOR_ERROR:
2818 case GST_ITERATOR_RESYNC:
2819 /* restart iteration */
2824 case GST_ITERATOR_DONE:
2829 g_value_unset (&data);
2830 gst_iterator_free (pad_it);
2833 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2840 gst_rtp_bin_request_new_pad (GstElement * element,
2841 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2844 GstElementClass *klass;
2847 gchar *pad_name = NULL;
2849 g_return_val_if_fail (templ != NULL, NULL);
2850 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2852 rtpbin = GST_RTP_BIN (element);
2853 klass = GST_ELEMENT_GET_CLASS (element);
2855 GST_RTP_BIN_LOCK (rtpbin);
2858 /* use a free pad name */
2859 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2861 /* use the provided name */
2862 pad_name = g_strdup (name);
2865 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2867 /* figure out the template */
2868 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2869 result = create_recv_rtp (rtpbin, templ, pad_name);
2870 } else if (templ == gst_element_class_get_pad_template (klass,
2871 "recv_rtcp_sink_%d")) {
2872 result = create_recv_rtcp (rtpbin, templ, pad_name);
2873 } else if (templ == gst_element_class_get_pad_template (klass,
2874 "send_rtp_sink_%d")) {
2875 result = create_send_rtp (rtpbin, templ, pad_name);
2876 } else if (templ == gst_element_class_get_pad_template (klass,
2877 "send_rtcp_src_%d")) {
2878 result = create_rtcp (rtpbin, templ, pad_name);
2880 goto wrong_template;
2883 GST_RTP_BIN_UNLOCK (rtpbin);
2891 GST_RTP_BIN_UNLOCK (rtpbin);
2892 g_warning ("gstrtpbin: this is not our template");
2898 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2900 GstRtpBinSession *session;
2903 g_return_if_fail (GST_IS_GHOST_PAD (pad));
2904 g_return_if_fail (GST_IS_RTP_BIN (element));
2906 rtpbin = GST_RTP_BIN (element);
2908 GST_RTP_BIN_LOCK (rtpbin);
2909 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2910 GST_DEBUG_PAD_NAME (pad));
2912 if (!(session = find_session_by_pad (rtpbin, pad)))
2915 if (session->recv_rtp_sink_ghost == pad) {
2916 remove_recv_rtp (rtpbin, session);
2917 } else if (session->recv_rtcp_sink_ghost == pad) {
2918 remove_recv_rtcp (rtpbin, session);
2919 } else if (session->send_rtp_sink_ghost == pad) {
2920 remove_send_rtp (rtpbin, session);
2921 } else if (session->send_rtcp_src_ghost == pad) {
2922 remove_rtcp (rtpbin, session);
2925 /* no more request pads, free the complete session */
2926 if (session->recv_rtp_sink_ghost == NULL
2927 && session->recv_rtcp_sink_ghost == NULL
2928 && session->send_rtp_sink_ghost == NULL
2929 && session->send_rtcp_src_ghost == NULL) {
2930 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2931 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2932 free_session (session, rtpbin);
2934 GST_RTP_BIN_UNLOCK (rtpbin);
2941 GST_RTP_BIN_UNLOCK (rtpbin);
2942 g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2943 GST_DEBUG_PAD_NAME (pad));