Use g_memdup2() where available and add fallback for older GLib versions
[platform/upstream/gstreamer.git] / gst-libs / gst / rtp / gstrtpbuffer.c
1 /* GStreamer
2  * Copyright (C) <2005> Philippe Khalaf <burger@speedy.org>
3  * Copyright (C) <2006> Wim Taymans <wim@fluendo.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:gstrtpbuffer
23  * @title: GstRTPBuffer
24  * @short_description: Helper methods for dealing with RTP buffers
25  * @see_also: #GstRTPBasePayload, #GstRTPBaseDepayload, gstrtcpbuffer
26  *
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.
30  *
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "gstrtpbuffer.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #define GST_RTP_HEADER_LEN 12
42
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
45  */
46 typedef struct _GstRTPHeader
47 {
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 */
62 #else
63 #error "G_BYTE_ORDER should be big or little endian."
64 #endif
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 */
69 } GstRTPHeader;
70
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))
84
85 /**
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
91  *
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.
97  */
98 void
99 gst_rtp_buffer_allocate_data (GstBuffer * buffer, guint payload_len,
100     guint8 pad_len, guint8 csrc_count)
101 {
102   GstMapInfo map;
103   GstMemory *mem;
104   gsize hlen;
105
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));
109
110   gst_buffer_remove_all_memory (buffer);
111
112   hlen = GST_RTP_HEADER_LEN + csrc_count * sizeof (guint32);
113
114   mem = gst_allocator_alloc (NULL, hlen, NULL);
115
116   gst_memory_map (mem, &map, GST_MAP_WRITE);
117   /* fill in defaults */
118   GST_RTP_HEADER_VERSION (map.data) = GST_RTP_VERSION;
119   if (pad_len)
120     GST_RTP_HEADER_PADDING (map.data) = TRUE;
121   else
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);
133
134   gst_buffer_append_memory (buffer, mem);
135
136   if (payload_len) {
137     mem = gst_allocator_alloc (NULL, payload_len, NULL);
138     gst_buffer_append_memory (buffer, mem);
139   }
140   if (pad_len) {
141     mem = gst_allocator_alloc (NULL, pad_len, NULL);
142
143     gst_memory_map (mem, &map, GST_MAP_WRITE);
144     map.data[pad_len - 1] = pad_len;
145     gst_memory_unmap (mem, &map);
146
147     gst_buffer_append_memory (buffer, mem);
148   }
149 }
150
151 /**
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
156  *
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.
160  *
161  * Returns: A newly allocated buffer with @data and of size @len.
162  */
163 GstBuffer *
164 gst_rtp_buffer_new_take_data (gpointer data, gsize len)
165 {
166   g_return_val_if_fail (data != NULL, NULL);
167   g_return_val_if_fail (len > 0, NULL);
168
169   return gst_buffer_new_wrapped (data, len);
170 }
171
172 /**
173  * gst_rtp_buffer_new_copy_data:
174  * @data: (array length=len) (element-type guint8): data for the new
175  *   buffer
176  * @len: the length of data
177  *
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
180  * is freed.
181  *
182  * Returns: A newly allocated buffer with a copy of @data and of size @len.
183  */
184 GstBuffer *
185 gst_rtp_buffer_new_copy_data (gconstpointer data, gsize len)
186 {
187   return gst_rtp_buffer_new_take_data (g_memdup2 (data, len), len);
188 }
189
190 /**
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
195  *
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.
199  *
200  * Returns: A newly allocated buffer that can hold an RTP packet with given
201  * parameters.
202  */
203 GstBuffer *
204 gst_rtp_buffer_new_allocate (guint payload_len, guint8 pad_len,
205     guint8 csrc_count)
206 {
207   GstBuffer *result;
208
209   g_return_val_if_fail (csrc_count <= 15, NULL);
210
211   result = gst_buffer_new ();
212   gst_rtp_buffer_allocate_data (result, payload_len, pad_len, csrc_count);
213
214   return result;
215 }
216
217 /**
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
222  *
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.
227  *
228  * Returns: A newly allocated buffer that can hold an RTP packet of @packet_len.
229  */
230 GstBuffer *
231 gst_rtp_buffer_new_allocate_len (guint packet_len, guint8 pad_len,
232     guint8 csrc_count)
233 {
234   guint len;
235
236   g_return_val_if_fail (csrc_count <= 15, NULL);
237
238   len = gst_rtp_buffer_calc_payload_len (packet_len, pad_len, csrc_count);
239
240   return gst_rtp_buffer_new_allocate (len, pad_len, csrc_count);
241 }
242
243 /**
244  * gst_rtp_buffer_calc_header_len:
245  * @csrc_count: the number of CSRC entries
246  *
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.
249  *
250  * Returns: The length of an RTP header with @csrc_count CSRC entries.
251  */
252 guint
253 gst_rtp_buffer_calc_header_len (guint8 csrc_count)
254 {
255   g_return_val_if_fail (csrc_count <= 15, 0);
256
257   return GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32));
258 }
259
260 /**
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
265  *
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.
268  *
269  * Returns: The total length of an RTP header with given parameters.
270  */
271 guint
272 gst_rtp_buffer_calc_packet_len (guint payload_len, guint8 pad_len,
273     guint8 csrc_count)
274 {
275   g_return_val_if_fail (csrc_count <= 15, 0);
276
277   return payload_len + GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32))
278       + pad_len;
279 }
280
281 /**
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
286  *
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.
289  *
290  * Returns: The length of the payload of an RTP packet  with given parameters.
291  */
292 guint
293 gst_rtp_buffer_calc_payload_len (guint packet_len, guint8 pad_len,
294     guint8 csrc_count)
295 {
296   g_return_val_if_fail (csrc_count <= 15, 0);
297
298   if (packet_len <
299       GST_RTP_HEADER_LEN + (csrc_count * sizeof (guint32)) + pad_len)
300     return 0;
301
302   return packet_len - GST_RTP_HEADER_LEN - (csrc_count * sizeof (guint32))
303       - pad_len;
304 }
305
306 /**
307  * gst_rtp_buffer_map:
308  * @buffer: a #GstBuffer
309  * @flags: #GstMapFlags
310  * @rtp: (out): a #GstRTPBuffer
311  *
312  * Map the contents of @buffer into @rtp.
313  *
314  * Returns: %TRUE if @buffer could be mapped.
315  */
316 gboolean
317 gst_rtp_buffer_map (GstBuffer * buffer, GstMapFlags flags, GstRTPBuffer * rtp)
318 {
319   guint8 padding;
320   guint8 csrc_count;
321   guint header_len;
322   guint8 version, pt;
323   guint8 *data;
324   guint size;
325   gsize bufsize, skip;
326   guint idx, length;
327   guint n_mem;
328
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);
332
333   n_mem = gst_buffer_n_memory (buffer);
334   if (n_mem < 1)
335     goto no_memory;
336
337   /* map first memory, this should be the header */
338   if (!gst_buffer_map_range (buffer, 0, 1, &rtp->map[0], flags))
339     goto map_failed;
340
341   data = rtp->data[0] = rtp->map[0].data;
342   size = rtp->map[0].size;
343
344   /* the header must be completely in the first buffer */
345   header_len = GST_RTP_HEADER_LEN;
346   if (G_UNLIKELY (size < header_len))
347     goto wrong_length;
348
349   /* check version */
350   version = (data[0] & 0xc0);
351   if (G_UNLIKELY (version != (GST_RTP_VERSION << 6)))
352     goto wrong_version;
353
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. */
357   pt = data[1];
358   if (G_UNLIKELY (pt >= 200 && pt <= 204))
359     goto reserved_pt;
360
361   /* calc header length with csrc */
362   csrc_count = (data[0] & 0x0f);
363   header_len += csrc_count * sizeof (guint32);
364
365   rtp->size[0] = header_len;
366
367   bufsize = gst_buffer_get_size (buffer);
368
369   /* calc extension length when present. */
370   if (data[0] & 0x10) {
371     guint8 *extdata;
372     gsize extlen;
373
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))
377       goto wrong_length;
378
379     if (!gst_buffer_map_range (buffer, idx, length, &rtp->map[1], flags))
380       goto map_failed;
381
382     extdata = rtp->data[1] = rtp->map[1].data + skip;
383     /* skip id */
384     extdata += 2;
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 */
389     extlen += 4;
390
391     /* all extension bytes must be in this block */
392     if (G_UNLIKELY (rtp->map[1].size < extlen))
393       goto wrong_length;
394
395     rtp->size[1] = extlen;
396
397     header_len += rtp->size[1];
398   } else {
399     rtp->data[1] = NULL;
400     rtp->size[1] = 0;
401   }
402
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))
408       goto wrong_length;
409
410     if (!gst_buffer_map_range (buffer, idx, length, &rtp->map[3], flags))
411       goto map_failed;
412
413     padding = rtp->map[3].data[skip];
414     rtp->data[3] = rtp->map[3].data + skip + 1 - padding;
415     rtp->size[3] = padding;
416
417     if (skip + 1 < padding)
418       goto wrong_length;
419   } else {
420     rtp->data[3] = NULL;
421     rtp->size[3] = 0;
422     padding = 0;
423   }
424
425   /* check if padding and header not bigger than packet length */
426   if (G_UNLIKELY (bufsize < padding + header_len))
427     goto wrong_padding;
428
429   rtp->buffer = buffer;
430
431   if (n_mem == 1) {
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;
436   } else {
437     /* we have not yet mapped the payload */
438     rtp->data[2] = NULL;
439     rtp->size[2] = 0;
440   }
441
442   /* rtp->state = 0; *//* unused */
443
444   return TRUE;
445
446   /* ERRORS */
447 no_memory:
448   {
449     GST_ERROR ("buffer without memory");
450     return FALSE;
451   }
452 map_failed:
453   {
454     GST_ERROR ("failed to map memory");
455     return FALSE;
456   }
457 wrong_length:
458   {
459     GST_DEBUG ("length check failed");
460     goto dump_packet;
461   }
462 wrong_version:
463   {
464     GST_DEBUG ("version check failed (%d != %d)", version, GST_RTP_VERSION);
465     goto dump_packet;
466   }
467 reserved_pt:
468   {
469     GST_DEBUG ("reserved PT %d found", pt);
470     goto dump_packet;
471   }
472 wrong_padding:
473   {
474     GST_DEBUG ("padding check failed (%" G_GSIZE_FORMAT " - %d < %d)", bufsize,
475         header_len, padding);
476     goto dump_packet;
477   }
478 dump_packet:
479   {
480     gint i;
481
482     GST_MEMDUMP ("buffer", data, size);
483
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]);
487     }
488     return FALSE;
489   }
490 }
491
492 /**
493  * gst_rtp_buffer_unmap:
494  * @rtp: a #GstRTPBuffer
495  *
496  * Unmap @rtp previously mapped with gst_rtp_buffer_map().
497  */
498 void
499 gst_rtp_buffer_unmap (GstRTPBuffer * rtp)
500 {
501   gint i;
502
503   g_return_if_fail (rtp != NULL);
504   g_return_if_fail (rtp->buffer != NULL);
505
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;
510     }
511     rtp->data[i] = NULL;
512     rtp->size[i] = 0;
513   }
514   rtp->buffer = NULL;
515 }
516
517
518 /**
519  * gst_rtp_buffer_set_packet_len:
520  * @rtp: the RTP packet
521  * @len: the new packet length
522  *
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.
525  */
526 void
527 gst_rtp_buffer_set_packet_len (GstRTPBuffer * rtp, guint len)
528 {
529   guint8 *data;
530
531   data = rtp->data[0];
532
533   /* FIXME */
534
535   if (rtp->map[0].maxsize <= len) {
536     /* FIXME, realloc bigger space */
537     g_warning ("not implemented");
538   }
539
540   gst_buffer_set_size (rtp->buffer, len);
541   rtp->map[0].size = len;
542
543   /* remove any padding */
544   GST_RTP_HEADER_PADDING (data) = FALSE;
545 }
546
547 /**
548  * gst_rtp_buffer_get_packet_len:
549  * @rtp: the RTP packet
550  *
551  * Return the total length of the packet in @buffer.
552  *
553  * Returns: The total length of the packet in @buffer.
554  */
555 guint
556 gst_rtp_buffer_get_packet_len (GstRTPBuffer * rtp)
557 {
558   return gst_buffer_get_size (rtp->buffer);
559 }
560
561 /**
562  * gst_rtp_buffer_get_header_len:
563  * @rtp: the RTP packet
564  *
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.
567  *
568  * Returns: The total length of the header in @buffer.
569  */
570 guint
571 gst_rtp_buffer_get_header_len (GstRTPBuffer * rtp)
572 {
573   return rtp->size[0] + rtp->size[1];
574 }
575
576 /**
577  * gst_rtp_buffer_get_version:
578  * @rtp: the RTP packet
579  *
580  * Get the version number of the RTP packet in @buffer.
581  *
582  * Returns: The version of @buffer.
583  */
584 guint8
585 gst_rtp_buffer_get_version (GstRTPBuffer * rtp)
586 {
587   return GST_RTP_HEADER_VERSION (rtp->data[0]);
588 }
589
590 /**
591  * gst_rtp_buffer_set_version:
592  * @rtp: the RTP packet
593  * @version: the new version
594  *
595  * Set the version of the RTP packet in @buffer to @version.
596  */
597 void
598 gst_rtp_buffer_set_version (GstRTPBuffer * rtp, guint8 version)
599 {
600   g_return_if_fail (version < 0x04);
601
602   GST_RTP_HEADER_VERSION (rtp->data[0]) = version;
603 }
604
605 /**
606  * gst_rtp_buffer_get_padding:
607  * @rtp: the RTP packet
608  *
609  * Check if the padding bit is set on the RTP packet in @buffer.
610  *
611  * Returns: TRUE if @buffer has the padding bit set.
612  */
613 gboolean
614 gst_rtp_buffer_get_padding (GstRTPBuffer * rtp)
615 {
616   return GST_RTP_HEADER_PADDING (rtp->data[0]);
617 }
618
619 /**
620  * gst_rtp_buffer_set_padding:
621  * @rtp: the buffer
622  * @padding: the new padding
623  *
624  * Set the padding bit on the RTP packet in @buffer to @padding.
625  */
626 void
627 gst_rtp_buffer_set_padding (GstRTPBuffer * rtp, gboolean padding)
628 {
629   GST_RTP_HEADER_PADDING (rtp->data[0]) = padding;
630 }
631
632 /**
633  * gst_rtp_buffer_pad_to:
634  * @rtp: the RTP packet
635  * @len: the new amount of padding
636  *
637  * Set the amount of padding in the RTP packet in @buffer to
638  * @len. If @len is 0, the padding is removed.
639  *
640  * NOTE: This function does not work correctly.
641  */
642 void
643 gst_rtp_buffer_pad_to (GstRTPBuffer * rtp, guint len)
644 {
645   guint8 *data;
646
647   data = rtp->data[0];
648
649   if (len > 0)
650     GST_RTP_HEADER_PADDING (data) = TRUE;
651   else
652     GST_RTP_HEADER_PADDING (data) = FALSE;
653
654   /* FIXME, set the padding byte at the end of the payload data */
655 }
656
657 /**
658  * gst_rtp_buffer_get_extension:
659  * @rtp: the RTP packet
660  *
661  * Check if the extension bit is set on the RTP packet in @buffer.
662  *
663  * Returns: TRUE if @buffer has the extension bit set.
664  */
665 gboolean
666 gst_rtp_buffer_get_extension (GstRTPBuffer * rtp)
667 {
668   return GST_RTP_HEADER_EXTENSION (rtp->data[0]);
669 }
670
671 /**
672  * gst_rtp_buffer_set_extension:
673  * @rtp: the RTP packet
674  * @extension: the new extension
675  *
676  * Set the extension bit on the RTP packet in @buffer to @extension.
677  */
678 void
679 gst_rtp_buffer_set_extension (GstRTPBuffer * rtp, gboolean extension)
680 {
681   GST_RTP_HEADER_EXTENSION (rtp->data[0]) = extension;
682 }
683
684 /**
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
690  *
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.
694  *
695  * If @buffer did not contain an extension, this function will return %FALSE
696  * with @bits, @data and @wordlen unchanged.
697  *
698  * Returns: TRUE if @buffer had the extension bit set.
699  */
700 gboolean
701 gst_rtp_buffer_get_extension_data (GstRTPBuffer * rtp, guint16 * bits,
702     gpointer * data, guint * wordlen)
703 {
704   guint8 *pdata;
705
706   /* move to the extension */
707   pdata = rtp->data[1];
708   if (!pdata)
709     return FALSE;
710
711   if (bits)
712     *bits = GST_READ_UINT16_BE (pdata);
713   if (wordlen)
714     *wordlen = GST_READ_UINT16_BE (pdata + 2);
715   pdata += 4;
716   if (data)
717     *data = (gpointer *) pdata;
718
719   return TRUE;
720 }
721
722 /**
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
726  *
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
730  * #GBytes structure.
731  *
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.
735  *
736  * Returns: (transfer full): A new #GBytes if an extension header was present
737  * and %NULL otherwise.
738  *
739  * Since: 1.2
740  */
741 GBytes *
742 gst_rtp_buffer_get_extension_bytes (GstRTPBuffer * rtp, guint16 * bits)
743 {
744   gpointer buf_data = NULL;
745   guint buf_len;
746
747   g_return_val_if_fail (rtp != NULL, FALSE);
748
749   if (!gst_rtp_buffer_get_extension_data (rtp, bits, &buf_data, &buf_len))
750     return NULL;
751
752   if (buf_len == 0) {
753     /* if no extension data is present return an empty GBytes */
754     buf_data = NULL;
755   }
756
757   /* multiply length with 4 to get length in bytes */
758   return g_bytes_new (buf_data, 4 * buf_len);
759 }
760
761 static gboolean
762 gst_rtp_buffer_map_payload (GstRTPBuffer * rtp)
763 {
764   guint hlen, plen;
765   guint idx, length;
766   gsize skip;
767
768   if (rtp->map[2].memory != NULL)
769     return TRUE;
770
771   hlen = gst_rtp_buffer_get_header_len (rtp);
772   plen = gst_buffer_get_size (rtp->buffer) - hlen - rtp->size[3];
773
774   if (!gst_buffer_find_memory (rtp->buffer, hlen, plen, &idx, &length, &skip))
775     return FALSE;
776
777   if (!gst_buffer_map_range (rtp->buffer, idx, length, &rtp->map[2],
778           rtp->map[0].flags))
779     return FALSE;
780
781   rtp->data[2] = rtp->map[2].data + skip;
782   rtp->size[2] = plen;
783
784   return TRUE;
785 }
786
787 /* ensure header, payload and padding are in separate buffers */
788 static void
789 ensure_buffers (GstRTPBuffer * rtp)
790 {
791   guint i, pos;
792   gboolean changed = FALSE;
793
794   /* make sure payload is mapped */
795   gst_rtp_buffer_map_payload (rtp);
796
797   for (i = 0, pos = 0; i < 4; i++) {
798     if (rtp->size[i]) {
799       gsize offset = (guint8 *) rtp->data[i] - rtp->map[i].data;
800
801       if (offset != 0 || rtp->map[i].size != rtp->size[i]) {
802         GstMemory *mem;
803
804         /* make copy */
805         mem = gst_memory_copy (rtp->map[i].memory, offset, rtp->size[i]);
806
807         /* insert new memory */
808         gst_buffer_insert_memory (rtp->buffer, pos, mem);
809
810         changed = TRUE;
811       }
812       pos++;
813     }
814   }
815
816   if (changed) {
817     GstBuffer *buf = rtp->buffer;
818
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);
822   }
823 }
824
825 /**
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)
831  *
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
834  * be made larger.
835  *
836  * Returns: True if done.
837  */
838 gboolean
839 gst_rtp_buffer_set_extension_data (GstRTPBuffer * rtp, guint16 bits,
840     guint16 length)
841 {
842   guint32 min_size = 0;
843   guint8 *data;
844   GstMemory *mem = NULL;
845
846   ensure_buffers (rtp);
847
848   /* this is the size of the extension data we need */
849   min_size = 4 + length * sizeof (guint32);
850
851   /* we should allocate and map the extension data */
852   if (rtp->data[1] == NULL || min_size > rtp->size[1]) {
853     GstMapInfo map;
854
855     /* we don't have (enough) extension data, make some */
856     mem = gst_allocator_alloc (NULL, min_size, NULL);
857
858     if (rtp->data[1]) {
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]);
864       }
865       gst_memory_unmap (mem, &map);
866
867       /* unmap old */
868       gst_buffer_unmap (rtp->buffer, &rtp->map[1]);
869       gst_buffer_replace_memory (rtp->buffer, 1, mem);
870     } else {
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);
875
876       /* we didn't have extension data, add */
877       gst_buffer_insert_memory (rtp->buffer, 1, mem);
878     }
879
880     /* map new */
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;
885   }
886
887   /* now we can set the extension bit */
888   data = rtp->data[0];
889   GST_RTP_HEADER_EXTENSION (data) = TRUE;
890
891   data = rtp->data[1];
892   GST_WRITE_UINT16_BE (data, bits);
893   GST_WRITE_UINT16_BE (data + 2, length);
894
895   return TRUE;
896 }
897
898 /**
899  * gst_rtp_buffer_get_ssrc:
900  * @rtp: the RTP packet
901  *
902  * Get the SSRC of the RTP packet in @buffer.
903  *
904  * Returns: the SSRC of @buffer in host order.
905  */
906 guint32
907 gst_rtp_buffer_get_ssrc (GstRTPBuffer * rtp)
908 {
909   return g_ntohl (GST_RTP_HEADER_SSRC (rtp->data[0]));
910 }
911
912 /**
913  * gst_rtp_buffer_set_ssrc:
914  * @rtp: the RTP packet
915  * @ssrc: the new SSRC
916  *
917  * Set the SSRC on the RTP packet in @buffer to @ssrc.
918  */
919 void
920 gst_rtp_buffer_set_ssrc (GstRTPBuffer * rtp, guint32 ssrc)
921 {
922   GST_RTP_HEADER_SSRC (rtp->data[0]) = g_htonl (ssrc);
923 }
924
925 /**
926  * gst_rtp_buffer_get_csrc_count:
927  * @rtp: the RTP packet
928  *
929  * Get the CSRC count of the RTP packet in @buffer.
930  *
931  * Returns: the CSRC count of @buffer.
932  */
933 guint8
934 gst_rtp_buffer_get_csrc_count (GstRTPBuffer * rtp)
935 {
936   return GST_RTP_HEADER_CSRC_COUNT (rtp->data[0]);
937 }
938
939 /**
940  * gst_rtp_buffer_get_csrc:
941  * @rtp: the RTP packet
942  * @idx: the index of the CSRC to get
943  *
944  * Get the CSRC at index @idx in @buffer.
945  *
946  * Returns: the CSRC at index @idx in host order.
947  */
948 guint32
949 gst_rtp_buffer_get_csrc (GstRTPBuffer * rtp, guint8 idx)
950 {
951   guint8 *data;
952
953   data = rtp->data[0];
954
955   g_return_val_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data), 0);
956
957   return GST_READ_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx));
958 }
959
960 /**
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
965  *
966  * Modify the CSRC at index @idx in @buffer to @csrc.
967  */
968 void
969 gst_rtp_buffer_set_csrc (GstRTPBuffer * rtp, guint8 idx, guint32 csrc)
970 {
971   guint8 *data;
972
973   data = rtp->data[0];
974
975   g_return_if_fail (idx < GST_RTP_HEADER_CSRC_COUNT (data));
976
977   GST_WRITE_UINT32_BE (GST_RTP_HEADER_CSRC_LIST_OFFSET (data, idx), csrc);
978 }
979
980 /**
981  * gst_rtp_buffer_get_marker:
982  * @rtp: the RTP packet
983  *
984  * Check if the marker bit is set on the RTP packet in @buffer.
985  *
986  * Returns: TRUE if @buffer has the marker bit set.
987  */
988 gboolean
989 gst_rtp_buffer_get_marker (GstRTPBuffer * rtp)
990 {
991   return GST_RTP_HEADER_MARKER (rtp->data[0]);
992 }
993
994 /**
995  * gst_rtp_buffer_set_marker:
996  * @rtp: the RTP packet
997  * @marker: the new marker
998  *
999  * Set the marker bit on the RTP packet in @buffer to @marker.
1000  */
1001 void
1002 gst_rtp_buffer_set_marker (GstRTPBuffer * rtp, gboolean marker)
1003 {
1004   GST_RTP_HEADER_MARKER (rtp->data[0]) = marker;
1005 }
1006
1007 /**
1008  * gst_rtp_buffer_get_payload_type:
1009  * @rtp: the RTP packet
1010  *
1011  * Get the payload type of the RTP packet in @buffer.
1012  *
1013  * Returns: The payload type.
1014  */
1015 guint8
1016 gst_rtp_buffer_get_payload_type (GstRTPBuffer * rtp)
1017 {
1018   return GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]);
1019 }
1020
1021 /**
1022  * gst_rtp_buffer_set_payload_type:
1023  * @rtp: the RTP packet
1024  * @payload_type: the new type
1025  *
1026  * Set the payload type of the RTP packet in @buffer to @payload_type.
1027  */
1028 void
1029 gst_rtp_buffer_set_payload_type (GstRTPBuffer * rtp, guint8 payload_type)
1030 {
1031   g_return_if_fail (payload_type < 0x80);
1032
1033   GST_RTP_HEADER_PAYLOAD_TYPE (rtp->data[0]) = payload_type;
1034 }
1035
1036 /**
1037  * gst_rtp_buffer_get_seq:
1038  * @rtp: the RTP packet
1039  *
1040  * Get the sequence number of the RTP packet in @buffer.
1041  *
1042  * Returns: The sequence number in host order.
1043  */
1044 guint16
1045 gst_rtp_buffer_get_seq (GstRTPBuffer * rtp)
1046 {
1047   return g_ntohs (GST_RTP_HEADER_SEQ (rtp->data[0]));
1048 }
1049
1050 /**
1051  * gst_rtp_buffer_set_seq:
1052  * @rtp: the RTP packet
1053  * @seq: the new sequence number
1054  *
1055  * Set the sequence number of the RTP packet in @buffer to @seq.
1056  */
1057 void
1058 gst_rtp_buffer_set_seq (GstRTPBuffer * rtp, guint16 seq)
1059 {
1060   GST_RTP_HEADER_SEQ (rtp->data[0]) = g_htons (seq);
1061 }
1062
1063 /**
1064  * gst_rtp_buffer_get_timestamp:
1065  * @rtp: the RTP packet
1066  *
1067  * Get the timestamp of the RTP packet in @buffer.
1068  *
1069  * Returns: The timestamp in host order.
1070  */
1071 guint32
1072 gst_rtp_buffer_get_timestamp (GstRTPBuffer * rtp)
1073 {
1074   return g_ntohl (GST_RTP_HEADER_TIMESTAMP (rtp->data[0]));
1075 }
1076
1077 /**
1078  * gst_rtp_buffer_set_timestamp:
1079  * @rtp: the RTP packet
1080  * @timestamp: the new timestamp
1081  *
1082  * Set the timestamp of the RTP packet in @buffer to @timestamp.
1083  */
1084 void
1085 gst_rtp_buffer_set_timestamp (GstRTPBuffer * rtp, guint32 timestamp)
1086 {
1087   GST_RTP_HEADER_TIMESTAMP (rtp->data[0]) = g_htonl (timestamp);
1088 }
1089
1090
1091 /**
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
1096  *
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.
1100  *
1101  * Returns: A new buffer with the specified data of the payload.
1102  */
1103 GstBuffer *
1104 gst_rtp_buffer_get_payload_subbuffer (GstRTPBuffer * rtp, guint offset,
1105     guint len)
1106 {
1107   guint poffset, plen;
1108
1109   plen = gst_rtp_buffer_get_payload_len (rtp);
1110   /* we can't go past the length */
1111   if (G_UNLIKELY (offset > plen))
1112     goto wrong_offset;
1113
1114   /* apply offset */
1115   poffset = gst_rtp_buffer_get_header_len (rtp) + offset;
1116   plen -= offset;
1117
1118   /* see if we need to shrink the buffer based on @len */
1119   if (len != -1 && len < plen)
1120     plen = len;
1121
1122   return gst_buffer_copy_region (rtp->buffer, GST_BUFFER_COPY_ALL, poffset,
1123       plen);
1124
1125   /* ERRORS */
1126 wrong_offset:
1127   {
1128     g_warning ("offset=%u should be less than plen=%u", offset, plen);
1129     return NULL;
1130   }
1131 }
1132
1133 /**
1134  * gst_rtp_buffer_get_payload_buffer:
1135  * @rtp: the RTP packet
1136  *
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
1139  * avoided.
1140  *
1141  * Returns: A new buffer with the data of the payload.
1142  */
1143 GstBuffer *
1144 gst_rtp_buffer_get_payload_buffer (GstRTPBuffer * rtp)
1145 {
1146   return gst_rtp_buffer_get_payload_subbuffer (rtp, 0, -1);
1147 }
1148
1149 /**
1150  * gst_rtp_buffer_get_payload_len:
1151  * @rtp: the RTP packet
1152  *
1153  * Get the length of the payload of the RTP packet in @buffer.
1154  *
1155  * Returns: The length of the payload in @buffer.
1156  */
1157 guint
1158 gst_rtp_buffer_get_payload_len (GstRTPBuffer * rtp)
1159 {
1160   return gst_buffer_get_size (rtp->buffer) - gst_rtp_buffer_get_header_len (rtp)
1161       - rtp->size[3];
1162 }
1163
1164 /**
1165  * gst_rtp_buffer_get_payload: (skip)
1166  * @rtp: the RTP packet
1167  *
1168  * Get a pointer to the payload data in @buffer. This pointer is valid as long
1169  * as a reference to @buffer is held.
1170  *
1171  * Returns: (array) (element-type guint8) (transfer none): A pointer
1172  * to the payload data in @buffer.
1173  */
1174 gpointer
1175 gst_rtp_buffer_get_payload (GstRTPBuffer * rtp)
1176 {
1177   if (rtp->data[2])
1178     return rtp->data[2];
1179
1180   if (!gst_rtp_buffer_map_payload (rtp))
1181     return NULL;
1182
1183   return rtp->data[2];
1184 }
1185
1186 /**
1187  * gst_rtp_buffer_get_payload_bytes: (rename-to gst_rtp_buffer_get_payload)
1188  * @rtp: the RTP packet
1189  *
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.
1193  *
1194  * Returns: (transfer full): A new #GBytes containing the payload data in @rtp.
1195  *
1196  * Since: 1.2
1197  */
1198 GBytes *
1199 gst_rtp_buffer_get_payload_bytes (GstRTPBuffer * rtp)
1200 {
1201   gpointer data;
1202
1203   g_return_val_if_fail (rtp != NULL, NULL);
1204
1205   data = gst_rtp_buffer_get_payload (rtp);
1206   if (data == NULL)
1207     return NULL;
1208
1209   return g_bytes_new (data, gst_rtp_buffer_get_payload_len (rtp));
1210 }
1211
1212 /**
1213  * gst_rtp_buffer_default_clock_rate:
1214  * @payload_type: the static payload type
1215  *
1216  * Get the default clock-rate for the static payload type @payload_type.
1217  *
1218  * Returns: the default clock rate or -1 if the payload type is not static or
1219  * the clock-rate is undefined.
1220  */
1221 guint32
1222 gst_rtp_buffer_default_clock_rate (guint8 payload_type)
1223 {
1224   const GstRTPPayloadInfo *info;
1225   guint32 res;
1226
1227   info = gst_rtp_payload_info_for_pt (payload_type);
1228   if (!info)
1229     return -1;
1230
1231   res = info->clock_rate;
1232   /* 0 means unknown so we have to return -1 from this function */
1233   if (res == 0)
1234     res = -1;
1235
1236   return res;
1237 }
1238
1239 /**
1240  * gst_rtp_buffer_compare_seqnum:
1241  * @seqnum1: a sequence number
1242  * @seqnum2: a sequence number
1243  *
1244  * Compare two sequence numbers, taking care of wraparounds. This function
1245  * returns the difference between @seqnum1 and @seqnum2.
1246  *
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.
1249  */
1250 gint
1251 gst_rtp_buffer_compare_seqnum (guint16 seqnum1, guint16 seqnum2)
1252 {
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
1257    */
1258   return (gint16) (seqnum2 - seqnum1);
1259 }
1260
1261 /**
1262  * gst_rtp_buffer_ext_timestamp:
1263  * @exttimestamp: (inout): a previous extended timestamp
1264  * @timestamp: a new timestamp
1265  *
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.
1269  *
1270  * This function is able to handle both forward and backward timestamps taking
1271  * into account:
1272  *   - timestamp wraparound making sure that the returned value is properly increased.
1273  *   - timestamp unwraparound making sure that the returned value is properly decreased.
1274  *
1275  * Returns: The extended timestamp of @timestamp or 0 if the result can't go anywhere backwards.
1276  */
1277 guint64
1278 gst_rtp_buffer_ext_timestamp (guint64 * exttimestamp, guint32 timestamp)
1279 {
1280   guint64 result, ext;
1281
1282   g_return_val_if_fail (exttimestamp != NULL, -1);
1283
1284   ext = *exttimestamp;
1285
1286   if (ext == -1) {
1287     result = timestamp;
1288   } else {
1289     /* pick wraparound counter from previous timestamp and add to new timestamp */
1290     result = timestamp + (ext & ~(G_GUINT64_CONSTANT (0xffffffff)));
1291
1292     /* check for timestamp wraparound */
1293     if (result < ext) {
1294       guint64 diff = ext - result;
1295
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);
1300       }
1301     } else {
1302       guint64 diff = result - ext;
1303
1304       if (diff > G_MAXINT32) {
1305         if (result < (G_GUINT64_CONSTANT (1) << 32)) {
1306           GST_WARNING
1307               ("Cannot unwrap, any wrapping took place yet. Returning 0 without updating extended timestamp.");
1308           return 0;
1309         } else {
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 */
1314           return result;
1315         }
1316       }
1317     }
1318   }
1319
1320   *exttimestamp = result;
1321
1322   return result;
1323 }
1324
1325
1326 static gboolean
1327 _get_extension_onebyte_header (const guint8 * pdata, guint len,
1328     guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1329 {
1330   gulong offset = 0;
1331   guint count = 0;
1332
1333   g_return_val_if_fail (id > 0 && id < 15, FALSE);
1334
1335   if (bit_pattern != 0xBEDE)
1336     return FALSE;
1337
1338   for (;;) {
1339     guint8 read_id, read_len;
1340
1341     if (offset + 1 >= len)
1342       break;
1343
1344     read_id = GST_READ_UINT8 (pdata + offset) >> 4;
1345     read_len = (GST_READ_UINT8 (pdata + offset) & 0x0F) + 1;
1346     offset += 1;
1347
1348     /* ID 0 means its padding, skip */
1349     if (read_id == 0)
1350       continue;
1351
1352     /* ID 15 is special and means we should stop parsing */
1353     if (read_id == 15)
1354       break;
1355
1356     /* Ignore extension headers where the size does not fit */
1357     if (offset + read_len > len)
1358       break;
1359
1360     /* If we have the right one */
1361     if (id == read_id) {
1362       if (nth == count) {
1363         if (data)
1364           *data = (gpointer) & pdata[offset];
1365         if (size)
1366           *size = read_len;
1367
1368         return TRUE;
1369       }
1370
1371       count++;
1372     }
1373     offset += read_len;
1374
1375     if (offset >= len)
1376       break;
1377   }
1378
1379   return FALSE;
1380 }
1381
1382
1383 /**
1384  * gst_rtp_buffer_get_extension_onebyte_header_from_bytes:
1385  * @bytes: #GBytes
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):
1390  *   location for data
1391  * @size: (out): the size of the data in bytes
1392  *
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.
1397  *
1398  * Returns: TRUE if @bytes had the requested header extension
1399  *
1400  * Since: 1.18
1401  */
1402 gboolean
1403 gst_rtp_buffer_get_extension_onebyte_header_from_bytes (GBytes * bytes,
1404     guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
1405 {
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,
1409       size);
1410 }
1411
1412
1413 /**
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):
1419  *   location for data
1420  * @size: (out): the size of the data in bytes
1421  *
1422  * Parses RFC 5285 style header extensions with a one byte header. It will
1423  * return the nth extension with the requested id.
1424  *
1425  * Returns: TRUE if @buffer had the requested header extension
1426  */
1427
1428 gboolean
1429 gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1430     guint nth, gpointer * data, guint * size)
1431 {
1432   guint16 bit_pattern;
1433   guint8 *pdata;
1434   guint wordlen;
1435
1436   if (!gst_rtp_buffer_get_extension_data (rtp, &bit_pattern, (gpointer) & pdata,
1437           &wordlen))
1438     return FALSE;
1439
1440   return _get_extension_onebyte_header (pdata, wordlen * 4, bit_pattern, id,
1441       nth, data, size);
1442 }
1443
1444
1445 /**
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):
1452  *   location for data
1453  * @size: (out): the size of the data in bytes
1454  *
1455  * Parses RFC 5285 style header extensions with a two bytes header. It will
1456  * return the nth extension with the requested id.
1457  *
1458  * Returns: TRUE if @buffer had the requested header extension
1459  */
1460
1461 gboolean
1462 gst_rtp_buffer_get_extension_twobytes_header (GstRTPBuffer * rtp,
1463     guint8 * appbits, guint8 id, guint nth, gpointer * data, guint * size)
1464 {
1465   guint16 bits;
1466   guint8 *pdata = NULL;
1467   guint wordlen;
1468   guint bytelen;
1469   gulong offset = 0;
1470   guint count = 0;
1471
1472   if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer *) & pdata,
1473           &wordlen))
1474     return FALSE;
1475
1476   if (bits >> 4 != 0x100)
1477     return FALSE;
1478
1479   bytelen = wordlen * 4;
1480
1481   for (;;) {
1482     guint8 read_id, read_len;
1483
1484     if (offset + 2 >= bytelen)
1485       break;
1486
1487     read_id = GST_READ_UINT8 (pdata + offset);
1488     offset += 1;
1489
1490     if (read_id == 0)
1491       continue;
1492
1493     read_len = GST_READ_UINT8 (pdata + offset);
1494     offset += 1;
1495
1496     /* Ignore extension headers where the size does not fit */
1497     if (offset + read_len > bytelen)
1498       break;
1499
1500     /* If we have the right one, return it */
1501     if (id == read_id) {
1502       if (nth == count) {
1503         if (data)
1504           *data = pdata + offset;
1505         if (size)
1506           *size = read_len;
1507         if (appbits)
1508           *appbits = bits;
1509
1510         return TRUE;
1511       }
1512
1513       count++;
1514     }
1515     offset += read_len;
1516   }
1517
1518   return FALSE;
1519 }
1520
1521 static gboolean
1522 get_onebyte_header_end_offset (guint8 * pdata, guint wordlen, guint * offset)
1523 {
1524   guint bytelen = wordlen * 4;
1525   guint paddingcount = 0;
1526
1527   *offset = 0;
1528
1529   while (*offset + 1 < bytelen) {
1530     guint8 read_id, read_len;
1531
1532     read_id = GST_READ_UINT8 (pdata + *offset) >> 4;
1533     read_len = (GST_READ_UINT8 (pdata + *offset) & 0x0F) + 1;
1534     *offset += 1;
1535
1536     /* ID 0 means its padding, skip */
1537     if (read_id == 0) {
1538       paddingcount++;
1539       continue;
1540     }
1541
1542     paddingcount = 0;
1543
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) {
1547       return FALSE;
1548     }
1549
1550     /* Ignore extension headers where the size does not fit */
1551     if (*offset + read_len > bytelen) {
1552       return FALSE;
1553     }
1554
1555     *offset += read_len;
1556   }
1557
1558   *offset -= paddingcount;
1559
1560   return TRUE;
1561 }
1562
1563 /**
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
1569  *
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()
1577  *
1578  * Returns: %TRUE if header extension could be added
1579  */
1580
1581 gboolean
1582 gst_rtp_buffer_add_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
1583     gconstpointer data, guint size)
1584 {
1585   guint16 bits;
1586   guint8 *pdata = 0;
1587   guint wordlen = 0;
1588   guint wordlen_new;
1589   gboolean has_bit;
1590   guint extlen, offset = 0;
1591
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);
1595
1596   has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1597       (gpointer) & pdata, &wordlen);
1598
1599   if (has_bit) {
1600     if (bits != 0xBEDE)
1601       return FALSE;
1602
1603     if (!get_onebyte_header_end_offset (pdata, wordlen, &offset))
1604       return FALSE;
1605   }
1606
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);
1612
1613   gst_rtp_buffer_set_extension_data (rtp, 0xBEDE, wordlen_new);
1614   gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1615
1616   pdata += offset;
1617
1618   pdata[0] = (id << 4) | (0x0F & (size - 1));
1619   memcpy (pdata + 1, data, size);
1620
1621   if (extlen % 4)
1622     memset (pdata + 1 + size, 0, 4 - (extlen % 4));
1623
1624   return TRUE;
1625 }
1626
1627
1628 static guint
1629 get_twobytes_header_end_offset (const guint8 * pdata, guint wordlen)
1630 {
1631   guint offset = 0;
1632   guint bytelen = wordlen * 4;
1633   guint paddingcount = 0;
1634
1635   while (offset + 2 < bytelen) {
1636     guint8 read_id, read_len;
1637
1638     read_id = GST_READ_UINT8 (pdata + offset);
1639     offset += 1;
1640
1641     /* ID 0 means its padding, skip */
1642     if (read_id == 0) {
1643       paddingcount++;
1644       continue;
1645     }
1646
1647     paddingcount = 0;
1648
1649     read_len = GST_READ_UINT8 (pdata + offset);
1650     offset += 1;
1651
1652     /* Ignore extension headers where the size does not fit */
1653     if (offset + read_len > bytelen)
1654       return 0;
1655
1656     offset += read_len;
1657   }
1658
1659   return offset - paddingcount;
1660 }
1661
1662 /**
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
1669  *
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()
1677  *
1678  * Returns: %TRUE if header extension could be added
1679  */
1680
1681 gboolean
1682 gst_rtp_buffer_add_extension_twobytes_header (GstRTPBuffer * rtp,
1683     guint8 appbits, guint8 id, gconstpointer data, guint size)
1684 {
1685   guint16 bits;
1686   guint8 *pdata = 0;
1687   guint wordlen;
1688   gboolean has_bit;
1689   gulong offset = 0;
1690   guint extlen;
1691
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);
1695
1696   has_bit = gst_rtp_buffer_get_extension_data (rtp, &bits,
1697       (gpointer) & pdata, &wordlen);
1698
1699   if (has_bit) {
1700     if (bits != ((0x100 << 4) | (appbits & 0x0f)))
1701       return FALSE;
1702
1703     offset = get_twobytes_header_end_offset (pdata, wordlen);
1704     if (offset == 0)
1705       return FALSE;
1706   }
1707
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);
1712
1713   gst_rtp_buffer_set_extension_data (rtp, (0x100 << 4) | (appbits & 0x0F),
1714       wordlen);
1715   gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata, &wordlen);
1716
1717   pdata += offset;
1718
1719   pdata[0] = id;
1720   pdata[1] = size;
1721   memcpy (pdata + 2, data, size);
1722   if (extlen % 4)
1723     memset (pdata + 2 + size, 0, 4 - (extlen % 4));
1724
1725   return TRUE;
1726 }