gio: Remove unused function
[platform/upstream/gstreamer.git] / tests / check / elements / appsink.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009, Axis Communications AB, LUND, SWEDEN
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <gst/check/gstcheck.h>
22 #include <gst/app/gstappsink.h>
23
24 gint global_testdata;
25
26 static GstPad *mysrcpad;
27
28 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
29     GST_PAD_SRC,
30     GST_PAD_ALWAYS,
31     GST_STATIC_CAPS ("application/x-gst-check")
32     );
33
34 static GstElement *
35 setup_appsink (void)
36 {
37   GstElement *appsink;
38
39   GST_DEBUG ("setup_appsink");
40   appsink = gst_check_setup_element ("appsink");
41   mysrcpad = gst_check_setup_src_pad (appsink, &srctemplate, NULL);
42
43   return appsink;
44 }
45
46 static void
47 cleanup_appsink (GstElement * appsink)
48 {
49   GST_DEBUG ("cleanup_appsink");
50
51   gst_check_teardown_src_pad (appsink);
52   gst_check_teardown_element (appsink);
53 }
54
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. */
59 gint
60 operate_on_data (gint indata)
61 {
62   return indata * 2;
63 }
64
65 void
66 notify_test_function (gpointer userdata)
67 {
68   global_testdata = operate_on_data (GPOINTER_TO_INT (userdata));
69 }
70
71 static GstFlowReturn
72 callback_function (GstAppSink * appsink, gpointer callback_data)
73 {
74   global_testdata = operate_on_data (*((gint *) callback_data));
75
76   return GST_FLOW_OK;
77 }
78
79 void
80 notify_function (gpointer callback_data)
81 {
82   global_testdata = operate_on_data (*((gint *) callback_data));
83 }
84
85 GST_START_TEST (test_non_clients)
86 {
87   GstElement *sink;
88   GstBuffer *buffer;
89   GstCaps *caps;
90
91   sink = setup_appsink ();
92
93   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
94
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);
100
101   GST_DEBUG ("cleaning up appsink");
102   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
103   cleanup_appsink (sink);
104 }
105
106 GST_END_TEST;
107
108 /* Verifies that the handoff callback gets run one time when passing a buffer */
109 GST_START_TEST (test_handoff_callback)
110 {
111   GstElement *sink;
112   GstBuffer *buffer;
113   GstCaps *caps;
114   gint testdata;
115   GstAppSinkCallbacks callbacks = { NULL };
116
117   sink = setup_appsink ();
118
119   global_testdata = 0;
120   testdata = 5;                 /* Arbitrary value */
121
122   callbacks.new_buffer = callback_function;
123
124   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
125
126   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
127
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);
134
135   testdata = operate_on_data (testdata);
136
137   /* If both test_data & global_testdata have been operated on, we're happy. */
138   fail_unless (testdata == global_testdata);
139
140   GST_DEBUG ("cleaning up appsink");
141   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
142   cleanup_appsink (sink);
143 }
144
145 GST_END_TEST;
146
147 /* Verifies that the notify function gets executed when the sink is destroyed */
148 GST_START_TEST (test_notify0)
149 {
150   GstElement *sink;
151   gint testdata;
152   GstAppSinkCallbacks callbacks = { NULL };
153
154   sink = gst_element_factory_make ("appsink", NULL);
155
156   global_testdata = 0;
157   testdata = 17;                /* Arbitrary value */
158
159   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
160       &testdata, (*notify_function));
161
162   GST_DEBUG ("cleaning up appsink");
163   /* Destroying sink should call our notify_function */
164   gst_object_unref (sink);
165
166   testdata = operate_on_data (testdata);
167
168   /* If both test_data & global_testdata have been operated on, we're happy. */
169   fail_unless (testdata == global_testdata);
170 }
171
172 GST_END_TEST;
173
174
175 /* Verifies that the notify function gets executed when
176  * gst_app_sink_set_callbacks () gets called */
177 GST_START_TEST (test_notify1)
178 {
179   GstElement *sink;
180   gint testdata;
181   GstAppSinkCallbacks callbacks = { NULL };
182
183   sink = gst_element_factory_make ("appsink", NULL);
184
185   global_testdata = 0;
186   testdata = 42;                /* Arbitrary value */
187
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);
192
193   testdata = operate_on_data (testdata);
194
195   /* If both test_data & global_testdata have been operated on, we're happy. */
196   fail_unless (testdata == global_testdata);
197
198   GST_DEBUG ("cleaning up appsink");
199   gst_object_unref (sink);
200 }
201
202 GST_END_TEST;
203
204 static GstBufferList *mylist;
205 static GstCaps *mycaps;
206
207 static GstBufferList *
208 create_buffer_list (void)
209 {
210   GstBufferListIterator *it;
211   GstBuffer *buffer;
212
213   mylist = gst_buffer_list_new ();
214   fail_if (mylist == NULL);
215
216   mycaps = gst_caps_from_string ("application/x-gst-check");
217   fail_if (mycaps == NULL);
218
219   it = gst_buffer_list_iterate (mylist);
220   fail_if (it == NULL);
221
222   gst_buffer_list_iterator_add_group (it);
223
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);
228
229   gst_buffer_list_iterator_add_group (it);
230
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);
235
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);
240
241   gst_buffer_list_iterator_free (it);
242
243   return mylist;
244 }
245
246 static void
247 check_buffer_list (GstBufferList * list)
248 {
249   GstBufferListIterator *it;
250   GstBuffer *buf;
251   GstCaps *caps;
252
253   fail_unless (list == mylist);
254   fail_unless (gst_buffer_list_n_groups (list) == 2);
255
256   it = gst_buffer_list_iterate (list);
257   fail_if (it == NULL);
258
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);
268
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);
277
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);
284
285   gst_buffer_list_iterator_free (it);
286 }
287
288 static GstFlowReturn
289 callback_function_buffer_list (GstAppSink * appsink, gpointer callback_data)
290 {
291   GstBufferList *list;
292
293   list = gst_app_sink_pull_buffer_list (appsink);
294   fail_unless (GST_IS_BUFFER_LIST (list));
295
296   check_buffer_list (list);
297
298   gst_buffer_list_unref (list);
299
300   return GST_FLOW_OK;
301 }
302
303 GST_START_TEST (test_buffer_list)
304 {
305   GstElement *sink;
306   GstBufferList *list;
307   GstAppSinkCallbacks callbacks = { NULL };
308
309   sink = setup_appsink ();
310
311   callbacks.new_buffer_list = callback_function_buffer_list;
312
313   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, NULL, NULL);
314
315   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
316
317   list = create_buffer_list ();
318   fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
319
320   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
321   cleanup_appsink (sink);
322 }
323
324 GST_END_TEST;
325
326 static Suite *
327 appsink_suite (void)
328 {
329   Suite *s = suite_create ("appsink");
330   TCase *tc_chain = tcase_create ("general");
331
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);
338
339   return s;
340 }
341
342 int
343 main (int argc, char **argv)
344 {
345   int nf;
346
347   Suite *s = appsink_suite ();
348   SRunner *sr = srunner_create (s);
349
350   gst_check_init (&argc, &argv);
351
352   srunner_run_all (sr, CK_NORMAL);
353   nf = srunner_ntests_failed (sr);
354   srunner_free (sr);
355
356   return nf;
357 }