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,
51 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("application/x-rtp")
57 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-rtp")
63 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
65 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
68 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
69 element, GstStateChange transition);
71 static void gst_rtp_rtx_queue_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_rtp_rtx_queue_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
75 static void gst_rtp_rtx_queue_finalize (GObject * object);
77 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
80 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
82 GObjectClass *gobject_class;
83 GstElementClass *gstelement_class;
85 gobject_class = (GObjectClass *) klass;
86 gstelement_class = (GstElementClass *) klass;
88 gobject_class->get_property = gst_rtp_rtx_queue_get_property;
89 gobject_class->set_property = gst_rtp_rtx_queue_set_property;
90 gobject_class->finalize = gst_rtp_rtx_queue_finalize;
92 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
93 g_param_spec_uint ("max-size-time", "Max Size Times",
94 "Amount of ms to queue (0 = unlimited)", 0, G_MAXUINT,
95 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_PACKETS,
98 g_param_spec_uint ("max-size-packets", "Max Size Packets",
99 "Amount of packets to queue (0 = unlimited)", 0, G_MAXUINT,
100 DEFAULT_MAX_SIZE_PACKETS,
101 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
103 gst_element_class_add_pad_template (gstelement_class,
104 gst_static_pad_template_get (&src_factory));
105 gst_element_class_add_pad_template (gstelement_class,
106 gst_static_pad_template_get (&sink_factory));
108 gst_element_class_set_static_metadata (gstelement_class,
109 "RTP Retransmission Queue", "Codec",
110 "Keep RTP packets in a queue for retransmission",
111 "Wim Taymans <wim.taymans@gmail.com>");
113 gstelement_class->change_state =
114 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
118 gst_rtp_rtx_queue_reset (GstRTPRtxQueue * rtx, gboolean full)
120 g_mutex_lock (&rtx->lock);
121 g_queue_foreach (rtx->queue, (GFunc) gst_buffer_unref, NULL);
122 g_queue_clear (rtx->queue);
123 g_list_foreach (rtx->pending, (GFunc) gst_buffer_unref, NULL);
124 g_list_free (rtx->pending);
126 g_mutex_unlock (&rtx->lock);
130 gst_rtp_rtx_queue_finalize (GObject * object)
132 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
134 gst_rtp_rtx_queue_reset (rtx, TRUE);
135 g_queue_free (rtx->queue);
136 g_mutex_clear (&rtx->lock);
138 G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
142 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
144 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
147 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
149 GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
150 GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
151 gst_pad_set_event_function (rtx->srcpad,
152 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_src_event));
153 gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
156 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
158 GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
159 GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
160 gst_pad_set_chain_function (rtx->sinkpad,
161 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain));
162 gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
164 rtx->queue = g_queue_new ();
165 g_mutex_init (&rtx->lock);
167 rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
168 rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
179 push_seqnum (GstBuffer * buffer, RTXData * data)
181 GstRTPRtxQueue *rtx = data->rtx;
182 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
188 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
191 seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
192 gst_rtp_buffer_unmap (&rtpbuffer);
194 if (seqnum == data->seqnum) {
196 GST_DEBUG_OBJECT (rtx, "found %d", seqnum);
197 rtx->pending = g_list_prepend (rtx->pending, gst_buffer_ref (buffer));
202 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
204 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
207 switch (GST_EVENT_TYPE (event)) {
208 case GST_EVENT_CUSTOM_UPSTREAM:
210 const GstStructure *s;
212 s = gst_event_get_structure (event);
213 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
217 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
220 GST_DEBUG_OBJECT (rtx, "request %d", seqnum);
222 g_mutex_lock (&rtx->lock);
224 data.seqnum = seqnum;
226 g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
227 g_mutex_unlock (&rtx->lock);
229 gst_event_unref (event);
232 res = gst_pad_event_default (pad, parent, event);
237 res = gst_pad_event_default (pad, parent, event);
244 do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
246 gst_pad_push (rtx->srcpad, buffer);
250 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
256 rtx = GST_RTP_RTX_QUEUE (parent);
258 g_mutex_lock (&rtx->lock);
259 g_queue_push_head (rtx->queue, gst_buffer_ref (buffer));
261 if (rtx->max_size_packets) {
262 while (g_queue_get_length (rtx->queue) > rtx->max_size_packets)
263 gst_buffer_unref (g_queue_pop_tail (rtx->queue));
266 pending = rtx->pending;
268 g_mutex_unlock (&rtx->lock);
270 g_list_foreach (pending, (GFunc) do_push, rtx);
271 g_list_free (pending);
273 ret = gst_pad_push (rtx->srcpad, buffer);
279 gst_rtp_rtx_queue_get_property (GObject * object,
280 guint prop_id, GValue * value, GParamSpec * pspec)
282 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
285 case PROP_MAX_SIZE_TIME:
286 g_value_set_uint (value, rtx->max_size_time);
288 case PROP_MAX_SIZE_PACKETS:
289 g_value_set_uint (value, rtx->max_size_packets);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 gst_rtp_rtx_queue_set_property (GObject * object,
299 guint prop_id, const GValue * value, GParamSpec * pspec)
301 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
304 case PROP_MAX_SIZE_TIME:
305 rtx->max_size_time = g_value_get_uint (value);
307 case PROP_MAX_SIZE_PACKETS:
308 rtx->max_size_packets = g_value_get_uint (value);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 static GstStateChangeReturn
317 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
319 GstStateChangeReturn ret;
322 rtx = GST_RTP_RTX_QUEUE (element);
324 switch (transition) {
330 GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
333 switch (transition) {
334 case GST_STATE_CHANGE_PAUSED_TO_READY:
335 gst_rtp_rtx_queue_reset (rtx, TRUE);
345 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
347 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
348 "rtp retransmission queue");
350 return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
351 GST_TYPE_RTP_RTX_QUEUE);