rtpbuffer: add gst_rtp_buffer_get_extension_onebyte_header_from_bytes
authorHavard Graff <havard@pexip.com>
Tue, 28 Jan 2020 17:17:47 +0000 (18:17 +0100)
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Tue, 4 Feb 2020 08:44:43 +0000 (08:44 +0000)
So that one can parse the GBytes returned by gst_rtp_buffer_get_extension_bytes

gst-libs/gst/rtp/gstrtpbuffer.c
gst-libs/gst/rtp/gstrtpbuffer.h

index 86d00a7..6a85524 100644 (file)
@@ -1314,44 +1314,23 @@ gst_rtp_buffer_ext_timestamp (guint64 * exttimestamp, guint32 timestamp)
   return result;
 }
 
-/**
- * gst_rtp_buffer_get_extension_onebyte_header:
- * @rtp: the RTP packet
- * @id: The ID of the header extension to be read (between 1 and 14).
- * @nth: Read the nth extension packet with the requested ID
- * @data: (out) (array length=size) (element-type guint8) (transfer none):
- *   location for data
- * @size: (out): the size of the data in bytes
- *
- * Parses RFC 5285 style header extensions with a one byte header. It will
- * return the nth extension with the requested id.
- *
- * Returns: TRUE if @buffer had the requested header extension
- */
 
-gboolean
-gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
-    guint nth, gpointer * data, guint * size)
+static gboolean
+_get_extension_onebyte_header (const guint8 * pdata, guint len,
+    guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
 {
-  guint16 bits;
-  guint8 *pdata;
-  guint wordlen;
   gulong offset = 0;
   guint count = 0;
 
   g_return_val_if_fail (id > 0 && id < 15, FALSE);
 
-  if (!gst_rtp_buffer_get_extension_data (rtp, &bits, (gpointer) & pdata,
-          &wordlen))
-    return FALSE;
-
-  if (bits != 0xBEDE)
+  if (bit_pattern != 0xBEDE)
     return FALSE;
 
   for (;;) {
     guint8 read_id, read_len;
 
-    if (offset + 1 >= wordlen * 4)
+    if (offset + 1 >= len)
       break;
 
     read_id = GST_READ_UINT8 (pdata + offset) >> 4;
@@ -1367,14 +1346,14 @@ gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
       break;
 
     /* Ignore extension headers where the size does not fit */
-    if (offset + read_len > wordlen * 4)
+    if (offset + read_len > len)
       break;
 
     /* If we have the right one */
     if (id == read_id) {
       if (nth == count) {
         if (data)
-          *data = pdata + offset;
+          *data = (gpointer) & pdata[offset];
         if (size)
           *size = read_len;
 
@@ -1385,13 +1364,76 @@ gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
     }
     offset += read_len;
 
-    if (offset >= wordlen * 4)
+    if (offset >= len)
       break;
   }
 
   return FALSE;
 }
 
+
+/**
+ * gst_rtp_buffer_get_extension_onebyte_header_from_bytes:
+ * @bytes: #GBytes
+ * @bit_pattern: The bit-pattern. Anything but 0xBEDE is rejected.
+ * @id: The ID of the header extension to be read (between 1 and 14).
+ * @nth: Read the nth extension packet with the requested ID
+ * @data: (out) (array length=size) (element-type guint8) (transfer none):
+ *   location for data
+ * @size: (out): the size of the data in bytes
+ *
+ * Similar to gst_rtp_buffer_get_extension_onebyte_header, but working
+ * on the #GBytes you get from gst_rtp_buffer_get_extension_bytes.
+ * Parses RFC 5285 style header extensions with a one byte header. It will
+ * return the nth extension with the requested id.
+ *
+ * Returns: TRUE if @bytes had the requested header extension
+ *
+ * Since: 1.18
+ */
+gboolean
+gst_rtp_buffer_get_extension_onebyte_header_from_bytes (GBytes * bytes,
+    guint16 bit_pattern, guint8 id, guint nth, gpointer * data, guint * size)
+{
+  const guint8 *pdata = g_bytes_get_data (bytes, NULL);
+  gsize len = g_bytes_get_size (bytes);
+  return _get_extension_onebyte_header (pdata, len, bit_pattern, id, nth, data,
+      size);
+}
+
+
+/**
+ * gst_rtp_buffer_get_extension_onebyte_header:
+ * @rtp: the RTP packet
+ * @id: The ID of the header extension to be read (between 1 and 14).
+ * @nth: Read the nth extension packet with the requested ID
+ * @data: (out) (array length=size) (element-type guint8) (transfer none):
+ *   location for data
+ * @size: (out): the size of the data in bytes
+ *
+ * Parses RFC 5285 style header extensions with a one byte header. It will
+ * return the nth extension with the requested id.
+ *
+ * Returns: TRUE if @buffer had the requested header extension
+ */
+
+gboolean
+gst_rtp_buffer_get_extension_onebyte_header (GstRTPBuffer * rtp, guint8 id,
+    guint nth, gpointer * data, guint * size)
+{
+  guint16 bit_pattern;
+  guint8 *pdata;
+  guint wordlen;
+
+  if (!gst_rtp_buffer_get_extension_data (rtp, &bit_pattern, (gpointer) & pdata,
+          &wordlen))
+    return FALSE;
+
+  return _get_extension_onebyte_header (pdata, wordlen * 4, bit_pattern, id,
+      nth, data, size);
+}
+
+
 /**
  * gst_rtp_buffer_get_extension_twobytes_header:
  * @rtp: the RTP packet
index 42e3791..ae3af14 100644 (file)
@@ -228,6 +228,14 @@ gboolean       gst_rtp_buffer_add_extension_twobytes_header (GstRTPBuffer *rtp,
                                                              gconstpointer data,
                                                              guint size);
 
+GST_RTP_API
+gboolean gst_rtp_buffer_get_extension_onebyte_header_from_bytes (GBytes * bytes,
+                                                                 guint16 bit_pattern,
+                                                                 guint8 id,
+                                                                 guint nth,
+                                                                 gpointer * data,
+                                                                 guint * size);
+
 /**
  * GstRTPBufferFlags:
  * @GST_RTP_BUFFER_FLAG_RETRANSMISSION: The #GstBuffer was once wrapped