gst/gstelement.h (GstState): Renamed from GstElementState, changed to be a normal...
[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_PAUSED, "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   gst_object_unref (sink);
55 }
56
57 GST_END_TEST
58 /* a sink should go ASYNC to PAUSE. PAUSE should complete when
59  * prerolled. */
60 GST_START_TEST (test_src_sink)
61 {
62   GstElement *sink, *src, *pipeline;
63   GstStateChangeReturn ret;
64   GstState current, pending;
65   GstPad *srcpad, *sinkpad;
66
67   pipeline = gst_pipeline_new ("pipeline");
68   src = gst_element_factory_make ("fakesrc", "src");
69   sink = gst_element_factory_make ("fakesink", "sink");
70
71   gst_bin_add (GST_BIN (pipeline), src);
72   gst_bin_add (GST_BIN (pipeline), sink);
73
74   srcpad = gst_element_get_pad (src, "src");
75   sinkpad = gst_element_get_pad (sink, "sink");
76   gst_pad_link (srcpad, sinkpad);
77   gst_object_unref (srcpad);
78   gst_object_unref (sinkpad);
79
80   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
81   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
82   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "no success state return");
83
84   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
85   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot start play");
86
87   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
88   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "not playing");
89   fail_unless (current == GST_STATE_PLAYING, "not playing");
90   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
91 }
92
93 GST_END_TEST
94 /* a pipeline with live source should return NO_PREROLL in
95  * PAUSE. When removing the live source it should return ASYNC
96  * from the sink */
97 GST_START_TEST (test_livesrc_remove)
98 {
99   GstElement *sink, *src, *pipeline;
100   GstStateChangeReturn ret;
101   GstState current, pending;
102   GstPad *srcpad, *sinkpad;
103   GTimeVal tv;
104
105   pipeline = gst_pipeline_new ("pipeline");
106   src = gst_element_factory_make ("fakesrc", "src");
107   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
108   sink = gst_element_factory_make ("fakesink", "sink");
109
110   gst_bin_add (GST_BIN (pipeline), src);
111   gst_bin_add (GST_BIN (pipeline), sink);
112
113   srcpad = gst_element_get_pad (src, "src");
114   sinkpad = gst_element_get_pad (sink, "sink");
115   gst_pad_link (srcpad, sinkpad);
116   gst_object_unref (srcpad);
117   gst_object_unref (sinkpad);
118
119   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
120   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
121       "no no_preroll state return");
122
123   ret = gst_element_get_state (src, &current, &pending, NULL);
124   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
125   fail_unless (current == GST_STATE_PAUSED, "not paused");
126   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
127
128   gst_bin_remove (GST_BIN (pipeline), src);
129
130   GST_TIME_TO_TIMEVAL (0, tv);
131   ret = gst_element_get_state (pipeline, &current, &pending, &tv);
132   fail_unless (ret == GST_STATE_CHANGE_ASYNC, "not async");
133   fail_unless (current == GST_STATE_PAUSED, "not paused");
134   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
135
136 }
137
138 GST_END_TEST
139 /* a sink should go ASYNC to PAUSE. PAUSE does not complete
140  * since we have a live source. */
141 GST_START_TEST (test_livesrc_sink)
142 {
143   GstElement *sink, *src, *pipeline;
144   GstStateChangeReturn ret;
145   GstState current, pending;
146   GstPad *srcpad, *sinkpad;
147
148   pipeline = gst_pipeline_new ("pipeline");
149   src = gst_element_factory_make ("fakesrc", "src");
150   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
151   sink = gst_element_factory_make ("fakesink", "sink");
152
153   gst_bin_add (GST_BIN (pipeline), src);
154   gst_bin_add (GST_BIN (pipeline), sink);
155
156   srcpad = gst_element_get_pad (src, "src");
157   sinkpad = gst_element_get_pad (sink, "sink");
158   gst_pad_link (srcpad, sinkpad);
159   gst_object_unref (srcpad);
160   gst_object_unref (sinkpad);
161
162   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
163   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
164       "no no_preroll state return");
165
166   ret = gst_element_get_state (src, &current, &pending, NULL);
167   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
168   fail_unless (current == GST_STATE_PAUSED, "not paused");
169   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
170
171   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
172   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL, "not paused");
173   fail_unless (current == GST_STATE_PAUSED, "not paused");
174   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
175
176   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
177   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "cannot force play");
178
179   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
180   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "not playing");
181   fail_unless (current == GST_STATE_PLAYING, "not playing");
182   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
183 }
184
185 GST_END_TEST;
186
187
188 /* test: try changing state of sinks */
189 Suite *
190 gst_object_suite (void)
191 {
192   Suite *s = suite_create ("Sinks");
193   TCase *tc_chain = tcase_create ("general");
194
195   suite_add_tcase (s, tc_chain);
196   tcase_add_test (tc_chain, test_sink);
197   tcase_add_test (tc_chain, test_src_sink);
198   tcase_add_test (tc_chain, test_livesrc_remove);
199   tcase_add_test (tc_chain, test_livesrc_sink);
200
201   return s;
202 }
203
204 int
205 main (int argc, char **argv)
206 {
207   int nf;
208
209   Suite *s = gst_object_suite ();
210   SRunner *sr = srunner_create (s);
211
212   gst_check_init (&argc, &argv);
213
214   srunner_run_all (sr, CK_NORMAL);
215   nf = srunner_ntests_failed (sr);
216   srunner_free (sr);
217
218   return nf;
219 }