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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:gstrtpbuffer
23 * @short_description: Helper methods for dealing with RTP buffers
24 * @see_also: #GstRTPBasePayload, #GstRTPBaseDepayload, gstrtcpbuffer
28 * The GstRTPBuffer helper functions makes it easy to parse and create regular
29 * #GstBuffer objects that contain RTP payloads. These buffers are typically of
30 * 'application/x-rtp' #GstCaps.
34 * Last reviewed on 2006-07-17 (0.10.10)
37 #include "gstrtpbuffer.h"
42 #define GST_RTP_HEADER_LEN 12
44 /* Note: we use bitfields here to make sure the compiler doesn't add padding
45 * between fields on certain architectures; can't assume aligned access either
47 typedef struct _GstRTPHeader
49 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
50 unsigned int csrc_count:4; /* CSRC count */
51 unsigned int extension:1; /* header extension flag */
52 unsigned int padding:1; /* padding flag */
53 unsigned int version:2; /* protocol version */
54 unsigned int payload_type:7; /* payload type */
55 unsigned int marker:1; /* marker bit */
56 #elif G_BYTE_ORDER == G_BIG_ENDIAN
57 unsigned int version:2; /* protocol version */
58 unsigned int padding:1; /* padding flag */
59 unsigned int extension:1; /* header extension flag */
60 unsigned int csrc_count:4; /* CSRC count */
61 unsigned int marker:1; /* marker bit */
62 unsigned int payload_type:7; /* payload type */
64 #error "G_BYTE_ORDER should be big or little endian."
66 unsigned int seq:16; /* sequence number */
67 unsigned int timestamp:32; /* timestamp */
68 unsigned int ssrc:32; /* synchronization source */
69 guint8 csrclist[4]; /* optional CSRC list, 32 bits each */
72 #define GST_RTP_HEADER_VERSION(data) (((GstRTPHeader *)(data))->version)
73 #define GST_RTP_HEADER_PADDING(data) (((GstRTPHeader *)(data))->padding)
74 #define GST_RTP_HEADER_EXTENSION(data) (((GstRTPHeader *)(data))->extension)
75 #define GST_RTP_HEADER_CSRC_COUNT(data) (((GstRTPHeader *)(data))->csrc_count)
76 #define GST_RTP_HEADER_MARKER(data) (((GstRTPHeader *)(data))->marker)
77 #define GST_RTP_HEADER_PAYLOAD_TYPE(data) (((GstRTPHeader *)(data))->payload_type)
78 #define GST_RTP_HEADER_SEQ(data) (((GstRTPHeader *)(data))->seq)
79 #define GST_RTP_HEADER_TIMESTAMP(data) (((GstRTPHeader *)(data))->timestamp)
80 #define GST_RTP_HEADER_SSRC(data) (((GstRTPHeader *)(data))->ssrc)
81 #define GST_RTP_HEADER_CSRC_LIST_OFFSET(data,i) \
82 data + G_STRUCT_OFFSET(GstRTPHeader, csrclist) + \
83 ((i) * sizeof(guint32))
84 #define GST_RTP_HEADER_CSRC_SIZE(data) (GST_RTP_HEADER_CSRC_COUNT(data) * sizeof (guint32))
87 * gst_rtp_buffer_allocate_data:
88 * @buffer: a #GstBuffer
89 * @payload_len: the length of the payload
90 * @pad_len: the amount of padding
91 * @csrc_count: the number of CSRC entries
93 * Allocate enough data in @buffer to hold an RTP packet with @csrc_count CSRCs,
94 * a payload length of @payload_len and padding of @pad_len.
95 * MALLOCDATA of @buffer will be overwritten and will not be freed.
96 * All other RTP header fields 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));
109 len = GST_RTP_HEADER_LEN + csrc_count * sizeof (guint32)
110 + payload_len + pad_len;
112 mem = gst_allocator_alloc (NULL, len, 0);
114 gst_memory_map (mem, &map, GST_MAP_WRITE);
115 /* fill in defaults */
116 GST_RTP_HEADER_VERSION (map.data) = GST_RTP_VERSION;
117 GST_RTP_HEADER_PADDING (map.data) = FALSE;
118 GST_RTP_HEADER_EXTENSION (map.data) = FALSE;
119 GST_RTP_HEADER_CSRC_COUNT (map.data) = csrc_count;
120 memset (GST_RTP_HEADER_CSRC_LIST_OFFSET (map.data, 0), 0,
121 csrc_count * sizeof (guint32));
122 GST_RTP_HEADER_MARKER (map.data) = FALSE;
123 GST_RTP_HEADER_PAYLOAD_TYPE (map.data) = 0;
124 GST_RTP_HEADER_SEQ (map.data) = 0;
125 GST_RTP_HEADER_TIMESTAMP (map.data) = 0;
126 GST_RTP_HEADER_SSRC (map.data) = 0;
127 gst_memory_unmap (mem, &map);
129 gst_buffer_take_memory (buffer, -1, mem);
133 * gst_rtp_buffer_new_take_data:
134 * @data: data for the new buffer
135 * @len: the length of data
137 * Create a new buffer and set the data and size of the buffer to @data and @len
138 * respectively. @data will be freed when the buffer is unreffed, so this
139 * function transfers ownership of @data to the new buffer.
141 * Returns: A newly allocated buffer with @data and of size @len.
144 gst_rtp_buffer_new_take_data (gpointer data, gsize len)
148 g_return_val_if_fail (data != NULL, NULL);
149 g_return_val_if_fail (len > 0, NULL);
151 result = gst_buffer_new ();
152 gst_buffer_take_memory (result, -1,
153 gst_memory_new_wrapped (0, data, len, 0, len, data, g_free));
159 * gst_rtp_buffer_new_copy_data:
160 * @data: data for the new buffer
161 * @len: the length of data
163 * Create a new buffer and set the data to a copy of @len
164 * bytes of @data and the size to @len. The data will be freed when the buffer
167 * Returns: A newly allocated buffer with a copy of @data and of size @len.
170 gst_rtp_buffer_new_copy_data (gpointer data, gsize len)
172 return gst_rtp_buffer_new_take_data (g_memdup (data, len), len);
176 * gst_rtp_buffer_new_allocate:
177 * @payload_len: the length of the payload
178 * @pad_len: the amount of padding
179 * @csrc_count: the number of CSRC entries
181 * Allocate a new #GstBuffer with enough data to hold an RTP packet with
182 * @csrc_count CSRCs, a payload length of @payload_len and padding of @pad_len.
183 * All other RTP header fields will be set to 0/FALSE.
185 * Returns: A newly allocated buffer that can hold an RTP packet with given
189 gst_rtp_buffer_new_allocate (guint payload_len, guint8 pad_len,
194 g_return_val_if_fail (csrc_count <= 15, NULL);
196 result = gst_buffer_new ();
197 gst_rtp_buffer_allocate_data (result, payload_len, pad_len, csrc_count);
203 * gst_rtp_buffer_new_allocate_len:
204 * @packet_len: the total length of the packet
205 * @pad_len: the amount of padding
206 * @csrc_count: the number of CSRC entries
208 * Create a new #GstBuffer that can hold an RTP packet that is exactly
209 * @packet_len long. The length of the payload depends on @pad_len and
210 * @csrc_count and can be calculated with gst_rtp_buffer_calc_payload_len().
211 * All RTP header fields will be set to 0/FALSE.
213 * Returns: A newly allocated buffer that can hold an RTP packet of @packet_len.
216 gst_rtp_buffer_new_allocate_len (guint packet_len, guint8 pad_len,
221 g_return_val_if_fail (csrc_count <= 15, NULL);
223 len = gst_rtp_buffer_calc_payload_len (packet_len, pad_len, csrc_count);
225 return gst_rtp_buffer_new_allocate (len, pad_len, csrc_count);
229 * gst_rtp_buffer_calc_header_len:
230 * @csrc_count: the number of CSRC entries
232 * Calculate the header length of an RTP packet with @csrc_count CSRC entries.
233 * An RTP packet can have at most 15 CSRC entries.
235 * Returns: The length of an RTP header with @csrc_count CSRC entries.
238 gst_rtp_buffer_calc_header_len (guint8 csrc_count)
240 g_return_val_if_fail (csrc_count <= 15, 0);
242 return GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32));
246 * gst_rtp_buffer_calc_packet_len:
247 * @payload_len: the length of the payload
248 * @pad_len: the amount of padding
249 * @csrc_count: the number of CSRC entries
251 * Calculate the total length of an RTP packet with a payload size of @payload_len,
252 * a padding of @pad_len and a @csrc_count CSRC entries.
254 * Returns: The total length of an RTP header with given parameters.
257 gst_rtp_buffer_calc_packet_len (guint payload_len, guint8 pad_len,
260 g_return_val_if_fail (csrc_count <= 15, 0);
262 return payload_len + GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32))
267 * gst_rtp_buffer_calc_payload_len:
268 * @packet_len: the length of the total RTP packet
269 * @pad_len: the amount of padding
270 * @csrc_count: the number of CSRC entries
272 * Calculate the length of the payload of an RTP packet with size @packet_len,
273 * a padding of @pad_len and a @csrc_count CSRC entries.
275 * Returns: The length of the payload of an RTP packet with given parameters.
278 gst_rtp_buffer_calc_payload_len (guint packet_len, guint8 pad_len,
281 g_return_val_if_fail (csrc_count <= 15, 0);
283 return packet_len - GST_RTP_HEADER_LEN - (csrc_count * sizeof (guint32))
289 * @data: the data to validate
290 * @len: the length of @data to validate
291 * @payload: the payload if @data represents the header only
292 * @payload_len: the len of the payload
294 * Checks if @data is a valid RTP packet.
296 * Returns: TRUE if @data is a valid RTP packet
299 validate_data (guint8 * data, guint len, guint8 * payload, guint payload_len)
306 g_return_val_if_fail (data != NULL, FALSE);
308 header_len = GST_RTP_HEADER_LEN;
309 if (G_UNLIKELY (len < header_len))
313 version = (data[0] & 0xc0);
314 if (G_UNLIKELY (version != (GST_RTP_VERSION << 6)))
317 /* calc header length with csrc */
318 csrc_count = (data[0] & 0x0f);
319 header_len += csrc_count * sizeof (guint32);
321 /* calc extension length when present. */
322 if (data[0] & 0x10) {
326 /* this points to the extension bits and header length */
327 extpos = &data[header_len];
329 /* skip the header and check that we have enough space */
331 if (G_UNLIKELY (len < header_len))
336 /* read length as the number of 32 bits words */
337 extlen = GST_READ_UINT16_BE (extpos);
339 header_len += extlen * sizeof (guint32);
342 /* check for padding */
343 if (data[0] & 0x20) {
345 padding = payload[payload_len - 1];
347 padding = data[len - 1];
352 /* check if padding and header not bigger than packet length */
353 if (G_UNLIKELY (len < padding + header_len))
361 GST_DEBUG ("len < header_len check failed (%d < %d)", len, header_len);
366 GST_DEBUG ("version check failed (%d != %d)", version, GST_RTP_VERSION);
371 GST_DEBUG ("padding check failed (%d - %d < %d)", len, header_len, padding);
376 GST_MEMDUMP ("buffer", data, len);
382 * gst_rtp_buffer_validate_data:
383 * @data: the data to validate
384 * @len: the length of @data to validate
386 * Check if the @data and @size point to the data of a valid RTP packet.
387 * This function checks the length, version and padding of the packet data.
388 * Use this function to validate a packet before using the other functions in
391 * Returns: TRUE if the data points to a valid RTP packet.
394 gst_rtp_buffer_validate_data (guint8 * data, gsize len)
396 return validate_data (data, len, NULL, 0);
400 * gst_rtp_buffer_validate:
401 * @buffer: the buffer to validate
403 * Check if the data pointed to by @buffer is a valid RTP packet using
404 * gst_rtp_buffer_validate_data().
405 * Use this function to validate a packet before using the other functions in
408 * Returns: TRUE if @buffer is a valid RTP packet.
411 gst_rtp_buffer_validate (GstBuffer * buffer)
416 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
418 gst_buffer_map (buffer, &map, GST_MAP_READ);
419 res = validate_data (map.data, map.size, NULL, 0);
420 gst_buffer_unmap (buffer, &map);
426 gst_rtp_buffer_map (GstBuffer * buffer, GstMapFlags flags, GstRTPBuffer * rtp)
428 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
429 g_return_val_if_fail (rtp != NULL, FALSE);
430 g_return_val_if_fail (rtp->buffer == NULL, FALSE);
432 if (!gst_buffer_map (buffer, &rtp->map, flags))
435 rtp->buffer = buffer;
441 gst_rtp_buffer_unmap (GstRTPBuffer * rtp)
443 g_return_val_if_fail (rtp != NULL, FALSE);
444 g_return_val_if_fail (rtp->buffer != NULL, FALSE);
446 gst_buffer_unmap (rtp->buffer, &rtp->map);
454 * gst_rtp_buffer_set_packet_len:
455 * @rtp: the RTP packet
456 * @len: the new packet length
458 * Set the total @rtp size to @len. The data in the buffer will be made
459 * larger if needed. Any padding will be removed from the packet.
462 gst_rtp_buffer_set_packet_len (GstRTPBuffer * rtp, guint len)
466 data = rtp->map.data;
468 if (rtp->map.maxsize <= len) {
469 /* FIXME, realloc bigger space */
470 g_warning ("not implemented");
473 gst_buffer_set_size (rtp->buffer, len);
476 /* remove any padding */
477 GST_RTP_HEADER_PADDING (data) = FALSE;
481 * gst_rtp_buffer_get_packet_len:
482 * @rtp: the RTP packet
484 * Return the total length of the packet in @buffer.
486 * Returns: The total length of the packet in @buffer.
489 gst_rtp_buffer_get_packet_len (GstRTPBuffer * rtp)
491 return gst_buffer_get_size (rtp->buffer);
495 * gst_rtp_buffer_get_header_len:
496 * @rtp: the RTP packet
498 * Return the total length of the header in @buffer. This include the length of
499 * the fixed header, the CSRC list and the extension header.
501 * Returns: The total length of the header in @buffer.
504 gst_rtp_buffer_get_header_len (GstRTPBuffer * rtp)
509 data = rtp->map.data;
511 len = GST_RTP_HEADER_LEN + GST_RTP_HEADER_CSRC_SIZE (data);
512 if (GST_RTP_HEADER_EXTENSION (data))
513 len += GST_READ_UINT16_BE (data + len + 2) * 4 + 4;
519 * gst_rtp_buffer_get_version:
520 * @rtp: the RTP packet
522 * Get the version number of the RTP packet in @buffer.
524 * Returns: The version of @buffer.
527 gst_rtp_buffer_get_version (GstRTPBuffer * rtp)
529 return GST_RTP_HEADER_VERSION (rtp->map.data);
533 * gst_rtp_buffer_set_version:
534 * @rtp: the RTP packet
535 * @version: the new version
537 * Set the version of the RTP packet in @buffer to @version.
540 gst_rtp_buffer_set_version (GstRTPBuffer * rtp, guint8 version)
542 g_return_if_fail (version < 0x04);
544 GST_RTP_HEADER_VERSION (rtp->map.data) = version;
548 * gst_rtp_buffer_get_padding:
549 * @rtp: the RTP packet
551 * Check if the padding bit is set on the RTP packet in @buffer.
553 * Returns: TRUE if @buffer has the padding bit set.
556 gst_rtp_buffer_get_padding (GstRTPBuffer * rtp)
558 return GST_RTP_HEADER_PADDING (rtp->map.data);
562 * gst_rtp_buffer_set_padding:
564 * @padding: the new padding
566 * Set the padding bit on the RTP packet in @buffer to @padding.
569 gst_rtp_buffer_set_padding (GstRTPBuffer * rtp, gboolean padding)
571 GST_RTP_HEADER_PADDING (rtp->map.data) = padding;
575 * gst_rtp_buffer_pad_to:
576 * @rtp: the RTP packet
577 * @len: the new amount of padding
579 * Set the amount of padding in the RTP packet in @buffer to
580 * @len. If @len is 0, the padding is removed.
582 * NOTE: This function does not work correctly.
585 gst_rtp_buffer_pad_to (GstRTPBuffer * rtp, guint len)
589 data = rtp->map.data;
592 GST_RTP_HEADER_PADDING (data) = TRUE;
594 GST_RTP_HEADER_PADDING (data) = FALSE;
596 /* FIXME, set the padding byte at the end of the payload data */
600 * gst_rtp_buffer_get_extension:
601 * @rtp: the RTP packet
603 * Check if the extension bit is set on the RTP packet in @buffer.
605 * Returns: TRUE if @buffer has the extension bit set.
608 gst_rtp_buffer_get_extension (GstRTPBuffer * rtp)
610 return GST_RTP_HEADER_EXTENSION (rtp->map.data);
614 * gst_rtp_buffer_set_extension:
615 * @rtp: the RTP packet
616 * @extension: the new extension
618 * Set the extension bit on the RTP packet in @buffer to @extension.
621 gst_rtp_buffer_set_extension (GstRTPBuffer * rtp, gboolean extension)
623 GST_RTP_HEADER_EXTENSION (rtp->map.data) = extension;
627 * gst_rtp_buffer_get_extension_data:
628 * @rtp: the RTP packet
629 * @bits: location for result bits
630 * @data: location for data
631 * @wordlen: location for length of @data in 32 bits words
633 * Get the extension data. @bits will contain the extension 16 bits of custom
634 * data. @data will point to the data in the extension and @wordlen will contain
635 * the length of @data in 32 bits words.
637 * If @buffer did not contain an extension, this function will return %FALSE
638 * with @bits, @data and @wordlen unchanged.
640 * Returns: TRUE if @buffer had the extension bit set.
645 gst_rtp_buffer_get_extension_data (GstRTPBuffer * rtp, guint16 * bits,
646 gpointer * data, guint * wordlen)
651 pdata = rtp->map.data;
653 if (!GST_RTP_HEADER_EXTENSION (pdata))
656 /* move to the extension */
657 len = GST_RTP_HEADER_LEN + GST_RTP_HEADER_CSRC_SIZE (pdata);
661 *bits = GST_READ_UINT16_BE (pdata);
663 *wordlen = GST_READ_UINT16_BE (pdata + 2);
666 *data = (gpointer *) pdata;
672 * gst_rtp_buffer_set_extension_data:
673 * @rtp: the RTP packet
674 * @bits: the bits specific for the extension
675 * @length: the length that counts the number of 32-bit words in
676 * the extension, excluding the extension header ( therefore zero is a valid length)
678 * Set the extension bit of the rtp buffer and fill in the @bits and @length of the
679 * extension header. It will refuse to set the extension data if the buffer is not
682 * Returns: True if done.
687 gst_rtp_buffer_set_extension_data (GstRTPBuffer * rtp, guint16 bits,
690 guint32 min_size = 0;
693 data = rtp->map.data;
695 /* check if the buffer is big enough to hold the extension */
697 GST_RTP_HEADER_LEN + GST_RTP_HEADER_CSRC_SIZE (data) + 4 +
698 length * sizeof (guint32);
699 if (G_UNLIKELY (min_size > rtp->map.size))
702 /* now we can set the extension bit */
703 GST_RTP_HEADER_EXTENSION (rtp->map.data) = TRUE;
705 data += GST_RTP_HEADER_LEN + GST_RTP_HEADER_CSRC_SIZE (data);
706 GST_WRITE_UINT16_BE (data, bits);
707 GST_WRITE_UINT16_BE (data + 2, length);
715 ("rtp buffer too small: need more than %d bytes but only have %"
716 G_GSIZE_FORMAT " bytes", min_size, rtp->map.size);
722 * gst_rtp_buffer_get_ssrc:
723 * @rtp: the RTP packet
725 * Get the SSRC of the RTP packet in @buffer.
727 * Returns: the SSRC of @buffer in host order.
730 gst_rtp_buffer_get_ssrc (GstRTPBuffer * rtp)
732 return g_ntohl (GST_RTP_HEADER_SSRC (rtp->map.data));
736 * gst_rtp_buffer_set_ssrc:
737 * @rtp: the RTP packet
738 * @ssrc: the new SSRC
740 * Set the SSRC on the RTP packet in @buffer to @ssrc.
743 gst_rtp_buffer_set_ssrc (GstRTPBuffer * rtp, guint32 ssrc)
745 GST_RTP_HEADER_SSRC (rtp->map.data) = g_htonl (ssrc);
749 * gst_rtp_buffer_get_csrc_count:
750 * @rtp: the RTP packet
752 * Get the CSRC count of the RTP packet in @buffer.
754 * Returns: the CSRC count of @buffer.
757 gst_rtp_buffer_get_csrc_count (GstRTPBuffer * rtp)
759 return GST_RTP_HEADER_CSRC_COUNT (rtp->map.data);
763 * gst_rtp_buffer_get_csrc:
764 * @rtp: the RTP packet
765 * @idx: the index of the CSRC to get
767 * Get the CSRC at index @idx in @buffer.
769 * Returns: the CSRC at index @idx in host order.
772 gst_rtp_buffer_get_csrc (GstRTPBuffer * rtp, guint8 idx)
776 data = rtp->map.data;
778 g_return_val_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data), 0);
780 return GST_READ_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx));
784 * gst_rtp_buffer_set_csrc:
785 * @rtp: the RTP packet
786 * @idx: the CSRC index to set
787 * @csrc: the CSRC in host order to set at @idx
789 * Modify the CSRC at index @idx in @buffer to @csrc.
792 gst_rtp_buffer_set_csrc (GstRTPBuffer * rtp, guint8 idx, guint32 csrc)
796 data = rtp->map.data;
798 g_return_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data));
800 GST_WRITE_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx), csrc);
804 * gst_rtp_buffer_get_marker:
805 * @rtp: the RTP packet
807 * Check if the marker bit is set on the RTP packet in @buffer.
809 * Returns: TRUE if @buffer has the marker bit set.
812 gst_rtp_buffer_get_marker (GstRTPBuffer * rtp)
814 return GST_RTP_HEADER_MARKER (rtp->map.data);
818 * gst_rtp_buffer_set_marker:
819 * @rtp: the RTP packet
820 * @marker: the new marker
822 * Set the marker bit on the RTP packet in @buffer to @marker.
825 gst_rtp_buffer_set_marker (GstRTPBuffer * rtp, gboolean marker)
827 GST_RTP_HEADER_MARKER (rtp->map.data) = marker;
831 * gst_rtp_buffer_get_payload_type:
832 * @rtp: the RTP packet
834 * Get the payload type of the RTP packet in @buffer.
836 * Returns: The payload type.
839 gst_rtp_buffer_get_payload_type (GstRTPBuffer * rtp)
841 return GST_RTP_HEADER_PAYLOAD_TYPE (rtp->map.data);
845 * gst_rtp_buffer_set_payload_type:
846 * @rtp: the RTP packet
847 * @payload_type: the new type
849 * Set the payload type of the RTP packet in @buffer to @payload_type.
852 gst_rtp_buffer_set_payload_type (GstRTPBuffer * rtp, guint8 payload_type)
854 g_return_if_fail (payload_type < 0x80);
856 GST_RTP_HEADER_PAYLOAD_TYPE (rtp->map.data) = payload_type;
860 * gst_rtp_buffer_get_seq:
861 * @rtp: the RTP packet
863 * Get the sequence number of the RTP packet in @buffer.
865 * Returns: The sequence number in host order.
868 gst_rtp_buffer_get_seq (GstRTPBuffer * rtp)
870 return g_ntohs (GST_RTP_HEADER_SEQ (rtp->map.data));
874 * gst_rtp_buffer_set_seq:
875 * @rtp: the RTP packet
876 * @seq: the new sequence number
878 * Set the sequence number of the RTP packet in @buffer to @seq.
881 gst_rtp_buffer_set_seq (GstRTPBuffer * rtp, guint16 seq)
883 GST_RTP_HEADER_SEQ (rtp->map.data) = g_htons (seq);
887 * gst_rtp_buffer_get_timestamp:
888 * @rtp: the RTP packet
890 * Get the timestamp of the RTP packet in @buffer.
892 * Returns: The timestamp in host order.
895 gst_rtp_buffer_get_timestamp (GstRTPBuffer * rtp)
897 return g_ntohl (GST_RTP_HEADER_TIMESTAMP (rtp->map.data));
901 * gst_rtp_buffer_set_timestamp:
902 * @rtp: the RTP packet
903 * @timestamp: the new timestamp
905 * Set the timestamp of the RTP packet in @buffer to @timestamp.
908 gst_rtp_buffer_set_timestamp (GstRTPBuffer * rtp, guint32 timestamp)
910 GST_RTP_HEADER_TIMESTAMP (rtp->map.data) = g_htonl (timestamp);
915 * gst_rtp_buffer_get_payload_subbuffer:
916 * @rtp: the RTP packet
917 * @offset: the offset in the payload
918 * @len: the length in the payload
920 * Create a subbuffer of the payload of the RTP packet in @buffer. @offset bytes
921 * are skipped in the payload and the subbuffer will be of size @len.
922 * If @len is -1 the total payload starting from @offset if subbuffered.
924 * Returns: A new buffer with the specified data of the payload.
929 gst_rtp_buffer_get_payload_subbuffer (GstRTPBuffer * rtp, guint offset,
934 plen = gst_rtp_buffer_get_payload_len (rtp);
935 /* we can't go past the length */
936 if (G_UNLIKELY (offset >= plen))
940 poffset = gst_rtp_buffer_get_header_len (rtp) + offset;
943 /* see if we need to shrink the buffer based on @len */
944 if (len != -1 && len < plen)
947 return gst_buffer_copy_region (rtp->buffer, GST_BUFFER_COPY_ALL, poffset,
953 g_warning ("offset=%u should be less then plen=%u", offset, plen);
959 * gst_rtp_buffer_get_payload_buffer:
960 * @rtp: the RTP packet
962 * Create a buffer of the payload of the RTP packet in @buffer. This function
963 * will internally create a subbuffer of @buffer so that a memcpy can be
966 * Returns: A new buffer with the data of the payload.
969 gst_rtp_buffer_get_payload_buffer (GstRTPBuffer * rtp)
971 return gst_rtp_buffer_get_payload_subbuffer (rtp, 0, -1);
975 * gst_rtp_buffer_get_payload_len:
976 * @rtp: the RTP packet
978 * Get the length of the payload of the RTP packet in @buffer.
980 * Returns: The length of the payload in @buffer.
983 gst_rtp_buffer_get_payload_len (GstRTPBuffer * rtp)
988 size = rtp->map.size;
989 data = rtp->map.data;
991 len = size - gst_rtp_buffer_get_header_len (rtp);
993 if (GST_RTP_HEADER_PADDING (data))
994 len -= data[size - 1];
1000 * gst_rtp_buffer_get_payload:
1001 * @rtp: the RTP packet
1003 * Get a pointer to the payload data in @buffer. This pointer is valid as long
1004 * as a reference to @buffer is held.
1006 * Returns: A pointer to the payload data in @buffer.
1009 gst_rtp_buffer_get_payload (GstRTPBuffer * rtp)
1011 return rtp->map.data + gst_rtp_buffer_get_header_len (rtp);
1015 * gst_rtp_buffer_default_clock_rate:
1016 * @payload_type: the static payload type
1018 * Get the default clock-rate for the static payload type @payload_type.
1020 * Returns: the default clock rate or -1 if the payload type is not static or
1021 * the clock-rate is undefined.
1026 gst_rtp_buffer_default_clock_rate (guint8 payload_type)
1028 const GstRTPPayloadInfo *info;
1031 info = gst_rtp_payload_info_for_pt (payload_type);
1035 res = info->clock_rate;
1036 /* 0 means unknown so we have to return -1 from this function */
1044 * gst_rtp_buffer_compare_seqnum:
1045 * @seqnum1: a sequence number
1046 * @seqnum2: a sequence number
1048 * Compare two sequence numbers, taking care of wraparounds. This function
1049 * returns the difference between @seqnum1 and @seqnum2.
1051 * Returns: a negative value if @seqnum1 is bigger than @seqnum2, 0 if they
1052 * are equal or a positive value if @seqnum1 is smaller than @segnum2.
1057 gst_rtp_buffer_compare_seqnum (guint16 seqnum1, guint16 seqnum2)
1059 return (gint16) (seqnum2 - seqnum1);
1063 * gst_rtp_buffer_ext_timestamp:
1064 * @exttimestamp: a previous extended timestamp
1065 * @timestamp: a new timestamp
1067 * Update the @exttimestamp field with @timestamp. For the first call of the
1068 * method, @exttimestamp should point to a location with a value of -1.
1070 * This function makes sure that the returned value is a constantly increasing
1071 * value even in the case where there is a timestamp wraparound.
1073 * Returns: The extended timestamp of @timestamp.
1078 gst_rtp_buffer_ext_timestamp (guint64 * exttimestamp, guint32 timestamp)
1080 guint64 result, diff, ext;
1082 g_return_val_if_fail (exttimestamp != NULL, -1);
1084 ext = *exttimestamp;
1089 /* pick wraparound counter from previous timestamp and add to new timestamp */
1090 result = timestamp + (ext & ~(G_GINT64_CONSTANT (0xffffffff)));
1092 /* check for timestamp wraparound */
1094 diff = ext - result;
1096 diff = result - ext;
1098 if (diff > G_MAXINT32) {
1099 /* timestamp went backwards more than allowed, we wrap around and get
1100 * updated extended timestamp. */
1101 result += (G_GINT64_CONSTANT (1) << 32);
1104 *exttimestamp = result;
1110 * gst_rtp_buffer_get_extension_onebyte_header:
1111 * @rtp: the RTP packet
1112 * @id: The ID of the header extension to be read (between 1 and 14).
1113 * @nth: Read the nth extension packet with the requested ID
1114 * @data: location for data
1115 * @size: the size of the data in bytes
1117 * Parses RFC 5285 style header extensions with a one byte header. It will
1118 * return the nth extension with the requested id.
1120 * Returns: TRUE if @buffer had the requested header extension
1126 gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1127 guint nth, gpointer * data, guint * size)
1135 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1137 if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata,
1145 guint8 read_id, read_len;
1147 if (offset + 1 >= wordlen * 4)
1150 read_id = GST_READ_UINT8 (pdata + offset) >> 4;
1151 read_len = (GST_READ_UINT8 (pdata + offset) & 0x0F) + 1;
1154 /* ID 0 means its padding, skip */
1158 /* ID 15 is special and means we should stop parsing */
1162 /* Ignore extension headers where the size does not fit */
1163 if (offset + read_len > wordlen * 4)
1166 /* If we have the right one */
1167 if (id == read_id) {
1170 *data = pdata + offset;
1181 if (offset >= wordlen * 4)
1189 * gst_rtp_buffer_get_extension_twobytes_header:
1190 * @rtp: the RTP packet
1191 * @appbits: Application specific bits
1192 * @id: The ID of the header extension to be read (between 1 and 14).
1193 * @nth: Read the nth extension packet with the requested ID
1194 * @data: location for data
1195 * @size: the size of the data in bytes
1197 * Parses RFC 5285 style header extensions with a two bytes header. It will
1198 * return the nth extension with the requested id.
1200 * Returns: TRUE if @buffer had the requested header extension
1206 gst_rtp_buffer_get_extension_twobytes_header (GstRTPBuffer * rtp,
1207 guint8 * appbits, guint8 id, guint nth, gpointer * data, guint * size)
1210 guint8 *pdata = NULL;
1216 if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer *) & pdata,
1220 if (bits >> 4 != 0x100)
1223 bytelen = wordlen * 4;
1226 guint8 read_id, read_len;
1228 if (offset + 2 >= bytelen)
1231 read_id = GST_READ_UINT8 (pdata + offset);
1237 read_len = GST_READ_UINT8 (pdata + offset);
1240 /* Ignore extension headers where the size does not fit */
1241 if (offset + read_len > bytelen)
1244 /* If we have the right one, return it */
1245 if (id == read_id) {
1248 *data = pdata + offset;
1266 get_onebyte_header_end_offset (guint8 * pdata, guint wordlen)
1269 guint bytelen = wordlen * 4;
1270 guint paddingcount = 0;
1272 while (offset + 1 < bytelen) {
1273 guint8 read_id, read_len;
1275 read_id = GST_READ_UINT8 (pdata + offset) >> 4;
1276 read_len = (GST_READ_UINT8 (pdata + offset) & 0x0F) + 1;
1279 /* ID 0 means its padding, skip */
1287 /* ID 15 is special and means we should stop parsing */
1288 /* It also means we can't add an extra packet */
1292 /* Ignore extension headers where the size does not fit */
1293 if (offset + read_len > bytelen)
1299 return offset - paddingcount;
1303 * gst_rtp_buffer_add_extension_onebyte_header:
1304 * @rtp: the RTP packet
1305 * @id: The ID of the header extension (between 1 and 14).
1306 * @data: location for data
1307 * @size: the size of the data in bytes
1309 * Adds a RFC 5285 header extension with a one byte header to the end of the
1310 * RTP header. If there is already a RFC 5285 header extension with a one byte
1311 * header, the new extension will be appended.
1312 * It will not work if there is already a header extension that does not follow
1313 * the mecanism described in RFC 5285 or if there is a header extension with
1314 * a two bytes header as described in RFC 5285. In that case, use
1315 * gst_rtp_buffer_add_extension_twobytes_header()
1317 * Returns: %TRUE if header extension could be added
1323 gst_rtp_buffer_add_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1324 gpointer data, guint size)
1331 g_return_val_if_fail (id > 0 && id < 15, FALSE);
1332 g_return_val_if_fail (size >= 1 && size <= 16, FALSE);
1333 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1335 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1336 (gpointer) & pdata, &wordlen);
1346 offset = get_onebyte_header_end_offset (pdata, wordlen);
1350 nextext = pdata + offset;
1351 offset = nextext - rtp->map.data;
1353 /* Don't add extra header if there isn't enough space */
1354 if (rtp->map.size < offset + size + 1)
1357 nextext[0] = (id << 4) | (0x0F & (size - 1));
1358 memcpy (nextext + 1, data, size);
1360 extlen = nextext - pdata + size + 1;
1362 wordlen = extlen / 4 + 1;
1363 memset (nextext + size + 1, 0, 4 - extlen % 4);
1365 wordlen = extlen / 4;
1368 gst_rtp_buffer_set_extension_data (rtp, 0xBEDE, wordlen);
1370 wordlen = (size + 1) / 4 + (((size + 1) % 4) ? 1 : 0);
1372 gst_rtp_buffer_set_extension_data (rtp, 0xBEDE, wordlen);
1374 gst_rtp_buffer_get_extension_data (rtp, &bits,
1375 (gpointer) & pdata, &wordlen);
1377 pdata[0] = (id << 4) | (0x0F & (size - 1));
1378 memcpy (pdata + 1, data, size);
1381 memset (pdata + size + 1, 0, 4 - ((size + 1) % 4));
1389 get_twobytes_header_end_offset (guint8 * pdata, guint wordlen)
1392 guint bytelen = wordlen * 4;
1393 guint paddingcount = 0;
1395 while (offset + 2 < bytelen) {
1396 guint8 read_id, read_len;
1398 read_id = GST_READ_UINT8 (pdata + offset);
1401 /* ID 0 means its padding, skip */
1409 read_len = GST_READ_UINT8 (pdata + offset);
1412 /* Ignore extension headers where the size does not fit */
1413 if (offset + read_len > bytelen)
1419 return offset - paddingcount;
1423 * gst_rtp_buffer_add_extension_twobytes_header:
1424 * @rtp: the RTP packet
1425 * @appbits: Application specific bits
1426 * @id: The ID of the header extension
1427 * @data: location for data
1428 * @size: the size of the data in bytes
1430 * Adds a RFC 5285 header extension with a two bytes header to the end of the
1431 * RTP header. If there is already a RFC 5285 header extension with a two bytes
1432 * header, the new extension will be appended.
1433 * It will not work if there is already a header extension that does not follow
1434 * the mecanism described in RFC 5285 or if there is a header extension with
1435 * a one byte header as described in RFC 5285. In that case, use
1436 * gst_rtp_buffer_add_extension_onebyte_header()
1438 * Returns: %TRUE if header extension could be added
1444 gst_rtp_buffer_add_extension_twobytes_header (GstRTPBuffer * rtp,
1445 guint8 appbits, guint8 id, gpointer data, guint size)
1452 g_return_val_if_fail ((appbits & 0xF0) == 0, FALSE);
1453 g_return_val_if_fail (size < 256, FALSE);
1454 g_return_val_if_fail (gst_buffer_is_writable (rtp->buffer), FALSE);
1456 has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1457 (gpointer) & pdata, &wordlen);
1464 if (bits != ((0x100 << 4) | (appbits & 0x0f)))
1467 offset = get_twobytes_header_end_offset (pdata, wordlen);
1469 nextext = pdata + offset;
1471 offset = nextext - rtp->map.data;
1473 /* Don't add extra header if there isn't enough space */
1474 if (rtp->map.size < offset + size + 2)
1479 memcpy (nextext + 2, data, size);
1481 extlen = nextext - pdata + size + 2;
1483 wordlen = extlen / 4 + 1;
1484 memset (nextext + size + 2, 0, 4 - extlen % 4);
1486 wordlen = extlen / 4;
1489 gst_rtp_buffer_set_extension_data (rtp, (0x100 << 4) | (appbits & 0x0F),
1492 wordlen = (size + 2) / 4 + (((size + 2) % 4) ? 1 : 0);
1494 gst_rtp_buffer_set_extension_data (rtp, (0x100 << 4) | (appbits & 0x0F),
1497 gst_rtp_buffer_get_extension_data (rtp, &bits,
1498 (gpointer) & pdata, &wordlen);
1502 memcpy (pdata + 2, data, size);
1504 memset (pdata + size + 2, 0, 4 - ((size + 2) % 4));