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-gstrtpssrcdemux
25 * gstrtpssrcdemux 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" ! gstrtpssrcdemux ! fakesink
36 * ]| Takes an RTP stream and send the RTP packets with the first detected SSRC
37 * to fakesink, discarding the other SSRCs.
40 * Last reviewed on 2007-05-28 (0.10.5)
48 #include <gst/rtp/gstrtpbuffer.h>
49 #include <gst/rtp/gstrtcpbuffer.h>
51 #include "gstrtpbin-marshal.h"
52 #include "gstrtpssrcdemux.h"
54 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
55 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
57 /* generic templates */
58 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("application/x-rtp")
65 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
66 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
69 GST_STATIC_CAPS ("application/x-rtcp")
72 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src_%u",
76 GST_STATIC_CAPS ("application/x-rtp")
79 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
80 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%u",
83 GST_STATIC_CAPS ("application/x-rtcp")
86 #define GST_PAD_LOCK(obj) (g_rec_mutex_lock (&(obj)->padlock))
87 #define GST_PAD_UNLOCK(obj) (g_rec_mutex_unlock (&(obj)->padlock))
99 SIGNAL_REMOVED_SSRC_PAD,
104 #define gst_rtp_ssrc_demux_parent_class parent_class
105 G_DEFINE_TYPE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GST_TYPE_ELEMENT);
107 /* GObject vmethods */
108 static void gst_rtp_ssrc_demux_dispose (GObject * object);
109 static void gst_rtp_ssrc_demux_finalize (GObject * object);
111 /* GstElement vmethods */
112 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
113 element, GstStateChange transition);
115 static void gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux,
119 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent,
121 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
124 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
125 GstObject * parent, GstBuffer * buf);
126 static gboolean gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad,
127 GstObject * parent, GstEvent * event);
128 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad *
129 pad, GstObject * parent);
132 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
134 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad,
136 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
139 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
142 * Item for storing GstPad <-> SSRC pairs.
144 struct _GstRtpSsrcDemuxPad
152 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
154 static GstRtpSsrcDemuxPad *
155 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
159 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
160 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
162 if (pad->ssrc == ssrc)
169 add_ssrc_and_ref (GstEvent * event, guint32 ssrc)
171 /* Set the ssrc on the output caps */
172 switch (GST_EVENT_TYPE (event)) {
179 gst_event_parse_caps (event, &caps);
180 newcaps = gst_caps_copy (caps);
182 s = gst_caps_get_structure (newcaps, 0);
183 gst_structure_set (s, "ssrc", G_TYPE_UINT, ssrc, NULL);
184 event = gst_event_new_caps (newcaps);
185 gst_caps_unref (newcaps);
189 gst_event_ref (event);
196 struct ForwardEventData
203 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
205 struct ForwardEventData *data = user_data;
208 newevent = add_ssrc_and_ref (*event, data->ssrc);
210 gst_pad_push_event (data->pad, newevent);
217 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc,
220 GstPad *rtp_pad, *rtcp_pad;
221 GstElementClass *klass;
222 GstPadTemplate *templ;
224 GstRtpSsrcDemuxPad *demuxpad;
226 struct ForwardEventData fdata;
228 gulong rtp_block, rtcp_block;
230 GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
232 GST_PAD_LOCK (demux);
234 demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
235 if (demuxpad != NULL) {
238 retpad = gst_object_ref (demuxpad->rtp_pad);
241 retpad = gst_object_ref (demuxpad->rtcp_pad);
245 g_assert_not_reached ();
247 GST_PAD_UNLOCK (demux);
251 klass = GST_ELEMENT_GET_CLASS (demux);
252 templ = gst_element_class_get_pad_template (klass, "src_%u");
253 padname = g_strdup_printf ("src_%u", ssrc);
254 rtp_pad = gst_pad_new_from_template (templ, padname);
257 templ = gst_element_class_get_pad_template (klass, "rtcp_src_%u");
258 padname = g_strdup_printf ("rtcp_src_%u", ssrc);
259 rtcp_pad = gst_pad_new_from_template (templ, padname);
262 /* wrap in structure and add to list */
263 demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
264 demuxpad->ssrc = ssrc;
265 demuxpad->rtp_pad = rtp_pad;
266 demuxpad->rtcp_pad = rtcp_pad;
270 gst_pad_set_element_private (rtp_pad, demuxpad);
271 gst_pad_set_element_private (rtcp_pad, demuxpad);
273 demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
275 gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
276 gst_pad_set_iterate_internal_links_function (rtp_pad,
277 gst_rtp_ssrc_demux_iterate_internal_links_src);
278 gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
279 gst_pad_use_fixed_caps (rtp_pad);
280 gst_pad_set_active (rtp_pad, TRUE);
282 gst_pad_sticky_events_foreach (demux->rtp_sink, forward_sticky_events,
285 gst_pad_set_event_function (rtcp_pad, gst_rtp_ssrc_demux_src_event);
286 gst_pad_set_iterate_internal_links_function (rtcp_pad,
287 gst_rtp_ssrc_demux_iterate_internal_links_src);
288 gst_pad_use_fixed_caps (rtcp_pad);
289 gst_pad_set_active (rtcp_pad, TRUE);
290 fdata.pad = rtcp_pad;
291 gst_pad_sticky_events_foreach (demux->rtcp_sink, forward_sticky_events,
294 /* copy caps from input */
295 if ((caps = gst_pad_get_current_caps (demux->rtp_sink))) {
296 gst_pad_set_caps (rtp_pad, caps);
297 gst_caps_unref (caps);
299 if ((caps = gst_pad_get_current_caps (demux->rtcp_sink))) {
300 gst_pad_set_caps (rtcp_pad, caps);
301 gst_caps_unref (caps);
303 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
304 gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
308 retpad = gst_object_ref (demuxpad->rtp_pad);
311 retpad = gst_object_ref (demuxpad->rtcp_pad);
315 g_assert_not_reached ();
318 gst_object_ref (rtp_pad);
319 gst_object_ref (rtcp_pad);
321 rtp_block = gst_pad_add_probe (rtp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
323 rtcp_block = gst_pad_add_probe (rtcp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
326 GST_PAD_UNLOCK (demux);
328 g_signal_emit (G_OBJECT (demux),
329 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
331 gst_pad_remove_probe (rtp_pad, rtp_block);
332 gst_pad_remove_probe (rtcp_pad, rtcp_block);
334 gst_object_unref (rtp_pad);
335 gst_object_unref (rtcp_pad);
341 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
343 GObjectClass *gobject_klass;
344 GstElementClass *gstelement_klass;
345 GstRtpSsrcDemuxClass *gstrtpssrcdemux_klass;
347 gobject_klass = (GObjectClass *) klass;
348 gstelement_klass = (GstElementClass *) klass;
349 gstrtpssrcdemux_klass = (GstRtpSsrcDemuxClass *) klass;
351 gobject_klass->dispose = gst_rtp_ssrc_demux_dispose;
352 gobject_klass->finalize = gst_rtp_ssrc_demux_finalize;
355 * GstRtpSsrcDemux::new-ssrc-pad:
356 * @demux: the object which received the signal
357 * @ssrc: the SSRC of the pad
360 * Emited when a new SSRC pad has been created.
362 gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
363 g_signal_new ("new-ssrc-pad",
364 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
365 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
366 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
367 G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
370 * GstRtpSsrcDemux::removed-ssrc-pad:
371 * @demux: the object which received the signal
372 * @ssrc: the SSRC of the pad
373 * @pad: the removed pad.
375 * Emited when a SSRC pad has been removed.
377 gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD] =
378 g_signal_new ("removed-ssrc-pad",
379 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
380 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, removed_ssrc_pad),
381 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
382 G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
385 * GstRtpSsrcDemux::clear-ssrc:
386 * @demux: the object which received the signal
387 * @ssrc: the SSRC of the pad
389 * Action signal to remove the pad for SSRC.
391 gst_rtp_ssrc_demux_signals[SIGNAL_CLEAR_SSRC] =
392 g_signal_new ("clear-ssrc",
393 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
394 G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, clear_ssrc),
395 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
397 gstelement_klass->change_state =
398 GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
399 gstrtpssrcdemux_klass->clear_ssrc =
400 GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_clear_ssrc);
402 gst_element_class_add_pad_template (gstelement_klass,
403 gst_static_pad_template_get (&rtp_ssrc_demux_sink_template));
404 gst_element_class_add_pad_template (gstelement_klass,
405 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_sink_template));
406 gst_element_class_add_pad_template (gstelement_klass,
407 gst_static_pad_template_get (&rtp_ssrc_demux_src_template));
408 gst_element_class_add_pad_template (gstelement_klass,
409 gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_src_template));
411 gst_element_class_set_static_metadata (gstelement_klass, "RTP SSRC Demux",
413 "Splits RTP streams based on the SSRC",
414 "Wim Taymans <wim.taymans@gmail.com>");
416 GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
417 "rtpssrcdemux", 0, "RTP SSRC demuxer");
421 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux)
423 GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
426 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
428 gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
429 gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
430 gst_pad_set_iterate_internal_links_function (demux->rtp_sink,
431 gst_rtp_ssrc_demux_iterate_internal_links_sink);
432 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
435 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
436 "rtcp_sink"), "rtcp_sink");
437 gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
438 gst_pad_set_event_function (demux->rtcp_sink,
439 gst_rtp_ssrc_demux_rtcp_sink_event);
440 gst_pad_set_iterate_internal_links_function (demux->rtcp_sink,
441 gst_rtp_ssrc_demux_iterate_internal_links_sink);
442 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
444 g_rec_mutex_init (&demux->padlock);
446 gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
450 gst_rtp_ssrc_demux_reset (GstRtpSsrcDemux * demux)
454 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
455 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
457 gst_pad_set_active (dpad->rtp_pad, FALSE);
458 gst_pad_set_active (dpad->rtcp_pad, FALSE);
460 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
461 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
464 g_slist_free (demux->srcpads);
465 demux->srcpads = NULL;
469 gst_rtp_ssrc_demux_dispose (GObject * object)
471 GstRtpSsrcDemux *demux;
473 demux = GST_RTP_SSRC_DEMUX (object);
475 gst_rtp_ssrc_demux_reset (demux);
477 G_OBJECT_CLASS (parent_class)->dispose (object);
481 gst_rtp_ssrc_demux_finalize (GObject * object)
483 GstRtpSsrcDemux *demux;
485 demux = GST_RTP_SSRC_DEMUX (object);
486 g_rec_mutex_clear (&demux->padlock);
488 G_OBJECT_CLASS (parent_class)->finalize (object);
492 gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
494 GstRtpSsrcDemuxPad *dpad;
496 GST_PAD_LOCK (demux);
497 dpad = find_demux_pad_for_ssrc (demux, ssrc);
499 GST_PAD_UNLOCK (demux);
503 GST_DEBUG_OBJECT (demux, "clearing pad for SSRC %08x", ssrc);
505 demux->srcpads = g_slist_remove (demux->srcpads, dpad);
506 GST_PAD_UNLOCK (demux);
508 gst_pad_set_active (dpad->rtp_pad, FALSE);
509 gst_pad_set_active (dpad->rtcp_pad, FALSE);
511 g_signal_emit (G_OBJECT (demux),
512 gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD], 0, ssrc,
515 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
516 gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
525 GST_WARNING_OBJECT (demux, "unknown SSRC %08x", ssrc);
531 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
534 GstRtpSsrcDemux *demux;
535 gboolean res = FALSE;
537 demux = GST_RTP_SSRC_DEMUX (parent);
539 switch (GST_EVENT_TYPE (event)) {
540 case GST_EVENT_FLUSH_STOP:
541 gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
549 /* need local snapshot of pads;
550 * should not push downstream while holding lock as that might deadlock
551 * with stuff traveling upstream tyring to get this lock while holding
552 * other (stream)lock */
553 GST_PAD_LOCK (demux);
554 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
555 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
557 pad = g_slice_dup (GstRtpSsrcDemuxPad, pad);
558 gst_object_ref (pad->rtp_pad);
560 pads = g_slist_prepend (pads, pad);
562 GST_PAD_UNLOCK (demux);
564 for (walk = pads; walk; walk = g_slist_next (walk)) {
565 GstRtpSsrcDemuxPad *dpad = walk->data;
568 newevent = add_ssrc_and_ref (event, dpad->ssrc);
570 res &= gst_pad_push_event (dpad->rtp_pad, newevent);
571 gst_object_unref (dpad->rtp_pad);
572 g_slice_free (GstRtpSsrcDemuxPad, dpad);
575 gst_event_unref (event);
584 gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad, GstObject * parent,
587 GstRtpSsrcDemux *demux;
592 demux = GST_RTP_SSRC_DEMUX (parent);
594 GST_PAD_LOCK (demux);
595 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
596 GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
598 pad = g_slice_dup (GstRtpSsrcDemuxPad, pad);
599 gst_object_ref (pad->rtcp_pad);
601 pads = g_slist_prepend (pads, pad);
603 GST_PAD_UNLOCK (demux);
605 for (walk = pads; walk; walk = g_slist_next (walk)) {
606 GstRtpSsrcDemuxPad *dpad = walk->data;
609 newevent = add_ssrc_and_ref (event, dpad->ssrc);
611 res &= gst_pad_push_event (dpad->rtcp_pad, newevent);
612 gst_object_unref (dpad->rtcp_pad);
613 g_slice_free (GstRtpSsrcDemuxPad, dpad);
616 gst_event_unref (event);
622 gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
625 GstRtpSsrcDemux *demux;
627 GstRTPBuffer rtp = { NULL };
629 GstRtpSsrcDemuxPad *dpad;
631 demux = GST_RTP_SSRC_DEMUX (parent);
633 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
634 goto invalid_payload;
636 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
637 gst_rtp_buffer_unmap (&rtp);
639 GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
641 srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTP_PAD);
646 ret = gst_pad_push (srcpad, buf);
648 if (ret != GST_FLOW_OK) {
649 /* check if the ssrc still there, may have been removed */
650 GST_PAD_LOCK (demux);
651 dpad = find_demux_pad_for_ssrc (demux, ssrc);
652 if (dpad == NULL || dpad->rtp_pad != srcpad) {
653 /* SSRC was removed during the push ... ignore the error */
656 GST_PAD_UNLOCK (demux);
659 gst_object_unref (srcpad);
666 /* this is fatal and should be filtered earlier */
667 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
668 ("Dropping invalid RTP payload"));
669 gst_buffer_unref (buf);
670 return GST_FLOW_ERROR;
674 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
675 ("Could not create new pad"));
676 gst_buffer_unref (buf);
677 return GST_FLOW_ERROR;
682 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstObject * parent,
686 GstRtpSsrcDemux *demux;
688 GstRTCPPacket packet;
689 GstRTCPBuffer rtcp = { NULL, };
691 GstRtpSsrcDemuxPad *dpad;
693 demux = GST_RTP_SSRC_DEMUX (parent);
695 if (!gst_rtcp_buffer_validate (buf))
698 gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcp);
699 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
700 gst_rtcp_buffer_unmap (&rtcp);
704 /* first packet must be SR or RR or else the validate would have failed */
705 switch (gst_rtcp_packet_get_type (&packet)) {
706 case GST_RTCP_TYPE_SR:
707 /* get the ssrc so that we can route it to the right source pad */
708 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
712 goto unexpected_rtcp;
714 gst_rtcp_buffer_unmap (&rtcp);
716 GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
718 srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTCP_PAD);
723 ret = gst_pad_push (srcpad, buf);
725 if (ret != GST_FLOW_OK) {
726 /* check if the ssrc still there, may have been removed */
727 GST_PAD_LOCK (demux);
728 dpad = find_demux_pad_for_ssrc (demux, ssrc);
729 if (dpad == NULL || dpad->rtcp_pad != srcpad) {
730 /* SSRC was removed during the push ... ignore the error */
733 GST_PAD_UNLOCK (demux);
736 gst_object_unref (srcpad);
743 /* this is fatal and should be filtered earlier */
744 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
745 ("Dropping invalid RTCP packet"));
746 gst_buffer_unref (buf);
747 return GST_FLOW_ERROR;
751 GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
752 gst_buffer_unref (buf);
757 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
758 ("Could not create new pad"));
759 gst_buffer_unref (buf);
760 return GST_FLOW_ERROR;
764 static GstRtpSsrcDemuxPad *
765 find_demux_pad_for_pad (GstRtpSsrcDemux * demux, GstPad * pad)
769 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
770 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
771 if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
781 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
784 GstRtpSsrcDemux *demux;
785 const GstStructure *s;
787 demux = GST_RTP_SSRC_DEMUX (parent);
789 switch (GST_EVENT_TYPE (event)) {
790 case GST_EVENT_CUSTOM_UPSTREAM:
791 case GST_EVENT_CUSTOM_BOTH:
792 case GST_EVENT_CUSTOM_BOTH_OOB:
793 s = gst_event_get_structure (event);
794 if (s && !gst_structure_has_field (s, "ssrc")) {
795 GstRtpSsrcDemuxPad *dpad = find_demux_pad_for_pad (demux, pad);
800 event = gst_event_make_writable (event);
801 ws = gst_event_writable_structure (event);
802 gst_structure_set (ws, "ssrc", G_TYPE_UINT, dpad->ssrc, NULL);
810 return gst_pad_event_default (pad, parent, event);
814 gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad, GstObject * parent)
816 GstRtpSsrcDemux *demux;
817 GstPad *otherpad = NULL;
818 GstIterator *it = NULL;
821 demux = GST_RTP_SSRC_DEMUX (parent);
823 GST_PAD_LOCK (demux);
824 for (current = demux->srcpads; current; current = g_slist_next (current)) {
825 GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
827 if (pad == dpad->rtp_pad) {
828 otherpad = demux->rtp_sink;
830 } else if (pad == dpad->rtcp_pad) {
831 otherpad = demux->rtcp_sink;
838 g_value_init (&val, GST_TYPE_PAD);
839 g_value_set_object (&val, otherpad);
840 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
841 g_value_unset (&val);
844 GST_PAD_UNLOCK (demux);
849 /* Should return 0 for elements to be included */
851 src_pad_compare_func (gconstpointer a, gconstpointer b)
853 GstPad *pad = GST_PAD (g_value_get_object (a));
854 const gchar *prefix = g_value_get_string (b);
857 GST_OBJECT_LOCK (pad);
858 res = !GST_PAD_NAME (pad) || g_str_has_prefix (GST_PAD_NAME (pad), prefix);
859 GST_OBJECT_UNLOCK (pad);
865 gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad * pad,
868 GstRtpSsrcDemux *demux;
869 GstIterator *it = NULL;
870 GValue gval = { 0, };
872 demux = GST_RTP_SSRC_DEMUX (parent);
874 g_value_init (&gval, G_TYPE_STRING);
875 if (pad == demux->rtp_sink)
876 g_value_set_static_string (&gval, "src_");
877 else if (pad == demux->rtcp_sink)
878 g_value_set_static_string (&gval, "rtcp_src_");
880 g_assert_not_reached ();
882 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
883 it = gst_iterator_filter (it, src_pad_compare_func, &gval);
890 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
893 GstRtpSsrcDemux *demux;
894 gboolean res = FALSE;
896 demux = GST_RTP_SSRC_DEMUX (parent);
898 switch (GST_QUERY_TYPE (query)) {
899 case GST_QUERY_LATENCY:
902 if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
904 GstClockTime min_latency, max_latency;
905 GstRtpSsrcDemuxPad *demuxpad;
907 demuxpad = gst_pad_get_element_private (pad);
909 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
911 GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
912 GST_TIME_ARGS (min_latency));
914 GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
916 gst_query_set_latency (query, live, min_latency, max_latency);
921 res = gst_pad_query_default (pad, parent, query);
928 static GstStateChangeReturn
929 gst_rtp_ssrc_demux_change_state (GstElement * element,
930 GstStateChange transition)
932 GstStateChangeReturn ret;
933 GstRtpSsrcDemux *demux;
935 demux = GST_RTP_SSRC_DEMUX (element);
937 switch (transition) {
938 case GST_STATE_CHANGE_NULL_TO_READY:
939 case GST_STATE_CHANGE_READY_TO_PAUSED:
940 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
945 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
947 switch (transition) {
948 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
950 case GST_STATE_CHANGE_PAUSED_TO_READY:
951 gst_rtp_ssrc_demux_reset (demux);
953 case GST_STATE_CHANGE_READY_TO_NULL: