3 * Copyright (C) 2009, Axis Communications AB, LUND, SWEDEN
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <gst/check/gstcheck.h>
22 #include <gst/app/gstappsink.h>
26 static GstPad *mysrcpad;
28 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
31 GST_STATIC_CAPS ("application/x-gst-check")
39 GST_DEBUG ("setup_appsink");
40 appsink = gst_check_setup_element ("appsink");
41 mysrcpad = gst_check_setup_src_pad (appsink, &srctemplate, NULL);
47 cleanup_appsink (GstElement * appsink)
49 GST_DEBUG ("cleanup_appsink");
51 gst_check_teardown_src_pad (appsink);
52 gst_check_teardown_element (appsink);
55 /* This function does an operation to it's indata argument and returns it.
56 * The exact operation performed doesn't matter. Currently it multiplies with
57 * two, but it could do anything. The idea is to use the function to verify
58 * that the code calling it gets run. */
60 operate_on_data (gint indata)
66 callback_function (GstAppSink * appsink, gpointer callback_data)
68 global_testdata = operate_on_data (*((gint *) callback_data));
74 notify_function (gpointer callback_data)
76 global_testdata = operate_on_data (*((gint *) callback_data));
79 GST_START_TEST (test_non_clients)
85 sink = setup_appsink ();
87 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
89 caps = gst_caps_from_string ("application/x-gst-check");
90 buffer = gst_buffer_new_and_alloc (4);
91 gst_buffer_set_caps (buffer, caps);
92 gst_caps_unref (caps);
93 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
95 GST_DEBUG ("cleaning up appsink");
96 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
97 cleanup_appsink (sink);
102 /* Verifies that the handoff callback gets run one time when passing a buffer */
103 GST_START_TEST (test_handoff_callback)
109 GstAppSinkCallbacks callbacks = { NULL };
111 sink = setup_appsink ();
114 testdata = 5; /* Arbitrary value */
116 callbacks.new_buffer = callback_function;
118 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
120 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
122 caps = gst_caps_from_string ("application/x-gst-check");
123 buffer = gst_buffer_new_and_alloc (4);
124 gst_buffer_set_caps (buffer, caps);
125 gst_caps_unref (caps);
126 /* Pushing a buffer should run our callback */
127 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
129 testdata = operate_on_data (testdata);
131 /* If both test_data & global_testdata have been operated on, we're happy. */
132 fail_unless (testdata == global_testdata);
134 GST_DEBUG ("cleaning up appsink");
135 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
136 cleanup_appsink (sink);
141 /* Verifies that the notify function gets executed when the sink is destroyed */
142 GST_START_TEST (test_notify0)
146 GstAppSinkCallbacks callbacks = { NULL };
148 sink = gst_element_factory_make ("appsink", NULL);
151 testdata = 17; /* Arbitrary value */
153 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
154 &testdata, (*notify_function));
156 GST_DEBUG ("cleaning up appsink");
157 /* Destroying sink should call our notify_function */
158 gst_object_unref (sink);
160 testdata = operate_on_data (testdata);
162 /* If both test_data & global_testdata have been operated on, we're happy. */
163 fail_unless (testdata == global_testdata);
169 /* Verifies that the notify function gets executed when
170 * gst_app_sink_set_callbacks () gets called */
171 GST_START_TEST (test_notify1)
175 GstAppSinkCallbacks callbacks = { NULL };
177 sink = gst_element_factory_make ("appsink", NULL);
180 testdata = 42; /* Arbitrary value */
182 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
183 &testdata, (*notify_function));
184 /* Setting new callbacks should trigger the destroy of the old data */
185 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
187 testdata = operate_on_data (testdata);
189 /* If both test_data & global_testdata have been operated on, we're happy. */
190 fail_unless (testdata == global_testdata);
192 GST_DEBUG ("cleaning up appsink");
193 gst_object_unref (sink);
198 static GstBufferList *mylist;
199 static GstCaps *mycaps;
201 static GstBufferList *
202 create_buffer_list (void)
204 GstBufferListIterator *it;
207 mylist = gst_buffer_list_new ();
208 fail_if (mylist == NULL);
210 mycaps = gst_caps_from_string ("application/x-gst-check");
211 fail_if (mycaps == NULL);
213 it = gst_buffer_list_iterate (mylist);
214 fail_if (it == NULL);
216 gst_buffer_list_iterator_add_group (it);
218 buffer = gst_buffer_new_and_alloc (sizeof (gint));
219 *(gint *) GST_BUFFER_DATA (buffer) = 1;
220 gst_buffer_set_caps (buffer, mycaps);
221 gst_buffer_list_iterator_add (it, buffer);
223 gst_buffer_list_iterator_add_group (it);
225 buffer = gst_buffer_new_and_alloc (sizeof (gint));
226 *(gint *) GST_BUFFER_DATA (buffer) = 2;
227 gst_buffer_set_caps (buffer, mycaps);
228 gst_buffer_list_iterator_add (it, buffer);
230 buffer = gst_buffer_new_and_alloc (sizeof (gint));
231 *(gint *) GST_BUFFER_DATA (buffer) = 4;
232 gst_buffer_set_caps (buffer, mycaps);
233 gst_buffer_list_iterator_add (it, buffer);
235 gst_buffer_list_iterator_free (it);
241 check_buffer_list (GstBufferList * list)
243 GstBufferListIterator *it;
247 fail_unless (list == mylist);
248 fail_unless (gst_buffer_list_n_groups (list) == 2);
250 it = gst_buffer_list_iterate (list);
251 fail_if (it == NULL);
253 fail_unless (gst_buffer_list_iterator_next_group (it));
254 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 1);
255 buf = gst_buffer_list_iterator_next (it);
256 fail_if (buf == NULL);
257 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 1);
258 caps = gst_buffer_get_caps (buf);
259 fail_unless (caps == mycaps);
260 fail_unless (gst_caps_is_equal (caps, mycaps));
261 gst_caps_unref (caps);
263 fail_unless (gst_buffer_list_iterator_next_group (it));
264 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 2);
265 buf = gst_buffer_list_iterator_next (it);
266 fail_if (buf == NULL);
267 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 2);
268 caps = gst_buffer_get_caps (buf);
269 fail_unless (caps == mycaps);
270 gst_caps_unref (caps);
272 buf = gst_buffer_list_iterator_next (it);
273 fail_if (buf == NULL);
274 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 4);
275 caps = gst_buffer_get_caps (buf);
276 fail_unless (caps == mycaps);
277 gst_caps_unref (caps);
279 gst_buffer_list_iterator_free (it);
283 callback_function_buffer_list (GstAppSink * appsink, gpointer callback_data)
287 list = gst_app_sink_pull_buffer_list (appsink);
288 fail_unless (GST_IS_BUFFER_LIST (list));
290 check_buffer_list (list);
292 gst_buffer_list_unref (list);
297 GST_START_TEST (test_buffer_list)
301 GstAppSinkCallbacks callbacks = { NULL };
303 sink = setup_appsink ();
305 callbacks.new_buffer_list = callback_function_buffer_list;
307 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, NULL, NULL);
309 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
311 list = create_buffer_list ();
312 fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
314 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
315 cleanup_appsink (sink);
321 callback_function_buffer (GstAppSink * appsink, gpointer p_counter)
324 gint *p_int_counter = p_counter;
326 buf = gst_app_sink_pull_buffer (appsink);
327 fail_unless (GST_IS_BUFFER (buf));
329 /* buffer list has 3 buffers in two groups */
330 switch (*p_int_counter) {
332 fail_unless_equals_int (GST_BUFFER_SIZE (buf), sizeof (gint));
333 fail_unless_equals_int ((((gint *) GST_BUFFER_DATA (buf))[0]), 1);
336 fail_unless_equals_int (GST_BUFFER_SIZE (buf), 2 * sizeof (gint));
337 fail_unless_equals_int ((((gint *) GST_BUFFER_DATA (buf))[0]), 2);
338 fail_unless_equals_int ((((gint *) GST_BUFFER_DATA (buf))[1]), 4);
341 g_warn_if_reached ();
345 gst_buffer_unref (buf);
352 GST_START_TEST (test_buffer_list_fallback)
356 GstAppSinkCallbacks callbacks = { NULL };
359 sink = setup_appsink ();
361 callbacks.new_buffer = callback_function_buffer;
363 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &counter, NULL);
365 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
367 list = create_buffer_list ();
368 fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
370 fail_unless_equals_int (counter, 2);
372 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
373 cleanup_appsink (sink);
378 GST_START_TEST (test_buffer_list_fallback_signal)
384 sink = setup_appsink ();
386 /* C calling convention to the rescue.. */
387 g_signal_connect (sink, "new-buffer", G_CALLBACK (callback_function_buffer),
390 g_object_set (sink, "emit-signals", TRUE, NULL);
392 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
394 list = create_buffer_list ();
395 fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
397 fail_unless_equals_int (counter, 2);
399 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
400 cleanup_appsink (sink);
408 Suite *s = suite_create ("appsink");
409 TCase *tc_chain = tcase_create ("general");
411 suite_add_tcase (s, tc_chain);
412 tcase_add_test (tc_chain, test_non_clients);
413 tcase_add_test (tc_chain, test_handoff_callback);
414 tcase_add_test (tc_chain, test_notify0);
415 tcase_add_test (tc_chain, test_notify1);
416 tcase_add_test (tc_chain, test_buffer_list);
417 tcase_add_test (tc_chain, test_buffer_list_fallback);
418 tcase_add_test (tc_chain, test_buffer_list_fallback_signal);
423 GST_CHECK_MAIN (appsink);