1 /* GStreamer plugin for forward error correction
2 * Copyright (C) 2017 Pexip
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.
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.
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
18 * Author: Mikhail Fludkov <misha@pexip.com>
22 #include "rtpulpfeccommon.h"
24 #define MIN_RTP_HEADER_LEN 12
28 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
29 unsigned int csrc_count:4; /* CSRC count */
30 unsigned int extension:1; /* header extension flag */
31 unsigned int padding:1; /* padding flag */
32 unsigned int version:2; /* protocol version */
33 unsigned int payload_type:7; /* payload type */
34 unsigned int marker:1; /* marker bit */
35 #elif G_BYTE_ORDER == G_BIG_ENDIAN
36 unsigned int version:2; /* protocol version */
37 unsigned int padding:1; /* padding flag */
38 unsigned int extension:1; /* header extension flag */
39 unsigned int csrc_count:4; /* CSRC count */
40 unsigned int marker:1; /* marker bit */
41 unsigned int payload_type:7; /* payload type */
43 #error "G_BYTE_ORDER should be big or little endian."
45 unsigned int seq:16; /* sequence number */
46 unsigned int timestamp:32; /* timestamp */
47 unsigned int ssrc:32; /* synchronization source */
48 guint8 csrclist[4]; /* optional CSRC list, 32 bits each */
52 fec_level_hdr_get_size (gboolean l_bit)
54 return sizeof (RtpUlpFecLevelHeader) - (l_bit ? 0 : 4);
58 fec_level_hdr_get_mask (RtpUlpFecLevelHeader const *fec_lvl_hdr, gboolean l_bit)
60 return ((guint64) g_ntohs (fec_lvl_hdr->mask) << 32) |
61 (l_bit ? g_ntohl (fec_lvl_hdr->mask_continued) : 0);
65 fec_level_hdr_set_mask (RtpUlpFecLevelHeader * fec_lvl_hdr, gboolean l_bit,
68 fec_lvl_hdr->mask = g_htons (mask >> 32);
70 fec_lvl_hdr->mask_continued = g_htonl (mask);
74 fec_level_hdr_get_protection_len (RtpUlpFecLevelHeader * fec_lvl_hdr)
76 return g_ntohs (fec_lvl_hdr->protection_len);
80 fec_level_hdr_set_protection_len (RtpUlpFecLevelHeader * fec_lvl_hdr,
83 fec_lvl_hdr->protection_len = g_htons (len);
86 static RtpUlpFecLevelHeader *
87 fec_hdr_get_level_hdr (RtpUlpFecHeader const *fec_hdr)
89 return (RtpUlpFecLevelHeader *) (fec_hdr + 1);
93 fec_hdr_get_mask (RtpUlpFecHeader const *fec_hdr)
95 return fec_level_hdr_get_mask (fec_hdr_get_level_hdr (fec_hdr), fec_hdr->L);
99 fec_hdr_get_seq_base (RtpUlpFecHeader const *fec_hdr, gboolean is_ulpfec,
102 guint16 seq = g_ntohs (fec_hdr->seq);
105 return fec_seq - seq;
109 fec_hdr_get_packets_len_recovery (RtpUlpFecHeader const *fec_hdr)
111 return g_htons (fec_hdr->len);
115 fec_hdr_get_timestamp_recovery (RtpUlpFecHeader const *fec_hdr)
117 return g_ntohl (fec_hdr->timestamp);
121 _xor_mem (guint8 * restrict dst, const guint8 * restrict src, gsize length)
125 for (i = 0; i < (length / sizeof (guint64)); ++i) {
126 *((guint64 *) dst) ^= *((const guint64 *) src);
127 dst += sizeof (guint64);
128 src += sizeof (guint64);
130 for (i = 0; i < (length % sizeof (guint64)); ++i)
135 rtp_ulpfec_hdr_get_protection_len (RtpUlpFecHeader const *fec_hdr)
137 return fec_level_hdr_get_protection_len (fec_hdr_get_level_hdr (fec_hdr));
141 rtp_ulpfec_buffer_get_fechdr (GstRTPBuffer * rtp)
143 return (RtpUlpFecHeader *) gst_rtp_buffer_get_payload (rtp);
147 rtp_ulpfec_buffer_get_mask (GstRTPBuffer * rtp)
149 return fec_hdr_get_mask (rtp_ulpfec_buffer_get_fechdr (rtp));
153 rtp_ulpfec_buffer_get_seq_base (GstRTPBuffer * rtp)
155 return g_ntohs (rtp_ulpfec_buffer_get_fechdr (rtp)->seq);
159 rtp_ulpfec_get_headers_len (gboolean fec_mask_long)
161 return sizeof (RtpUlpFecHeader) + fec_level_hdr_get_size (fec_mask_long);
164 #define ONE_64BIT G_GUINT64_CONSTANT(1)
167 rtp_ulpfec_packet_mask_from_seqnum (guint16 seq,
168 guint16 fec_seq_base, gboolean fec_mask_long)
170 gint seq_delta = gst_rtp_buffer_compare_seqnum (fec_seq_base, seq);
172 && seq_delta <= RTP_ULPFEC_SEQ_BASE_OFFSET_MAX (fec_mask_long)) {
173 return ONE_64BIT << (RTP_ULPFEC_SEQ_BASE_OFFSET_MAX (TRUE) - seq_delta);
179 rtp_ulpfec_mask_is_long (guint64 mask)
181 return (mask & 0xffffffff) ? TRUE : FALSE;
185 rtp_ulpfec_buffer_is_valid (GstRTPBuffer * rtp)
187 guint payload_len = gst_rtp_buffer_get_payload_len (rtp);
188 RtpUlpFecHeader *fec_hdr;
190 guint fec_packet_len;
192 if (payload_len < sizeof (RtpUlpFecHeader))
195 fec_hdr = rtp_ulpfec_buffer_get_fechdr (rtp);
199 fec_hdrs_len = rtp_ulpfec_get_headers_len (fec_hdr->L);
200 if (payload_len < fec_hdrs_len)
203 fec_packet_len = fec_hdrs_len + rtp_ulpfec_hdr_get_protection_len (fec_hdr);
204 if (fec_packet_len != payload_len)
209 GST_WARNING ("FEC packet too small");
213 GST_WARNING ("invalid FEC packet (declared length %u, real length %u)",
214 fec_packet_len, payload_len);
218 GST_WARNING ("FEC Header contains invalid fields: %u", fec_hdr->E);
224 rtp_buffer_to_ulpfec_bitstring (GstRTPBuffer * rtp, GArray * dst_arr,
225 gboolean fec_buffer, gboolean fec_mask_long)
227 if (G_UNLIKELY (fec_buffer)) {
228 guint payload_len = gst_rtp_buffer_get_payload_len (rtp);
229 g_array_set_size (dst_arr, MAX (payload_len, dst_arr->len));
230 memcpy (dst_arr->data, gst_rtp_buffer_get_payload (rtp), payload_len);
232 const guint8 *src = rtp->data[0];
233 guint len = gst_rtp_buffer_get_packet_len (rtp) - MIN_RTP_HEADER_LEN;
234 guint dst_offset = rtp_ulpfec_get_headers_len (fec_mask_long);
235 guint src_offset = MIN_RTP_HEADER_LEN;
238 g_array_set_size (dst_arr, MAX (dst_offset + len, dst_arr->len));
239 dst = (guint8 *) dst_arr->data;
241 *((guint64 *) dst) ^= *((const guint64 *) src);
242 ((RtpUlpFecHeader *) dst)->len ^= g_htons (len);
243 _xor_mem (dst + dst_offset, src + src_offset, len);
248 rtp_ulpfec_bitstring_to_media_rtp_buffer (GArray * arr,
249 gboolean fec_mask_long, guint32 ssrc, guint16 seq)
251 guint fec_hdrs_len = rtp_ulpfec_get_headers_len (fec_mask_long);
253 fec_hdr_get_packets_len_recovery ((RtpUlpFecHeader *) arr->data);
254 GstMapInfo ret_info = GST_MAP_INFO_INIT;
258 if (payload_len > arr->len - fec_hdrs_len)
259 return NULL; // Not enough data
261 ret_mem = gst_allocator_alloc (NULL, MIN_RTP_HEADER_LEN + payload_len, NULL);
262 gst_memory_map (ret_mem, &ret_info, GST_MAP_READWRITE);
264 /* Filling 12 bytes of RTP header */
265 *((guint64 *) ret_info.data) = *((guint64 *) arr->data);
266 ((RtpHeader *) ret_info.data)->version = 2;
267 ((RtpHeader *) ret_info.data)->seq = g_htons (seq);
268 ((RtpHeader *) ret_info.data)->ssrc = g_htonl (ssrc);
269 /* Filling payload */
270 memcpy (ret_info.data + MIN_RTP_HEADER_LEN,
271 arr->data + fec_hdrs_len, payload_len);
273 gst_memory_unmap (ret_mem, &ret_info);
274 ret = gst_buffer_new ();
275 gst_buffer_append_memory (ret, ret_mem);
280 rtp_ulpfec_bitstring_to_fec_rtp_buffer (GArray * arr,
281 guint16 seq_base, gboolean fec_mask_long, guint64 fec_mask,
282 gboolean marker, guint8 pt, guint16 seq, guint32 timestamp, guint32 ssrc)
284 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
287 /* Filling FEC headers */
289 RtpUlpFecHeader *hdr = (RtpUlpFecHeader *) arr->data;
290 RtpUlpFecLevelHeader *lvlhdr;
292 hdr->L = fec_mask_long;
293 hdr->seq = g_htons (seq_base);
295 lvlhdr = fec_hdr_get_level_hdr (hdr);
296 fec_level_hdr_set_protection_len (lvlhdr,
297 arr->len - rtp_ulpfec_get_headers_len (fec_mask_long));
298 fec_level_hdr_set_mask (lvlhdr, fec_mask_long, fec_mask);
301 /* Filling RTP header, copying payload */
302 ret = gst_rtp_buffer_new_allocate (arr->len, 0, 0);
303 if (!gst_rtp_buffer_map (ret, GST_MAP_READWRITE, &rtp))
304 g_assert_not_reached ();
306 gst_rtp_buffer_set_marker (&rtp, marker);
307 gst_rtp_buffer_set_payload_type (&rtp, pt);
308 gst_rtp_buffer_set_seq (&rtp, seq);
309 gst_rtp_buffer_set_timestamp (&rtp, timestamp);
310 gst_rtp_buffer_set_ssrc (&rtp, ssrc);
312 memcpy (gst_rtp_buffer_get_payload (&rtp), arr->data, arr->len);
314 gst_rtp_buffer_unmap (&rtp);
320 * rtp_ulpfec_map_info_map:
321 * @buffer: (transfer: full) #GstBuffer
322 * @info: #RtpUlpFecMapInfo
324 * Maps the contents of @buffer into @info. If @buffer made of many #GstMemory
325 * objects, merges them together to create a new buffer made of single
326 * continious #GstMemory.
328 * Returns: %TRUE if @buffer could be mapped
331 rtp_ulpfec_map_info_map (GstBuffer * buffer, RtpUlpFecMapInfo * info)
333 /* We need to make sure we are working with continious memory chunk.
334 * If not merge all memories together */
335 if (gst_buffer_n_memory (buffer) > 1) {
336 GstBuffer *new_buffer = gst_buffer_new ();
337 GstMemory *mem = gst_buffer_get_all_memory (buffer);
338 gst_buffer_append_memory (new_buffer, mem);
340 /* We supposed to own the old buffer, but we don't use it here, so unref */
341 gst_buffer_unref (buffer);
345 if (!gst_rtp_buffer_map (buffer,
346 GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &info->rtp)) {
347 /* info->rtp.buffer = NULL is an indication for rtp_ulpfec_map_info_unmap()
348 * that mapping has failed */
349 g_assert (NULL == info->rtp.buffer);
350 gst_buffer_unref (buffer);
357 * rtp_ulpfec_map_info_unmap:
358 * @info: #RtpUlpFecMapInfo
360 * Unmap @info previously mapped with rtp_ulpfec_map_info_map() and unrefs the
361 * buffer. For convenience can even be called even if rtp_ulpfec_map_info_map
365 rtp_ulpfec_map_info_unmap (RtpUlpFecMapInfo * info)
367 GstBuffer *buffer = info->rtp.buffer;
370 gst_rtp_buffer_unmap (&info->rtp);
371 gst_buffer_unref (buffer);
375 #ifndef GST_DISABLE_GST_DEBUG
377 rtp_ulpfec_log_rtppacket (GstDebugCategory * cat, GstDebugLevel level,
378 gpointer object, const gchar * name, GstRTPBuffer * rtp)
385 if (level > gst_debug_category_get_threshold (cat))
388 seq = gst_rtp_buffer_get_seq (rtp);
389 ssrc = gst_rtp_buffer_get_ssrc (rtp);
390 timestamp = gst_rtp_buffer_get_timestamp (rtp);
391 pt = gst_rtp_buffer_get_payload_type (rtp);
393 GST_CAT_LEVEL_LOG (cat, level, object,
394 "%-22s: [%c%c%c%c] ssrc=0x%08x pt=%u tstamp=%u seq=%u size=%u(%u,%u)",
396 gst_rtp_buffer_get_marker (rtp) ? 'M' : ' ',
397 gst_rtp_buffer_get_extension (rtp) ? 'X' : ' ',
398 gst_rtp_buffer_get_padding (rtp) ? 'P' : ' ',
399 gst_rtp_buffer_get_csrc_count (rtp) > 0 ? 'C' : ' ',
400 ssrc, pt, timestamp, seq,
401 gst_rtp_buffer_get_packet_len (rtp),
402 gst_rtp_buffer_get_packet_len (rtp) - MIN_RTP_HEADER_LEN,
403 gst_rtp_buffer_get_payload_len (rtp));
405 #endif /* GST_DISABLE_GST_DEBUG */
407 #ifndef GST_DISABLE_GST_DEBUG
409 rtp_ulpfec_log_fec_packet (GstDebugCategory * cat, GstDebugLevel level,
410 gpointer object, GstRTPBuffer * fecrtp)
412 RtpUlpFecHeader *fec_hdr;
413 RtpUlpFecLevelHeader *fec_level_hdr;
415 if (level > gst_debug_category_get_threshold (cat))
418 fec_hdr = gst_rtp_buffer_get_payload (fecrtp);
419 GST_CAT_LEVEL_LOG (cat, level, object,
420 "%-22s: [%c%c%c%c%c%c] pt=%u tstamp=%u seq=%u recovery_len=%u",
422 fec_hdr->E ? 'E' : ' ',
423 fec_hdr->L ? 'L' : ' ',
424 fec_hdr->P ? 'P' : ' ',
425 fec_hdr->X ? 'X' : ' ',
426 fec_hdr->CC ? 'C' : ' ',
427 fec_hdr->M ? 'M' : ' ',
429 fec_hdr_get_timestamp_recovery (fec_hdr),
430 fec_hdr_get_seq_base (fec_hdr, TRUE,
431 gst_rtp_buffer_get_seq (fecrtp)),
432 fec_hdr_get_packets_len_recovery (fec_hdr));
434 fec_level_hdr = fec_hdr_get_level_hdr (fec_hdr);
435 GST_CAT_LEVEL_LOG (cat, level, object,
436 "%-22s: protection_len=%u mask=0x%012" G_GINT64_MODIFIER "x",
438 g_ntohs (fec_level_hdr->protection_len),
439 fec_level_hdr_get_mask (fec_level_hdr, fec_hdr->L));
441 #endif /* GST_DISABLE_GST_DEBUG */