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: (out): location for result bits
688 * @data: (out) (array) (element-type guint8) (transfer none): location for data
689 * @wordlen: (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 * Returns: True if done.
839 gst_rtp_buffer_set_extension_data (GstRTPBuffer * rtp, guint16 bits,
842 guint32 min_size = 0;
844 GstMemory *mem = NULL;
846 ensure_buffers (rtp);
848 /* this is the size of the extension data we need */
849 min_size = 4 + length * sizeof (guint32);
851 /* we should allocate and map the extension data */
852 if (rtp->data[1] == NULL || min_size > rtp->size[1]) {
855 /* we don't have (enough) extension data, make some */
856 mem = gst_allocator_alloc (NULL, min_size, NULL);
859 /* copy old data & initialize the remainder of the new buffer */
860 gst_memory_map (mem, &map, GST_MAP_WRITE);
861 memcpy (map.data, rtp->data[1], rtp->size[1]);
862 if (min_size > rtp->size[1]) {
863 memset (map.data + rtp->size[1], 0, min_size - rtp->size[1]);
865 gst_memory_unmap (mem, &map);
868 gst_buffer_unmap (rtp->buffer, &rtp->map[1]);
869 gst_buffer_replace_memory (rtp->buffer, 1, mem);
871 /* don't leak data from uninitialized memory via the padding */
872 gst_memory_map (mem, &map, GST_MAP_WRITE);
873 memset (map.data, 0, map.size);
874 gst_memory_unmap (mem, &map);
876 /* we didn't have extension data, add */
877 gst_buffer_insert_memory (rtp->buffer, 1, mem);
881 gst_memory_map (mem, &rtp->map[1], GST_MAP_READWRITE);
882 gst_memory_ref (mem);
883 rtp->data[1] = rtp->map[1].data;
884 rtp->size[1] = rtp->map[1].size;
887 /* now we can set the extension bit */
889 GST_RTP_HEADER_EXTENSION (data) = TRUE;
892 GST_WRITE_UINT16_BE (data, bits);
893 GST_WRITE_UINT16_BE (data + 2, length);
899 * gst_rtp_buffer_get_ssrc:
900 * @rtp: the RTP packet
902 * Get the SSRC of the RTP packet in @buffer.
904 * Returns: the SSRC of @buffer in host order.
907 gst_rtp_buffer_get_ssrc (GstRTPBuffer * rtp)
909 return g_ntohl (GST_RTP_HEADER_SSRC (rtp->data[0]));
913 * gst_rtp_buffer_set_ssrc:
914 * @rtp: the RTP packet
915 * @ssrc: the new SSRC
917 * Set the SSRC on the RTP packet in @buffer to @ssrc.
920 gst_rtp_buffer_set_ssrc (GstRTPBuffer * rtp, guint32 ssrc)
922 GST_RTP_HEADER_SSRC (rtp->data[0]) = g_htonl (ssrc);
926 * gst_rtp_buffer_get_csrc_count:
927 * @rtp: the RTP packet
929 * Get the CSRC count of the RTP packet in @buffer.
931 * Returns: the CSRC count of @buffer.
934 gst_rtp_buffer_get_csrc_count (GstRTPBuffer * rtp)
936 return GST_RTP_HEADER_CSRC_COUNT (rtp->data[0]);
940 * gst_rtp_buffer_get_csrc:
941 * @rtp: the RTP packet
942 * @idx: the index of the CSRC to get
944 * Get the CSRC at index @idx in @buffer.
946 * Returns: the CSRC at index @idx in host order.
949 gst_rtp_buffer_get_csrc (GstRTPBuffer * rtp, guint8 idx)
955 g_return_val_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data), 0);
957 return GST_READ_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx));
961 * gst_rtp_buffer_set_csrc:
962 * @rtp: the RTP packet
963 * @idx: the CSRC index to set
964 * @csrc: the CSRC in host order to set at @idx
966 * Modify the CSRC at index @idx in @buffer to @csrc.
969 gst_rtp_buffer_set_csrc (GstRTPBuffer * rtp, guint8 idx, guint32 csrc)
975 g_return_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data));
977 GST_WRITE_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx), csrc);
981 * gst_rtp_buffer_get_marker:
982 * @rtp: the RTP packet
984 * Check if the marker bit is set on the RTP packet in @buffer.
986 * Returns: TRUE if @buffer has the marker bit set.
989 gst_rtp_buffer_get_marker (GstRTPBuffer * rtp)
991 return GST_RTP_HEADER_MARKER (rtp->data[0]);
995 * gst_rtp_buffer_set_marker:
996 * @rtp: the RTP packet
997 * @marker: the new marker
999 * Set the marker bit on the RTP packet in @buffer to @marker.
1002 gst_rtp_buffer_set_marker (GstRTPBuffer * rtp, gboolean marker)
1004 GST_RTP_HEADER_MARKER (rtp->data[0]) = marker;
1008 * gst_rtp_buffer_get_payload_type:
1009 * @rtp: the RTP packet
1011 * Get the payload type of the RTP packet in @buffer.
1013 * Returns: The payload type.
1016 gst_rtp_buffer_get_payload_type (GstRTPBuffer * rtp)
1018 return GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]);
1022 * gst_rtp_buffer_set_payload_type:
1023 * @rtp: the RTP packet
1024 * @payload_type: the new type
1026 * Set the payload type of the RTP packet in @buffer to @payload_type.
1029 gst_rtp_buffer_set_payload_type (GstRTPBuffer * rtp, guint8 payload_type)
1031 g_return_if_fail (payload_type < 0x80);
1033 GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]) = payload_type;
1037 * gst_rtp_buffer_get_seq:
1038 * @rtp: the RTP packet
1040 * Get the sequence number of the RTP packet in @buffer.
1042 * Returns: The sequence number in host order.
1045 gst_rtp_buffer_get_seq (GstRTPBuffer * rtp)
1047 return g_ntohs (GST_RTP_HEADER_SEQ (rtp->data[0]));
1051 * gst_rtp_buffer_set_seq:
1052 * @rtp: the RTP packet
1053 * @seq: the new sequence number
1055 * Set the sequence number of the RTP packet in @buffer to @seq.
1058 gst_rtp_buffer_set_seq (GstRTPBuffer * rtp, guint16 seq)
1060 GST_RTP_HEADER_SEQ (rtp->data[0]) = g_htons (seq);
1064 * gst_rtp_buffer_get_timestamp:
1065 * @rtp: the RTP packet
1067 * Get the timestamp of the RTP packet in @buffer.
1069 * Returns: The timestamp in host order.
1072 gst_rtp_buffer_get_timestamp (GstRTPBuffer * rtp)
1074 return g_ntohl (GST_RTP_HEADER_TIMESTAMP (rtp->data[0]));
1078 * gst_rtp_buffer_set_timestamp:
1079 * @rtp: the RTP packet
1080 * @timestamp: the new timestamp
1082 * Set the timestamp of the RTP packet in @buffer to @timestamp.
1085 gst_rtp_buffer_set_timestamp (GstRTPBuffer * rtp, guint32 timestamp)
1087 GST_RTP_HEADER_TIMESTAMP (rtp->data[0]) = g_htonl (timestamp);
1092 * gst_rtp_buffer_get_payload_subbuffer:
1093 * @rtp: the RTP packet
1094 * @offset: the offset in the payload
1095 * @len: the length in the payload
1097 * Create a subbuffer of the payload of the RTP packet in @buffer. @offset bytes
1098 * are skipped in the payload and the subbuffer will be of size @len.
1099 * If @len is -1 the total payload starting from @offset is subbuffered.
1101 * Returns: A new buffer with the specified data of the payload.
1104 gst_rtp_buffer_get_payload_subbuffer (GstRTPBuffer * rtp, guint offset,
1107 guint poffset, plen;
1109 plen = gst_rtp_buffer_get_payload_len (rtp);
1110 /* we can't go past the length */
1111 if (G_UNLIKELY (offset > plen))
1115 poffset = gst_rtp_buffer_get_header_len (rtp) + offset;
1118 /* see if we need to shrink the buffer based on @len */
1119 if (len != -1 && len < plen)
1122 return gst_buffer_copy_region (rtp->buffer, GST_BUFFER_COPY_ALL, poffset,
1128 g_warning ("offset=%u should be less than plen=%u", offset, plen);
1134 * gst_rtp_buffer_get_payload_buffer:
1135 * @rtp: the RTP packet
1137 * Create a buffer of the payload of the RTP packet in @buffer. This function
1138 * will internally create a subbuffer of @buffer so that a memcpy can be
1141 * Returns: A new buffer with the data of the payload.
1144 gst_rtp_buffer_get_payload_buffer (GstRTPBuffer * rtp)
1146 return gst_rtp_buffer_get_payload_subbuffer (rtp, 0, -1);
1150 * gst_rtp_buffer_get_payload_len:
1151 * @rtp: the RTP packet
1153 * Get the length of the payload of the RTP packet in @buffer.
1155 * Returns: The length of the payload in @buffer.
1158 gst_rtp_buffer_get_payload_len (GstRTPBuffer * rtp)
1160 return gst_buffer_get_size (rtp->buffer) - gst_rtp_buffer_get_header_len (rtp)
1165 * gst_rtp_buffer_get_payload: (skip)
1166 * @rtp: the RTP packet
1168 * Get a pointer to the payload data in @buffer. This pointer is valid as long
1169 * as a reference to @buffer is held.
1171 * Returns: (array) (element-type guint8) (transfer none): A pointer
1172 * to the payload data in @buffer.
1175 gst_rtp_buffer_get_payload (GstRTPBuffer * rtp)
1178 return rtp->data[2];
1180 if (!gst_rtp_buffer_map_payload (rtp))
1183 return rtp->data[2];
1187 * gst_rtp_buffer_get_payload_bytes: (rename-to gst_rtp_buffer_get_payload)
1188 * @rtp: the RTP packet
1190 * Similar to gst_rtp_buffer_get_payload, but more suitable for language
1191 * bindings usage. The return value is a pointer to a #GBytes structure
1192 * containing the payload data in @rtp.
1194 * Returns: (transfer full): A new #GBytes containing the payload data in @rtp.
1199 gst_rtp_buffer_get_payload_bytes (GstRTPBuffer * rtp)
1203 g_return_val_if_fail (rtp != NULL, NULL);
1205 data = gst_rtp_buffer_get_payload (rtp);
1209 return g_bytes_new (data, gst_rtp_buffer_get_payload_len (rtp));
1213 * gst_rtp_buffer_default_clock_rate:
1214 * @payload_type: the static payload type
1216 * Get the default clock-rate for the static payload type @payload_type.
1218 * Returns: the default clock rate or -1 if the payload type is not static or
1219 * the clock-rate is undefined.
1222 gst_rtp_buffer_default_clock_rate (guint8 payload_type)
1224 const GstRTPPayloadInfo *info;
1227 info = gst_rtp_payload_info_for_pt (payload_type);
1231 res = info->clock_rate;
1232 /* 0 means unknown so we have to return -1 from this function */
1240 * gst_rtp_buffer_compare_seqnum:
1241 * @seqnum1: a sequence number
1242 * @seqnum2: a sequence number
1244 * Compare two sequence numbers, taking care of wraparounds. This function
1245 * returns the difference between @seqnum1 and @seqnum2.
1247 * Returns: a negative value if @seqnum1 is bigger than @seqnum2, 0 if they
1248 * are equal or a positive value if @seqnum1 is smaller than @segnum2.
1251 gst_rtp_buffer_compare_seqnum (guint16 seqnum1, guint16 seqnum2)
1253 /* See http://en.wikipedia.org/wiki/Serial_number_arithmetic
1254 * for an explanation why this does the right thing even for
1255 * wraparounds, under the assumption that the difference is
1256 * never bigger than 2**15 sequence numbers
1258 return (gint16) (seqnum2 - seqnum1);
1262 * gst_rtp_buffer_ext_timestamp:
1263 * @exttimestamp: (inout): a previous extended timestamp
1264 * @timestamp: a new timestamp
1266 * Update the @exttimestamp field with the extended timestamp of @timestamp
1267 * For the first call of the method, @exttimestamp should point to a location
1268 * with a value of -1.
1270 * This function is able to handle both forward and backward timestamps taking
1272 * - timestamp wraparound making sure that the returned value is properly increased.
1273 * - timestamp unwraparound making sure that the returned value is properly decreased.
1275 * Returns: The extended timestamp of @timestamp or 0 if the result can't go anywhere backwards.
1278 gst_rtp_buffer_ext_timestamp (guint64 * exttimestamp, guint32 timestamp)
1280 guint64 result, ext;
1282 g_return_val_if_fail (exttimestamp != NULL, -1);
1284 ext = *exttimestamp;
1289 /* pick wraparound counter from previous timestamp and add to new timestamp */
1290 result = timestamp + (ext & ~(G_GUINT64_CONSTANT (0xffffffff)));
1292 /* check for timestamp wraparound */
1294 guint64 diff = ext - result;
1296 if (diff > G_MAXINT32) {
1297 /* timestamp went backwards more than allowed, we wrap around and get
1298 * updated extended timestamp. */
1299 result += (G_GUINT64_CONSTANT (1) << 32);
1302 guint64 diff = result - ext;
1304 if (diff > G_MAXINT32) {
1305 if (result < (G_GUINT64_CONSTANT (1) << 32)) {
1307 ("Cannot unwrap, any wrapping took place yet. Returning 0 without updating extended timestamp.");
1310 /* timestamp went forwards more than allowed, we unwrap around and get
1311 * updated extended timestamp. */
1312 result -= (G_GUINT64_CONSTANT (1) << 32);
1313 /* We don't want the extended timestamp storage to go back, ever */
1320 *exttimestamp = result;
1327 _get_extension_onebyte_header (const guint8 * pdata, guint len,
1328 guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1333 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1335 if (bit_pattern != 0xBEDE)
1339 guint8 read_id, read_len;
1341 if (offset + 1 >= len)
1344 read_id = GST_READ_UINT8 (pdata + offset) >> 4;
1345 read_len = (GST_READ_UINT8 (pdata + offset) & 0x0F) + 1;
1348 /* ID 0 means its padding, skip */
1352 /* ID 15 is special and means we should stop parsing */
1356 /* Ignore extension headers where the size does not fit */
1357 if (offset + read_len > len)
1360 /* If we have the right one */
1361 if (id == read_id) {
1364 *data = (gpointer) & pdata[offset];
1384 * gst_rtp_buffer_get_extension_onebyte_header_from_bytes:
1386 * @bit_pattern: The bit-pattern. Anything but 0xBEDE is rejected.
1387 * @id: The ID of the header extension to be read (between 1 and 14).
1388 * @nth: Read the nth extension packet with the requested ID
1389 * @data: (out) (array length=size) (element-type guint8) (transfer none):
1391 * @size: (out): the size of the data in bytes
1393 * Similar to gst_rtp_buffer_get_extension_onebyte_header, but working
1394 * on the #GBytes you get from gst_rtp_buffer_get_extension_bytes.
1395 * Parses RFC 5285 style header extensions with a one byte header. It will
1396 * return the nth extension with the requested id.
1398 * Returns: TRUE if @bytes had the requested header extension
1403 gst_rtp_buffer_get_extension_onebyte_header_from_bytes (GBytes * bytes,
1404 guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1406 const guint8 *pdata = g_bytes_get_data (bytes, NULL);
1407 gsize len = g_bytes_get_size (bytes);
1408 return _get_extension_onebyte_header (pdata, len, bit_pattern, id, nth, data,
1414 * gst_rtp_buffer_get_extension_onebyte_header:
1415 * @rtp: the RTP packet
1416 * @id: The ID of the header extension to be read (between 1 and 14).
1417 * @nth: Read the nth extension packet with the requested ID
1418 * @data: (out) (array length=size) (element-type guint8) (transfer none):
1420 * @size: (out): the size of the data in bytes
1422 * Parses RFC 5285 style header extensions with a one byte header. It will
1423 * return the nth extension with the requested id.
1425 * Returns: TRUE if @buffer had the requested header extension
1429 gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1430 guint nth, gpointer * data, guint * size)
1432 guint16 bit_pattern;
1436 if (!gst_rtp_buffer_get_extension_data (rtp, &bit_pattern, (gpointer) & pdata,
1440 return _get_extension_onebyte_header (pdata, wordlen * 4, bit_pattern, id,
1446 * gst_rtp_buffer_get_extension_twobytes_header:
1447 * @rtp: the RTP packet
1448 * @appbits: (out): Application specific bits
1449 * @id: The ID of the header extension to be read (between 1 and 14).
1450 * @nth: Read the nth extension packet with the requested ID
1451 * @data: (out) (array length=size) (element-type guint8) (transfer none):
1453 * @size: (out): the size of the data in bytes
1455 * Parses RFC 5285 style header extensions with a two bytes header. It will
1456 * return the nth extension with the requested id.
1458 * Returns: TRUE if @buffer had the requested header extension
1462 gst_rtp_buffer_get_extension_twobytes_header (GstRTPBuffer * rtp,
1463 guint8 * appbits, guint8 id, guint nth, gpointer * data, guint * size)
1466 guint8 *pdata = NULL;
1472 if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer *) & pdata,
1476 if (bits >> 4 != 0x100)
1479 bytelen = wordlen * 4;
1482 guint8 read_id, read_len;
1484 if (offset + 2 >= bytelen)
1487 read_id = GST_READ_UINT8 (pdata + offset);
1493 read_len = GST_READ_UINT8 (pdata + offset);
1496 /* Ignore extension headers where the size does not fit */
1497 if (offset + read_len > bytelen)
1500 /* If we have the right one, return it */
1501 if (id == read_id) {
1504 *data = pdata + offset;
1522 get_onebyte_header_end_offset (guint8 * pdata, guint wordlen, guint * offset)
1524 guint bytelen = wordlen * 4;
1525 guint paddingcount = 0;
1529 while (*offset + 1 < bytelen) {
1530 guint8 read_id, read_len;
1532 read_id = GST_READ_UINT8 (pdata + *offset) >> 4;
1533 read_len = (GST_READ_UINT8 (pdata + *offset) & 0x0F) + 1;
1536 /* ID 0 means its padding, skip */
1544 /* ID 15 is special and means we should stop parsing */
1545 /* It also means we can't add an extra packet */
1546 if (read_id == 15) {
1550 /* Ignore extension headers where the size does not fit */
1551 if (*offset + read_len > bytelen) {
1555 *offset += read_len;
1558 *offset -= paddingcount;
1564 * gst_rtp_buffer_add_extension_onebyte_header:
1565 * @rtp: the RTP packet
1566 * @id: The ID of the header extension (between 1 and 14).
1567 * @data: (array length=size) (element-type guint8): location for data
1568 * @size: the size of the data in bytes
1570 * Adds a RFC 5285 header extension with a one byte header to the end of the
1571 * RTP header. If there is already a RFC 5285 header extension with a one byte
1572 * header, the new extension will be appended.
1573 * It will not work if there is already a header extension that does not follow
1574 * the mechanism described in RFC 5285 or if there is a header extension with
1575 * a two bytes header as described in RFC 5285. In that case, use
1576 * gst_rtp_buffer_add_extension_twobytes_header()
1578 * Returns: %TRUE if header extension could be added
1582 gst_rtp_buffer_add_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1583 gconstpointer data, guint size)
1590 guint extlen, offset = 0;
1592 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1593 g_return_val_if_fail (size >= 1 && size <= 16, FALSE);
1594 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1596 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1597 (gpointer) & pdata, &wordlen);
1603 if (!get_onebyte_header_end_offset (pdata, wordlen, &offset))
1607 /* the required size of the new extension data */
1608 extlen = offset + size + 1;
1609 /* calculate amount of words */
1610 wordlen_new = extlen / 4 + ((extlen % 4) ? 1 : 0);
1611 wordlen_new = MAX (wordlen_new, wordlen);
1613 gst_rtp_buffer_set_extension_data (rtp, 0xBEDE, wordlen_new);
1614 gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1618 pdata[0] = (id << 4) | (0x0F & (size - 1));
1619 memcpy (pdata + 1, data, size);
1622 memset (pdata + 1 + size, 0, 4 - (extlen % 4));
1629 get_twobytes_header_end_offset (const guint8 * pdata, guint wordlen)
1632 guint bytelen = wordlen * 4;
1633 guint paddingcount = 0;
1635 while (offset + 2 < bytelen) {
1636 guint8 read_id, read_len;
1638 read_id = GST_READ_UINT8 (pdata + offset);
1641 /* ID 0 means its padding, skip */
1649 read_len = GST_READ_UINT8 (pdata + offset);
1652 /* Ignore extension headers where the size does not fit */
1653 if (offset + read_len > bytelen)
1659 return offset - paddingcount;
1663 * gst_rtp_buffer_add_extension_twobytes_header:
1664 * @rtp: the RTP packet
1665 * @appbits: Application specific bits
1666 * @id: The ID of the header extension
1667 * @data: (array length=size) (element-type guint8): location for data
1668 * @size: the size of the data in bytes
1670 * Adds a RFC 5285 header extension with a two bytes header to the end of the
1671 * RTP header. If there is already a RFC 5285 header extension with a two bytes
1672 * header, the new extension will be appended.
1673 * It will not work if there is already a header extension that does not follow
1674 * the mechanism described in RFC 5285 or if there is a header extension with
1675 * a one byte header as described in RFC 5285. In that case, use
1676 * gst_rtp_buffer_add_extension_onebyte_header()
1678 * Returns: %TRUE if header extension could be added
1682 gst_rtp_buffer_add_extension_twobytes_header (GstRTPBuffer * rtp,
1683 guint8 appbits, guint8 id, gconstpointer data, guint size)
1692 g_return_val_if_fail ((appbits & 0xF0) == 0, FALSE);
1693 g_return_val_if_fail (size < 256, FALSE);
1694 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1696 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1697 (gpointer) & pdata, &wordlen);
1700 if (bits != ((0x100 << 4) | (appbits & 0x0f)))
1703 offset = get_twobytes_header_end_offset (pdata, wordlen);
1708 /* the required size of the new extension data */
1709 extlen = offset + size + 2;
1710 /* calculate amount of words */
1711 wordlen = extlen / 4 + ((extlen % 4) ? 1 : 0);
1713 gst_rtp_buffer_set_extension_data (rtp, (0x100 << 4) | (appbits & 0x0F),
1715 gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1721 memcpy (pdata + 2, data, size);
1723 memset (pdata + 2 + size, 0, 4 - (extlen % 4));