-
Notifications
You must be signed in to change notification settings - Fork 8
/
gstrtpfecdec.c
442 lines (334 loc) · 12 KB
/
gstrtpfecdec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* RTP forward error correction encoding plugin for GStreamer
*
* Copyright (C) 2012 Carlos Rafael Giani <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <assert.h>
#include <gst/rtp/gstrtpbuffer.h>
#include "gstrtpfecdec.h"
/**** Debugging ****/
GST_DEBUG_CATEGORY_STATIC(rtpfecdec_debug);
#define GST_CAT_DEFAULT rtpfecdec_debug
/**** Typedefs ****/
typedef enum
{
BUFFER_TYPE_MEDIA,
BUFFER_TYPE_FEC
}
packet_types;
/**** Constants ****/
enum
{
PROP_0 = 0, /* GStreamer disallows properties with id 0 -> using dummy enum to prevent 0 */
PROP_NUM_MEDIA_PACKETS,
PROP_NUM_FEC_PACKETS
};
enum
{
DEFAULT_NUM_MEDIA_PACKETS = 9,
DEFAULT_NUM_FEC_PACKETS = 3
};
/**** Function declarations ****/
/* Handles incoming media and FEC packets, pushing them to the decoder and retrieving recoverd packets */
static GstFlowReturn gst_rtp_fec_dec_handle_incoming_packet(GstRtpFECDec *rtp_fec_dec, GstBuffer *packet, packet_types const packet_type);
/* This function is invoked when the sink pad receives data (media packets) */
static GstFlowReturn gst_rtp_fec_dec_chain_media(GstPad *pad, GstBuffer *packet);
/* This function is invoked when the fec pad receives data (fec packets) */
static GstFlowReturn gst_rtp_fec_dec_chain_fec(GstPad *pad, GstBuffer *packet);
/* Property accessors */
static void gst_rtp_fec_dec_set_property(GObject *object, guint prop_id, GValue const *value, GParamSpec *pspec);
static void gst_rtp_fec_dec_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
/* Called when a packet is about to be recovered and needs a buffer */
static GstBuffer* gst_rtp_fec_dec_create_recovered_buffer(guint const size_in_bytes, void *data);
/* Called when the pipeline state changes */
static GstStateChangeReturn gst_rtp_fec_dec_change_state(GstElement *element, GstStateChange transition);
/* Finalizer; cleans up states */
static void gst_rtp_fec_dec_finalize(GObject *object);
/**** GStreamer boilerplate ****/
GST_BOILERPLATE(GstRtpFECDec, gst_rtp_fec_dec, GstElement, GST_TYPE_ELEMENT)
/**** Pads ****/
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
"sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS("application/x-rtp")
);
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
"src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS("application/x-rtp")
);
static GstStaticPadTemplate fec_template = GST_STATIC_PAD_TEMPLATE(
"fec",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS(
"application/x-rtp,"
"media = (string) { \"video\", \"audio\", \"application\" }, "
"payload = (int) [ 96, 127 ], "
"clock-rate = (int) [ 1, MAX ], "
"encoding-name = (string) \"parityfec\""
)
);
/**** Function definition ****/
static void gst_rtp_fec_dec_base_init(gpointer klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
gst_element_class_set_details_simple(
element_class,
"RTP forward error correction depayloader",
"Codec/Payloader/Network/RTP",
"Restores lost RTP packets using forward error correction",
"Carlos Rafael Giani <[email protected]>"
);
gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sink_template));
gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&src_template));
gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&fec_template));
}
static void gst_rtp_fec_dec_class_init(GstRtpFECDecClass *klass)
{
GObjectClass *object_class;
GstElementClass *element_class;
GST_DEBUG_CATEGORY_INIT(rtpfecdec_debug, "rtpfecdec", 0, "RTP FEC depayloader");
object_class = G_OBJECT_CLASS(klass);
element_class = GST_ELEMENT_CLASS(klass);
/* Set functions */
object_class->finalize = GST_DEBUG_FUNCPTR(gst_rtp_fec_dec_finalize);
element_class->change_state = GST_DEBUG_FUNCPTR(gst_rtp_fec_dec_change_state);
object_class->set_property = GST_DEBUG_FUNCPTR(gst_rtp_fec_dec_set_property);
object_class->get_property = GST_DEBUG_FUNCPTR(gst_rtp_fec_dec_get_property);
/* Install properties */
g_object_class_install_property(
object_class,
PROP_NUM_MEDIA_PACKETS,
g_param_spec_uint(
"num-media-packets",
"Number of media packets",
"Number of media packets to expect for FEC packet generation",
1, 24,
DEFAULT_NUM_MEDIA_PACKETS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
)
);
g_object_class_install_property(
object_class,
PROP_NUM_FEC_PACKETS,
g_param_spec_uint(
"num-fec-packets",
"Number of FEC packets",
"Number of forward error correction packets to expect",
1, 24,
DEFAULT_NUM_FEC_PACKETS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
)
);
}
static void gst_rtp_fec_dec_init(GstRtpFECDec *rtp_fec_dec, GstRtpFECDecClass *klass)
{
GstElement *element;
klass = klass;
element = GST_ELEMENT(rtp_fec_dec);
/* Create pads out of the templates defined earlier */
rtp_fec_dec->sinkpad = gst_pad_new_from_static_template(&sink_template, "sink");
rtp_fec_dec->srcpad = gst_pad_new_from_static_template(&src_template, "src");
rtp_fec_dec->fecpad = gst_pad_new_from_static_template(&fec_template, "fec");
/* Set chain functions for sink and fec pads */
gst_pad_set_chain_function(rtp_fec_dec->sinkpad, gst_rtp_fec_dec_chain_media);
gst_pad_set_chain_function(rtp_fec_dec->fecpad, gst_rtp_fec_dec_chain_fec);
/* Add the pads to the element */
gst_element_add_pad(element, rtp_fec_dec->sinkpad);
gst_element_add_pad(element, rtp_fec_dec->srcpad);
gst_element_add_pad(element, rtp_fec_dec->fecpad);
/* Initialize the mutex */
rtp_fec_dec->mutex = g_mutex_new();
/* Finally, create the FEC decoder */
rtp_fec_dec->dec = fec_dec_create(DEFAULT_NUM_MEDIA_PACKETS, DEFAULT_NUM_FEC_PACKETS, gst_rtp_fec_dec_create_recovered_buffer, rtp_fec_dec);
}
static GstFlowReturn gst_rtp_fec_dec_handle_incoming_packet(GstRtpFECDec *rtp_fec_dec, GstBuffer *packet, packet_types const packet_type)
{
switch (packet_type)
{
case BUFFER_TYPE_FEC:
fec_dec_push_fec_packet(rtp_fec_dec->dec, packet);
/*
fec_dec_push_fec_packet() refs the packet, and since the packet is not needed by
anybody else, unref it here
*/
gst_buffer_unref(packet);
break;
case BUFFER_TYPE_MEDIA:
{
GstFlowReturn ret;
fec_dec_push_media_packet(rtp_fec_dec->dec, packet);
/*
unlike with the fec packet, the media packet is not unref'd here,
instead it is pushed downstream - another element might need it
*/
ret = gst_pad_push(rtp_fec_dec->srcpad, packet);
if (ret != GST_FLOW_OK)
return ret;
break;
}
default:
assert(0);
}
while (fec_dec_has_recovered_packets(rtp_fec_dec->dec))
{
GstFlowReturn ret;
guint16 seqnum;
GstBuffer *recovered_packet;
recovered_packet = fec_dec_pop_recovered_packet(rtp_fec_dec->dec);
seqnum = gst_rtp_buffer_get_seq(recovered_packet);
GST_DEBUG_OBJECT(rtp_fec_dec, "pushing recovered RTP media packet, seqnum %u", seqnum);
ret = gst_pad_push(rtp_fec_dec->srcpad, recovered_packet);
if (ret != GST_FLOW_OK)
{
GST_ERROR_OBJECT(rtp_fec_dec, "Could not push RTP media packet, flushing remaining packets");
fec_dec_flush_recovered_packets(rtp_fec_dec->dec);
return ret;
}
}
return GST_FLOW_OK;
}
static GstFlowReturn gst_rtp_fec_dec_chain_media(GstPad *pad, GstBuffer *packet)
{
GstRtpFECDec *rtp_fec_dec;
guint16 seqnum;
GstFlowReturn ret;
rtp_fec_dec = GST_RTP_FEC_DEC(gst_pad_get_parent(pad));
g_mutex_lock(rtp_fec_dec->mutex);
seqnum = gst_rtp_buffer_get_seq(packet);
GST_DEBUG_OBJECT(rtp_fec_dec, "received RTP media packet, seqnum %u", seqnum);
ret = gst_rtp_fec_dec_handle_incoming_packet(rtp_fec_dec, packet, BUFFER_TYPE_MEDIA);
g_mutex_unlock(rtp_fec_dec->mutex);
gst_object_unref(rtp_fec_dec);
return ret;
}
static GstFlowReturn gst_rtp_fec_dec_chain_fec(GstPad *pad, GstBuffer *packet)
{
GstRtpFECDec *rtp_fec_dec;
guint16 seqnum;
GstFlowReturn ret;
rtp_fec_dec = GST_RTP_FEC_DEC(gst_pad_get_parent(pad));
g_mutex_lock(rtp_fec_dec->mutex);
seqnum = gst_rtp_buffer_get_seq(packet);
GST_DEBUG_OBJECT(rtp_fec_dec, "received RTP FEC packet, seqnum %u", seqnum);
ret = gst_rtp_fec_dec_handle_incoming_packet(rtp_fec_dec, packet, BUFFER_TYPE_FEC);
g_mutex_unlock(rtp_fec_dec->mutex);
gst_object_unref(rtp_fec_dec);
return ret;
}
static void gst_rtp_fec_dec_set_property(GObject *object, guint prop_id, GValue const *value, GParamSpec *pspec)
{
GstRtpFECDec *rtp_fec_dec;
GST_OBJECT_LOCK(object);
rtp_fec_dec = GST_RTP_FEC_DEC(object);
switch (prop_id)
{
case PROP_NUM_MEDIA_PACKETS:
{
guint num_media_packets = g_value_get_uint(value);
GST_DEBUG_OBJECT(rtp_fec_dec, "Set number of media packets to %u", num_media_packets);
g_mutex_lock(rtp_fec_dec->mutex);
fec_dec_set_num_media_packets(rtp_fec_dec->dec, num_media_packets);
g_mutex_unlock(rtp_fec_dec->mutex);
break;
}
case PROP_NUM_FEC_PACKETS:
{
guint num_fec_packets = g_value_get_uint(value);
GST_DEBUG_OBJECT(rtp_fec_dec, "Set number of FEC packets to %u", num_fec_packets);
g_mutex_lock(rtp_fec_dec->mutex);
fec_dec_set_num_fec_packets(rtp_fec_dec->dec, num_fec_packets);
g_mutex_unlock(rtp_fec_dec->mutex);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
GST_OBJECT_UNLOCK(object);
}
static void gst_rtp_fec_dec_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
GstRtpFECDec *rtp_fec_dec;
GST_OBJECT_LOCK(object);
rtp_fec_dec = GST_RTP_FEC_DEC(object);
switch (prop_id)
{
case PROP_NUM_MEDIA_PACKETS:
g_value_set_uint(value, fec_dec_get_num_media_packets(rtp_fec_dec->dec));
break;
case PROP_NUM_FEC_PACKETS:
g_value_set_uint(value, fec_dec_get_num_fec_packets(rtp_fec_dec->dec));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
GST_OBJECT_UNLOCK(object);
}
static GstBuffer* gst_rtp_fec_dec_create_recovered_buffer(guint const size_in_bytes, void *data)
{
GstRtpFECDec *rtp_fec_dec;
GstBuffer *buffer;
GstFlowReturn ret;
rtp_fec_dec = (GstRtpFECDec*)data;
ret = gst_pad_alloc_buffer(rtp_fec_dec->srcpad, 0, size_in_bytes, GST_PAD_CAPS(rtp_fec_dec->srcpad), &buffer);
if (ret != GST_FLOW_OK)
{
buffer = gst_buffer_new_and_alloc(size_in_bytes);
gst_buffer_set_caps(buffer, GST_PAD_CAPS(rtp_fec_dec->srcpad));
GST_DEBUG_OBJECT(rtp_fec_dec, "Created new buffer with %u bytes for recovered packet using gst_buffer_new_and_alloc()", size_in_bytes);
}
else
{
GST_DEBUG_OBJECT(rtp_fec_dec, "Created new buffer with %u bytes for recovered packet", size_in_bytes);
}
return buffer;
}
static GstStateChangeReturn gst_rtp_fec_dec_change_state(GstElement *element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstRtpFECDec *rtp_fec_dec;
rtp_fec_dec = GST_RTP_FEC_DEC(element);
ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
switch (transition)
{
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
g_mutex_lock(rtp_fec_dec->mutex);
fec_dec_reset(rtp_fec_dec->dec);
g_mutex_unlock(rtp_fec_dec->mutex);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
static void gst_rtp_fec_dec_finalize(GObject *object)
{
GstRtpFECDec *rtp_fec_dec = GST_RTP_FEC_DEC(object);
g_mutex_free(rtp_fec_dec->mutex);
fec_dec_destroy(rtp_fec_dec->dec);
GST_DEBUG_OBJECT(rtp_fec_dec, "Cleaned up FEC decoder");
G_OBJECT_CLASS(parent_class)->finalize(object);
}