2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-rtpssrcdemux
25 * rtpssrcdemux acts as a demuxer for RTP packets based on the SSRC of the
26 * packets. Its main purpose is to allow an application to easily receive and
27 * decode an RTP stream with multiple SSRCs.
29 * For each SSRC that is detected, a new pad will be created and the
30 * #GstRtpSsrcDemux::new-ssrc-pad signal will be emitted.
33 * <title>Example pipelines</title>
35 * gst-launch-1.0 udpsrc caps="application/x-rtp" ! rtpssrcdemux ! fakesink
36 * ]| Takes an RTP stream and send the RTP packets with the first detected SSRC
37 * to fakesink, discarding the other SSRCs.
46 #include <gst/rtp/gstrtpbuffer.h>
47 #include <gst/rtp/gstrtcpbuffer.h>
49 #include "gstrtpssrcdemux.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
52 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
54 /* generic templates */
55 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-rtp")
62 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
66 GST_STATIC_CAPS ("application/x-rtcp")
69 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src_%u",
73 GST_STATIC_CAPS ("application/x-rtp")
76 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
77 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%u",
80 GST_STATIC_CAPS ("application/x-rtcp")
83 #define GST_PAD_LOCK(obj) (g_rec_mutex_lock (&(obj)->padlock))
84 #define GST_PAD_UNLOCK(obj) (g_rec_mutex_unlock (&(obj)->padlock))
96 SIGNAL_REMOVED_SSRC_PAD,
101 #define gst_rtp_ssrc_demux_parent_class parent_class
102 G_DEFINE_TYPE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GST_TYPE_ELEMENT);
104 /* GObject vmethods */
105 static void gst_rtp_ssrc_demux_dispose (GObject * object);
106 static void gst_rtp_ssrc_demux_finalize (GObject * object);
108 /* GstElement vmethods */
109 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
110 element, GstStateChange transition);
112 static void gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux,
116 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent,
118 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
121 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
122 GstObject * parent, GstBuffer * buf);
123 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad *
124 pad, GstObject * parent);
127 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
129 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad,
131 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
134 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
137 * Item for storing GstPad <-> SSRC pairs.
139 struct _GstRtpSsrcDemuxPad
146 gboolean pushed_initial_rtp_events;
147 gboolean pushed_initial_rtcp_events;
150 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
152 static GstRtpSsrcDemuxPad *
153 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
157 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
158 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
160 if (pad->ssrc == ssrc)
167 add_ssrc_and_ref (GstEvent * event, guint32 ssrc)
169 /* Set the ssrc on the output caps */
170 switch (GST_EVENT_TYPE (event)) {
177 gst_event_parse_caps (event, &caps);
178 newcaps = gst_caps_copy (caps);
180 s = gst_caps_get_structure (newcaps, 0);
181 gst_structure_set (s, "ssrc", G_TYPE_UINT, ssrc, NULL);
182 event = gst_event_new_caps (newcaps);
183 gst_caps_unref (newcaps);
187 gst_event_ref (event);
194 struct ForwardStickyEventData
201 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
203 struct ForwardStickyEventData *data = user_data;
206 newevent = add_ssrc_and_ref (*event, data->ssrc);
208 gst_pad_push_event (data->pad, newevent);
214 forward_initial_events (GstRtpSsrcDemux * demux, guint32 ssrc, GstPad * pad,
217 struct ForwardStickyEventData fdata;
218 GstPad *sinkpad = NULL;
220 if (padtype == RTP_PAD)
221 sinkpad = demux->rtp_sink;
222 else if (padtype == RTCP_PAD)
223 sinkpad = demux->rtcp_sink;
225 g_assert_not_reached ();
230 gst_pad_sticky_events_foreach (sinkpad, forward_sticky_events, &fdata);
234 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc,
237 GstPad *rtp_pad, *rtcp_pad;
238 GstElementClass *klass;
239 GstPadTemplate *templ;
241 GstRtpSsrcDemuxPad *demuxpad;
243 gulong rtp_block, rtcp_block;
245 GST_PAD_LOCK (demux);
247 demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
248 if (demuxpad != NULL) {
249 gboolean forward = FALSE;
253 retpad = gst_object_ref (demuxpad->rtp_pad);
254 if (!demuxpad->pushed_initial_rtp_events) {
256 demuxpad->pushed_initial_rtp_events = TRUE;
260 retpad = gst_object_ref (demuxpad->rtcp_pad);
261 if (!demuxpad->pushed_initial_rtcp_events) {
263 demuxpad->pushed_initial_rtcp_events = TRUE;
268 g_assert_not_reached ();
271 GST_PAD_UNLOCK (demux);
274 forward_initial_events (demux, ssrc, retpad, padtype);
278 GST_DEBUG_OBJECT (demux, "creating new pad for SSRC %08x", ssrc);
280 klass = GST_ELEMENT_GET_CLASS (demux);
281 templ = gst_element_class_get_pad_template (klass, "src_%u");
282 padname = g_strdup_printf ("src_%u", ssrc);
283 rtp_pad = gst_pad_new_from_template (templ, padname);
286 templ = gst_element_class_get_pad_template (klass, "rtcp_src_%u");
287 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
288 rtcp_pad = gst_pad_new_from_template (templ, padname);
291 /* wrap in structure and add to list */
292 demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
293 demuxpad->ssrc = ssrc;
294 demuxpad->rtp_pad = rtp_pad;
295 demuxpad->rtcp_pad = rtcp_pad;
297 gst_pad_set_element_private (rtp_pad, demuxpad);
298 gst_pad_set_element_private (rtcp_pad, demuxpad);
300 demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
302 gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
303 gst_pad_set_iterate_internal_links_function (rtp_pad,
304 gst_rtp_ssrc_demux_iterate_internal_links_src);
305 gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
306 gst_pad_use_fixed_caps (rtp_pad);
307 gst_pad_set_active (rtp_pad, TRUE);
309 gst_pad_set_event_function (rtcp_pad, gst_rtp_ssrc_demux_src_event);
310 gst_pad_set_iterate_internal_links_function (rtcp_pad,
311 gst_rtp_ssrc_demux_iterate_internal_links_src);
312 gst_pad_use_fixed_caps (rtcp_pad);
313 gst_pad_set_active (rtcp_pad, TRUE);
315 if (padtype == RTP_PAD) {
316 demuxpad->pushed_initial_rtp_events = TRUE;
317 forward_initial_events (demux, ssrc, rtp_pad, padtype);
318 } else if (padtype == RTCP_PAD) {
319 demuxpad->pushed_initial_rtcp_events = TRUE;
320 forward_initial_events (demux, ssrc, rtcp_pad, padtype);
322 g_assert_not_reached ();
325 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
326 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
330 retpad = gst_object_ref (demuxpad->rtp_pad);
333 retpad = gst_object_ref (demuxpad->rtcp_pad);
337 g_assert_not_reached ();
340 gst_object_ref (rtp_pad);
341 gst_object_ref (rtcp_pad);
343 rtp_block = gst_pad_add_probe (rtp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
345 rtcp_block = gst_pad_add_probe (rtcp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
348 GST_PAD_UNLOCK (demux);
350 g_signal_emit (G_OBJECT (demux),
351 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
353 gst_pad_remove_probe (rtp_pad, rtp_block);
354 gst_pad_remove_probe (rtcp_pad, rtcp_block);
356 gst_object_unref (rtp_pad);
357 gst_object_unref (rtcp_pad);
363 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
365 GObjectClass *gobject_klass;
366 GstElementClass *gstelement_klass;
367 GstRtpSsrcDemuxClass *gstrtpssrcdemux_klass;
369 gobject_klass = (GObjectClass *) klass;
370 gstelement_klass = (GstElementClass *) klass;
371 gstrtpssrcdemux_klass = (GstRtpSsrcDemuxClass *) klass;
373 gobject_klass->dispose = gst_rtp_ssrc_demux_dispose;
374 gobject_klass->finalize = gst_rtp_ssrc_demux_finalize;
377 * GstRtpSsrcDemux::new-ssrc-pad:
378 * @demux: the object which received the signal
379 * @ssrc: the SSRC of the pad
382 * Emited when a new SSRC pad has been created.
384 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
385 g_signal_new ("new-ssrc-pad",
386 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
387 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
388 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
392 * GstRtpSsrcDemux::removed-ssrc-pad:
393 * @demux: the object which received the signal
394 * @ssrc: the SSRC of the pad
395 * @pad: the removed pad.
397 * Emited when a SSRC pad has been removed.
399 gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD] =
400 g_signal_new ("removed-ssrc-pad",
401 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
402 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, removed_ssrc_pad),
403 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
407 * GstRtpSsrcDemux::clear-ssrc:
408 * @demux: the object which received the signal
409 * @ssrc: the SSRC of the pad
411 * Action signal to remove the pad for SSRC.
413 gst_rtp_ssrc_demux_signals[SIGNAL_CLEAR_SSRC] =
414 g_signal_new ("clear-ssrc",
415 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
416 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, clear_ssrc),
417 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT);
419 gstelement_klass->change_state =
420 GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
421 gstrtpssrcdemux_klass->clear_ssrc =
422 GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_clear_ssrc);
424 gst_element_class_add_pad_template (gstelement_klass,
425 gst_static_pad_template_get (&rtp_ssrc_demux_sink_template));
426 gst_element_class_add_pad_template (gstelement_klass,
427 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_sink_template));
428 gst_element_class_add_pad_template (gstelement_klass,
429 gst_static_pad_template_get (&rtp_ssrc_demux_src_template));
430 gst_element_class_add_pad_template (gstelement_klass,
431 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_src_template));
433 gst_element_class_set_static_metadata (gstelement_klass, "RTP SSRC Demux",
435 "Splits RTP streams based on the SSRC",
436 "Wim Taymans <wim.taymans@gmail.com>");
438 GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
439 "rtpssrcdemux", 0, "RTP SSRC demuxer");
443 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux)
445 GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
448 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
450 gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
451 gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
452 gst_pad_set_iterate_internal_links_function (demux->rtp_sink,
453 gst_rtp_ssrc_demux_iterate_internal_links_sink);
454 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
457 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
458 "rtcp_sink"), "rtcp_sink");
459 gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
460 gst_pad_set_event_function (demux->rtcp_sink, gst_rtp_ssrc_demux_sink_event);
461 gst_pad_set_iterate_internal_links_function (demux->rtcp_sink,
462 gst_rtp_ssrc_demux_iterate_internal_links_sink);
463 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
465 g_rec_mutex_init (&demux->padlock);
469 gst_rtp_ssrc_demux_reset (GstRtpSsrcDemux * demux)
473 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
474 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
476 gst_pad_set_active (dpad->rtp_pad, FALSE);
477 gst_pad_set_active (dpad->rtcp_pad, FALSE);
479 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
480 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
483 g_slist_free (demux->srcpads);
484 demux->srcpads = NULL;
488 gst_rtp_ssrc_demux_dispose (GObject * object)
490 GstRtpSsrcDemux *demux;
492 demux = GST_RTP_SSRC_DEMUX (object);
494 gst_rtp_ssrc_demux_reset (demux);
496 G_OBJECT_CLASS (parent_class)->dispose (object);
500 gst_rtp_ssrc_demux_finalize (GObject * object)
502 GstRtpSsrcDemux *demux;
504 demux = GST_RTP_SSRC_DEMUX (object);
505 g_rec_mutex_clear (&demux->padlock);
507 G_OBJECT_CLASS (parent_class)->finalize (object);
511 gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
513 GstRtpSsrcDemuxPad *dpad;
515 GST_PAD_LOCK (demux);
516 dpad = find_demux_pad_for_ssrc (demux, ssrc);
518 GST_PAD_UNLOCK (demux);
522 GST_DEBUG_OBJECT (demux, "clearing pad for SSRC %08x", ssrc);
524 demux->srcpads = g_slist_remove (demux->srcpads, dpad);
525 GST_PAD_UNLOCK (demux);
527 gst_pad_set_active (dpad->rtp_pad, FALSE);
528 gst_pad_set_active (dpad->rtcp_pad, FALSE);
530 g_signal_emit (G_OBJECT (demux),
531 gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD], 0, ssrc,
534 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
535 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
544 GST_WARNING_OBJECT (demux, "unknown SSRC %08x", ssrc);
549 struct ForwardEventData
551 GstRtpSsrcDemux *demux;
558 forward_event (GstPad * pad, gpointer user_data)
560 struct ForwardEventData *fdata = user_data;
562 GstEvent *newevent = NULL;
564 GST_PAD_LOCK (fdata->demux);
565 for (walk = fdata->demux->srcpads; walk; walk = walk->next) {
566 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
568 /* Only forward the event if the initial events have been through first,
569 * the initial events should be forwarded before any other event
570 * or buffer is pushed */
571 if ((pad == dpad->rtp_pad && dpad->pushed_initial_rtp_events) ||
572 (pad == dpad->rtcp_pad && dpad->pushed_initial_rtcp_events)) {
573 newevent = add_ssrc_and_ref (fdata->event, dpad->ssrc);
577 GST_PAD_UNLOCK (fdata->demux);
580 fdata->res &= gst_pad_push_event (pad, newevent);
587 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
590 GstRtpSsrcDemux *demux;
591 struct ForwardEventData fdata;
593 demux = GST_RTP_SSRC_DEMUX (parent);
600 gst_pad_forward (pad, forward_event, &fdata);
602 gst_event_unref (event);
608 gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
611 GstRtpSsrcDemux *demux;
613 GstRTPBuffer rtp = { NULL };
615 GstRtpSsrcDemuxPad *dpad;
617 demux = GST_RTP_SSRC_DEMUX (parent);
619 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
620 goto invalid_payload;
622 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
623 gst_rtp_buffer_unmap (&rtp);
625 GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
627 srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTP_PAD);
632 ret = gst_pad_push (srcpad, buf);
634 if (ret != GST_FLOW_OK) {
635 /* check if the ssrc still there, may have been removed */
636 GST_PAD_LOCK (demux);
637 dpad = find_demux_pad_for_ssrc (demux, ssrc);
638 if (dpad == NULL || dpad->rtp_pad != srcpad) {
639 /* SSRC was removed during the push ... ignore the error */
642 GST_PAD_UNLOCK (demux);
645 gst_object_unref (srcpad);
652 /* this is fatal and should be filtered earlier */
653 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
654 ("Dropping invalid RTP payload"));
655 gst_buffer_unref (buf);
656 return GST_FLOW_ERROR;
660 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
661 ("Could not create new pad"));
662 gst_buffer_unref (buf);
663 return GST_FLOW_ERROR;
668 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstObject * parent,
672 GstRtpSsrcDemux *demux;
674 GstRTCPPacket packet;
675 GstRTCPBuffer rtcp = { NULL, };
677 GstRtpSsrcDemuxPad *dpad;
679 demux = GST_RTP_SSRC_DEMUX (parent);
681 if (!gst_rtcp_buffer_validate (buf))
684 gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcp);
685 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
686 gst_rtcp_buffer_unmap (&rtcp);
690 /* first packet must be SR or RR or else the validate would have failed */
691 switch (gst_rtcp_packet_get_type (&packet)) {
692 case GST_RTCP_TYPE_SR:
693 /* get the ssrc so that we can route it to the right source pad */
694 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
697 case GST_RTCP_TYPE_RR:
698 ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
701 goto unexpected_rtcp;
703 gst_rtcp_buffer_unmap (&rtcp);
705 GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
707 srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTCP_PAD);
712 ret = gst_pad_push (srcpad, buf);
714 if (ret != GST_FLOW_OK) {
715 /* check if the ssrc still there, may have been removed */
716 GST_PAD_LOCK (demux);
717 dpad = find_demux_pad_for_ssrc (demux, ssrc);
718 if (dpad == NULL || dpad->rtcp_pad != srcpad) {
719 /* SSRC was removed during the push ... ignore the error */
722 GST_PAD_UNLOCK (demux);
725 gst_object_unref (srcpad);
732 /* this is fatal and should be filtered earlier */
733 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
734 ("Dropping invalid RTCP packet"));
735 gst_buffer_unref (buf);
736 return GST_FLOW_ERROR;
740 GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
741 gst_buffer_unref (buf);
746 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
747 ("Could not create new pad"));
748 gst_buffer_unref (buf);
749 return GST_FLOW_ERROR;
753 static GstRtpSsrcDemuxPad *
754 find_demux_pad_for_pad (GstRtpSsrcDemux * demux, GstPad * pad)
758 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
759 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
760 if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
770 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
773 GstRtpSsrcDemux *demux;
774 const GstStructure *s;
776 demux = GST_RTP_SSRC_DEMUX (parent);
778 switch (GST_EVENT_TYPE (event)) {
779 case GST_EVENT_CUSTOM_UPSTREAM:
780 case GST_EVENT_CUSTOM_BOTH:
781 case GST_EVENT_CUSTOM_BOTH_OOB:
782 s = gst_event_get_structure (event);
783 if (s && !gst_structure_has_field (s, "ssrc")) {
784 GstRtpSsrcDemuxPad *dpad = find_demux_pad_for_pad (demux, pad);
789 event = gst_event_make_writable (event);
790 ws = gst_event_writable_structure (event);
791 gst_structure_set (ws, "ssrc", G_TYPE_UINT, dpad->ssrc, NULL);
799 return gst_pad_event_default (pad, parent, event);
803 gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad, GstObject * parent)
805 GstRtpSsrcDemux *demux;
806 GstPad *otherpad = NULL;
807 GstIterator *it = NULL;
810 demux = GST_RTP_SSRC_DEMUX (parent);
812 GST_PAD_LOCK (demux);
813 for (current = demux->srcpads; current; current = g_slist_next (current)) {
814 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
816 if (pad == dpad->rtp_pad) {
817 otherpad = demux->rtp_sink;
819 } else if (pad == dpad->rtcp_pad) {
820 otherpad = demux->rtcp_sink;
827 g_value_init (&val, GST_TYPE_PAD);
828 g_value_set_object (&val, otherpad);
829 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
830 g_value_unset (&val);
833 GST_PAD_UNLOCK (demux);
838 /* Should return 0 for elements to be included */
840 src_pad_compare_func (gconstpointer a, gconstpointer b)
842 GstPad *pad = GST_PAD (g_value_get_object (a));
843 const gchar *prefix = g_value_get_string (b);
846 /* 0 means equal means we accept the pad, accepted if there is a name
847 * and it starts with the prefix */
848 GST_OBJECT_LOCK (pad);
849 res = !GST_PAD_NAME (pad) || !g_str_has_prefix (GST_PAD_NAME (pad), prefix);
850 GST_OBJECT_UNLOCK (pad);
856 gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad * pad,
859 GstRtpSsrcDemux *demux;
860 GstIterator *it = NULL;
861 GValue gval = { 0, };
863 demux = GST_RTP_SSRC_DEMUX (parent);
865 g_value_init (&gval, G_TYPE_STRING);
866 if (pad == demux->rtp_sink)
867 g_value_set_static_string (&gval, "src_");
868 else if (pad == demux->rtcp_sink)
869 g_value_set_static_string (&gval, "rtcp_src_");
871 g_assert_not_reached ();
873 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
874 it = gst_iterator_filter (it, src_pad_compare_func, &gval);
881 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
884 GstRtpSsrcDemux *demux;
885 gboolean res = FALSE;
887 demux = GST_RTP_SSRC_DEMUX (parent);
889 switch (GST_QUERY_TYPE (query)) {
890 case GST_QUERY_LATENCY:
893 if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
895 GstClockTime min_latency, max_latency;
896 GstRtpSsrcDemuxPad *demuxpad;
898 demuxpad = gst_pad_get_element_private (pad);
900 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
902 GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
903 GST_TIME_ARGS (min_latency));
905 GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
907 gst_query_set_latency (query, live, min_latency, max_latency);
912 res = gst_pad_query_default (pad, parent, query);
919 static GstStateChangeReturn
920 gst_rtp_ssrc_demux_change_state (GstElement * element,
921 GstStateChange transition)
923 GstStateChangeReturn ret;
924 GstRtpSsrcDemux *demux;
926 demux = GST_RTP_SSRC_DEMUX (element);
928 switch (transition) {
929 case GST_STATE_CHANGE_NULL_TO_READY:
930 case GST_STATE_CHANGE_READY_TO_PAUSED:
931 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
936 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
938 switch (transition) {
939 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
941 case GST_STATE_CHANGE_PAUSED_TO_READY:
942 gst_rtp_ssrc_demux_reset (demux);
944 case GST_STATE_CHANGE_READY_TO_NULL: