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