2014-01-30 07:11:12 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2014 Stefan Sauer <ensonic@users.sf.net>
|
|
|
|
*
|
|
|
|
* gstbufferpool.c: Unit test for GstBufferPool
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2017-11-24 12:41:20 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2014-01-30 07:11:12 +00:00
|
|
|
|
|
|
|
#include <gst/check/gstcheck.h>
|
|
|
|
|
|
|
|
static GstBufferPool *
|
|
|
|
create_pool (guint size, guint min_buf, guint max_buf)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = gst_buffer_pool_new ();
|
|
|
|
GstStructure *conf = gst_buffer_pool_get_config (pool);
|
2014-05-06 19:35:14 +00:00
|
|
|
GstCaps *caps = gst_caps_new_empty_simple ("test/data");
|
2014-01-30 07:11:12 +00:00
|
|
|
|
2014-05-06 19:35:14 +00:00
|
|
|
gst_buffer_pool_config_set_params (conf, caps, size, min_buf, max_buf);
|
2014-01-30 07:11:12 +00:00
|
|
|
gst_buffer_pool_set_config (pool, conf);
|
2014-05-06 19:35:14 +00:00
|
|
|
gst_caps_unref (caps);
|
2014-01-30 07:11:12 +00:00
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
static void
|
|
|
|
buffer_destroy_notify (gpointer ptr)
|
|
|
|
{
|
|
|
|
gint *counter = ptr;
|
|
|
|
|
|
|
|
GST_DEBUG ("buffer destroyed");
|
|
|
|
|
|
|
|
*counter += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Track when a buffer is destroyed. The counter will be increased if the
|
|
|
|
* buffer is finalized (but not if it was re-surrected in dispose and put
|
|
|
|
* back into the buffer pool. */
|
|
|
|
static void
|
|
|
|
buffer_track_destroy (GstBuffer * buf, gint * counter)
|
|
|
|
{
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (buf),
|
|
|
|
g_quark_from_static_string ("TestTracker"),
|
|
|
|
counter, buffer_destroy_notify);
|
|
|
|
}
|
|
|
|
|
2014-01-30 07:11:12 +00:00
|
|
|
GST_START_TEST (test_new_buffer_from_empty_pool)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
fail_if (buf == NULL, "acquiring buffer returned NULL");
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-01-31 07:08:37 +00:00
|
|
|
|
2014-01-30 07:11:12 +00:00
|
|
|
GST_START_TEST (test_buffer_is_recycled)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstBuffer *buf = NULL, *prev;
|
2016-04-10 19:04:07 +00:00
|
|
|
gint dcount = 0;
|
2014-01-30 07:11:12 +00:00
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
prev = buf;
|
2016-04-10 19:04:07 +00:00
|
|
|
buffer_track_destroy (buf, &dcount);
|
2014-01-30 07:11:12 +00:00
|
|
|
gst_buffer_unref (buf);
|
2014-01-31 07:08:37 +00:00
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
/* buffer should not have been freed, but have been recycled */
|
|
|
|
fail_unless (dcount == 0);
|
|
|
|
|
2014-01-30 07:11:12 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
fail_unless (buf == prev, "got a fresh buffer instead of previous");
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
2016-04-10 19:04:07 +00:00
|
|
|
|
|
|
|
/* buffer should now be gone */
|
|
|
|
fail_unless (dcount == 1);
|
2014-01-30 07:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-01-31 07:08:37 +00:00
|
|
|
|
|
|
|
GST_START_TEST (test_buffer_out_of_order_reuse)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstBuffer *buf1 = NULL, *buf2 = NULL, *prev;
|
2016-04-10 19:04:07 +00:00
|
|
|
gint dcount1 = 0, dcount2 = 0;
|
2014-01-31 07:08:37 +00:00
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf1, NULL);
|
2016-04-10 19:04:07 +00:00
|
|
|
buffer_track_destroy (buf1, &dcount1);
|
2014-01-31 07:08:37 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf2, NULL);
|
2016-04-10 19:04:07 +00:00
|
|
|
buffer_track_destroy (buf2, &dcount2);
|
2014-01-31 07:08:37 +00:00
|
|
|
prev = buf2;
|
|
|
|
gst_buffer_unref (buf2);
|
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
/* buffer should not have been freed, but have been recycled */
|
|
|
|
fail_unless (dcount2 == 0);
|
|
|
|
|
2014-01-31 07:08:37 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf2, NULL);
|
|
|
|
fail_unless (buf2 == prev, "got a fresh buffer instead of previous");
|
|
|
|
|
|
|
|
gst_buffer_unref (buf1);
|
|
|
|
gst_buffer_unref (buf2);
|
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
2016-04-10 19:04:07 +00:00
|
|
|
|
|
|
|
fail_unless (dcount1 == 1);
|
|
|
|
fail_unless (dcount2 == 1);
|
2014-01-31 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
|
|
|
|
GST_START_TEST (test_pool_config_buffer_size)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
ck_assert_int_eq (gst_buffer_get_size (buf), 10);
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
|
|
|
|
GST_START_TEST (test_inactive_pool_returns_flushing)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
|
|
|
|
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-02-27 14:14:59 +00:00
|
|
|
GST_START_TEST (test_buffer_modify_discard)
|
|
|
|
{
|
|
|
|
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstBuffer *buf = NULL, *prev;
|
|
|
|
GstMemory *mem;
|
2016-04-10 19:04:07 +00:00
|
|
|
gint dcount = 0;
|
2014-02-27 14:14:59 +00:00
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
2016-04-10 19:04:07 +00:00
|
|
|
fail_unless (buf != NULL);
|
|
|
|
buffer_track_destroy (buf, &dcount);
|
2014-02-27 14:14:59 +00:00
|
|
|
/* remove all memory, pool should not reuse this buffer */
|
|
|
|
gst_buffer_remove_all_memory (buf);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
/* buffer should've been destroyed instead of going back into pool */
|
|
|
|
fail_unless_equals_int (dcount, 1);
|
|
|
|
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
2016-04-10 19:04:07 +00:00
|
|
|
buffer_track_destroy (buf, &dcount);
|
2018-03-22 00:02:50 +00:00
|
|
|
/* do resize, as we didn't modify the memory, pool should reuse this buffer */
|
2019-06-29 13:22:05 +00:00
|
|
|
gst_buffer_resize (buf, 8, 2);
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2018-03-22 00:02:50 +00:00
|
|
|
/* buffer should've gone back into pool */
|
|
|
|
fail_unless_equals_int (dcount, 1);
|
2016-04-10 19:04:07 +00:00
|
|
|
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
prev = buf;
|
2018-03-22 00:02:50 +00:00
|
|
|
fail_unless (buf == prev, "got a fresh buffer instead of previous");
|
2014-02-27 14:14:59 +00:00
|
|
|
/* keep ref to memory, not exclusive so pool should reuse this buffer */
|
|
|
|
mem = gst_buffer_get_memory (buf, 0);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_memory_unref (mem);
|
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
/* buffer should not have been destroyed and gone back into pool */
|
2018-03-22 00:02:50 +00:00
|
|
|
fail_unless_equals_int (dcount, 1);
|
2016-04-10 19:04:07 +00:00
|
|
|
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
fail_unless (buf == prev, "got a fresh buffer instead of previous");
|
2016-04-10 19:04:07 +00:00
|
|
|
/* we're already did track_destroy on this buf, so no need to do it again */
|
2014-02-27 14:14:59 +00:00
|
|
|
mem = gst_buffer_get_memory (buf, 0);
|
2014-02-27 15:40:34 +00:00
|
|
|
/* exclusive lock so pool should not reuse this buffer */
|
|
|
|
gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_unref (buf);
|
2014-02-27 15:40:34 +00:00
|
|
|
gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
gst_memory_unref (mem);
|
2014-02-27 14:14:59 +00:00
|
|
|
|
2016-04-10 19:04:07 +00:00
|
|
|
/* buffer should have been destroyed and not gone back into pool because
|
|
|
|
* of the exclusive lock */
|
2018-03-22 00:02:50 +00:00
|
|
|
fail_unless_equals_int (dcount, 2);
|
2016-04-10 19:04:07 +00:00
|
|
|
|
2014-02-27 14:14:59 +00:00
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
2014-01-31 07:08:37 +00:00
|
|
|
|
2014-05-06 19:35:14 +00:00
|
|
|
GST_START_TEST (test_pool_activation_and_config)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = gst_buffer_pool_new ();
|
|
|
|
GstStructure *config = gst_buffer_pool_get_config (pool);
|
|
|
|
GstCaps *caps = gst_caps_new_empty_simple ("test/data");
|
|
|
|
|
|
|
|
/* unconfigured pool cannot be activated */
|
|
|
|
fail_if (gst_buffer_pool_set_active (pool, TRUE));
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, 10, 10, 0);
|
|
|
|
fail_unless (gst_buffer_pool_set_config (pool, config));
|
|
|
|
fail_unless (gst_buffer_pool_set_active (pool, TRUE));
|
|
|
|
|
|
|
|
/* setting the same config on an active pool is ok */
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
fail_unless (gst_buffer_pool_set_config (pool, config));
|
|
|
|
|
2015-03-01 18:15:40 +00:00
|
|
|
/* setting a different config on active pool should fail */
|
2014-05-06 19:35:14 +00:00
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, 12, 10, 0);
|
|
|
|
fail_if (gst_buffer_pool_set_config (pool, config));
|
2015-03-01 18:15:40 +00:00
|
|
|
fail_unless (gst_buffer_pool_is_active (pool));
|
2014-05-06 19:35:14 +00:00
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, FALSE);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-05-08 16:47:43 +00:00
|
|
|
GST_START_TEST (test_pool_config_validate)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (5, 4, 30);
|
|
|
|
GstStructure *config = gst_buffer_pool_get_config (pool);
|
|
|
|
GstCaps *caps = gst_caps_new_empty_simple ("test/data");
|
|
|
|
|
|
|
|
fail_unless (gst_buffer_pool_config_validate_params (config, caps, 5, 4, 0));
|
|
|
|
fail_unless (gst_buffer_pool_config_validate_params (config, caps, 5, 2, 0));
|
2014-12-16 15:13:03 +00:00
|
|
|
fail_unless (gst_buffer_pool_config_validate_params (config, caps, 4, 4, 0));
|
2014-05-08 16:47:43 +00:00
|
|
|
fail_if (gst_buffer_pool_config_validate_params (config, caps, 5, 6, 0));
|
|
|
|
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = gst_caps_new_empty_simple ("test/data2");
|
|
|
|
fail_if (gst_buffer_pool_config_validate_params (config, caps, 5, 4, 0));
|
|
|
|
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
gst_structure_free (config);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-05-23 19:26:59 +00:00
|
|
|
GST_START_TEST (test_flushing_pool_returns_flushing)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = create_pool (10, 0, 0);
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
gst_buffer_pool_set_flushing (pool, TRUE);
|
|
|
|
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
|
|
|
|
|
|
|
|
gst_buffer_pool_set_flushing (pool, FALSE);
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
|
|
|
|
ck_assert_int_eq (ret, GST_FLOW_OK);
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2020-05-11 10:21:48 +00:00
|
|
|
static gpointer
|
|
|
|
unref_buf (gpointer p)
|
|
|
|
{
|
|
|
|
GstBuffer *buf = (GstBuffer *) p;
|
|
|
|
/* remove all memory, pool should not reuse this buffer */
|
|
|
|
gst_buffer_remove_all_memory (buf);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_START_TEST (test_no_deadlock_for_buffer_discard)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool;
|
|
|
|
GstBuffer *buf1, *buf2;
|
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
pool = create_pool (1, 1, 1);
|
|
|
|
fail_unless (pool);
|
|
|
|
gst_buffer_pool_set_active (pool, TRUE);
|
|
|
|
|
|
|
|
fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf1,
|
|
|
|
NULL) == GST_FLOW_OK);
|
|
|
|
thread = g_thread_new (NULL, unref_buf, buf1);
|
|
|
|
fail_unless (thread);
|
|
|
|
/* we will be blocked here until buf1 unrefed */
|
|
|
|
fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf2,
|
|
|
|
NULL) == GST_FLOW_OK);
|
|
|
|
|
|
|
|
gst_buffer_unref (buf2);
|
|
|
|
g_thread_join (thread);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2014-01-30 07:11:12 +00:00
|
|
|
static Suite *
|
|
|
|
gst_buffer_pool_suite (void)
|
|
|
|
{
|
|
|
|
Suite *s = suite_create ("GstBufferPool");
|
|
|
|
TCase *tc_chain = tcase_create ("buffer_pool tests");
|
|
|
|
|
|
|
|
tcase_set_timeout (tc_chain, 0);
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_chain);
|
|
|
|
tcase_add_test (tc_chain, test_new_buffer_from_empty_pool);
|
|
|
|
tcase_add_test (tc_chain, test_buffer_is_recycled);
|
2014-01-31 07:08:37 +00:00
|
|
|
tcase_add_test (tc_chain, test_buffer_out_of_order_reuse);
|
|
|
|
tcase_add_test (tc_chain, test_pool_config_buffer_size);
|
|
|
|
tcase_add_test (tc_chain, test_inactive_pool_returns_flushing);
|
2014-02-27 14:14:59 +00:00
|
|
|
tcase_add_test (tc_chain, test_buffer_modify_discard);
|
2014-05-06 19:35:14 +00:00
|
|
|
tcase_add_test (tc_chain, test_pool_activation_and_config);
|
2014-05-08 16:47:43 +00:00
|
|
|
tcase_add_test (tc_chain, test_pool_config_validate);
|
2014-05-23 19:26:59 +00:00
|
|
|
tcase_add_test (tc_chain, test_flushing_pool_returns_flushing);
|
2020-05-11 10:21:48 +00:00
|
|
|
tcase_add_test (tc_chain, test_no_deadlock_for_buffer_discard);
|
2014-01-30 07:11:12 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_CHECK_MAIN (gst_buffer_pool);
|