• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24  /**
25  * SECTION:element-rtpfunnel
26  * @title: rtpfunnel
27  * @see_also: rtpbasepaylaoder, rtpsession
28  *
29  * RTP funnel is basically like a normal funnel with a few added
30  * functionalities to support bundling.
31  *
32  * Bundle is the concept of sending multiple streams in a single RTP session.
33  * These can be both audio and video streams, and several of both.
34  * One of the advantages with bundling is that you can get away with fewer
35  * ports for sending and receiving media. Also the RTCP traffic gets more
36  * compact if you can report on multiple streams in a single sender/receiver
37  * report.
38  *
39  * One of the reasons for a specialized RTP funnel is that some messages
40  * coming upstream want to find their way back to the right stream,
41  * and a normal funnel can't know which of its sinkpads it should send
42  * these messages to. The RTP funnel achieves this by keeping track of the
43  * SSRC of each stream on its sinkpad, and then uses the fact that upstream
44  * events are tagged inside rtpbin with the appropriate SSRC, so that upon
45  * receiving such an event, the RTP funnel can do a simple lookup for the
46  * right pad to forward the event to.
47  *
48  * A good example here is the KeyUnit event. If several video encoders are
49  * being bundled together using the RTP funnel, and one of the decoders on
50  * the receiving side asks for a KeyUnit, typically a RTCP PLI message will
51  * be sent from the receiver to the sender, and this will be transformed into
52  * a GstForceKeyUnit event inside GstRTPSession, and sent upstream. The
53  * RTP funnel can than make sure that this event hits the right encoder based
54  * on the SSRC embedded in the event.
55  *
56  * Another feature of the RTP funnel is that it will mux together TWCC
57  * (Transport-Wide Congestion Control) sequence-numbers. The point being that
58  * it should increment "transport-wide", meaning potentially several
59  * bundled streams. Note that not *all* streams being bundled needs to be
60  * affected by this. As an example Google WebRTC will use bundle with audio
61  * and video, but will only use TWCC sequence-numbers for the video-stream(s).
62  *
63  */
64 
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68 
69 #include <gst/rtp/gstrtpbuffer.h>
70 #include <gst/rtp/gstrtphdrext.h>
71 
72 #include "gstrtpfunnel.h"
73 
74 GST_DEBUG_CATEGORY_STATIC (gst_rtp_funnel_debug);
75 #define GST_CAT_DEFAULT gst_rtp_funnel_debug
76 
77 /**************** GstRTPFunnelPad ****************/
78 
79 struct _GstRtpFunnelPadClass
80 {
81   GstPadClass class;
82 };
83 
84 struct _GstRtpFunnelPad
85 {
86   GstPad pad;
87   guint32 ssrc;
88   gboolean has_twcc;
89 };
90 
91 G_DEFINE_TYPE (GstRtpFunnelPad, gst_rtp_funnel_pad, GST_TYPE_PAD);
92 GST_ELEMENT_REGISTER_DEFINE (rtpfunnel, "rtpfunnel", GST_RANK_NONE,
93     GST_TYPE_RTP_FUNNEL);
94 
95 static void
gst_rtp_funnel_pad_class_init(G_GNUC_UNUSED GstRtpFunnelPadClass * klass)96 gst_rtp_funnel_pad_class_init (G_GNUC_UNUSED GstRtpFunnelPadClass * klass)
97 {
98 }
99 
100 static void
gst_rtp_funnel_pad_init(G_GNUC_UNUSED GstRtpFunnelPad * pad)101 gst_rtp_funnel_pad_init (G_GNUC_UNUSED GstRtpFunnelPad * pad)
102 {
103 }
104 
105 /**************** GstRTPFunnel ****************/
106 
107 enum
108 {
109   PROP_0,
110   PROP_COMMON_TS_OFFSET,
111 };
112 
113 #define DEFAULT_COMMON_TS_OFFSET -1
114 
115 struct _GstRtpFunnelClass
116 {
117   GstElementClass class;
118 };
119 
120 struct _GstRtpFunnel
121 {
122   GstElement element;
123 
124   GstPad *srcpad;
125   GstCaps *srccaps;             /* protected by OBJECT_LOCK */
126   gboolean send_sticky_events;
127   GHashTable *ssrc_to_pad;      /* protected by OBJECT_LOCK */
128   /* The last pad data was chained on */
129   GstPad *current_pad;
130 
131   guint twcc_pads;              /* numer of sinkpads with negotiated twcc */
132   GstRTPHeaderExtension *twcc_ext;
133 
134   /* properties */
135   gint common_ts_offset;
136 };
137 
138 #define RTP_CAPS "application/x-rtp"
139 
140 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
141     GST_PAD_SINK,
142     GST_PAD_REQUEST,
143     GST_STATIC_CAPS (RTP_CAPS));
144 
145 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
146     GST_PAD_SRC,
147     GST_PAD_ALWAYS,
148     GST_STATIC_CAPS (RTP_CAPS));
149 
150 #define gst_rtp_funnel_parent_class parent_class
151 G_DEFINE_TYPE (GstRtpFunnel, gst_rtp_funnel, GST_TYPE_ELEMENT);
152 
153 
154 static void
gst_rtp_funnel_send_sticky(GstRtpFunnel * funnel,GstPad * pad)155 gst_rtp_funnel_send_sticky (GstRtpFunnel * funnel, GstPad * pad)
156 {
157   GstEvent *stream_start;
158   GstCaps *caps;
159   GstEvent *caps_ev;
160 
161   if (!funnel->send_sticky_events)
162     goto done;
163 
164   stream_start = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
165   if (stream_start && !gst_pad_push_event (funnel->srcpad, stream_start)) {
166     GST_ERROR_OBJECT (funnel, "Could not push stream start");
167     goto done;
168   }
169 
170   /* We modify these caps in our sink pad event handlers, so make sure to
171    * send a copy downstream so that we can keep our internal caps writable */
172   GST_OBJECT_LOCK (funnel);
173   caps = gst_caps_copy (funnel->srccaps);
174   GST_OBJECT_UNLOCK (funnel);
175 
176   caps_ev = gst_event_new_caps (caps);
177   gst_caps_unref (caps);
178   if (caps_ev && !gst_pad_push_event (funnel->srcpad, caps_ev)) {
179     GST_ERROR_OBJECT (funnel, "Could not push caps");
180     goto done;
181   }
182 
183   funnel->send_sticky_events = FALSE;
184 
185 done:
186   return;
187 }
188 
189 static void
gst_rtp_funnel_forward_segment(GstRtpFunnel * funnel,GstPad * pad)190 gst_rtp_funnel_forward_segment (GstRtpFunnel * funnel, GstPad * pad)
191 {
192   GstEvent *event;
193   guint i;
194 
195   if (pad == funnel->current_pad) {
196     goto done;
197   }
198 
199   event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
200   if (event && !gst_pad_push_event (funnel->srcpad, event)) {
201     GST_ERROR_OBJECT (funnel, "Could not push segment");
202     goto done;
203   }
204 
205   for (i = 0;; i++) {
206     event = gst_pad_get_sticky_event (pad, GST_EVENT_CUSTOM_DOWNSTREAM_STICKY,
207         i);
208     if (event == NULL)
209       break;
210     if (!gst_pad_push_event (funnel->srcpad, event))
211       GST_ERROR_OBJECT (funnel, "Could not push custom event");
212   }
213 
214   funnel->current_pad = pad;
215 
216 done:
217   return;
218 }
219 
220 static void
gst_rtp_funnel_set_twcc_seqnum(GstRtpFunnel * funnel,GstPad * pad,GstBuffer ** buf)221 gst_rtp_funnel_set_twcc_seqnum (GstRtpFunnel * funnel,
222     GstPad * pad, GstBuffer ** buf)
223 {
224   GstRtpFunnelPad *fpad = GST_RTP_FUNNEL_PAD_CAST (pad);
225   guint8 twcc_seq[2] = { 0, };
226   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
227   guint ext_id = gst_rtp_header_extension_get_id (funnel->twcc_ext);
228   guint8 *existing;
229   guint size;
230 
231   if (!funnel->twcc_ext || !fpad->has_twcc)
232     return;
233 
234   *buf = gst_buffer_make_writable (*buf);
235 
236   gst_rtp_header_extension_write (funnel->twcc_ext, *buf,
237       GST_RTP_HEADER_EXTENSION_ONE_BYTE, *buf, twcc_seq, sizeof (twcc_seq));
238 
239   if (!gst_rtp_buffer_map (*buf, GST_MAP_READWRITE, &rtp))
240     goto map_failed;
241 
242   if (gst_rtp_buffer_get_extension_onebyte_header (&rtp, ext_id,
243           0, (gpointer) & existing, &size)) {
244     if (size >= gst_rtp_header_extension_get_max_size (funnel->twcc_ext, *buf)) {
245       existing[0] = twcc_seq[0];
246       existing[1] = twcc_seq[1];
247     }
248   }
249   /* TODO: two-byte variant */
250 
251   gst_rtp_buffer_unmap (&rtp);
252 
253   return;
254 
255 map_failed:
256   {
257     GST_ERROR ("failed to map buffer %p", *buf);
258   }
259 }
260 
261 static GstFlowReturn
gst_rtp_funnel_sink_chain_object(GstPad * pad,GstRtpFunnel * funnel,gboolean is_list,GstMiniObject * obj)262 gst_rtp_funnel_sink_chain_object (GstPad * pad, GstRtpFunnel * funnel,
263     gboolean is_list, GstMiniObject * obj)
264 {
265   GstFlowReturn res;
266 
267   GST_DEBUG_OBJECT (pad, "received %" GST_PTR_FORMAT, obj);
268 
269   GST_PAD_STREAM_LOCK (funnel->srcpad);
270 
271   gst_rtp_funnel_send_sticky (funnel, pad);
272   gst_rtp_funnel_forward_segment (funnel, pad);
273 
274   if (is_list) {
275     res = gst_pad_push_list (funnel->srcpad, GST_BUFFER_LIST_CAST (obj));
276   } else {
277     GstBuffer *buf = GST_BUFFER_CAST (obj);
278     gst_rtp_funnel_set_twcc_seqnum (funnel, pad, &buf);
279     res = gst_pad_push (funnel->srcpad, buf);
280   }
281   GST_PAD_STREAM_UNLOCK (funnel->srcpad);
282 
283   return res;
284 }
285 
286 static GstFlowReturn
gst_rtp_funnel_sink_chain_list(GstPad * pad,GstObject * parent,GstBufferList * list)287 gst_rtp_funnel_sink_chain_list (GstPad * pad, GstObject * parent,
288     GstBufferList * list)
289 {
290   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
291 
292   return gst_rtp_funnel_sink_chain_object (pad, funnel, TRUE,
293       GST_MINI_OBJECT_CAST (list));
294 }
295 
296 static GstFlowReturn
gst_rtp_funnel_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)297 gst_rtp_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
298 {
299   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
300 
301   return gst_rtp_funnel_sink_chain_object (pad, funnel, FALSE,
302       GST_MINI_OBJECT_CAST (buffer));
303 }
304 
305 static void
gst_rtp_funnel_set_twcc_ext_id(GstRtpFunnel * funnel,guint8 twcc_ext_id)306 gst_rtp_funnel_set_twcc_ext_id (GstRtpFunnel * funnel, guint8 twcc_ext_id)
307 {
308   gchar *name;
309   guint current_ext_id;
310 
311   current_ext_id = gst_rtp_header_extension_get_id (funnel->twcc_ext);
312   g_object_set (funnel->twcc_ext, "n-streams", funnel->twcc_pads, NULL);
313 
314   if (current_ext_id == twcc_ext_id)
315     return;
316 
317   name = g_strdup_printf ("extmap-%u", twcc_ext_id);
318 
319   gst_caps_set_simple (funnel->srccaps, name, G_TYPE_STRING,
320       gst_rtp_header_extension_get_uri (funnel->twcc_ext), NULL);
321 
322   g_free (name);
323 
324   /* make sure we update the sticky with the new caps */
325   funnel->send_sticky_events = TRUE;
326 
327   gst_rtp_header_extension_set_id (funnel->twcc_ext, twcc_ext_id);
328 }
329 
330 static guint8
_get_extmap_id_for_attribute(const GstStructure * s,const gchar * ext_name)331 _get_extmap_id_for_attribute (const GstStructure * s, const gchar * ext_name)
332 {
333   guint i;
334   guint8 extmap_id = 0;
335   guint n_fields = gst_structure_n_fields (s);
336 
337   for (i = 0; i < n_fields; i++) {
338     const gchar *field_name = gst_structure_nth_field_name (s, i);
339     if (g_str_has_prefix (field_name, "extmap-")) {
340       const gchar *str = gst_structure_get_string (s, field_name);
341       if (str && g_strcmp0 (str, ext_name) == 0) {
342         gint64 id = g_ascii_strtoll (field_name + 7, NULL, 10);
343         if (id > 0 && id < 15) {
344           extmap_id = id;
345           break;
346         }
347       }
348     }
349   }
350   return extmap_id;
351 }
352 
353 #define TWCC_EXTMAP_STR "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
354 
355 static gboolean
gst_rtp_funnel_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)356 gst_rtp_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
357 {
358   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
359   GstRtpFunnelPad *fpad = GST_RTP_FUNNEL_PAD_CAST (pad);
360 
361   gboolean forward = TRUE;
362   gboolean ret = TRUE;
363 
364   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
365 
366   switch (GST_EVENT_TYPE (event)) {
367     case GST_EVENT_STREAM_START:
368     case GST_EVENT_SEGMENT:
369       forward = FALSE;
370       break;
371     case GST_EVENT_CAPS:
372     {
373       GstCaps *caps;
374       GstStructure *s;
375       guint ssrc;
376       guint8 ext_id;
377       GstCaps *rtpcaps = gst_caps_new_empty_simple (RTP_CAPS);
378 
379       gst_event_parse_caps (event, &caps);
380 
381       GST_OBJECT_LOCK (funnel);
382       if (!gst_caps_can_intersect (rtpcaps, caps)) {
383         GST_ERROR_OBJECT (funnel, "Can't intersect with caps %" GST_PTR_FORMAT,
384             caps);
385         g_assert_not_reached ();
386       }
387 
388       gst_caps_unref (rtpcaps);
389 
390       s = gst_caps_get_structure (caps, 0);
391       if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
392         fpad->ssrc = ssrc;
393         GST_DEBUG_OBJECT (pad, "Got ssrc: %u", ssrc);
394         g_hash_table_insert (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc), pad);
395       }
396 
397       if (!funnel->twcc_ext)
398         funnel->twcc_ext =
399             gst_rtp_header_extension_create_from_uri (TWCC_EXTMAP_STR);
400 
401       ext_id = _get_extmap_id_for_attribute (s, TWCC_EXTMAP_STR);
402       if (ext_id > 0) {
403         fpad->has_twcc = TRUE;
404         funnel->twcc_pads++;
405         gst_rtp_funnel_set_twcc_ext_id (funnel, ext_id);
406       }
407       GST_OBJECT_UNLOCK (funnel);
408 
409       forward = FALSE;
410       break;
411     }
412     default:
413       break;
414   }
415 
416   if (forward) {
417     ret = gst_pad_event_default (pad, parent, event);
418   } else {
419     gst_event_unref (event);
420   }
421 
422   return ret;
423 }
424 
425 static gboolean
gst_rtp_funnel_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)426 gst_rtp_funnel_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
427 {
428   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
429   gboolean res = TRUE;
430 
431   switch (GST_QUERY_TYPE (query)) {
432     case GST_QUERY_CAPS:
433     {
434       GstCaps *filter_caps;
435       GstCaps *new_caps;
436       GstCaps *rtpcaps = gst_caps_new_empty_simple (RTP_CAPS);
437 
438       gst_query_parse_caps (query, &filter_caps);
439 
440       GST_OBJECT_LOCK (funnel);
441       if (filter_caps) {
442         new_caps = gst_caps_intersect_full (rtpcaps, filter_caps,
443             GST_CAPS_INTERSECT_FIRST);
444         gst_caps_unref (rtpcaps);
445       } else {
446         new_caps = rtpcaps;
447       }
448       GST_OBJECT_UNLOCK (funnel);
449 
450       if (funnel->common_ts_offset >= 0)
451         gst_caps_set_simple (new_caps, "timestamp-offset", G_TYPE_UINT,
452             (guint) funnel->common_ts_offset, NULL);
453 
454       gst_query_set_caps_result (query, new_caps);
455       GST_DEBUG_OBJECT (pad, "Answering caps-query with caps: %"
456           GST_PTR_FORMAT, new_caps);
457       gst_caps_unref (new_caps);
458       break;
459     }
460     case GST_QUERY_ACCEPT_CAPS:
461     {
462       GstCaps *caps;
463       gboolean result;
464 
465       gst_query_parse_accept_caps (query, &caps);
466 
467       GST_OBJECT_LOCK (funnel);
468       result = gst_caps_can_intersect (caps, funnel->srccaps);
469       if (!result) {
470         GST_ERROR_OBJECT (pad,
471             "caps: %" GST_PTR_FORMAT " were not compatible with: %"
472             GST_PTR_FORMAT, caps, funnel->srccaps);
473       }
474       GST_OBJECT_UNLOCK (funnel);
475 
476       gst_query_set_accept_caps_result (query, result);
477       break;
478     }
479     default:
480       res = gst_pad_query_default (pad, parent, query);
481       break;
482   }
483 
484   return res;
485 }
486 
487 static gboolean
gst_rtp_funnel_src_event(GstPad * pad,GstObject * parent,GstEvent * event)488 gst_rtp_funnel_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
489 {
490   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (parent);
491   gboolean handled = FALSE;
492   gboolean ret = TRUE;
493 
494   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
495 
496   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
497     const GstStructure *s = gst_event_get_structure (event);
498     GstPad *fpad;
499     guint ssrc;
500     if (s && gst_structure_get_uint (s, "ssrc", &ssrc)) {
501       handled = TRUE;
502 
503       GST_OBJECT_LOCK (funnel);
504       fpad = g_hash_table_lookup (funnel->ssrc_to_pad, GUINT_TO_POINTER (ssrc));
505       if (fpad)
506         gst_object_ref (fpad);
507       GST_OBJECT_UNLOCK (funnel);
508 
509       if (fpad) {
510         GST_INFO_OBJECT (pad, "Sending %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT,
511             event, fpad);
512         ret = gst_pad_push_event (fpad, event);
513         gst_object_unref (fpad);
514       } else {
515         gst_event_unref (event);
516       }
517     }
518   }
519 
520   if (!handled) {
521     gst_pad_event_default (pad, parent, event);
522   }
523 
524   return ret;
525 }
526 
527 static GstPad *
gst_rtp_funnel_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)528 gst_rtp_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
529     const gchar * name, const GstCaps * caps)
530 {
531   GstPad *sinkpad;
532   (void) caps;
533 
534   GST_DEBUG_OBJECT (element, "requesting pad");
535 
536   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_RTP_FUNNEL_PAD,
537           "name", name, "direction", templ->direction, "template", templ,
538           NULL));
539 
540   gst_pad_set_chain_function (sinkpad,
541       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain));
542   gst_pad_set_chain_list_function (sinkpad,
543       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_chain_list));
544   gst_pad_set_event_function (sinkpad,
545       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_event));
546   gst_pad_set_query_function (sinkpad,
547       GST_DEBUG_FUNCPTR (gst_rtp_funnel_sink_query));
548 
549   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
550   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
551 
552   gst_pad_set_active (sinkpad, TRUE);
553 
554   gst_element_add_pad (element, sinkpad);
555 
556   GST_DEBUG_OBJECT (element, "requested pad %s:%s",
557       GST_DEBUG_PAD_NAME (sinkpad));
558 
559   return sinkpad;
560 }
561 
562 static void
gst_rtp_funnel_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)563 gst_rtp_funnel_set_property (GObject * object, guint prop_id,
564     const GValue * value, GParamSpec * pspec)
565 {
566   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
567 
568   switch (prop_id) {
569     case PROP_COMMON_TS_OFFSET:
570       funnel->common_ts_offset = g_value_get_int (value);
571       break;
572     default:
573       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574       break;
575   }
576 }
577 
578 static void
gst_rtp_funnel_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)579 gst_rtp_funnel_get_property (GObject * object, guint prop_id, GValue * value,
580     GParamSpec * pspec)
581 {
582   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
583 
584   switch (prop_id) {
585     case PROP_COMMON_TS_OFFSET:
586       g_value_set_int (value, funnel->common_ts_offset);
587       break;
588     default:
589       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590       break;
591   }
592 }
593 
594 static GstStateChangeReturn
gst_rtp_funnel_change_state(GstElement * element,GstStateChange transition)595 gst_rtp_funnel_change_state (GstElement * element, GstStateChange transition)
596 {
597   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
598   GstStateChangeReturn ret;
599 
600   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
601 
602   switch (transition) {
603     case GST_STATE_CHANGE_PAUSED_TO_READY:
604       funnel->send_sticky_events = TRUE;
605       break;
606     default:
607       break;
608   }
609 
610   return ret;
611 }
612 
613 static gboolean
_remove_pad_func(gpointer key,gpointer value,gpointer user_data)614 _remove_pad_func (gpointer key, gpointer value, gpointer user_data)
615 {
616   (void) key;
617   if (GST_PAD_CAST (value) == GST_PAD_CAST (user_data))
618     return TRUE;
619   return FALSE;
620 }
621 
622 static void
gst_rtp_funnel_release_pad(GstElement * element,GstPad * pad)623 gst_rtp_funnel_release_pad (GstElement * element, GstPad * pad)
624 {
625   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (element);
626 
627   GST_DEBUG_OBJECT (funnel, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
628 
629   g_hash_table_foreach_remove (funnel->ssrc_to_pad, _remove_pad_func, pad);
630 
631   gst_pad_set_active (pad, FALSE);
632   gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
633 }
634 
635 static void
gst_rtp_funnel_finalize(GObject * object)636 gst_rtp_funnel_finalize (GObject * object)
637 {
638   GstRtpFunnel *funnel = GST_RTP_FUNNEL_CAST (object);
639 
640   gst_caps_unref (funnel->srccaps);
641   g_hash_table_destroy (funnel->ssrc_to_pad);
642 
643   gst_clear_object (&funnel->twcc_ext);
644 
645   G_OBJECT_CLASS (parent_class)->finalize (object);
646 }
647 
648 static void
gst_rtp_funnel_class_init(GstRtpFunnelClass * klass)649 gst_rtp_funnel_class_init (GstRtpFunnelClass * klass)
650 {
651   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
652   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
653 
654   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_funnel_finalize);
655   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_get_property);
656   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_rtp_funnel_set_property);
657   gstelement_class->request_new_pad =
658       GST_DEBUG_FUNCPTR (gst_rtp_funnel_request_new_pad);
659   gstelement_class->release_pad =
660       GST_DEBUG_FUNCPTR (gst_rtp_funnel_release_pad);
661   gstelement_class->change_state =
662       GST_DEBUG_FUNCPTR (gst_rtp_funnel_change_state);
663 
664   gst_element_class_set_static_metadata (gstelement_class, "RTP funnel",
665       "RTP Funneling",
666       "Funnel RTP buffers together for multiplexing",
667       "Havard Graff <havard@gstip.com>");
668 
669   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
670   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
671 
672   g_object_class_install_property (gobject_class, PROP_COMMON_TS_OFFSET,
673       g_param_spec_int ("common-ts-offset", "Common Timestamp Offset",
674           "Use the same RTP timestamp offset for all sinkpads (-1 = disable)",
675           -1, G_MAXINT32, DEFAULT_COMMON_TS_OFFSET,
676           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
677 
678   GST_DEBUG_CATEGORY_INIT (gst_rtp_funnel_debug,
679       "gstrtpfunnel", 0, "funnel element");
680 }
681 
682 static void
gst_rtp_funnel_init(GstRtpFunnel * funnel)683 gst_rtp_funnel_init (GstRtpFunnel * funnel)
684 {
685   funnel->srcpad = gst_pad_new_from_static_template (&src_template, "src");
686   gst_pad_use_fixed_caps (funnel->srcpad);
687   gst_pad_set_event_function (funnel->srcpad,
688       GST_DEBUG_FUNCPTR (gst_rtp_funnel_src_event));
689   gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
690 
691   funnel->send_sticky_events = TRUE;
692   funnel->srccaps = gst_caps_new_empty_simple (RTP_CAPS);
693   funnel->ssrc_to_pad = g_hash_table_new (NULL, NULL);
694   funnel->current_pad = NULL;
695 }
696