rtpssrcdemux: Release lock before emitting signal
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpssrcdemux.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * RTP SSRC demuxer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-gstrtpssrcdemux
24  *
25  * gstrtpssrcdemux acts as a demuxer for RTP packets based on the SSRC of the
26  * packets. Its main purpose is to allow an application to easily receive and
27  * decode an RTP stream with multiple SSRCs.
28  * 
29  * For each SSRC that is detected, a new pad will be created and the
30  * #GstRtpSsrcDemux::new-ssrc-pad signal will be emitted. 
31  * 
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch udpsrc caps="application/x-rtp" ! gstrtpssrcdemux ! fakesink
36  * ]| Takes an RTP stream and send the RTP packets with the first detected SSRC
37  * to fakesink, discarding the other SSRCs.
38  * </refsect2>
39  *
40  * Last reviewed on 2007-05-28 (0.10.5)
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <string.h>
48 #include <gst/rtp/gstrtpbuffer.h>
49 #include <gst/rtp/gstrtcpbuffer.h>
50
51 #include "gstrtpbin-marshal.h"
52 #include "gstrtpssrcdemux.h"
53
54 GST_DEBUG_CATEGORY_STATIC (gst_rtp_ssrc_demux_debug);
55 #define GST_CAT_DEFAULT gst_rtp_ssrc_demux_debug
56
57 /* generic templates */
58 static GstStaticPadTemplate rtp_ssrc_demux_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("application/x-rtp")
63     );
64
65 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_sink_template =
66 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("application/x-rtcp")
70     );
71
72 static GstStaticPadTemplate rtp_ssrc_demux_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src_%d",
74     GST_PAD_SRC,
75     GST_PAD_SOMETIMES,
76     GST_STATIC_CAPS ("application/x-rtp")
77     );
78
79 static GstStaticPadTemplate rtp_ssrc_demux_rtcp_src_template =
80 GST_STATIC_PAD_TEMPLATE ("rtcp_src_%d",
81     GST_PAD_SRC,
82     GST_PAD_SOMETIMES,
83     GST_STATIC_CAPS ("application/x-rtcp")
84     );
85
86 #define GST_PAD_LOCK(obj)   (g_mutex_lock ((obj)->padlock))
87 #define GST_PAD_UNLOCK(obj) (g_mutex_unlock ((obj)->padlock))
88
89 /* signals */
90 enum
91 {
92   SIGNAL_NEW_SSRC_PAD,
93   SIGNAL_REMOVED_SSRC_PAD,
94   SIGNAL_CLEAR_SSRC,
95   LAST_SIGNAL
96 };
97
98 GST_BOILERPLATE (GstRtpSsrcDemux, gst_rtp_ssrc_demux, GstElement,
99     GST_TYPE_ELEMENT);
100
101
102 /* GObject vmethods */
103 static void gst_rtp_ssrc_demux_dispose (GObject * object);
104 static void gst_rtp_ssrc_demux_finalize (GObject * object);
105
106 /* GstElement vmethods */
107 static GstStateChangeReturn gst_rtp_ssrc_demux_change_state (GstElement *
108     element, GstStateChange transition);
109
110 static void gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux,
111     guint32 ssrc);
112
113 /* sinkpad stuff */
114 static GstFlowReturn gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf);
115 static gboolean gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event);
116
117 static GstFlowReturn gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad,
118     GstBuffer * buf);
119 static gboolean gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad,
120     GstEvent * event);
121
122 /* srcpad stuff */
123 static gboolean gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event);
124 static GstIterator *gst_rtp_ssrc_demux_iterate_internal_links (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 static GstRtpSsrcDemuxPad *
157 find_or_create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
158 {
159   GstPad *rtp_pad, *rtcp_pad;
160   GstElementClass *klass;
161   GstPadTemplate *templ;
162   gchar *padname;
163   GstRtpSsrcDemuxPad *demuxpad;
164
165   GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
166
167   GST_OBJECT_LOCK (demux);
168
169   demuxpad = find_demux_pad_for_ssrc (demux, ssrc);
170   if (demuxpad != NULL) {
171     GST_OBJECT_UNLOCK (demux);
172     return demuxpad;
173   }
174
175   klass = GST_ELEMENT_GET_CLASS (demux);
176   templ = gst_element_class_get_pad_template (klass, "src_%d");
177   padname = g_strdup_printf ("src_%d", ssrc);
178   rtp_pad = gst_pad_new_from_template (templ, padname);
179   g_free (padname);
180
181   templ = gst_element_class_get_pad_template (klass, "rtcp_src_%d");
182   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
183   rtcp_pad = gst_pad_new_from_template (templ, padname);
184   g_free (padname);
185
186   /* wrap in structure and add to list */
187   demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
188   demuxpad->ssrc = ssrc;
189   demuxpad->rtp_pad = rtp_pad;
190   demuxpad->rtcp_pad = rtcp_pad;
191
192   gst_pad_set_element_private (rtp_pad, demuxpad);
193   gst_pad_set_element_private (rtcp_pad, demuxpad);
194
195   demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
196
197   /* copy caps from input */
198   gst_pad_set_caps (rtp_pad, GST_PAD_CAPS (demux->rtp_sink));
199   gst_pad_use_fixed_caps (rtp_pad);
200   gst_pad_set_caps (rtcp_pad, GST_PAD_CAPS (demux->rtcp_sink));
201   gst_pad_use_fixed_caps (rtcp_pad);
202
203   gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
204   gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
205   gst_pad_set_iterate_internal_links_function (rtp_pad,
206       gst_rtp_ssrc_demux_iterate_internal_links);
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);
212   gst_pad_set_active (rtcp_pad, TRUE);
213
214   GST_OBJECT_UNLOCK (demux);
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   dpad = find_or_create_demux_pad_for_ssrc (demux, ssrc);
525   if (dpad == NULL)
526     goto create_failed;
527
528   /* push to srcpad */
529   ret = gst_pad_push (dpad->rtp_pad, buf);
530
531   return ret;
532
533   /* ERRORS */
534 invalid_payload:
535   {
536     /* this is fatal and should be filtered earlier */
537     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
538         ("Dropping invalid RTP payload"));
539     gst_buffer_unref (buf);
540     return GST_FLOW_ERROR;
541   }
542 create_failed:
543   {
544     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
545         ("Could not create new pad"));
546     gst_buffer_unref (buf);
547     return GST_FLOW_ERROR;
548   }
549 }
550
551 static GstFlowReturn
552 gst_rtp_ssrc_demux_rtcp_chain (GstPad * pad, GstBuffer * buf)
553 {
554   GstFlowReturn ret;
555   GstRtpSsrcDemux *demux;
556   guint32 ssrc;
557   GstRtpSsrcDemuxPad *dpad;
558   GstRTCPPacket packet;
559
560   demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
561
562   if (!gst_rtcp_buffer_validate (buf))
563     goto invalid_rtcp;
564
565   if (!gst_rtcp_buffer_get_first_packet (buf, &packet))
566     goto invalid_rtcp;
567
568   /* first packet must be SR or RR or else the validate would have failed */
569   switch (gst_rtcp_packet_get_type (&packet)) {
570     case GST_RTCP_TYPE_SR:
571       /* get the ssrc so that we can route it to the right source pad */
572       gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, NULL, NULL,
573           NULL);
574       break;
575     default:
576       goto unexpected_rtcp;
577   }
578
579   GST_DEBUG_OBJECT (demux, "received RTCP of SSRC %08x", ssrc);
580
581   dpad = find_or_create_demux_pad_for_ssrc (demux, ssrc);
582   if (dpad == NULL)
583     goto create_failed;
584
585
586   /* push to srcpad */
587   ret = gst_pad_push (dpad->rtcp_pad, buf);
588
589   return ret;
590
591   /* ERRORS */
592 invalid_rtcp:
593   {
594     /* this is fatal and should be filtered earlier */
595     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
596         ("Dropping invalid RTCP packet"));
597     gst_buffer_unref (buf);
598     return GST_FLOW_ERROR;
599   }
600 unexpected_rtcp:
601   {
602     GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
603     gst_buffer_unref (buf);
604     return GST_FLOW_OK;
605   }
606 create_failed:
607   {
608     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
609         ("Could not create new pad"));
610     gst_buffer_unref (buf);
611     return GST_FLOW_ERROR;
612   }
613 }
614
615 static gboolean
616 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event)
617 {
618   GstRtpSsrcDemux *demux;
619   const GstStructure *s;
620
621   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
622
623   switch (GST_EVENT_TYPE (event)) {
624     case GST_EVENT_CUSTOM_UPSTREAM:
625     case GST_EVENT_CUSTOM_BOTH:
626     case GST_EVENT_CUSTOM_BOTH_OOB:
627       s = gst_event_get_structure (event);
628       if (s && !gst_structure_has_field (s, "ssrc")) {
629         GSList *walk;
630
631         for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
632           GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
633
634           if (dpad->rtp_pad == pad || dpad->rtcp_pad == pad) {
635             event =
636                 GST_EVENT_CAST (gst_mini_object_make_writable
637                 (GST_MINI_OBJECT_CAST (event)));
638             gst_structure_set (event->structure, "ssrc", G_TYPE_UINT,
639                 dpad->ssrc, NULL);
640             break;
641           }
642         }
643       }
644       break;
645     default:
646       break;
647   }
648
649   gst_object_unref (demux);
650
651   return gst_pad_event_default (pad, event);
652 }
653
654 static GstIterator *
655 gst_rtp_ssrc_demux_iterate_internal_links (GstPad * pad)
656 {
657   GstRtpSsrcDemux *demux;
658   GstPad *otherpad = NULL;
659   GstIterator *it;
660   GSList *current;
661
662   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
663
664   GST_PAD_LOCK (demux);
665   for (current = demux->srcpads; current; current = g_slist_next (current)) {
666     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
667
668     if (pad == demux->rtp_sink) {
669       otherpad = dpad->rtp_pad;
670       break;
671     } else if (pad == demux->rtcp_sink) {
672       otherpad = dpad->rtcp_pad;
673     } else if (pad == dpad->rtp_pad) {
674       otherpad = demux->rtp_sink;
675       break;
676     } else if (pad == dpad->rtcp_pad) {
677       otherpad = demux->rtcp_sink;
678       break;
679     }
680   }
681   it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
682       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
683   GST_PAD_UNLOCK (demux);
684
685   gst_object_unref (demux);
686   return it;
687 }
688
689 static gboolean
690 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query)
691 {
692   GstRtpSsrcDemux *demux;
693   gboolean res = FALSE;
694
695   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
696   if (G_UNLIKELY (demux == NULL))
697     return FALSE;
698
699   switch (GST_QUERY_TYPE (query)) {
700     case GST_QUERY_LATENCY:
701     {
702
703       if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
704         gboolean live;
705         GstClockTime min_latency, max_latency;
706         GstRtpSsrcDemuxPad *demuxpad;
707
708         demuxpad = gst_pad_get_element_private (pad);
709
710         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
711
712         GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
713             GST_TIME_ARGS (min_latency));
714
715         GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
716
717         gst_query_set_latency (query, live, min_latency, max_latency);
718       }
719       break;
720     }
721     default:
722       res = gst_pad_query_default (pad, query);
723       break;
724   }
725   gst_object_unref (demux);
726
727   return res;
728 }
729
730 static GstStateChangeReturn
731 gst_rtp_ssrc_demux_change_state (GstElement * element,
732     GstStateChange transition)
733 {
734   GstStateChangeReturn ret;
735   GstRtpSsrcDemux *demux;
736
737   demux = GST_RTP_SSRC_DEMUX (element);
738
739   switch (transition) {
740     case GST_STATE_CHANGE_NULL_TO_READY:
741     case GST_STATE_CHANGE_READY_TO_PAUSED:
742     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
743     default:
744       break;
745   }
746
747   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
748
749   switch (transition) {
750     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
751       break;
752     case GST_STATE_CHANGE_PAUSED_TO_READY:
753       gst_rtp_ssrc_demux_reset (demux);
754       break;
755     case GST_STATE_CHANGE_READY_TO_NULL:
756     default:
757       break;
758   }
759   return ret;
760 }