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
32 #include <gst/rtp/gstrtpbuffer.h>
35 #include "gstrtprtxqueue.h"
37 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_queue_debug);
38 #define GST_CAT_DEFAULT gst_rtp_rtx_queue_debug
40 #define DEFAULT_MAX_SIZE_TIME 0
41 #define DEFAULT_MAX_SIZE_PACKETS 100
47 PROP_MAX_SIZE_PACKETS,
49 PROP_FULFILLED_REQUESTS,
52 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("application/x-rtp")
58 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp")
64 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
66 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
68 static GstFlowReturn gst_rtp_rtx_queue_chain_list (GstPad * pad,
69 GstObject * parent, GstBufferList * list);
71 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
72 element, GstStateChange transition);
74 static void gst_rtp_rtx_queue_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void gst_rtp_rtx_queue_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
78 static void gst_rtp_rtx_queue_finalize (GObject * object);
80 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
83 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
85 GObjectClass *gobject_class;
86 GstElementClass *gstelement_class;
88 gobject_class = (GObjectClass *) klass;
89 gstelement_class = (GstElementClass *) klass;
91 gobject_class->get_property = gst_rtp_rtx_queue_get_property;
92 gobject_class->set_property = gst_rtp_rtx_queue_set_property;
93 gobject_class->finalize = gst_rtp_rtx_queue_finalize;
95 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
96 g_param_spec_uint ("max-size-time", "Max Size Times",
97 "Amount of ms to queue (0 = unlimited)", 0, G_MAXUINT,
98 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_PACKETS,
101 g_param_spec_uint ("max-size-packets", "Max Size Packets",
102 "Amount of packets to queue (0 = unlimited)", 0, G_MAXUINT,
103 DEFAULT_MAX_SIZE_PACKETS,
104 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 g_object_class_install_property (gobject_class, PROP_REQUESTS,
107 g_param_spec_uint ("requests", "Requests",
108 "Total number of retransmission requests", 0, G_MAXUINT,
109 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
111 g_object_class_install_property (gobject_class, PROP_FULFILLED_REQUESTS,
112 g_param_spec_uint ("fulfilled-requests", "Fulfilled Requests",
113 "Number of fulfilled retransmission requests", 0, G_MAXUINT,
114 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
116 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
117 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
119 gst_element_class_set_static_metadata (gstelement_class,
120 "RTP Retransmission Queue", "Codec",
121 "Keep RTP packets in a queue for retransmission",
122 "Wim Taymans <wim.taymans@gmail.com>");
124 gstelement_class->change_state =
125 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
129 gst_rtp_rtx_queue_reset (GstRTPRtxQueue * rtx, gboolean full)
131 g_mutex_lock (&rtx->lock);
132 g_queue_foreach (rtx->queue, (GFunc) gst_buffer_unref, NULL);
133 g_queue_clear (rtx->queue);
134 g_list_foreach (rtx->pending, (GFunc) gst_buffer_unref, NULL);
135 g_list_free (rtx->pending);
138 rtx->n_fulfilled_requests = 0;
139 g_mutex_unlock (&rtx->lock);
143 gst_rtp_rtx_queue_finalize (GObject * object)
145 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
147 gst_rtp_rtx_queue_reset (rtx, TRUE);
148 g_queue_free (rtx->queue);
149 g_mutex_clear (&rtx->lock);
151 G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
155 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
157 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
160 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
162 GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
163 GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
164 gst_pad_set_event_function (rtx->srcpad,
165 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_src_event));
166 gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
169 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
171 GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
172 GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
173 gst_pad_set_chain_function (rtx->sinkpad,
174 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain));
175 gst_pad_set_chain_list_function (rtx->sinkpad,
176 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain_list));
177 gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
179 rtx->queue = g_queue_new ();
180 g_mutex_init (&rtx->lock);
182 rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
183 rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
194 push_seqnum (GstBuffer * buffer, RTXData * data)
196 GstRTPRtxQueue *rtx = data->rtx;
197 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
203 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
206 seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
207 gst_rtp_buffer_unmap (&rtpbuffer);
209 if (seqnum == data->seqnum) {
211 GST_DEBUG_OBJECT (rtx, "found %d", seqnum);
212 rtx->pending = g_list_prepend (rtx->pending, gst_buffer_ref (buffer));
217 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
219 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
222 switch (GST_EVENT_TYPE (event)) {
223 case GST_EVENT_CUSTOM_UPSTREAM:
225 const GstStructure *s;
227 s = gst_event_get_structure (event);
228 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
232 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
235 GST_DEBUG_OBJECT (rtx, "request %d", seqnum);
237 g_mutex_lock (&rtx->lock);
239 data.seqnum = seqnum;
241 rtx->n_requests += 1;
242 g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
243 g_mutex_unlock (&rtx->lock);
245 gst_event_unref (event);
248 res = gst_pad_event_default (pad, parent, event);
253 res = gst_pad_event_default (pad, parent, event);
260 do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
262 rtx->n_fulfilled_requests += 1;
263 gst_pad_push (rtx->srcpad, buffer);
266 /* Must be called with rtx->lock */
268 shrink_queue (GstRTPRtxQueue * rtx)
270 if (rtx->max_size_packets) {
271 while (g_queue_get_length (rtx->queue) > rtx->max_size_packets)
272 gst_buffer_unref (g_queue_pop_tail (rtx->queue));
277 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
283 rtx = GST_RTP_RTX_QUEUE (parent);
285 g_mutex_lock (&rtx->lock);
286 g_queue_push_head (rtx->queue, gst_buffer_ref (buffer));
289 pending = rtx->pending;
291 g_mutex_unlock (&rtx->lock);
293 pending = g_list_reverse (pending);
294 g_list_foreach (pending, (GFunc) do_push, rtx);
295 g_list_free (pending);
297 ret = gst_pad_push (rtx->srcpad, buffer);
303 push_to_queue (GstBuffer ** buffer, guint idx, gpointer user_data)
305 GQueue *queue = user_data;
307 g_queue_push_head (queue, gst_buffer_ref (*buffer));
313 gst_rtp_rtx_queue_chain_list (GstPad * pad, GstObject * parent,
314 GstBufferList * list)
320 rtx = GST_RTP_RTX_QUEUE (parent);
322 g_mutex_lock (&rtx->lock);
323 gst_buffer_list_foreach (list, push_to_queue, rtx->queue);
326 pending = rtx->pending;
328 g_mutex_unlock (&rtx->lock);
330 pending = g_list_reverse (pending);
331 g_list_foreach (pending, (GFunc) do_push, rtx);
332 g_list_free (pending);
334 ret = gst_pad_push_list (rtx->srcpad, list);
340 gst_rtp_rtx_queue_get_property (GObject * object,
341 guint prop_id, GValue * value, GParamSpec * pspec)
343 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
346 case PROP_MAX_SIZE_TIME:
347 g_value_set_uint (value, rtx->max_size_time);
349 case PROP_MAX_SIZE_PACKETS:
350 g_value_set_uint (value, rtx->max_size_packets);
353 g_value_set_uint (value, rtx->n_requests);
355 case PROP_FULFILLED_REQUESTS:
356 g_value_set_uint (value, rtx->n_fulfilled_requests);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gst_rtp_rtx_queue_set_property (GObject * object,
366 guint prop_id, const GValue * value, GParamSpec * pspec)
368 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
371 case PROP_MAX_SIZE_TIME:
372 rtx->max_size_time = g_value_get_uint (value);
374 case PROP_MAX_SIZE_PACKETS:
375 rtx->max_size_packets = g_value_get_uint (value);
378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383 static GstStateChangeReturn
384 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
386 GstStateChangeReturn ret;
389 rtx = GST_RTP_RTX_QUEUE (element);
391 switch (transition) {
397 GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
400 switch (transition) {
401 case GST_STATE_CHANGE_PAUSED_TO_READY:
402 gst_rtp_rtx_queue_reset (rtx, TRUE);
412 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
414 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
415 "rtp retransmission queue");
417 return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
418 GST_TYPE_RTP_RTX_QUEUE);