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);
165 rtp_ulpfec_packet_mask_from_seqnum (guint16 seq,
166 guint16 fec_seq_base, gboolean fec_mask_long)
168 gint seq_delta = gst_rtp_buffer_compare_seqnum (fec_seq_base, seq);
170 && seq_delta <= RTP_ULPFEC_SEQ_BASE_OFFSET_MAX (fec_mask_long))
171 return 1ULL << (RTP_ULPFEC_SEQ_BASE_OFFSET_MAX (TRUE) - seq_delta);
176 rtp_ulpfec_mask_is_long (guint64 mask)
178 return (mask & 0xffffffff) ? TRUE : FALSE;
182 rtp_ulpfec_buffer_is_valid (GstRTPBuffer * rtp)
184 guint payload_len = gst_rtp_buffer_get_payload_len (rtp);
185 RtpUlpFecHeader *fec_hdr;
187 guint fec_packet_len;
189 if (payload_len < sizeof (RtpUlpFecHeader))
192 fec_hdr = rtp_ulpfec_buffer_get_fechdr (rtp);
196 fec_hdrs_len = rtp_ulpfec_get_headers_len (fec_hdr->L);
197 if (payload_len < fec_hdrs_len)
200 fec_packet_len = fec_hdrs_len + rtp_ulpfec_hdr_get_protection_len (fec_hdr);
201 if (fec_packet_len != payload_len)
206 GST_WARNING ("FEC packet too small");
210 GST_WARNING ("invalid FEC packet (declared length %u, real length %u)",
211 fec_packet_len, payload_len);
215 GST_WARNING ("FEC Header contains invalid fields: %u", fec_hdr->E);
221 rtp_buffer_to_ulpfec_bitstring (GstRTPBuffer * rtp, GArray * dst_arr,
222 gboolean fec_buffer, gboolean fec_mask_long)
224 if (G_UNLIKELY (fec_buffer)) {
225 guint payload_len = gst_rtp_buffer_get_payload_len (rtp);
226 g_array_set_size (dst_arr, MAX (payload_len, dst_arr->len));
227 memcpy (dst_arr->data, gst_rtp_buffer_get_payload (rtp), payload_len);
229 const guint8 *src = rtp->data[0];
230 guint len = gst_rtp_buffer_get_packet_len (rtp) - MIN_RTP_HEADER_LEN;
231 guint dst_offset = rtp_ulpfec_get_headers_len (fec_mask_long);
232 guint src_offset = MIN_RTP_HEADER_LEN;
235 g_array_set_size (dst_arr, MAX (dst_offset + len, dst_arr->len));
236 dst = (guint8 *) dst_arr->data;
238 *((guint64 *) dst) ^= *((const guint64 *) src);
239 ((RtpUlpFecHeader *) dst)->len ^= g_htons (len);
240 _xor_mem (dst + dst_offset, src + src_offset, len);
245 rtp_ulpfec_bitstring_to_media_rtp_buffer (GArray * arr,
246 gboolean fec_mask_long, guint32 ssrc, guint16 seq)
248 guint fec_hdrs_len = rtp_ulpfec_get_headers_len (fec_mask_long);
250 fec_hdr_get_packets_len_recovery ((RtpUlpFecHeader *) arr->data);
251 GstMapInfo ret_info = GST_MAP_INFO_INIT;
255 if (payload_len > arr->len - fec_hdrs_len)
256 return NULL; // Not enough data
258 ret_mem = gst_allocator_alloc (NULL, MIN_RTP_HEADER_LEN + payload_len, NULL);
259 gst_memory_map (ret_mem, &ret_info, GST_MAP_READWRITE);
261 /* Filling 12 bytes of RTP header */
262 *((guint64 *) ret_info.data) = *((guint64 *) arr->data);
263 ((RtpHeader *) ret_info.data)->version = 2;
264 ((RtpHeader *) ret_info.data)->seq = g_htons (seq);
265 ((RtpHeader *) ret_info.data)->ssrc = g_htonl (ssrc);
266 /* Filling payload */
267 memcpy (ret_info.data + MIN_RTP_HEADER_LEN,
268 arr->data + fec_hdrs_len, payload_len);
270 gst_memory_unmap (ret_mem, &ret_info);
271 ret = gst_buffer_new ();
272 gst_buffer_append_memory (ret, ret_mem);
277 rtp_ulpfec_bitstring_to_fec_rtp_buffer (GArray * arr,
278 guint16 seq_base, gboolean fec_mask_long, guint64 fec_mask,
279 gboolean marker, guint8 pt, guint16 seq, guint32 timestamp, guint32 ssrc)
281 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
284 /* Filling FEC headers */
286 RtpUlpFecHeader *hdr = (RtpUlpFecHeader *) arr->data;
287 RtpUlpFecLevelHeader *lvlhdr;
289 hdr->L = fec_mask_long;
290 hdr->seq = g_htons (seq_base);
292 lvlhdr = fec_hdr_get_level_hdr (hdr);
293 fec_level_hdr_set_protection_len (lvlhdr,
294 arr->len - rtp_ulpfec_get_headers_len (fec_mask_long));
295 fec_level_hdr_set_mask (lvlhdr, fec_mask_long, fec_mask);
298 /* Filling RTP header, copying payload */
299 ret = gst_rtp_buffer_new_allocate (arr->len, 0, 0);
300 if (!gst_rtp_buffer_map (ret, GST_MAP_READWRITE, &rtp))
301 g_assert_not_reached ();
303 gst_rtp_buffer_set_marker (&rtp, marker);
304 gst_rtp_buffer_set_payload_type (&rtp, pt);
305 gst_rtp_buffer_set_seq (&rtp, seq);
306 gst_rtp_buffer_set_timestamp (&rtp, timestamp);
307 gst_rtp_buffer_set_ssrc (&rtp, ssrc);
309 memcpy (gst_rtp_buffer_get_payload (&rtp), arr->data, arr->len);
311 gst_rtp_buffer_unmap (&rtp);
317 * rtp_ulpfec_map_info_map:
318 * @buffer: (transfer: full) #GstBuffer
319 * @info: #RtpUlpFecMapInfo
321 * Maps the contents of @buffer into @info. If @buffer made of many #GstMemory
322 * objects, merges them together to create a new buffer made of single
323 * continious #GstMemory.
325 * Returns: %TRUE if @buffer could be mapped
328 rtp_ulpfec_map_info_map (GstBuffer * buffer, RtpUlpFecMapInfo * info)
330 /* We need to make sure we are working with continious memory chunk.
331 * If not merge all memories together */
332 if (gst_buffer_n_memory (buffer) > 1) {
333 GstBuffer *new_buffer = gst_buffer_new ();
334 GstMemory *mem = gst_buffer_get_all_memory (buffer);
335 gst_buffer_append_memory (new_buffer, mem);
337 /* We supposed to own the old buffer, but we don't use it here, so unref */
338 gst_buffer_unref (buffer);
342 if (!gst_rtp_buffer_map (buffer,
343 GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &info->rtp)) {
344 /* info->rtp.buffer = NULL is an indication for rtp_ulpfec_map_info_unmap()
345 * that mapping has failed */
346 g_assert (NULL == info->rtp.buffer);
347 gst_buffer_unref (buffer);
354 * rtp_ulpfec_map_info_unmap:
355 * @info: #RtpUlpFecMapInfo
357 * Unmap @info previously mapped with rtp_ulpfec_map_info_map() and unrefs the
358 * buffer. For convinience can even be called even if rtp_ulpfec_map_info_map
362 rtp_ulpfec_map_info_unmap (RtpUlpFecMapInfo * info)
364 GstBuffer *buffer = info->rtp.buffer;
367 gst_rtp_buffer_unmap (&info->rtp);
368 gst_buffer_unref (buffer);
373 rtp_ulpfec_log_rtppacket (GstDebugCategory * cat, GstDebugLevel level,
374 gpointer object, const gchar * name, GstRTPBuffer * rtp)
381 if (level > gst_debug_category_get_threshold (cat))
384 seq = gst_rtp_buffer_get_seq (rtp);
385 ssrc = gst_rtp_buffer_get_ssrc (rtp);
386 timestamp = gst_rtp_buffer_get_timestamp (rtp);
387 pt = gst_rtp_buffer_get_payload_type (rtp);
389 GST_CAT_LEVEL_LOG (cat, level, object,
390 "%-22s: [%c%c%c%c] ssrc=0x%08x pt=%u tstamp=%u seq=%u size=%u(%u,%u)",
392 gst_rtp_buffer_get_marker (rtp) ? 'M' : ' ',
393 gst_rtp_buffer_get_extension (rtp) ? 'X' : ' ',
394 gst_rtp_buffer_get_padding (rtp) ? 'P' : ' ',
395 gst_rtp_buffer_get_csrc_count (rtp) > 0 ? 'C' : ' ',
396 ssrc, pt, timestamp, seq,
397 gst_rtp_buffer_get_packet_len (rtp),
398 gst_rtp_buffer_get_packet_len (rtp) - MIN_RTP_HEADER_LEN,
399 gst_rtp_buffer_get_payload_len (rtp));
403 rtp_ulpfec_log_fec_packet (GstDebugCategory * cat, GstDebugLevel level,
404 gpointer object, GstRTPBuffer * fecrtp)
406 RtpUlpFecHeader *fec_hdr;
407 RtpUlpFecLevelHeader *fec_level_hdr;
409 if (level > gst_debug_category_get_threshold (cat))
412 fec_hdr = gst_rtp_buffer_get_payload (fecrtp);
413 GST_CAT_LEVEL_LOG (cat, level, object,
414 "%-22s: [%c%c%c%c%c%c] pt=%u tstamp=%u seq=%u recovery_len=%u",
416 fec_hdr->E ? 'E' : ' ',
417 fec_hdr->L ? 'L' : ' ',
418 fec_hdr->P ? 'P' : ' ',
419 fec_hdr->X ? 'X' : ' ',
420 fec_hdr->CC ? 'C' : ' ',
421 fec_hdr->M ? 'M' : ' ',
423 fec_hdr_get_timestamp_recovery (fec_hdr),
424 fec_hdr_get_seq_base (fec_hdr, TRUE,
425 gst_rtp_buffer_get_seq (fecrtp)),
426 fec_hdr_get_packets_len_recovery (fec_hdr));
428 fec_level_hdr = fec_hdr_get_level_hdr (fec_hdr);
429 GST_CAT_LEVEL_LOG (cat, level, object,
430 "%-22s: protection_len=%u mask=0x%012lx",
432 g_ntohs (fec_level_hdr->protection_len),
433 fec_level_hdr_get_mask (fec_level_hdr, fec_hdr->L));