2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-gstrtpssrcdemux
24 * @short_description: separate RTP payloads based on the SSRC
28 * gstrtpssrcdemux acts as a demuxer for RTP packets based on the SSRC of the
29 * packets. Its main purpose is to allow an application to easily receive and
30 * decode an RTP stream with multiple SSRCs.
33 * For each SSRC that is detected, a new pad will be created and the
34 * ::new-ssrc-pad signal will be emitted.
36 * <title>Example pipelines</title>
39 * gst-launch udpsrc caps="application/x-rtp" ! gstrtpssrcdemux ! fakesink
41 * Takes an RTP stream and send the RTP packets with the first detected SSRC
42 * to fakesink, discarding the other SSRCs.
46 * Last reviewed on 2007-05-28 (0.10.5)
54 #include <gst/rtp/gstrtpbuffer.h>
55 #include <gst/rtp/gstrtcpbuffer.h>
57 #include "gstrtpbin-marshal.h"
58 #include "gstrtpssrcdemux.h"
60 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
61 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
63 /* generic templates */
64 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("application/x-rtp")
71 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
72 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
75 GST_STATIC_CAPS ("application/x-rtcp")
78 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
79 GST_STATIC_PAD_TEMPLATE ("src_%d",
82 GST_STATIC_CAPS ("application/x-rtp")
85 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
86 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%d",
89 GST_STATIC_CAPS ("application/x-rtcp")
92 static GstElementDetails gst_rtp_ssrc_demux_details = {
95 "Splits RTP streams based on the SSRC",
96 "Wim Taymans <wim@fluendo.com>"
99 #define GST_PAD_LOCK(obj) (g_mutex_lock ((obj)->padlock))
100 #define GST_PAD_UNLOCK(obj) (g_mutex_unlock ((obj)->padlock))
109 GST_BOILERPLATE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GstElement,
113 /* GObject vmethods */
114 static void gst_rtp_ssrc_demux_dispose (GObject * object);
115 static void gst_rtp_ssrc_demux_finalize (GObject * object);
117 /* GstElement vmethods */
118 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
119 element, GstStateChange transition);
122 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf);
123 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event);
125 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
127 static gboolean gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad,
131 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event);
132 static GList *gst_rtp_ssrc_demux_internal_links (GstPad * pad);
133 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query);
135 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
138 * Item for storing GstPad <-> SSRC pairs.
140 struct _GstRtpSsrcDemuxPad
146 GstClockTime first_ts;
149 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
151 static GstRtpSsrcDemuxPad *
152 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
156 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
157 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
159 if (pad->ssrc == ssrc)
166 static GstRtpSsrcDemuxPad *
167 create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc,
168 GstClockTime timestamp)
170 GstPad *rtp_pad, *rtcp_pad;
171 GstElementClass *klass;
172 GstPadTemplate *templ;
174 GstRtpSsrcDemuxPad *demuxpad;
176 GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
178 klass = GST_ELEMENT_GET_CLASS (demux);
179 templ = gst_element_class_get_pad_template (klass, "src_%d");
180 padname = g_strdup_printf ("src_%d", ssrc);
181 rtp_pad = gst_pad_new_from_template (templ, padname);
184 templ = gst_element_class_get_pad_template (klass, "rtcp_src_%d");
185 padname = g_strdup_printf ("rtcp_src_%d", ssrc);
186 rtcp_pad = gst_pad_new_from_template (templ, padname);
189 /* we use the first timestamp received to calculate the difference between
190 * timestamps on all streams */
191 GST_DEBUG_OBJECT (demux, "SSRC %08x, first timestamp %" GST_TIME_FORMAT,
192 ssrc, GST_TIME_ARGS (timestamp));
194 /* wrap in structure and add to list */
195 demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
196 demuxpad->ssrc = ssrc;
197 demuxpad->rtp_pad = rtp_pad;
198 demuxpad->rtcp_pad = rtcp_pad;
199 demuxpad->first_ts = timestamp;
201 GST_DEBUG_OBJECT (demux, "first timestamp %" GST_TIME_FORMAT,
202 GST_TIME_ARGS (timestamp));
204 gst_pad_set_element_private (rtp_pad, demuxpad);
205 gst_pad_set_element_private (rtcp_pad, demuxpad);
207 demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
209 /* copy caps from input */
210 gst_pad_set_caps (rtp_pad, GST_PAD_CAPS (demux->rtp_sink));
211 gst_pad_use_fixed_caps (rtp_pad);
212 gst_pad_set_caps (rtcp_pad, GST_PAD_CAPS (demux->rtcp_sink));
213 gst_pad_use_fixed_caps (rtcp_pad);
215 gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
216 gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
217 gst_pad_set_internal_link_function (rtp_pad,
218 gst_rtp_ssrc_demux_internal_links);
219 gst_pad_set_active (rtp_pad, TRUE);
221 gst_pad_set_internal_link_function (rtcp_pad,
222 gst_rtp_ssrc_demux_internal_links);
223 gst_pad_set_active (rtcp_pad, TRUE);
225 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
226 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
228 g_signal_emit (G_OBJECT (demux),
229 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
235 gst_rtp_ssrc_demux_base_init (gpointer g_class)
237 GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
239 gst_element_class_add_pad_template (gstelement_klass,
240 gst_static_pad_template_get (&rtp_ssrc_demux_sink_template));
241 gst_element_class_add_pad_template (gstelement_klass,
242 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_sink_template));
243 gst_element_class_add_pad_template (gstelement_klass,
244 gst_static_pad_template_get (&rtp_ssrc_demux_src_template));
245 gst_element_class_add_pad_template (gstelement_klass,
246 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_src_template));
248 gst_element_class_set_details (gstelement_klass, &gst_rtp_ssrc_demux_details);
252 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
254 GObjectClass *gobject_klass;
255 GstElementClass *gstelement_klass;
257 gobject_klass = (GObjectClass *) klass;
258 gstelement_klass = (GstElementClass *) klass;
260 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_dispose);
261 gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_finalize);
264 * GstRtpSsrcDemux::new-ssrc-pad:
265 * @demux: the object which received the signal
266 * @ssrc: the SSRC of the pad
269 * Emited when a new SSRC pad has been created.
271 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
272 g_signal_new ("new-ssrc-pad",
273 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
274 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
275 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
276 G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
278 gstelement_klass->change_state =
279 GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
281 GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
282 "rtpssrcdemux", 0, "RTP SSRC demuxer");
286 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux,
287 GstRtpSsrcDemuxClass * g_class)
289 GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
292 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
294 gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
295 gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
296 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
299 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
300 "rtcp_sink"), "rtcp_sink");
301 gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
302 gst_pad_set_event_function (demux->rtcp_sink,
303 gst_rtp_ssrc_demux_rtcp_sink_event);
304 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
306 demux->padlock = g_mutex_new ();
308 gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
312 gst_rtp_ssrc_demux_dispose (GObject * object)
314 GstRtpSsrcDemux *demux;
316 demux = GST_RTP_SSRC_DEMUX (object);
318 g_slist_foreach (demux->srcpads, (GFunc) g_free, NULL);
319 g_slist_free (demux->srcpads);
320 demux->srcpads = NULL;
322 G_OBJECT_CLASS (parent_class)->dispose (object);
326 gst_rtp_ssrc_demux_finalize (GObject * object)
328 GstRtpSsrcDemux *demux;
330 demux = GST_RTP_SSRC_DEMUX (object);
331 g_mutex_free (demux->padlock);
333 G_OBJECT_CLASS (parent_class)->finalize (object);
337 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event)
339 GstRtpSsrcDemux *demux;
340 gboolean res = FALSE;
342 demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
344 switch (GST_EVENT_TYPE (event)) {
345 case GST_EVENT_FLUSH_STOP:
346 gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
347 case GST_EVENT_NEWSEGMENT:
353 GST_PAD_LOCK (demux);
354 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
355 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
357 gst_event_ref (event);
358 res &= gst_pad_push_event (pad->rtp_pad, event);
360 GST_PAD_UNLOCK (demux);
361 gst_event_unref (event);
366 gst_object_unref (demux);
371 gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad, GstEvent * event)
373 GstRtpSsrcDemux *demux;
374 gboolean res = FALSE;
376 demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
378 switch (GST_EVENT_TYPE (event)) {
379 case GST_EVENT_NEWSEGMENT:
385 GST_PAD_LOCK (demux);
386 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
387 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
389 res &= gst_pad_push_event (pad->rtcp_pad, event);
391 GST_PAD_UNLOCK (demux);
395 gst_object_unref (demux);
400 gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf)
403 GstRtpSsrcDemux *demux;
405 GstRtpSsrcDemuxPad *dpad;
407 demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
409 if (!gst_rtp_buffer_validate (buf))
410 goto invalid_payload;
412 ssrc = gst_rtp_buffer_get_ssrc (buf);
414 GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
416 GST_PAD_LOCK (demux);
417 dpad = find_demux_pad_for_ssrc (demux, ssrc);
420 create_demux_pad_for_ssrc (demux, ssrc,
421 GST_BUFFER_TIMESTAMP (buf))))
424 GST_PAD_UNLOCK (demux);
427 ret = gst_pad_push (dpad->rtp_pad, buf);
434 /* this is fatal and should be filtered earlier */
435 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
436 ("Dropping invalid RTP payload"));
437 gst_buffer_unref (buf);
438 return GST_FLOW_ERROR;
442 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
443 ("Could not create new pad"));
444 GST_PAD_UNLOCK (demux);
445 gst_buffer_unref (buf);
446 return GST_FLOW_ERROR;
451 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstBuffer * buf)
454 GstRtpSsrcDemux *demux;
456 GstRtpSsrcDemuxPad *dpad;
457 GstRTCPPacket packet;
459 demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
461 if (!gst_rtcp_buffer_validate (buf))
464 if (!gst_rtcp_buffer_get_first_packet (buf, &packet))
467 /* first packet must be SR or RR or else the validate would have failed */
468 switch (gst_rtcp_packet_get_type (&packet)) {
469 case GST_RTCP_TYPE_SR:
470 /* get the ssrc so that we can route it to the right source pad */
471 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
474 case GST_RTCP_TYPE_RR:
475 ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
481 GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
483 GST_PAD_LOCK (demux);
484 dpad = find_demux_pad_for_ssrc (demux, ssrc);
486 GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
487 if (!(dpad = create_demux_pad_for_ssrc (demux, ssrc, -1)))
490 GST_PAD_UNLOCK (demux);
493 ret = gst_pad_push (dpad->rtcp_pad, buf);
500 /* this is fatal and should be filtered earlier */
501 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
502 ("Dropping invalid RTCP packet"));
503 gst_buffer_unref (buf);
504 return GST_FLOW_ERROR;
508 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
509 ("Could not create new pad"));
510 GST_PAD_UNLOCK (demux);
511 gst_buffer_unref (buf);
512 return GST_FLOW_ERROR;
517 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event)
519 GstRtpSsrcDemux *demux;
520 gboolean res = FALSE;
522 demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
524 switch (GST_EVENT_TYPE (event)) {
527 res = gst_pad_event_default (pad, event);
530 gst_object_unref (demux);
535 gst_rtp_ssrc_demux_internal_links (GstPad * pad)
537 GstRtpSsrcDemux *demux;
541 demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
543 GST_PAD_LOCK (demux);
544 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
545 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
547 if (pad == demux->rtp_sink) {
548 res = g_list_prepend (res, dpad->rtp_pad);
549 } else if (pad == demux->rtcp_sink) {
550 res = g_list_prepend (res, dpad->rtcp_pad);
551 } else if (pad == dpad->rtp_pad) {
552 res = g_list_prepend (res, demux->rtp_sink);
554 } else if (pad == dpad->rtcp_pad) {
555 res = g_list_prepend (res, demux->rtcp_sink);
559 GST_PAD_UNLOCK (demux);
561 gst_object_unref (demux);
566 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query)
568 GstRtpSsrcDemux *demux;
569 gboolean res = FALSE;
571 demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
573 switch (GST_QUERY_TYPE (query)) {
574 case GST_QUERY_LATENCY:
577 if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
579 GstClockTime min_latency, max_latency;
580 GstRtpSsrcDemuxPad *demuxpad;
582 demuxpad = gst_pad_get_element_private (pad);
584 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
586 GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
587 GST_TIME_ARGS (min_latency));
589 GST_DEBUG_OBJECT (demux,
590 "latency for SSRC %08x, latency %" GST_TIME_FORMAT, demuxpad->ssrc,
591 GST_TIME_ARGS (demuxpad->first_ts));
594 min_latency += demuxpad->first_ts;
595 if (max_latency != -1)
596 max_latency += demuxpad->first_ts;
599 gst_query_set_latency (query, live, min_latency, max_latency);
604 res = gst_pad_query_default (pad, query);
607 gst_object_unref (demux);
612 static GstStateChangeReturn
613 gst_rtp_ssrc_demux_change_state (GstElement * element,
614 GstStateChange transition)
616 GstStateChangeReturn ret;
617 GstRtpSsrcDemux *demux;
619 demux = GST_RTP_SSRC_DEMUX (element);
621 switch (transition) {
622 case GST_STATE_CHANGE_NULL_TO_READY:
623 case GST_STATE_CHANGE_READY_TO_PAUSED:
624 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
629 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
631 switch (transition) {
632 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
633 case GST_STATE_CHANGE_PAUSED_TO_READY:
634 case GST_STATE_CHANGE_READY_TO_NULL: