1 /* RTP funnel element for GStreamer
2  *
3  * gstrtpfunnel.c:
4  *
5  * Copyright (C) <2017> Pexip.
6  *   Contact: Havard Graff <havard@pexip.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "gstrtpfunnel.h"
28 
29 GST_DEBUG_CATEGORY_STATIC (gst_rtp_funnel_debug);
30 #define GST_CAT_DEFAULT gst_rtp_funnel_debug
31 
32 struct _GstRtpFunnelPadClass
33 {
34   GstPadClass class;
35 };
36 
37 struct _GstRtpFunnelPad
38 {
39   GstPad pad;
40   guint32 ssrc;
41 };
42 
43 enum
44 {
45   PROP_0,
46   PROP_COMMON_TS_OFFSET,
47 };
48 
49 #define DEFAULT_COMMON_TS_OFFSET -1
50 
51 G_DEFINE_TYPE (GstRtpFunnelPad, gst_rtp_funnel_pad, GST_TYPE_PAD);
52 
53 static void
gst_rtp_funnel_pad_class_init(GstRtpFunnelPadClass * klass)54 gst_rtp_funnel_pad_class_init (GstRtpFunnelPadClass * klass)
55 {
56   (void) klass;
57 }
58 
59 static void
gst_rtp_funnel_pad_init(GstRtpFunnelPad * pad)60 gst_rtp_funnel_pad_init (GstRtpFunnelPad * pad)
61 {
62   (void) pad;
63 }
64 
65 struct _GstRtpFunnelClass
66 {
67   GstElementClass class;
68 };
69 
70 struct _GstRtpFunnel
71 {
72   GstElement element;
73 
74   GstPad *srcpad;
75   GstCaps *srccaps;
76   gboolean send_sticky_events;
77   GHashTable *ssrc_to_pad;
78   /* The last pad data was chained on */
79   GstPad *current_pad;
80 
81   /* properties */
82   gint common_ts_offset;
83 };
84 
85 #define RTP_CAPS "application/x-rtp"
86 
87 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
88     GST_PAD_SINK,
89     GST_PAD_REQUEST,
90     GST_STATIC_CAPS (RTP_CAPS));
91 
92 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
93     GST_PAD_SRC,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS (RTP_CAPS));
96 
97 #define gst_rtp_funnel_parent_class parent_class
98 G_DEFINE_TYPE (GstRtpFunnel, gst_rtp_funnel, GST_TYPE_ELEMENT);
99 
100 
101 static void
gst_rtp_funnel_send_sticky(GstRtpFunnel * funnel,GstPad * pad)102 gst_rtp_funnel_send_sticky (GstRtpFunnel * funnel, GstPad * pad)
103 {
104   GstEvent *stream_start;
105   GstEvent *caps;
106 
107   if (!funnel->send_sticky_events)
108     goto done;
109 
110   stream_start = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
111   if (stream_start && !gst_pad_push_event (funnel->srcpad, stream_start)) {
112     GST_ERROR_OBJECT (funnel, "Could not push stream start");
113     goto done;
114   }
115 
116   caps = gst_event_new_caps (funnel->srccaps);
117   if (caps && !gst_pad_push_event (funnel->srcpad, caps)) {
118     GST_ERROR_OBJECT (funnel, "Could not push caps");
119     goto done;
120   }
121 
122   funnel->send_sticky_events = FALSE;
123 
124 done:
125   return;
126 }
127 
128 static void
gst_rtp_funnel_forward_segment(GstRtpFunnel * funnel,GstPad * pad)129 gst_rtp_funnel_forward_segment (GstRtpFunnel * funnel, GstPad * pad)
130 {
131   GstEvent *segment;
132 
133   if (pad == funnel->current_pad) {
134     goto done;
135   }
136 
137   segment = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
138   if (segment && !gst_pad_push_event (funnel->srcpad, segment)) {
139     GST_ERROR_OBJECT (funnel, "Could not push segment");
140     goto done;
141   }
142 
143   funnel->current_pad = pad;
144 
145 done:
146   return;
147 }
148 
149 static GstFlowReturn
gst_rtp_funnel_sink_chain_object(GstPad * pad,GstRtpFunnel * funnel,gboolean is_list,GstMiniObject * obj)150 gst_rtp_funnel_sink_chain_object (GstPad * pad, GstRtpFunnel * funnel,
151     gboolean is_list, GstMiniObject * obj)
152 {
153   GstFlowReturn res;
154 
155   GST_DEBUG_OBJECT (pad, "received %" GST_PTR_FORMAT, obj);
156 
157   GST_PAD_STREAM_LOCK (funnel->srcpad);
158 
159   gst_rtp_funnel_send_sticky (funnel, pad);
160   gst_rtp_funnel_forward_segment (funnel, pad);
161 
162   if (is_list)
163     res = gst_pad_push_list (funnel->srcpad, GST_BUFFER_LIST_CAST (obj));
164   else
165     res = gst_pad_push (funnel->srcpad, GST_BUFFER_CAST (obj));
166 
167   GST_PAD_STREAM_UNLOCK (funnel->srcpad);
168 
169   return res;
170 }
171 
172 static GstFlowReturn
gst_rtp_funnel_sink_chain_list(GstPad * pad,GstObject * parent,GstBufferList * list)173 gst_rtp_funnel_sink_chain_list (GstPad * pad, GstObject * parent,
174     GstBufferList * list)
175 {
176   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
177 
178   return gst_rtp_funnel_sink_chain_object (pad, funnel, TRUE,
179       GST_MINI_OBJECT_CAST (list));
180 }
181 
182 static GstFlowReturn
gst_rtp_funnel_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)183 gst_rtp_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
184 {
185   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
186 
187   return gst_rtp_funnel_sink_chain_object (pad, funnel, FALSE,
188       GST_MINI_OBJECT_CAST (buffer));
189 }
190 
191 static gboolean
gst_rtp_funnel_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)192 gst_rtp_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
193 {
194   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
195   gboolean forward = TRUE;
196   gboolean ret = TRUE;
197 
198   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
199 
200   switch (GST_EVENT_TYPE (event)) {
201     case GST_EVENT_STREAM_START:
202     case GST_EVENT_SEGMENT:
203       forward = FALSE;
204       break;
205     case GST_EVENT_CAPS:
206     {
207       GstCaps *caps;
208       GstStructure *s;
209       guint ssrc;
210       gst_event_parse_caps (event, &caps);
211 
212       if (!gst_caps_can_intersect (funnel->srccaps, caps)) {
213         GST_ERROR_OBJECT (funnel, "Can't intersect with caps %" GST_PTR_FORMAT,
214             caps);
215         g_assert_not_reached ();
216       }
217 
218       s = gst_caps_get_structure (caps, 0);
219       if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
220         GstRtpFunnelPad *fpad = GST_RTP_FUNNEL_PAD_CAST (pad);
221         fpad->ssrc = ssrc;
222         GST_DEBUG_OBJECT (pad, "Got ssrc: %u", ssrc);
223         GST_OBJECT_LOCK (funnel);
224         g_hash_table_insert (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc), pad);
225         GST_OBJECT_UNLOCK (funnel);
226       }
227 
228       forward = FALSE;
229       break;
230     }
231     default:
232       break;
233   }
234 
235   if (forward) {
236     ret = gst_pad_event_default (pad, parent, event);
237   } else {
238     gst_event_unref (event);
239   }
240 
241   return ret;
242 }
243 
244 static gboolean
gst_rtp_funnel_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)245 gst_rtp_funnel_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
246 {
247   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
248   gboolean res = FALSE;
249   (void) funnel;
250 
251   switch (GST_QUERY_TYPE (query)) {
252     case GST_QUERY_CAPS:
253     {
254       GstCaps *filter_caps;
255       GstCaps *new_caps;
256 
257       gst_query_parse_caps (query, &filter_caps);
258 
259       if (filter_caps) {
260         new_caps = gst_caps_intersect_full (funnel->srccaps, filter_caps,
261             GST_CAPS_INTERSECT_FIRST);
262       } else {
263         new_caps = gst_caps_copy (funnel->srccaps);
264       }
265 
266       if (funnel->common_ts_offset >= 0)
267         gst_caps_set_simple (new_caps, "timestamp-offset", G_TYPE_UINT,
268             (guint) funnel->common_ts_offset, NULL);
269 
270       gst_query_set_caps_result (query, new_caps);
271       GST_DEBUG_OBJECT (pad, "Answering caps-query with caps: %"
272           GST_PTR_FORMAT, new_caps);
273       gst_caps_unref (new_caps);
274       res = TRUE;
275       break;
276     }
277     default:
278       res = gst_pad_query_default (pad, parent, query);
279       break;
280   }
281 
282   return res;
283 }
284 
285 static gboolean
gst_rtp_funnel_src_event(GstPad * pad,GstObject * parent,GstEvent * event)286 gst_rtp_funnel_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
287 {
288   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
289   gboolean handled = FALSE;
290   gboolean ret = TRUE;
291 
292   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
293 
294   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
295     const GstStructure *s = gst_event_get_structure (event);
296     GstPad *fpad;
297     guint ssrc;
298     if (s && gst_structure_get_uint (s, "ssrc", &ssrc)) {
299       handled = TRUE;
300 
301       GST_OBJECT_LOCK (funnel);
302       fpad = g_hash_table_lookup (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc));
303       if (fpad)
304         gst_object_ref (fpad);
305       GST_OBJECT_UNLOCK (funnel);
306 
307       if (fpad) {
308         GST_INFO_OBJECT (pad, "Sending %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT,
309             event, fpad);
310         ret = gst_pad_push_event (fpad, event);
311         gst_object_unref (fpad);
312       } else {
313         gst_event_unref (event);
314       }
315     }
316   }
317 
318   if (!handled) {
319     gst_pad_event_default (pad, parent, event);
320   }
321 
322   return ret;
323 }
324 
325 static GstPad *
gst_rtp_funnel_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)326 gst_rtp_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
327     const gchar * name, const GstCaps * caps)
328 {
329   GstPad *sinkpad;
330   (void) caps;
331 
332   GST_DEBUG_OBJECT (element, "requesting pad");
333 
334   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_RTP_FUNNEL_PAD,
335           "name", name, "direction", templ->direction, "template", templ,
336           NULL));
337 
338   gst_pad_set_chain_function (sinkpad,
339       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain));
340   gst_pad_set_chain_list_function (sinkpad,
341       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain_list));
342   gst_pad_set_event_function (sinkpad,
343       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_event));
344   gst_pad_set_query_function (sinkpad,
345       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_query));
346 
347   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
348   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
349 
350   gst_pad_set_active (sinkpad, TRUE);
351 
352   gst_element_add_pad (element, sinkpad);
353 
354   GST_DEBUG_OBJECT (element, "requested pad %s:%s",
355       GST_DEBUG_PAD_NAME (sinkpad));
356 
357   return sinkpad;
358 }
359 
360 static void
gst_rtp_funnel_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)361 gst_rtp_funnel_set_property (GObject * object, guint prop_id,
362     const GValue * value, GParamSpec * pspec)
363 {
364   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
365 
366   switch (prop_id) {
367     case PROP_COMMON_TS_OFFSET:
368       funnel->common_ts_offset = g_value_get_int (value);
369       break;
370     default:
371       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372       break;
373   }
374 }
375 
376 static void
gst_rtp_funnel_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)377 gst_rtp_funnel_get_property (GObject * object, guint prop_id, GValue * value,
378     GParamSpec * pspec)
379 {
380   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
381 
382   switch (prop_id) {
383     case PROP_COMMON_TS_OFFSET:
384       g_value_set_int (value, funnel->common_ts_offset);
385       break;
386     default:
387       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388       break;
389   }
390 }
391 
392 static GstStateChangeReturn
gst_rtp_funnel_change_state(GstElement * element,GstStateChange transition)393 gst_rtp_funnel_change_state (GstElement * element, GstStateChange transition)
394 {
395   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
396   GstStateChangeReturn ret;
397 
398   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
399 
400   switch (transition) {
401     case GST_STATE_CHANGE_PAUSED_TO_READY:
402       funnel->send_sticky_events = TRUE;
403       break;
404     default:
405       break;
406   }
407 
408   return ret;
409 }
410 
411 static gboolean
_remove_pad_func(gpointer key,gpointer value,gpointer user_data)412 _remove_pad_func (gpointer key, gpointer value, gpointer user_data)
413 {
414   (void) key;
415   if (GST_PAD_CAST (value) == GST_PAD_CAST (user_data))
416     return TRUE;
417   return FALSE;
418 }
419 
420 static void
gst_rtp_funnel_release_pad(GstElement * element,GstPad * pad)421 gst_rtp_funnel_release_pad (GstElement * element, GstPad * pad)
422 {
423   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
424 
425   GST_DEBUG_OBJECT (funnel, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
426 
427   g_hash_table_foreach_remove (funnel->ssrc_to_pad, _remove_pad_func, pad);
428 
429   gst_pad_set_active (pad, FALSE);
430   gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
431 }
432 
433 static void
gst_rtp_funnel_finalize(GObject * object)434 gst_rtp_funnel_finalize (GObject * object)
435 {
436   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
437 
438   gst_caps_unref (funnel->srccaps);
439   g_hash_table_destroy (funnel->ssrc_to_pad);
440 
441   G_OBJECT_CLASS (parent_class)->finalize (object);
442 }
443 
444 static void
gst_rtp_funnel_class_init(GstRtpFunnelClass * klass)445 gst_rtp_funnel_class_init (GstRtpFunnelClass * klass)
446 {
447   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
448   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
449 
450   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_funnel_finalize);
451   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_get_property);
452   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_set_property);
453   gstelement_class->request_new_pad =
454       GST_DEBUG_FUNCPTR (gst_rtp_funnel_request_new_pad);
455   gstelement_class->release_pad =
456       GST_DEBUG_FUNCPTR (gst_rtp_funnel_release_pad);
457   gstelement_class->change_state =
458       GST_DEBUG_FUNCPTR (gst_rtp_funnel_change_state);
459 
460   gst_element_class_set_static_metadata (gstelement_class, "RTP funnel",
461       "RTP Funneling",
462       "Funnel RTP buffers together for multiplexing",
463       "Havard Graff <havard@gstip.com>");
464 
465   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
466   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
467 
468   g_object_class_install_property (gobject_class, PROP_COMMON_TS_OFFSET,
469       g_param_spec_int ("common-ts-offset", "Common Timestamp Offset",
470           "Use the same RTP timestamp offset for all sinkpads (-1 = disable)",
471           -1, G_MAXINT32, DEFAULT_COMMON_TS_OFFSET,
472           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
473 
474   GST_DEBUG_CATEGORY_INIT (gst_rtp_funnel_debug,
475       "gstrtpfunnel", 0, "funnel element");
476 }
477 
478 static void
gst_rtp_funnel_init(GstRtpFunnel * funnel)479 gst_rtp_funnel_init (GstRtpFunnel * funnel)
480 {
481   funnel->srcpad = gst_pad_new_from_static_template (&src_template, "src");
482   gst_pad_use_fixed_caps (funnel->srcpad);
483   gst_pad_set_event_function (funnel->srcpad,
484       GST_DEBUG_FUNCPTR (gst_rtp_funnel_src_event));
485   gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
486 
487   funnel->send_sticky_events = TRUE;
488   funnel->srccaps = gst_caps_new_empty_simple (RTP_CAPS);
489   funnel->ssrc_to_pad = g_hash_table_new (NULL, NULL);
490   funnel->current_pad = NULL;
491 }
492