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