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