1 /* RTP Retransmission queue element for GStreamer
2  *
3  * gstrtprtxqueue.c:
4  *
5  * Copyright (C) 2013 Wim Taymans <wim.taymans@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /**
24  * SECTION:element-rtprtxqueue
25  *
26  * rtprtxqueue maintains a queue of transmitted RTP packets, up to a
27  * configurable limit (see #GstRTPRtxQueue::max-size-time,
28  * #GstRTPRtxQueue::max-size-packets), and retransmits them upon request
29  * from the downstream rtpsession (GstRTPRetransmissionRequest event).
30  *
31  * This element is similar to rtprtxsend, but it has differences:
32  * - Retransmission from rtprtxqueue is not RFC 4588 compliant. The
33  * retransmitted packets have the same ssrc and payload type as the original
34  * stream.
35  * - As a side-effect of the above, rtprtxqueue does not require the use of
36  * rtprtxreceive on the receiving end. rtpjitterbuffer alone is able to
37  * reconstruct the stream.
38  * - Retransmission from rtprtxqueue happens as soon as the next regular flow
39  * packet is chained, while rtprtxsend retransmits as soon as the retransmission
40  * event is received, using a helper thread.
41  * - rtprtxqueue can be used with rtpbin without the need of hooking to its
42  * #GstRtpBin::request-aux-sender signal, which means it can be used with
43  * rtpbin using gst-launch.
44  *
45  * See also #GstRtpRtxSend, #GstRtpRtxReceive
46  *
47  * # Example pipelines
48  * |[
49  * gst-launch-1.0 rtpbin name=b rtp-profile=avpf \
50  *    audiotestsrc is-live=true ! opusenc ! rtpopuspay pt=96 ! rtprtxqueue ! b.send_rtp_sink_0 \
51  *    b.send_rtp_src_0 ! identity drop-probability=0.01 ! udpsink host="127.0.0.1" port=5000 \
52  *    udpsrc port=5001 ! b.recv_rtcp_sink_0 \
53  *    b.send_rtcp_src_0 ! udpsink host="127.0.0.1" port=5002 sync=false async=false
54  * ]| Sender pipeline
55  * |[
56  * gst-launch-1.0 rtpbin name=b rtp-profile=avpf do-retransmission=true \
57  *    udpsrc port=5000 caps="application/x-rtp,media=(string)audio,clock-rate=(int)48000,encoding-name=(string)OPUS,payload=(int)96" ! \
58  *        b.recv_rtp_sink_0 \
59  *    b. ! rtpopusdepay ! opusdec ! audioconvert ! audioresample ! autoaudiosink \
60  *    udpsrc port=5002 ! b.recv_rtcp_sink_0 \
61  *    b.send_rtcp_src_0 ! udpsink host="127.0.0.1" port=5001 sync=false async=false
62  * ]| Receiver pipeline
63  */
64 
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68 
69 #include <gst/gst.h>
70 #include <gst/rtp/gstrtpbuffer.h>
71 #include <string.h>
72 
73 #include "gstrtprtxqueue.h"
74 
75 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_queue_debug);
76 #define GST_CAT_DEFAULT gst_rtp_rtx_queue_debug
77 
78 #define DEFAULT_MAX_SIZE_TIME    0
79 #define DEFAULT_MAX_SIZE_PACKETS 100
80 
81 enum
82 {
83   PROP_0,
84   PROP_MAX_SIZE_TIME,
85   PROP_MAX_SIZE_PACKETS,
86   PROP_REQUESTS,
87   PROP_FULFILLED_REQUESTS,
88 };
89 
90 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS ("application/x-rtp")
94     );
95 
96 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
97     GST_PAD_SINK,
98     GST_PAD_ALWAYS,
99     GST_STATIC_CAPS ("application/x-rtp")
100     );
101 
102 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
103     GstEvent * event);
104 static gboolean gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
105     GstEvent * event);
106 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
107     GstBuffer * buffer);
108 static GstFlowReturn gst_rtp_rtx_queue_chain_list (GstPad * pad,
109     GstObject * parent, GstBufferList * list);
110 
111 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
112     element, GstStateChange transition);
113 
114 static void gst_rtp_rtx_queue_set_property (GObject * object, guint prop_id,
115     const GValue * value, GParamSpec * pspec);
116 static void gst_rtp_rtx_queue_get_property (GObject * object, guint prop_id,
117     GValue * value, GParamSpec * pspec);
118 static void gst_rtp_rtx_queue_finalize (GObject * object);
119 
120 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
121 
122 static void
gst_rtp_rtx_queue_class_init(GstRTPRtxQueueClass * klass)123 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *gstelement_class;
127 
128   gobject_class = (GObjectClass *) klass;
129   gstelement_class = (GstElementClass *) klass;
130 
131   gobject_class->get_property = gst_rtp_rtx_queue_get_property;
132   gobject_class->set_property = gst_rtp_rtx_queue_set_property;
133   gobject_class->finalize = gst_rtp_rtx_queue_finalize;
134 
135   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
136       g_param_spec_uint ("max-size-time", "Max Size Times",
137           "Amount of ms to queue (0 = unlimited)", 0, G_MAXUINT,
138           DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139 
140   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_PACKETS,
141       g_param_spec_uint ("max-size-packets", "Max Size Packets",
142           "Amount of packets to queue (0 = unlimited)", 0, G_MAXUINT,
143           DEFAULT_MAX_SIZE_PACKETS,
144           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145 
146   g_object_class_install_property (gobject_class, PROP_REQUESTS,
147       g_param_spec_uint ("requests", "Requests",
148           "Total number of retransmission requests", 0, G_MAXUINT,
149           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
150 
151   g_object_class_install_property (gobject_class, PROP_FULFILLED_REQUESTS,
152       g_param_spec_uint ("fulfilled-requests", "Fulfilled Requests",
153           "Number of fulfilled retransmission requests", 0, G_MAXUINT,
154           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
155 
156   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
157   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
158 
159   gst_element_class_set_static_metadata (gstelement_class,
160       "RTP Retransmission Queue", "Codec",
161       "Keep RTP packets in a queue for retransmission",
162       "Wim Taymans <wim.taymans@gmail.com>");
163 
164   gstelement_class->change_state =
165       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
166 }
167 
168 static void
gst_rtp_rtx_queue_reset(GstRTPRtxQueue * rtx,gboolean full)169 gst_rtp_rtx_queue_reset (GstRTPRtxQueue * rtx, gboolean full)
170 {
171   g_mutex_lock (&rtx->lock);
172   g_queue_foreach (rtx->queue, (GFunc) gst_buffer_unref, NULL);
173   g_queue_clear (rtx->queue);
174   g_list_foreach (rtx->pending, (GFunc) gst_buffer_unref, NULL);
175   g_list_free (rtx->pending);
176   rtx->pending = NULL;
177   rtx->n_requests = 0;
178   rtx->n_fulfilled_requests = 0;
179   g_mutex_unlock (&rtx->lock);
180 }
181 
182 static void
gst_rtp_rtx_queue_finalize(GObject * object)183 gst_rtp_rtx_queue_finalize (GObject * object)
184 {
185   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
186 
187   gst_rtp_rtx_queue_reset (rtx, TRUE);
188   g_queue_free (rtx->queue);
189   g_mutex_clear (&rtx->lock);
190 
191   G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
192 }
193 
194 static void
gst_rtp_rtx_queue_init(GstRTPRtxQueue * rtx)195 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
196 {
197   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
198 
199   rtx->srcpad =
200       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
201           "src"), "src");
202   GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
203   GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
204   gst_pad_set_event_function (rtx->srcpad,
205       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_src_event));
206   gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
207 
208   rtx->sinkpad =
209       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
210           "sink"), "sink");
211   GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
212   GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
213   gst_pad_set_event_function (rtx->sinkpad,
214       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_sink_event));
215   gst_pad_set_chain_function (rtx->sinkpad,
216       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain));
217   gst_pad_set_chain_list_function (rtx->sinkpad,
218       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain_list));
219   gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
220 
221   rtx->queue = g_queue_new ();
222   g_mutex_init (&rtx->lock);
223 
224   rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
225   rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
226 }
227 
228 typedef struct
229 {
230   GstRTPRtxQueue *rtx;
231   guint seqnum;
232   gboolean found;
233 } RTXData;
234 
235 static void
push_seqnum(GstBuffer * buffer,RTXData * data)236 push_seqnum (GstBuffer * buffer, RTXData * data)
237 {
238   GstRTPRtxQueue *rtx = data->rtx;
239   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
240   guint16 seqnum;
241 
242   if (data->found)
243     return;
244 
245   if (!GST_IS_BUFFER (buffer) ||
246       !gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
247     return;
248 
249   seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
250   gst_rtp_buffer_unmap (&rtpbuffer);
251 
252   if (seqnum == data->seqnum) {
253     data->found = TRUE;
254     GST_DEBUG_OBJECT (rtx, "found %d", seqnum);
255     rtx->pending = g_list_prepend (rtx->pending, gst_buffer_ref (buffer));
256   }
257 }
258 
259 static gboolean
gst_rtp_rtx_queue_src_event(GstPad * pad,GstObject * parent,GstEvent * event)260 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
261 {
262   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
263   gboolean res;
264 
265   switch (GST_EVENT_TYPE (event)) {
266     case GST_EVENT_CUSTOM_UPSTREAM:
267     {
268       const GstStructure *s;
269 
270       s = gst_event_get_structure (event);
271       if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
272         guint seqnum;
273         RTXData data;
274 
275         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
276           seqnum = -1;
277 
278         GST_DEBUG_OBJECT (rtx, "request %d", seqnum);
279 
280         g_mutex_lock (&rtx->lock);
281         data.rtx = rtx;
282         data.seqnum = seqnum;
283         data.found = FALSE;
284         rtx->n_requests += 1;
285         g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
286         g_mutex_unlock (&rtx->lock);
287 
288         gst_event_unref (event);
289         res = TRUE;
290       } else {
291         res = gst_pad_event_default (pad, parent, event);
292       }
293       break;
294     }
295     default:
296       res = gst_pad_event_default (pad, parent, event);
297       break;
298   }
299   return res;
300 }
301 
302 static gboolean
gst_rtp_rtx_queue_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)303 gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
304     GstEvent * event)
305 {
306   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
307   gboolean res;
308 
309   switch (GST_EVENT_TYPE (event)) {
310     case GST_EVENT_SEGMENT:
311     {
312       g_mutex_lock (&rtx->lock);
313       gst_event_copy_segment (event, &rtx->head_segment);
314       g_queue_push_head (rtx->queue, gst_event_ref (event));
315       g_mutex_unlock (&rtx->lock);
316       /* fall through */
317     }
318     default:
319       res = gst_pad_event_default (pad, parent, event);
320       break;
321   }
322   return res;
323 }
324 
325 static void
do_push(GstBuffer * buffer,GstRTPRtxQueue * rtx)326 do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
327 {
328   rtx->n_fulfilled_requests += 1;
329   gst_pad_push (rtx->srcpad, buffer);
330 }
331 
332 static guint32
get_ts_diff(GstRTPRtxQueue * rtx)333 get_ts_diff (GstRTPRtxQueue * rtx)
334 {
335   GstClockTime high_ts, low_ts;
336   GstClockTimeDiff result;
337   GstBuffer *high_buf, *low_buf;
338 
339   high_buf = g_queue_peek_head (rtx->queue);
340 
341   while (GST_IS_EVENT ((low_buf = g_queue_peek_tail (rtx->queue)))) {
342     GstEvent *event = g_queue_pop_tail (rtx->queue);
343     gst_event_copy_segment (event, &rtx->tail_segment);
344     gst_event_unref (event);
345   }
346 
347   if (!high_buf || !low_buf || high_buf == low_buf)
348     return 0;
349 
350   high_ts = GST_BUFFER_TIMESTAMP (high_buf);
351   low_ts = GST_BUFFER_TIMESTAMP (low_buf);
352 
353   high_ts = gst_segment_to_running_time (&rtx->head_segment, GST_FORMAT_TIME,
354       high_ts);
355   low_ts = gst_segment_to_running_time (&rtx->tail_segment, GST_FORMAT_TIME,
356       low_ts);
357 
358   result = high_ts - low_ts;
359 
360   /* return value in ms instead of ns */
361   return (guint32) gst_util_uint64_scale_int (result, 1, GST_MSECOND);
362 }
363 
364 /* Must be called with rtx->lock */
365 static void
shrink_queue(GstRTPRtxQueue * rtx)366 shrink_queue (GstRTPRtxQueue * rtx)
367 {
368   if (rtx->max_size_packets) {
369     while (g_queue_get_length (rtx->queue) > rtx->max_size_packets)
370       gst_buffer_unref (g_queue_pop_tail (rtx->queue));
371   }
372   if (rtx->max_size_time) {
373     while (get_ts_diff (rtx) > rtx->max_size_time)
374       gst_buffer_unref (g_queue_pop_tail (rtx->queue));
375   }
376 }
377 
378 static GstFlowReturn
gst_rtp_rtx_queue_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)379 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
380 {
381   GstRTPRtxQueue *rtx;
382   GstFlowReturn ret;
383   GList *pending;
384 
385   rtx = GST_RTP_RTX_QUEUE (parent);
386 
387   g_mutex_lock (&rtx->lock);
388   g_queue_push_head (rtx->queue, gst_buffer_ref (buffer));
389   shrink_queue (rtx);
390 
391   pending = rtx->pending;
392   rtx->pending = NULL;
393   g_mutex_unlock (&rtx->lock);
394 
395   pending = g_list_reverse (pending);
396   g_list_foreach (pending, (GFunc) do_push, rtx);
397   g_list_free (pending);
398 
399   ret = gst_pad_push (rtx->srcpad, buffer);
400 
401   return ret;
402 }
403 
404 static gboolean
push_to_queue(GstBuffer ** buffer,guint idx,gpointer user_data)405 push_to_queue (GstBuffer ** buffer, guint idx, gpointer user_data)
406 {
407   GQueue *queue = user_data;
408 
409   g_queue_push_head (queue, gst_buffer_ref (*buffer));
410 
411   return TRUE;
412 }
413 
414 static GstFlowReturn
gst_rtp_rtx_queue_chain_list(GstPad * pad,GstObject * parent,GstBufferList * list)415 gst_rtp_rtx_queue_chain_list (GstPad * pad, GstObject * parent,
416     GstBufferList * list)
417 {
418   GstRTPRtxQueue *rtx;
419   GstFlowReturn ret;
420   GList *pending;
421 
422   rtx = GST_RTP_RTX_QUEUE (parent);
423 
424   g_mutex_lock (&rtx->lock);
425   gst_buffer_list_foreach (list, push_to_queue, rtx->queue);
426   shrink_queue (rtx);
427 
428   pending = rtx->pending;
429   rtx->pending = NULL;
430   g_mutex_unlock (&rtx->lock);
431 
432   pending = g_list_reverse (pending);
433   g_list_foreach (pending, (GFunc) do_push, rtx);
434   g_list_free (pending);
435 
436   ret = gst_pad_push_list (rtx->srcpad, list);
437 
438   return ret;
439 }
440 
441 static void
gst_rtp_rtx_queue_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)442 gst_rtp_rtx_queue_get_property (GObject * object,
443     guint prop_id, GValue * value, GParamSpec * pspec)
444 {
445   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
446 
447   switch (prop_id) {
448     case PROP_MAX_SIZE_TIME:
449       g_value_set_uint (value, rtx->max_size_time);
450       break;
451     case PROP_MAX_SIZE_PACKETS:
452       g_value_set_uint (value, rtx->max_size_packets);
453       break;
454     case PROP_REQUESTS:
455       g_value_set_uint (value, rtx->n_requests);
456       break;
457     case PROP_FULFILLED_REQUESTS:
458       g_value_set_uint (value, rtx->n_fulfilled_requests);
459       break;
460     default:
461       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462       break;
463   }
464 }
465 
466 static void
gst_rtp_rtx_queue_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)467 gst_rtp_rtx_queue_set_property (GObject * object,
468     guint prop_id, const GValue * value, GParamSpec * pspec)
469 {
470   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
471 
472   switch (prop_id) {
473     case PROP_MAX_SIZE_TIME:
474       rtx->max_size_time = g_value_get_uint (value);
475       break;
476     case PROP_MAX_SIZE_PACKETS:
477       rtx->max_size_packets = g_value_get_uint (value);
478       break;
479     default:
480       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481       break;
482   }
483 }
484 
485 static GstStateChangeReturn
gst_rtp_rtx_queue_change_state(GstElement * element,GstStateChange transition)486 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
487 {
488   GstStateChangeReturn ret;
489   GstRTPRtxQueue *rtx;
490 
491   rtx = GST_RTP_RTX_QUEUE (element);
492 
493   switch (transition) {
494     default:
495       break;
496   }
497 
498   ret =
499       GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
500       transition);
501 
502   switch (transition) {
503     case GST_STATE_CHANGE_PAUSED_TO_READY:
504       gst_rtp_rtx_queue_reset (rtx, TRUE);
505       break;
506     default:
507       break;
508   }
509 
510   return ret;
511 }
512 
513 gboolean
gst_rtp_rtx_queue_plugin_init(GstPlugin * plugin)514 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
515 {
516   GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
517       "rtp retransmission queue");
518 
519   return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
520       GST_TYPE_RTP_RTX_QUEUE);
521 }
522