rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpulpfecenc.c
1 /* GStreamer plugin for forward error correction
2  * Copyright (C) 2017 Pexip
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Mikhail Fludkov <misha@pexip.com>
19  */
20
21 /**
22  * SECTION:element-rtpulpfecenc
23  * @short_description: Generic RTP Forward Error Correction (FEC) encoder
24  * @title: rtpulpfecenc
25  *
26  * Generic Forward Error Correction (FEC) encoder using Uneven Level
27  * Protection (ULP) as described in RFC 5109.
28  *
29  * It differs from the RFC in one important way, it multiplexes the
30  * FEC packets in the same sequence number as media packets. This is to be
31  * compatible with libwebrtc as using in Google Chrome and with Microsoft
32  * Lync / Skype for Business.
33  *
34  * Be warned that after using this element, it is no longer possible to know if
35  * there is a gap in the media stream based on the sequence numbers as the FEC
36  * packets become interleaved with the media packets.
37  *
38  * This element will insert protection packets in any RTP stream, which
39  * can then be used on the receiving side to recover lost packets.
40  *
41  * This element rewrites packets' seqnums, which means that when combined
42  * with retransmission elements such as #GstRtpRtxSend, it *must* be
43  * placed upstream of those, otherwise retransmission requests will request
44  * incorrect seqnums.
45  *
46  * A payload type for the protection packets *must* be specified, different
47  * from the payload type of the protected packets, with the GstRtpUlpFecEnc:pt
48  * property.
49  *
50  * The marker bit of RTP packets is used to determine sets of packets to
51  * protect as a unit, in order to modulate the level of protection, this
52  * behaviour can be disabled with GstRtpUlpFecEnc:multipacket, but should
53  * be left enabled for video streams.
54  *
55  * The level of protection can be configured with two properties,
56  * #GstRtpUlpFecEnc:percentage and #GstRtpUlpFecEnc:percentage-important,
57  * the element will determine which percentage to use for a given set of
58  * packets based on the presence of the #GST_BUFFER_FLAG_NON_DROPPABLE
59  * flag, upstream payloaders are expected to set this flag on "important"
60  * packets such as those making up a keyframe.
61  *
62  * The percentage is expressed not in terms of bytes, but in terms of
63  * packets, this for implementation convenience. The drawback with this
64  * approach is that when using a percentage different from 100 %, and a
65  * low bitrate, entire frames may be contained in a single packet, leading
66  * to some packets not being protected, thus lowering the overall recovery
67  * rate on the receiving side.
68  *
69  * When using #GstRtpBin, this element should be inserted through the
70  * #GstRtpBin::request-fec-encoder signal.
71  *
72  * ## Example pipeline
73  *
74  * |[
75  * gst-launch-1.0 videotestsrc ! x264enc ! video/x-h264, profile=baseline ! rtph264pay pt=96 ! rtpulpfecenc percentage=100 pt=122 ! udpsink port=8888
76  * ]| This example will receive a stream with FEC and try to reconstruct the packets.
77  *
78  * Example programs are available at
79  * <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/blob/master/examples/src/bin/rtpfecserver.rs>
80  * and
81  * <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/blob/master/examples/src/bin/rtpfecclient.rs>
82  *
83  * See also: #GstRtpUlpFecDec, #GstRtpBin
84  * Since: 1.14
85  */
86
87 #include <gst/rtp/gstrtp-enumtypes.h>
88 #include <gst/rtp/gstrtpbuffer.h>
89 #include <string.h>
90
91 #include "rtpulpfeccommon.h"
92 #include "gstrtpulpfecenc.h"
93
94 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
95     GST_PAD_SINK,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS ("application/x-rtp"));
98
99 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
100     GST_PAD_SRC,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS ("application/x-rtp"));
103
104 #define UNDEF_PT                255
105
106 #define DEFAULT_PT              UNDEF_PT
107 #define DEFAULT_PCT             0
108 #define DEFAULT_PCT_IMPORTANT   0
109 #define DEFAULT_MULTIPACKET     TRUE
110
111 #define PACKETS_BUF_MAX_LENGTH  (RTP_ULPFEC_PROTECTED_PACKETS_MAX(TRUE))
112
113 GST_DEBUG_CATEGORY (gst_rtp_ulpfec_enc_debug);
114 #define GST_CAT_DEFAULT (gst_rtp_ulpfec_enc_debug)
115
116 G_DEFINE_TYPE (GstRtpUlpFecEnc, gst_rtp_ulpfec_enc, GST_TYPE_ELEMENT);
117
118 enum
119 {
120   PROP_0,
121   PROP_PT,
122   PROP_MULTIPACKET,
123   PROP_PROTECTED,
124   PROP_PERCENTAGE,
125   PROP_PERCENTAGE_IMPORTANT,
126 };
127
128 #define RTP_FEC_MAP_INFO_NTH(ctx, data) (&g_array_index (\
129     ((GstRtpUlpFecEncStreamCtx *)ctx)->info_arr, \
130     RtpUlpFecMapInfo, \
131     GPOINTER_TO_UINT(data)))
132
133 static void
134 gst_rtp_ulpfec_enc_stream_ctx_start (GstRtpUlpFecEncStreamCtx * ctx,
135     GQueue * packets, guint fec_packets)
136 {
137   GList *it = packets->tail;
138   guint i;
139
140   g_array_set_size (ctx->info_arr, packets->length);
141
142   for (i = 0; i < packets->length; ++i) {
143     GstBuffer *buffer = it->data;
144     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (ctx, i);
145
146     if (!rtp_ulpfec_map_info_map (gst_buffer_ref (buffer), info))
147       g_assert_not_reached ();
148
149     GST_LOG_RTP_PACKET (ctx->parent, "rtp header (incoming)", &info->rtp);
150
151     it = g_list_previous (it);
152   }
153
154   ctx->fec_packets = fec_packets;
155   ctx->fec_packet_idx = 0;
156 }
157
158 static void
159 gst_rtp_ulpfec_enc_stream_ctx_stop (GstRtpUlpFecEncStreamCtx * ctx)
160 {
161   g_array_set_size (ctx->info_arr, 0);
162   g_array_set_size (ctx->scratch_buf, 0);
163
164   ctx->fec_packets = 0;
165   ctx->fec_packet_idx = 0;
166 }
167
168 static void
169     gst_rtp_ulpfec_enc_stream_ctx_get_protection_parameters
170     (GstRtpUlpFecEncStreamCtx * ctx, guint16 * dst_seq_base, guint64 * dst_mask,
171     guint * dst_start, guint * dst_end)
172 {
173   guint media_packets = ctx->info_arr->len;
174   guint start = ctx->fec_packet_idx * media_packets / ctx->fec_packets;
175   guint end =
176       ((ctx->fec_packet_idx + 1) * media_packets + ctx->fec_packets -
177       1) / ctx->fec_packets - 1;
178   guint len = end - start + 1;
179   guint64 mask = 0;
180   guint16 seq_base = 0;
181   guint i;
182
183   len = MIN (len, RTP_ULPFEC_PROTECTED_PACKETS_MAX (TRUE));
184   end = start + len - 1;
185
186   for (i = start; i <= end; ++i) {
187     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (ctx, i);
188     guint16 seq = gst_rtp_buffer_get_seq (&info->rtp);
189
190     if (mask) {
191       gint diff = gst_rtp_buffer_compare_seqnum (seq_base, seq);
192       if (diff < 0) {
193         seq_base = seq;
194         mask = mask >> (-diff);
195       }
196       mask |= rtp_ulpfec_packet_mask_from_seqnum (seq, seq_base, TRUE);
197     } else {
198       seq_base = seq;
199       mask = rtp_ulpfec_packet_mask_from_seqnum (seq, seq_base, TRUE);
200     }
201   }
202
203   *dst_start = start;
204   *dst_end = end;
205   *dst_mask = mask;
206   *dst_seq_base = seq_base;
207 }
208
209 static GstBuffer *
210 gst_rtp_ulpfec_enc_stream_ctx_protect (GstRtpUlpFecEncStreamCtx * ctx,
211     guint8 pt, guint16 seq, guint32 timestamp, guint32 ssrc)
212 {
213   guint end = 0;
214   guint start = 0;
215   guint64 fec_mask = 0;
216   guint16 seq_base = 0;
217   GstBuffer *ret;
218   guint64 tmp_mask;
219   gboolean fec_mask_long;
220   guint i;
221
222   if (ctx->fec_packet_idx >= ctx->fec_packets)
223     return NULL;
224
225   g_array_set_size (ctx->scratch_buf, 0);
226   gst_rtp_ulpfec_enc_stream_ctx_get_protection_parameters (ctx, &seq_base,
227       &fec_mask, &start, &end);
228
229   tmp_mask = fec_mask;
230   fec_mask_long = rtp_ulpfec_mask_is_long (fec_mask);
231   for (i = start; i <= end; ++i) {
232     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (ctx, i);
233     guint64 packet_mask =
234         rtp_ulpfec_packet_mask_from_seqnum (gst_rtp_buffer_get_seq (&info->rtp),
235         seq_base,
236         TRUE);
237
238     if (tmp_mask & packet_mask) {
239       tmp_mask ^= packet_mask;
240       rtp_buffer_to_ulpfec_bitstring (&info->rtp, ctx->scratch_buf, FALSE,
241           fec_mask_long);
242     }
243   }
244
245   g_assert (tmp_mask == 0);
246   ret =
247       rtp_ulpfec_bitstring_to_fec_rtp_buffer (ctx->scratch_buf, seq_base,
248       fec_mask_long, fec_mask, FALSE, pt, seq, timestamp, ssrc);
249   ++ctx->fec_packet_idx;
250   return ret;
251 }
252
253 static void
254 gst_rtp_ulpfec_enc_stream_ctx_report_budget (GstRtpUlpFecEncStreamCtx * ctx)
255 {
256   GST_TRACE_OBJECT (ctx->parent, "budget = %f budget_important = %f",
257       ctx->budget, ctx->budget_important);
258 }
259
260 static void
261 gst_rtp_ulpfec_enc_stream_ctx_increment_budget (GstRtpUlpFecEncStreamCtx * ctx,
262     GstBuffer * buffer)
263 {
264   if (ctx->percentage == 0 && ctx->percentage_important == 0) {
265     if (ctx->budget > 0) {
266       ctx->budget = 0;
267       ctx->budget_important = 0;
268     }
269     if (ctx->budget < 0)
270       ctx->budget += ctx->budget_inc;
271
272     return;
273   }
274   ctx->budget += ctx->budget_inc;
275
276   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_NON_DROPPABLE)) {
277     ctx->budget_important += ctx->budget_inc_important;
278   }
279
280   gst_rtp_ulpfec_enc_stream_ctx_report_budget (ctx);
281 }
282
283 static void
284 gst_rtp_ulpfec_enc_stream_ctx_decrement_budget (GstRtpUlpFecEncStreamCtx * ctx,
285     guint fec_packets_num)
286 {
287   if (ctx->budget_important >= 1.)
288     ctx->budget_important -= fec_packets_num;
289   ctx->budget -= fec_packets_num;
290
291   gst_rtp_ulpfec_enc_stream_ctx_report_budget (ctx);
292 }
293
294 static guint
295 gst_rtp_ulpfec_enc_stream_ctx_get_fec_packets_num (GstRtpUlpFecEncStreamCtx *
296     ctx)
297 {
298   g_assert_cmpfloat (ctx->budget_important, >=, 0.);
299
300   if (ctx->budget_important >= 1.)
301     return ctx->budget_important;
302   return ctx->budget > 0. ? (guint) ctx->budget : 0;
303 }
304
305 static void
306 gst_rtp_ulpfec_enc_stream_ctx_free_packets_buf (GstRtpUlpFecEncStreamCtx * ctx)
307 {
308   while (ctx->packets_buf.length)
309     gst_buffer_unref (g_queue_pop_tail (&ctx->packets_buf));
310 }
311
312 static void
313 gst_rtp_ulpfec_enc_stream_ctx_prepend_to_fec_buffer (GstRtpUlpFecEncStreamCtx *
314     ctx, GstRTPBuffer * rtp, guint buf_max_size)
315 {
316   GList *new_head;
317   if (ctx->packets_buf.length == buf_max_size) {
318     new_head = g_queue_pop_tail_link (&ctx->packets_buf);
319   } else {
320     new_head = g_list_alloc ();
321   }
322
323   gst_buffer_replace ((GstBuffer **) & new_head->data, rtp->buffer);
324   g_queue_push_head_link (&ctx->packets_buf, new_head);
325
326   g_assert_cmpint (ctx->packets_buf.length, <=, buf_max_size);
327 }
328
329 static GstFlowReturn
330 gst_rtp_ulpfec_enc_stream_ctx_push_fec_packets (GstRtpUlpFecEncStreamCtx * ctx,
331     guint8 pt, guint16 seq, guint32 timestamp, guint32 ssrc)
332 {
333   GstFlowReturn ret = GST_FLOW_OK;
334   guint fec_packets_num =
335       gst_rtp_ulpfec_enc_stream_ctx_get_fec_packets_num (ctx);
336
337   if (fec_packets_num) {
338     guint fec_packets_pushed = 0;
339     GstBuffer *latest_packet = ctx->packets_buf.head->data;
340     GstBuffer *fec = NULL;
341
342     gst_rtp_ulpfec_enc_stream_ctx_start (ctx, &ctx->packets_buf,
343         fec_packets_num);
344
345     while (NULL != (fec =
346             gst_rtp_ulpfec_enc_stream_ctx_protect (ctx, pt,
347                 seq + fec_packets_pushed, timestamp, ssrc))) {
348       gst_buffer_copy_into (fec, latest_packet, GST_BUFFER_COPY_TIMESTAMPS, 0,
349           -1);
350
351       ret = gst_pad_push (ctx->srcpad, fec);
352       if (GST_FLOW_OK == ret)
353         ++fec_packets_pushed;
354       else
355         break;
356     }
357
358     gst_rtp_ulpfec_enc_stream_ctx_stop (ctx);
359
360     g_assert_cmpint (fec_packets_pushed, <=, fec_packets_num);
361
362     ctx->num_packets_protected += ctx->packets_buf.length;
363     ctx->num_packets_fec += fec_packets_pushed;
364     ctx->seqnum_offset += fec_packets_pushed;
365     ctx->seqnum += fec_packets_pushed;
366   }
367
368   gst_rtp_ulpfec_enc_stream_ctx_decrement_budget (ctx, fec_packets_num);
369   return ret;
370 }
371
372 static void
373 gst_rtp_ulpfec_enc_stream_ctx_cache_packet (GstRtpUlpFecEncStreamCtx * ctx,
374     GstRTPBuffer * rtp, gboolean * dst_empty_packet_buffer,
375     gboolean * dst_push_fec)
376 {
377   if (ctx->multipacket) {
378     gst_rtp_ulpfec_enc_stream_ctx_prepend_to_fec_buffer (ctx, rtp,
379         PACKETS_BUF_MAX_LENGTH);
380     gst_rtp_ulpfec_enc_stream_ctx_increment_budget (ctx, rtp->buffer);
381
382     *dst_empty_packet_buffer = gst_rtp_buffer_get_marker (rtp);
383     *dst_push_fec = *dst_empty_packet_buffer;
384   } else {
385     gboolean push_fec;
386
387     gst_rtp_ulpfec_enc_stream_ctx_prepend_to_fec_buffer (ctx, rtp, 1);
388
389     push_fec = ctx->fec_nth == 0 ? FALSE :
390         0 == (ctx->num_packets_received % ctx->fec_nth);
391
392     ctx->budget = push_fec ? 1 : 0;
393     ctx->budget_important = 0;
394
395     *dst_push_fec = push_fec;
396     *dst_empty_packet_buffer = FALSE;
397   }
398 }
399
400 static void
401 gst_rtp_ulpfec_enc_stream_ctx_configure (GstRtpUlpFecEncStreamCtx * ctx,
402     guint pt, guint percentage, guint percentage_important,
403     gboolean multipacket)
404 {
405   ctx->pt = pt;
406   ctx->percentage = percentage;
407   ctx->percentage_important = percentage_important;
408   ctx->multipacket = multipacket;
409
410   ctx->fec_nth = percentage ? 100 / percentage : 0;
411   if (percentage) {
412     ctx->budget_inc = percentage / 100.;
413     ctx->budget_inc_important = percentage > percentage_important ?
414         ctx->budget_inc : percentage_important / 100.;
415   }
416 /*
417    else {
418     ctx->budget_inc = 0.0;
419   }
420 */
421   ctx->budget_inc_important = percentage > percentage_important ?
422       ctx->budget_inc : percentage_important / 100.;
423 }
424
425 static GstRtpUlpFecEncStreamCtx *
426 gst_rtp_ulpfec_enc_stream_ctx_new (guint ssrc,
427     GstElement * parent, GstPad * srcpad,
428     guint pt, guint percentage, guint percentage_important,
429     gboolean multipacket)
430 {
431   GstRtpUlpFecEncStreamCtx *ctx = g_new0 (GstRtpUlpFecEncStreamCtx, 1);
432
433   ctx->ssrc = ssrc;
434   ctx->parent = parent;
435   ctx->srcpad = srcpad;
436
437   ctx->seqnum = g_random_int_range (0, G_MAXUINT16 / 2);
438
439   ctx->info_arr = g_array_new (FALSE, TRUE, sizeof (RtpUlpFecMapInfo));
440   g_array_set_clear_func (ctx->info_arr,
441       (GDestroyNotify) rtp_ulpfec_map_info_unmap);
442   ctx->parent = parent;
443   ctx->scratch_buf = g_array_new (FALSE, TRUE, sizeof (guint8));
444   gst_rtp_ulpfec_enc_stream_ctx_configure (ctx, pt,
445       percentage, percentage_important, multipacket);
446
447   return ctx;
448 }
449
450 static void
451 gst_rtp_ulpfec_enc_stream_ctx_free (GstRtpUlpFecEncStreamCtx * ctx)
452 {
453   if (ctx->num_packets_received) {
454     GST_INFO_OBJECT (ctx->parent, "Actual FEC overhead is %4.2f%% (%u/%u)\n",
455         ctx->num_packets_fec * (double) 100. / ctx->num_packets_received,
456         ctx->num_packets_fec, ctx->num_packets_received);
457   }
458   gst_rtp_ulpfec_enc_stream_ctx_free_packets_buf (ctx);
459
460   g_assert (0 == ctx->info_arr->len);
461   g_array_free (ctx->info_arr, TRUE);
462   g_array_free (ctx->scratch_buf, TRUE);
463   g_slice_free1 (sizeof (GstRtpUlpFecEncStreamCtx), ctx);
464 }
465
466 static GstFlowReturn
467 gst_rtp_ulpfec_enc_stream_ctx_process (GstRtpUlpFecEncStreamCtx * ctx,
468     GstBuffer * buffer)
469 {
470   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
471   GstFlowReturn ret;
472   gboolean push_fec = FALSE;
473   gboolean empty_packet_buffer = FALSE;
474
475   ctx->num_packets_received++;
476
477   if (ctx->seqnum_offset > 0) {
478     buffer = gst_buffer_make_writable (buffer);
479     if (!gst_rtp_buffer_map (buffer,
480             GST_MAP_READWRITE | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtp))
481       g_assert_not_reached ();
482     gst_rtp_buffer_set_seq (&rtp,
483         gst_rtp_buffer_get_seq (&rtp) + ctx->seqnum_offset);
484   } else {
485     if (!gst_rtp_buffer_map (buffer,
486             GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtp))
487       g_assert_not_reached ();
488   }
489
490   gst_rtp_ulpfec_enc_stream_ctx_cache_packet (ctx, &rtp, &empty_packet_buffer,
491       &push_fec);
492
493   if (push_fec) {
494     guint32 fec_timestamp = gst_rtp_buffer_get_timestamp (&rtp);
495     guint32 fec_ssrc = gst_rtp_buffer_get_ssrc (&rtp);
496     guint16 fec_seq = gst_rtp_buffer_get_seq (&rtp) + 1;
497
498     gst_rtp_buffer_unmap (&rtp);
499
500     ret = gst_pad_push (ctx->srcpad, buffer);
501     if (GST_FLOW_OK == ret)
502       ret =
503           gst_rtp_ulpfec_enc_stream_ctx_push_fec_packets (ctx, ctx->pt, fec_seq,
504           fec_timestamp, fec_ssrc);
505   } else {
506     gst_rtp_buffer_unmap (&rtp);
507     ret = gst_pad_push (ctx->srcpad, buffer);
508   }
509
510   if (empty_packet_buffer)
511     gst_rtp_ulpfec_enc_stream_ctx_free_packets_buf (ctx);
512
513   return ret;
514 }
515
516 static GstRtpUlpFecEncStreamCtx *
517 gst_rtp_ulpfec_enc_aquire_ctx (GstRtpUlpFecEnc * fec, guint ssrc)
518 {
519   GstRtpUlpFecEncStreamCtx *ctx;
520
521   GST_OBJECT_LOCK (fec);
522   ctx = g_hash_table_lookup (fec->ssrc_to_ctx, GUINT_TO_POINTER (ssrc));
523   if (ctx == NULL) {
524     ctx =
525         gst_rtp_ulpfec_enc_stream_ctx_new (ssrc, GST_ELEMENT_CAST (fec),
526         fec->srcpad, fec->pt, fec->percentage,
527         fec->percentage_important, fec->multipacket);
528     g_hash_table_insert (fec->ssrc_to_ctx, GUINT_TO_POINTER (ssrc), ctx);
529   }
530   GST_OBJECT_UNLOCK (fec);
531
532   return ctx;
533 }
534
535 static GstFlowReturn
536 gst_rtp_ulpfec_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
537 {
538   GstRtpUlpFecEnc *fec = GST_RTP_ULPFEC_ENC (parent);
539   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
540   GstFlowReturn ret;
541   guint ssrc = 0;
542   GstRtpUlpFecEncStreamCtx *ctx;
543
544   if (fec->pt == UNDEF_PT)
545     return gst_pad_push (fec->srcpad, buffer);
546
547   /* FIXME: avoid this additional mapping of the buffer to get the
548      ssrc! */
549   if (!gst_rtp_buffer_map (buffer,
550           GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtp)) {
551     g_assert_not_reached ();
552   }
553   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
554   gst_rtp_buffer_unmap (&rtp);
555
556   ctx = gst_rtp_ulpfec_enc_aquire_ctx (fec, ssrc);
557
558   ret = gst_rtp_ulpfec_enc_stream_ctx_process (ctx, buffer);
559
560   /* FIXME: does not work for multiple ssrcs */
561   fec->num_packets_protected = ctx->num_packets_protected;
562
563   return ret;
564 }
565
566 static void
567 gst_rtp_ulpfec_enc_configure_ctx (gpointer key, gpointer value,
568     gpointer user_data)
569 {
570   GstRtpUlpFecEnc *fec = user_data;
571   GstRtpUlpFecEncStreamCtx *ctx = value;
572
573   gst_rtp_ulpfec_enc_stream_ctx_configure (ctx, fec->pt,
574       fec->percentage, fec->percentage_important, fec->multipacket);
575 }
576
577 static void
578 gst_rtp_ulpfec_enc_set_property (GObject * object, guint prop_id,
579     const GValue * value, GParamSpec * pspec)
580 {
581   GstRtpUlpFecEnc *fec = GST_RTP_ULPFEC_ENC (object);
582
583   switch (prop_id) {
584     case PROP_PT:
585       fec->pt = g_value_get_uint (value);
586       break;
587     case PROP_MULTIPACKET:
588       fec->multipacket = g_value_get_boolean (value);
589       break;
590     case PROP_PERCENTAGE:
591       fec->percentage = g_value_get_uint (value);
592       break;
593     case PROP_PERCENTAGE_IMPORTANT:
594       fec->percentage_important = g_value_get_uint (value);
595       break;
596     default:
597       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
598       break;
599   }
600
601   GST_OBJECT_LOCK (fec);
602   g_hash_table_foreach (fec->ssrc_to_ctx, gst_rtp_ulpfec_enc_configure_ctx,
603       fec);
604   GST_OBJECT_UNLOCK (fec);
605 }
606
607 static void
608 gst_rtp_ulpfec_enc_get_property (GObject * object, guint prop_id,
609     GValue * value, GParamSpec * pspec)
610 {
611   GstRtpUlpFecEnc *fec = GST_RTP_ULPFEC_ENC (object);
612   switch (prop_id) {
613     case PROP_PT:
614       g_value_set_uint (value, fec->pt);
615       break;
616     case PROP_PROTECTED:
617       g_value_set_uint (value, fec->num_packets_protected);
618       break;
619     case PROP_PERCENTAGE:
620       g_value_set_uint (value, fec->percentage);
621       break;
622     case PROP_PERCENTAGE_IMPORTANT:
623       g_value_set_uint (value, fec->percentage_important);
624       break;
625     case PROP_MULTIPACKET:
626       g_value_set_boolean (value, fec->multipacket);
627       break;
628     default:
629       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630       break;
631   }
632 }
633
634 static void
635 gst_rtp_ulpfec_enc_dispose (GObject * obj)
636 {
637   GstRtpUlpFecEnc *fec = GST_RTP_ULPFEC_ENC (obj);
638
639   g_hash_table_destroy (fec->ssrc_to_ctx);
640
641   G_OBJECT_CLASS (gst_rtp_ulpfec_enc_parent_class)->dispose (obj);
642 }
643
644 static void
645 gst_rtp_ulpfec_enc_init (GstRtpUlpFecEnc * fec)
646 {
647   fec->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
648   gst_element_add_pad (GST_ELEMENT (fec), fec->srcpad);
649
650   fec->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
651   GST_PAD_SET_PROXY_CAPS (fec->sinkpad);
652   GST_PAD_SET_PROXY_ALLOCATION (fec->sinkpad);
653   gst_pad_set_chain_function (fec->sinkpad,
654       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_enc_chain));
655   gst_element_add_pad (GST_ELEMENT (fec), fec->sinkpad);
656
657   fec->ssrc_to_ctx = g_hash_table_new_full (NULL, NULL, NULL,
658       (GDestroyNotify) gst_rtp_ulpfec_enc_stream_ctx_free);
659 }
660
661 static void
662 gst_rtp_ulpfec_enc_class_init (GstRtpUlpFecEncClass * klass)
663 {
664   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
665   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
666
667   GST_DEBUG_CATEGORY_INIT (gst_rtp_ulpfec_enc_debug, "rtpulpfecenc", 0,
668       "FEC encoder element");
669
670   gst_element_class_add_pad_template (element_class,
671       gst_static_pad_template_get (&srctemplate));
672   gst_element_class_add_pad_template (element_class,
673       gst_static_pad_template_get (&sinktemplate));
674
675   gst_element_class_set_static_metadata (element_class,
676       "RTP FEC Encoder",
677       "Codec/Payloader/Network/RTP",
678       "Encodes RTP FEC (RFC5109)", "Mikhail Fludkov <misha@pexip.com>");
679
680   gobject_class->set_property =
681       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_enc_set_property);
682   gobject_class->get_property =
683       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_enc_get_property);
684   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_enc_dispose);
685
686   g_object_class_install_property (gobject_class, PROP_PT,
687       g_param_spec_uint ("pt", "payload type",
688           "The payload type of FEC packets", 0, 255, DEFAULT_PT,
689           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690
691   g_object_class_install_property (gobject_class, PROP_MULTIPACKET,
692       g_param_spec_boolean ("multipacket", "Multipacket",
693           "Apply FEC on multiple packets", DEFAULT_MULTIPACKET,
694           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695
696   g_object_class_install_property (gobject_class, PROP_PERCENTAGE,
697       g_param_spec_uint ("percentage", "Percentage",
698           "FEC overhead percentage for the whole stream", 0, 100, DEFAULT_PCT,
699           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
700
701   g_object_class_install_property (gobject_class, PROP_PERCENTAGE_IMPORTANT,
702       g_param_spec_uint ("percentage-important", "Percentage important",
703           "FEC overhead percentage for important packets",
704           0, 100, DEFAULT_PCT_IMPORTANT,
705           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
706
707   g_object_class_install_property (gobject_class, PROP_PROTECTED,
708       g_param_spec_uint ("protected", "Protected",
709           "Count of protected packets", 0, G_MAXUINT32, 0,
710           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
711 }