rtxqueue: add retransmission queue element
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtprtxqueue.c
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
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <gst/gst.h>
32 #include <gst/rtp/gstrtpbuffer.h>
33 #include <string.h>
34
35 #include "gstrtprtxqueue.h"
36
37 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_queue_debug);
38 #define GST_CAT_DEFAULT gst_rtp_rtx_queue_debug
39
40 enum
41 {
42   PROP_0,
43   PROP_LAST
44 };
45
46 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp")
50     );
51
52 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("application/x-rtp")
56     );
57
58 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
59     GstEvent * event);
60 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
61     GstBuffer * buffer);
62
63 static GstStateChangeReturn gst_rtp_rtx_queue_change_state (GstElement *
64     element, GstStateChange transition);
65
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);
71
72 G_DEFINE_TYPE (GstRTPRtxQueue, gst_rtp_rtx_queue, GST_TYPE_ELEMENT);
73
74 static void
75 gst_rtp_rtx_queue_class_init (GstRTPRtxQueueClass * klass)
76 {
77   GObjectClass *gobject_class;
78   GstElementClass *gstelement_class;
79
80   gobject_class = (GObjectClass *) klass;
81   gstelement_class = (GstElementClass *) klass;
82
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));
87
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>");
92
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;
96
97   gstelement_class->change_state =
98       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_change_state);
99 }
100
101 static void
102 gst_rtp_rtx_queue_finalize (GObject * object)
103 {
104   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (object);
105
106   g_queue_free (rtx->queue);
107
108   G_OBJECT_CLASS (gst_rtp_rtx_queue_parent_class)->finalize (object);
109 }
110
111 static void
112 gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
113 {
114   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
115
116   rtx->srcpad =
117       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
118           "src"), "src");
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);
124
125   rtx->sinkpad =
126       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
127           "sink"), "sink");
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);
133
134
135   rtx->queue = g_queue_new ();
136 }
137
138 typedef struct
139 {
140   GstRTPRtxQueue *rtx;
141   guint seqnum;
142 } RTXData;
143
144 static void
145 push_seqnum (GstBuffer * buffer, RTXData * data)
146 {
147   GstRTPRtxQueue *rtx = data->rtx;
148   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
149   guint16 seqnum;
150
151   if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
152     return;
153
154   seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
155   gst_rtp_buffer_unmap (&rtpbuffer);
156
157   if (seqnum == data->seqnum) {
158     gst_pad_push (rtx->srcpad, gst_buffer_ref (buffer));
159   }
160 }
161
162 static gboolean
163 gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
164 {
165   GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
166   gboolean res;
167
168   switch (GST_EVENT_TYPE (event)) {
169     case GST_EVENT_CUSTOM_UPSTREAM:
170     {
171       const GstStructure *s;
172
173       s = gst_event_get_structure (event);
174       if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
175         guint seqnum;
176         RTXData data;
177
178         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
179           seqnum = -1;
180
181         data.rtx = rtx;
182         data.seqnum = seqnum;
183         g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
184         gst_event_unref (event);
185         res = TRUE;
186       } else {
187         res = gst_pad_event_default (pad, parent, event);
188       }
189       break;
190     }
191     default:
192       res = gst_pad_event_default (pad, parent, event);
193       break;
194   }
195   return res;
196 }
197
198 static GstFlowReturn
199 gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
200 {
201   GstRTPRtxQueue *rtx;
202   GstFlowReturn ret;
203
204   rtx = GST_RTP_RTX_QUEUE (parent);
205
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));
209   }
210
211   ret = gst_pad_push (rtx->srcpad, buffer);
212
213   return ret;
214 }
215
216 static void
217 gst_rtp_rtx_queue_get_property (GObject * object,
218     guint prop_id, GValue * value, GParamSpec * pspec)
219 {
220   switch (prop_id) {
221     default:
222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223       break;
224   }
225 }
226
227 static void
228 gst_rtp_rtx_queue_set_property (GObject * object,
229     guint prop_id, const GValue * value, GParamSpec * pspec)
230 {
231   switch (prop_id) {
232     default:
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234       break;
235   }
236 }
237
238 static GstStateChangeReturn
239 gst_rtp_rtx_queue_change_state (GstElement * element, GstStateChange transition)
240 {
241   GstStateChangeReturn ret;
242
243   switch (transition) {
244     default:
245       break;
246   }
247
248   ret =
249       GST_ELEMENT_CLASS (gst_rtp_rtx_queue_parent_class)->change_state (element,
250       transition);
251
252   switch (transition) {
253     default:
254       break;
255   }
256
257   return ret;
258 }
259
260 gboolean
261 gst_rtp_rtx_queue_plugin_init (GstPlugin * plugin)
262 {
263   GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_queue_debug, "rtprtxqueue", 0,
264       "rtp retransmission queue");
265
266   return gst_element_register (plugin, "rtprtxqueue", GST_RANK_NONE,
267       GST_TYPE_RTP_RTX_QUEUE);
268 }