2 * Copyright (C) <2005> Philippe Khalaf <burger@speedy.org>
3 * Copyright (C) <2006> Wim Taymans <wim@fluendo.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:gstrtpbuffer
23 * @title: GstRTPBuffer
24 * @short_description: Helper methods for dealing with RTP buffers
25 * @see_also: #GstRTPBasePayload, #GstRTPBaseDepayload, gstrtcpbuffer
27 * The GstRTPBuffer helper functions makes it easy to parse and create regular
28 * #GstBuffer objects that contain RTP payloads. These buffers are typically of
29 * 'application/x-rtp' #GstCaps.
36 #include "gstrtpbuffer.h"
41 #define GST_RTP_HEADER_LEN 12
43 /* Note: we use bitfields here to make sure the compiler doesn't add padding
44 * between fields on certain architectures; can't assume aligned access either
46 typedef struct _GstRTPHeader
48 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
49 unsigned int csrc_count:4; /* CSRC count */
50 unsigned int extension:1; /* header extension flag */
51 unsigned int padding:1; /* padding flag */
52 unsigned int version:2; /* protocol version */
53 unsigned int payload_type:7; /* payload type */
54 unsigned int marker:1; /* marker bit */
55 #elif G_BYTE_ORDER == G_BIG_ENDIAN
56 unsigned int version:2; /* protocol version */
57 unsigned int padding:1; /* padding flag */
58 unsigned int extension:1; /* header extension flag */
59 unsigned int csrc_count:4; /* CSRC count */
60 unsigned int marker:1; /* marker bit */
61 unsigned int payload_type:7; /* payload type */
63 #error "G_BYTE_ORDER should be big or little endian."
65 unsigned int seq:16; /* sequence number */
66 unsigned int timestamp:32; /* timestamp */
67 unsigned int ssrc:32; /* synchronization source */
68 guint8 csrclist[4]; /* optional CSRC list, 32 bits each */
71 #define GST_RTP_HEADER_VERSION(data) (((GstRTPHeader *)(data))->version)
72 #define GST_RTP_HEADER_PADDING(data) (((GstRTPHeader *)(data))->padding)
73 #define GST_RTP_HEADER_EXTENSION(data) (((GstRTPHeader *)(data))->extension)
74 #define GST_RTP_HEADER_CSRC_COUNT(data) (((GstRTPHeader *)(data))->csrc_count)
75 #define GST_RTP_HEADER_MARKER(data) (((GstRTPHeader *)(data))->marker)
76 #define GST_RTP_HEADER_PAYLOAD_TYPE(data) (((GstRTPHeader *)(data))->payload_type)
77 #define GST_RTP_HEADER_SEQ(data) (((GstRTPHeader *)(data))->seq)
78 #define GST_RTP_HEADER_TIMESTAMP(data) (((GstRTPHeader *)(data))->timestamp)
79 #define GST_RTP_HEADER_SSRC(data) (((GstRTPHeader *)(data))->ssrc)
80 #define GST_RTP_HEADER_CSRC_LIST_OFFSET(data,i) \
81 data + G_STRUCT_OFFSET(GstRTPHeader, csrclist) + \
82 ((i) * sizeof(guint32))
83 #define GST_RTP_HEADER_CSRC_SIZE(data) (GST_RTP_HEADER_CSRC_COUNT(data) * sizeof (guint32))
86 * gst_rtp_buffer_allocate_data:
87 * @buffer: a #GstBuffer
88 * @payload_len: the length of the payload
89 * @pad_len: the amount of padding
90 * @csrc_count: the number of CSRC entries
92 * Allocate enough data in @buffer to hold an RTP packet with @csrc_count CSRCs,
93 * a payload length of @payload_len and padding of @pad_len.
94 * @buffer must be writable and all previous memory in @buffer will be freed.
95 * If @pad_len is >0, the padding bit will be set. All other RTP header fields
96 * will be set to 0/FALSE.
99 gst_rtp_buffer_allocate_data (GstBuffer * buffer, guint payload_len,
100 guint8 pad_len, guint8 csrc_count)
106 g_return_if_fail (csrc_count <= 15);
107 g_return_if_fail (GST_IS_BUFFER (buffer));
108 g_return_if_fail (gst_buffer_is_writable (buffer));
110 gst_buffer_remove_all_memory (buffer);
112 hlen = GST_RTP_HEADER_LEN + csrc_count * sizeof (guint32);
114 mem = gst_allocator_alloc (NULL, hlen, NULL);
116 gst_memory_map (mem, &map, GST_MAP_WRITE);
117 /* fill in defaults */
118 GST_RTP_HEADER_VERSION (map.data) = GST_RTP_VERSION;
120 GST_RTP_HEADER_PADDING (map.data) = TRUE;
122 GST_RTP_HEADER_PADDING (map.data) = FALSE;
123 GST_RTP_HEADER_EXTENSION (map.data) = FALSE;
124 GST_RTP_HEADER_CSRC_COUNT (map.data) = csrc_count;
125 memset (GST_RTP_HEADER_CSRC_LIST_OFFSET (map.data, 0), 0,
126 csrc_count * sizeof (guint32));
127 GST_RTP_HEADER_MARKER (map.data) = FALSE;
128 GST_RTP_HEADER_PAYLOAD_TYPE (map.data) = 0;
129 GST_RTP_HEADER_SEQ (map.data) = 0;
130 GST_RTP_HEADER_TIMESTAMP (map.data) = 0;
131 GST_RTP_HEADER_SSRC (map.data) = 0;
132 gst_memory_unmap (mem, &map);
134 gst_buffer_append_memory (buffer, mem);
137 mem = gst_allocator_alloc (NULL, payload_len, NULL);
138 gst_buffer_append_memory (buffer, mem);
141 mem = gst_allocator_alloc (NULL, pad_len, NULL);
143 gst_memory_map (mem, &map, GST_MAP_WRITE);
144 map.data[pad_len - 1] = pad_len;
145 gst_memory_unmap (mem, &map);
147 gst_buffer_append_memory (buffer, mem);
152 * gst_rtp_buffer_new_take_data:
153 * @data: (array length=len) (transfer full) (element-type guint8):
154 * data for the new buffer
155 * @len: the length of data
157 * Create a new buffer and set the data and size of the buffer to @data and @len
158 * respectively. @data will be freed when the buffer is unreffed, so this
159 * function transfers ownership of @data to the new buffer.
161 * Returns: A newly allocated buffer with @data and of size @len.
164 gst_rtp_buffer_new_take_data (gpointer data, gsize len)
166 g_return_val_if_fail (data != NULL, NULL);
167 g_return_val_if_fail (len > 0, NULL);
169 return gst_buffer_new_wrapped (data, len);
173 * gst_rtp_buffer_new_copy_data:
174 * @data: (array length=len) (element-type guint8): data for the new
176 * @len: the length of data
178 * Create a new buffer and set the data to a copy of @len
179 * bytes of @data and the size to @len. The data will be freed when the buffer
182 * Returns: A newly allocated buffer with a copy of @data and of size @len.
185 gst_rtp_buffer_new_copy_data (gconstpointer data, gsize len)
187 return gst_rtp_buffer_new_take_data (g_memdup2 (data, len), len);
191 * gst_rtp_buffer_new_allocate:
192 * @payload_len: the length of the payload
193 * @pad_len: the amount of padding
194 * @csrc_count: the number of CSRC entries
196 * Allocate a new #GstBuffer with enough data to hold an RTP packet with
197 * @csrc_count CSRCs, a payload length of @payload_len and padding of @pad_len.
198 * All other RTP header fields will be set to 0/FALSE.
200 * Returns: A newly allocated buffer that can hold an RTP packet with given
204 gst_rtp_buffer_new_allocate (guint payload_len, guint8 pad_len,
209 g_return_val_if_fail (csrc_count <= 15, NULL);
211 result = gst_buffer_new ();
212 gst_rtp_buffer_allocate_data (result, payload_len, pad_len, csrc_count);
218 * gst_rtp_buffer_new_allocate_len:
219 * @packet_len: the total length of the packet
220 * @pad_len: the amount of padding
221 * @csrc_count: the number of CSRC entries
223 * Create a new #GstBuffer that can hold an RTP packet that is exactly
224 * @packet_len long. The length of the payload depends on @pad_len and
225 * @csrc_count and can be calculated with gst_rtp_buffer_calc_payload_len().
226 * All RTP header fields will be set to 0/FALSE.
228 * Returns: A newly allocated buffer that can hold an RTP packet of @packet_len.
231 gst_rtp_buffer_new_allocate_len (guint packet_len, guint8 pad_len,
236 g_return_val_if_fail (csrc_count <= 15, NULL);
238 len = gst_rtp_buffer_calc_payload_len (packet_len, pad_len, csrc_count);
240 return gst_rtp_buffer_new_allocate (len, pad_len, csrc_count);
244 * gst_rtp_buffer_calc_header_len:
245 * @csrc_count: the number of CSRC entries
247 * Calculate the header length of an RTP packet with @csrc_count CSRC entries.
248 * An RTP packet can have at most 15 CSRC entries.
250 * Returns: The length of an RTP header with @csrc_count CSRC entries.
253 gst_rtp_buffer_calc_header_len (guint8 csrc_count)
255 g_return_val_if_fail (csrc_count <= 15, 0);
257 return GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32));
261 * gst_rtp_buffer_calc_packet_len:
262 * @payload_len: the length of the payload
263 * @pad_len: the amount of padding
264 * @csrc_count: the number of CSRC entries
266 * Calculate the total length of an RTP packet with a payload size of @payload_len,
267 * a padding of @pad_len and a @csrc_count CSRC entries.
269 * Returns: The total length of an RTP header with given parameters.
272 gst_rtp_buffer_calc_packet_len (guint payload_len, guint8 pad_len,
275 g_return_val_if_fail (csrc_count <= 15, 0);
277 return payload_len + GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32))
282 * gst_rtp_buffer_calc_payload_len:
283 * @packet_len: the length of the total RTP packet
284 * @pad_len: the amount of padding
285 * @csrc_count: the number of CSRC entries
287 * Calculate the length of the payload of an RTP packet with size @packet_len,
288 * a padding of @pad_len and a @csrc_count CSRC entries.
290 * Returns: The length of the payload of an RTP packet with given parameters.
293 gst_rtp_buffer_calc_payload_len (guint packet_len, guint8 pad_len,
296 g_return_val_if_fail (csrc_count <= 15, 0);
299 GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32)) + pad_len)
302 return packet_len - GST_RTP_HEADER_LEN - (csrc_count * sizeof (guint32))
307 * gst_rtp_buffer_map:
308 * @buffer: a #GstBuffer
309 * @flags: #GstMapFlags
310 * @rtp: (out): a #GstRTPBuffer
312 * Map the contents of @buffer into @rtp.
314 * Returns: %TRUE if @buffer could be mapped.
317 gst_rtp_buffer_map (GstBuffer * buffer, GstMapFlags flags, GstRTPBuffer * rtp)
329 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
330 g_return_val_if_fail (rtp != NULL, FALSE);
331 g_return_val_if_fail (rtp->buffer == NULL, FALSE);
333 n_mem = gst_buffer_n_memory (buffer);
337 /* map first memory, this should be the header */
338 if (!gst_buffer_map_range (buffer, 0, 1, &rtp->map[0], flags))
341 data = rtp->data[0] = rtp->map[0].data;
342 size = rtp->map[0].size;
344 /* the header must be completely in the first buffer */
345 header_len = GST_RTP_HEADER_LEN;
346 if (G_UNLIKELY (size < header_len))
350 version = (data[0] & 0xc0);
351 if (G_UNLIKELY (version != (GST_RTP_VERSION << 6)))
354 /* check reserved PT and marker bit, this is to check for RTCP
355 * packets. We do a relaxed check, you can still use 72-76 as long
356 * as the marker bit is cleared. */
358 if (G_UNLIKELY (pt >= 200 && pt <= 204))
361 /* calc header length with csrc */
362 csrc_count = (data[0] & 0x0f);
363 header_len += csrc_count * sizeof (guint32);
365 rtp->size[0] = header_len;
367 bufsize = gst_buffer_get_size (buffer);
369 /* calc extension length when present. */
370 if (data[0] & 0x10) {
374 /* find memory for the extension bits, we find the block for the first 4
375 * bytes, all other extension bytes should also be in this block */
376 if (!gst_buffer_find_memory (buffer, header_len, 4, &idx, &length, &skip))
379 if (!gst_buffer_map_range (buffer, idx, length, &rtp->map[1], flags))
382 extdata = rtp->data[1] = rtp->map[1].data + skip;
385 /* read length as the number of 32 bits words */
386 extlen = GST_READ_UINT16_BE (extdata);
387 extlen *= sizeof (guint32);
388 /* add id and length */
391 /* all extension bytes must be in this block */
392 if (G_UNLIKELY (rtp->map[1].size < extlen))
395 rtp->size[1] = extlen;
397 header_len += rtp->size[1];
403 /* check for padding unless flags says to skip */
404 if ((data[0] & 0x20) != 0 &&
405 (flags & GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING) == 0) {
406 /* find memory for the padding bits */
407 if (!gst_buffer_find_memory (buffer, bufsize - 1, 1, &idx, &length, &skip))
410 if (!gst_buffer_map_range (buffer, idx, length, &rtp->map[3], flags))
413 padding = rtp->map[3].data[skip];
414 rtp->data[3] = rtp->map[3].data + skip + 1 - padding;
415 rtp->size[3] = padding;
417 if (skip + 1 < padding)
425 /* check if padding and header not bigger than packet length */
426 if (G_UNLIKELY (bufsize < padding + header_len))
429 rtp->buffer = buffer;
432 /* we have mapped the buffer already, so might just as well fill in the
433 * payload pointer and size and avoid another buffer map/unmap later */
434 rtp->data[2] = rtp->map[0].data + header_len;
435 rtp->size[2] = bufsize - header_len - padding;
437 /* we have not yet mapped the payload */
442 /* rtp->state = 0; *//* unused */
449 GST_ERROR ("buffer without memory");
454 GST_ERROR ("failed to map memory");
459 GST_DEBUG ("length check failed");
464 GST_DEBUG ("version check failed (%d != %d)", version, GST_RTP_VERSION);
469 GST_DEBUG ("reserved PT %d found", pt);
474 GST_DEBUG ("padding check failed (%" G_GSIZE_FORMAT " - %d < %d)", bufsize,
475 header_len, padding);
482 GST_MEMDUMP ("buffer", data, size);
484 for (i = 0; i < G_N_ELEMENTS (rtp->map); ++i) {
485 if (rtp->map[i].memory != NULL)
486 gst_buffer_unmap (buffer, &rtp->map[i]);
493 * gst_rtp_buffer_unmap:
494 * @rtp: a #GstRTPBuffer
496 * Unmap @rtp previously mapped with gst_rtp_buffer_map().
499 gst_rtp_buffer_unmap (GstRTPBuffer * rtp)
503 g_return_if_fail (rtp != NULL);
504 g_return_if_fail (rtp->buffer != NULL);
506 for (i = 0; i < 4; i++) {
507 if (rtp->map[i].memory != NULL) {
508 gst_buffer_unmap (rtp->buffer, &rtp->map[i]);
509 rtp->map[i].memory = NULL;
519 * gst_rtp_buffer_set_packet_len:
520 * @rtp: the RTP packet
521 * @len: the new packet length
523 * Set the total @rtp size to @len. The data in the buffer will be made
524 * larger if needed. Any padding will be removed from the packet.
527 gst_rtp_buffer_set_packet_len (GstRTPBuffer * rtp, guint len)
535 if (rtp->map[0].maxsize <= len) {
536 /* FIXME, realloc bigger space */
537 g_warning ("not implemented");
540 gst_buffer_set_size (rtp->buffer, len);
541 rtp->map[0].size = len;
543 /* remove any padding */
544 GST_RTP_HEADER_PADDING (data) = FALSE;
548 * gst_rtp_buffer_get_packet_len:
549 * @rtp: the RTP packet
551 * Return the total length of the packet in @buffer.
553 * Returns: The total length of the packet in @buffer.
556 gst_rtp_buffer_get_packet_len (GstRTPBuffer * rtp)
558 return gst_buffer_get_size (rtp->buffer);
562 * gst_rtp_buffer_get_header_len:
563 * @rtp: the RTP packet
565 * Return the total length of the header in @buffer. This include the length of
566 * the fixed header, the CSRC list and the extension header.
568 * Returns: The total length of the header in @buffer.
571 gst_rtp_buffer_get_header_len (GstRTPBuffer * rtp)
573 return rtp->size[0] + rtp->size[1];
577 * gst_rtp_buffer_get_version:
578 * @rtp: the RTP packet
580 * Get the version number of the RTP packet in @buffer.
582 * Returns: The version of @buffer.
585 gst_rtp_buffer_get_version (GstRTPBuffer * rtp)
587 return GST_RTP_HEADER_VERSION (rtp->data[0]);
591 * gst_rtp_buffer_set_version:
592 * @rtp: the RTP packet
593 * @version: the new version
595 * Set the version of the RTP packet in @buffer to @version.
598 gst_rtp_buffer_set_version (GstRTPBuffer * rtp, guint8 version)
600 g_return_if_fail (version < 0x04);
602 GST_RTP_HEADER_VERSION (rtp->data[0]) = version;
606 * gst_rtp_buffer_get_padding:
607 * @rtp: the RTP packet
609 * Check if the padding bit is set on the RTP packet in @buffer.
611 * Returns: TRUE if @buffer has the padding bit set.
614 gst_rtp_buffer_get_padding (GstRTPBuffer * rtp)
616 return GST_RTP_HEADER_PADDING (rtp->data[0]);
620 * gst_rtp_buffer_set_padding:
622 * @padding: the new padding
624 * Set the padding bit on the RTP packet in @buffer to @padding.
627 gst_rtp_buffer_set_padding (GstRTPBuffer * rtp, gboolean padding)
629 GST_RTP_HEADER_PADDING (rtp->data[0]) = padding;
633 * gst_rtp_buffer_pad_to:
634 * @rtp: the RTP packet
635 * @len: the new amount of padding
637 * Set the amount of padding in the RTP packet in @buffer to
638 * @len. If @len is 0, the padding is removed.
640 * NOTE: This function does not work correctly.
643 gst_rtp_buffer_pad_to (GstRTPBuffer * rtp, guint len)
650 GST_RTP_HEADER_PADDING (data) = TRUE;
652 GST_RTP_HEADER_PADDING (data) = FALSE;
654 /* FIXME, set the padding byte at the end of the payload data */
658 * gst_rtp_buffer_get_extension:
659 * @rtp: the RTP packet
661 * Check if the extension bit is set on the RTP packet in @buffer.
663 * Returns: TRUE if @buffer has the extension bit set.
666 gst_rtp_buffer_get_extension (GstRTPBuffer * rtp)
668 return GST_RTP_HEADER_EXTENSION (rtp->data[0]);
672 * gst_rtp_buffer_set_extension:
673 * @rtp: the RTP packet
674 * @extension: the new extension
676 * Set the extension bit on the RTP packet in @buffer to @extension.
679 gst_rtp_buffer_set_extension (GstRTPBuffer * rtp, gboolean extension)
681 GST_RTP_HEADER_EXTENSION (rtp->data[0]) = extension;
685 * gst_rtp_buffer_get_extension_data: (skip)
686 * @rtp: the RTP packet
687 * @bits: (optional) (out): location for result bits
688 * @data: (optional) (out) (array) (element-type guint8) (transfer none): location for data
689 * @wordlen: (optional) (out): location for length of @data in 32 bits words
691 * Get the extension data. @bits will contain the extension 16 bits of custom
692 * data. @data will point to the data in the extension and @wordlen will contain
693 * the length of @data in 32 bits words.
695 * If @buffer did not contain an extension, this function will return %FALSE
696 * with @bits, @data and @wordlen unchanged.
698 * Returns: TRUE if @buffer had the extension bit set.
701 gst_rtp_buffer_get_extension_data (GstRTPBuffer * rtp, guint16 * bits,
702 gpointer * data, guint * wordlen)
706 /* move to the extension */
707 pdata = rtp->data[1];
712 *bits = GST_READ_UINT16_BE (pdata);
714 *wordlen = GST_READ_UINT16_BE (pdata + 2);
717 *data = (gpointer *) pdata;
723 * gst_rtp_buffer_get_extension_bytes: (rename-to gst_rtp_buffer_get_extension_data)
724 * @rtp: the RTP packet
725 * @bits: (out): location for header bits
727 * Similar to gst_rtp_buffer_get_extension_data, but more suitable for language
728 * bindings usage. @bits will contain the extension 16 bits of custom data and
729 * the extension data (not including the extension header) is placed in a new
732 * If @rtp did not contain an extension, this function will return %NULL, with
733 * @bits unchanged. If there is an extension header but no extension data then
734 * an empty #GBytes will be returned.
736 * Returns: (transfer full): A new #GBytes if an extension header was present
737 * and %NULL otherwise.
742 gst_rtp_buffer_get_extension_bytes (GstRTPBuffer * rtp, guint16 * bits)
744 gpointer buf_data = NULL;
747 g_return_val_if_fail (rtp != NULL, FALSE);
749 if (!gst_rtp_buffer_get_extension_data (rtp, bits, &buf_data, &buf_len))
753 /* if no extension data is present return an empty GBytes */
757 /* multiply length with 4 to get length in bytes */
758 return g_bytes_new (buf_data, 4 * buf_len);
762 gst_rtp_buffer_map_payload (GstRTPBuffer * rtp)
768 if (rtp->map[2].memory != NULL)
771 hlen = gst_rtp_buffer_get_header_len (rtp);
772 plen = gst_buffer_get_size (rtp->buffer) - hlen - rtp->size[3];
774 if (!gst_buffer_find_memory (rtp->buffer, hlen, plen, &idx, &length, &skip))
777 if (!gst_buffer_map_range (rtp->buffer, idx, length, &rtp->map[2],
781 rtp->data[2] = rtp->map[2].data + skip;
787 /* ensure header, payload and padding are in separate buffers */
789 ensure_buffers (GstRTPBuffer * rtp)
792 gboolean changed = FALSE;
794 /* make sure payload is mapped */
795 gst_rtp_buffer_map_payload (rtp);
797 for (i = 0, pos = 0; i < 4; i++) {
799 gsize offset = (guint8 *) rtp->data[i] - rtp->map[i].data;
801 if (offset != 0 || rtp->map[i].size != rtp->size[i]) {
805 mem = gst_memory_copy (rtp->map[i].memory, offset, rtp->size[i]);
807 /* insert new memory */
808 gst_buffer_insert_memory (rtp->buffer, pos, mem);
817 GstBuffer *buf = rtp->buffer;
819 gst_rtp_buffer_unmap (rtp);
820 gst_buffer_remove_memory_range (buf, pos, -1);
821 gst_rtp_buffer_map (buf, GST_MAP_READWRITE, rtp);
826 * gst_rtp_buffer_set_extension_data:
827 * @rtp: the RTP packet
828 * @bits: the bits specific for the extension
829 * @length: the length that counts the number of 32-bit words in
830 * the extension, excluding the extension header ( therefore zero is a valid length)
832 * Set the extension bit of the rtp buffer and fill in the @bits and @length of the
833 * extension header. If the existing extension data is not large enough, it will
836 * Will also shorten the extension data from 1.20.
838 * Returns: True if done.
841 gst_rtp_buffer_set_extension_data (GstRTPBuffer * rtp, guint16 bits,
844 guint32 min_size = 0;
846 GstMemory *mem = NULL;
848 ensure_buffers (rtp);
850 /* this is the size of the extension data we need */
851 min_size = 4 + length * sizeof (guint32);
853 /* we should allocate and map the extension data */
854 if (rtp->data[1] == NULL || min_size > rtp->size[1]) {
857 /* we don't have (enough) extension data, make some */
858 mem = gst_allocator_alloc (NULL, min_size, NULL);
861 /* copy old data & initialize the remainder of the new buffer */
862 gst_memory_map (mem, &map, GST_MAP_WRITE);
863 memcpy (map.data, rtp->data[1], rtp->size[1]);
864 if (min_size > rtp->size[1]) {
865 memset (map.data + rtp->size[1], 0, min_size - rtp->size[1]);
867 gst_memory_unmap (mem, &map);
870 gst_buffer_unmap (rtp->buffer, &rtp->map[1]);
871 gst_buffer_replace_memory (rtp->buffer, 1, mem);
873 /* don't leak data from uninitialized memory via the padding */
874 gst_memory_map (mem, &map, GST_MAP_WRITE);
875 memset (map.data, 0, map.size);
876 gst_memory_unmap (mem, &map);
878 /* we didn't have extension data, add */
879 gst_buffer_insert_memory (rtp->buffer, 1, mem);
883 gst_memory_map (mem, &rtp->map[1], GST_MAP_READWRITE);
884 gst_memory_ref (mem);
885 rtp->data[1] = rtp->map[1].data;
886 rtp->size[1] = rtp->map[1].size;
887 } else if (min_size < rtp->size[1]) {
888 GstMemory *mem = rtp->map[1].memory;
890 gst_memory_ref (mem);
891 gst_buffer_unmap (rtp->buffer, &rtp->map[1]);
892 gst_memory_resize (mem, 0, min_size);
893 gst_memory_map (mem, &rtp->map[1], GST_MAP_READWRITE);
894 rtp->data[1] = rtp->map[1].data;
895 rtp->size[1] = rtp->map[1].size;
898 /* now we can set the extension bit */
900 GST_RTP_HEADER_EXTENSION (data) = TRUE;
903 GST_WRITE_UINT16_BE (data, bits);
904 GST_WRITE_UINT16_BE (data + 2, length);
910 * gst_rtp_buffer_remove_extension_data:
911 * @rtp: the RTP packet
913 * Unsets the extension bit of the RTP buffer and removes the extension header
916 * If the RTP buffer has no header extension data, the action has no effect.
917 * The RTP buffer must be mapped READWRITE only once and the underlying
918 * GstBuffer must be writable.
923 gst_rtp_buffer_remove_extension_data (GstRTPBuffer * rtp)
925 g_return_if_fail (gst_buffer_is_writable (rtp->buffer));
926 g_return_if_fail (rtp->map[0].flags & GST_MAP_WRITE);
928 if (rtp->data[1] != NULL) {
929 GstBuffer *buf = rtp->buffer;
931 ensure_buffers (rtp);
933 GST_RTP_HEADER_EXTENSION (rtp->data[0]) = FALSE;
934 gst_rtp_buffer_unmap (rtp);
935 gst_buffer_remove_memory (buf, 1);
936 gst_rtp_buffer_map (buf, GST_MAP_READWRITE, rtp);
941 * gst_rtp_buffer_get_ssrc:
942 * @rtp: the RTP packet
944 * Get the SSRC of the RTP packet in @buffer.
946 * Returns: the SSRC of @buffer in host order.
949 gst_rtp_buffer_get_ssrc (GstRTPBuffer * rtp)
951 return g_ntohl (GST_RTP_HEADER_SSRC (rtp->data[0]));
955 * gst_rtp_buffer_set_ssrc:
956 * @rtp: the RTP packet
957 * @ssrc: the new SSRC
959 * Set the SSRC on the RTP packet in @buffer to @ssrc.
962 gst_rtp_buffer_set_ssrc (GstRTPBuffer * rtp, guint32 ssrc)
964 GST_RTP_HEADER_SSRC (rtp->data[0]) = g_htonl (ssrc);
968 * gst_rtp_buffer_get_csrc_count:
969 * @rtp: the RTP packet
971 * Get the CSRC count of the RTP packet in @buffer.
973 * Returns: the CSRC count of @buffer.
976 gst_rtp_buffer_get_csrc_count (GstRTPBuffer * rtp)
978 return GST_RTP_HEADER_CSRC_COUNT (rtp->data[0]);
982 * gst_rtp_buffer_get_csrc:
983 * @rtp: the RTP packet
984 * @idx: the index of the CSRC to get
986 * Get the CSRC at index @idx in @buffer.
988 * Returns: the CSRC at index @idx in host order.
991 gst_rtp_buffer_get_csrc (GstRTPBuffer * rtp, guint8 idx)
997 g_return_val_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data), 0);
999 return GST_READ_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx));
1003 * gst_rtp_buffer_set_csrc:
1004 * @rtp: the RTP packet
1005 * @idx: the CSRC index to set
1006 * @csrc: the CSRC in host order to set at @idx
1008 * Modify the CSRC at index @idx in @buffer to @csrc.
1011 gst_rtp_buffer_set_csrc (GstRTPBuffer * rtp, guint8 idx, guint32 csrc)
1015 data = rtp->data[0];
1017 g_return_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data));
1019 GST_WRITE_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx), csrc);
1023 * gst_rtp_buffer_get_marker:
1024 * @rtp: the RTP packet
1026 * Check if the marker bit is set on the RTP packet in @buffer.
1028 * Returns: TRUE if @buffer has the marker bit set.
1031 gst_rtp_buffer_get_marker (GstRTPBuffer * rtp)
1033 return GST_RTP_HEADER_MARKER (rtp->data[0]);
1037 * gst_rtp_buffer_set_marker:
1038 * @rtp: the RTP packet
1039 * @marker: the new marker
1041 * Set the marker bit on the RTP packet in @buffer to @marker.
1044 gst_rtp_buffer_set_marker (GstRTPBuffer * rtp, gboolean marker)
1046 GST_RTP_HEADER_MARKER (rtp->data[0]) = marker;
1050 * gst_rtp_buffer_get_payload_type:
1051 * @rtp: the RTP packet
1053 * Get the payload type of the RTP packet in @buffer.
1055 * Returns: The payload type.
1058 gst_rtp_buffer_get_payload_type (GstRTPBuffer * rtp)
1060 return GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]);
1064 * gst_rtp_buffer_set_payload_type:
1065 * @rtp: the RTP packet
1066 * @payload_type: the new type
1068 * Set the payload type of the RTP packet in @buffer to @payload_type.
1071 gst_rtp_buffer_set_payload_type (GstRTPBuffer * rtp, guint8 payload_type)
1073 g_return_if_fail (payload_type < 0x80);
1075 GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]) = payload_type;
1079 * gst_rtp_buffer_get_seq:
1080 * @rtp: the RTP packet
1082 * Get the sequence number of the RTP packet in @buffer.
1084 * Returns: The sequence number in host order.
1087 gst_rtp_buffer_get_seq (GstRTPBuffer * rtp)
1089 return g_ntohs (GST_RTP_HEADER_SEQ (rtp->data[0]));
1093 * gst_rtp_buffer_set_seq:
1094 * @rtp: the RTP packet
1095 * @seq: the new sequence number
1097 * Set the sequence number of the RTP packet in @buffer to @seq.
1100 gst_rtp_buffer_set_seq (GstRTPBuffer * rtp, guint16 seq)
1102 GST_RTP_HEADER_SEQ (rtp->data[0]) = g_htons (seq);
1106 * gst_rtp_buffer_get_timestamp:
1107 * @rtp: the RTP packet
1109 * Get the timestamp of the RTP packet in @buffer.
1111 * Returns: The timestamp in host order.
1114 gst_rtp_buffer_get_timestamp (GstRTPBuffer * rtp)
1116 return g_ntohl (GST_RTP_HEADER_TIMESTAMP (rtp->data[0]));
1120 * gst_rtp_buffer_set_timestamp:
1121 * @rtp: the RTP packet
1122 * @timestamp: the new timestamp
1124 * Set the timestamp of the RTP packet in @buffer to @timestamp.
1127 gst_rtp_buffer_set_timestamp (GstRTPBuffer * rtp, guint32 timestamp)
1129 GST_RTP_HEADER_TIMESTAMP (rtp->data[0]) = g_htonl (timestamp);
1134 * gst_rtp_buffer_get_payload_subbuffer:
1135 * @rtp: the RTP packet
1136 * @offset: the offset in the payload
1137 * @len: the length in the payload
1139 * Create a subbuffer of the payload of the RTP packet in @buffer. @offset bytes
1140 * are skipped in the payload and the subbuffer will be of size @len.
1141 * If @len is -1 the total payload starting from @offset is subbuffered.
1143 * Returns: A new buffer with the specified data of the payload.
1146 gst_rtp_buffer_get_payload_subbuffer (GstRTPBuffer * rtp, guint offset,
1149 guint poffset, plen;
1151 plen = gst_rtp_buffer_get_payload_len (rtp);
1152 /* we can't go past the length */
1153 if (G_UNLIKELY (offset > plen))
1157 poffset = gst_rtp_buffer_get_header_len (rtp) + offset;
1160 /* see if we need to shrink the buffer based on @len */
1161 if (len != -1 && len < plen)
1164 return gst_buffer_copy_region (rtp->buffer, GST_BUFFER_COPY_ALL, poffset,
1170 g_warning ("offset=%u should be less than plen=%u", offset, plen);
1176 * gst_rtp_buffer_get_payload_buffer:
1177 * @rtp: the RTP packet
1179 * Create a buffer of the payload of the RTP packet in @buffer. This function
1180 * will internally create a subbuffer of @buffer so that a memcpy can be
1183 * Returns: A new buffer with the data of the payload.
1186 gst_rtp_buffer_get_payload_buffer (GstRTPBuffer * rtp)
1188 return gst_rtp_buffer_get_payload_subbuffer (rtp, 0, -1);
1192 * gst_rtp_buffer_get_payload_len:
1193 * @rtp: the RTP packet
1195 * Get the length of the payload of the RTP packet in @buffer.
1197 * Returns: The length of the payload in @buffer.
1200 gst_rtp_buffer_get_payload_len (GstRTPBuffer * rtp)
1202 return gst_buffer_get_size (rtp->buffer) - gst_rtp_buffer_get_header_len (rtp)
1207 * gst_rtp_buffer_get_payload: (skip)
1208 * @rtp: the RTP packet
1210 * Get a pointer to the payload data in @buffer. This pointer is valid as long
1211 * as a reference to @buffer is held.
1213 * Returns: (array) (element-type guint8) (transfer none): A pointer
1214 * to the payload data in @buffer.
1217 gst_rtp_buffer_get_payload (GstRTPBuffer * rtp)
1220 return rtp->data[2];
1222 if (!gst_rtp_buffer_map_payload (rtp))
1225 return rtp->data[2];
1229 * gst_rtp_buffer_get_payload_bytes: (rename-to gst_rtp_buffer_get_payload)
1230 * @rtp: the RTP packet
1232 * Similar to gst_rtp_buffer_get_payload, but more suitable for language
1233 * bindings usage. The return value is a pointer to a #GBytes structure
1234 * containing the payload data in @rtp.
1236 * Returns: (transfer full): A new #GBytes containing the payload data in @rtp.
1241 gst_rtp_buffer_get_payload_bytes (GstRTPBuffer * rtp)
1245 g_return_val_if_fail (rtp != NULL, NULL);
1247 data = gst_rtp_buffer_get_payload (rtp);
1251 return g_bytes_new (data, gst_rtp_buffer_get_payload_len (rtp));
1255 * gst_rtp_buffer_default_clock_rate:
1256 * @payload_type: the static payload type
1258 * Get the default clock-rate for the static payload type @payload_type.
1260 * Returns: the default clock rate or -1 if the payload type is not static or
1261 * the clock-rate is undefined.
1264 gst_rtp_buffer_default_clock_rate (guint8 payload_type)
1266 const GstRTPPayloadInfo *info;
1269 info = gst_rtp_payload_info_for_pt (payload_type);
1273 res = info->clock_rate;
1274 /* 0 means unknown so we have to return -1 from this function */
1282 * gst_rtp_buffer_compare_seqnum:
1283 * @seqnum1: a sequence number
1284 * @seqnum2: a sequence number
1286 * Compare two sequence numbers, taking care of wraparounds. This function
1287 * returns the difference between @seqnum1 and @seqnum2.
1289 * Returns: a negative value if @seqnum1 is bigger than @seqnum2, 0 if they
1290 * are equal or a positive value if @seqnum1 is smaller than @segnum2.
1293 gst_rtp_buffer_compare_seqnum (guint16 seqnum1, guint16 seqnum2)
1295 /* See http://en.wikipedia.org/wiki/Serial_number_arithmetic
1296 * for an explanation why this does the right thing even for
1297 * wraparounds, under the assumption that the difference is
1298 * never bigger than 2**15 sequence numbers
1300 return (gint16) (seqnum2 - seqnum1);
1304 * gst_rtp_buffer_ext_timestamp:
1305 * @exttimestamp: (inout): a previous extended timestamp
1306 * @timestamp: a new timestamp
1308 * Update the @exttimestamp field with the extended timestamp of @timestamp
1309 * For the first call of the method, @exttimestamp should point to a location
1310 * with a value of -1.
1312 * This function is able to handle both forward and backward timestamps taking
1314 * - timestamp wraparound making sure that the returned value is properly increased.
1315 * - timestamp unwraparound making sure that the returned value is properly decreased.
1317 * Returns: The extended timestamp of @timestamp or 0 if the result can't go anywhere backwards.
1320 gst_rtp_buffer_ext_timestamp (guint64 * exttimestamp, guint32 timestamp)
1322 guint64 result, ext;
1324 g_return_val_if_fail (exttimestamp != NULL, -1);
1326 ext = *exttimestamp;
1331 /* pick wraparound counter from previous timestamp and add to new timestamp */
1332 result = timestamp + (ext & ~(G_GUINT64_CONSTANT (0xffffffff)));
1334 /* check for timestamp wraparound */
1336 guint64 diff = ext - result;
1338 if (diff > G_MAXINT32) {
1339 /* timestamp went backwards more than allowed, we wrap around and get
1340 * updated extended timestamp. */
1341 result += (G_GUINT64_CONSTANT (1) << 32);
1344 guint64 diff = result - ext;
1346 if (diff > G_MAXINT32) {
1347 if (result < (G_GUINT64_CONSTANT (1) << 32)) {
1349 ("Cannot unwrap, any wrapping took place yet. Returning 0 without updating extended timestamp.");
1352 /* timestamp went forwards more than allowed, we unwrap around and get
1353 * updated extended timestamp. */
1354 result -= (G_GUINT64_CONSTANT (1) << 32);
1355 /* We don't want the extended timestamp storage to go back, ever */
1362 *exttimestamp = result;
1369 _get_extension_onebyte_header (const guint8 * pdata, guint len,
1370 guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1375 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1377 if (bit_pattern != 0xBEDE)
1381 guint8 read_id, read_len;
1383 if (offset + 1 >= len)
1386 read_id = GST_READ_UINT8 (pdata + offset) >> 4;
1387 read_len = (GST_READ_UINT8 (pdata + offset) & 0x0F) + 1;
1390 /* ID 0 means its padding, skip */
1394 /* ID 15 is special and means we should stop parsing */
1398 /* Ignore extension headers where the size does not fit */
1399 if (offset + read_len > len)
1402 /* If we have the right one */
1403 if (id == read_id) {
1406 *data = (gpointer) & pdata[offset];
1426 * gst_rtp_buffer_get_extension_onebyte_header_from_bytes:
1428 * @bit_pattern: The bit-pattern. Anything but 0xBEDE is rejected.
1429 * @id: The ID of the header extension to be read (between 1 and 14).
1430 * @nth: Read the nth extension packet with the requested ID
1431 * @data: (out) (array length=size) (element-type guint8) (transfer none):
1433 * @size: (out): the size of the data in bytes
1435 * Similar to gst_rtp_buffer_get_extension_onebyte_header, but working
1436 * on the #GBytes you get from gst_rtp_buffer_get_extension_bytes.
1437 * Parses RFC 5285 style header extensions with a one byte header. It will
1438 * return the nth extension with the requested id.
1440 * Returns: TRUE if @bytes had the requested header extension
1445 gst_rtp_buffer_get_extension_onebyte_header_from_bytes (GBytes * bytes,
1446 guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1448 const guint8 *pdata = g_bytes_get_data (bytes, NULL);
1449 gsize len = g_bytes_get_size (bytes);
1450 return _get_extension_onebyte_header (pdata, len, bit_pattern, id, nth, data,
1456 * gst_rtp_buffer_get_extension_onebyte_header:
1457 * @rtp: the RTP packet
1458 * @id: The ID of the header extension to be read (between 1 and 14).
1459 * @nth: Read the nth extension packet with the requested ID
1460 * @data: (optional) (out) (array length=size) (element-type guint8) (transfer none):
1462 * @size: (optional) (out): the size of the data in bytes
1464 * Parses RFC 5285 style header extensions with a one byte header. It will
1465 * return the nth extension with the requested id.
1467 * Returns: TRUE if @buffer had the requested header extension
1471 gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1472 guint nth, gpointer * data, guint * size)
1474 guint16 bit_pattern;
1478 if (!gst_rtp_buffer_get_extension_data (rtp, &bit_pattern, (gpointer) & pdata,
1482 return _get_extension_onebyte_header (pdata, wordlen * 4, bit_pattern, id,
1488 * gst_rtp_buffer_get_extension_twobytes_header:
1489 * @rtp: the RTP packet
1490 * @appbits: (optional) (out): Application specific bits
1491 * @id: The ID of the header extension to be read (between 1 and 14).
1492 * @nth: Read the nth extension packet with the requested ID
1493 * @data: (optional) (out) (array length=size) (element-type guint8) (transfer none):
1495 * @size: (optional) (out): the size of the data in bytes
1497 * Parses RFC 5285 style header extensions with a two bytes header. It will
1498 * return the nth extension with the requested id.
1500 * Returns: TRUE if @buffer had the requested header extension
1504 gst_rtp_buffer_get_extension_twobytes_header (GstRTPBuffer * rtp,
1505 guint8 * appbits, guint8 id, guint nth, gpointer * data, guint * size)
1508 guint8 *pdata = NULL;
1514 if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer *) & pdata,
1518 if (bits >> 4 != 0x100)
1521 bytelen = wordlen * 4;
1524 guint8 read_id, read_len;
1526 if (offset + 2 >= bytelen)
1529 read_id = GST_READ_UINT8 (pdata + offset);
1535 read_len = GST_READ_UINT8 (pdata + offset);
1538 /* Ignore extension headers where the size does not fit */
1539 if (offset + read_len > bytelen)
1542 /* If we have the right one, return it */
1543 if (id == read_id) {
1546 *data = pdata + offset;
1564 get_onebyte_header_end_offset (guint8 * pdata, guint wordlen, guint * offset)
1566 guint bytelen = wordlen * 4;
1567 guint paddingcount = 0;
1571 while (*offset + 1 < bytelen) {
1572 guint8 read_id, read_len;
1574 read_id = GST_READ_UINT8 (pdata + *offset) >> 4;
1575 read_len = (GST_READ_UINT8 (pdata + *offset) & 0x0F) + 1;
1578 /* ID 0 means its padding, skip */
1586 /* ID 15 is special and means we should stop parsing */
1587 /* It also means we can't add an extra packet */
1588 if (read_id == 15) {
1592 /* Ignore extension headers where the size does not fit */
1593 if (*offset + read_len > bytelen) {
1597 *offset += read_len;
1600 *offset -= paddingcount;
1606 * gst_rtp_buffer_add_extension_onebyte_header:
1607 * @rtp: the RTP packet
1608 * @id: The ID of the header extension (between 1 and 14).
1609 * @data: (array length=size) (element-type guint8): location for data
1610 * @size: the size of the data in bytes
1612 * Adds a RFC 5285 header extension with a one byte header to the end of the
1613 * RTP header. If there is already a RFC 5285 header extension with a one byte
1614 * header, the new extension will be appended.
1615 * It will not work if there is already a header extension that does not follow
1616 * the mechanism described in RFC 5285 or if there is a header extension with
1617 * a two bytes header as described in RFC 5285. In that case, use
1618 * gst_rtp_buffer_add_extension_twobytes_header()
1620 * Returns: %TRUE if header extension could be added
1624 gst_rtp_buffer_add_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1625 gconstpointer data, guint size)
1632 guint extlen, offset = 0;
1634 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1635 g_return_val_if_fail (size >= 1 && size <= 16, FALSE);
1636 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1638 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1639 (gpointer) & pdata, &wordlen);
1645 if (!get_onebyte_header_end_offset (pdata, wordlen, &offset))
1649 /* the required size of the new extension data */
1650 extlen = offset + size + 1;
1651 /* calculate amount of words */
1652 wordlen_new = extlen / 4 + ((extlen % 4) ? 1 : 0);
1653 wordlen_new = MAX (wordlen_new, wordlen);
1655 gst_rtp_buffer_set_extension_data (rtp, 0xBEDE, wordlen_new);
1656 gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1660 pdata[0] = (id << 4) | (0x0F & (size - 1));
1661 memcpy (pdata + 1, data, size);
1664 memset (pdata + 1 + size, 0, 4 - (extlen % 4));
1671 get_twobytes_header_end_offset (const guint8 * pdata, guint wordlen)
1674 guint bytelen = wordlen * 4;
1675 guint paddingcount = 0;
1677 while (offset + 2 < bytelen) {
1678 guint8 read_id, read_len;
1680 read_id = GST_READ_UINT8 (pdata + offset);
1683 /* ID 0 means its padding, skip */
1691 read_len = GST_READ_UINT8 (pdata + offset);
1694 /* Ignore extension headers where the size does not fit */
1695 if (offset + read_len > bytelen)
1701 return offset - paddingcount;
1705 * gst_rtp_buffer_add_extension_twobytes_header:
1706 * @rtp: the RTP packet
1707 * @appbits: Application specific bits
1708 * @id: The ID of the header extension
1709 * @data: (array length=size) (element-type guint8): location for data
1710 * @size: the size of the data in bytes
1712 * Adds a RFC 5285 header extension with a two bytes header to the end of the
1713 * RTP header. If there is already a RFC 5285 header extension with a two bytes
1714 * header, the new extension will be appended.
1715 * It will not work if there is already a header extension that does not follow
1716 * the mechanism described in RFC 5285 or if there is a header extension with
1717 * a one byte header as described in RFC 5285. In that case, use
1718 * gst_rtp_buffer_add_extension_onebyte_header()
1720 * Returns: %TRUE if header extension could be added
1724 gst_rtp_buffer_add_extension_twobytes_header (GstRTPBuffer * rtp,
1725 guint8 appbits, guint8 id, gconstpointer data, guint size)
1734 g_return_val_if_fail ((appbits & 0xF0) == 0, FALSE);
1735 g_return_val_if_fail (size < 256, FALSE);
1736 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1738 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1739 (gpointer) & pdata, &wordlen);
1742 if (bits != ((0x100 << 4) | (appbits & 0x0f)))
1745 offset = get_twobytes_header_end_offset (pdata, wordlen);
1750 /* the required size of the new extension data */
1751 extlen = offset + size + 2;
1752 /* calculate amount of words */
1753 wordlen = extlen / 4 + ((extlen % 4) ? 1 : 0);
1755 gst_rtp_buffer_set_extension_data (rtp, (0x100 << 4) | (appbits & 0x0F),
1757 gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1763 memcpy (pdata + 2, data, size);
1765 memset (pdata + 2 + size, 0, 4 - (extlen % 4));