66ae5f344d832746a5ec61160ac49973e828870d
[platform/upstream/gstreamer.git] / tests / check / pipelines / simple-launch-lines.c
1 /* GStreamer
2  * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3  *
4  * simple_launch_lines.c: Unit test for simple pipelines
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
23 #include <gst/check/gstcheck.h>
24
25
26 static GstElement *
27 setup_pipeline (gchar * pipe_descr)
28 {
29   GstElement *pipeline;
30
31   pipeline = gst_parse_launch (pipe_descr, NULL);
32   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
33   return pipeline;
34 }
35
36 /*
37  * run_pipeline:
38  * @pipe: the pipeline to run
39  * @desc: the description for use in messages
40  * @events: is a mask of expected events
41  * @tevent: is the expected terminal event.
42  *
43  * the poll call will time out after half a second.
44  */
45 static void
46 run_pipeline (GstElement * pipeline, gchar * descr,
47     GstMessageType events, GstMessageType tevent)
48 {
49   GstBus *bus;
50   GstMessageType revent;
51   GstStateChangeReturn ret;
52
53   g_assert (pipeline);
54   bus = gst_element_get_bus (pipeline);
55   g_assert (bus);
56
57   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
58   ret = gst_element_get_state (pipeline, NULL, NULL, NULL);
59
60   if (ret != GST_STATE_CHANGE_SUCCESS) {
61     g_critical ("Couldn't set pipeline to PLAYING");
62     goto done;
63   }
64
65   while (1) {
66     GstMessage *message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 2);
67
68     if (message) {
69       revent = GST_MESSAGE_TYPE (message);
70       gst_message_unref (message);
71     } else {
72       revent = GST_MESSAGE_UNKNOWN;
73     }
74
75     if (revent == tevent) {
76       break;
77     } else if (revent == GST_MESSAGE_UNKNOWN) {
78       g_critical ("Unexpected timeout in gst_bus_poll, looking for %d: %s",
79           tevent, descr);
80       break;
81     } else if (revent & events) {
82       continue;
83     }
84     g_critical ("Unexpected message received of type %d, looking for %d: %s",
85         revent, tevent, descr);
86   }
87
88 done:
89   gst_element_set_state (pipeline, GST_STATE_NULL);
90   gst_object_unref (pipeline);
91   gst_object_unref (bus);
92 }
93
94 GST_START_TEST (test_2_elements)
95 {
96   gchar *s;
97
98   s = "fakesrc can-activate-push=false ! fakesink can-activate-pull=true";
99   run_pipeline (setup_pipeline (s), s,
100       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_UNKNOWN);
101
102   s = "fakesrc can-activate-push=true ! fakesink can-activate-pull=false";
103   run_pipeline (setup_pipeline (s), s,
104       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_UNKNOWN);
105
106   s = "fakesrc can-activate-push=false num-buffers=10 ! fakesink can-activate-pull=true";
107   run_pipeline (setup_pipeline (s), s,
108       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_EOS);
109
110   s = "fakesrc can-activate-push=true num-buffers=10 ! fakesink can-activate-pull=false";
111   run_pipeline (setup_pipeline (s), s,
112       GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_EOS);
113
114   s = "fakesrc can-activate-push=false ! fakesink can-activate-pull=false";
115   ASSERT_CRITICAL (run_pipeline (setup_pipeline (s), s,
116           GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED,
117           GST_MESSAGE_UNKNOWN));
118 }
119
120 GST_END_TEST;
121
122 static void
123 got_handoff (GstElement * sink, GstBuffer * buf, GstPad * pad, gpointer unused)
124 {
125   gst_element_post_message
126       (sink, gst_message_new_application (NULL, gst_structure_new ("foo",
127               NULL)));
128 }
129
130 static void
131 assert_live_count (GType type, gint live)
132 {
133   GstAllocTrace *trace;
134   const gchar *name;
135
136   if (gst_alloc_trace_available ()) {
137     name = g_type_name (type);
138     g_assert (name);
139     trace = gst_alloc_trace_get (name);
140     if (trace) {
141       g_return_if_fail (trace->live == live);
142     }
143   } else {
144     g_print ("\nSkipping live count tests; recompile with traces to enable\n");
145   }
146 }
147
148 GST_START_TEST (test_stop_from_app)
149 {
150   GstElement *fakesrc, *fakesink, *pipeline;
151   GstBus *bus;
152   GstMessageType revent;
153   GstMessage *message;
154
155   assert_live_count (GST_TYPE_BUFFER, 0);
156
157   fakesrc = gst_element_factory_make ("fakesrc", NULL);
158   fakesink = gst_element_factory_make ("fakesink", NULL);
159   pipeline = gst_element_factory_make ("pipeline", NULL);
160
161   g_return_if_fail (fakesrc && fakesink && pipeline);
162
163   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
164   gst_element_link (fakesrc, fakesink);
165
166   g_object_set (fakesink, "signal-handoffs", (gboolean) TRUE, NULL);
167   g_signal_connect (fakesink, "handoff", G_CALLBACK (got_handoff), NULL);
168
169   gst_element_set_state (pipeline, GST_STATE_PLAYING);
170
171   bus = gst_element_get_bus (pipeline);
172   g_assert (bus);
173
174   /* will time out after half a second */
175   message = gst_bus_poll (bus, GST_MESSAGE_APPLICATION, GST_SECOND / 2);
176   if (message) {
177     revent = GST_MESSAGE_TYPE (message);
178     gst_message_unref (message);
179   } else {
180     revent = GST_MESSAGE_UNKNOWN;
181   }
182   g_return_if_fail (revent == GST_MESSAGE_APPLICATION);
183
184   gst_element_set_state (pipeline, GST_STATE_NULL);
185   gst_object_unref (pipeline);
186   gst_object_unref (bus);
187
188   assert_live_count (GST_TYPE_BUFFER, 0);
189 }
190 GST_END_TEST Suite *
191 simple_launch_lines_suite (void)
192 {
193   Suite *s = suite_create ("Pipelines");
194   TCase *tc_chain = tcase_create ("linear");
195
196   /* time out after 20s, not the default 3 */
197   tcase_set_timeout (tc_chain, 0);
198
199   suite_add_tcase (s, tc_chain);
200   tcase_add_test (tc_chain, test_2_elements);
201   tcase_add_test (tc_chain, test_stop_from_app);
202   return s;
203 }
204
205 int
206 main (int argc, char **argv)
207 {
208   int nf;
209
210   Suite *s = simple_launch_lines_suite ();
211   SRunner *sr = srunner_create (s);
212
213   gst_check_init (&argc, &argv);
214
215   srunner_run_all (sr, CK_NORMAL);
216   nf = srunner_ntests_failed (sr);
217   srunner_free (sr);
218
219   return nf;
220 }