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