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-rtpulpfecdec
23  * @short_description: Generic RTP Forward Error Correction (FEC) decoder
24  * @title: rtpulpfecdec
25  *
26  * Generic Forward Error Correction (FEC) decoder for 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  * This element will work in combination with an upstream #GstRtpStorage
35  * element and attempt to recover packets declared lost through custom
36  * 'GstRTPPacketLost' events, usually emitted by #GstRtpJitterBuffer.
37  *
38  * If no storage is provided using the #GstRtpUlpFecDec:storage
39  * property, it will try to get it from an element upstream.
40  *
41  * Additionally, the payload types of the protection packets *must* be
42  * provided to this element via its #GstRtpUlpFecDec:pt property.
43  *
44  * When using #GstRtpBin, this element should be inserted through the
45  * #GstRtpBin::request-fec-decoder signal.
46  *
47  * <refsect2>
48  * <title>Example pipeline</title>
49  * |[
50  * gst-launch-1.0 udpsrc port=8888 caps="application/x-rtp, payload=96, clock-rate=90000" ! rtpstorage size-time=220000000 ! rtpssrcdemux ! application/x-rtp, payload=96, clock-rate=90000, media=video, encoding-name=H264 ! rtpjitterbuffer do-lost=1 latency=200 !  rtpulpfecdec pt=122 ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink
51  * ]| This example will receive a stream with FEC and try to reconstruct the packets.
52  *
53  * Example programs are available at
54  * <ulink url="https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/blob/master/examples/src/bin/rtpfecserver.rs">rtpfecserver.rs</ulink>
55  * and
56  * <ulink url="https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/blob/master/examples/src/bin/rtpfecclient.rs">rtpfecclient.rs</ulink>
57  *
58  * </refsect2>
59  *
60  * See also: #GstRtpUlpFecEnc, #GstRtpBin, #GstRtpStorage
61  * Since: 1.14
62  */
63 
64 #include <gst/rtp/gstrtpbuffer.h>
65 #include <gst/rtp/gstrtp-enumtypes.h>
66 
67 #include "rtpulpfeccommon.h"
68 #include "gstrtpulpfecdec.h"
69 
70 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-rtp")
74     );
75 
76 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
77     GST_PAD_SRC,
78     GST_PAD_ALWAYS,
79     GST_STATIC_CAPS ("application/x-rtp")
80     );
81 
82 enum
83 {
84   PROP_0,
85   PROP_PT,
86   PROP_STORAGE,
87   PROP_RECOVERED,
88   PROP_UNRECOVERED,
89   N_PROPERTIES
90 };
91 
92 #define DEFAULT_FEC_PT 0
93 
94 static GParamSpec *klass_properties[N_PROPERTIES] = { NULL, };
95 
96 GST_DEBUG_CATEGORY (gst_rtp_ulpfec_dec_debug);
97 #define GST_CAT_DEFAULT (gst_rtp_ulpfec_dec_debug)
98 
99 G_DEFINE_TYPE (GstRtpUlpFecDec, gst_rtp_ulpfec_dec, GST_TYPE_ELEMENT);
100 
101 #define RTP_FEC_MAP_INFO_NTH(dec, data) (&g_array_index (\
102     ((GstRtpUlpFecDec *)dec)->info_arr, \
103     RtpUlpFecMapInfo, \
104     GPOINTER_TO_UINT(data)))
105 
106 static gint
_compare_fec_map_info(gconstpointer a,gconstpointer b,gpointer userdata)107 _compare_fec_map_info (gconstpointer a, gconstpointer b, gpointer userdata)
108 {
109   guint16 aseq =
110       gst_rtp_buffer_get_seq (&RTP_FEC_MAP_INFO_NTH (userdata, a)->rtp);
111   guint16 bseq =
112       gst_rtp_buffer_get_seq (&RTP_FEC_MAP_INFO_NTH (userdata, b)->rtp);
113   return gst_rtp_buffer_compare_seqnum (bseq, aseq);
114 }
115 
116 static void
gst_rtp_ulpfec_dec_start(GstRtpUlpFecDec * self,GstBufferList * buflist,guint8 fec_pt,guint16 lost_seq)117 gst_rtp_ulpfec_dec_start (GstRtpUlpFecDec * self, GstBufferList * buflist,
118     guint8 fec_pt, guint16 lost_seq)
119 {
120   guint fec_packets = 0;
121   gsize i;
122 
123   g_assert (NULL == self->info_media);
124   g_assert (0 == self->info_fec->len);
125   g_assert (0 == self->info_arr->len);
126 
127   g_array_set_size (self->info_arr, gst_buffer_list_length (buflist));
128 
129   for (i = 0;
130       i < gst_buffer_list_length (buflist) && !self->lost_packet_from_storage;
131       ++i) {
132     GstBuffer *buffer = gst_buffer_list_get (buflist, i);
133     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (self, i);
134 
135     if (!rtp_ulpfec_map_info_map (gst_buffer_ref (buffer), info))
136       g_assert_not_reached ();
137 
138     if (fec_pt == gst_rtp_buffer_get_payload_type (&info->rtp)) {
139       GST_DEBUG_RTP_PACKET (self, "rtp header (fec)", &info->rtp);
140 
141       ++fec_packets;
142       if (rtp_ulpfec_buffer_is_valid (&info->rtp)) {
143         GST_DEBUG_FEC_PACKET (self, &info->rtp);
144         g_ptr_array_add (self->info_fec, GUINT_TO_POINTER (i));
145       }
146     } else {
147       GST_LOG_RTP_PACKET (self, "rtp header (incoming)", &info->rtp);
148 
149       if (lost_seq == gst_rtp_buffer_get_seq (&info->rtp)) {
150         GST_DEBUG_OBJECT (self, "Received lost packet from the storage");
151         g_list_free (self->info_media);
152         self->info_media = NULL;
153         self->lost_packet_from_storage = TRUE;
154       }
155       self->info_media =
156           g_list_insert_sorted_with_data (self->info_media,
157           GUINT_TO_POINTER (i), _compare_fec_map_info, self);
158     }
159   }
160   if (!self->lost_packet_from_storage) {
161     self->fec_packets_received += fec_packets;
162     self->fec_packets_rejected += fec_packets - self->info_fec->len;
163   }
164 }
165 
166 static void
gst_rtp_ulpfec_dec_stop(GstRtpUlpFecDec * self)167 gst_rtp_ulpfec_dec_stop (GstRtpUlpFecDec * self)
168 {
169   g_array_set_size (self->info_arr, 0);
170   g_ptr_array_set_size (self->info_fec, 0);
171   g_list_free (self->info_media);
172   self->info_media = NULL;
173   self->lost_packet_from_storage = FALSE;
174   self->lost_packet_returned = FALSE;
175 }
176 
177 static guint64
gst_rtp_ulpfec_dec_get_media_buffers_mask(GstRtpUlpFecDec * self,guint16 fec_seq_base)178 gst_rtp_ulpfec_dec_get_media_buffers_mask (GstRtpUlpFecDec * self,
179     guint16 fec_seq_base)
180 {
181   guint64 mask = 0;
182   GList *it;
183 
184   for (it = self->info_media; it; it = it->next) {
185     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (self, it->data);
186     mask |=
187         rtp_ulpfec_packet_mask_from_seqnum (gst_rtp_buffer_get_seq (&info->rtp),
188         fec_seq_base, TRUE);
189   }
190   return mask;
191 }
192 
193 static gboolean
gst_rtp_ulpfec_dec_is_recovered_pt_valid(GstRtpUlpFecDec * self,gint media_pt,guint8 recovered_pt)194 gst_rtp_ulpfec_dec_is_recovered_pt_valid (GstRtpUlpFecDec * self, gint media_pt,
195     guint8 recovered_pt)
196 {
197   GList *it;
198   if (media_pt == recovered_pt)
199     return TRUE;
200 
201   for (it = self->info_media; it; it = it->next) {
202     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (self, it->data);
203     if (gst_rtp_buffer_get_payload_type (&info->rtp) == recovered_pt)
204       return TRUE;
205   }
206   return FALSE;
207 }
208 
209 static GstBuffer *
gst_rtp_ulpfec_dec_recover_from_fec(GstRtpUlpFecDec * self,RtpUlpFecMapInfo * info_fec,guint32 ssrc,gint media_pt,guint16 seq,guint8 * dst_pt)210 gst_rtp_ulpfec_dec_recover_from_fec (GstRtpUlpFecDec * self,
211     RtpUlpFecMapInfo * info_fec, guint32 ssrc, gint media_pt, guint16 seq,
212     guint8 * dst_pt)
213 {
214   guint64 fec_mask = rtp_ulpfec_buffer_get_mask (&info_fec->rtp);
215   gboolean fec_mask_long = rtp_ulpfec_buffer_get_fechdr (&info_fec->rtp)->L;
216   guint16 fec_seq_base = rtp_ulpfec_buffer_get_seq_base (&info_fec->rtp);
217   GstBuffer *ret;
218   GList *it;
219 
220   g_array_set_size (self->scratch_buf, 0);
221   rtp_buffer_to_ulpfec_bitstring (&info_fec->rtp, self->scratch_buf, TRUE,
222       fec_mask_long);
223 
224   for (it = self->info_media; it; it = it->next) {
225     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (self, it->data);
226     guint64 packet_mask =
227         rtp_ulpfec_packet_mask_from_seqnum (gst_rtp_buffer_get_seq (&info->rtp),
228         fec_seq_base, TRUE);
229 
230     if (fec_mask & packet_mask) {
231       fec_mask ^= packet_mask;
232       rtp_buffer_to_ulpfec_bitstring (&info->rtp, self->scratch_buf, FALSE,
233           fec_mask_long);
234     }
235   }
236 
237   ret =
238       rtp_ulpfec_bitstring_to_media_rtp_buffer (self->scratch_buf,
239       fec_mask_long, ssrc, seq);
240   if (ret) {
241     /* We are about to put recovered packet back in self->info_media to be able
242      * to reuse it later for recovery of other packets
243      **/
244     gint i = self->info_arr->len;
245     RtpUlpFecMapInfo *info;
246     guint8 recovered_pt;
247 
248     g_array_set_size (self->info_arr, self->info_arr->len + 1);
249     info = RTP_FEC_MAP_INFO_NTH (self, i);
250 
251     if (!rtp_ulpfec_map_info_map (gst_buffer_ref (ret), info)) {
252       GST_WARNING_OBJECT (self, "Invalid recovered packet");
253       goto recovered_packet_invalid;
254     }
255 
256     recovered_pt = gst_rtp_buffer_get_payload_type (&info->rtp);
257     if (!gst_rtp_ulpfec_dec_is_recovered_pt_valid (self, media_pt,
258             recovered_pt)) {
259       GST_WARNING_OBJECT (self,
260           "Recovered packet has unexpected payload type (%u)", recovered_pt);
261       goto recovered_packet_invalid;
262     }
263 
264     GST_DEBUG_RTP_PACKET (self, "rtp header (recovered)", &info->rtp);
265     self->info_media =
266         g_list_insert_sorted_with_data (self->info_media, GUINT_TO_POINTER (i),
267         _compare_fec_map_info, self);
268     *dst_pt = recovered_pt;
269   }
270   return ret;
271 
272 recovered_packet_invalid:
273   g_array_set_size (self->info_arr, self->info_arr->len - 1);
274   gst_buffer_unref (ret);
275   return NULL;
276 }
277 
278 static GstBuffer *
gst_rtp_ulpfec_dec_recover_from_storage(GstRtpUlpFecDec * self,guint8 * dst_pt,guint16 * dst_seq)279 gst_rtp_ulpfec_dec_recover_from_storage (GstRtpUlpFecDec * self,
280     guint8 * dst_pt, guint16 * dst_seq)
281 {
282   RtpUlpFecMapInfo *info;
283 
284   if (self->lost_packet_returned)
285     return NULL;
286 
287   g_assert (g_list_length (self->info_media) == 1);
288 
289   info = RTP_FEC_MAP_INFO_NTH (self, self->info_media->data);
290   *dst_seq = gst_rtp_buffer_get_seq (&info->rtp);
291   *dst_pt = gst_rtp_buffer_get_payload_type (&info->rtp);
292   self->lost_packet_returned = TRUE;
293   GST_DEBUG_RTP_PACKET (self, "rtp header (recovered)", &info->rtp);
294   return gst_buffer_ref (info->rtp.buffer);
295 }
296 
297 /* __has_builtin only works with clang, so test compiler version for gcc */
298 /* Intel compiler and MSVC probably have their own things as well */
299 /* TODO: make sure we use builtin for clang as well */
300 #if defined(__GNUC__) && __GNUC__ >= 4
301 #define rtp_ulpfec_ctz64 __builtin_ctzll
302 #else
303 static inline gint
rtp_ulpfec_ctz64_inline(guint64 mask)304 rtp_ulpfec_ctz64_inline (guint64 mask)
305 {
306   gint nth_bit = 0;
307 
308   do {
309     if ((mask & 1))
310       return nth_bit;
311     mask = mask >> 1;
312   } while (++nth_bit < 64);
313 
314   return -1;                    /* should not be reached, since mask must not be 0 */
315 }
316 
317 #define rtp_ulpfec_ctz64 rtp_ulpfec_ctz64_inline
318 #endif
319 
320 static GstBuffer *
gst_rtp_ulpfec_dec_recover(GstRtpUlpFecDec * self,guint32 ssrc,gint media_pt,guint8 * dst_pt,guint16 * dst_seq)321 gst_rtp_ulpfec_dec_recover (GstRtpUlpFecDec * self, guint32 ssrc, gint media_pt,
322     guint8 * dst_pt, guint16 * dst_seq)
323 {
324   guint64 media_mask = 0;
325   gint media_mask_seq_base = -1;
326   gsize i;
327 
328   if (self->lost_packet_from_storage)
329     return gst_rtp_ulpfec_dec_recover_from_storage (self, dst_pt, dst_seq);
330 
331   /* Looking for a FEC packet which can be used for recovery */
332   for (i = 0; i < self->info_fec->len; ++i) {
333     RtpUlpFecMapInfo *info = RTP_FEC_MAP_INFO_NTH (self,
334         g_ptr_array_index (self->info_fec, i));
335     guint16 seq_base = rtp_ulpfec_buffer_get_seq_base (&info->rtp);
336     guint64 fec_mask = rtp_ulpfec_buffer_get_mask (&info->rtp);
337     guint64 missing_packets_mask;
338 
339     if (media_mask_seq_base != (gint) seq_base) {
340       media_mask_seq_base = seq_base;
341       media_mask = gst_rtp_ulpfec_dec_get_media_buffers_mask (self, seq_base);
342     }
343 
344     /* media_mask has 1s if packet exist.
345      * fec_mask is the mask of protected packets
346      * The statement below excludes existing packets from the protected. So
347      * we are left with 1s only for missing packets which can be recovered
348      * by this FEC packet. */
349     missing_packets_mask = fec_mask & (~media_mask);
350 
351     /* Do we have any 1s? Checking if current FEC packet can be used for recovery */
352     if (0 != missing_packets_mask) {
353       guint trailing_zeros = rtp_ulpfec_ctz64 (missing_packets_mask);
354 
355       /* Is it the only 1 in the mask? Checking if we lacking single packet in
356        * that case FEC packet can be used for recovery */
357       if (missing_packets_mask == (G_GUINT64_CONSTANT (1) << trailing_zeros)) {
358         GstBuffer *ret;
359 
360         *dst_seq =
361             seq_base + (RTP_ULPFEC_SEQ_BASE_OFFSET_MAX (TRUE) - trailing_zeros);
362         ret =
363             gst_rtp_ulpfec_dec_recover_from_fec (self, info, ssrc, media_pt,
364             *dst_seq, dst_pt);
365         if (ret)
366           return ret;
367       }
368     }
369   }
370   return NULL;
371 }
372 
373 static GstFlowReturn
gst_rtp_ulpfec_dec_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)374 gst_rtp_ulpfec_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
375 {
376   GstRtpUlpFecDec *self = GST_RTP_ULPFEC_DEC (parent);
377 
378   if (G_LIKELY (GST_FLOW_OK == self->chain_return_val)) {
379     GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
380     buf = gst_buffer_make_writable (buf);
381 
382     if (G_UNLIKELY (self->unset_discont_flag)) {
383       self->unset_discont_flag = FALSE;
384       GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
385     }
386 
387     gst_rtp_buffer_map (buf, GST_MAP_WRITE, &rtp);
388     gst_rtp_buffer_set_seq (&rtp, self->next_seqnum++);
389     gst_rtp_buffer_unmap (&rtp);
390 
391     return gst_pad_push (self->srcpad, buf);
392   }
393 
394   gst_buffer_unref (buf);
395   return self->chain_return_val;
396 }
397 
398 static gboolean
gst_rtp_ulpfec_dec_handle_packet_loss(GstRtpUlpFecDec * self,guint16 seqnum,GstClockTime timestamp,GstClockTime duration)399 gst_rtp_ulpfec_dec_handle_packet_loss (GstRtpUlpFecDec * self, guint16 seqnum,
400     GstClockTime timestamp, GstClockTime duration)
401 {
402   gint caps_pt = self->have_caps_pt ? self->caps_pt : -1;
403   gboolean ret = TRUE;
404   GstBufferList *buflist =
405       rtp_storage_get_packets_for_recovery (self->storage, self->fec_pt,
406       self->caps_ssrc, seqnum);
407 
408   if (buflist) {
409     GstBuffer *recovered_buffer = NULL;
410     guint16 recovered_seq = 0;
411     guint8 recovered_pt = 0;
412 
413     gst_rtp_ulpfec_dec_start (self, buflist, self->fec_pt, seqnum);
414 
415     while (NULL != (recovered_buffer =
416             gst_rtp_ulpfec_dec_recover (self, self->caps_ssrc, caps_pt,
417                 &recovered_pt, &recovered_seq))) {
418       if (seqnum == recovered_seq) {
419         GstBuffer *sent_buffer;
420         GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
421 
422         recovered_buffer = gst_buffer_make_writable (recovered_buffer);
423         GST_BUFFER_PTS (recovered_buffer) = timestamp;
424         /* GST_BUFFER_DURATION (recovered_buffer) = duration;
425          * JB does not set the duration, so we will not too */
426 
427         if (!self->lost_packet_from_storage)
428           rtp_storage_put_recovered_packet (self->storage,
429               recovered_buffer, recovered_pt, self->caps_ssrc, recovered_seq);
430 
431         GST_DEBUG_OBJECT (self,
432             "Pushing recovered packet ssrc=0x%08x seq=%u %" GST_PTR_FORMAT,
433             self->caps_ssrc, seqnum, recovered_buffer);
434 
435         sent_buffer = gst_buffer_copy_deep (recovered_buffer);
436 
437         if (self->lost_packet_from_storage)
438           gst_buffer_unref (recovered_buffer);
439 
440         gst_rtp_buffer_map (sent_buffer, GST_MAP_WRITE, &rtp);
441         gst_rtp_buffer_set_seq (&rtp, self->next_seqnum++);
442         gst_rtp_buffer_unmap (&rtp);
443 
444         ret = FALSE;
445         self->unset_discont_flag = TRUE;
446         self->chain_return_val = gst_pad_push (self->srcpad, sent_buffer);
447         break;
448       }
449 
450       if (!self->lost_packet_from_storage) {
451         rtp_storage_put_recovered_packet (self->storage,
452             recovered_buffer, recovered_pt, self->caps_ssrc, recovered_seq);
453       } else {
454         gst_buffer_unref (recovered_buffer);
455       }
456     }
457 
458     gst_rtp_ulpfec_dec_stop (self);
459     gst_buffer_list_unref (buflist);
460   }
461 
462   GST_DEBUG_OBJECT (self, "Packet lost ssrc=0x%08x seq=%u", self->caps_ssrc,
463       seqnum);
464 
465   return ret;
466 }
467 
468 static gboolean
gst_rtp_ulpfec_dec_handle_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)469 gst_rtp_ulpfec_dec_handle_sink_event (GstPad * pad, GstObject * parent,
470     GstEvent * event)
471 {
472   GstRtpUlpFecDec *self = GST_RTP_ULPFEC_DEC (parent);
473   gboolean forward = TRUE;
474 
475   GST_LOG_OBJECT (self, "Received event %" GST_PTR_FORMAT, event);
476 
477   if (GST_FLOW_OK == self->chain_return_val &&
478       GST_EVENT_CUSTOM_DOWNSTREAM == GST_EVENT_TYPE (event) &&
479       gst_event_has_name (event, "GstRTPPacketLost")) {
480     guint seqnum;
481     GstClockTime timestamp, duration;
482     GstStructure *s;
483 
484     event = gst_event_make_writable (event);
485     s = gst_event_writable_structure (event);
486 
487     g_assert (self->have_caps_ssrc);
488 
489     if (self->storage == NULL) {
490       GstQuery *q = gst_query_new_custom (GST_QUERY_CUSTOM,
491           gst_structure_new_empty ("GstRtpStorage"));
492 
493       if (gst_pad_peer_query (self->sinkpad, q)) {
494         const GstStructure *s = gst_query_get_structure (q);
495 
496         if (gst_structure_has_field_typed (s, "storage", G_TYPE_OBJECT)) {
497           gst_structure_get (s, "storage", G_TYPE_OBJECT, &self->storage, NULL);
498         }
499       }
500       gst_query_unref (q);
501     }
502 
503     if (self->storage == NULL) {
504       GST_ELEMENT_WARNING (self, STREAM, FAILED, ("Internal storage not found"),
505           ("You need to add rtpstorage element upstream from rtpulpfecdec."));
506       return FALSE;
507     }
508 
509     if (!gst_structure_get (s,
510             "seqnum", G_TYPE_UINT, &seqnum,
511             "timestamp", G_TYPE_UINT64, &timestamp,
512             "duration", G_TYPE_UINT64, &duration, NULL))
513       g_assert_not_reached ();
514 
515     forward =
516         gst_rtp_ulpfec_dec_handle_packet_loss (self, seqnum, timestamp,
517         duration);
518 
519     if (forward) {
520       gst_structure_remove_field (s, "seqnum");
521       gst_structure_set (s, "might-have-been-fec", G_TYPE_BOOLEAN, TRUE, NULL);
522       ++self->packets_unrecovered;
523     } else {
524       ++self->packets_recovered;
525     }
526 
527     GST_DEBUG_OBJECT (self, "Unrecovered / Recovered: %lu / %lu",
528         (gulong) self->packets_unrecovered, (gulong) self->packets_recovered);
529   } else if (GST_EVENT_CAPS == GST_EVENT_TYPE (event)) {
530     GstCaps *caps;
531     gboolean have_caps_pt = FALSE;
532     gboolean have_caps_ssrc = FALSE;
533     guint caps_ssrc = 0;
534     gint caps_pt = 0;
535 
536     gst_event_parse_caps (event, &caps);
537     have_caps_ssrc =
538         gst_structure_get_uint (gst_caps_get_structure (caps, 0), "ssrc",
539         &caps_ssrc);
540     have_caps_pt =
541         gst_structure_get_int (gst_caps_get_structure (caps, 0), "payload",
542         &caps_pt);
543 
544     if (self->have_caps_ssrc != have_caps_ssrc || self->caps_ssrc != caps_ssrc)
545       GST_DEBUG_OBJECT (self, "SSRC changed %u, 0x%08x -> %u, 0x%08x",
546           self->have_caps_ssrc, self->caps_ssrc, have_caps_ssrc, caps_ssrc);
547     if (self->have_caps_pt != have_caps_pt || self->caps_pt != caps_pt)
548       GST_DEBUG_OBJECT (self, "PT changed %u, %u -> %u, %u",
549           self->have_caps_pt, self->caps_pt, have_caps_pt, caps_pt);
550 
551     self->have_caps_ssrc = have_caps_ssrc;
552     self->have_caps_pt = have_caps_pt;
553     self->caps_ssrc = caps_ssrc;
554     self->caps_pt = caps_pt;
555   }
556 
557   if (forward)
558     return gst_pad_push_event (self->srcpad, event);
559   gst_event_unref (event);
560   return TRUE;
561 }
562 
563 static void
gst_rtp_ulpfec_dec_init(GstRtpUlpFecDec * self)564 gst_rtp_ulpfec_dec_init (GstRtpUlpFecDec * self)
565 {
566   self->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
567   self->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
568   GST_PAD_SET_PROXY_CAPS (self->sinkpad);
569   GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
570   gst_pad_set_chain_function (self->sinkpad,
571       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_dec_chain));
572   gst_pad_set_event_function (self->sinkpad,
573       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_dec_handle_sink_event));
574 
575   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
576   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
577 
578   self->fec_pt = DEFAULT_FEC_PT;
579 
580   self->next_seqnum = g_random_int_range (0, G_MAXINT16);
581 
582   self->chain_return_val = GST_FLOW_OK;
583   self->have_caps_ssrc = FALSE;
584   self->caps_ssrc = 0;
585   self->info_fec = g_ptr_array_new ();
586   self->info_arr = g_array_new (FALSE, TRUE, sizeof (RtpUlpFecMapInfo));
587   g_array_set_clear_func (self->info_arr,
588       (GDestroyNotify) rtp_ulpfec_map_info_unmap);
589   self->scratch_buf = g_array_new (FALSE, TRUE, sizeof (guint8));
590 }
591 
592 static void
gst_rtp_ulpfec_dec_dispose(GObject * obj)593 gst_rtp_ulpfec_dec_dispose (GObject * obj)
594 {
595   GstRtpUlpFecDec *self = GST_RTP_ULPFEC_DEC (obj);
596 
597   GST_INFO_OBJECT (self,
598       " ssrc=0x%08x pt=%u"
599       " packets_recovered=%" G_GSIZE_FORMAT
600       " packets_unrecovered=%" G_GSIZE_FORMAT,
601       self->caps_ssrc, self->caps_pt,
602       self->packets_recovered, self->packets_unrecovered);
603 
604   if (self->storage)
605     g_object_unref (self->storage);
606 
607   g_assert (NULL == self->info_media);
608   g_assert (0 == self->info_fec->len);
609   g_assert (0 == self->info_arr->len);
610 
611   if (self->fec_packets_received) {
612     GST_INFO_OBJECT (self,
613         " fec_packets_received=%" G_GSIZE_FORMAT
614         " fec_packets_rejected=%" G_GSIZE_FORMAT
615         " packets_rejected=%" G_GSIZE_FORMAT,
616         self->fec_packets_received,
617         self->fec_packets_rejected, self->packets_rejected);
618   }
619 
620   g_ptr_array_free (self->info_fec, TRUE);
621   g_array_free (self->info_arr, TRUE);
622   g_array_free (self->scratch_buf, TRUE);
623 
624   G_OBJECT_CLASS (gst_rtp_ulpfec_dec_parent_class)->dispose (obj);
625 }
626 
627 static void
gst_rtp_ulpfec_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)628 gst_rtp_ulpfec_dec_set_property (GObject * object, guint prop_id,
629     const GValue * value, GParamSpec * pspec)
630 {
631   GstRtpUlpFecDec *self = GST_RTP_ULPFEC_DEC (object);
632 
633   switch (prop_id) {
634     case PROP_PT:
635       self->fec_pt = g_value_get_uint (value);
636       break;
637     case PROP_STORAGE:
638       if (self->storage)
639         g_object_unref (self->storage);
640       self->storage = g_value_get_object (value);
641       if (self->storage)
642         g_object_ref (self->storage);
643       break;
644     default:
645       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646       break;
647   }
648 }
649 
650 static void
gst_rtp_ulpfec_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)651 gst_rtp_ulpfec_dec_get_property (GObject * object, guint prop_id,
652     GValue * value, GParamSpec * pspec)
653 {
654   GstRtpUlpFecDec *self = GST_RTP_ULPFEC_DEC (object);
655 
656   switch (prop_id) {
657     case PROP_PT:
658       g_value_set_uint (value, self->fec_pt);
659       break;
660     case PROP_STORAGE:
661       g_value_set_object (value, self->storage);
662       break;
663     case PROP_RECOVERED:
664       g_value_set_uint (value, (guint) self->packets_recovered);
665       break;
666     case PROP_UNRECOVERED:
667       g_value_set_uint (value, (guint) self->packets_unrecovered);
668       break;
669     default:
670       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671       break;
672   }
673 }
674 
675 static void
gst_rtp_ulpfec_dec_class_init(GstRtpUlpFecDecClass * klass)676 gst_rtp_ulpfec_dec_class_init (GstRtpUlpFecDecClass * klass)
677 {
678   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
679   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
680 
681   GST_DEBUG_CATEGORY_INIT (gst_rtp_ulpfec_dec_debug,
682       "rtpulpfecdec", 0, "RTP FEC Decoder");
683 
684   gst_element_class_add_pad_template (element_class,
685       gst_static_pad_template_get (&srctemplate));
686   gst_element_class_add_pad_template (element_class,
687       gst_static_pad_template_get (&sinktemplate));
688 
689   gst_element_class_set_static_metadata (element_class,
690       "RTP FEC Decoder",
691       "Codec/Depayloader/Network/RTP",
692       "Decodes RTP FEC (RFC5109)", "Mikhail Fludkov <misha@pexip.com>");
693 
694   gobject_class->set_property =
695       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_dec_set_property);
696   gobject_class->get_property =
697       GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_dec_get_property);
698   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_rtp_ulpfec_dec_dispose);
699 
700   klass_properties[PROP_PT] = g_param_spec_uint ("pt", "pt",
701       "FEC packets payload type", 0, 127,
702       DEFAULT_FEC_PT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
703   klass_properties[PROP_STORAGE] =
704       g_param_spec_object ("storage", "RTP storage", "RTP storage",
705       G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
706   klass_properties[PROP_RECOVERED] =
707       g_param_spec_uint ("recovered", "recovered",
708       "The number of recovered packets", 0, G_MAXUINT, 0,
709       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
710   klass_properties[PROP_UNRECOVERED] =
711       g_param_spec_uint ("unrecovered", "unrecovered",
712       "The number of unrecovered packets", 0, G_MAXUINT, 0,
713       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
714 
715   g_object_class_install_properties (gobject_class, N_PROPERTIES,
716       klass_properties);
717 
718   g_assert (rtp_ulpfec_ctz64 (G_GUINT64_CONSTANT (0x1)) == 0);
719   g_assert (rtp_ulpfec_ctz64 (G_GUINT64_CONSTANT (0x8000000000000000)) == 63);
720 }
721