• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* RTP Retransmission receiver element for GStreamer
2  *
3  * gstrtprtxreceive.c:
4  *
5  * Copyright (C) 2013-2019 Collabora Ltd.
6  *   @author Julien Isorce <julien.isorce@collabora.co.uk>
7  *           Nicolas Dufresne <nicolas.dufresne@collabora.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 /**
26  * SECTION:element-ristrtxreceive
27  * @title: ristrtxreceive
28  * @see_also: ristrtxsend
29  *
30  * This element translates RIST RTX packets into its original form with the
31  * %GST_RTP_BUFFER_FLAG_RETRANSMISSION flag set. This element is intented to
32  * be used by ristsrc element.
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include <gst/gst.h>
40 #include <gst/rtp/gstrtpbuffer.h>
41 
42 #include "gstrist.h"
43 
44 GST_DEBUG_CATEGORY_STATIC (gst_rist_rtx_receive_debug);
45 #define GST_CAT_DEFAULT gst_rist_rtx_receive_debug
46 
47 enum
48 {
49   PROP_0,
50   PROP_NUM_RTX_REQUESTS,
51   PROP_NUM_RTX_PACKETS,
52   PROP_RIST
53 };
54 
55 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("application/x-rtp")
59     );
60 
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp")
65     );
66 
67 struct _GstRistRtxReceive
68 {
69   GstElement element;
70 
71   /* pad */
72   GstPad *sinkpad;
73   GstPad *srcpad;
74 
75   /* statistics */
76   guint num_rtx_requests;
77   guint num_rtx_packets;
78 
79   GstClockTime last_time;
80 };
81 
82 static gboolean gst_rist_rtx_receive_src_event (GstPad * pad,
83     GstObject * parent, GstEvent * event);
84 static GstFlowReturn gst_rist_rtx_receive_chain (GstPad * pad,
85     GstObject * parent, GstBuffer * buffer);
86 static GstStateChangeReturn gst_rist_rtx_receive_change_state (GstElement *
87     element, GstStateChange transition);
88 static void gst_rist_rtx_receive_get_property (GObject * object, guint prop_id,
89     GValue * value, GParamSpec * pspec);
90 
91 G_DEFINE_TYPE_WITH_CODE (GstRistRtxReceive, gst_rist_rtx_receive,
92     GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_rist_rtx_receive_debug,
93         "ristrtxreceive", 0, "RIST retransmission receiver"));
94 GST_ELEMENT_REGISTER_DEFINE (ristrtxreceive, "ristrtxreceive",
95     GST_RANK_NONE, GST_TYPE_RIST_RTX_RECEIVE);
96 
97 static void
gst_rist_rtx_receive_class_init(GstRistRtxReceiveClass * klass)98 gst_rist_rtx_receive_class_init (GstRistRtxReceiveClass * klass)
99 {
100   GObjectClass *gobject_class;
101   GstElementClass *gstelement_class;
102 
103   gobject_class = (GObjectClass *) klass;
104   gstelement_class = (GstElementClass *) klass;
105 
106   gobject_class->get_property = gst_rist_rtx_receive_get_property;
107 
108   g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS,
109       g_param_spec_uint ("num-rtx-requests", "Num RTX Requests",
110           "Number of retransmission events received", 0, G_MAXUINT,
111           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
112 
113   g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS,
114       g_param_spec_uint ("num-rtx-packets", "Num RTX Packets",
115           " Number of retransmission packets received", 0, G_MAXUINT,
116           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
117 
118   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
119   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
120 
121   gst_element_class_set_static_metadata (gstelement_class,
122       "RIST Retransmission receiver", "Codec",
123       "Receive retransmitted RIST packets according to VSF TR-06-1",
124       "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
125 
126   gstelement_class->change_state =
127       GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_change_state);
128 }
129 
130 static void
gst_rist_rtx_receive_reset(GstRistRtxReceive * rtx)131 gst_rist_rtx_receive_reset (GstRistRtxReceive * rtx)
132 {
133   GST_OBJECT_LOCK (rtx);
134   rtx->num_rtx_requests = 0;
135   rtx->num_rtx_packets = 0;
136   GST_OBJECT_UNLOCK (rtx);
137 }
138 
139 static void
gst_rist_rtx_receive_init(GstRistRtxReceive * rtx)140 gst_rist_rtx_receive_init (GstRistRtxReceive * rtx)
141 {
142   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
143 
144   rtx->srcpad =
145       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
146           "src"), "src");
147   GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
148   GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
149   gst_pad_set_event_function (rtx->srcpad,
150       GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_src_event));
151   gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
152 
153   rtx->sinkpad =
154       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
155           "sink"), "sink");
156   GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
157   GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
158   gst_pad_set_chain_function (rtx->sinkpad,
159       GST_DEBUG_FUNCPTR (gst_rist_rtx_receive_chain));
160   gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
161 }
162 
163 static gboolean
gst_rist_rtx_receive_src_event(GstPad * pad,GstObject * parent,GstEvent * event)164 gst_rist_rtx_receive_src_event (GstPad * pad, GstObject * parent,
165     GstEvent * event)
166 {
167   GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (parent);
168 
169   switch (GST_EVENT_TYPE (event)) {
170     case GST_EVENT_CUSTOM_UPSTREAM:
171     {
172       const GstStructure *s = gst_event_get_structure (event);
173 
174       /* This event usually comes from the downstream gstrtpjitterbuffer */
175       if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
176 #ifndef GST_DISABLE_GST_DEBUG
177         guint seqnum = 0;
178         guint ssrc = 0;
179 
180         /* retrieve seqnum of the packet that need to be retransmitted */
181         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
182           seqnum = -1;
183 
184         /* retrieve ssrc of the packet that need to be retransmitted
185          * it's useful when reconstructing the original packet from the rtx packet */
186         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
187           ssrc = -1;
188 
189         GST_DEBUG_OBJECT (rtx, "got rtx request for seqnum: %u, ssrc: %X",
190             seqnum, ssrc);
191 #endif
192 
193         GST_OBJECT_LOCK (rtx);
194         /* increase number of seen requests for our statistics */
195         ++rtx->num_rtx_requests;
196         GST_OBJECT_UNLOCK (rtx);
197       }
198       break;
199     }
200     default:
201       break;
202   }
203 
204   return gst_pad_event_default (pad, parent, event);
205 }
206 
207 static GstFlowReturn
gst_rist_rtx_receive_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)208 gst_rist_rtx_receive_chain (GstPad * pad, GstObject * parent,
209     GstBuffer * buffer)
210 {
211   GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (parent);
212   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
213   guint32 ssrc = 0;
214   guint16 seqnum = 0;
215   gboolean is_rtx;
216 
217   /* map current rtp packet to parse its header */
218   if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
219     goto invalid_buffer;
220 
221   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
222   seqnum = gst_rtp_buffer_get_seq (&rtp);
223 
224   /* check if we have a retransmission packet (this information comes from SDP) */
225   GST_OBJECT_LOCK (rtx);
226 
227   /* RIST sets SSRC LSB to 1 to indicate an RTC packet */
228   is_rtx = ssrc & 0x1;
229   rtx->last_time = GST_BUFFER_PTS (buffer);
230 
231   if (is_rtx)
232     /* increase our statistic */
233     ++rtx->num_rtx_packets;
234 
235   GST_OBJECT_UNLOCK (rtx);
236 
237   /* create the retransmission packet */
238   if (is_rtx) {
239     GST_DEBUG_OBJECT (rtx,
240         "Recovered packet from RIST RTX seqnum:%u ssrc: %u",
241         gst_rtp_buffer_get_seq (&rtp), gst_rtp_buffer_get_ssrc (&rtp));
242     gst_rtp_buffer_set_ssrc (&rtp, ssrc & 0xFFFFFFFE);
243     GST_BUFFER_FLAG_SET (buffer, GST_RTP_BUFFER_FLAG_RETRANSMISSION);
244   }
245 
246   gst_rtp_buffer_unmap (&rtp);
247 
248   GST_TRACE_OBJECT (rtx, "pushing packet seqnum:%u from master stream "
249       "ssrc: %X", seqnum, ssrc);
250   return gst_pad_push (rtx->srcpad, buffer);
251 
252 invalid_buffer:
253   {
254     GST_ELEMENT_WARNING (rtx, STREAM, DECODE, (NULL),
255         ("Received invalid RTP payload, dropping"));
256     gst_buffer_unref (buffer);
257     return GST_FLOW_OK;
258   }
259 }
260 
261 static void
gst_rist_rtx_receive_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)262 gst_rist_rtx_receive_get_property (GObject * object,
263     guint prop_id, GValue * value, GParamSpec * pspec)
264 {
265   GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (object);
266 
267   switch (prop_id) {
268     case PROP_NUM_RTX_REQUESTS:
269       GST_OBJECT_LOCK (rtx);
270       g_value_set_uint (value, rtx->num_rtx_requests);
271       GST_OBJECT_UNLOCK (rtx);
272       break;
273     case PROP_NUM_RTX_PACKETS:
274       GST_OBJECT_LOCK (rtx);
275       g_value_set_uint (value, rtx->num_rtx_packets);
276       GST_OBJECT_UNLOCK (rtx);
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281   }
282 }
283 
284 static GstStateChangeReturn
gst_rist_rtx_receive_change_state(GstElement * element,GstStateChange transition)285 gst_rist_rtx_receive_change_state (GstElement * element,
286     GstStateChange transition)
287 {
288   GstRistRtxReceive *rtx = GST_RIST_RTX_RECEIVE (element);
289   GstStateChangeReturn ret;
290 
291   ret =
292       GST_ELEMENT_CLASS (gst_rist_rtx_receive_parent_class)->change_state
293       (element, transition);
294 
295   switch (transition) {
296     case GST_STATE_CHANGE_PAUSED_TO_READY:
297       gst_rist_rtx_receive_reset (rtx);
298       break;
299     default:
300       break;
301   }
302 
303   return ret;
304 }
305