check/: Check fixes, use API as stated in design docs, remove hacks.
[platform/upstream/gstreamer.git] / tests / check / generic / sinks.c
1 /* GStreamer
2  *
3  * unit test for sinks
4  *
5  * Copyright (C) <2005> Wim Taymans <wim at fluendo dot com>
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 #include <gst/check/gstcheck.h>
24
25 /* a sink should go ASYNC to PAUSE. forcing PLAYING is possible */
26 GST_START_TEST (test_sink)
27 {
28   GstElement *sink;
29   GstStateChangeReturn ret;
30   GstState current, pending;
31   GTimeVal tv;
32
33   sink = gst_element_factory_make ("fakesink", "sink");
34
35   ret = gst_element_set_state (sink, GST_STATE_PAUSED);
36   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "no async state return");
37
38   ret = gst_element_set_state (sink, GST_STATE_PLAYING);
39   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "no forced async state change");
40
41   GST_TIME_TO_TIMEVAL ((GstClockTime) 0, tv);
42
43   ret = gst_element_get_state (sink, &current, &pending, &tv);
44   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "not changing state async");
45   fail_unless (current == GST_STATE_READY, "bad current state");
46   fail_unless (pending == GST_STATE_PLAYING, "bad pending state");
47
48   ret = gst_element_set_state (sink, GST_STATE_PAUSED);
49   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "no async going back to paused");
50
51   ret = gst_element_set_state (sink, GST_STATE_READY);
52   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "failed to go to ready");
53
54   ret = gst_element_set_state (sink, GST_STATE_NULL);
55   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "failed to go to null");
56
57   gst_object_unref (sink);
58 }
59
60 GST_END_TEST
61 /* a sink should go ASYNC to PAUSE. PAUSE should complete when
62  * prerolled. */
63 GST_START_TEST (test_src_sink)
64 {
65   GstElement *sink, *src, *pipeline;
66   GstStateChangeReturn ret;
67   GstState current, pending;
68   GstPad *srcpad, *sinkpad;
69
70   pipeline = gst_pipeline_new ("pipeline");
71   src = gst_element_factory_make ("fakesrc", "src");
72   sink = gst_element_factory_make ("fakesink", "sink");
73
74   gst_bin_add (GST_BIN (pipeline), src);
75   gst_bin_add (GST_BIN (pipeline), sink);
76
77   srcpad = gst_element_get_pad (src, "src");
78   sinkpad = gst_element_get_pad (sink, "sink");
79   gst_pad_link (srcpad, sinkpad);
80   gst_object_unref (srcpad);
81   gst_object_unref (sinkpad);
82
83   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
84   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "no async state return");
85   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
86   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "no success state return");
87
88   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
89   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot start play");
90
91   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
92   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "not playing");
93   fail_unless (current == GST_STATE_PLAYING, "not playing");
94   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
95   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
96   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot null pipeline");
97
98   gst_object_unref (pipeline);
99
100 }
101
102 GST_END_TEST
103 /* a pipeline with live source should return NO_PREROLL in
104  * PAUSE. When removing the live source it should return ASYNC
105  * from the sink */
106 GST_START_TEST (test_livesrc_remove)
107 {
108   GstElement *sink, *src, *pipeline;
109   GstStateChangeReturn ret;
110   GstState current, pending;
111   GstPad *srcpad, *sinkpad;
112   GTimeVal tv;
113
114   pipeline = gst_pipeline_new ("pipeline");
115   src = gst_element_factory_make ("fakesrc", "src");
116   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
117   sink = gst_element_factory_make ("fakesink", "sink");
118
119   gst_bin_add (GST_BIN (pipeline), src);
120   gst_bin_add (GST_BIN (pipeline), sink);
121
122   srcpad = gst_element_get_pad (src, "src");
123   sinkpad = gst_element_get_pad (sink, "sink");
124   gst_pad_link (srcpad, sinkpad);
125   gst_object_unref (srcpad);
126   gst_object_unref (sinkpad);
127
128   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
129   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
130       "no no_preroll state return");
131
132   ret = gst_element_get_state (src, &current, &pending, NULL);
133   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
134   fail_unless (current == GST_STATE_PAUSED, "not paused");
135   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
136
137   gst_bin_remove (GST_BIN (pipeline), src);
138
139   GST_TIME_TO_TIMEVAL (0, tv);
140   ret = gst_element_get_state (pipeline, &current, &pending, &tv);
141   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "not async");
142   fail_unless (current == GST_STATE_PAUSED, "not paused");
143   fail_unless (pending == GST_STATE_PAUSED, "not paused");
144 }
145
146 GST_END_TEST
147 /* a sink should go ASYNC to PAUSE. PAUSE does not complete
148  * since we have a live source. */
149 GST_START_TEST (test_livesrc_sink)
150 {
151   GstElement *sink, *src, *pipeline;
152   GstStateChangeReturn ret;
153   GstState current, pending;
154   GstPad *srcpad, *sinkpad;
155
156   pipeline = gst_pipeline_new ("pipeline");
157   src = gst_element_factory_make ("fakesrc", "src");
158   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
159   sink = gst_element_factory_make ("fakesink", "sink");
160
161   gst_bin_add (GST_BIN (pipeline), src);
162   gst_bin_add (GST_BIN (pipeline), sink);
163
164   srcpad = gst_element_get_pad (src, "src");
165   sinkpad = gst_element_get_pad (sink, "sink");
166   gst_pad_link (srcpad, sinkpad);
167   gst_object_unref (srcpad);
168   gst_object_unref (sinkpad);
169
170   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
171   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
172       "no no_preroll state return");
173
174   ret = gst_element_get_state (src, &current, &pending, NULL);
175   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
176   fail_unless (current == GST_STATE_PAUSED, "not paused");
177   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
178
179   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
180   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
181   fail_unless (current == GST_STATE_PAUSED, "not paused");
182   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
183
184   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
185   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
186   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot force play got %d",
187       ret);
188
189   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
190   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "not playing");
191   fail_unless (current == GST_STATE_PLAYING, "not playing");
192   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
193 }
194
195 GST_END_TEST;
196
197
198 /* test: try changing state of sinks */
199 Suite *
200 gst_object_suite (void)
201 {
202   Suite *s = suite_create ("Sinks");
203   TCase *tc_chain = tcase_create ("general");
204
205   suite_add_tcase (s, tc_chain);
206   tcase_add_test (tc_chain, test_sink);
207   tcase_add_test (tc_chain, test_src_sink);
208   tcase_add_test (tc_chain, test_livesrc_remove);
209   tcase_add_test (tc_chain, test_livesrc_sink);
210
211   return s;
212 }
213
214 int
215 main (int argc, char **argv)
216 {
217   int nf;
218
219   Suite *s = gst_object_suite ();
220   SRunner *sr = srunner_create (s);
221
222   gst_check_init (&argc, &argv);
223
224   srunner_run_all (sr, CK_NORMAL);
225   nf = srunner_ntests_failed (sr);
226   srunner_free (sr);
227
228   return nf;
229 }