rtpssrcdemux: do not hold custom PAD_LOCK when pushing downstream
[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 /* with PAD_LOCK */
157 static GstRtpSsrcDemuxPad *
158 create_demux_pad_for_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc,
159     GstClockTime timestamp)
160 {
161   GstPad *rtp_pad, *rtcp_pad;
162   GstElementClass *klass;
163   GstPadTemplate *templ;
164   gchar *padname;
165   GstRtpSsrcDemuxPad *demuxpad;
166
167   GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
168
169   klass = GST_ELEMENT_GET_CLASS (demux);
170   templ = gst_element_class_get_pad_template (klass, "src_%d");
171   padname = g_strdup_printf ("src_%d", ssrc);
172   rtp_pad = gst_pad_new_from_template (templ, padname);
173   g_free (padname);
174
175   templ = gst_element_class_get_pad_template (klass, "rtcp_src_%d");
176   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
177   rtcp_pad = gst_pad_new_from_template (templ, padname);
178   g_free (padname);
179
180   /* we use the first timestamp received to calculate the difference between
181    * timestamps on all streams */
182   GST_DEBUG_OBJECT (demux, "SSRC %08x, first timestamp %" GST_TIME_FORMAT,
183       ssrc, GST_TIME_ARGS (timestamp));
184
185   /* wrap in structure and add to list */
186   demuxpad = g_new0 (GstRtpSsrcDemuxPad, 1);
187   demuxpad->ssrc = ssrc;
188   demuxpad->rtp_pad = rtp_pad;
189   demuxpad->rtcp_pad = rtcp_pad;
190
191   GST_DEBUG_OBJECT (demux, "first timestamp %" GST_TIME_FORMAT,
192       GST_TIME_ARGS (timestamp));
193
194   gst_pad_set_element_private (rtp_pad, demuxpad);
195   gst_pad_set_element_private (rtcp_pad, demuxpad);
196
197   demux->srcpads = g_slist_prepend (demux->srcpads, demuxpad);
198
199   /* copy caps from input */
200   gst_pad_set_caps (rtp_pad, GST_PAD_CAPS (demux->rtp_sink));
201   gst_pad_use_fixed_caps (rtp_pad);
202   gst_pad_set_caps (rtcp_pad, GST_PAD_CAPS (demux->rtcp_sink));
203   gst_pad_use_fixed_caps (rtcp_pad);
204
205   gst_pad_set_event_function (rtp_pad, gst_rtp_ssrc_demux_src_event);
206   gst_pad_set_query_function (rtp_pad, gst_rtp_ssrc_demux_src_query);
207   gst_pad_set_iterate_internal_links_function (rtp_pad,
208       gst_rtp_ssrc_demux_iterate_internal_links);
209   gst_pad_set_active (rtp_pad, TRUE);
210
211   gst_pad_set_iterate_internal_links_function (rtcp_pad,
212       gst_rtp_ssrc_demux_iterate_internal_links);
213   gst_pad_set_active (rtcp_pad, TRUE);
214
215   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtp_pad);
216   gst_element_add_pad (GST_ELEMENT_CAST (demux), rtcp_pad);
217
218   g_signal_emit (G_OBJECT (demux),
219       gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD], 0, ssrc, rtp_pad);
220
221   return demuxpad;
222 }
223
224 static void
225 gst_rtp_ssrc_demux_base_init (gpointer g_class)
226 {
227   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
228
229   gst_element_class_add_pad_template (gstelement_klass,
230       gst_static_pad_template_get (&rtp_ssrc_demux_sink_template));
231   gst_element_class_add_pad_template (gstelement_klass,
232       gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_sink_template));
233   gst_element_class_add_pad_template (gstelement_klass,
234       gst_static_pad_template_get (&rtp_ssrc_demux_src_template));
235   gst_element_class_add_pad_template (gstelement_klass,
236       gst_static_pad_template_get (&rtp_ssrc_demux_rtcp_src_template));
237
238   gst_element_class_set_details_simple (gstelement_klass, "RTP SSRC Demux",
239       "Demux/Network/RTP",
240       "Splits RTP streams based on the SSRC",
241       "Wim Taymans <wim.taymans@gmail.com>");
242 }
243
244 static void
245 gst_rtp_ssrc_demux_class_init (GstRtpSsrcDemuxClass * klass)
246 {
247   GObjectClass *gobject_klass;
248   GstElementClass *gstelement_klass;
249   GstRtpSsrcDemuxClass *gstrtpssrcdemux_klass;
250
251   gobject_klass = (GObjectClass *) klass;
252   gstelement_klass = (GstElementClass *) klass;
253   gstrtpssrcdemux_klass = (GstRtpSsrcDemuxClass *) klass;
254
255   gobject_klass->dispose = gst_rtp_ssrc_demux_dispose;
256   gobject_klass->finalize = gst_rtp_ssrc_demux_finalize;
257
258   /**
259    * GstRtpSsrcDemux::new-ssrc-pad:
260    * @demux: the object which received the signal
261    * @ssrc: the SSRC of the pad
262    * @pad: the new pad.
263    *
264    * Emited when a new SSRC pad has been created.
265    */
266   gst_rtp_ssrc_demux_signals[SIGNAL_NEW_SSRC_PAD] =
267       g_signal_new ("new-ssrc-pad",
268       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, new_ssrc_pad),
270       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
271       G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
272
273   /**
274    * GstRtpSsrcDemux::removed-ssrc-pad:
275    * @demux: the object which received the signal
276    * @ssrc: the SSRC of the pad
277    * @pad: the removed pad.
278    *
279    * Emited when a SSRC pad has been removed.
280    */
281   gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD] =
282       g_signal_new ("removed-ssrc-pad",
283       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
284       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, removed_ssrc_pad),
285       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT,
286       G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
287
288   /**
289    * GstRtpSsrcDemux::clear-ssrc:
290    * @demux: the object which received the signal
291    * @ssrc: the SSRC of the pad
292    *
293    * Action signal to remove the pad for SSRC.
294    */
295   gst_rtp_ssrc_demux_signals[SIGNAL_CLEAR_SSRC] =
296       g_signal_new ("clear-ssrc",
297       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
298       G_STRUCT_OFFSET (GstRtpSsrcDemuxClass, clear_ssrc),
299       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
300
301   gstelement_klass->change_state =
302       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_change_state);
303   gstrtpssrcdemux_klass->clear_ssrc =
304       GST_DEBUG_FUNCPTR (gst_rtp_ssrc_demux_clear_ssrc);
305
306   GST_DEBUG_CATEGORY_INIT (gst_rtp_ssrc_demux_debug,
307       "rtpssrcdemux", 0, "RTP SSRC demuxer");
308 }
309
310 static void
311 gst_rtp_ssrc_demux_init (GstRtpSsrcDemux * demux,
312     GstRtpSsrcDemuxClass * g_class)
313 {
314   GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
315
316   demux->rtp_sink =
317       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
318           "sink"), "sink");
319   gst_pad_set_chain_function (demux->rtp_sink, gst_rtp_ssrc_demux_chain);
320   gst_pad_set_event_function (demux->rtp_sink, gst_rtp_ssrc_demux_sink_event);
321   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtp_sink);
322
323   demux->rtcp_sink =
324       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
325           "rtcp_sink"), "rtcp_sink");
326   gst_pad_set_chain_function (demux->rtcp_sink, gst_rtp_ssrc_demux_rtcp_chain);
327   gst_pad_set_event_function (demux->rtcp_sink,
328       gst_rtp_ssrc_demux_rtcp_sink_event);
329   gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->rtcp_sink);
330
331   demux->padlock = g_mutex_new ();
332
333   gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
334 }
335
336 static void
337 gst_rtp_ssrc_demux_reset (GstRtpSsrcDemux * demux)
338 {
339   GSList *walk;
340
341   for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
342     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) walk->data;
343
344     gst_pad_set_active (dpad->rtp_pad, FALSE);
345     gst_pad_set_active (dpad->rtcp_pad, FALSE);
346
347     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
348     gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
349     g_free (dpad);
350   }
351   g_slist_free (demux->srcpads);
352   demux->srcpads = NULL;
353 }
354
355 static void
356 gst_rtp_ssrc_demux_dispose (GObject * object)
357 {
358   GstRtpSsrcDemux *demux;
359
360   demux = GST_RTP_SSRC_DEMUX (object);
361
362   gst_rtp_ssrc_demux_reset (demux);
363
364   G_OBJECT_CLASS (parent_class)->dispose (object);
365 }
366
367 static void
368 gst_rtp_ssrc_demux_finalize (GObject * object)
369 {
370   GstRtpSsrcDemux *demux;
371
372   demux = GST_RTP_SSRC_DEMUX (object);
373   g_mutex_free (demux->padlock);
374
375   G_OBJECT_CLASS (parent_class)->finalize (object);
376 }
377
378 static void
379 gst_rtp_ssrc_demux_clear_ssrc (GstRtpSsrcDemux * demux, guint32 ssrc)
380 {
381   GstRtpSsrcDemuxPad *dpad;
382
383   GST_PAD_LOCK (demux);
384   dpad = find_demux_pad_for_ssrc (demux, ssrc);
385   if (dpad == NULL)
386     goto unknown_pad;
387
388   GST_DEBUG_OBJECT (demux, "clearing pad for SSRC %08x", ssrc);
389
390   demux->srcpads = g_slist_remove (demux->srcpads, dpad);
391   GST_PAD_UNLOCK (demux);
392
393   gst_pad_set_active (dpad->rtp_pad, FALSE);
394   gst_pad_set_active (dpad->rtcp_pad, FALSE);
395
396   g_signal_emit (G_OBJECT (demux),
397       gst_rtp_ssrc_demux_signals[SIGNAL_REMOVED_SSRC_PAD], 0, ssrc,
398       dpad->rtp_pad);
399
400   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtp_pad);
401   gst_element_remove_pad (GST_ELEMENT_CAST (demux), dpad->rtcp_pad);
402
403   g_free (dpad);
404
405   return;
406
407   /* ERRORS */
408 unknown_pad:
409   {
410     g_warning ("unknown SSRC %08x", ssrc);
411     return;
412   }
413 }
414
415 static gboolean
416 gst_rtp_ssrc_demux_sink_event (GstPad * pad, GstEvent * event)
417 {
418   GstRtpSsrcDemux *demux;
419   gboolean res = FALSE;
420
421   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
422
423   switch (GST_EVENT_TYPE (event)) {
424     case GST_EVENT_FLUSH_STOP:
425       gst_segment_init (&demux->segment, GST_FORMAT_UNDEFINED);
426     case GST_EVENT_NEWSEGMENT:
427     default:
428     {
429       GSList *walk;
430       GSList *pads = NULL;
431
432       res = TRUE;
433       /* need local snapshot of pads;
434        * should not push downstream while holding lock as that might deadlock
435        * with stuff traveling upstream tyring to get this lock while holding
436        * other (stream)lock */
437       GST_PAD_LOCK (demux);
438       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
439         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
440
441         pads = g_slist_prepend (pads, gst_object_ref (pad->rtp_pad));
442       }
443       GST_PAD_UNLOCK (demux);
444       for (walk = pads; walk; walk = g_slist_next (walk)) {
445         GstPad *pad = (GstPad *) walk->data;
446
447         gst_event_ref (event);
448         res &= gst_pad_push_event (pad, event);
449         gst_object_unref (pad);
450       }
451       g_slist_free (pads);
452       gst_event_unref (event);
453       break;
454     }
455   }
456
457   gst_object_unref (demux);
458   return res;
459 }
460
461 static gboolean
462 gst_rtp_ssrc_demux_rtcp_sink_event (GstPad * pad, GstEvent * event)
463 {
464   GstRtpSsrcDemux *demux;
465   gboolean res = FALSE;
466
467   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
468
469   switch (GST_EVENT_TYPE (event)) {
470     case GST_EVENT_NEWSEGMENT:
471     default:
472     {
473       GSList *walk;
474       GSList *pads = NULL;
475
476       res = TRUE;
477       GST_PAD_LOCK (demux);
478       for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
479         GstRtpSsrcDemuxPad *pad = (GstRtpSsrcDemuxPad *) walk->data;
480
481         pads = g_slist_prepend (pads, gst_object_ref (pad->rtcp_pad));
482       }
483       GST_PAD_UNLOCK (demux);
484       for (walk = pads; walk; walk = g_slist_next (walk)) {
485         GstPad *pad = (GstPad *) walk->data;
486
487         gst_event_ref (event);
488         res &= gst_pad_push_event (pad, event);
489         gst_object_unref (pad);
490       }
491       g_slist_free (pads);
492       gst_event_unref (event);
493       break;
494     }
495   }
496   gst_object_unref (demux);
497   return res;
498 }
499
500 static GstFlowReturn
501 gst_rtp_ssrc_demux_chain (GstPad * pad, GstBuffer * buf)
502 {
503   GstFlowReturn ret;
504   GstRtpSsrcDemux *demux;
505   guint32 ssrc;
506   GstRtpSsrcDemuxPad *dpad;
507
508   demux = GST_RTP_SSRC_DEMUX (GST_OBJECT_PARENT (pad));
509
510   if (!gst_rtp_buffer_validate (buf))
511     goto invalid_payload;
512
513   ssrc = gst_rtp_buffer_get_ssrc (buf);
514
515   GST_DEBUG_OBJECT (demux, "received buffer of SSRC %08x", ssrc);
516
517   GST_PAD_LOCK (demux);
518   dpad = find_demux_pad_for_ssrc (demux, ssrc);
519   if (dpad == NULL) {
520     if (!(dpad =
521             create_demux_pad_for_ssrc (demux, ssrc,
522                 GST_BUFFER_TIMESTAMP (buf))))
523       goto create_failed;
524   }
525   GST_PAD_UNLOCK (demux);
526
527   /* push to srcpad */
528   ret = gst_pad_push (dpad->rtp_pad, buf);
529
530   return ret;
531
532   /* ERRORS */
533 invalid_payload:
534   {
535     /* this is fatal and should be filtered earlier */
536     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
537         ("Dropping invalid RTP payload"));
538     gst_buffer_unref (buf);
539     return GST_FLOW_ERROR;
540   }
541 create_failed:
542   {
543     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
544         ("Could not create new pad"));
545     GST_PAD_UNLOCK (demux);
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   GST_PAD_LOCK (demux);
582   dpad = find_demux_pad_for_ssrc (demux, ssrc);
583   if (dpad == NULL) {
584     GST_DEBUG_OBJECT (demux, "creating pad for SSRC %08x", ssrc);
585     if (!(dpad = create_demux_pad_for_ssrc (demux, ssrc, -1)))
586       goto create_failed;
587   }
588   GST_PAD_UNLOCK (demux);
589
590   /* push to srcpad */
591   ret = gst_pad_push (dpad->rtcp_pad, buf);
592
593   return ret;
594
595   /* ERRORS */
596 invalid_rtcp:
597   {
598     /* this is fatal and should be filtered earlier */
599     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
600         ("Dropping invalid RTCP packet"));
601     gst_buffer_unref (buf);
602     return GST_FLOW_ERROR;
603   }
604 unexpected_rtcp:
605   {
606     GST_DEBUG_OBJECT (demux, "dropping unexpected RTCP packet");
607     gst_buffer_unref (buf);
608     return GST_FLOW_OK;
609   }
610 create_failed:
611   {
612     GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
613         ("Could not create new pad"));
614     GST_PAD_UNLOCK (demux);
615     gst_buffer_unref (buf);
616     return GST_FLOW_ERROR;
617   }
618 }
619
620 static gboolean
621 gst_rtp_ssrc_demux_src_event (GstPad * pad, GstEvent * event)
622 {
623   GstRtpSsrcDemux *demux;
624   gboolean res = FALSE;
625
626   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
627
628   switch (GST_EVENT_TYPE (event)) {
629     case GST_EVENT_SEEK:
630     default:
631       res = gst_pad_event_default (pad, event);
632       break;
633   }
634   gst_object_unref (demux);
635   return res;
636 }
637
638 static GstIterator *
639 gst_rtp_ssrc_demux_iterate_internal_links (GstPad * pad)
640 {
641   GstRtpSsrcDemux *demux;
642   GstPad *otherpad = NULL;
643   GstIterator *it;
644   GSList *current;
645
646   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
647
648   GST_PAD_LOCK (demux);
649   for (current = demux->srcpads; current; current = g_slist_next (current)) {
650     GstRtpSsrcDemuxPad *dpad = (GstRtpSsrcDemuxPad *) current->data;
651
652     if (pad == demux->rtp_sink) {
653       otherpad = dpad->rtp_pad;
654       break;
655     } else if (pad == demux->rtcp_sink) {
656       otherpad = dpad->rtcp_pad;
657     } else if (pad == dpad->rtp_pad) {
658       otherpad = demux->rtp_sink;
659       break;
660     } else if (pad == dpad->rtcp_pad) {
661       otherpad = demux->rtcp_sink;
662       break;
663     }
664   }
665   it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
666       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
667   GST_PAD_UNLOCK (demux);
668
669   gst_object_unref (demux);
670   return it;
671 }
672
673 static gboolean
674 gst_rtp_ssrc_demux_src_query (GstPad * pad, GstQuery * query)
675 {
676   GstRtpSsrcDemux *demux;
677   gboolean res = FALSE;
678
679   demux = GST_RTP_SSRC_DEMUX (gst_pad_get_parent (pad));
680
681   switch (GST_QUERY_TYPE (query)) {
682     case GST_QUERY_LATENCY:
683     {
684
685       if ((res = gst_pad_peer_query (demux->rtp_sink, query))) {
686         gboolean live;
687         GstClockTime min_latency, max_latency;
688         GstRtpSsrcDemuxPad *demuxpad;
689
690         demuxpad = gst_pad_get_element_private (pad);
691
692         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
693
694         GST_DEBUG_OBJECT (demux, "peer min latency %" GST_TIME_FORMAT,
695             GST_TIME_ARGS (min_latency));
696
697         GST_DEBUG_OBJECT (demux, "latency for SSRC %08x", demuxpad->ssrc);
698
699         gst_query_set_latency (query, live, min_latency, max_latency);
700       }
701       break;
702     }
703     default:
704       res = gst_pad_query_default (pad, query);
705       break;
706   }
707   gst_object_unref (demux);
708
709   return res;
710 }
711
712 static GstStateChangeReturn
713 gst_rtp_ssrc_demux_change_state (GstElement * element,
714     GstStateChange transition)
715 {
716   GstStateChangeReturn ret;
717   GstRtpSsrcDemux *demux;
718
719   demux = GST_RTP_SSRC_DEMUX (element);
720
721   switch (transition) {
722     case GST_STATE_CHANGE_NULL_TO_READY:
723     case GST_STATE_CHANGE_READY_TO_PAUSED:
724     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
725     default:
726       break;
727   }
728
729   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
730
731   switch (transition) {
732     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
733       break;
734     case GST_STATE_CHANGE_PAUSED_TO_READY:
735       gst_rtp_ssrc_demux_reset (demux);
736       break;
737     case GST_STATE_CHANGE_READY_TO_NULL:
738     default:
739       break;
740   }
741   return ret;
742 }