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