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