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
46 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("application/x-rtp")
52 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("application/x-rtp")
58 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
60 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
63 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
64 element, GstStateChange transition);
66 static void gst_rtp_rtx_queue_set_property (GObject * object, guint prop_id,
67 const GValue * value, GParamSpec * pspec);
68 static void gst_rtp_rtx_queue_get_property (GObject * object, guint prop_id,
69 GValue * value, GParamSpec * pspec);
70 static void gst_rtp_rtx_queue_finalize (GObject * object);
72 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
75 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
77 GObjectClass *gobject_class;
78 GstElementClass *gstelement_class;
80 gobject_class = (GObjectClass *) klass;
81 gstelement_class = (GstElementClass *) klass;
83 gst_element_class_add_pad_template (gstelement_class,
84 gst_static_pad_template_get (&src_factory));
85 gst_element_class_add_pad_template (gstelement_class,
86 gst_static_pad_template_get (&sink_factory));
88 gst_element_class_set_static_metadata (gstelement_class,
89 "RTP Retransmission Queue", "Codec",
90 "Keep RTP packets in a queue for retransmission",
91 "Wim Taymans <wim.taymans@gmail.com>");
93 gobject_class->get_property = gst_rtp_rtx_queue_get_property;
94 gobject_class->set_property = gst_rtp_rtx_queue_set_property;
95 gobject_class->finalize = gst_rtp_rtx_queue_finalize;
97 gstelement_class->change_state =
98 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
102 gst_rtp_rtx_queue_finalize (GObject * object)
104 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
106 g_queue_free (rtx->queue);
108 G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
112 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
114 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
117 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
119 GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
120 GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
121 gst_pad_set_event_function (rtx->srcpad,
122 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_src_event));
123 gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
126 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
128 GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
129 GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
130 gst_pad_set_chain_function (rtx->sinkpad,
131 GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain));
132 gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
135 rtx->queue = g_queue_new ();
145 push_seqnum (GstBuffer * buffer, RTXData * data)
147 GstRTPRtxQueue *rtx = data->rtx;
148 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
151 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
154 seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
155 gst_rtp_buffer_unmap (&rtpbuffer);
157 if (seqnum == data->seqnum) {
158 gst_pad_push (rtx->srcpad, gst_buffer_ref (buffer));
163 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
165 GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
168 switch (GST_EVENT_TYPE (event)) {
169 case GST_EVENT_CUSTOM_UPSTREAM:
171 const GstStructure *s;
173 s = gst_event_get_structure (event);
174 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
178 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
182 data.seqnum = seqnum;
183 g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
184 gst_event_unref (event);
187 res = gst_pad_event_default (pad, parent, event);
192 res = gst_pad_event_default (pad, parent, event);
199 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
204 rtx = GST_RTP_RTX_QUEUE (parent);
206 g_queue_push_head (rtx->queue, gst_buffer_ref (buffer));
207 while (g_queue_get_length (rtx->queue) > 100) {
208 gst_buffer_unref (g_queue_pop_tail (rtx->queue));
211 ret = gst_pad_push (rtx->srcpad, buffer);
217 gst_rtp_rtx_queue_get_property (GObject * object,
218 guint prop_id, GValue * value, GParamSpec * pspec)
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 gst_rtp_rtx_queue_set_property (GObject * object,
229 guint prop_id, const GValue * value, GParamSpec * pspec)
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 static GstStateChangeReturn
239 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
241 GstStateChangeReturn ret;
243 switch (transition) {
249 GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
252 switch (transition) {
261 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
263 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
264 "rtp retransmission queue");
266 return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
267 GST_TYPE_RTP_RTX_QUEUE);