gstbuffer: Unref memories before metas
[platform/upstream/gstreamer.git] / subprojects / gstreamer / tests / check / gst / gstbufferpool.c
1 /* GStreamer
2  * Copyright (C) 2014 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gstbufferpool.c: Unit test for GstBufferPool
5  *
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.
10  *
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.
15  *
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.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/check/gstcheck.h>
26
27 static GstBufferPool *
28 create_pool (guint size, guint min_buf, guint max_buf)
29 {
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");
33
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);
37
38   return pool;
39 }
40
41 static void
42 buffer_destroy_notify (gpointer ptr)
43 {
44   gint *counter = ptr;
45
46   GST_DEBUG ("buffer destroyed");
47
48   *counter += 1;
49 }
50
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. */
54 static void
55 buffer_track_destroy (GstBuffer * buf, gint * counter)
56 {
57   gst_mini_object_set_qdata (GST_MINI_OBJECT (buf),
58       g_quark_from_static_string ("TestTracker"),
59       counter, buffer_destroy_notify);
60 }
61
62 GST_START_TEST (test_new_buffer_from_empty_pool)
63 {
64   GstBufferPool *pool = create_pool (10, 0, 0);
65   GstBuffer *buf = NULL;
66
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");
70
71   gst_buffer_unref (buf);
72   gst_buffer_pool_set_active (pool, FALSE);
73   gst_object_unref (pool);
74 }
75
76 GST_END_TEST;
77
78
79 GST_START_TEST (test_buffer_is_recycled)
80 {
81   GstBufferPool *pool = create_pool (10, 0, 0);
82   GstBuffer *buf = NULL, *prev;
83   gint dcount = 0;
84
85   gst_buffer_pool_set_active (pool, TRUE);
86   gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
87   prev = buf;
88   buffer_track_destroy (buf, &dcount);
89   gst_buffer_unref (buf);
90
91   /* buffer should not have been freed, but have been recycled */
92   fail_unless (dcount == 0);
93
94   gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
95   fail_unless (buf == prev, "got a fresh buffer instead of previous");
96
97   gst_buffer_unref (buf);
98   gst_buffer_pool_set_active (pool, FALSE);
99   gst_object_unref (pool);
100
101   /* buffer should now be gone */
102   fail_unless (dcount == 1);
103 }
104
105 GST_END_TEST;
106
107
108 GST_START_TEST (test_buffer_out_of_order_reuse)
109 {
110   GstBufferPool *pool = create_pool (10, 0, 0);
111   GstBuffer *buf1 = NULL, *buf2 = NULL, *prev;
112   gint dcount1 = 0, dcount2 = 0;
113
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);
119   prev = buf2;
120   gst_buffer_unref (buf2);
121
122   /* buffer should not have been freed, but have been recycled */
123   fail_unless (dcount2 == 0);
124
125   gst_buffer_pool_acquire_buffer (pool, &buf2, NULL);
126   fail_unless (buf2 == prev, "got a fresh buffer instead of previous");
127
128   gst_buffer_unref (buf1);
129   gst_buffer_unref (buf2);
130   gst_buffer_pool_set_active (pool, FALSE);
131   gst_object_unref (pool);
132
133   fail_unless (dcount1 == 1);
134   fail_unless (dcount2 == 1);
135 }
136
137 GST_END_TEST;
138
139
140 GST_START_TEST (test_pool_config_buffer_size)
141 {
142   GstBufferPool *pool = create_pool (10, 0, 0);
143   GstBuffer *buf = NULL;
144
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);
148
149   gst_buffer_unref (buf);
150   gst_buffer_pool_set_active (pool, FALSE);
151   gst_object_unref (pool);
152 }
153
154 GST_END_TEST;
155
156
157 GST_START_TEST (test_inactive_pool_returns_flushing)
158 {
159   GstBufferPool *pool = create_pool (10, 0, 0);
160   GstFlowReturn ret;
161   GstBuffer *buf = NULL;
162
163   ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
164   ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
165
166   gst_object_unref (pool);
167 }
168
169 GST_END_TEST;
170
171 GST_START_TEST (test_buffer_modify_discard)
172 {
173
174   GstBufferPool *pool = create_pool (10, 0, 0);
175   GstBuffer *buf = NULL, *prev;
176   GstMemory *mem;
177   gint dcount = 0;
178
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);
186
187   /* buffer should've been destroyed instead of going back into pool */
188   fail_unless_equals_int (dcount, 1);
189
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);
195
196   /* buffer should've gone back into pool */
197   fail_unless_equals_int (dcount, 1);
198
199   gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
200   prev = buf;
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);
206
207   /* buffer should not have been destroyed and gone back into pool */
208   fail_unless_equals_int (dcount, 1);
209
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);
219
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);
223
224   gst_buffer_pool_set_active (pool, FALSE);
225   gst_object_unref (pool);
226 }
227
228 GST_END_TEST;
229
230 GST_START_TEST (test_pool_activation_and_config)
231 {
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");
235
236   /* unconfigured pool cannot be activated */
237   fail_if (gst_buffer_pool_set_active (pool, TRUE));
238
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));
242
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));
246
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));
252
253   gst_buffer_pool_set_active (pool, FALSE);
254   gst_object_unref (pool);
255   gst_caps_unref (caps);
256 }
257
258 GST_END_TEST;
259
260 GST_START_TEST (test_pool_config_validate)
261 {
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");
265
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));
270
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));
274
275   gst_caps_unref (caps);
276   gst_structure_free (config);
277   gst_object_unref (pool);
278 }
279
280 GST_END_TEST;
281
282 GST_START_TEST (test_flushing_pool_returns_flushing)
283 {
284   GstBufferPool *pool = create_pool (10, 0, 0);
285   GstFlowReturn ret;
286   GstBuffer *buf = NULL;
287
288   gst_buffer_pool_set_active (pool, TRUE);
289   gst_buffer_pool_set_flushing (pool, TRUE);
290
291   ret = gst_buffer_pool_acquire_buffer (pool, &buf, NULL);
292   ck_assert_int_eq (ret, GST_FLOW_FLUSHING);
293
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);
297
298   gst_buffer_unref (buf);
299   gst_object_unref (pool);
300 }
301
302 GST_END_TEST;
303
304 static gpointer
305 unref_buf (gpointer p)
306 {
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);
311   return NULL;
312 }
313
314 GST_START_TEST (test_no_deadlock_for_buffer_discard)
315 {
316   GstBufferPool *pool;
317   GstBuffer *buf1, *buf2;
318   GThread *thread;
319
320   pool = create_pool (1, 1, 1);
321   fail_unless (pool);
322   gst_buffer_pool_set_active (pool, TRUE);
323
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);
331
332   gst_buffer_unref (buf2);
333   g_thread_join (thread);
334   gst_object_unref (pool);
335 }
336
337 GST_END_TEST;
338
339 GST_START_TEST (test_parent_meta)
340 {
341   GstBufferPool *pool;
342   GstBuffer *buf1, *buf2, *buf3;
343   GstMemory *mem;
344   gint buf1_dcount = 0;
345   gint buf2_dcount = 0;
346
347   pool = create_pool (1, 0, 0);
348   fail_unless (pool);
349   gst_buffer_pool_set_active (pool, TRUE);
350
351   fail_unless (gst_buffer_pool_acquire_buffer (pool, &buf1,
352           NULL) == GST_FLOW_OK);
353   buffer_track_destroy (buf1, &buf1_dcount);
354
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);
362
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);
367
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);
372
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));
378
379   gst_buffer_unref (buf3);
380   fail_unless_equals_int (buf1_dcount, 0);
381   fail_unless_equals_int (buf2_dcount, 1);
382
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);
387 }
388
389 GST_END_TEST;
390
391 static Suite *
392 gst_buffer_pool_suite (void)
393 {
394   Suite *s = suite_create ("GstBufferPool");
395   TCase *tc_chain = tcase_create ("buffer_pool tests");
396
397   tcase_set_timeout (tc_chain, 0);
398
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);
411
412   return s;
413 }
414
415 GST_CHECK_MAIN (gst_buffer_pool);