rtpbuffer: Add gst_rtp_buffer_remove_extension_data()
authorJakub Adam <jakub.adam@ktknet.cz>
Mon, 24 May 2021 17:02:42 +0000 (19:02 +0200)
committerJakub Adam <jakub.adam@collabora.com>
Mon, 28 Jun 2021 17:28:41 +0000 (19:28 +0200)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1173>

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

index 7d72cd9..ce3949a 100644 (file)
@@ -896,6 +896,37 @@ gst_rtp_buffer_set_extension_data (GstRTPBuffer * rtp, guint16 bits,
 }
 
 /**
+ * gst_rtp_buffer_remove_extension_data:
+ * @rtp: the RTP packet
+ *
+ * Unsets the extension bit of the RTP buffer and removes the extension header
+ * and data.
+ *
+ * If the RTP buffer has no header extension data, the action has no effect.
+ * The RTP buffer must be mapped READWRITE only once and the underlying
+ * GstBuffer must be writable.
+ *
+ * Since: 1.20
+ */
+void
+gst_rtp_buffer_remove_extension_data (GstRTPBuffer * rtp)
+{
+  g_return_if_fail (gst_buffer_is_writable (rtp->buffer));
+  g_return_if_fail (rtp->map[0].flags & GST_MAP_WRITE);
+
+  if (rtp->data[1] != NULL) {
+    GstBuffer *buf = rtp->buffer;
+
+    ensure_buffers (rtp);
+
+    GST_RTP_HEADER_EXTENSION (rtp->data[0]) = FALSE;
+    gst_rtp_buffer_unmap (rtp);
+    gst_buffer_remove_memory (buf, 1);
+    gst_rtp_buffer_map (buf, GST_MAP_READWRITE, rtp);
+  }
+}
+
+/**
  * gst_rtp_buffer_get_ssrc:
  * @rtp: the RTP packet
  *
index ae3af14..cac8998 100644 (file)
@@ -136,6 +136,9 @@ GST_RTP_API
 gboolean        gst_rtp_buffer_set_extension_data    (GstRTPBuffer *rtp, guint16 bits, guint16 length);
 
 GST_RTP_API
+void            gst_rtp_buffer_remove_extension_data (GstRTPBuffer *rtp);
+
+GST_RTP_API
 guint32         gst_rtp_buffer_get_ssrc              (GstRTPBuffer *rtp);
 
 GST_RTP_API
index 39f1812..d5879ca 100644 (file)
@@ -2172,6 +2172,49 @@ GST_START_TEST (test_rtp_buffer_extlen_wraparound)
 
 GST_END_TEST;
 
+GST_START_TEST (test_rtp_buffer_remove_extension_data)
+{
+  GstBuffer *buf;
+  GstMapInfo info;
+  guint8 rtp_test_buffer[] = {
+    0x90, 0x7c, 0x18, 0xa6,     /* |V=2|P|X|CC|M|PT|sequence number| */
+    0x7a, 0x62, 0x17, 0x0f,     /* |timestamp| */
+    0x70, 0x23, 0x91, 0x38,     /* |synchronization source (SSRC) identifier| */
+    0xbe, 0xde, 0x00, 0x02,     /* |0xBE|0xDE|length=2| */
+    0x00, 0x00, 0x00, 0x00,     /* |0 (pad)|0 (pad)|0 (pad)|0 (pad)| */
+    0x00, 0x00, 0x00, 0x00,     /* |0 (pad)|0 (pad)|0 (pad)|0 (pad)| */
+    0xff, 0xff, 0xff, 0xff      /* |dummy payload| */
+  };
+
+  guint8 expected_result[] = {
+    0x80, 0x7c, 0x18, 0xa6,     /* |V=2|P|X|CC|M|PT|sequence number| */
+    0x7a, 0x62, 0x17, 0x0f,     /* |timestamp| */
+    0x70, 0x23, 0x91, 0x38,     /* |synchronization source (SSRC) identifier| */
+    0xff, 0xff, 0xff, 0xff      /* |dummy payload| */
+  };
+
+  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
+
+  buf = gst_buffer_new_and_alloc (sizeof (rtp_test_buffer));
+  gst_buffer_fill (buf, 0, rtp_test_buffer, sizeof (rtp_test_buffer));
+
+  fail_unless (gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtp));
+
+  gst_rtp_buffer_remove_extension_data (&rtp);
+  gst_rtp_buffer_unmap (&rtp);
+
+  gst_buffer_map (buf, &info, GST_MAP_READ);
+
+  fail_unless_equals_int (info.size, sizeof (expected_result));
+  fail_unless_equals_int
+      (memcmp (info.data, expected_result, sizeof (expected_result)), 0);
+
+  gst_buffer_unmap (buf, &info);
+  gst_buffer_unref (buf);
+}
+
+GST_END_TEST;
+
 static Suite *
 rtp_suite (void)
 {
@@ -2226,6 +2269,7 @@ rtp_suite (void)
 
   tcase_add_test (tc_chain, test_rtcp_compound_padding);
   tcase_add_test (tc_chain, test_rtp_buffer_extlen_wraparound);
+  tcase_add_test (tc_chain, test_rtp_buffer_remove_extension_data);
 
   return s;
 }