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;
1157 GST_DEBUG_OBJECT (bin, "sync handler called");
1159 /* get the last relation between the rtp timestamps and the gstreamer
1160 * timestamps. We get this info directly from the jitterbuffer which
1161 * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1162 * what the current situation is. */
1164 g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1165 base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1166 clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1168 g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1169 buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1173 GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
1174 /* first packet must be SR or RR or else the validate would have failed */
1175 switch (gst_rtcp_packet_get_type (&packet)) {
1176 case GST_RTCP_TYPE_SR:
1177 /* only parse first. There is only supposed to be one SR in the packet
1178 * but we will deal with malformed packets gracefully */
1181 /* get NTP and RTP times */
1182 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1185 GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1186 /* ignore SR that is not ours */
1187 if (ssrc != stream->ssrc)
1192 case GST_RTCP_TYPE_SDES:
1194 gboolean more_items, more_entries;
1196 /* only deal with first SDES, there is only supposed to be one SDES in
1197 * the RTCP packet but we deal with bad packets gracefully. Also bail
1198 * out if we have not seen an SR item yet. */
1199 if (have_sdes || !have_sr)
1202 GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1203 /* skip items that are not about the SSRC of the sender */
1204 if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1207 /* find the CNAME entry */
1208 GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1209 GstRTCPSDESType type;
1213 gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1215 if (type == GST_RTCP_SDES_CNAME) {
1216 GST_RTP_BIN_LOCK (bin);
1217 /* associate the stream to CNAME */
1218 gst_rtp_bin_associate (bin, stream, len, data,
1219 ntptime, extrtptime, base_rtptime, base_time, clock_rate);
1220 GST_RTP_BIN_UNLOCK (bin);
1228 /* we can ignore these packets */
1234 /* create a new stream with @ssrc in @session. Must be called with
1235 * RTP_SESSION_LOCK. */
1236 static GstRtpBinStream *
1237 create_stream (GstRtpBinSession * session, guint32 ssrc)
1239 GstElement *buffer, *demux = NULL;
1240 GstRtpBinStream *stream;
1244 rtpbin = session->bin;
1246 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
1247 goto no_jitterbuffer;
1249 if (!rtpbin->ignore_pt)
1250 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
1254 stream = g_new0 (GstRtpBinStream, 1);
1255 stream->ssrc = ssrc;
1256 stream->bin = rtpbin;
1257 stream->session = session;
1258 stream->buffer = buffer;
1259 stream->demux = demux;
1261 stream->have_sync = FALSE;
1262 stream->rt_delta = 0;
1263 stream->percent = 100;
1264 session->streams = g_slist_prepend (session->streams, stream);
1266 /* provide clock_rate to the jitterbuffer when needed */
1267 stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1268 (GCallback) pt_map_requested, session);
1269 stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1270 (GCallback) on_npt_stop, stream);
1272 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1273 g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1275 /* configure latency and packet lost */
1276 g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1277 g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1278 g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1280 if (!rtpbin->ignore_pt)
1281 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1282 gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1286 gst_element_link (buffer, demux);
1288 if (rtpbin->buffering) {
1291 GST_INFO_OBJECT (rtpbin,
1292 "bin is buffering, set jitterbuffer as not active");
1293 g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1297 GST_OBJECT_LOCK (rtpbin);
1298 target = GST_STATE_TARGET (rtpbin);
1299 GST_OBJECT_UNLOCK (rtpbin);
1301 /* from sink to source */
1303 gst_element_set_state (demux, target);
1305 gst_element_set_state (buffer, target);
1312 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1317 gst_object_unref (buffer);
1318 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1324 free_stream (GstRtpBinStream * stream)
1326 GstRtpBinSession *session;
1328 session = stream->session;
1330 if (stream->demux) {
1331 g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1332 g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1333 g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1335 g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1336 g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1337 g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1340 gst_element_set_locked_state (stream->demux, TRUE);
1341 gst_element_set_locked_state (stream->buffer, TRUE);
1344 gst_element_set_state (stream->demux, GST_STATE_NULL);
1345 gst_element_set_state (stream->buffer, GST_STATE_NULL);
1347 /* now remove this signal, we need this while going to NULL because it to
1348 * do some cleanups */
1350 g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1352 gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1354 gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1359 /* GObject vmethods */
1360 static void gst_rtp_bin_dispose (GObject * object);
1361 static void gst_rtp_bin_finalize (GObject * object);
1362 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1363 const GValue * value, GParamSpec * pspec);
1364 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1365 GValue * value, GParamSpec * pspec);
1367 /* GstElement vmethods */
1368 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1369 GstStateChange transition);
1370 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1371 GstPadTemplate * templ, const gchar * name);
1372 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1373 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1375 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1378 gst_rtp_bin_base_init (gpointer klass)
1380 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1383 gst_element_class_add_pad_template (element_class,
1384 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1385 gst_element_class_add_pad_template (element_class,
1386 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1387 gst_element_class_add_pad_template (element_class,
1388 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1391 gst_element_class_add_pad_template (element_class,
1392 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1393 gst_element_class_add_pad_template (element_class,
1394 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1395 gst_element_class_add_pad_template (element_class,
1396 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1398 gst_element_class_set_details_simple (element_class, "RTP Bin",
1399 "Filter/Network/RTP",
1400 "Real-Time Transport Protocol bin",
1401 "Wim Taymans <wim.taymans@gmail.com>");
1405 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1407 GObjectClass *gobject_class;
1408 GstElementClass *gstelement_class;
1409 GstBinClass *gstbin_class;
1411 gobject_class = (GObjectClass *) klass;
1412 gstelement_class = (GstElementClass *) klass;
1413 gstbin_class = (GstBinClass *) klass;
1415 g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1417 gobject_class->dispose = gst_rtp_bin_dispose;
1418 gobject_class->finalize = gst_rtp_bin_finalize;
1419 gobject_class->set_property = gst_rtp_bin_set_property;
1420 gobject_class->get_property = gst_rtp_bin_get_property;
1422 g_object_class_install_property (gobject_class, PROP_LATENCY,
1423 g_param_spec_uint ("latency", "Buffer latency in ms",
1424 "Default amount of ms to buffer in the jitterbuffers", 0,
1425 G_MAXUINT, DEFAULT_LATENCY_MS,
1426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1429 * GstRtpBin::request-pt-map:
1430 * @rtpbin: the object which received the signal
1431 * @session: the session
1434 * Request the payload type as #GstCaps for @pt in @session.
1436 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1437 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1438 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1439 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1440 G_TYPE_UINT, G_TYPE_UINT);
1443 * GstRtpBin::payload-type-change:
1444 * @rtpbin: the object which received the signal
1445 * @session: the session
1448 * Signal that the current payload type changed to @pt in @session.
1452 gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1453 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1454 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1455 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1456 G_TYPE_UINT, G_TYPE_UINT);
1459 * GstRtpBin::clear-pt-map:
1460 * @rtpbin: the object which received the signal
1462 * Clear all previously cached pt-mapping obtained with
1463 * #GstRtpBin::request-pt-map.
1465 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1466 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1467 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1468 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1472 * GstRtpBin::reset-sync:
1473 * @rtpbin: the object which received the signal
1475 * Reset all currently configured lip-sync parameters and require new SR
1476 * packets for all streams before lip-sync is attempted again.
1478 gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1479 g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1480 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1481 reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1485 * GstRtpBin::get-internal-session:
1486 * @rtpbin: the object which received the signal
1487 * @id: the session id
1489 * Request the internal RTPSession object as #GObject in session @id.
1491 gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1492 g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1493 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1494 get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1495 RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1498 * GstRtpBin::on-new-ssrc:
1499 * @rtpbin: the object which received the signal
1500 * @session: the session
1503 * Notify of a new SSRC that entered @session.
1505 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1506 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1507 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1508 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1509 G_TYPE_UINT, G_TYPE_UINT);
1511 * GstRtpBin::on-ssrc-collision:
1512 * @rtpbin: the object which received the signal
1513 * @session: the session
1516 * Notify when we have an SSRC collision
1518 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1519 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1520 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1521 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1522 G_TYPE_UINT, G_TYPE_UINT);
1524 * GstRtpBin::on-ssrc-validated:
1525 * @rtpbin: the object which received the signal
1526 * @session: the session
1529 * Notify of a new SSRC that became validated.
1531 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1532 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1533 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1534 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1535 G_TYPE_UINT, G_TYPE_UINT);
1537 * GstRtpBin::on-ssrc-active:
1538 * @rtpbin: the object which received the signal
1539 * @session: the session
1542 * Notify of a SSRC that is active, i.e., sending RTCP.
1544 gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1545 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1546 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1547 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1548 G_TYPE_UINT, G_TYPE_UINT);
1550 * GstRtpBin::on-ssrc-sdes:
1551 * @rtpbin: the object which received the signal
1552 * @session: the session
1555 * Notify of a SSRC that is active, i.e., sending RTCP.
1557 gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1558 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1559 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1560 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1561 G_TYPE_UINT, G_TYPE_UINT);
1564 * GstRtpBin::on-bye-ssrc:
1565 * @rtpbin: the object which received the signal
1566 * @session: the session
1569 * Notify of an SSRC that became inactive because of a BYE packet.
1571 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1572 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1573 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1574 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1575 G_TYPE_UINT, G_TYPE_UINT);
1577 * GstRtpBin::on-bye-timeout:
1578 * @rtpbin: the object which received the signal
1579 * @session: the session
1582 * Notify of an SSRC that has timed out because of BYE
1584 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1585 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1586 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1587 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1588 G_TYPE_UINT, G_TYPE_UINT);
1590 * GstRtpBin::on-timeout:
1591 * @rtpbin: the object which received the signal
1592 * @session: the session
1595 * Notify of an SSRC that has timed out
1597 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1598 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1599 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1600 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1601 G_TYPE_UINT, G_TYPE_UINT);
1603 * GstRtpBin::on-sender-timeout:
1604 * @rtpbin: the object which received the signal
1605 * @session: the session
1608 * Notify of a sender SSRC that has timed out and became a receiver
1610 gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1611 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1612 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1613 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1614 G_TYPE_UINT, G_TYPE_UINT);
1617 * GstRtpBin::on-npt-stop:
1618 * @rtpbin: the object which received the signal
1619 * @session: the session
1622 * Notify that SSRC sender has sent data up to the configured NPT stop time.
1624 gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1625 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1626 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1627 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1628 G_TYPE_UINT, G_TYPE_UINT);
1630 g_object_class_install_property (gobject_class, PROP_SDES,
1631 g_param_spec_boxed ("sdes", "SDES",
1632 "The SDES items of this session",
1633 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1635 g_object_class_install_property (gobject_class, PROP_DO_LOST,
1636 g_param_spec_boolean ("do-lost", "Do Lost",
1637 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1638 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1640 g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1641 g_param_spec_boolean ("autoremove", "Auto Remove",
1642 "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1643 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1645 g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1646 g_param_spec_boolean ("ignore-pt", "Ignore PT",
1647 "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1648 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1650 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1651 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1652 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1653 DEFAULT_USE_PIPELINE_CLOCK,
1654 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1656 * GstRtpBin::buffer-mode:
1658 * Control the buffering and timestamping mode used by the jitterbuffer.
1662 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1663 g_param_spec_enum ("buffer-mode", "Buffer Mode",
1664 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1665 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1667 * GstRtpBin::ntp-sync:
1669 * Synchronize received streams to the NTP clock. When the NTP clock is shared
1670 * between the receivers and the senders (such as when using ntpd) this option
1671 * can be used to synchronize receivers on multiple machines.
1675 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1676 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1677 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1678 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1680 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1681 gstelement_class->request_new_pad =
1682 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1683 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
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, GstRtpBinClass * klass)
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_caps (gpad, GST_PAD_CAPS (pad));
2170 gst_pad_set_active (gpad, TRUE);
2171 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2173 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2179 GST_DEBUG ("ignoring, we are shutting down");
2185 payload_pad_removed (GstElement * element, GstPad * pad,
2186 GstRtpBinStream * stream)
2191 rtpbin = stream->bin;
2193 GST_DEBUG ("payload pad removed");
2195 GST_RTP_BIN_DYN_LOCK (rtpbin);
2196 if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2197 g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2199 gst_pad_set_active (gpad, FALSE);
2200 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2202 GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2206 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2211 rtpbin = session->bin;
2213 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2216 caps = get_pt_map (session, pt);
2225 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2231 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2233 GST_DEBUG_OBJECT (session->bin,
2234 "emiting signal for pt type changed to %d in session %d", pt,
2237 g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2238 0, session->id, pt);
2241 /* emited when caps changed for the session */
2243 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2248 const GstStructure *s;
2252 g_object_get (pad, "caps", &caps, NULL);
2257 GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2259 s = gst_caps_get_structure (caps, 0);
2261 /* get payload, finish when it's not there */
2262 if (!gst_structure_get_int (s, "payload", &payload))
2265 GST_RTP_SESSION_LOCK (session);
2266 GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2267 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2268 GST_RTP_SESSION_UNLOCK (session);
2271 /* a new pad (SSRC) was created in @session */
2273 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2274 GstRtpBinSession * session)
2277 GstRtpBinStream *stream;
2278 GstPad *sinkpad, *srcpad;
2281 rtpbin = session->bin;
2283 GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2284 GST_DEBUG_PAD_NAME (pad));
2286 GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2288 GST_RTP_SESSION_LOCK (session);
2290 /* create new stream */
2291 stream = create_stream (session, ssrc);
2295 /* get pad and link */
2296 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2297 padname = g_strdup_printf ("src_%d", ssrc);
2298 srcpad = gst_element_get_static_pad (element, padname);
2300 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2301 gst_pad_link (srcpad, sinkpad);
2302 gst_object_unref (sinkpad);
2303 gst_object_unref (srcpad);
2305 GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2306 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2307 srcpad = gst_element_get_static_pad (element, padname);
2309 sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2310 gst_pad_link (srcpad, sinkpad);
2311 gst_object_unref (sinkpad);
2312 gst_object_unref (srcpad);
2314 /* connect to the RTCP sync signal from the jitterbuffer */
2315 GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2316 stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2317 "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2319 if (stream->demux) {
2320 /* connect to the new-pad signal of the payload demuxer, this will expose the
2321 * new pad by ghosting it. */
2322 stream->demux_newpad_sig = g_signal_connect (stream->demux,
2323 "new-payload-type", (GCallback) new_payload_found, stream);
2324 stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2325 "pad-removed", (GCallback) payload_pad_removed, stream);
2327 /* connect to the request-pt-map signal. This signal will be emited by the
2328 * demuxer so that it can apply a proper caps on the buffers for the
2330 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2331 "request-pt-map", (GCallback) pt_map_requested, session);
2332 /* connect to the signal so it can be forwarded. */
2333 stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2334 "payload-type-change", (GCallback) payload_type_change, session);
2336 /* add gstrtpjitterbuffer src pad to pads */
2337 GstElementClass *klass;
2338 GstPadTemplate *templ;
2342 pad = gst_element_get_static_pad (stream->buffer, "src");
2344 /* ghost the pad to the parent */
2345 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2346 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2347 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2348 stream->session->id, stream->ssrc, 255);
2349 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2352 gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2353 gst_pad_set_active (gpad, TRUE);
2354 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2356 gst_object_unref (pad);
2359 GST_RTP_SESSION_UNLOCK (session);
2360 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2367 GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2372 GST_RTP_SESSION_UNLOCK (session);
2373 GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2374 GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2379 /* Create a pad for receiving RTP for the session in @name. Must be called with
2383 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2387 GstRtpBinSession *session;
2388 GstPadLinkReturn lres;
2390 /* first get the session number */
2391 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2394 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2396 /* get or create session */
2397 session = find_session_by_id (rtpbin, sessid);
2399 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2400 /* create session now */
2401 session = create_session (rtpbin, sessid);
2402 if (session == NULL)
2406 /* check if pad was requested */
2407 if (session->recv_rtp_sink_ghost != NULL)
2408 return session->recv_rtp_sink_ghost;
2410 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2411 /* get recv_rtp pad and store */
2412 session->recv_rtp_sink =
2413 gst_element_get_request_pad (session->session, "recv_rtp_sink");
2414 if (session->recv_rtp_sink == NULL)
2417 g_signal_connect (session->recv_rtp_sink, "notify::caps",
2418 (GCallback) caps_changed, session);
2420 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2421 /* get srcpad, link to SSRCDemux */
2422 session->recv_rtp_src =
2423 gst_element_get_static_pad (session->session, "recv_rtp_src");
2424 if (session->recv_rtp_src == NULL)
2427 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2428 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2429 GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2430 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2431 gst_object_unref (sinkdpad);
2432 if (lres != GST_PAD_LINK_OK)
2435 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2436 session->demux_newpad_sig = g_signal_connect (session->demux,
2437 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2438 session->demux_padremoved_sig = g_signal_connect (session->demux,
2439 "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2441 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2442 session->recv_rtp_sink_ghost =
2443 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2444 gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2445 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2447 return session->recv_rtp_sink_ghost;
2452 g_warning ("gstrtpbin: invalid name given");
2457 /* create_session already warned */
2462 g_warning ("gstrtpbin: failed to get session pad");
2467 g_warning ("gstrtpbin: failed to link pads");
2473 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2475 if (session->demux_newpad_sig) {
2476 g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2477 session->demux_newpad_sig = 0;
2479 if (session->demux_padremoved_sig) {
2480 g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2481 session->demux_padremoved_sig = 0;
2483 if (session->recv_rtp_src) {
2484 gst_object_unref (session->recv_rtp_src);
2485 session->recv_rtp_src = NULL;
2487 if (session->recv_rtp_sink) {
2488 gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2489 gst_object_unref (session->recv_rtp_sink);
2490 session->recv_rtp_sink = NULL;
2492 if (session->recv_rtp_sink_ghost) {
2493 gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2494 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2495 session->recv_rtp_sink_ghost);
2496 session->recv_rtp_sink_ghost = NULL;
2500 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2504 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2508 GstRtpBinSession *session;
2510 GstPadLinkReturn lres;
2512 /* first get the session number */
2513 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2516 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2518 /* get or create the session */
2519 session = find_session_by_id (rtpbin, sessid);
2521 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2522 /* create session now */
2523 session = create_session (rtpbin, sessid);
2524 if (session == NULL)
2528 /* check if pad was requested */
2529 if (session->recv_rtcp_sink_ghost != NULL)
2530 return session->recv_rtcp_sink_ghost;
2532 /* get recv_rtp pad and store */
2533 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2534 session->recv_rtcp_sink =
2535 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2536 if (session->recv_rtcp_sink == NULL)
2539 /* get srcpad, link to SSRCDemux */
2540 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2541 session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2542 if (session->sync_src == NULL)
2545 GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2546 sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2547 lres = gst_pad_link (session->sync_src, sinkdpad);
2548 gst_object_unref (sinkdpad);
2549 if (lres != GST_PAD_LINK_OK)
2552 session->recv_rtcp_sink_ghost =
2553 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2554 gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2555 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2556 session->recv_rtcp_sink_ghost);
2558 return session->recv_rtcp_sink_ghost;
2563 g_warning ("gstrtpbin: invalid name given");
2568 /* create_session already warned */
2573 g_warning ("gstrtpbin: failed to get session pad");
2578 g_warning ("gstrtpbin: failed to link pads");
2584 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2586 if (session->recv_rtcp_sink_ghost) {
2587 gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2588 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2589 session->recv_rtcp_sink_ghost);
2590 session->recv_rtcp_sink_ghost = NULL;
2592 if (session->sync_src) {
2593 /* releasing the request pad should also unref the sync pad */
2594 gst_object_unref (session->sync_src);
2595 session->sync_src = NULL;
2597 if (session->recv_rtcp_sink) {
2598 gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2599 gst_object_unref (session->recv_rtcp_sink);
2600 session->recv_rtcp_sink = NULL;
2604 /* Create a pad for sending RTP for the session in @name. Must be called with
2608 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2612 GstRtpBinSession *session;
2613 GstElementClass *klass;
2615 /* first get the session number */
2616 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2619 /* get or create session */
2620 session = find_session_by_id (rtpbin, sessid);
2622 /* create session now */
2623 session = create_session (rtpbin, sessid);
2624 if (session == NULL)
2628 /* check if pad was requested */
2629 if (session->send_rtp_sink_ghost != NULL)
2630 return session->send_rtp_sink_ghost;
2632 /* get send_rtp pad and store */
2633 session->send_rtp_sink =
2634 gst_element_get_request_pad (session->session, "send_rtp_sink");
2635 if (session->send_rtp_sink == NULL)
2638 session->send_rtp_sink_ghost =
2639 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2640 gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2641 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2644 session->send_rtp_src =
2645 gst_element_get_static_pad (session->session, "send_rtp_src");
2646 if (session->send_rtp_src == NULL)
2649 /* ghost the new source pad */
2650 klass = GST_ELEMENT_GET_CLASS (rtpbin);
2651 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2652 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2653 session->send_rtp_src_ghost =
2654 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2655 gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2656 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2659 return session->send_rtp_sink_ghost;
2664 g_warning ("gstrtpbin: invalid name given");
2669 /* create_session already warned */
2674 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2679 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2686 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2688 if (session->send_rtp_src_ghost) {
2689 gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2690 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2691 session->send_rtp_src_ghost);
2692 session->send_rtp_src_ghost = NULL;
2694 if (session->send_rtp_src) {
2695 gst_object_unref (session->send_rtp_src);
2696 session->send_rtp_src = NULL;
2698 if (session->send_rtp_sink) {
2699 gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2700 session->send_rtp_sink);
2701 gst_object_unref (session->send_rtp_sink);
2702 session->send_rtp_sink = NULL;
2704 if (session->send_rtp_sink_ghost) {
2705 gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2706 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2707 session->send_rtp_sink_ghost);
2708 session->send_rtp_sink_ghost = NULL;
2712 /* Create a pad for sending RTCP for the session in @name. Must be called with
2716 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2719 GstRtpBinSession *session;
2721 /* first get the session number */
2722 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2725 /* get or create session */
2726 session = find_session_by_id (rtpbin, sessid);
2730 /* check if pad was requested */
2731 if (session->send_rtcp_src_ghost != NULL)
2732 return session->send_rtcp_src_ghost;
2734 /* get rtcp_src pad and store */
2735 session->send_rtcp_src =
2736 gst_element_get_request_pad (session->session, "send_rtcp_src");
2737 if (session->send_rtcp_src == NULL)
2740 session->send_rtcp_src_ghost =
2741 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2742 gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2743 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2745 return session->send_rtcp_src_ghost;
2750 g_warning ("gstrtpbin: invalid name given");
2755 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2760 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2766 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2768 if (session->send_rtcp_src_ghost) {
2769 gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2770 gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2771 session->send_rtcp_src_ghost);
2772 session->send_rtcp_src_ghost = NULL;
2774 if (session->send_rtcp_src) {
2775 gst_element_release_request_pad (session->session, session->send_rtcp_src);
2776 gst_object_unref (session->send_rtcp_src);
2777 session->send_rtcp_src = NULL;
2781 /* If the requested name is NULL we should create a name with
2782 * the session number assuming we want the lowest posible session
2783 * with a free pad like the template */
2785 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2787 gboolean name_found = FALSE;
2789 GstIterator *pad_it = NULL;
2790 gchar *pad_name = NULL;
2792 GST_DEBUG_OBJECT (element, "find a free pad name for template");
2793 while (!name_found) {
2794 gboolean done = FALSE;
2796 pad_name = g_strdup_printf (templ->name_template, session++);
2797 pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2802 switch (gst_iterator_next (pad_it, &data)) {
2803 case GST_ITERATOR_OK:
2808 pad = GST_PAD_CAST (data);
2809 name = gst_pad_get_name (pad);
2811 if (strcmp (name, pad_name) == 0) {
2816 gst_object_unref (pad);
2819 case GST_ITERATOR_ERROR:
2820 case GST_ITERATOR_RESYNC:
2821 /* restart iteration */
2826 case GST_ITERATOR_DONE:
2831 gst_iterator_free (pad_it);
2834 GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2841 gst_rtp_bin_request_new_pad (GstElement * element,
2842 GstPadTemplate * templ, const gchar * name)
2845 GstElementClass *klass;
2848 gchar *pad_name = NULL;
2850 g_return_val_if_fail (templ != NULL, NULL);
2851 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2853 rtpbin = GST_RTP_BIN (element);
2854 klass = GST_ELEMENT_GET_CLASS (element);
2856 GST_RTP_BIN_LOCK (rtpbin);
2859 /* use a free pad name */
2860 pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2862 /* use the provided name */
2863 pad_name = g_strdup (name);
2866 GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2868 /* figure out the template */
2869 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2870 result = create_recv_rtp (rtpbin, templ, pad_name);
2871 } else if (templ == gst_element_class_get_pad_template (klass,
2872 "recv_rtcp_sink_%d")) {
2873 result = create_recv_rtcp (rtpbin, templ, pad_name);
2874 } else if (templ == gst_element_class_get_pad_template (klass,
2875 "send_rtp_sink_%d")) {
2876 result = create_send_rtp (rtpbin, templ, pad_name);
2877 } else if (templ == gst_element_class_get_pad_template (klass,
2878 "send_rtcp_src_%d")) {
2879 result = create_rtcp (rtpbin, templ, pad_name);
2881 goto wrong_template;
2884 GST_RTP_BIN_UNLOCK (rtpbin);
2892 GST_RTP_BIN_UNLOCK (rtpbin);
2893 g_warning ("gstrtpbin: this is not our template");
2899 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2901 GstRtpBinSession *session;
2904 g_return_if_fail (GST_IS_GHOST_PAD (pad));
2905 g_return_if_fail (GST_IS_RTP_BIN (element));
2907 rtpbin = GST_RTP_BIN (element);
2909 GST_RTP_BIN_LOCK (rtpbin);
2910 GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2911 GST_DEBUG_PAD_NAME (pad));
2913 if (!(session = find_session_by_pad (rtpbin, pad)))
2916 if (session->recv_rtp_sink_ghost == pad) {
2917 remove_recv_rtp (rtpbin, session);
2918 } else if (session->recv_rtcp_sink_ghost == pad) {
2919 remove_recv_rtcp (rtpbin, session);
2920 } else if (session->send_rtp_sink_ghost == pad) {
2921 remove_send_rtp (rtpbin, session);
2922 } else if (session->send_rtcp_src_ghost == pad) {
2923 remove_rtcp (rtpbin, session);
2926 /* no more request pads, free the complete session */
2927 if (session->recv_rtp_sink_ghost == NULL
2928 && session->recv_rtcp_sink_ghost == NULL
2929 && session->send_rtp_sink_ghost == NULL
2930 && session->send_rtcp_src_ghost == NULL) {
2931 GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2932 rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2933 free_session (session, rtpbin);
2935 GST_RTP_BIN_UNLOCK (rtpbin);
2942 GST_RTP_BIN_UNLOCK (rtpbin);
2943 g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2944 GST_DEBUG_PAD_NAME (pad));