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