Moving twolame mp2 encoder plugin from -ugly
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:element-rtpssrcdemux
24  *
25  * rtpssrcdemux 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-1.0 udpsrc caps="application/x-rtp" ! rtpssrcdemux ! 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
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46 #include <gst/rtp/gstrtpbuffer.h>
47 #include <gst/rtp/gstrtcpbuffer.h>
48
49 #include "gstrtpssrcdemux.h"
50
51 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
52 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
53
54 /* generic templates */
55 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp")
60     );
61
62 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-rtcp")
67     );
68
69 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src_%u",
71     GST_PAD_SRC,
72     GST_PAD_SOMETIMES,
73     GST_STATIC_CAPS ("application/x-rtp")
74     );
75
76 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
77 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%u",
78     GST_PAD_SRC,
79     GST_PAD_SOMETIMES,
80     GST_STATIC_CAPS ("application/x-rtcp")
81     );
82
83 #define GST_PAD_LOCK(obj)   (g_rec_mutex_lock (&(obj)->padlock))
84 #define GST_PAD_UNLOCK(obj) (g_rec_mutex_unlock (&(obj)->padlock))
85
86 typedef enum
87 {
88   RTP_PAD,
89   RTCP_PAD
90 } PadType;
91
92 /* signals */
93 enum
94 {
95   SIGNAL_NEW_SSRC_PAD,
96   SIGNAL_REMOVED_SSRC_PAD,
97   SIGNAL_CLEAR_SSRC,
98   LAST_SIGNAL
99 };
100
101 #define gst_rtp_ssrc_demux_parent_class parent_class
102 G_DEFINE_TYPE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GST_TYPE_ELEMENT);
103
104 /* GObject vmethods */
105 static void gst_rtp_ssrc_demux_dispose (GObject * object);
106 static void gst_rtp_ssrc_demux_finalize (GObject * object);
107
108 /* GstElement vmethods */
109 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
110     element, GstStateChange transition);
111
112 static void gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux,
113     guint32 ssrc);
114
115 /* sinkpad stuff */
116 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent,
117     GstBuffer * buf);
118 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
119     GstEvent * event);
120
121 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
122     GstObject * parent, GstBuffer * buf);
123 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad *
124     pad, GstObject * parent);
125
126 /* srcpad stuff */
127 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
128     GstEvent * event);
129 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad,
130     GstObject * parent);
131 static gboolean gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
132     GstQuery * query);
133
134 static guint gst_rtp_ssrc_demux_signals[LAST_SIGNAL] = { 0 };
135
136 /*
137  * Item for storing GstPad <-> SSRC pairs.
138  */
139 struct _GstRtpSsrcDemuxPad
140 {
141   guint32 ssrc;
142   GstPad *rtp_pad;
143   GstCaps *caps;
144   GstPad *rtcp_pad;
145
146   gboolean pushed_initial_rtp_events;
147   gboolean pushed_initial_rtcp_events;
148 };
149
150 /* find a src pad for a given SSRC, returns NULL if the SSRC was not found
151  */
152 static GstRtpSsrcDemuxPad *
153 find_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
154 {
155   GSList *walk;
156
157   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
158     GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
159
160     if (pad->ssrc == ssrc)
161       return pad;
162   }
163   return NULL;
164 }
165
166 static GstEvent *
167 add_ssrc_and_ref (GstEvent * event, guint32 ssrc)
168 {
169   /* Set the ssrc on the output caps */
170   switch (GST_EVENT_TYPE (event)) {
171     case GST_EVENT_CAPS:
172     {
173       GstCaps *caps;
174       GstCaps *newcaps;
175       GstStructure *s;
176
177       gst_event_parse_caps (event, &caps);
178       newcaps = gst_caps_copy (caps);
179
180       s = gst_caps_get_structure (newcaps, 0);
181       gst_structure_set (s, "ssrc", G_TYPE_UINT, ssrc, NULL);
182       event = gst_event_new_caps (newcaps);
183       gst_caps_unref (newcaps);
184       break;
185     }
186     default:
187       gst_event_ref (event);
188       break;
189   }
190
191   return event;
192 }
193
194 struct ForwardStickyEventData
195 {
196   GstPad *pad;
197   guint32 ssrc;
198 };
199
200 static gboolean
201 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
202 {
203   struct ForwardStickyEventData *data = user_data;
204   GstEvent *newevent;
205
206   newevent = add_ssrc_and_ref (*event, data->ssrc);
207
208   gst_pad_push_event (data->pad, newevent);
209
210   return TRUE;
211 }
212
213 static void
214 forward_initial_events (GstRtpSsrcDemux * demux, guint32 ssrc, GstPad * pad,
215     PadType padtype)
216 {
217   struct ForwardStickyEventData fdata;
218   GstPad *sinkpad = NULL;
219
220   if (padtype == RTP_PAD)
221     sinkpad = demux->rtp_sink;
222   else if (padtype == RTCP_PAD)
223     sinkpad = demux->rtcp_sink;
224   else
225     g_assert_not_reached ();
226
227   fdata.ssrc = ssrc;
228   fdata.pad = pad;
229
230   gst_pad_sticky_events_foreach (sinkpad, forward_sticky_events, &fdata);
231 }
232
233 static GstPad *
234 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc,
235     PadType padtype)
236 {
237   GstPad *rtp_pad, *rtcp_pad;
238   GstElementClass *klass;
239   GstPadTemplate *templ;
240   gchar *padname;
241   GstRtpSsrcDemuxPad *demuxpad;
242   GstPad *retpad;
243   gulong rtp_block, rtcp_block;
244
245   GST_PAD_LOCK (demux);
246
247   demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
248   if (demuxpad != NULL) {
249     gboolean forward = FALSE;
250
251     switch (padtype) {
252       case RTP_PAD:
253         retpad = gst_object_ref (demuxpad->rtp_pad);
254         if (!demuxpad->pushed_initial_rtp_events) {
255           forward = TRUE;
256           demuxpad->pushed_initial_rtp_events = TRUE;
257         }
258         break;
259       case RTCP_PAD:
260         retpad = gst_object_ref (demuxpad->rtcp_pad);
261         if (!demuxpad->pushed_initial_rtcp_events) {
262           forward = TRUE;
263           demuxpad->pushed_initial_rtcp_events = TRUE;
264         }
265         break;
266       default:
267         retpad = NULL;
268         g_assert_not_reached ();
269     }
270
271     GST_PAD_UNLOCK (demux);
272
273     if (forward)
274       forward_initial_events (demux, ssrc, retpad, padtype);
275     return retpad;
276   }
277
278   GST_DEBUG_OBJECT (demux, "creating new pad for SSRC %08x", ssrc);
279
280   klass = GST_ELEMENT_GET_CLASS (demux);
281   templ = gst_element_class_get_pad_template (klass, "src_%u");
282   padname = g_strdup_printf ("src_%u", ssrc);
283   rtp_pad = gst_pad_new_from_template (templ, padname);
284   g_free (padname);
285
286   templ = gst_element_class_get_pad_template (klass, "rtcp_src_%u");
287   padname = g_strdup_printf ("rtcp_src_%u", ssrc);
288   rtcp_pad = gst_pad_new_from_template (templ, padname);
289   g_free (padname);
290
291   /* wrap in structure and add to list */
292   demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
293   demuxpad->ssrc = ssrc;
294   demuxpad->rtp_pad = rtp_pad;
295   demuxpad->rtcp_pad = rtcp_pad;
296
297   gst_pad_set_element_private (rtp_pad, demuxpad);
298   gst_pad_set_element_private (rtcp_pad, demuxpad);
299
300   demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
301
302   gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
303   gst_pad_set_iterate_internal_links_function (rtp_pad,
304       gst_rtp_ssrc_demux_iterate_internal_links_src);
305   gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
306   gst_pad_use_fixed_caps (rtp_pad);
307   gst_pad_set_active (rtp_pad, TRUE);
308
309   gst_pad_set_event_function (rtcp_pad, gst_rtp_ssrc_demux_src_event);
310   gst_pad_set_iterate_internal_links_function (rtcp_pad,
311       gst_rtp_ssrc_demux_iterate_internal_links_src);
312   gst_pad_use_fixed_caps (rtcp_pad);
313   gst_pad_set_active (rtcp_pad, TRUE);
314
315   if (padtype == RTP_PAD) {
316     demuxpad->pushed_initial_rtp_events = TRUE;
317     forward_initial_events (demux, ssrc, rtp_pad, padtype);
318   } else if (padtype == RTCP_PAD) {
319     demuxpad->pushed_initial_rtcp_events = TRUE;
320     forward_initial_events (demux, ssrc, rtcp_pad, padtype);
321   } else {
322     g_assert_not_reached ();
323   }
324
325   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
326   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
327
328   switch (padtype) {
329     case RTP_PAD:
330       retpad = gst_object_ref (demuxpad->rtp_pad);
331       break;
332     case RTCP_PAD:
333       retpad = gst_object_ref (demuxpad->rtcp_pad);
334       break;
335     default:
336       retpad = NULL;
337       g_assert_not_reached ();
338   }
339
340   gst_object_ref (rtp_pad);
341   gst_object_ref (rtcp_pad);
342
343   rtp_block = gst_pad_add_probe (rtp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
344       NULL, NULL, NULL);
345   rtcp_block = gst_pad_add_probe (rtcp_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
346       NULL, NULL, NULL);
347
348   GST_PAD_UNLOCK (demux);
349
350   g_signal_emit (G_OBJECT (demux),
351       gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
352
353   gst_pad_remove_probe (rtp_pad, rtp_block);
354   gst_pad_remove_probe (rtcp_pad, rtcp_block);
355
356   gst_object_unref (rtp_pad);
357   gst_object_unref (rtcp_pad);
358
359   return retpad;
360 }
361
362 static void
363 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
364 {
365   GObjectClass *gobject_klass;
366   GstElementClass *gstelement_klass;
367   GstRtpSsrcDemuxClass *gstrtpssrcdemux_klass;
368
369   gobject_klass = (GObjectClass *) klass;
370   gstelement_klass = (GstElementClass *) klass;
371   gstrtpssrcdemux_klass = (GstRtpSsrcDemuxClass *) klass;
372
373   gobject_klass->dispose = gst_rtp_ssrc_demux_dispose;
374   gobject_klass->finalize = gst_rtp_ssrc_demux_finalize;
375
376   /**
377    * GstRtpSsrcDemux::new-ssrc-pad:
378    * @demux: the object which received the signal
379    * @ssrc: the SSRC of the pad
380    * @pad: the new pad.
381    *
382    * Emited when a new SSRC pad has been created.
383    */
384   gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
385       g_signal_new ("new-ssrc-pad",
386       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
387       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
388       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
389       GST_TYPE_PAD);
390
391   /**
392    * GstRtpSsrcDemux::removed-ssrc-pad:
393    * @demux: the object which received the signal
394    * @ssrc: the SSRC of the pad
395    * @pad: the removed pad.
396    *
397    * Emited when a SSRC pad has been removed.
398    */
399   gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD] =
400       g_signal_new ("removed-ssrc-pad",
401       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
402       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, removed_ssrc_pad),
403       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
404       GST_TYPE_PAD);
405
406   /**
407    * GstRtpSsrcDemux::clear-ssrc:
408    * @demux: the object which received the signal
409    * @ssrc: the SSRC of the pad
410    *
411    * Action signal to remove the pad for SSRC.
412    */
413   gst_rtp_ssrc_demux_signals[SIGNAL_CLEAR_SSRC] =
414       g_signal_new ("clear-ssrc",
415       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
416       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, clear_ssrc),
417       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT);
418
419   gstelement_klass->change_state =
420       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
421   gstrtpssrcdemux_klass->clear_ssrc =
422       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_clear_ssrc);
423
424   gst_element_class_add_static_pad_template (gstelement_klass,
425       &rtp_ssrc_demux_sink_template);
426   gst_element_class_add_static_pad_template (gstelement_klass,
427       &rtp_ssrc_demux_rtcp_sink_template);
428   gst_element_class_add_static_pad_template (gstelement_klass,
429       &rtp_ssrc_demux_src_template);
430   gst_element_class_add_static_pad_template (gstelement_klass,
431       &rtp_ssrc_demux_rtcp_src_template);
432
433   gst_element_class_set_static_metadata (gstelement_klass, "RTP SSRC Demux",
434       "Demux/Network/RTP",
435       "Splits RTP streams based on the SSRC",
436       "Wim Taymans <wim.taymans@gmail.com>");
437
438   GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
439       "rtpssrcdemux", 0, "RTP SSRC demuxer");
440 }
441
442 static void
443 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux)
444 {
445   GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
446
447   demux->rtp_sink =
448       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
449           "sink"), "sink");
450   gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
451   gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
452   gst_pad_set_iterate_internal_links_function (demux->rtp_sink,
453       gst_rtp_ssrc_demux_iterate_internal_links_sink);
454   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
455
456   demux->rtcp_sink =
457       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
458           "rtcp_sink"), "rtcp_sink");
459   gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
460   gst_pad_set_event_function (demux->rtcp_sink, gst_rtp_ssrc_demux_sink_event);
461   gst_pad_set_iterate_internal_links_function (demux->rtcp_sink,
462       gst_rtp_ssrc_demux_iterate_internal_links_sink);
463   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
464
465   g_rec_mutex_init (&demux->padlock);
466 }
467
468 static void
469 gst_rtp_ssrc_demux_reset (GstRtpSsrcDemux * demux)
470 {
471   GSList *walk;
472
473   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
474     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
475
476     gst_pad_set_active (dpad->rtp_pad, FALSE);
477     gst_pad_set_active (dpad->rtcp_pad, FALSE);
478
479     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
480     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
481     g_free (dpad);
482   }
483   g_slist_free (demux->srcpads);
484   demux->srcpads = NULL;
485 }
486
487 static void
488 gst_rtp_ssrc_demux_dispose (GObject * object)
489 {
490   GstRtpSsrcDemux *demux;
491
492   demux = GST_RTP_SSRC_DEMUX (object);
493
494   gst_rtp_ssrc_demux_reset (demux);
495
496   G_OBJECT_CLASS (parent_class)->dispose (object);
497 }
498
499 static void
500 gst_rtp_ssrc_demux_finalize (GObject * object)
501 {
502   GstRtpSsrcDemux *demux;
503
504   demux = GST_RTP_SSRC_DEMUX (object);
505   g_rec_mutex_clear (&demux->padlock);
506
507   G_OBJECT_CLASS (parent_class)->finalize (object);
508 }
509
510 static void
511 gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
512 {
513   GstRtpSsrcDemuxPad *dpad;
514
515   GST_PAD_LOCK (demux);
516   dpad = find_demux_pad_for_ssrc (demux, ssrc);
517   if (dpad == NULL) {
518     GST_PAD_UNLOCK (demux);
519     goto unknown_pad;
520   }
521
522   GST_DEBUG_OBJECT (demux, "clearing pad for SSRC %08x", ssrc);
523
524   demux->srcpads = g_slist_remove (demux->srcpads, dpad);
525   GST_PAD_UNLOCK (demux);
526
527   gst_pad_set_active (dpad->rtp_pad, FALSE);
528   gst_pad_set_active (dpad->rtcp_pad, FALSE);
529
530   g_signal_emit (G_OBJECT (demux),
531       gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD], 0, ssrc,
532       dpad->rtp_pad);
533
534   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
535   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
536
537   g_free (dpad);
538
539   return;
540
541   /* ERRORS */
542 unknown_pad:
543   {
544     GST_WARNING_OBJECT (demux, "unknown SSRC %08x", ssrc);
545     return;
546   }
547 }
548
549 struct ForwardEventData
550 {
551   GstRtpSsrcDemux *demux;
552   GstEvent *event;
553   gboolean res;
554   GstPad *pad;
555 };
556
557 static gboolean
558 forward_event (GstPad * pad, gpointer user_data)
559 {
560   struct ForwardEventData *fdata = user_data;
561   GSList *walk = NULL;
562   GstEvent *newevent = NULL;
563
564   GST_PAD_LOCK (fdata->demux);
565   for (walk = fdata->demux->srcpads; walk; walk = walk->next) {
566     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
567
568     /* Only forward the event if the initial events have been through first,
569      * the initial events should be forwarded before any other event
570      * or buffer is pushed */
571     if ((pad == dpad->rtp_pad && dpad->pushed_initial_rtp_events) ||
572         (pad == dpad->rtcp_pad && dpad->pushed_initial_rtcp_events)) {
573       newevent = add_ssrc_and_ref (fdata->event, dpad->ssrc);
574       break;
575     }
576   }
577   GST_PAD_UNLOCK (fdata->demux);
578
579   if (newevent)
580     fdata->res &= gst_pad_push_event (pad, newevent);
581
582   return TRUE;
583 }
584
585
586 static gboolean
587 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstObject * parent,
588     GstEvent * event)
589 {
590   GstRtpSsrcDemux *demux;
591   struct ForwardEventData fdata;
592
593   demux = GST_RTP_SSRC_DEMUX (parent);
594
595   fdata.demux = demux;
596   fdata.pad = pad;
597   fdata.event = event;
598   fdata.res = TRUE;
599
600   gst_pad_forward (pad, forward_event, &fdata);
601
602   gst_event_unref (event);
603
604   return fdata.res;
605 }
606
607 static GstFlowReturn
608 gst_rtp_ssrc_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
609 {
610   GstFlowReturn ret;
611   GstRtpSsrcDemux *demux;
612   guint32 ssrc;
613   GstRTPBuffer rtp = { NULL };
614   GstPad *srcpad;
615   GstRtpSsrcDemuxPad *dpad;
616
617   demux = GST_RTP_SSRC_DEMUX (parent);
618
619   if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
620     goto invalid_payload;
621
622   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
623   gst_rtp_buffer_unmap (&rtp);
624
625   GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
626
627   srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTP_PAD);
628   if (srcpad == NULL)
629     goto create_failed;
630
631   /* push to srcpad */
632   ret = gst_pad_push (srcpad, buf);
633
634   if (ret != GST_FLOW_OK) {
635     /* check if the ssrc still there, may have been removed */
636     GST_PAD_LOCK (demux);
637     dpad = find_demux_pad_for_ssrc (demux, ssrc);
638     if (dpad == NULL || dpad->rtp_pad != srcpad) {
639       /* SSRC was removed during the push ... ignore the error */
640       ret = GST_FLOW_OK;
641     }
642     GST_PAD_UNLOCK (demux);
643   }
644
645   gst_object_unref (srcpad);
646
647   return ret;
648
649   /* ERRORS */
650 invalid_payload:
651   {
652     /* this is fatal and should be filtered earlier */
653     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
654         ("Dropping invalid RTP payload"));
655     gst_buffer_unref (buf);
656     return GST_FLOW_ERROR;
657   }
658 create_failed:
659   {
660     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
661         ("Could not create new pad"));
662     gst_buffer_unref (buf);
663     return GST_FLOW_ERROR;
664   }
665 }
666
667 static GstFlowReturn
668 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstObject * parent,
669     GstBuffer * buf)
670 {
671   GstFlowReturn ret;
672   GstRtpSsrcDemux *demux;
673   guint32 ssrc;
674   GstRTCPPacket packet;
675   GstRTCPBuffer rtcp = { NULL, };
676   GstPad *srcpad;
677   GstRtpSsrcDemuxPad *dpad;
678
679   demux = GST_RTP_SSRC_DEMUX (parent);
680
681   if (!gst_rtcp_buffer_validate_reduced (buf))
682     goto invalid_rtcp;
683
684   gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcp);
685   if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
686     gst_rtcp_buffer_unmap (&rtcp);
687     goto invalid_rtcp;
688   }
689
690   /* first packet must be SR or RR, or in case of a reduced size RTCP packet
691    * it must be APP, RTPFB or PSFB feeadback, or else the validate would
692    * have failed */
693   switch (gst_rtcp_packet_get_type (&packet)) {
694     case GST_RTCP_TYPE_SR:
695       /* get the ssrc so that we can route it to the right source pad */
696       gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
697           NULL);
698       break;
699     case GST_RTCP_TYPE_RR:
700       ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
701       break;
702     case GST_RTCP_TYPE_APP:
703     case GST_RTCP_TYPE_RTPFB:
704     case GST_RTCP_TYPE_PSFB:
705       ssrc = gst_rtcp_packet_fb_get_sender_ssrc (&packet);
706       break;
707     default:
708       goto unexpected_rtcp;
709   }
710   gst_rtcp_buffer_unmap (&rtcp);
711
712   GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
713
714   srcpad = find_or_create_demux_pad_for_ssrc (demux, ssrc, RTCP_PAD);
715   if (srcpad == NULL)
716     goto create_failed;
717
718   /* push to srcpad */
719   ret = gst_pad_push (srcpad, buf);
720
721   if (ret != GST_FLOW_OK) {
722     /* check if the ssrc still there, may have been removed */
723     GST_PAD_LOCK (demux);
724     dpad = find_demux_pad_for_ssrc (demux, ssrc);
725     if (dpad == NULL || dpad->rtcp_pad != srcpad) {
726       /* SSRC was removed during the push ... ignore the error */
727       ret = GST_FLOW_OK;
728     }
729     GST_PAD_UNLOCK (demux);
730   }
731
732   gst_object_unref (srcpad);
733
734   return ret;
735
736   /* ERRORS */
737 invalid_rtcp:
738   {
739     /* this is fatal and should be filtered earlier */
740     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
741         ("Dropping invalid RTCP packet"));
742     gst_buffer_unref (buf);
743     return GST_FLOW_ERROR;
744   }
745 unexpected_rtcp:
746   {
747     GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
748     gst_buffer_unref (buf);
749     return GST_FLOW_OK;
750   }
751 create_failed:
752   {
753     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
754         ("Could not create new pad"));
755     gst_buffer_unref (buf);
756     return GST_FLOW_ERROR;
757   }
758 }
759
760 static GstRtpSsrcDemuxPad *
761 find_demux_pad_for_pad (GstRtpSsrcDemux * demux, GstPad * pad)
762 {
763   GSList *walk;
764
765   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
766     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
767     if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
768       return dpad;
769     }
770   }
771
772   return NULL;
773 }
774
775
776 static gboolean
777 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstObject * parent,
778     GstEvent * event)
779 {
780   GstRtpSsrcDemux *demux;
781   const GstStructure *s;
782
783   demux = GST_RTP_SSRC_DEMUX (parent);
784
785   switch (GST_EVENT_TYPE (event)) {
786     case GST_EVENT_CUSTOM_UPSTREAM:
787     case GST_EVENT_CUSTOM_BOTH:
788     case GST_EVENT_CUSTOM_BOTH_OOB:
789       s = gst_event_get_structure (event);
790       if (s && !gst_structure_has_field (s, "ssrc")) {
791         GstRtpSsrcDemuxPad *dpad = find_demux_pad_for_pad (demux, pad);
792
793         if (dpad) {
794           GstStructure *ws;
795
796           event = gst_event_make_writable (event);
797           ws = gst_event_writable_structure (event);
798           gst_structure_set (ws, "ssrc", G_TYPE_UINT, dpad->ssrc, NULL);
799         }
800       }
801       break;
802     default:
803       break;
804   }
805
806   return gst_pad_event_default (pad, parent, event);
807 }
808
809 static GstIterator *
810 gst_rtp_ssrc_demux_iterate_internal_links_src (GstPad * pad, GstObject * parent)
811 {
812   GstRtpSsrcDemux *demux;
813   GstPad *otherpad = NULL;
814   GstIterator *it = NULL;
815   GSList *current;
816
817   demux = GST_RTP_SSRC_DEMUX (parent);
818
819   GST_PAD_LOCK (demux);
820   for (current = demux->srcpads; current; current = g_slist_next (current)) {
821     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
822
823     if (pad == dpad->rtp_pad) {
824       otherpad = demux->rtp_sink;
825       break;
826     } else if (pad == dpad->rtcp_pad) {
827       otherpad = demux->rtcp_sink;
828       break;
829     }
830   }
831   if (otherpad) {
832     GValue val = { 0, };
833
834     g_value_init (&val, GST_TYPE_PAD);
835     g_value_set_object (&val, otherpad);
836     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
837     g_value_unset (&val);
838
839   }
840   GST_PAD_UNLOCK (demux);
841
842   return it;
843 }
844
845 /* Should return 0 for elements to be included */
846 static gint
847 src_pad_compare_func (gconstpointer a, gconstpointer b)
848 {
849   GstPad *pad = GST_PAD (g_value_get_object (a));
850   const gchar *prefix = g_value_get_string (b);
851   gint res;
852
853   /* 0 means equal means we accept the pad, accepted if there is a name
854    * and it starts with the prefix */
855   GST_OBJECT_LOCK (pad);
856   res = !GST_PAD_NAME (pad) || !g_str_has_prefix (GST_PAD_NAME (pad), prefix);
857   GST_OBJECT_UNLOCK (pad);
858
859   return res;
860 }
861
862 static GstIterator *
863 gst_rtp_ssrc_demux_iterate_internal_links_sink (GstPad * pad,
864     GstObject * parent)
865 {
866   GstRtpSsrcDemux *demux;
867   GstIterator *it = NULL;
868   GValue gval = { 0, };
869
870   demux = GST_RTP_SSRC_DEMUX (parent);
871
872   g_value_init (&gval, G_TYPE_STRING);
873   if (pad == demux->rtp_sink)
874     g_value_set_static_string (&gval, "src_");
875   else if (pad == demux->rtcp_sink)
876     g_value_set_static_string (&gval, "rtcp_src_");
877   else
878     g_assert_not_reached ();
879
880   it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
881   it = gst_iterator_filter (it, src_pad_compare_func, &gval);
882
883   return it;
884 }
885
886
887 static gboolean
888 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstObject * parent,
889     GstQuery * query)
890 {
891   GstRtpSsrcDemux *demux;
892   gboolean res = FALSE;
893
894   demux = GST_RTP_SSRC_DEMUX (parent);
895
896   switch (GST_QUERY_TYPE (query)) {
897     case GST_QUERY_LATENCY:
898     {
899
900       if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
901         gboolean live;
902         GstClockTime min_latency, max_latency;
903         GstRtpSsrcDemuxPad *demuxpad;
904
905         demuxpad = gst_pad_get_element_private (pad);
906
907         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
908
909         GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
910             GST_TIME_ARGS (min_latency));
911
912         GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
913
914         gst_query_set_latency (query, live, min_latency, max_latency);
915       }
916       break;
917     }
918     default:
919       res = gst_pad_query_default (pad, parent, query);
920       break;
921   }
922
923   return res;
924 }
925
926 static GstStateChangeReturn
927 gst_rtp_ssrc_demux_change_state (GstElement * element,
928     GstStateChange transition)
929 {
930   GstStateChangeReturn ret;
931   GstRtpSsrcDemux *demux;
932
933   demux = GST_RTP_SSRC_DEMUX (element);
934
935   switch (transition) {
936     case GST_STATE_CHANGE_NULL_TO_READY:
937     case GST_STATE_CHANGE_READY_TO_PAUSED:
938     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
939     default:
940       break;
941   }
942
943   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
944
945   switch (transition) {
946     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
947       break;
948     case GST_STATE_CHANGE_PAUSED_TO_READY:
949       gst_rtp_ssrc_demux_reset (demux);
950       break;
951     case GST_STATE_CHANGE_READY_TO_NULL:
952     default:
953       break;
954   }
955   return ret;
956 }