2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 * @short_description: handle media from one RTP bin
23 * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
27 * RTP bin combines the functions of gstrtpsession, gstrtpssrcdemux, gstrtpjitterbuffer
28 * and gstrtpptdemux in one element. It allows for multiple RTP sessions that will
29 * be synchronized together using RTCP SR packets.
32 * gstrtpbin is configured with a number of request pads that define the
33 * functionality that is activated, similar to the gstrtpsession element.
36 * To use gstrtpbin as an RTP receiver, request a recv_rtp_sink_%%d pad. The session
37 * number must be specified in the pad name.
38 * Data received on the recv_rtp_sink_%%d pad will be processed in the gstrtpsession
39 * manager and after being validated forwarded on gstrtpssrcdemuxer element. Each
40 * RTP stream is demuxed based on the SSRC and send to a gstrtpjitterbuffer. After
41 * the packets are released from the jitterbuffer, they will be forwarded to a
42 * gstrtpptdemuxer element. The gstrtpptdemuxer element will demux the packets based
43 * on the payload type and will create a unique pad recv_rtp_src_%%d_%%d_%%d on
44 * gstrtpbin with the session number, SSRC and payload type respectively as the pad
48 * To also use gstrtpbin as an RTCP receiver, request a recv_rtcp_sink_%%d pad. The
49 * session number must be specified in the pad name.
52 * If you want the session manager to generate and send RTCP packets, request
53 * the send_rtcp_src_%%d pad with the session number in the pad name. Packet pushed
54 * on this pad contain SR/RR RTCP reports that should be sent to all participants
58 * To use gstrtpbin as a sender, request a send_rtp_sink_%%d pad, which will
59 * automatically create a send_rtp_src_%%d pad. The session number must be specified when
60 * requesting the sink pad. The session manager will modify the
61 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
62 * send_rtp_src_%%d pad after updating its internal state.
65 * The session manager needs the clock-rate of the payload types it is handling
66 * and will signal the GstRTPSession::request-pt-map signal when it needs such a
67 * mapping. One can clear the cached values with the GstRTPSession::clear-pt-map
70 * <title>Example pipelines</title>
73 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
74 * gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
76 * Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
80 * Last reviewed on 2007-05-28 (0.10.5)
88 #include "gstrtpbin-marshal.h"
89 #include "gstrtpbin.h"
91 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
92 #define GST_CAT_DEFAULT gst_rtp_bin_debug
95 /* elementfactory information */
96 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
98 "Implement an RTP bin",
99 "Wim Taymans <wim@fluendo.com>");
102 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
103 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
106 GST_STATIC_CAPS ("application/x-rtp")
109 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
110 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
113 GST_STATIC_CAPS ("application/x-rtcp")
116 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
117 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
120 GST_STATIC_CAPS ("application/x-rtp")
124 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
125 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
128 GST_STATIC_CAPS ("application/x-rtp")
131 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
132 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
135 GST_STATIC_CAPS ("application/x-rtcp")
138 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
139 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
142 GST_STATIC_CAPS ("application/x-rtp")
145 #define GST_RTP_BIN_GET_PRIVATE(obj) \
146 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRTPBinPrivate))
148 #define GST_RTP_BIN_LOCK(bin) g_mutex_lock ((bin)->priv->bin_lock)
149 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
151 struct _GstRTPBinPrivate
156 /* signals and args */
159 SIGNAL_REQUEST_PT_MAP,
163 SIGNAL_ON_SSRC_COLLISION,
164 SIGNAL_ON_SSRC_VALIDATED,
166 SIGNAL_ON_BYE_TIMEOUT,
171 #define DEFAULT_LATENCY_MS 200
180 typedef struct _GstRTPBinSession GstRTPBinSession;
181 typedef struct _GstRTPBinStream GstRTPBinStream;
182 typedef struct _GstRTPBinClient GstRTPBinClient;
184 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
186 static GstCaps *pt_map_requested (GstElement * element, guint pt,
187 GstRTPBinSession * session);
189 /* Manages the RTP stream for one SSRC.
191 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
192 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
193 * common CNAME, we create a GstRTPBinClient structure to group the SSRCs
194 * together (see below).
196 struct _GstRTPBinStream
198 /* the SSRC of this stream */
202 /* the session this SSRC belongs to */
203 GstRTPBinSession *session;
204 /* the jitterbuffer of the SSRC */
206 /* the PT demuxer of the SSRC */
208 gulong demux_newpad_sig;
209 gulong demux_ptreq_sig;
212 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->lock)
213 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
215 /* Manages the receiving end of the packets.
217 * There is one such structure for each RTP session (audio/video/...).
218 * We get the RTP/RTCP packets and stuff them into the session manager. From
219 * there they are pushed into an SSRC demuxer that splits the stream based on
220 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
221 * the GstRTPBinStream above).
223 struct _GstRTPBinSession
229 /* the session element */
231 /* the SSRC demuxer */
233 gulong demux_newpad_sig;
237 /* list of GstRTPBinStream */
240 /* mapping of payload type to caps */
243 /* the pads of the session */
244 GstPad *recv_rtp_sink;
245 GstPad *recv_rtp_src;
246 GstPad *recv_rtcp_sink;
247 GstPad *recv_rtcp_src;
248 GstPad *send_rtp_sink;
249 GstPad *send_rtp_src;
250 GstPad *send_rtcp_src;
253 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
254 static GstRTPBinSession *
255 find_session_by_id (GstRTPBin * rtpbin, gint id)
259 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
260 GstRTPBinSession *sess = (GstRTPBinSession *) walk->data;
269 on_new_ssrc (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
271 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
276 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
278 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
283 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
285 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
290 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
292 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
297 on_bye_timeout (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
299 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
304 on_timeout (GstElement * session, guint32 ssrc, GstRTPBinSession * sess)
306 g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
310 /* create a session with the given id. Must be called with RTP_BIN_LOCK */
311 static GstRTPBinSession *
312 create_session (GstRTPBin * rtpbin, gint id)
314 GstRTPBinSession *sess;
315 GstElement *session, *demux;
317 if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
320 if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
323 sess = g_new0 (GstRTPBinSession, 1);
324 sess->lock = g_mutex_new ();
327 sess->session = session;
329 sess->ptmap = g_hash_table_new (NULL, NULL);
330 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
332 /* provide clock_rate to the session manager when needed */
333 g_signal_connect (session, "request-pt-map",
334 (GCallback) pt_map_requested, sess);
336 g_signal_connect (sess->session, "on-new-ssrc",
337 (GCallback) on_new_ssrc, sess);
338 g_signal_connect (sess->session, "on-ssrc-collision",
339 (GCallback) on_ssrc_collision, sess);
340 g_signal_connect (sess->session, "on-ssrc-validated",
341 (GCallback) on_ssrc_validated, sess);
342 g_signal_connect (sess->session, "on-bye-ssrc",
343 (GCallback) on_bye_ssrc, sess);
344 g_signal_connect (sess->session, "on-bye-timeout",
345 (GCallback) on_bye_timeout, sess);
346 g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
348 gst_bin_add (GST_BIN_CAST (rtpbin), session);
349 gst_element_set_state (session, GST_STATE_PLAYING);
350 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
351 gst_element_set_state (demux, GST_STATE_PLAYING);
358 g_warning ("gstrtpbin: could not create gstrtpsession element");
363 gst_object_unref (session);
364 g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
370 static GstRTPBinStream *
371 find_stream_by_ssrc (GstRTPBinSession * session, guint32 ssrc)
375 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
376 GstRTPBinStream *stream = (GstRTPBinStream *) walk->data;
378 if (stream->ssrc == ssrc)
385 /* get the payload type caps for the specific payload @pt in @session */
387 get_pt_map (GstRTPBinSession * session, guint pt)
389 GstCaps *caps = NULL;
392 GValue args[3] = { {0}, {0}, {0} };
394 GST_DEBUG ("searching pt %d in cache", pt);
396 GST_RTP_SESSION_LOCK (session);
398 /* first look in the cache */
399 caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
405 GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
407 /* not in cache, send signal to request caps */
408 g_value_init (&args[0], GST_TYPE_ELEMENT);
409 g_value_set_object (&args[0], bin);
410 g_value_init (&args[1], G_TYPE_UINT);
411 g_value_set_uint (&args[1], session->id);
412 g_value_init (&args[2], G_TYPE_UINT);
413 g_value_set_uint (&args[2], pt);
415 g_value_init (&ret, GST_TYPE_CAPS);
416 g_value_set_boxed (&ret, NULL);
418 g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
420 caps = (GstCaps *) g_value_get_boxed (&ret);
424 GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
427 g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
430 GST_RTP_SESSION_UNLOCK (session);
437 GST_RTP_SESSION_UNLOCK (session);
438 GST_DEBUG ("no pt map could be obtained");
444 return_true (gpointer key, gpointer value, gpointer user_data)
450 gst_rtp_bin_clear_pt_map (GstRTPBin * bin)
454 GST_RTP_BIN_LOCK (bin);
455 for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
456 GstRTPBinSession *session = (GstRTPBinSession *) walk->data;
458 GST_RTP_SESSION_LOCK (session);
460 /* This requires GLib 2.12 */
461 g_hash_table_remove_all (session->ptmap);
463 g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
465 GST_RTP_SESSION_UNLOCK (session);
467 GST_RTP_BIN_UNLOCK (bin);
470 /* create a new stream with @ssrc in @session. Must be called with
471 * RTP_SESSION_LOCK. */
472 static GstRTPBinStream *
473 create_stream (GstRTPBinSession * session, guint32 ssrc)
475 GstElement *buffer, *demux;
476 GstRTPBinStream *stream;
478 if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
479 goto no_jitterbuffer;
481 if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
484 stream = g_new0 (GstRTPBinStream, 1);
486 stream->bin = session->bin;
487 stream->session = session;
488 stream->buffer = buffer;
489 stream->demux = demux;
490 session->streams = g_slist_prepend (session->streams, stream);
492 /* provide clock_rate to the jitterbuffer when needed */
493 g_signal_connect (buffer, "request-pt-map",
494 (GCallback) pt_map_requested, session);
496 /* configure latency */
497 g_object_set (buffer, "latency", session->bin->latency, NULL);
499 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
500 gst_element_set_state (buffer, GST_STATE_PLAYING);
501 gst_bin_add (GST_BIN_CAST (session->bin), demux);
502 gst_element_set_state (demux, GST_STATE_PLAYING);
505 gst_element_link (buffer, demux);
512 g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
517 gst_object_unref (buffer);
518 g_warning ("gstrtpbin: could not create gstrtpptdemux element");
523 /* Manages the RTP streams that come from one client and should therefore be
526 struct _GstRTPBinClient
528 /* the common CNAME for the streams */
534 /* GObject vmethods */
535 static void gst_rtp_bin_finalize (GObject * object);
536 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
537 const GValue * value, GParamSpec * pspec);
538 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
539 GValue * value, GParamSpec * pspec);
541 /* GstElement vmethods */
542 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
543 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
544 GstStateChange transition);
545 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
546 GstPadTemplate * templ, const gchar * name);
547 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
548 static void gst_rtp_bin_clear_pt_map (GstRTPBin * bin);
550 GST_BOILERPLATE (GstRTPBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
553 gst_rtp_bin_base_init (gpointer klass)
555 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
558 gst_element_class_add_pad_template (element_class,
559 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
560 gst_element_class_add_pad_template (element_class,
561 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
562 gst_element_class_add_pad_template (element_class,
563 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
566 gst_element_class_add_pad_template (element_class,
567 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
568 gst_element_class_add_pad_template (element_class,
569 gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
570 gst_element_class_add_pad_template (element_class,
571 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
573 gst_element_class_set_details (element_class, &rtpbin_details);
577 gst_rtp_bin_class_init (GstRTPBinClass * klass)
579 GObjectClass *gobject_class;
580 GstElementClass *gstelement_class;
582 gobject_class = (GObjectClass *) klass;
583 gstelement_class = (GstElementClass *) klass;
585 g_type_class_add_private (klass, sizeof (GstRTPBinPrivate));
587 gobject_class->finalize = gst_rtp_bin_finalize;
588 gobject_class->set_property = gst_rtp_bin_set_property;
589 gobject_class->get_property = gst_rtp_bin_get_property;
591 g_object_class_install_property (gobject_class, PROP_LATENCY,
592 g_param_spec_uint ("latency", "Buffer latency in ms",
593 "Default amount of ms to buffer in the jitterbuffers", 0,
594 G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
597 * GstRTPBin::request-pt-map:
598 * @rtpbin: the object which received the signal
599 * @session: the session
602 * Request the payload type as #GstCaps for @pt in @session.
604 gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
605 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
606 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, request_pt_map),
607 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
608 G_TYPE_UINT, G_TYPE_UINT);
610 * GstRTPBin::clear-pt-map:
611 * @rtpbin: the object which received the signal
613 * Clear all previously cached pt-mapping obtained with
614 * GstRTPBin::request-pt-map.
616 gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
617 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
618 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTPBinClass, clear_pt_map),
619 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
622 * GstRTPBin::on-new-ssrc:
623 * @rtpbin: the object which received the signal
624 * @session: the session
627 * Notify of a new SSRC that entered @session.
629 gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
630 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
631 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_new_ssrc),
632 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
633 G_TYPE_UINT, G_TYPE_UINT);
635 * GstRTPBin::on-ssrc_collision:
636 * @rtpbin: the object which received the signal
637 * @session: the session
640 * Notify when we have an SSRC collision
642 gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
643 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
644 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_ssrc_collision),
645 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
646 G_TYPE_UINT, G_TYPE_UINT);
648 * GstRTPBin::on-ssrc_validated:
649 * @rtpbin: the object which received the signal
650 * @session: the session
653 * Notify of a new SSRC that became validated.
655 gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
656 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
657 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_ssrc_validated),
658 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
659 G_TYPE_UINT, G_TYPE_UINT);
662 * GstRTPBin::on-bye-ssrc:
663 * @rtpbin: the object which received the signal
664 * @session: the session
667 * Notify of an SSRC that became inactive because of a BYE packet.
669 gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
670 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
671 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_bye_ssrc),
672 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
673 G_TYPE_UINT, G_TYPE_UINT);
675 * GstRTPBin::on-bye-timeout:
676 * @rtpbin: the object which received the signal
677 * @session: the session
680 * Notify of an SSRC that has timed out because of BYE
682 gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
683 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
684 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_bye_timeout),
685 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
686 G_TYPE_UINT, G_TYPE_UINT);
688 * GstRTPBin::on-timeout:
689 * @rtpbin: the object which received the signal
690 * @session: the session
693 * Notify of an SSRC that has timed out
695 gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
696 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
697 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPBinClass, on_timeout),
698 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
699 G_TYPE_UINT, G_TYPE_UINT);
701 gstelement_class->provide_clock =
702 GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
703 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
704 gstelement_class->request_new_pad =
705 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
706 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
708 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
710 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
714 gst_rtp_bin_init (GstRTPBin * rtpbin, GstRTPBinClass * klass)
716 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
717 rtpbin->priv->bin_lock = g_mutex_new ();
718 rtpbin->provided_clock = gst_system_clock_obtain ();
722 gst_rtp_bin_finalize (GObject * object)
726 rtpbin = GST_RTP_BIN (object);
728 g_mutex_free (rtpbin->priv->bin_lock);
730 G_OBJECT_CLASS (parent_class)->finalize (object);
734 gst_rtp_bin_set_property (GObject * object, guint prop_id,
735 const GValue * value, GParamSpec * pspec)
739 rtpbin = GST_RTP_BIN (object);
743 rtpbin->latency = g_value_get_uint (value);
746 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
752 gst_rtp_bin_get_property (GObject * object, guint prop_id,
753 GValue * value, GParamSpec * pspec)
757 rtpbin = GST_RTP_BIN (object);
761 g_value_set_uint (value, rtpbin->latency);
764 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
770 gst_rtp_bin_provide_clock (GstElement * element)
774 rtpbin = GST_RTP_BIN (element);
776 return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
779 static GstStateChangeReturn
780 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
782 GstStateChangeReturn res;
785 rtpbin = GST_RTP_BIN (element);
787 switch (transition) {
788 case GST_STATE_CHANGE_NULL_TO_READY:
790 case GST_STATE_CHANGE_READY_TO_PAUSED:
792 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
798 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
800 switch (transition) {
801 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
803 case GST_STATE_CHANGE_PAUSED_TO_READY:
805 case GST_STATE_CHANGE_READY_TO_NULL:
813 /* a new pad (SSRC) was created in @session */
815 new_payload_found (GstElement * element, guint pt, GstPad * pad,
816 GstRTPBinStream * stream)
819 GstElementClass *klass;
820 GstPadTemplate *templ;
824 rtpbin = stream->bin;
826 GST_DEBUG ("new payload pad %d", pt);
828 /* ghost the pad to the parent */
829 klass = GST_ELEMENT_GET_CLASS (rtpbin);
830 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
831 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
832 stream->session->id, stream->ssrc, pt);
833 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
836 gst_pad_set_active (gpad, TRUE);
837 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
841 pt_map_requested (GstElement * element, guint pt, GstRTPBinSession * session)
846 rtpbin = session->bin;
848 GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
851 caps = get_pt_map (session, pt);
860 GST_DEBUG_OBJECT (rtpbin, "could not get caps");
865 /* a new pad (SSRC) was created in @session */
867 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
868 GstRTPBinSession * session)
870 GstRTPBinStream *stream;
873 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
875 GST_RTP_SESSION_LOCK (session);
877 /* create new stream */
878 stream = create_stream (session, ssrc);
882 /* get pad and link */
883 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
884 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
885 gst_pad_link (pad, sinkpad);
886 gst_object_unref (sinkpad);
888 /* connect to the new-pad signal of the payload demuxer, this will expose the
889 * new pad by ghosting it. */
890 stream->demux_newpad_sig = g_signal_connect (stream->demux,
891 "new-payload-type", (GCallback) new_payload_found, stream);
892 /* connect to the request-pt-map signal. This signal will be emited by the
893 * demuxer so that it can apply a proper caps on the buffers for the
895 stream->demux_ptreq_sig = g_signal_connect (stream->demux,
896 "request-pt-map", (GCallback) pt_map_requested, session);
898 GST_RTP_SESSION_UNLOCK (session);
905 GST_RTP_SESSION_UNLOCK (session);
906 GST_DEBUG ("could not create stream");
911 /* Create a pad for receiving RTP for the session in @name. Must be called with
915 create_recv_rtp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
917 GstPad *result, *sinkdpad;
919 GstRTPBinSession *session;
920 GstPadLinkReturn lres;
922 /* first get the session number */
923 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
926 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
928 /* get or create session */
929 session = find_session_by_id (rtpbin, sessid);
931 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
932 /* create session now */
933 session = create_session (rtpbin, sessid);
938 /* check if pad was requested */
939 if (session->recv_rtp_sink != NULL)
942 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
943 /* get recv_rtp pad and store */
944 session->recv_rtp_sink =
945 gst_element_get_request_pad (session->session, "recv_rtp_sink");
946 if (session->recv_rtp_sink == NULL)
949 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
950 /* get srcpad, link to SSRCDemux */
951 session->recv_rtp_src =
952 gst_element_get_static_pad (session->session, "recv_rtp_src");
953 if (session->recv_rtp_src == NULL)
956 GST_DEBUG_OBJECT (rtpbin, "getting demuxer sink pad");
957 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
958 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
959 gst_object_unref (sinkdpad);
960 if (lres != GST_PAD_LINK_OK)
963 /* connect to the new-ssrc-pad signal of the SSRC demuxer */
964 session->demux_newpad_sig = g_signal_connect (session->demux,
965 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
967 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
969 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
970 gst_pad_set_active (result, TRUE);
971 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
978 g_warning ("gstrtpbin: invalid name given");
983 /* create_session already warned */
988 g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
994 g_warning ("gstrtpbin: failed to get session pad");
999 g_warning ("gstrtpbin: failed to link pads");
1004 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1008 create_recv_rtcp (GstRTPBin * rtpbin, GstPadTemplate * templ,
1013 GstRTPBinSession *session;
1017 GstPadLinkReturn lres;
1020 /* first get the session number */
1021 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1024 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1026 /* get or create the session */
1027 session = find_session_by_id (rtpbin, sessid);
1029 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1030 /* create session now */
1031 session = create_session (rtpbin, sessid);
1032 if (session == NULL)
1036 /* check if pad was requested */
1037 if (session->recv_rtcp_sink != NULL)
1040 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1042 /* get recv_rtp pad and store */
1043 session->recv_rtcp_sink =
1044 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1045 if (session->recv_rtcp_sink == NULL)
1049 /* get srcpad, link to SSRCDemux */
1050 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1051 session->recv_rtcp_src =
1052 gst_element_get_static_pad (session->session, "sync_src");
1053 if (session->recv_rtcp_src == NULL)
1056 GST_DEBUG_OBJECT (rtpbin, "linking sync to demux");
1057 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1058 lres = gst_pad_link (session->recv_rtcp_src, sinkdpad);
1059 gst_object_unref (sinkdpad);
1060 if (lres != GST_PAD_LINK_OK)
1065 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1066 gst_pad_set_active (result, TRUE);
1067 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1074 g_warning ("gstrtpbin: invalid name given");
1079 /* create_session already warned */
1084 g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1090 g_warning ("gstrtpbin: failed to get session pad");
1096 g_warning ("gstrtpbin: failed to link pads");
1102 /* Create a pad for sending RTP for the session in @name. Must be called with
1106 create_send_rtp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1108 GstPad *result, *srcghost;
1111 GstRTPBinSession *session;
1112 GstElementClass *klass;
1114 /* first get the session number */
1115 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1118 /* get or create session */
1119 session = find_session_by_id (rtpbin, sessid);
1121 /* create session now */
1122 session = create_session (rtpbin, sessid);
1123 if (session == NULL)
1127 /* check if pad was requested */
1128 if (session->send_rtp_sink != NULL)
1131 /* get send_rtp pad and store */
1132 session->send_rtp_sink =
1133 gst_element_get_request_pad (session->session, "send_rtp_sink");
1134 if (session->send_rtp_sink == NULL)
1138 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1139 gst_pad_set_active (result, TRUE);
1140 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1143 session->send_rtp_src =
1144 gst_element_get_static_pad (session->session, "send_rtp_src");
1145 if (session->send_rtp_src == NULL)
1148 /* ghost the new source pad */
1149 klass = GST_ELEMENT_GET_CLASS (rtpbin);
1150 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1151 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1153 gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1154 gst_pad_set_active (srcghost, TRUE);
1155 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1163 g_warning ("gstrtpbin: invalid name given");
1168 /* create_session already warned */
1173 g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1179 g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1184 g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1190 /* Create a pad for sending RTCP for the session in @name. Must be called with
1194 create_rtcp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1198 GstRTPBinSession *session;
1200 /* first get the session number */
1201 if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1204 /* get or create session */
1205 session = find_session_by_id (rtpbin, sessid);
1209 /* check if pad was requested */
1210 if (session->send_rtcp_src != NULL)
1213 /* get rtcp_src pad and store */
1214 session->send_rtcp_src =
1215 gst_element_get_request_pad (session->session, "send_rtcp_src");
1216 if (session->send_rtcp_src == NULL)
1220 gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1221 gst_pad_set_active (result, TRUE);
1222 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1229 g_warning ("gstrtpbin: invalid name given");
1234 g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1239 g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1245 g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1253 gst_rtp_bin_request_new_pad (GstElement * element,
1254 GstPadTemplate * templ, const gchar * name)
1257 GstElementClass *klass;
1260 g_return_val_if_fail (templ != NULL, NULL);
1261 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1263 rtpbin = GST_RTP_BIN (element);
1264 klass = GST_ELEMENT_GET_CLASS (element);
1266 GST_RTP_BIN_LOCK (rtpbin);
1268 /* figure out the template */
1269 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1270 result = create_recv_rtp (rtpbin, templ, name);
1271 } else if (templ == gst_element_class_get_pad_template (klass,
1272 "recv_rtcp_sink_%d")) {
1273 result = create_recv_rtcp (rtpbin, templ, name);
1274 } else if (templ == gst_element_class_get_pad_template (klass,
1275 "send_rtp_sink_%d")) {
1276 result = create_send_rtp (rtpbin, templ, name);
1277 } else if (templ == gst_element_class_get_pad_template (klass,
1278 "send_rtcp_src_%d")) {
1279 result = create_rtcp (rtpbin, templ, name);
1281 goto wrong_template;
1283 GST_RTP_BIN_UNLOCK (rtpbin);
1290 GST_RTP_BIN_UNLOCK (rtpbin);
1291 g_warning ("gstrtpbin: this is not our template");
1297 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)