bin: undo upward state changes on children when a child fails
[platform/upstream/gstreamer.git] / tests / check / generic / states.c
1 /* GStreamer
2  *
3  * unit test for state changes on all elements
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <unistd.h>
28
29 #include <gst/check/gstcheck.h>
30
31 static GList *elements = NULL;
32
33 static void
34 setup (void)
35 {
36   GList *features, *f;
37   GList *plugins, *p;
38   gchar **ignorelist = NULL;
39   const gchar *STATE_IGNORE_ELEMENTS = NULL;
40
41   GST_DEBUG ("getting elements for package %s", PACKAGE);
42   STATE_IGNORE_ELEMENTS = g_getenv ("GST_STATE_IGNORE_ELEMENTS");
43   if (!g_getenv ("GST_NO_STATE_IGNORE_ELEMENTS") && STATE_IGNORE_ELEMENTS) {
44     GST_DEBUG ("Will ignore element factories: '%s'", STATE_IGNORE_ELEMENTS);
45     ignorelist = g_strsplit (STATE_IGNORE_ELEMENTS, " ", 0);
46   }
47
48   plugins = gst_registry_get_plugin_list (gst_registry_get ());
49
50   for (p = plugins; p; p = p->next) {
51     GstPlugin *plugin = p->data;
52
53     if (strcmp (gst_plugin_get_source (plugin), PACKAGE) != 0)
54       continue;
55
56     features =
57         gst_registry_get_feature_list_by_plugin (gst_registry_get (),
58         gst_plugin_get_name (plugin));
59
60     for (f = features; f; f = f->next) {
61       GstPluginFeature *feature = f->data;
62       const gchar *name;
63       gboolean ignore = FALSE;
64
65       if (!GST_IS_ELEMENT_FACTORY (feature))
66         continue;
67
68       name = GST_OBJECT_NAME (feature);
69
70       if (ignorelist) {
71         gchar **s;
72
73         for (s = ignorelist; s && *s; ++s) {
74           if (g_str_has_prefix (name, *s)) {
75             GST_DEBUG ("ignoring element %s", name);
76             ignore = TRUE;
77           }
78         }
79         if (ignore)
80           continue;
81       }
82
83       GST_DEBUG ("adding element %s", name);
84       elements = g_list_prepend (elements, g_strdup (name));
85     }
86     gst_plugin_feature_list_free (features);
87   }
88   gst_plugin_list_free (plugins);
89   g_strfreev (ignorelist);
90 }
91
92 static void
93 teardown (void)
94 {
95   GList *e;
96
97   for (e = elements; e; e = e->next) {
98     g_free (e->data);
99   }
100   g_list_free (elements);
101   elements = NULL;
102 }
103
104
105 GST_START_TEST (test_state_changes_up_and_down_seq)
106 {
107   GstElement *element;
108   GList *e;
109
110   for (e = elements; e; e = e->next) {
111     const gchar *name = e->data;
112
113     GST_DEBUG ("testing element %s", name);
114     element = gst_element_factory_make (name, name);
115     fail_if (element == NULL, "Could not make element from factory %s", name);
116
117     if (GST_IS_PIPELINE (element)) {
118       GST_DEBUG ("element %s is a pipeline", name);
119     }
120
121     gst_element_set_state (element, GST_STATE_READY);
122     gst_element_set_state (element, GST_STATE_PAUSED);
123     gst_element_set_state (element, GST_STATE_PLAYING);
124     gst_element_set_state (element, GST_STATE_PAUSED);
125     gst_element_set_state (element, GST_STATE_READY);
126     gst_element_set_state (element, GST_STATE_NULL);
127     gst_element_set_state (element, GST_STATE_PAUSED);
128     gst_element_set_state (element, GST_STATE_READY);
129     gst_element_set_state (element, GST_STATE_PLAYING);
130     gst_element_set_state (element, GST_STATE_PAUSED);
131     gst_element_set_state (element, GST_STATE_NULL);
132     gst_object_unref (GST_OBJECT (element));
133   }
134 }
135
136 GST_END_TEST;
137
138 GST_START_TEST (test_state_changes_up_seq)
139 {
140   GstElement *element;
141   GList *e;
142
143   for (e = elements; e; e = e->next) {
144     const gchar *name = e->data;
145
146     GST_DEBUG ("testing element %s", name);
147     element = gst_element_factory_make (name, name);
148     fail_if (element == NULL, "Could not make element from factory %s", name);
149
150     if (GST_IS_PIPELINE (element)) {
151       GST_DEBUG ("element %s is a pipeline", name);
152     }
153
154     gst_element_set_state (element, GST_STATE_READY);
155
156     gst_element_set_state (element, GST_STATE_PAUSED);
157     gst_element_set_state (element, GST_STATE_READY);
158
159     gst_element_set_state (element, GST_STATE_PAUSED);
160     gst_element_set_state (element, GST_STATE_PLAYING);
161     gst_element_set_state (element, GST_STATE_PAUSED);
162     gst_element_set_state (element, GST_STATE_READY);
163
164     gst_element_set_state (element, GST_STATE_NULL);
165     gst_object_unref (GST_OBJECT (element));
166   }
167 }
168
169 GST_END_TEST;
170
171 GST_START_TEST (test_state_changes_down_seq)
172 {
173   GstElement *element;
174   GList *e;
175
176   for (e = elements; e; e = e->next) {
177     const gchar *name = e->data;
178
179     GST_DEBUG ("testing element %s", name);
180     element = gst_element_factory_make (name, name);
181     fail_if (element == NULL, "Could not make element from factory %s", name);
182
183     if (GST_IS_PIPELINE (element)) {
184       GST_DEBUG ("element %s is a pipeline", name);
185     }
186
187     gst_element_set_state (element, GST_STATE_READY);
188     gst_element_set_state (element, GST_STATE_PAUSED);
189     gst_element_set_state (element, GST_STATE_PLAYING);
190
191     gst_element_set_state (element, GST_STATE_PAUSED);
192     gst_element_set_state (element, GST_STATE_PLAYING);
193
194     gst_element_set_state (element, GST_STATE_PAUSED);
195     gst_element_set_state (element, GST_STATE_READY);
196     gst_element_set_state (element, GST_STATE_PAUSED);
197     gst_element_set_state (element, GST_STATE_PLAYING);
198
199     gst_element_set_state (element, GST_STATE_PAUSED);
200     gst_element_set_state (element, GST_STATE_READY);
201     gst_element_set_state (element, GST_STATE_NULL);
202     gst_object_unref (GST_OBJECT (element));
203   }
204 }
205
206 GST_END_TEST;
207
208 static gboolean
209 element_state_is (GstElement * e, GstState s)
210 {
211   GstStateChangeReturn ret;
212   GstState state;
213
214   ret = gst_element_get_state (e, &state, NULL, GST_CLOCK_TIME_NONE);
215   return (ret == GST_STATE_CHANGE_SUCCESS && state == s);
216 }
217
218 GST_START_TEST (test_state_changes_up_failure)
219 {
220   GstElement *bin;
221   GstElement *mid[3];
222   int n;
223
224   /* we want at least one before and one after */
225   g_assert (G_N_ELEMENTS (mid) >= 3);
226
227   /* make a bin */
228   bin = gst_element_factory_make ("bin", NULL);
229
230   /* add children */
231   for (n = 0; n < G_N_ELEMENTS (mid); ++n) {
232     const char *element = n != 1 ? "identity" : "fakesink";
233     mid[n] = gst_element_factory_make (element, NULL);
234     gst_bin_add (GST_BIN (bin), mid[n]);
235     if (n == 1)
236       g_object_set (mid[n], "async", FALSE, NULL);
237   }
238
239   /* This one should work */
240   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
241     fail_unless (element_state_is (mid[n], GST_STATE_NULL));
242   gst_element_set_state (bin, GST_STATE_READY);
243   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
244     fail_unless (element_state_is (mid[n], GST_STATE_READY));
245   gst_element_set_state (bin, GST_STATE_NULL);
246   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
247     fail_unless (element_state_is (mid[n], GST_STATE_NULL));
248
249   /* make the middle element fail to switch up */
250   g_object_set (mid[1], "state-error", 1 /* null-to-ready */ , NULL);
251
252   /* This one should not */
253   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
254     fail_unless (element_state_is (mid[n], GST_STATE_NULL));
255   gst_element_set_state (bin, GST_STATE_READY);
256   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
257     fail_unless (element_state_is (mid[n], GST_STATE_NULL));
258   gst_element_set_state (bin, GST_STATE_NULL);
259   for (n = 0; n < G_N_ELEMENTS (mid); ++n)
260     fail_unless (element_state_is (mid[n], GST_STATE_NULL));
261
262   /* cleanup */
263   gst_object_unref (bin);
264 }
265
266 GST_END_TEST;
267
268
269 static Suite *
270 states_suite (void)
271 {
272   Suite *s = suite_create ("states");
273   TCase *tc_chain = tcase_create ("general");
274
275   suite_add_tcase (s, tc_chain);
276   tcase_add_checked_fixture (tc_chain, setup, teardown);
277   tcase_add_test (tc_chain, test_state_changes_up_and_down_seq);
278   tcase_add_test (tc_chain, test_state_changes_up_seq);
279   tcase_add_test (tc_chain, test_state_changes_down_seq);
280   tcase_add_test (tc_chain, test_state_changes_up_failure);
281
282   return s;
283 }
284
285 GST_CHECK_MAIN (states);