ed88eebc9da851d2d0bcdc73a676321f2e925cae
[platform/upstream/gstreamer.git] / check / states / 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 "../gstcheck.h"
24
25 /* a sink should go ASYNC to PAUSE. forcing PLAYING is possible */
26 START_TEST (test_sink)
27 {
28   GstElement *sink;
29   GstElementStateReturn ret;
30   GstElementState current, pending;
31
32   sink = gst_element_factory_make ("fakesink", "sink");
33
34   ret = gst_element_set_state (sink, GST_STATE_PAUSED);
35   fail_unless (ret == GST_STATE_ASYNC, "no async state return");
36
37   ret = gst_element_set_state (sink, GST_STATE_PLAYING);
38   fail_unless (ret == GST_STATE_SUCCESS, "cannot force play");
39
40   ret = gst_element_get_state (sink, &current, &pending, NULL);
41   fail_unless (ret == GST_STATE_SUCCESS, "not playing");
42   fail_unless (current == GST_STATE_PLAYING, "not playing");
43   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
44 }
45
46 END_TEST
47 /* a sink should go ASYNC to PAUSE. PAUSE should complete when
48  * prerolled. */
49 START_TEST (test_src_sink)
50 {
51   GstElement *sink, *src, *pipeline;
52   GstElementStateReturn ret;
53   GstElementState current, pending;
54   GstPad *srcpad, *sinkpad;
55
56   pipeline = gst_pipeline_new ("pipeline");
57   src = gst_element_factory_make ("fakesrc", "src");
58   sink = gst_element_factory_make ("fakesink", "sink");
59
60   gst_bin_add (GST_BIN (pipeline), src);
61   gst_bin_add (GST_BIN (pipeline), sink);
62
63   srcpad = gst_element_get_pad (src, "src");
64   sinkpad = gst_element_get_pad (sink, "sink");
65   gst_pad_link (srcpad, sinkpad);
66   gst_object_unref (GST_OBJECT (srcpad));
67   gst_object_unref (GST_OBJECT (sinkpad));
68
69   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
70   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
71   fail_unless (ret == GST_STATE_SUCCESS, "no success state return");
72
73   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
74   fail_unless (ret == GST_STATE_SUCCESS, "cannot start play");
75
76   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
77   fail_unless (ret == GST_STATE_SUCCESS, "not playing");
78   fail_unless (current == GST_STATE_PLAYING, "not playing");
79   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
80 }
81
82 END_TEST
83 /* a pipeline with live source should return NO_PREROLL in
84  * PAUSE. When removing the live source it should return ASYNC
85  * from the sink */
86 START_TEST (test_livesrc_remove)
87 {
88   GstElement *sink, *src, *pipeline;
89   GstElementStateReturn ret;
90   GstElementState current, pending;
91   GstPad *srcpad, *sinkpad;
92   GTimeVal tv;
93
94   pipeline = gst_pipeline_new ("pipeline");
95   src = gst_element_factory_make ("fakesrc", "src");
96   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
97   sink = gst_element_factory_make ("fakesink", "sink");
98
99   gst_bin_add (GST_BIN (pipeline), src);
100   gst_bin_add (GST_BIN (pipeline), sink);
101
102   srcpad = gst_element_get_pad (src, "src");
103   sinkpad = gst_element_get_pad (sink, "sink");
104   gst_pad_link (srcpad, sinkpad);
105   gst_object_unref (GST_OBJECT (srcpad));
106   gst_object_unref (GST_OBJECT (sinkpad));
107
108   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
109   fail_unless (ret == GST_STATE_NO_PREROLL, "no no_preroll state return");
110
111   ret = gst_element_get_state (src, &current, &pending, NULL);
112   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
113   fail_unless (current == GST_STATE_PAUSED, "not paused");
114   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
115
116   gst_bin_remove (GST_BIN (pipeline), src);
117
118   GST_TIME_TO_TIMEVAL (0, tv);
119   ret = gst_element_get_state (pipeline, &current, &pending, &tv);
120   fail_unless (ret == GST_STATE_ASYNC, "not async");
121   fail_unless (current == GST_STATE_PAUSED, "not paused");
122   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
123
124 }
125
126 END_TEST
127 /* a sink should go ASYNC to PAUSE. PAUSE does not complete
128  * since we have a live source. */
129 START_TEST (test_livesrc_sink)
130 {
131   GstElement *sink, *src, *pipeline;
132   GstElementStateReturn ret;
133   GstElementState current, pending;
134   GstPad *srcpad, *sinkpad;
135
136   pipeline = gst_pipeline_new ("pipeline");
137   src = gst_element_factory_make ("fakesrc", "src");
138   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
139   sink = gst_element_factory_make ("fakesink", "sink");
140
141   gst_bin_add (GST_BIN (pipeline), src);
142   gst_bin_add (GST_BIN (pipeline), sink);
143
144   srcpad = gst_element_get_pad (src, "src");
145   sinkpad = gst_element_get_pad (sink, "sink");
146   gst_pad_link (srcpad, sinkpad);
147   gst_object_unref (GST_OBJECT (srcpad));
148   gst_object_unref (GST_OBJECT (sinkpad));
149
150   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
151   fail_unless (ret == GST_STATE_NO_PREROLL, "no no_preroll state return");
152
153   ret = gst_element_get_state (src, &current, &pending, NULL);
154   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
155   fail_unless (current == GST_STATE_PAUSED, "not paused");
156   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
157
158   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
159   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
160   fail_unless (current == GST_STATE_PAUSED, "not paused");
161   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
162
163   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
164   fail_unless (ret == GST_STATE_SUCCESS, "cannot force play");
165
166   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
167   fail_unless (ret == GST_STATE_SUCCESS, "not playing");
168   fail_unless (current == GST_STATE_PLAYING, "not playing");
169   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
170 }
171
172 END_TEST
173 /* test: try changing state of sinks */
174     Suite * gst_object_suite (void)
175 {
176   Suite *s = suite_create ("Sinks");
177   TCase *tc_chain = tcase_create ("general");
178
179   /* turn off timeout */
180   tcase_set_timeout (tc_chain, 60);
181
182   suite_add_tcase (s, tc_chain);
183   tcase_add_test (tc_chain, test_sink);
184   tcase_add_test (tc_chain, test_src_sink);
185   tcase_add_test (tc_chain, test_livesrc_remove);
186   tcase_add_test (tc_chain, test_livesrc_sink);
187
188   return s;
189 }
190
191 int
192 main (int argc, char **argv)
193 {
194   int nf;
195
196   Suite *s = gst_object_suite ();
197   SRunner *sr = srunner_create (s);
198
199   gst_check_init (&argc, &argv);
200
201   srunner_run_all (sr, CK_NORMAL);
202   nf = srunner_ntests_failed (sr);
203   srunner_free (sr);
204
205   return nf;
206 }