tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / libs / basesrc.c
1 /* GStreamer
2  *
3  * some unit tests for GstBaseSrc
4  *
5  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gst/gst.h>
27 #include <gst/check/gstcheck.h>
28 #include <gst/base/gstbasesrc.h>
29
30 static gboolean
31 eos_event_counter (GstObject * pad, GstEvent * event, guint * p_num_eos)
32 {
33   fail_unless (event != NULL);
34   fail_unless (GST_IS_EVENT (event));
35
36   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
37     *p_num_eos += 1;
38
39   return TRUE;
40 }
41
42 /* basesrc_eos_events_push_live_op:
43  *  - make sure source does send an EOS event when operating in push
44  *    mode and being set to READY explicitly (like one might with
45  *    live sources)
46  */
47 GST_START_TEST (basesrc_eos_events_push_live_op)
48 {
49   GstStateChangeReturn state_ret;
50   GstElement *src, *sink, *pipe;
51   GstMessage *msg;
52   GstBus *bus;
53   GstPad *srcpad;
54   guint probe, num_eos = 0;
55
56   pipe = gst_pipeline_new ("pipeline");
57   sink = gst_element_factory_make ("fakesink", "sink");
58   src = gst_element_factory_make ("fakesrc", "src");
59
60   g_assert (pipe != NULL);
61   g_assert (sink != NULL);
62   g_assert (src != NULL);
63
64   fail_unless (gst_bin_add (GST_BIN (pipe), src) == TRUE);
65   fail_unless (gst_bin_add (GST_BIN (pipe), sink) == TRUE);
66
67   fail_unless (gst_element_link (src, sink) == TRUE);
68
69   g_object_set (sink, "can-activate-push", TRUE, NULL);
70   g_object_set (sink, "can-activate-pull", FALSE, NULL);
71
72   g_object_set (src, "can-activate-push", TRUE, NULL);
73   g_object_set (src, "can-activate-pull", FALSE, NULL);
74
75   /* set up event probe to count EOS events */
76   srcpad = gst_element_get_pad (src, "src");
77   fail_unless (srcpad != NULL);
78
79   probe = gst_pad_add_event_probe (srcpad,
80       G_CALLBACK (eos_event_counter), &num_eos);
81
82   bus = gst_element_get_bus (pipe);
83
84   gst_element_set_state (pipe, GST_STATE_PLAYING);
85   state_ret = gst_element_get_state (pipe, NULL, NULL, -1);
86   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
87
88   /* wait a second, then do controlled shutdown */
89   g_usleep (GST_USECOND * 1);
90
91   /* shut down source only (should send EOS event) ... */
92   gst_element_set_state (src, GST_STATE_NULL);
93   state_ret = gst_element_get_state (src, NULL, NULL, -1);
94   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
95
96   fail_unless (gst_element_set_locked_state (src, TRUE) == TRUE);
97
98   /* ... and wait for the EOS message from the sink */
99   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
100   fail_unless (msg != NULL);
101   fail_unless (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
102   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
103
104   /* should be exactly one EOS event */
105   fail_unless (num_eos == 1);
106
107   gst_element_set_state (pipe, GST_STATE_NULL);
108   gst_element_get_state (pipe, NULL, NULL, -1);
109
110   /* make sure source hasn't sent a second one when going PAUSED => READY */
111   fail_unless (num_eos == 1);
112
113   gst_pad_remove_event_probe (srcpad, probe);
114   gst_object_unref (srcpad);
115   gst_message_unref (msg);
116   gst_object_unref (bus);
117   gst_object_unref (pipe);
118 }
119
120 GST_END_TEST;
121
122 /* basesrc_eos_events_push:
123  *  - make sure source only sends one EOS when operating in push-mode,
124  *    reaching the max number of buffers, and is then shut down.
125  */
126 GST_START_TEST (basesrc_eos_events_push)
127 {
128   GstStateChangeReturn state_ret;
129   GstElement *src, *sink, *pipe;
130   GstMessage *msg;
131   GstBus *bus;
132   GstPad *srcpad;
133   guint probe, num_eos = 0;
134
135   pipe = gst_pipeline_new ("pipeline");
136   sink = gst_element_factory_make ("fakesink", "sink");
137   src = gst_element_factory_make ("fakesrc", "src");
138
139   g_assert (pipe != NULL);
140   g_assert (sink != NULL);
141   g_assert (src != NULL);
142
143   fail_unless (gst_bin_add (GST_BIN (pipe), src) == TRUE);
144   fail_unless (gst_bin_add (GST_BIN (pipe), sink) == TRUE);
145
146   fail_unless (gst_element_link (src, sink) == TRUE);
147
148   g_object_set (sink, "can-activate-push", TRUE, NULL);
149   g_object_set (sink, "can-activate-pull", FALSE, NULL);
150
151   g_object_set (src, "can-activate-push", TRUE, NULL);
152   g_object_set (src, "can-activate-pull", FALSE, NULL);
153   g_object_set (src, "num-buffers", 8, NULL);
154
155   /* set up event probe to count EOS events */
156   srcpad = gst_element_get_pad (src, "src");
157   fail_unless (srcpad != NULL);
158
159   probe = gst_pad_add_event_probe (srcpad,
160       G_CALLBACK (eos_event_counter), &num_eos);
161
162   bus = gst_element_get_bus (pipe);
163
164   gst_element_set_state (pipe, GST_STATE_PLAYING);
165   state_ret = gst_element_get_state (pipe, NULL, NULL, -1);
166   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
167
168   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
169   fail_unless (msg != NULL);
170   fail_unless (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
171   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
172
173   /* should be exactly one EOS event */
174   fail_unless (num_eos == 1);
175
176   gst_element_set_state (pipe, GST_STATE_NULL);
177   gst_element_get_state (pipe, NULL, NULL, -1);
178
179   /* make sure source hasn't sent a second one when going PAUSED => READY */
180   fail_unless (num_eos == 1);
181
182   gst_pad_remove_event_probe (srcpad, probe);
183   gst_object_unref (srcpad);
184   gst_message_unref (msg);
185   gst_object_unref (bus);
186   gst_object_unref (pipe);
187 }
188
189 GST_END_TEST;
190
191 /* basesrc_eos_events_pull_live_op:
192  *  - make sure source doesn't send an EOS event when operating in
193  *    pull mode and being set to READY explicitly (like one might with
194  *    live sources)
195  */
196 GST_START_TEST (basesrc_eos_events_pull_live_op)
197 {
198   GstStateChangeReturn state_ret;
199   GstElement *src, *sink, *pipe;
200   GstPad *srcpad;
201   guint probe, num_eos = 0;
202
203   pipe = gst_pipeline_new ("pipeline");
204   sink = gst_element_factory_make ("fakesink", "sink");
205   src = gst_element_factory_make ("fakesrc", "src");
206
207   g_assert (pipe != NULL);
208   g_assert (sink != NULL);
209   g_assert (src != NULL);
210
211   fail_unless (gst_bin_add (GST_BIN (pipe), src) == TRUE);
212   fail_unless (gst_bin_add (GST_BIN (pipe), sink) == TRUE);
213
214   fail_unless (gst_element_link (src, sink) == TRUE);
215
216   g_object_set (sink, "can-activate-push", FALSE, NULL);
217   g_object_set (sink, "can-activate-pull", TRUE, NULL);
218
219   g_object_set (src, "can-activate-push", FALSE, NULL);
220   g_object_set (src, "can-activate-pull", TRUE, NULL);
221
222   /* set up event probe to count EOS events */
223   srcpad = gst_element_get_pad (src, "src");
224   fail_unless (srcpad != NULL);
225
226   probe = gst_pad_add_event_probe (srcpad,
227       G_CALLBACK (eos_event_counter), &num_eos);
228
229   gst_element_set_state (pipe, GST_STATE_PLAYING);
230   state_ret = gst_element_get_state (pipe, NULL, NULL, -1);
231   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
232
233   /* wait a second, then do controlled shutdown */
234   g_usleep (GST_USECOND * 1);
235
236   /* shut down source only ... */
237   gst_element_set_state (src, GST_STATE_NULL);
238   state_ret = gst_element_get_state (src, NULL, NULL, -1);
239   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
240
241   fail_unless (gst_element_set_locked_state (src, TRUE) == TRUE);
242
243   /* source shouldn't have sent any EOS event in pull mode */
244   fail_unless (num_eos == 0);
245
246   gst_element_set_state (pipe, GST_STATE_NULL);
247   gst_element_get_state (pipe, NULL, NULL, -1);
248
249   /* make sure source hasn't sent an EOS when going PAUSED => READY either */
250   fail_unless (num_eos == 0);
251
252   gst_pad_remove_event_probe (srcpad, probe);
253   gst_object_unref (srcpad);
254   gst_object_unref (pipe);
255 }
256
257 GST_END_TEST;
258
259 /* basesrc_eos_events_pull:
260  *  - makes sure source doesn't send EOS event when reaching the max.
261  *    number of buffers configured in pull-mode
262  *  - make sure source doesn't send EOS event either when being shut down
263  *    (PAUSED => READY state change) after EOSing in pull mode 
264  */
265 GST_START_TEST (basesrc_eos_events_pull)
266 {
267   GstStateChangeReturn state_ret;
268   GstElement *src, *sink, *pipe;
269   GstMessage *msg;
270   GstBus *bus;
271   GstPad *srcpad;
272   guint probe, num_eos = 0;
273
274   pipe = gst_pipeline_new ("pipeline");
275   sink = gst_element_factory_make ("fakesink", "sink");
276   src = gst_element_factory_make ("fakesrc", "src");
277
278   g_assert (pipe != NULL);
279   g_assert (sink != NULL);
280   g_assert (src != NULL);
281
282   fail_unless (gst_bin_add (GST_BIN (pipe), src) == TRUE);
283   fail_unless (gst_bin_add (GST_BIN (pipe), sink) == TRUE);
284
285   fail_unless (gst_element_link (src, sink) == TRUE);
286
287   g_object_set (sink, "can-activate-push", FALSE, NULL);
288   g_object_set (sink, "can-activate-pull", TRUE, NULL);
289
290   g_object_set (src, "can-activate-push", FALSE, NULL);
291   g_object_set (src, "can-activate-pull", TRUE, NULL);
292   g_object_set (src, "num-buffers", 8, NULL);
293
294   /* set up event probe to count EOS events */
295   srcpad = gst_element_get_pad (src, "src");
296   fail_unless (srcpad != NULL);
297
298   probe = gst_pad_add_event_probe (srcpad,
299       G_CALLBACK (eos_event_counter), &num_eos);
300
301   bus = gst_element_get_bus (pipe);
302
303   gst_element_set_state (pipe, GST_STATE_PLAYING);
304   state_ret = gst_element_get_state (pipe, NULL, NULL, -1);
305   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
306
307   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
308   fail_unless (msg != NULL);
309   fail_unless (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
310   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
311
312   /* source shouldn't have sent any EOS event in pull mode */
313   fail_unless (num_eos == 0);
314
315   gst_element_set_state (pipe, GST_STATE_NULL);
316   gst_element_get_state (pipe, NULL, NULL, -1);
317
318   /* make sure source hasn't sent an EOS when going PAUSED => READY either */
319   fail_unless (num_eos == 0);
320
321   gst_pad_remove_event_probe (srcpad, probe);
322   gst_object_unref (srcpad);
323   gst_message_unref (msg);
324   gst_object_unref (bus);
325   gst_object_unref (pipe);
326 }
327
328 GST_END_TEST;
329
330
331 Suite *
332 gst_basesrc_suite (void)
333 {
334   Suite *s = suite_create ("GstBaseSrc");
335   TCase *tc = tcase_create ("general");
336
337   suite_add_tcase (s, tc);
338   tcase_add_test (tc, basesrc_eos_events_pull);
339   tcase_add_test (tc, basesrc_eos_events_push);
340   tcase_add_test (tc, basesrc_eos_events_push_live_op);
341   tcase_add_test (tc, basesrc_eos_events_pull_live_op);
342
343   return s;
344 }
345
346 GST_CHECK_MAIN (gst_basesrc);