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-rtpbin
22 * @short_description: handle media from one RTP bin
23 * @see_also: rtpjitterbuffer, rtpclient, rtpsession
28 * <title>Example pipelines</title>
31 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
36 * Last reviewed on 2007-04-02 (0.10.6)
44 #include "gstrtpbin.h"
46 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
47 #define GST_CAT_DEFAULT gst_rtp_bin_debug
50 /* elementfactory information */
51 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
52 "Filter/Editor/Video",
53 "Implement an RTP bin",
54 "Wim Taymans <wim@fluendo.com>");
57 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
61 GST_STATIC_CAPS ("application/x-rtp")
64 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
68 GST_STATIC_CAPS ("application/x-rtcp")
71 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
72 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
75 GST_STATIC_CAPS ("application/x-rtp")
79 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
80 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
83 GST_STATIC_CAPS ("application/x-rtp")
86 static GstStaticPadTemplate rtpbin_rtcp_src_template =
87 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%d",
90 GST_STATIC_CAPS ("application/x-rtcp")
93 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
94 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
97 GST_STATIC_CAPS ("application/x-rtp")
100 #define GST_RTP_BIN_GET_PRIVATE(obj) \
101 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRTPBinPrivate))
103 struct _GstRTPBinPrivate
108 /* signals and args */
121 typedef struct _GstRTPBinSession GstRTPBinSession;
122 typedef struct _GstRTPBinStream GstRTPBinStream;
123 typedef struct _GstRTPBinClient GstRTPBinClient;
125 /* Manages the RTP stream for one SSRC.
127 * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
128 * If we see an SDES RTCP packet that links multiple SSRCs together based on a
129 * common CNAME, we create a GstRTPBinClient structure to group the SSRCs
130 * together (see below).
132 struct _GstRTPBinStream
134 /* the SSRC of this stream */
138 /* the session this SSRC belongs to */
139 GstRTPBinSession *session;
140 /* the jitterbuffer of the SSRC */
142 /* the PT demuxer of the SSRC */
144 gulong demux_newpad_sig;
147 /* Manages the receiving end of the packets.
149 * There is one such structure for each RTP session (audio/video/...).
150 * We get the RTP/RTCP packets and stuff them into the session manager. From
151 * there they are pushed into an SSRC demuxer that splits the stream based on
152 * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
153 * the GstRTPBinStream above).
155 struct _GstRTPBinSession
161 /* the session element */
163 /* the SSRC demuxer */
165 gulong demux_newpad_sig;
167 /* list of GstRTPBinStream */
170 /* the pads of the session */
171 GstPad *recv_rtp_sink;
172 GstPad *recv_rtp_src;
173 GstPad *recv_rtcp_sink;
174 GstPad *recv_rtcp_src;
175 GstPad *send_rtp_sink;
176 GstPad *send_rtp_src;
180 /* find a session with the given id */
181 static GstRTPBinSession *
182 find_session_by_id (GstRTPBin * rtpbin, gint id)
186 for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
187 GstRTPBinSession *sess = (GstRTPBinSession *) walk->data;
195 /* create a session with the given id */
196 static GstRTPBinSession *
197 create_session (GstRTPBin * rtpbin, gint id)
199 GstRTPBinSession *sess;
200 GstElement *elem, *demux;
202 if (!(elem = gst_element_factory_make ("rtpsession", NULL)))
205 if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
208 sess = g_new0 (GstRTPBinSession, 1);
211 sess->session = elem;
213 rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
215 gst_bin_add (GST_BIN_CAST (rtpbin), elem);
216 gst_element_set_state (elem, GST_STATE_PLAYING);
217 gst_bin_add (GST_BIN_CAST (rtpbin), demux);
218 gst_element_set_state (demux, GST_STATE_PLAYING);
225 g_warning ("rtpbin: could not create rtpsession element");
230 gst_object_unref (elem);
231 g_warning ("rtpbin: could not create rtpssrcdemux element");
237 static GstRTPBinStream *
238 find_stream_by_ssrc (GstRTPBinSession * session, guint32 ssrc)
242 for (walk = session->streams; walk; walk = g_slist_next (walk)) {
243 GstRTPBinStream *stream = (GstRTPBinStream *) walk->data;
245 if (stream->ssrc == ssrc)
252 static GstRTPBinStream *
253 create_stream (GstRTPBinSession * session, guint32 ssrc)
255 GstElement *buffer, *demux;
256 GstRTPBinStream *stream;
258 if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
259 goto no_jitterbuffer;
261 if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
264 stream = g_new0 (GstRTPBinStream, 1);
266 stream->bin = session->bin;
267 stream->session = session;
268 stream->buffer = buffer;
269 stream->demux = demux;
270 session->streams = g_slist_prepend (session->streams, stream);
272 gst_bin_add (GST_BIN_CAST (session->bin), buffer);
273 gst_element_set_state (buffer, GST_STATE_PLAYING);
274 gst_bin_add (GST_BIN_CAST (session->bin), demux);
275 gst_element_set_state (demux, GST_STATE_PLAYING);
278 gst_element_link (buffer, demux);
285 g_warning ("rtpbin: could not create rtpjitterbuffer element");
290 gst_object_unref (buffer);
291 g_warning ("rtpbin: could not create rtpptdemux element");
296 /* Manages the RTP streams that come from one client and should therefore be
299 struct _GstRTPBinClient
301 /* the common CNAME for the streams */
307 /* GObject vmethods */
308 static void gst_rtp_bin_finalize (GObject * object);
309 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
310 const GValue * value, GParamSpec * pspec);
311 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
312 GValue * value, GParamSpec * pspec);
314 /* GstElement vmethods */
315 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
316 GstStateChange transition);
317 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
318 GstPadTemplate * templ, const gchar * name);
319 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
321 /*static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 }; */
323 GST_BOILERPLATE (GstRTPBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
326 gst_rtp_bin_base_init (gpointer klass)
328 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
331 gst_element_class_add_pad_template (element_class,
332 gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
333 gst_element_class_add_pad_template (element_class,
334 gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
335 gst_element_class_add_pad_template (element_class,
336 gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
339 gst_element_class_add_pad_template (element_class,
340 gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
341 gst_element_class_add_pad_template (element_class,
342 gst_static_pad_template_get (&rtpbin_rtcp_src_template));
343 gst_element_class_add_pad_template (element_class,
344 gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
346 gst_element_class_set_details (element_class, &rtpbin_details);
350 gst_rtp_bin_class_init (GstRTPBinClass * klass)
352 GObjectClass *gobject_class;
353 GstElementClass *gstelement_class;
355 gobject_class = (GObjectClass *) klass;
356 gstelement_class = (GstElementClass *) klass;
358 g_type_class_add_private (klass, sizeof (GstRTPBinPrivate));
360 gobject_class->finalize = gst_rtp_bin_finalize;
361 gobject_class->set_property = gst_rtp_bin_set_property;
362 gobject_class->get_property = gst_rtp_bin_get_property;
364 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
365 gstelement_class->request_new_pad =
366 GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
367 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
369 GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
373 gst_rtp_bin_init (GstRTPBin * rtpbin, GstRTPBinClass * klass)
375 rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
379 gst_rtp_bin_finalize (GObject * object)
383 rtpbin = GST_RTP_BIN (object);
385 G_OBJECT_CLASS (parent_class)->finalize (object);
389 gst_rtp_bin_set_property (GObject * object, guint prop_id,
390 const GValue * value, GParamSpec * pspec)
394 rtpbin = GST_RTP_BIN (object);
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404 gst_rtp_bin_get_property (GObject * object, guint prop_id,
405 GValue * value, GParamSpec * pspec)
409 rtpbin = GST_RTP_BIN (object);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 static GstStateChangeReturn
419 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
421 GstStateChangeReturn res;
424 rtpbin = GST_RTP_BIN (element);
426 switch (transition) {
427 case GST_STATE_CHANGE_NULL_TO_READY:
429 case GST_STATE_CHANGE_READY_TO_PAUSED:
431 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
437 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
439 switch (transition) {
440 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
442 case GST_STATE_CHANGE_PAUSED_TO_READY:
444 case GST_STATE_CHANGE_READY_TO_NULL:
452 /* a new pad (SSRC) was created in @session */
454 new_payload_found (GstElement * element, guint pt, GstPad * pad,
455 GstRTPBinStream * stream)
458 GstElementClass *klass;
459 GstPadTemplate *templ;
463 rtpbin = stream->bin;
465 GST_DEBUG ("new payload pad %d", pt);
467 /* ghost the pad to the parent */
468 klass = GST_ELEMENT_GET_CLASS (rtpbin);
469 templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
470 padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
471 stream->session->id, stream->ssrc, pt);
472 gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
475 gst_pad_set_active (gpad, TRUE);
476 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
479 /* a new pad (SSRC) was created in @session */
481 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
482 GstRTPBinSession * session)
484 GstRTPBinStream *stream;
487 GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
489 /* create new stream */
490 stream = create_stream (session, ssrc);
494 /* get pad and link */
495 GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
496 sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
497 gst_pad_link (pad, sinkpad);
498 gst_object_unref (sinkpad);
500 /* connect to the new-pad signal of the payload demuxer */
501 stream->demux_newpad_sig = g_signal_connect (stream->demux,
502 "new-payload-type", (GCallback) new_payload_found, stream);
509 GST_DEBUG ("could not create stream");
514 /* Create a pad for receiving RTP for the session in @name
517 create_recv_rtp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
519 GstPad *result, *sinkdpad;
521 GstRTPBinSession *session;
522 GstPadLinkReturn lres;
524 /* first get the session number */
525 if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
528 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
530 /* get or create session */
531 session = find_session_by_id (rtpbin, sessid);
533 GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
534 /* create session now */
535 session = create_session (rtpbin, sessid);
539 /* check if pad was requested */
540 if (session->recv_rtp_sink != NULL)
543 GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
544 /* get recv_rtp pad and store */
545 session->recv_rtp_sink =
546 gst_element_get_request_pad (session->session, "recv_rtp_sink");
547 if (session->recv_rtp_sink == NULL)
550 GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
551 /* get srcpad, link to SSRCDemux */
552 session->recv_rtp_src =
553 gst_element_get_static_pad (session->session, "recv_rtp_src");
554 if (session->recv_rtp_src == NULL)
557 GST_DEBUG_OBJECT (rtpbin, "getting demuxer sink pad");
558 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
559 lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
560 gst_object_unref (sinkdpad);
561 if (lres != GST_PAD_LINK_OK)
564 /* connect to the new-ssrc-pad signal of the demuxer */
565 session->demux_newpad_sig = g_signal_connect (session->demux,
566 "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
568 GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
570 gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
571 gst_pad_set_active (result, TRUE);
572 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
579 g_warning ("rtpbin: invalid name given");
584 /* create_session already warned */
589 g_warning ("rtpbin: recv_rtp pad already requested for session %d", sessid);
594 g_warning ("rtpbin: failed to get session pad");
599 g_warning ("rtpbin: failed to link pads");
604 /* Create a pad for receiving RTCP for the session in @name
607 create_recv_rtcp (GstRTPBin * rtpbin, GstPadTemplate * templ,
612 GstRTPBinSession *session;
616 GstPadLinkReturn lres;
619 /* first get the session number */
620 if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
623 GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
625 /* get the session, it must exist or we error */
626 session = find_session_by_id (rtpbin, sessid);
630 /* check if pad was requested */
631 if (session->recv_rtcp_sink != NULL)
634 GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
636 /* get recv_rtp pad and store */
637 session->recv_rtcp_sink =
638 gst_element_get_request_pad (session->session, "recv_rtcp_sink");
639 if (session->recv_rtcp_sink == NULL)
643 /* get srcpad, link to SSRCDemux */
644 GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
645 session->recv_rtcp_src =
646 gst_element_get_static_pad (session->session, "sync_src");
647 if (session->recv_rtcp_src == NULL)
650 GST_DEBUG_OBJECT (rtpbin, "linking sync to demux");
651 sinkdpad = gst_element_get_static_pad (session->demux, "sink");
652 lres = gst_pad_link (session->recv_rtcp_src, sinkdpad);
653 gst_object_unref (sinkdpad);
654 if (lres != GST_PAD_LINK_OK)
659 gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
660 gst_pad_set_active (result, TRUE);
661 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
668 g_warning ("rtpbin: invalid name given");
673 g_warning ("rtpbin: no session with id %d", sessid);
678 g_warning ("rtpbin: recv_rtcp pad already requested for session %d",
684 g_warning ("rtpbin: failed to get session pad");
690 g_warning ("rtpbin: failed to link pads");
696 /* Create a pad for sending RTP for the session in @name
699 create_send_rtp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
701 GstPad *result, *srcpad, *srcghost;
704 GstRTPBinSession *session;
705 GstElementClass *klass;
707 /* first get the session number */
708 if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
711 /* get or create session */
712 session = find_session_by_id (rtpbin, sessid);
714 /* create session now */
715 session = create_session (rtpbin, sessid);
720 /* check if pad was requested */
721 if (session->send_rtp_sink != NULL)
724 /* get recv_rtp pad and store */
725 session->send_rtp_sink =
726 gst_element_get_request_pad (session->session, "send_rtp_sink");
727 if (session->send_rtp_sink == NULL)
731 gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
732 gst_pad_set_active (result, TRUE);
733 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
736 srcpad = gst_element_get_pad (session->session, "send_rtp_src");
740 /* ghost the new source pad */
741 klass = GST_ELEMENT_GET_CLASS (rtpbin);
742 gname = g_strdup_printf ("send_rtp_src_%d", sessid);
743 templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
745 gst_ghost_pad_new_from_template (gname, session->send_rtp_sink, templ);
746 gst_pad_set_active (srcghost, TRUE);
747 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
755 g_warning ("rtpbin: invalid name given");
760 /* create_session already warned */
765 g_warning ("rtpbin: send_rtp pad already requested for session %d", sessid);
770 g_warning ("rtpbin: failed to get session pad for session %d", sessid);
775 g_warning ("rtpbin: failed to get rtp source pad for session %d", sessid);
780 /* Create a pad for sending RTCP for the session in @name
783 create_rtcp (GstRTPBin * rtpbin, GstPadTemplate * templ, const gchar * name)
787 GstRTPBinSession *session;
789 /* first get the session number */
790 if (name == NULL || sscanf (name, "rtcp_src_%d", &sessid) != 1)
793 /* get or create session */
794 session = find_session_by_id (rtpbin, sessid);
798 /* check if pad was requested */
799 if (session->rtcp_src != NULL)
802 /* get rtcp_src pad and store */
804 gst_element_get_request_pad (session->session, "rtcp_src");
805 if (session->rtcp_src == NULL)
808 result = gst_ghost_pad_new_from_template (name, session->rtcp_src, templ);
809 gst_pad_set_active (result, TRUE);
810 gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
817 g_warning ("rtpbin: invalid name given");
822 g_warning ("rtpbin: session with id %d does not exist", sessid);
827 g_warning ("rtpbin: rtcp_src pad already requested for session %d", sessid);
832 g_warning ("rtpbin: failed to get rtcp pad for session %d", sessid);
840 gst_rtp_bin_request_new_pad (GstElement * element,
841 GstPadTemplate * templ, const gchar * name)
844 GstElementClass *klass;
847 g_return_val_if_fail (templ != NULL, NULL);
848 g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
850 rtpbin = GST_RTP_BIN (element);
851 klass = GST_ELEMENT_GET_CLASS (element);
853 /* figure out the template */
854 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
855 result = create_recv_rtp (rtpbin, templ, name);
856 } else if (templ == gst_element_class_get_pad_template (klass,
857 "recv_rtcp_sink_%d")) {
858 result = create_recv_rtcp (rtpbin, templ, name);
859 } else if (templ == gst_element_class_get_pad_template (klass,
860 "send_rtp_sink_%d")) {
861 result = create_send_rtp (rtpbin, templ, name);
862 } else if (templ == gst_element_class_get_pad_template (klass, "rtcp_src_%d")) {
863 result = create_rtcp (rtpbin, templ, name);
872 g_warning ("rtpbin: this is not our template");
878 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)