1 /* RTP Retransmission queue element for GStreamer
5 * Copyright (C) 2013 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
24 * SECTION:element-rtprtxqueue
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).
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
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.
45 * See also #GstRtpRtxSend, #GstRtpRtxReceive
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
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" ! \
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
70 #include <gst/rtp/gstrtpbuffer.h>
73 #include "gstrtprtxqueue.h"
75 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_queue_debug);
76 #define GST_CAT_DEFAULT gst_rtp_rtx_queue_debug
78 #define DEFAULT_MAX_SIZE_TIME 0
79 #define DEFAULT_MAX_SIZE_PACKETS 100
85 PROP_MAX_SIZE_PACKETS,
87 PROP_FULFILLED_REQUESTS,
90 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
93 GST_STATIC_CAPS ("application/x-rtp")
96 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_STATIC_CAPS ("application/x-rtp")
102 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
104 static gboolean gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
106 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
108 static GstFlowReturn gst_rtp_rtx_queue_chain_list (GstPad * pad,
109 GstObject * parent, GstBufferList * list);
111 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
112 element, GstStateChange transition);
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);
120 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
123 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
125 GObjectClass *gobject_class;
126 GstElementClass *gstelement_class;
128 gobject_class = (GObjectClass *) klass;
129 gstelement_class = (GstElementClass *) klass;
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;
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));
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));
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));
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));
156 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
157 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
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>");
164 gstelement_class->change_state =
165 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
169 gst_rtp_rtx_queue_reset (GstRTPRtxQueue * rtx, gboolean full)
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);
178 rtx->n_fulfilled_requests = 0;
179 g_mutex_unlock (&rtx->lock);
183 gst_rtp_rtx_queue_finalize (GObject * object)
185 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
187 gst_rtp_rtx_queue_reset (rtx, TRUE);
188 g_queue_free (rtx->queue);
189 g_mutex_clear (&rtx->lock);
191 G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
195 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
197 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
200 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
209 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
221 rtx->queue = g_queue_new ();
222 g_mutex_init (&rtx->lock);
224 rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
225 rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
236 push_seqnum (GstBuffer * buffer, RTXData * data)
238 GstRTPRtxQueue *rtx = data->rtx;
239 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
245 if (!GST_IS_BUFFER (buffer) ||
246 !gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
249 seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
250 gst_rtp_buffer_unmap (&rtpbuffer);
252 if (seqnum == data->seqnum) {
254 GST_DEBUG_OBJECT (rtx, "found %d", seqnum);
255 rtx->pending = g_list_prepend (rtx->pending, gst_buffer_ref (buffer));
260 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
262 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
265 switch (GST_EVENT_TYPE (event)) {
266 case GST_EVENT_CUSTOM_UPSTREAM:
268 const GstStructure *s;
270 s = gst_event_get_structure (event);
271 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
275 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
278 GST_DEBUG_OBJECT (rtx, "request %d", seqnum);
280 g_mutex_lock (&rtx->lock);
282 data.seqnum = seqnum;
284 rtx->n_requests += 1;
285 g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
286 g_mutex_unlock (&rtx->lock);
288 gst_event_unref (event);
291 res = gst_pad_event_default (pad, parent, event);
296 res = gst_pad_event_default (pad, parent, event);
303 gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
306 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
309 switch (GST_EVENT_TYPE (event)) {
310 case GST_EVENT_SEGMENT:
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);
319 res = gst_pad_event_default (pad, parent, event);
326 do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
328 rtx->n_fulfilled_requests += 1;
329 gst_pad_push (rtx->srcpad, buffer);
333 get_ts_diff (GstRTPRtxQueue * rtx)
335 GstClockTime high_ts, low_ts;
336 GstClockTimeDiff result;
337 GstBuffer *high_buf, *low_buf;
339 high_buf = g_queue_peek_head (rtx->queue);
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);
347 if (!high_buf || !low_buf || high_buf == low_buf)
350 high_ts = GST_BUFFER_TIMESTAMP (high_buf);
351 low_ts = GST_BUFFER_TIMESTAMP (low_buf);
353 high_ts = gst_segment_to_running_time (&rtx->head_segment, GST_FORMAT_TIME,
355 low_ts = gst_segment_to_running_time (&rtx->tail_segment, GST_FORMAT_TIME,
358 result = high_ts - low_ts;
360 /* return value in ms instead of ns */
361 return (guint32) gst_util_uint64_scale_int (result, 1, GST_MSECOND);
364 /* Must be called with rtx->lock */
366 shrink_queue (GstRTPRtxQueue * rtx)
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));
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));
379 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
385 rtx = GST_RTP_RTX_QUEUE (parent);
387 g_mutex_lock (&rtx->lock);
388 g_queue_push_head (rtx->queue, gst_buffer_ref (buffer));
391 pending = rtx->pending;
393 g_mutex_unlock (&rtx->lock);
395 pending = g_list_reverse (pending);
396 g_list_foreach (pending, (GFunc) do_push, rtx);
397 g_list_free (pending);
399 ret = gst_pad_push (rtx->srcpad, buffer);
405 push_to_queue (GstBuffer ** buffer, guint idx, gpointer user_data)
407 GQueue *queue = user_data;
409 g_queue_push_head (queue, gst_buffer_ref (*buffer));
415 gst_rtp_rtx_queue_chain_list (GstPad * pad, GstObject * parent,
416 GstBufferList * list)
422 rtx = GST_RTP_RTX_QUEUE (parent);
424 g_mutex_lock (&rtx->lock);
425 gst_buffer_list_foreach (list, push_to_queue, rtx->queue);
428 pending = rtx->pending;
430 g_mutex_unlock (&rtx->lock);
432 pending = g_list_reverse (pending);
433 g_list_foreach (pending, (GFunc) do_push, rtx);
434 g_list_free (pending);
436 ret = gst_pad_push_list (rtx->srcpad, list);
442 gst_rtp_rtx_queue_get_property (GObject * object,
443 guint prop_id, GValue * value, GParamSpec * pspec)
445 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
448 case PROP_MAX_SIZE_TIME:
449 g_value_set_uint (value, rtx->max_size_time);
451 case PROP_MAX_SIZE_PACKETS:
452 g_value_set_uint (value, rtx->max_size_packets);
455 g_value_set_uint (value, rtx->n_requests);
457 case PROP_FULFILLED_REQUESTS:
458 g_value_set_uint (value, rtx->n_fulfilled_requests);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 gst_rtp_rtx_queue_set_property (GObject * object,
468 guint prop_id, const GValue * value, GParamSpec * pspec)
470 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
473 case PROP_MAX_SIZE_TIME:
474 rtx->max_size_time = g_value_get_uint (value);
476 case PROP_MAX_SIZE_PACKETS:
477 rtx->max_size_packets = g_value_get_uint (value);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 static GstStateChangeReturn
486 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
488 GstStateChangeReturn ret;
491 rtx = GST_RTP_RTX_QUEUE (element);
493 switch (transition) {
499 GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
502 switch (transition) {
503 case GST_STATE_CHANGE_PAUSED_TO_READY:
504 gst_rtp_rtx_queue_reset (rtx, TRUE);
514 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
516 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
517 "rtp retransmission queue");
519 return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
520 GST_TYPE_RTP_RTX_QUEUE);