gst/buffer: add a new function for wrapping GBytes

One restriction on the GBytes is that the data cannot be NULL as this is
explicitly forbidden by GstMemory.

Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/318
This commit is contained in:
Matthew Waters 2018-09-27 19:13:35 +10:00
parent 874ad5faca
commit ddfcc28c8b
4 changed files with 48 additions and 0 deletions

View file

@ -193,6 +193,7 @@ GST_BUFFER_IS_DISCONT
gst_buffer_new gst_buffer_new
gst_buffer_new_allocate gst_buffer_new_allocate
gst_buffer_new_wrapped gst_buffer_new_wrapped
gst_buffer_new_wrapped_bytes
gst_buffer_new_wrapped_full gst_buffer_new_wrapped_full
gst_buffer_ref gst_buffer_ref

View file

@ -978,6 +978,33 @@ gst_buffer_new_wrapped (gpointer data, gsize size)
return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free); return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
} }
/**
* gst_buffer_new_wrapped_bytes:
* @bytes: (transfer none): a #GBytes to wrap
*
* Creates a new #GstBuffer that wraps the given @bytes. The data inside
* @bytes cannot be %NULL and the resulting buffer will be marked as read only.
*
* MT safe.
*
* Returns: (transfer full): a new #GstBuffer wrapping @bytes
*
* Since: 1.16
*/
GstBuffer *
gst_buffer_new_wrapped_bytes (GBytes * bytes)
{
guint8 *bytes_data;
gsize size;
g_return_val_if_fail (bytes != NULL, NULL);
bytes_data = (guint8 *) g_bytes_get_data (bytes, &size);
g_return_val_if_fail (bytes_data != NULL, NULL);
return gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, bytes_data,
size, 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
}
/** /**
* gst_buffer_n_memory: * gst_buffer_n_memory:
* @buffer: a #GstBuffer. * @buffer: a #GstBuffer.

View file

@ -297,6 +297,8 @@ GstBuffer * gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
GDestroyNotify notify); GDestroyNotify notify);
GST_API GST_API
GstBuffer * gst_buffer_new_wrapped (gpointer data, gsize size); GstBuffer * gst_buffer_new_wrapped (gpointer data, gsize size);
GST_API
GstBuffer * gst_buffer_new_wrapped_bytes (GBytes * bytes);
/* memory blocks */ /* memory blocks */

View file

@ -905,6 +905,23 @@ GST_START_TEST (test_writable_memory)
GST_END_TEST; GST_END_TEST;
GST_START_TEST (test_wrapped_bytes)
{
GBytes *bytes = g_bytes_new_static (ro_memory, sizeof (ro_memory));
GstBuffer *buf;
GstMemory *mem;
buf = gst_buffer_new_wrapped_bytes (bytes);
/* the memory should not be writable */
mem = gst_buffer_peek_memory (buf, 0);
fail_unless (GST_MEMORY_IS_READONLY (mem));
gst_buffer_unref (buf);
}
GST_END_TEST;
static Suite * static Suite *
gst_buffer_suite (void) gst_buffer_suite (void)
{ {
@ -929,6 +946,7 @@ gst_buffer_suite (void)
tcase_add_test (tc_chain, test_fill); tcase_add_test (tc_chain, test_fill);
tcase_add_test (tc_chain, test_parent_buffer_meta); tcase_add_test (tc_chain, test_parent_buffer_meta);
tcase_add_test (tc_chain, test_writable_memory); tcase_add_test (tc_chain, test_writable_memory);
tcase_add_test (tc_chain, test_wrapped_bytes);
return s; return s;
} }