gstrtpsession: improve stats about rtx requests
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpfunnel.c
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
54 gst_rtp_funnel_pad_class_init (GstRtpFunnelPadClass * klass)
55 {
56   (void) klass;
57 }
58
59 static void
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
79   /* properties */
80   gint common_ts_offset;
81 };
82
83 #define RTP_CAPS "application/x-rtp"
84
85 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
86     GST_PAD_SINK,
87     GST_PAD_REQUEST,
88     GST_STATIC_CAPS (RTP_CAPS));
89
90 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS (RTP_CAPS));
94
95 #define gst_rtp_funnel_parent_class parent_class
96 G_DEFINE_TYPE (GstRtpFunnel, gst_rtp_funnel, GST_TYPE_ELEMENT);
97
98
99 static gboolean
100 gst_rtp_funnel_send_sticky (GstRtpFunnel * funnel, GstPad * pad)
101 {
102   GstEvent *stream_start;
103   GstEvent *caps;
104   GstEvent *segment;
105
106   if (!funnel->send_sticky_events)
107     goto done;
108
109   stream_start = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
110   if (stream_start && !gst_pad_push_event (funnel->srcpad, stream_start)) {
111     GST_ERROR_OBJECT (funnel, "Could not push stream start");
112     goto done;
113   }
114
115   caps = gst_event_new_caps (funnel->srccaps);
116   if (caps && !gst_pad_push_event (funnel->srcpad, caps)) {
117     GST_ERROR_OBJECT (funnel, "Could not push caps");
118     goto done;
119   }
120
121   segment = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
122   if (segment && !gst_pad_push_event (funnel->srcpad, segment)) {
123     GST_ERROR_OBJECT (funnel, "Could not push segment");
124     goto done;
125   }
126
127   funnel->send_sticky_events = FALSE;
128
129 done:
130   return !funnel->send_sticky_events;
131 }
132
133 static GstFlowReturn
134 gst_rtp_funnel_sink_chain_object (GstPad * pad, GstRtpFunnel * funnel,
135     gboolean is_list, GstMiniObject * obj)
136 {
137   GstFlowReturn res;
138
139   GST_DEBUG_OBJECT (pad, "received %" GST_PTR_FORMAT, obj);
140
141   GST_PAD_STREAM_LOCK (funnel->srcpad);
142
143   if (!gst_rtp_funnel_send_sticky (funnel, pad)) {
144     GST_PAD_STREAM_UNLOCK (funnel->srcpad);
145     gst_mini_object_unref (obj);
146     return GST_FLOW_OK;
147   }
148
149   if (is_list)
150     res = gst_pad_push_list (funnel->srcpad, GST_BUFFER_LIST_CAST (obj));
151   else
152     res = gst_pad_push (funnel->srcpad, GST_BUFFER_CAST (obj));
153
154   GST_PAD_STREAM_UNLOCK (funnel->srcpad);
155
156   return res;
157 }
158
159 static GstFlowReturn
160 gst_rtp_funnel_sink_chain_list (GstPad * pad, GstObject * parent,
161     GstBufferList * list)
162 {
163   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
164
165   return gst_rtp_funnel_sink_chain_object (pad, funnel, TRUE,
166       GST_MINI_OBJECT_CAST (list));
167 }
168
169 static GstFlowReturn
170 gst_rtp_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
171 {
172   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
173
174   return gst_rtp_funnel_sink_chain_object (pad, funnel, FALSE,
175       GST_MINI_OBJECT_CAST (buffer));
176 }
177
178 static gboolean
179 gst_rtp_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
180 {
181   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
182   gboolean forward = TRUE;
183   gboolean ret = TRUE;
184
185   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
186
187   switch (GST_EVENT_TYPE (event)) {
188     case GST_EVENT_STREAM_START:
189     case GST_EVENT_SEGMENT:
190       forward = FALSE;
191       break;
192     case GST_EVENT_CAPS:
193     {
194       GstCaps *caps;
195       GstStructure *s;
196       guint ssrc;
197       gst_event_parse_caps (event, &caps);
198
199       if (!gst_caps_can_intersect (funnel->srccaps, caps)) {
200         GST_ERROR_OBJECT (funnel, "Can't intersect with caps %" GST_PTR_FORMAT,
201             caps);
202         g_assert_not_reached ();
203       }
204
205       s = gst_caps_get_structure (caps, 0);
206       if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
207         GstRtpFunnelPad *fpad = GST_RTP_FUNNEL_PAD_CAST (pad);
208         fpad->ssrc = ssrc;
209         GST_DEBUG_OBJECT (pad, "Got ssrc: %u", ssrc);
210         GST_OBJECT_LOCK (funnel);
211         g_hash_table_insert (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc), pad);
212         GST_OBJECT_UNLOCK (funnel);
213       }
214
215       forward = FALSE;
216       break;
217     }
218     default:
219       break;
220   }
221
222   if (forward) {
223     ret = gst_pad_event_default (pad, parent, event);
224   } else {
225     gst_event_unref (event);
226   }
227
228   return ret;
229 }
230
231 static gboolean
232 gst_rtp_funnel_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
233 {
234   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
235   gboolean res = FALSE;
236   (void) funnel;
237
238   switch (GST_QUERY_TYPE (query)) {
239     case GST_QUERY_CAPS:
240     {
241       GstCaps *filter_caps;
242       GstCaps *new_caps;
243
244       gst_query_parse_caps (query, &filter_caps);
245
246       if (filter_caps) {
247         new_caps = gst_caps_intersect_full (funnel->srccaps, filter_caps,
248             GST_CAPS_INTERSECT_FIRST);
249       } else {
250         new_caps = gst_caps_copy (funnel->srccaps);
251       }
252
253       if (funnel->common_ts_offset >= 0)
254         gst_caps_set_simple (new_caps, "timestamp-offset", G_TYPE_UINT,
255             (guint) funnel->common_ts_offset, NULL);
256
257       gst_query_set_caps_result (query, new_caps);
258       GST_DEBUG_OBJECT (pad, "Answering caps-query with caps: %"
259           GST_PTR_FORMAT, new_caps);
260       gst_caps_unref (new_caps);
261       res = TRUE;
262       break;
263     }
264     default:
265       res = gst_pad_query_default (pad, parent, query);
266       break;
267   }
268
269   return res;
270 }
271
272 static gboolean
273 gst_rtp_funnel_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
274 {
275   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
276   gboolean handled = FALSE;
277   gboolean ret = TRUE;
278
279   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
280
281   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
282     const GstStructure *s = gst_event_get_structure (event);
283     GstPad *fpad;
284     guint ssrc;
285     if (s && gst_structure_get_uint (s, "ssrc", &ssrc)) {
286       handled = TRUE;
287
288       GST_OBJECT_LOCK (funnel);
289       fpad = g_hash_table_lookup (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc));
290       if (fpad)
291         gst_object_ref (fpad);
292       GST_OBJECT_UNLOCK (funnel);
293
294       if (fpad) {
295         GST_INFO_OBJECT (pad, "Sending %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT,
296             event, fpad);
297         ret = gst_pad_push_event (fpad, event);
298         gst_object_unref (fpad);
299       } else {
300         gst_event_unref (event);
301       }
302     }
303   }
304
305   if (!handled) {
306     gst_pad_event_default (pad, parent, event);
307   }
308
309   return ret;
310 }
311
312 static GstPad *
313 gst_rtp_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
314     const gchar * name, const GstCaps * caps)
315 {
316   GstPad *sinkpad;
317   (void) caps;
318
319   GST_DEBUG_OBJECT (element, "requesting pad");
320
321   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_RTP_FUNNEL_PAD,
322           "name", name, "direction", templ->direction, "template", templ,
323           NULL));
324
325   gst_pad_set_chain_function (sinkpad,
326       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain));
327   gst_pad_set_chain_list_function (sinkpad,
328       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain_list));
329   gst_pad_set_event_function (sinkpad,
330       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_event));
331   gst_pad_set_query_function (sinkpad,
332       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_query));
333
334   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
335   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
336
337   gst_pad_set_active (sinkpad, TRUE);
338
339   gst_element_add_pad (element, sinkpad);
340
341   GST_DEBUG_OBJECT (element, "requested pad %s:%s",
342       GST_DEBUG_PAD_NAME (sinkpad));
343
344   return sinkpad;
345 }
346
347 static void
348 gst_rtp_funnel_set_property (GObject * object, guint prop_id,
349     const GValue * value, GParamSpec * pspec)
350 {
351   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
352
353   switch (prop_id) {
354     case PROP_COMMON_TS_OFFSET:
355       funnel->common_ts_offset = g_value_get_int (value);
356       break;
357     default:
358       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359       break;
360   }
361 }
362
363 static void
364 gst_rtp_funnel_get_property (GObject * object, guint prop_id, GValue * value,
365     GParamSpec * pspec)
366 {
367   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
368
369   switch (prop_id) {
370     case PROP_COMMON_TS_OFFSET:
371       g_value_set_int (value, funnel->common_ts_offset);
372       break;
373     default:
374       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375       break;
376   }
377 }
378
379 static GstStateChangeReturn
380 gst_rtp_funnel_change_state (GstElement * element, GstStateChange transition)
381 {
382   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
383   GstStateChangeReturn ret;
384
385   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
386
387   switch (transition) {
388     case GST_STATE_CHANGE_PAUSED_TO_READY:
389       funnel->send_sticky_events = TRUE;
390       break;
391     default:
392       break;
393   }
394
395   return ret;
396 }
397
398 static gboolean
399 _remove_pad_func (gpointer key, gpointer value, gpointer user_data)
400 {
401   (void) key;
402   if (GST_PAD_CAST (value) == GST_PAD_CAST (user_data))
403     return TRUE;
404   return FALSE;
405 }
406
407 static void
408 gst_rtp_funnel_release_pad (GstElement * element, GstPad * pad)
409 {
410   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
411
412   GST_DEBUG_OBJECT (funnel, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
413
414   g_hash_table_foreach_remove (funnel->ssrc_to_pad, _remove_pad_func, pad);
415
416   gst_pad_set_active (pad, FALSE);
417   gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
418 }
419
420 static void
421 gst_rtp_funnel_finalize (GObject * object)
422 {
423   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
424
425   gst_caps_unref (funnel->srccaps);
426   g_hash_table_destroy (funnel->ssrc_to_pad);
427
428   G_OBJECT_CLASS (parent_class)->finalize (object);
429 }
430
431 static void
432 gst_rtp_funnel_class_init (GstRtpFunnelClass * klass)
433 {
434   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
435   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
436
437   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_funnel_finalize);
438   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_get_property);
439   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_set_property);
440   gstelement_class->request_new_pad =
441       GST_DEBUG_FUNCPTR (gst_rtp_funnel_request_new_pad);
442   gstelement_class->release_pad =
443       GST_DEBUG_FUNCPTR (gst_rtp_funnel_release_pad);
444   gstelement_class->change_state =
445       GST_DEBUG_FUNCPTR (gst_rtp_funnel_change_state);
446
447   gst_element_class_set_static_metadata (gstelement_class, "RTP funnel",
448       "RTP Funneling",
449       "Funnel RTP buffers together for multiplexing",
450       "Havard Graff <havard@gstip.com>");
451
452   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
453   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
454
455   g_object_class_install_property (gobject_class, PROP_COMMON_TS_OFFSET,
456       g_param_spec_int ("common-ts-offset", "Common Timestamp Offset",
457           "Use the same RTP timestamp offset for all sinkpads (-1 = disable)",
458           -1, G_MAXINT32, DEFAULT_COMMON_TS_OFFSET,
459           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460
461   GST_DEBUG_CATEGORY_INIT (gst_rtp_funnel_debug,
462       "gstrtpfunnel", 0, "funnel element");
463 }
464
465 static void
466 gst_rtp_funnel_init (GstRtpFunnel * funnel)
467 {
468   funnel->srcpad = gst_pad_new_from_static_template (&src_template, "src");
469   gst_pad_use_fixed_caps (funnel->srcpad);
470   gst_pad_set_event_function (funnel->srcpad,
471       GST_DEBUG_FUNCPTR (gst_rtp_funnel_src_event));
472   gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
473
474   funnel->send_sticky_events = TRUE;
475   funnel->srccaps = gst_caps_new_empty_simple (RTP_CAPS);
476   funnel->ssrc_to_pad = g_hash_table_new (NULL, NULL);
477 }