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