*.c: Don't cast to GST_OBJECT when reffing or unreffing. Large source-munging commit!!!
[platform/upstream/gstreamer.git] / tests / old / testsuite / states / bin.c
1 /* GStreamer
2  * Copyright (C) <2004> Benjamin Otte <otte@gnome.org>
3  *
4  * bin.c: 
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/gst.h>
23
24 #define RETURN_NAME(x) ((x) == GST_STATE_SUCCESS ? "GST_STATE_SUCCESS" : \
25     (x) == GST_STATE_ASYNC ? "GST_STATE_ASYNC" : "GST_STATE_FAILURE")
26 static void
27 assert_state (GstElement * element, GstElementState state)
28 {
29   if (gst_element_get_state (element) != state) {
30     g_printerr ("%s: state is %s instead of %s",
31         GST_OBJECT_NAME (element),
32         gst_element_state_get_name (GST_STATE (element)),
33         gst_element_state_get_name (state));
34     g_assert_not_reached ();
35   }
36 }
37
38 static void
39 assert_state_change (GstElement * element, GstElementState new_state,
40     GstElementStateReturn result, GstElementState result_state)
41 {
42   GstElementStateReturn ret = gst_element_set_state (element, new_state);
43
44   if (ret != result) {
45     g_printerr ("%s: change state to %s returned %s instead of %s",
46         GST_OBJECT_NAME (element), gst_element_state_get_name (new_state),
47         RETURN_NAME (ret), RETURN_NAME (result));
48     g_assert_not_reached ();
49   }
50   assert_state (element, result_state);
51 }
52
53 static void
54 empty_bin (gchar * bin_name)
55 {
56   /* Test the behaviour of empty bins. Since a bin's state is always the state
57    * of its highest child, nothing should change in here
58    * Return values when no error occured but the state didn't change should be
59    * GST_STATE_ASYNC */
60   GstElement *bin = gst_element_factory_make (bin_name, NULL);
61
62   /* obvious */
63   assert_state (bin, GST_STATE_NULL);
64   /* see above */
65   assert_state_change (bin, GST_STATE_READY, GST_STATE_ASYNC, GST_STATE_NULL);
66   assert_state_change (bin, GST_STATE_PAUSED, GST_STATE_ASYNC, GST_STATE_NULL);
67   assert_state_change (bin, GST_STATE_PLAYING, GST_STATE_ASYNC, GST_STATE_NULL);
68 }
69
70 static void
71 test_adding_one_element (GstElement * bin)
72 {
73   /* Tests behaviour of adding/removing elements to/from bins. It makes sure the
74    * state of the bin is always the highest of all contained children. */
75   GstElementState test_states[] = { GST_STATE_READY, GST_STATE_PAUSED,
76     GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY, GST_STATE_NULL
77   };
78   GstElement *test = gst_element_factory_make ("identity", NULL);
79   GstElementState bin_state = gst_element_get_state (bin);
80   gint i;
81
82   g_assert (test);
83   gst_object_ref (test);
84   assert_state (test, GST_STATE_NULL);
85   gst_bin_add (GST_BIN (bin), test);
86   assert_state (bin, MAX (bin_state, GST_STATE_NULL));
87   for (i = 0; i < G_N_ELEMENTS (test_states); i++) {
88     GstElementState test_state = test_states[i];
89
90     assert_state_change (test, test_state, GST_STATE_SUCCESS, test_state);
91     assert_state (test, test_state);
92     assert_state (bin, MAX (bin_state, test_state));
93     gst_bin_remove (GST_BIN (bin), test);
94     assert_state (bin, bin_state);
95     gst_bin_add (GST_BIN (bin), test);
96     assert_state (test, test_state);
97     assert_state (bin, MAX (bin_state, test_state));
98   }
99   gst_bin_remove (GST_BIN (bin), test);
100   gst_object_unref (test);
101   assert_state (bin, bin_state);
102 }
103
104 static void
105 test_element_in_bin (gchar * bin_name)
106 {
107   gint i;
108   GstElementState test_states[] = { GST_STATE_NULL, GST_STATE_READY,
109     GST_STATE_PAUSED, GST_STATE_PLAYING
110   };
111   GstElement *id, *bin = gst_element_factory_make (bin_name, NULL);
112
113   g_assert (bin);
114
115   /* test correct behaviour in empty bins */
116   test_adding_one_element (bin);
117
118   id = gst_element_factory_make ("identity", NULL);
119   g_assert (id);
120   assert_state (id, GST_STATE_NULL);
121   gst_bin_add (GST_BIN (bin), id);
122   /* test correct behaviour in bins which contain elements in various states */
123   for (i = 0; i < G_N_ELEMENTS (test_states); i++) {
124     GstElementState test_state = test_states[i];
125
126     assert_state_change (bin, test_state, GST_STATE_SUCCESS, test_state);
127     assert_state (id, test_state);
128     test_adding_one_element (bin);
129   }
130
131   gst_object_unref (bin);
132 }
133
134 gint
135 main (gint argc, gchar * argv[])
136 {
137   gst_init (&argc, &argv);
138
139   /* test behaviour of empty bins */
140   empty_bin ("bin");
141   empty_bin ("thread");
142   empty_bin ("pipeline");
143
144   /* test behaviour of adding/removing elements to/from all core bin types */
145   test_element_in_bin ("bin");
146   test_element_in_bin ("thread");
147   test_element_in_bin ("pipeline");
148
149   return 0;
150 }