Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpssrcdemux.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * RTP SSRC demuxer
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-gstrtpssrcdemux
24  *
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.
28  * 
29  * For each SSRC that is detected, a new pad will be created and the
30  * #GstRtpSsrcDemux::new-ssrc-pad signal will be emitted. 
31  * 
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch 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.
38  * </refsect2>
39  *
40  * Last reviewed on 2007-05-28 (0.10.5)
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <string.h>
48 #include <gst/rtp/gstrtpbuffer.h>
49 #include <gst/rtp/gstrtcpbuffer.h>
50
51 #include "gstrtpbin-marshal.h"
52 #include "gstrtpssrcdemux.h"
53
54 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
55 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
56
57 /* generic templates */
58 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("application/x-rtp")
63     );
64
65 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
66 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("application/x-rtcp")
70     );
71
72 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src_%d",
74     GST_PAD_SRC,
75     GST_PAD_SOMETIMES,
76     GST_STATIC_CAPS ("application/x-rtp")
77     );
78
79 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
80 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%d",
81     GST_PAD_SRC,
82     GST_PAD_SOMETIMES,
83     GST_STATIC_CAPS ("application/x-rtcp")
84     );
85
86 #define GST_PAD_LOCK(obj)   (g_mutex_lock ((obj)->padlock))
87 #define GST_PAD_UNLOCK(obj) (g_mutex_unlock ((obj)->padlock))
88
89 /* signals */
90 enum
91 {
92   SIGNAL_NEW_SSRC_PAD,
93   SIGNAL_REMOVED_SSRC_PAD,
94   SIGNAL_CLEAR_SSRC,
95   LAST_SIGNAL
96 };
97
98 GST_BOILERPLATE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GstElement,
99     GST_TYPE_ELEMENT);
100
101
102 /* GObject vmethods */
103 static void gst_rtp_ssrc_demux_dispose (GObject * object);
104 static void gst_rtp_ssrc_demux_finalize (GObject * object);
105
106 /* GstElement vmethods */
107 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
108     element, GstStateChange transition);
109
110 static void gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux,
111     guint32 ssrc);
112
113 /* sinkpad stuff */
114 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf);
115 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event);
116
117 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
118     GstBuffer * buf);
119 static gboolean gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad,
120     GstEvent * event);
121 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad *
122     pad);
123
124 /* srcpad stuff */
125 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event);
126 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad *
127     pad);
128 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query);
129
130 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
131
132 /*
133  * Item for storing GstPad <-> SSRC pairs.
134  */
135 struct _GstRtpSsrcDemuxPad
136 {
137   guint32 ssrc;
138   GstPad *rtp_pad;
139   GstCaps *caps;
140   GstPad *rtcp_pad;
141 };
142
143 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
144  */
145 static GstRtpSsrcDemuxPad *
146 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
147 {
148   GSList *walk;
149
150   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
151     GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
152
153     if (pad->ssrc == ssrc)
154       return pad;
155   }
156   return NULL;
157 }
158
159 static GstRtpSsrcDemuxPad *
160 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
161 {
162   GstPad *rtp_pad, *rtcp_pad;
163   GstElementClass *klass;
164   GstPadTemplate *templ;
165   gchar *padname;
166   GstRtpSsrcDemuxPad *demuxpad;
167
168   GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
169
170   GST_OBJECT_LOCK (demux);
171
172   demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
173   if (demuxpad != NULL) {
174     GST_OBJECT_UNLOCK (demux);
175     return demuxpad;
176   }
177
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);
182   g_free (padname);
183
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);
187   g_free (padname);
188
189   /* wrap in structure and add to list */
190   demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
191   demuxpad->ssrc = ssrc;
192   demuxpad->rtp_pad = rtp_pad;
193   demuxpad->rtcp_pad = rtcp_pad;
194
195   gst_pad_set_element_private (rtp_pad, demuxpad);
196   gst_pad_set_element_private (rtcp_pad, demuxpad);
197
198   demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
199
200   /* copy caps from input */
201   gst_pad_set_caps (rtp_pad, GST_PAD_CAPS (demux->rtp_sink));
202   gst_pad_use_fixed_caps (rtp_pad);
203   gst_pad_set_caps (rtcp_pad, GST_PAD_CAPS (demux->rtcp_sink));
204   gst_pad_use_fixed_caps (rtcp_pad);
205
206   gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
207   gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
208   gst_pad_set_iterate_internal_links_function (rtp_pad,
209       gst_rtp_ssrc_demux_iterate_internal_links_src);
210   gst_pad_set_active (rtp_pad, TRUE);
211
212   gst_pad_set_event_function (rtcp_pad, gst_rtp_ssrc_demux_src_event);
213   gst_pad_set_iterate_internal_links_function (rtcp_pad,
214       gst_rtp_ssrc_demux_iterate_internal_links_src);
215   gst_pad_set_active (rtcp_pad, TRUE);
216
217   GST_OBJECT_UNLOCK (demux);
218
219   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
220   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
221
222   g_signal_emit (G_OBJECT (demux),
223       gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
224
225   return demuxpad;
226 }
227
228 static void
229 gst_rtp_ssrc_demux_base_init (gpointer g_class)
230 {
231   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
232
233   gst_element_class_add_pad_template (gstelement_klass,
234       gst_static_pad_template_get (&rtp_ssrc_demux_sink_template));
235   gst_element_class_add_pad_template (gstelement_klass,
236       gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_sink_template));
237   gst_element_class_add_pad_template (gstelement_klass,
238       gst_static_pad_template_get (&rtp_ssrc_demux_src_template));
239   gst_element_class_add_pad_template (gstelement_klass,
240       gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_src_template));
241
242   gst_element_class_set_details_simple (gstelement_klass, "RTP SSRC Demux",
243       "Demux/Network/RTP",
244       "Splits RTP streams based on the SSRC",
245       "Wim Taymans <wim.taymans@gmail.com>");
246 }
247
248 static void
249 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
250 {
251   GObjectClass *gobject_klass;
252   GstElementClass *gstelement_klass;
253   GstRtpSsrcDemuxClass *gstrtpssrcdemux_klass;
254
255   gobject_klass = (GObjectClass *) klass;
256   gstelement_klass = (GstElementClass *) klass;
257   gstrtpssrcdemux_klass = (GstRtpSsrcDemuxClass *) klass;
258
259   gobject_klass->dispose = gst_rtp_ssrc_demux_dispose;
260   gobject_klass->finalize = gst_rtp_ssrc_demux_finalize;
261
262   /**
263    * GstRtpSsrcDemux::new-ssrc-pad:
264    * @demux: the object which received the signal
265    * @ssrc: the SSRC of the pad
266    * @pad: the new pad.
267    *
268    * Emited when a new SSRC pad has been created.
269    */
270   gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
271       g_signal_new ("new-ssrc-pad",
272       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
273       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
274       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
275       G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
276
277   /**
278    * GstRtpSsrcDemux::removed-ssrc-pad:
279    * @demux: the object which received the signal
280    * @ssrc: the SSRC of the pad
281    * @pad: the removed pad.
282    *
283    * Emited when a SSRC pad has been removed.
284    */
285   gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD] =
286       g_signal_new ("removed-ssrc-pad",
287       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
288       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, removed_ssrc_pad),
289       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
290       G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
291
292   /**
293    * GstRtpSsrcDemux::clear-ssrc:
294    * @demux: the object which received the signal
295    * @ssrc: the SSRC of the pad
296    *
297    * Action signal to remove the pad for SSRC.
298    */
299   gst_rtp_ssrc_demux_signals[SIGNAL_CLEAR_SSRC] =
300       g_signal_new ("clear-ssrc",
301       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
302       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, clear_ssrc),
303       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
304
305   gstelement_klass->change_state =
306       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
307   gstrtpssrcdemux_klass->clear_ssrc =
308       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_clear_ssrc);
309
310   GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
311       "rtpssrcdemux", 0, "RTP SSRC demuxer");
312 }
313
314 static void
315 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux,
316     GstRtpSsrcDemuxClass * g_class)
317 {
318   GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
319
320   demux->rtp_sink =
321       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
322           "sink"), "sink");
323   gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
324   gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
325   gst_pad_set_iterate_internal_links_function (demux->rtp_sink,
326       gst_rtp_ssrc_demux_iterate_internal_links_sink);
327   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
328
329   demux->rtcp_sink =
330       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
331           "rtcp_sink"), "rtcp_sink");
332   gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
333   gst_pad_set_event_function (demux->rtcp_sink,
334       gst_rtp_ssrc_demux_rtcp_sink_event);
335   gst_pad_set_iterate_internal_links_function (demux->rtcp_sink,
336       gst_rtp_ssrc_demux_iterate_internal_links_sink);
337   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
338
339   demux->padlock = g_mutex_new ();
340
341   gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
342 }
343
344 static void
345 gst_rtp_ssrc_demux_reset (GstRtpSsrcDemux * demux)
346 {
347   GSList *walk;
348
349   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
350     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
351
352     gst_pad_set_active (dpad->rtp_pad, FALSE);
353     gst_pad_set_active (dpad->rtcp_pad, FALSE);
354
355     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
356     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
357     g_free (dpad);
358   }
359   g_slist_free (demux->srcpads);
360   demux->srcpads = NULL;
361 }
362
363 static void
364 gst_rtp_ssrc_demux_dispose (GObject * object)
365 {
366   GstRtpSsrcDemux *demux;
367
368   demux = GST_RTP_SSRC_DEMUX (object);
369
370   gst_rtp_ssrc_demux_reset (demux);
371
372   G_OBJECT_CLASS (parent_class)->dispose (object);
373 }
374
375 static void
376 gst_rtp_ssrc_demux_finalize (GObject * object)
377 {
378   GstRtpSsrcDemux *demux;
379
380   demux = GST_RTP_SSRC_DEMUX (object);
381   g_mutex_free (demux->padlock);
382
383   G_OBJECT_CLASS (parent_class)->finalize (object);
384 }
385
386 static void
387 gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
388 {
389   GstRtpSsrcDemuxPad *dpad;
390
391   GST_PAD_LOCK (demux);
392   dpad = find_demux_pad_for_ssrc (demux, ssrc);
393   if (dpad == NULL) {
394     GST_PAD_UNLOCK (demux);
395     goto unknown_pad;
396   }
397
398   GST_DEBUG_OBJECT (demux, "clearing pad for SSRC %08x", ssrc);
399
400   demux->srcpads = g_slist_remove (demux->srcpads, dpad);
401   GST_PAD_UNLOCK (demux);
402
403   gst_pad_set_active (dpad->rtp_pad, FALSE);
404   gst_pad_set_active (dpad->rtcp_pad, FALSE);
405
406   g_signal_emit (G_OBJECT (demux),
407       gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD], 0, ssrc,
408       dpad->rtp_pad);
409
410   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
411   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
412
413   g_free (dpad);
414
415   return;
416
417   /* ERRORS */
418 unknown_pad:
419   {
420     GST_WARNING_OBJECT (demux, "unknown SSRC %08x", ssrc);
421     return;
422   }
423 }
424
425 static gboolean
426 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event)
427 {
428   GstRtpSsrcDemux *demux;
429   gboolean res = FALSE;
430
431   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
432   if (G_UNLIKELY (demux == NULL)) {
433     gst_event_unref (event);
434     return FALSE;
435   }
436
437   switch (GST_EVENT_TYPE (event)) {
438     case GST_EVENT_FLUSH_STOP:
439       gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
440     case GST_EVENT_NEWSEGMENT:
441     default:
442     {
443       GSList *walk;
444       GSList *pads = NULL;
445
446       res = TRUE;
447       /* need local snapshot of pads;
448        * should not push downstream while holding lock as that might deadlock
449        * with stuff traveling upstream tyring to get this lock while holding
450        * other (stream)lock */
451       GST_PAD_LOCK (demux);
452       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
453         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
454
455         pads = g_slist_prepend (pads, gst_object_ref (pad->rtp_pad));
456       }
457       GST_PAD_UNLOCK (demux);
458       for (walk = pads; walk; walk = g_slist_next (walk)) {
459         GstPad *pad = (GstPad *) walk->data;
460
461         gst_event_ref (event);
462         res &= gst_pad_push_event (pad, event);
463         gst_object_unref (pad);
464       }
465       g_slist_free (pads);
466       gst_event_unref (event);
467       break;
468     }
469   }
470
471   gst_object_unref (demux);
472   return res;
473 }
474
475 static gboolean
476 gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad, GstEvent * event)
477 {
478   GstRtpSsrcDemux *demux;
479   gboolean res = FALSE;
480
481   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
482
483   switch (GST_EVENT_TYPE (event)) {
484     case GST_EVENT_NEWSEGMENT:
485     default:
486     {
487       GSList *walk;
488       GSList *pads = NULL;
489
490       res = TRUE;
491       GST_PAD_LOCK (demux);
492       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
493         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
494
495         pads = g_slist_prepend (pads, gst_object_ref (pad->rtcp_pad));
496       }
497       GST_PAD_UNLOCK (demux);
498       for (walk = pads; walk; walk = g_slist_next (walk)) {
499         GstPad *pad = (GstPad *) walk->data;
500
501         gst_event_ref (event);
502         res &= gst_pad_push_event (pad, event);
503         gst_object_unref (pad);
504       }
505       g_slist_free (pads);
506       gst_event_unref (event);
507       break;
508     }
509   }
510   gst_object_unref (demux);
511   return res;
512 }
513
514 static GstFlowReturn
515 gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf)
516 {
517   GstFlowReturn ret;
518   GstRtpSsrcDemux *demux;
519   guint32 ssrc;
520   GstRtpSsrcDemuxPad *dpad;
521
522   demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
523
524   if (!gst_rtp_buffer_validate (buf))
525     goto invalid_payload;
526
527   ssrc = gst_rtp_buffer_get_ssrc (buf);
528
529   GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
530
531   dpad = find_or_create_demux_pad_for_ssrc (demux, ssrc);
532   if (dpad == NULL)
533     goto create_failed;
534
535   /* push to srcpad */
536   ret = gst_pad_push (dpad->rtp_pad, buf);
537
538   return ret;
539
540   /* ERRORS */
541 invalid_payload:
542   {
543     /* this is fatal and should be filtered earlier */
544     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
545         ("Dropping invalid RTP payload"));
546     gst_buffer_unref (buf);
547     return GST_FLOW_ERROR;
548   }
549 create_failed:
550   {
551     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
552         ("Could not create new pad"));
553     gst_buffer_unref (buf);
554     return GST_FLOW_ERROR;
555   }
556 }
557
558 static GstFlowReturn
559 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstBuffer * buf)
560 {
561   GstFlowReturn ret;
562   GstRtpSsrcDemux *demux;
563   guint32 ssrc;
564   GstRtpSsrcDemuxPad *dpad;
565   GstRTCPPacket packet;
566
567   demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
568
569   if (!gst_rtcp_buffer_validate (buf))
570     goto invalid_rtcp;
571
572   if (!gst_rtcp_buffer_get_first_packet (buf, &packet))
573     goto invalid_rtcp;
574
575   /* first packet must be SR or RR or else the validate would have failed */
576   switch (gst_rtcp_packet_get_type (&packet)) {
577     case GST_RTCP_TYPE_SR:
578       /* get the ssrc so that we can route it to the right source pad */
579       gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
580           NULL);
581       break;
582     default:
583       goto unexpected_rtcp;
584   }
585
586   GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
587
588   dpad = find_or_create_demux_pad_for_ssrc (demux, ssrc);
589   if (dpad == NULL)
590     goto create_failed;
591
592
593   /* push to srcpad */
594   ret = gst_pad_push (dpad->rtcp_pad, buf);
595
596   return ret;
597
598   /* ERRORS */
599 invalid_rtcp:
600   {
601     /* this is fatal and should be filtered earlier */
602     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
603         ("Dropping invalid RTCP packet"));
604     gst_buffer_unref (buf);
605     return GST_FLOW_ERROR;
606   }
607 unexpected_rtcp:
608   {
609     GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
610     gst_buffer_unref (buf);
611     return GST_FLOW_OK;
612   }
613 create_failed:
614   {
615     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
616         ("Could not create new pad"));
617     gst_buffer_unref (buf);
618     return GST_FLOW_ERROR;
619   }
620 }
621
622 static gboolean
623 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event)
624 {
625   GstRtpSsrcDemux *demux;
626   const GstStructure *s;
627
628   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
629
630   switch (GST_EVENT_TYPE (event)) {
631     case GST_EVENT_CUSTOM_UPSTREAM:
632     case GST_EVENT_CUSTOM_BOTH:
633     case GST_EVENT_CUSTOM_BOTH_OOB:
634       s = gst_event_get_structure (event);
635       if (s && !gst_structure_has_field (s, "ssrc")) {
636         GSList *walk;
637
638         for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
639           GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
640
641           if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
642             event =
643                 GST_EVENT_CAST (gst_mini_object_make_writable
644                 (GST_MINI_OBJECT_CAST (event)));
645             gst_structure_set (event->structure, "ssrc", G_TYPE_UINT,
646                 dpad->ssrc, NULL);
647             break;
648           }
649         }
650       }
651       break;
652     default:
653       break;
654   }
655
656   gst_object_unref (demux);
657
658   return gst_pad_event_default (pad, event);
659 }
660
661 static GstIterator *
662 gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad)
663 {
664   GstRtpSsrcDemux *demux;
665   GstPad *otherpad = NULL;
666   GstIterator *it = NULL;
667   GSList *current;
668
669   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
670
671   if (!demux)
672     return NULL;
673
674   GST_PAD_LOCK (demux);
675   for (current = demux->srcpads; current; current = g_slist_next (current)) {
676     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
677
678     if (pad == dpad->rtp_pad) {
679       otherpad = demux->rtp_sink;
680       break;
681     } else if (pad == dpad->rtcp_pad) {
682       otherpad = demux->rtcp_sink;
683       break;
684     }
685   }
686   it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
687       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
688   GST_PAD_UNLOCK (demux);
689
690   gst_object_unref (demux);
691   return it;
692 }
693
694 /* Should return 0 for elements to be included */
695 static gint
696 src_pad_compare_func (gconstpointer a, gconstpointer b)
697 {
698   GstPad *pad = GST_PAD (a);
699   const gchar *prefix = b;
700   gint res = 1;
701
702   GST_OBJECT_LOCK (pad);
703   res = !GST_PAD_NAME (pad) || g_str_has_prefix (GST_PAD_NAME (pad), prefix);
704   GST_OBJECT_UNLOCK (pad);
705
706   return res;
707 }
708
709 static GstIterator *
710 gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad * pad)
711 {
712   GstRtpSsrcDemux *demux;
713   GstIterator *it = NULL;
714   const gchar *prefix = NULL;
715
716   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
717
718   if (!demux)
719     return NULL;
720
721   if (pad == demux->rtp_sink)
722     prefix = "src_";
723   else if (pad == demux->rtcp_sink)
724     prefix = "rtcp_src_";
725   else
726     g_assert_not_reached ();
727
728   it = gst_element_iterate_src_pads (GST_ELEMENT (demux));
729
730   return gst_iterator_filter (it, src_pad_compare_func, (gpointer) prefix);
731 }
732
733
734 static gboolean
735 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query)
736 {
737   GstRtpSsrcDemux *demux;
738   gboolean res = FALSE;
739
740   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
741   if (G_UNLIKELY (demux == NULL))
742     return FALSE;
743
744   switch (GST_QUERY_TYPE (query)) {
745     case GST_QUERY_LATENCY:
746     {
747
748       if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
749         gboolean live;
750         GstClockTime min_latency, max_latency;
751         GstRtpSsrcDemuxPad *demuxpad;
752
753         demuxpad = gst_pad_get_element_private (pad);
754
755         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
756
757         GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
758             GST_TIME_ARGS (min_latency));
759
760         GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
761
762         gst_query_set_latency (query, live, min_latency, max_latency);
763       }
764       break;
765     }
766     default:
767       res = gst_pad_query_default (pad, query);
768       break;
769   }
770   gst_object_unref (demux);
771
772   return res;
773 }
774
775 static GstStateChangeReturn
776 gst_rtp_ssrc_demux_change_state (GstElement * element,
777     GstStateChange transition)
778 {
779   GstStateChangeReturn ret;
780   GstRtpSsrcDemux *demux;
781
782   demux = GST_RTP_SSRC_DEMUX (element);
783
784   switch (transition) {
785     case GST_STATE_CHANGE_NULL_TO_READY:
786     case GST_STATE_CHANGE_READY_TO_PAUSED:
787     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
788     default:
789       break;
790   }
791
792   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
793
794   switch (transition) {
795     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
796       break;
797     case GST_STATE_CHANGE_PAUSED_TO_READY:
798       gst_rtp_ssrc_demux_reset (demux);
799       break;
800     case GST_STATE_CHANGE_READY_TO_NULL:
801     default:
802       break;
803   }
804   return ret;
805 }