Added support for live sources and other elements that cannot do preroll.
[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 "../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   fail_unless (ret == GST_STATE_SUCCESS, "no success state return");
71
72   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
73   fail_unless (ret == GST_STATE_SUCCESS, "cannot start play");
74
75   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
76   fail_unless (ret == GST_STATE_SUCCESS, "not playing");
77   fail_unless (current == GST_STATE_PLAYING, "not playing");
78   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
79 }
80
81 END_TEST
82 /* a pipeline with live source should return NO_PREROLL in
83  * PAUSE. When removing the live source it should return ASYNC
84  * from the sink */
85 START_TEST (test_livesrc_remove)
86 {
87   GstElement *sink, *src, *pipeline;
88   GstElementStateReturn ret;
89   GstElementState current, pending;
90   GstPad *srcpad, *sinkpad;
91   GTimeVal tv;
92
93   pipeline = gst_pipeline_new ("pipeline");
94   src = gst_element_factory_make ("fakesrc", "src");
95   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
96   sink = gst_element_factory_make ("fakesink", "sink");
97
98   gst_bin_add (GST_BIN (pipeline), src);
99   gst_bin_add (GST_BIN (pipeline), sink);
100
101   srcpad = gst_element_get_pad (src, "src");
102   sinkpad = gst_element_get_pad (sink, "sink");
103   gst_pad_link (srcpad, sinkpad);
104   gst_object_unref (GST_OBJECT (srcpad));
105   gst_object_unref (GST_OBJECT (sinkpad));
106
107   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
108   fail_unless (ret == GST_STATE_NO_PREROLL, "no no_preroll state return");
109
110   ret = gst_element_get_state (src, &current, &pending, NULL);
111   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
112   fail_unless (current == GST_STATE_PAUSED, "not paused");
113   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
114
115   gst_bin_remove (GST_BIN (pipeline), src);
116
117   GST_TIME_TO_TIMEVAL (0, tv);
118   ret = gst_element_get_state (pipeline, &current, &pending, &tv);
119   fail_unless (ret == GST_STATE_ASYNC, "not async");
120   fail_unless (current == GST_STATE_PAUSED, "not paused");
121   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
122
123 }
124
125 END_TEST
126 /* a sink should go ASYNC to PAUSE. PAUSE does not complete
127  * since we have a live source. */
128 START_TEST (test_livesrc_sink)
129 {
130   GstElement *sink, *src, *pipeline;
131   GstElementStateReturn ret;
132   GstElementState current, pending;
133   GstPad *srcpad, *sinkpad;
134
135   pipeline = gst_pipeline_new ("pipeline");
136   src = gst_element_factory_make ("fakesrc", "src");
137   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
138   sink = gst_element_factory_make ("fakesink", "sink");
139
140   gst_bin_add (GST_BIN (pipeline), src);
141   gst_bin_add (GST_BIN (pipeline), sink);
142
143   srcpad = gst_element_get_pad (src, "src");
144   sinkpad = gst_element_get_pad (sink, "sink");
145   gst_pad_link (srcpad, sinkpad);
146   gst_object_unref (GST_OBJECT (srcpad));
147   gst_object_unref (GST_OBJECT (sinkpad));
148
149   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
150   fail_unless (ret == GST_STATE_NO_PREROLL, "no no_preroll state return");
151
152   ret = gst_element_get_state (src, &current, &pending, NULL);
153   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
154   fail_unless (current == GST_STATE_PAUSED, "not paused");
155   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
156
157   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
158   fail_unless (ret == GST_STATE_NO_PREROLL, "not paused");
159   fail_unless (current == GST_STATE_PAUSED, "not paused");
160   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
161
162   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
163   fail_unless (ret == GST_STATE_SUCCESS, "cannot force play");
164
165   ret = gst_element_get_state (pipeline, &current, &pending, NULL);
166   fail_unless (ret == GST_STATE_SUCCESS, "not playing");
167   fail_unless (current == GST_STATE_PLAYING, "not playing");
168   fail_unless (pending == GST_STATE_VOID_PENDING, "not playing");
169 }
170
171 END_TEST
172 /* test: try changing state of sinks */
173     Suite * gst_object_suite (void)
174 {
175   Suite *s = suite_create ("Sinks");
176   TCase *tc_chain = tcase_create ("general");
177
178   /* turn off timeout */
179   tcase_set_timeout (tc_chain, 60);
180
181   suite_add_tcase (s, tc_chain);
182   tcase_add_test (tc_chain, test_sink);
183   tcase_add_test (tc_chain, test_src_sink);
184   tcase_add_test (tc_chain, test_livesrc_remove);
185   tcase_add_test (tc_chain, test_livesrc_sink);
186
187   return s;
188 }
189
190 int
191 main (int argc, char **argv)
192 {
193   int nf;
194
195   Suite *s = gst_object_suite ();
196   SRunner *sr = srunner_create (s);
197
198   gst_check_init (&argc, &argv);
199
200   srunner_run_all (sr, CK_NORMAL);
201   nf = srunner_ntests_failed (sr);
202   srunner_free (sr);
203
204   return nf;
205 }