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 notify_test_function (gpointer userdata)
68 global_testdata = operate_on_data (GPOINTER_TO_INT (userdata));
72 callback_function (GstAppSink * appsink, gpointer callback_data)
74 global_testdata = operate_on_data (*((gint *) callback_data));
80 notify_function (gpointer callback_data)
82 global_testdata = operate_on_data (*((gint *) callback_data));
85 GST_START_TEST (test_non_clients)
91 sink = setup_appsink ();
93 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
95 caps = gst_caps_from_string ("application/x-gst-check");
96 buffer = gst_buffer_new_and_alloc (4);
97 gst_buffer_set_caps (buffer, caps);
98 gst_caps_unref (caps);
99 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
101 GST_DEBUG ("cleaning up appsink");
102 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
103 cleanup_appsink (sink);
108 /* Verifies that the handoff callback gets run one time when passing a buffer */
109 GST_START_TEST (test_handoff_callback)
115 GstAppSinkCallbacks callbacks = { NULL };
117 sink = setup_appsink ();
120 testdata = 5; /* Arbitrary value */
122 callbacks.new_buffer = callback_function;
124 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
126 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
128 caps = gst_caps_from_string ("application/x-gst-check");
129 buffer = gst_buffer_new_and_alloc (4);
130 gst_buffer_set_caps (buffer, caps);
131 gst_caps_unref (caps);
132 /* Pushing a buffer should run our callback */
133 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
135 testdata = operate_on_data (testdata);
137 /* If both test_data & global_testdata have been operated on, we're happy. */
138 fail_unless (testdata == global_testdata);
140 GST_DEBUG ("cleaning up appsink");
141 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
142 cleanup_appsink (sink);
147 /* Verifies that the notify function gets executed when the sink is destroyed */
148 GST_START_TEST (test_notify0)
152 GstAppSinkCallbacks callbacks = { NULL };
154 sink = gst_element_factory_make ("appsink", NULL);
157 testdata = 17; /* Arbitrary value */
159 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
160 &testdata, (*notify_function));
162 GST_DEBUG ("cleaning up appsink");
163 /* Destroying sink should call our notify_function */
164 gst_object_unref (sink);
166 testdata = operate_on_data (testdata);
168 /* If both test_data & global_testdata have been operated on, we're happy. */
169 fail_unless (testdata == global_testdata);
175 /* Verifies that the notify function gets executed when
176 * gst_app_sink_set_callbacks () gets called */
177 GST_START_TEST (test_notify1)
181 GstAppSinkCallbacks callbacks = { NULL };
183 sink = gst_element_factory_make ("appsink", NULL);
186 testdata = 42; /* Arbitrary value */
188 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
189 &testdata, (*notify_function));
190 /* Setting new callbacks should trigger the destroy of the old data */
191 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
193 testdata = operate_on_data (testdata);
195 /* If both test_data & global_testdata have been operated on, we're happy. */
196 fail_unless (testdata == global_testdata);
198 GST_DEBUG ("cleaning up appsink");
199 gst_object_unref (sink);
204 static GstBufferList *mylist;
205 static GstCaps *mycaps;
207 static GstBufferList *
208 create_buffer_list (void)
210 GstBufferListIterator *it;
213 mylist = gst_buffer_list_new ();
214 fail_if (mylist == NULL);
216 mycaps = gst_caps_from_string ("application/x-gst-check");
217 fail_if (mycaps == NULL);
219 it = gst_buffer_list_iterate (mylist);
220 fail_if (it == NULL);
222 gst_buffer_list_iterator_add_group (it);
224 buffer = gst_buffer_new_and_alloc (sizeof (gint));
225 *(gint *) GST_BUFFER_DATA (buffer) = 1;
226 gst_buffer_set_caps (buffer, mycaps);
227 gst_buffer_list_iterator_add (it, buffer);
229 gst_buffer_list_iterator_add_group (it);
231 buffer = gst_buffer_new_and_alloc (sizeof (gint));
232 *(gint *) GST_BUFFER_DATA (buffer) = 2;
233 gst_buffer_set_caps (buffer, mycaps);
234 gst_buffer_list_iterator_add (it, buffer);
236 buffer = gst_buffer_new_and_alloc (sizeof (gint));
237 *(gint *) GST_BUFFER_DATA (buffer) = 4;
238 gst_buffer_set_caps (buffer, mycaps);
239 gst_buffer_list_iterator_add (it, buffer);
241 gst_buffer_list_iterator_free (it);
247 check_buffer_list (GstBufferList * list)
249 GstBufferListIterator *it;
253 fail_unless (list == mylist);
254 fail_unless (gst_buffer_list_n_groups (list) == 2);
256 it = gst_buffer_list_iterate (list);
257 fail_if (it == NULL);
259 fail_unless (gst_buffer_list_iterator_next_group (it));
260 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 1);
261 buf = gst_buffer_list_iterator_next (it);
262 fail_if (buf == NULL);
263 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 1);
264 caps = gst_buffer_get_caps (buf);
265 fail_unless (caps == mycaps);
266 fail_unless (gst_caps_is_equal (caps, mycaps));
267 gst_caps_unref (caps);
269 fail_unless (gst_buffer_list_iterator_next_group (it));
270 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 2);
271 buf = gst_buffer_list_iterator_next (it);
272 fail_if (buf == NULL);
273 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 2);
274 caps = gst_buffer_get_caps (buf);
275 fail_unless (caps == mycaps);
276 gst_caps_unref (caps);
278 buf = gst_buffer_list_iterator_next (it);
279 fail_if (buf == NULL);
280 fail_unless (*(gint *) GST_BUFFER_DATA (buf) == 4);
281 caps = gst_buffer_get_caps (buf);
282 fail_unless (caps == mycaps);
283 gst_caps_unref (caps);
285 gst_buffer_list_iterator_free (it);
289 callback_function_buffer_list (GstAppSink * appsink, gpointer callback_data)
293 list = gst_app_sink_pull_buffer_list (appsink);
294 fail_unless (GST_IS_BUFFER_LIST (list));
296 check_buffer_list (list);
298 gst_buffer_list_unref (list);
303 GST_START_TEST (test_buffer_list)
307 GstAppSinkCallbacks callbacks = { NULL };
309 sink = setup_appsink ();
311 callbacks.new_buffer_list = callback_function_buffer_list;
313 gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, NULL, NULL);
315 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
317 list = create_buffer_list ();
318 fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
320 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
321 cleanup_appsink (sink);
329 Suite *s = suite_create ("appsink");
330 TCase *tc_chain = tcase_create ("general");
332 suite_add_tcase (s, tc_chain);
333 tcase_add_test (tc_chain, test_non_clients);
334 tcase_add_test (tc_chain, test_handoff_callback);
335 tcase_add_test (tc_chain, test_notify0);
336 tcase_add_test (tc_chain, test_notify1);
337 tcase_add_test (tc_chain, test_buffer_list);
343 main (int argc, char **argv)
347 Suite *s = appsink_suite ();
348 SRunner *sr = srunner_create (s);
350 gst_check_init (&argc, &argv);
352 srunner_run_all (sr, CK_NORMAL);
353 nf = srunner_ntests_failed (sr);