rtpmanager: Initialize GstRTPBuffer before usage
[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_%u",
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_%u",
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, GstObject * parent,
114     GstBuffer * buf);
115 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
116     GstEvent * event);
117
118 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
119     GstObject * parent, GstBuffer * buf);
120 static gboolean gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad,
121     GstObject * parent, GstEvent * event);
122 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad *
123     pad, GstObject * parent);
124
125 /* srcpad stuff */
126 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
127     GstEvent * event);
128 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad,
129     GstObject * parent);
130 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
131     GstQuery * query);
132
133 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
134
135 /*
136  * Item for storing GstPad <-> SSRC pairs.
137  */
138 struct _GstRtpSsrcDemuxPad
139 {
140   guint32 ssrc;
141   GstPad *rtp_pad;
142   GstCaps *caps;
143   GstPad *rtcp_pad;
144 };
145
146 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
147  */
148 static GstRtpSsrcDemuxPad *
149 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
150 {
151   GSList *walk;
152
153   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
154     GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
155
156     if (pad->ssrc == ssrc)
157       return pad;
158   }
159   return NULL;
160 }
161
162 /* with PAD_LOCK */
163 static GstRtpSsrcDemuxPad *
164 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
165 {
166   GstPad *rtp_pad, *rtcp_pad;
167   GstElementClass *klass;
168   GstPadTemplate *templ;
169   gchar *padname;
170   GstRtpSsrcDemuxPad *demuxpad;
171   GstCaps *caps;
172
173   GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
174
175   demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
176   if (demuxpad != NULL) {
177     return demuxpad;
178   }
179
180   klass = GST_ELEMENT_GET_CLASS (demux);
181   templ = gst_element_class_get_pad_template (klass, "src_%u");
182   padname = g_strdup_printf ("src_%u", ssrc);
183   rtp_pad = gst_pad_new_from_template (templ, padname);
184   g_free (padname);
185
186   templ = gst_element_class_get_pad_template (klass, "rtcp_src_%u");
187   padname = g_strdup_printf ("rtcp_src_%u", ssrc);
188   rtcp_pad = gst_pad_new_from_template (templ, padname);
189   g_free (padname);
190
191   /* wrap in structure and add to list */
192   demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
193   demuxpad->ssrc = ssrc;
194   demuxpad->rtp_pad = rtp_pad;
195   demuxpad->rtcp_pad = rtcp_pad;
196
197   gst_pad_set_element_private (rtp_pad, demuxpad);
198   gst_pad_set_element_private (rtcp_pad, demuxpad);
199
200   demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
201
202   /* copy caps from input */
203   caps = gst_pad_get_current_caps (demux->rtp_sink);
204   gst_pad_set_caps (rtp_pad, caps);
205   gst_caps_unref (caps);
206   gst_pad_use_fixed_caps (rtp_pad);
207   caps = gst_pad_get_current_caps (demux->rtcp_sink);
208   gst_pad_set_caps (rtcp_pad, caps);
209   gst_caps_unref (caps);
210   gst_pad_use_fixed_caps (rtcp_pad);
211
212   gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
213   gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
214   gst_pad_set_iterate_internal_links_function (rtp_pad,
215       gst_rtp_ssrc_demux_iterate_internal_links_src);
216   gst_pad_set_active (rtp_pad, TRUE);
217
218   gst_pad_set_event_function (rtcp_pad, gst_rtp_ssrc_demux_src_event);
219   gst_pad_set_iterate_internal_links_function (rtcp_pad,
220       gst_rtp_ssrc_demux_iterate_internal_links_src);
221   gst_pad_set_active (rtcp_pad, TRUE);
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   g_static_rec_mutex_init (&demux->padlock);
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_static_rec_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, GstObject * parent,
424     GstEvent * event)
425 {
426   GstRtpSsrcDemux *demux;
427   gboolean res = FALSE;
428
429   demux = GST_RTP_SSRC_DEMUX (parent);
430
431   switch (GST_EVENT_TYPE (event)) {
432     case GST_EVENT_FLUSH_STOP:
433       gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
434     default:
435     {
436       GSList *walk;
437       GSList *pads = NULL;
438
439       res = TRUE;
440       /* need local snapshot of pads;
441        * should not push downstream while holding lock as that might deadlock
442        * with stuff traveling upstream tyring to get this lock while holding
443        * other (stream)lock */
444       GST_PAD_LOCK (demux);
445       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
446         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
447
448         pads = g_slist_prepend (pads, gst_object_ref (pad->rtp_pad));
449       }
450       GST_PAD_UNLOCK (demux);
451       for (walk = pads; walk; walk = g_slist_next (walk)) {
452         GstPad *pad = (GstPad *) walk->data;
453
454         gst_event_ref (event);
455         res &= gst_pad_push_event (pad, event);
456         gst_object_unref (pad);
457       }
458       g_slist_free (pads);
459       gst_event_unref (event);
460       break;
461     }
462   }
463
464   return res;
465 }
466
467 static gboolean
468 gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad, GstObject * parent,
469     GstEvent * event)
470 {
471   GstRtpSsrcDemux *demux;
472   gboolean res = FALSE;
473
474   demux = GST_RTP_SSRC_DEMUX (parent);
475
476   switch (GST_EVENT_TYPE (event)) {
477     default:
478     {
479       GSList *walk;
480       GSList *pads = NULL;
481
482       res = TRUE;
483       GST_PAD_LOCK (demux);
484       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
485         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
486
487         pads = g_slist_prepend (pads, gst_object_ref (pad->rtcp_pad));
488       }
489       GST_PAD_UNLOCK (demux);
490       for (walk = pads; walk; walk = g_slist_next (walk)) {
491         GstPad *pad = (GstPad *) walk->data;
492
493         gst_event_ref (event);
494         res &= gst_pad_push_event (pad, event);
495         gst_object_unref (pad);
496       }
497       g_slist_free (pads);
498       gst_event_unref (event);
499       break;
500     }
501   }
502   return res;
503 }
504
505 static GstFlowReturn
506 gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
507 {
508   GstFlowReturn ret;
509   GstRtpSsrcDemux *demux;
510   guint32 ssrc;
511   GstRtpSsrcDemuxPad *dpad;
512   GstRTPBuffer rtp = {NULL};
513   GstPad *srcpad;
514
515   demux = GST_RTP_SSRC_DEMUX (parent);
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, GstObject * parent,
562     GstBuffer * buf)
563 {
564   GstFlowReturn ret;
565   GstRtpSsrcDemux *demux;
566   guint32 ssrc;
567   GstRtpSsrcDemuxPad *dpad;
568   GstRTCPPacket packet;
569   GstRTCPBuffer rtcp;
570   GstPad *srcpad;
571
572   demux = GST_RTP_SSRC_DEMUX (parent);
573
574   if (!gst_rtcp_buffer_validate (buf))
575     goto invalid_rtcp;
576
577   gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcp);
578   if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
579     gst_rtcp_buffer_unmap (&rtcp);
580     goto invalid_rtcp;
581   }
582
583   /* first packet must be SR or RR or else the validate would have failed */
584   switch (gst_rtcp_packet_get_type (&packet)) {
585     case GST_RTCP_TYPE_SR:
586       /* get the ssrc so that we can route it to the right source pad */
587       gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
588           NULL);
589       break;
590     default:
591       goto unexpected_rtcp;
592   }
593   gst_rtcp_buffer_unmap (&rtcp);
594
595   GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
596
597   GST_PAD_LOCK (demux);
598   dpad = find_or_create_demux_pad_for_ssrc (demux, ssrc);
599   if (dpad == NULL) {
600     GST_PAD_UNLOCK (demux);
601     goto create_failed;
602   }
603   srcpad = gst_object_ref (dpad->rtcp_pad);
604   GST_PAD_UNLOCK (demux);
605
606   /* push to srcpad */
607   ret = gst_pad_push (srcpad, buf);
608
609   gst_object_unref (srcpad);
610
611   return ret;
612
613   /* ERRORS */
614 invalid_rtcp:
615   {
616     /* this is fatal and should be filtered earlier */
617     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
618         ("Dropping invalid RTCP packet"));
619     gst_buffer_unref (buf);
620     return GST_FLOW_ERROR;
621   }
622 unexpected_rtcp:
623   {
624     GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
625     gst_buffer_unref (buf);
626     return GST_FLOW_OK;
627   }
628 create_failed:
629   {
630     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
631         ("Could not create new pad"));
632     gst_buffer_unref (buf);
633     return GST_FLOW_ERROR;
634   }
635 }
636
637 static gboolean
638 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
639     GstEvent * event)
640 {
641   GstRtpSsrcDemux *demux;
642   const GstStructure *s;
643
644   demux = GST_RTP_SSRC_DEMUX (parent);
645
646   switch (GST_EVENT_TYPE (event)) {
647     case GST_EVENT_CUSTOM_UPSTREAM:
648     case GST_EVENT_CUSTOM_BOTH:
649     case GST_EVENT_CUSTOM_BOTH_OOB:
650       s = gst_event_get_structure (event);
651       if (s && !gst_structure_has_field (s, "ssrc")) {
652         GSList *walk;
653
654         for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
655           GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
656
657           if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
658             GstStructure *ws;
659
660             event =
661                 GST_EVENT_CAST (gst_mini_object_make_writable
662                 (GST_MINI_OBJECT_CAST (event)));
663             ws = gst_event_writable_structure (event);
664             gst_structure_set (ws, "ssrc", G_TYPE_UINT, dpad->ssrc, NULL);
665             break;
666           }
667         }
668       }
669       break;
670     default:
671       break;
672   }
673
674   return gst_pad_event_default (pad, parent, event);
675 }
676
677 static GstIterator *
678 gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad, GstObject * parent)
679 {
680   GstRtpSsrcDemux *demux;
681   GstPad *otherpad = NULL;
682   GstIterator *it = NULL;
683   GSList *current;
684
685   demux = GST_RTP_SSRC_DEMUX (parent);
686
687   GST_PAD_LOCK (demux);
688   for (current = demux->srcpads; current; current = g_slist_next (current)) {
689     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
690
691     if (pad == dpad->rtp_pad) {
692       otherpad = demux->rtp_sink;
693       break;
694     } else if (pad == dpad->rtcp_pad) {
695       otherpad = demux->rtcp_sink;
696       break;
697     }
698   }
699   if (otherpad) {
700     GValue val = { 0, };
701
702     g_value_init (&val, GST_TYPE_PAD);
703     g_value_set_object (&val, otherpad);
704     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
705     g_value_unset (&val);
706     gst_object_unref (otherpad);
707   }
708   GST_PAD_UNLOCK (demux);
709
710   return it;
711 }
712
713 /* Should return 0 for elements to be included */
714 static gint
715 src_pad_compare_func (gconstpointer a, gconstpointer b)
716 {
717   GstPad *pad = GST_PAD (a);
718   const gchar *prefix = b;
719   gint res = 1;
720
721   GST_OBJECT_LOCK (pad);
722   res = !GST_PAD_NAME (pad) || g_str_has_prefix (GST_PAD_NAME (pad), prefix);
723   GST_OBJECT_UNLOCK (pad);
724
725   return res;
726 }
727
728 static GstIterator *
729 gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad * pad,
730     GstObject * parent)
731 {
732   GstRtpSsrcDemux *demux;
733   GstIterator *it = NULL;
734   GValue gval = { 0, };
735
736   demux = GST_RTP_SSRC_DEMUX (parent);
737
738   g_value_init (&gval, G_TYPE_STRING);
739   if (pad == demux->rtp_sink)
740     g_value_set_static_string (&gval, "src_");
741   else if (pad == demux->rtcp_sink)
742     g_value_set_static_string (&gval, "rtcp_src_");
743   else
744     g_assert_not_reached ();
745
746   it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
747
748   it = gst_iterator_filter (it, src_pad_compare_func, &gval);
749
750   return it;
751 }
752
753
754 static gboolean
755 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
756     GstQuery * query)
757 {
758   GstRtpSsrcDemux *demux;
759   gboolean res = FALSE;
760
761   demux = GST_RTP_SSRC_DEMUX (parent);
762
763   switch (GST_QUERY_TYPE (query)) {
764     case GST_QUERY_LATENCY:
765     {
766
767       if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
768         gboolean live;
769         GstClockTime min_latency, max_latency;
770         GstRtpSsrcDemuxPad *demuxpad;
771
772         demuxpad = gst_pad_get_element_private (pad);
773
774         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
775
776         GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
777             GST_TIME_ARGS (min_latency));
778
779         GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
780
781         gst_query_set_latency (query, live, min_latency, max_latency);
782       }
783       break;
784     }
785     default:
786       res = gst_pad_query_default (pad, parent, query);
787       break;
788   }
789
790   return res;
791 }
792
793 static GstStateChangeReturn
794 gst_rtp_ssrc_demux_change_state (GstElement * element,
795     GstStateChange transition)
796 {
797   GstStateChangeReturn ret;
798   GstRtpSsrcDemux *demux;
799
800   demux = GST_RTP_SSRC_DEMUX (element);
801
802   switch (transition) {
803     case GST_STATE_CHANGE_NULL_TO_READY:
804     case GST_STATE_CHANGE_READY_TO_PAUSED:
805     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
806     default:
807       break;
808   }
809
810   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
811
812   switch (transition) {
813     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
814       break;
815     case GST_STATE_CHANGE_PAUSED_TO_READY:
816       gst_rtp_ssrc_demux_reset (demux);
817       break;
818     case GST_STATE_CHANGE_READY_TO_NULL:
819     default:
820       break;
821   }
822   return ret;
823 }