2 * Copyright (C) 2014 Stefan Sauer <ensonic@users.sf.net>
4 * gstbufferpool.c: Unit test for GstBufferPool
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include <gst/check/gstcheck.h>
27 static GstBufferPool *
28 create_pool (guint size, guint min_buf, guint max_buf)
30 GstBufferPool *pool = gst_buffer_pool_new ();
31 GstStructure *conf = gst_buffer_pool_get_config (pool);
32 GstCaps *caps = gst_caps_new_empty_simple ("test/data");
34 gst_buffer_pool_config_set_params (conf, caps, size, min_buf, max_buf);
35 gst_buffer_pool_set_config (pool, conf);
36 gst_caps_unref (caps);
42 buffer_destroy_notify (gpointer ptr)
46 GST_DEBUG ("buffer destroyed");
51 /* Track when a buffer is destroyed. The counter will be increased if the
52 * buffer is finalized (but not if it was re-surrected in dispose and put
53 * back into the buffer pool. */
55 buffer_track_destroy (GstBuffer * buf, gint * counter)
57 gst_mini_object_set_qdata (GST_MINI_OBJECT (buf),
58 g_quark_from_static_string ("TestTracker"),
59 counter, buffer_destroy_notify);
62 GST_START_TEST (test_new_buffer_from_empty_pool)
64 GstBufferPool *pool = create_pool (10, 0, 0);
65 GstBuffer *buf = NULL;
67 gst_buffer_pool_set_active (pool, TRUE);
68 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
69 fail_if (buf == NULL, "acquiring buffer returned NULL");
71 gst_buffer_unref (buf);
72 gst_buffer_pool_set_active (pool, FALSE);
73 gst_object_unref (pool);
79 GST_START_TEST (test_buffer_is_recycled)
81 GstBufferPool *pool = create_pool (10, 0, 0);
82 GstBuffer *buf = NULL, *prev;
85 gst_buffer_pool_set_active (pool, TRUE);
86 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
88 buffer_track_destroy (buf, &dcount);
89 gst_buffer_unref (buf);
91 /* buffer should not have been freed, but have been recycled */
92 fail_unless (dcount == 0);
94 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
95 fail_unless (buf == prev, "got a fresh buffer instead of previous");
97 gst_buffer_unref (buf);
98 gst_buffer_pool_set_active (pool, FALSE);
99 gst_object_unref (pool);
101 /* buffer should now be gone */
102 fail_unless (dcount == 1);
108 GST_START_TEST (test_buffer_out_of_order_reuse)
110 GstBufferPool *pool = create_pool (10, 0, 0);
111 GstBuffer *buf1 = NULL, *buf2 = NULL, *prev;
112 gint dcount1 = 0, dcount2 = 0;
114 gst_buffer_pool_set_active (pool, TRUE);
115 gst_buffer_pool_acquire_buffer (pool, &buf1, NULL);
116 buffer_track_destroy (buf1, &dcount1);
117 gst_buffer_pool_acquire_buffer (pool, &buf2, NULL);
118 buffer_track_destroy (buf2, &dcount2);
120 gst_buffer_unref (buf2);
122 /* buffer should not have been freed, but have been recycled */
123 fail_unless (dcount2 == 0);
125 gst_buffer_pool_acquire_buffer (pool, &buf2, NULL);
126 fail_unless (buf2 == prev, "got a fresh buffer instead of previous");
128 gst_buffer_unref (buf1);
129 gst_buffer_unref (buf2);
130 gst_buffer_pool_set_active (pool, FALSE);
131 gst_object_unref (pool);
133 fail_unless (dcount1 == 1);
134 fail_unless (dcount2 == 1);
140 GST_START_TEST (test_pool_config_buffer_size)
142 GstBufferPool *pool = create_pool (10, 0, 0);
143 GstBuffer *buf = NULL;
145 gst_buffer_pool_set_active (pool, TRUE);
146 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
147 ck_assert_int_eq (gst_buffer_get_size (buf), 10);
149 gst_buffer_unref (buf);
150 gst_buffer_pool_set_active (pool, FALSE);
151 gst_object_unref (pool);
157 GST_START_TEST (test_inactive_pool_returns_flushing)
159 GstBufferPool *pool = create_pool (10, 0, 0);
161 GstBuffer *buf = NULL;
163 ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
164 ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
166 gst_object_unref (pool);
171 GST_START_TEST (test_buffer_modify_discard)
174 GstBufferPool *pool = create_pool (10, 0, 0);
175 GstBuffer *buf = NULL, *prev;
179 gst_buffer_pool_set_active (pool, TRUE);
180 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
181 fail_unless (buf != NULL);
182 buffer_track_destroy (buf, &dcount);
183 /* remove all memory, pool should not reuse this buffer */
184 gst_buffer_remove_all_memory (buf);
185 gst_buffer_unref (buf);
187 /* buffer should've been destroyed instead of going back into pool */
188 fail_unless_equals_int (dcount, 1);
190 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
191 buffer_track_destroy (buf, &dcount);
192 /* do resize, as we didn't modify the memory, pool should reuse this buffer */
193 gst_buffer_resize (buf, 8, 2);
194 gst_buffer_unref (buf);
196 /* buffer should've gone back into pool */
197 fail_unless_equals_int (dcount, 1);
199 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
201 fail_unless (buf == prev, "got a fresh buffer instead of previous");
202 /* keep ref to memory, not exclusive so pool should reuse this buffer */
203 mem = gst_buffer_get_memory (buf, 0);
204 gst_buffer_unref (buf);
205 gst_memory_unref (mem);
207 /* buffer should not have been destroyed and gone back into pool */
208 fail_unless_equals_int (dcount, 1);
210 gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
211 fail_unless (buf == prev, "got a fresh buffer instead of previous");
212 /* we're already did track_destroy on this buf, so no need to do it again */
213 mem = gst_buffer_get_memory (buf, 0);
214 /* exclusive lock so pool should not reuse this buffer */
215 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
216 gst_buffer_unref (buf);
217 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
218 gst_memory_unref (mem);
220 /* buffer should have been destroyed and not gone back into pool because
221 * of the exclusive lock */
222 fail_unless_equals_int (dcount, 2);
224 gst_buffer_pool_set_active (pool, FALSE);
225 gst_object_unref (pool);
230 GST_START_TEST (test_pool_activation_and_config)
232 GstBufferPool *pool = gst_buffer_pool_new ();
233 GstStructure *config = gst_buffer_pool_get_config (pool);
234 GstCaps *caps = gst_caps_new_empty_simple ("test/data");
236 /* unconfigured pool cannot be activated */
237 fail_if (gst_buffer_pool_set_active (pool, TRUE));
239 gst_buffer_pool_config_set_params (config, caps, 10, 10, 0);
240 fail_unless (gst_buffer_pool_set_config (pool, config));
241 fail_unless (gst_buffer_pool_set_active (pool, TRUE));
243 /* setting the same config on an active pool is ok */
244 config = gst_buffer_pool_get_config (pool);
245 fail_unless (gst_buffer_pool_set_config (pool, config));
247 /* setting a different config on active pool should fail */
248 config = gst_buffer_pool_get_config (pool);
249 gst_buffer_pool_config_set_params (config, caps, 12, 10, 0);
250 fail_if (gst_buffer_pool_set_config (pool, config));
251 fail_unless (gst_buffer_pool_is_active (pool));
253 gst_buffer_pool_set_active (pool, FALSE);
254 gst_object_unref (pool);
255 gst_caps_unref (caps);
260 GST_START_TEST (test_pool_config_validate)
262 GstBufferPool *pool = create_pool (5, 4, 30);
263 GstStructure *config = gst_buffer_pool_get_config (pool);
264 GstCaps *caps = gst_caps_new_empty_simple ("test/data");
266 fail_unless (gst_buffer_pool_config_validate_params (config, caps, 5, 4, 0));
267 fail_unless (gst_buffer_pool_config_validate_params (config, caps, 5, 2, 0));
268 fail_unless (gst_buffer_pool_config_validate_params (config, caps, 4, 4, 0));
269 fail_if (gst_buffer_pool_config_validate_params (config, caps, 5, 6, 0));
271 gst_caps_unref (caps);
272 caps = gst_caps_new_empty_simple ("test/data2");
273 fail_if (gst_buffer_pool_config_validate_params (config, caps, 5, 4, 0));
275 gst_caps_unref (caps);
276 gst_structure_free (config);
277 gst_object_unref (pool);
282 GST_START_TEST (test_flushing_pool_returns_flushing)
284 GstBufferPool *pool = create_pool (10, 0, 0);
286 GstBuffer *buf = NULL;
288 gst_buffer_pool_set_active (pool, TRUE);
289 gst_buffer_pool_set_flushing (pool, TRUE);
291 ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
292 ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
294 gst_buffer_pool_set_flushing (pool, FALSE);
295 ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
296 ck_assert_int_eq (ret, GST_FLOW_OK);
298 gst_buffer_unref (buf);
299 gst_object_unref (pool);
305 unref_buf (gpointer p)
307 GstBuffer *buf = (GstBuffer *) p;
308 /* remove all memory, pool should not reuse this buffer */
309 gst_buffer_remove_all_memory (buf);
310 gst_buffer_unref (buf);
314 GST_START_TEST (test_no_deadlock_for_buffer_discard)
317 GstBuffer *buf1, *buf2;
320 pool = create_pool (1, 1, 1);
322 gst_buffer_pool_set_active (pool, TRUE);
324 fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf1,
325 NULL) == GST_FLOW_OK);
326 thread = g_thread_new (NULL, unref_buf, buf1);
327 fail_unless (thread);
328 /* we will be blocked here until buf1 unrefed */
329 fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf2,
330 NULL) == GST_FLOW_OK);
332 gst_buffer_unref (buf2);
333 g_thread_join (thread);
334 gst_object_unref (pool);
339 GST_START_TEST (test_parent_meta)
342 GstBuffer *buf1, *buf2, *buf3;
344 gint buf1_dcount = 0;
345 gint buf2_dcount = 0;
347 pool = create_pool (1, 0, 0);
349 gst_buffer_pool_set_active (pool, TRUE);
351 fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf1,
352 NULL) == GST_FLOW_OK);
353 buffer_track_destroy (buf1, &buf1_dcount);
355 /* Create a 2nd buffer reffing the same memory. Set parent meta to make sure
356 * buf1 does not return to pool until buf2 is destroyed. */
357 mem = gst_buffer_get_memory (buf1, 0);
358 buf2 = gst_buffer_new ();
359 gst_buffer_append_memory (buf2, mem);
360 gst_buffer_add_parent_buffer_meta (buf2, buf1);
361 buffer_track_destroy (buf2, &buf2_dcount);
363 /* buf1 is still reffed by the meta */
364 gst_buffer_unref (buf1);
365 fail_unless_equals_int (buf1_dcount, 0);
366 fail_unless_equals_int (buf2_dcount, 0);
368 /* buf2 gets destroyed and buf1 returns to pool */
369 gst_buffer_unref (buf2);
370 fail_unless_equals_int (buf1_dcount, 0);
371 fail_unless_equals_int (buf2_dcount, 1);
373 /* buf1 should be recycled with the same memory */
374 fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf3,
375 NULL) == GST_FLOW_OK);
376 fail_unless_equals_pointer (buf1, buf3);
377 fail_unless_equals_pointer (mem, gst_buffer_peek_memory (buf3, 0));
379 gst_buffer_unref (buf3);
380 fail_unless_equals_int (buf1_dcount, 0);
381 fail_unless_equals_int (buf2_dcount, 1);
383 gst_buffer_pool_set_active (pool, FALSE);
384 gst_object_unref (pool);
385 fail_unless_equals_int (buf1_dcount, 1);
386 fail_unless_equals_int (buf2_dcount, 1);
392 gst_buffer_pool_suite (void)
394 Suite *s = suite_create ("GstBufferPool");
395 TCase *tc_chain = tcase_create ("buffer_pool tests");
397 tcase_set_timeout (tc_chain, 0);
399 suite_add_tcase (s, tc_chain);
400 tcase_add_test (tc_chain, test_new_buffer_from_empty_pool);
401 tcase_add_test (tc_chain, test_buffer_is_recycled);
402 tcase_add_test (tc_chain, test_buffer_out_of_order_reuse);
403 tcase_add_test (tc_chain, test_pool_config_buffer_size);
404 tcase_add_test (tc_chain, test_inactive_pool_returns_flushing);
405 tcase_add_test (tc_chain, test_buffer_modify_discard);
406 tcase_add_test (tc_chain, test_pool_activation_and_config);
407 tcase_add_test (tc_chain, test_pool_config_validate);
408 tcase_add_test (tc_chain, test_flushing_pool_returns_flushing);
409 tcase_add_test (tc_chain, test_no_deadlock_for_buffer_discard);
410 tcase_add_test (tc_chain, test_parent_meta);
415 GST_CHECK_MAIN (gst_buffer_pool);