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