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