2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * simple_launch_lines.c: Unit test for simple pipelines
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.
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.
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.
23 #include <gst/check/gstcheck.h>
27 setup_pipeline (gchar * pipe_descr)
31 pipeline = gst_parse_launch (pipe_descr, NULL);
32 fail_unless (GST_IS_PIPELINE (pipeline));
38 * @pipe: the pipeline to run
39 * @desc: the description for use in messages
40 * @message_types: is a mask of expected message_types
41 * @tmessage: is the expected terminal message
43 * the poll call will time out after half a second.
46 run_pipeline (GstElement * pipeline, gchar * descr,
47 GstMessageType message_types, GstMessageType tmessage)
50 GstMessageType rmessage;
51 GstStateChangeReturn ret;
53 fail_if (pipeline == NULL);
54 bus = gst_element_get_bus (pipeline);
55 fail_if (bus == NULL);
57 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
58 ret = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
60 if (ret != GST_STATE_CHANGE_SUCCESS) {
61 g_critical ("Couldn't set pipeline to PLAYING");
66 GstMessage *message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 2);
69 rmessage = GST_MESSAGE_TYPE (message);
70 gst_message_unref (message);
72 rmessage = GST_MESSAGE_UNKNOWN;
75 if (rmessage == tmessage) {
77 } else if (rmessage == GST_MESSAGE_UNKNOWN) {
78 g_critical ("Unexpected timeout in gst_bus_poll, looking for %d: %s",
81 } else if (rmessage & message_types) {
84 g_critical ("Unexpected message received of type %d, looking for %d: %s",
85 rmessage, tmessage, descr);
89 gst_element_set_state (pipeline, GST_STATE_NULL);
90 gst_object_unref (pipeline);
91 gst_object_unref (bus);
94 GST_START_TEST (test_2_elements)
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);
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);
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);
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);
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));
123 got_handoff (GstElement * sink, GstBuffer * buf, GstPad * pad, gpointer unused)
125 gst_element_post_message
126 (sink, gst_message_new_application (NULL, gst_structure_new ("foo",
131 assert_live_count (GType type, gint live)
133 GstAllocTrace *trace;
136 if (gst_alloc_trace_available ()) {
137 name = g_type_name (type);
138 fail_if (name == NULL);
139 trace = gst_alloc_trace_get (name);
141 g_return_if_fail (trace->live == live);
144 g_print ("\nSkipping live count tests; recompile with traces to enable\n");
148 GST_START_TEST (test_stop_from_app)
150 GstElement *fakesrc, *fakesink, *pipeline;
152 GstStateChangeReturn ret;
153 GstMessageType rmessage;
156 assert_live_count (GST_TYPE_BUFFER, 0);
158 fakesrc = gst_element_factory_make ("fakesrc", NULL);
159 fakesink = gst_element_factory_make ("fakesink", NULL);
160 pipeline = gst_element_factory_make ("pipeline", NULL);
162 fail_unless (fakesrc && fakesink && pipeline);
164 gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
165 fail_unless (gst_element_link (fakesrc, fakesink) != FALSE);
167 g_object_set (fakesink, "signal-handoffs", (gboolean) TRUE, NULL);
168 g_signal_connect (fakesink, "handoff", G_CALLBACK (got_handoff), NULL);
170 gst_element_set_state (pipeline, GST_STATE_PAUSED);
171 ret = gst_element_get_state (pipeline, NULL, NULL, 5 * GST_SECOND);
173 case GST_STATE_CHANGE_FAILURE:
174 g_error ("Failed to change state to PAUSED");
176 case GST_STATE_CHANGE_ASYNC:
177 g_error ("Failed to change state to PAUSED within 5 seconds");
183 gst_element_set_state (pipeline, GST_STATE_PLAYING);
185 bus = gst_element_get_bus (pipeline);
186 fail_if (bus == NULL);
188 /* will time out after half a second */
189 message = gst_bus_poll (bus, GST_MESSAGE_APPLICATION, GST_SECOND / 2);
191 rmessage = GST_MESSAGE_TYPE (message);
192 gst_message_unref (message);
194 rmessage = GST_MESSAGE_UNKNOWN;
196 fail_unless (rmessage == GST_MESSAGE_APPLICATION,
197 "polled message is not APPLICATION but %s",
198 gst_message_type_get_name (rmessage));
200 gst_element_set_state (pipeline, GST_STATE_NULL);
201 gst_object_unref (pipeline);
202 gst_object_unref (bus);
204 assert_live_count (GST_TYPE_BUFFER, 0);
210 simple_launch_lines_suite (void)
212 Suite *s = suite_create ("Pipelines");
213 TCase *tc_chain = tcase_create ("linear");
215 /* time out after 20s, not the default 3 */
216 tcase_set_timeout (tc_chain, 0);
218 suite_add_tcase (s, tc_chain);
219 tcase_add_test (tc_chain, test_2_elements);
220 tcase_add_test (tc_chain, test_stop_from_app);
224 GST_CHECK_MAIN (simple_launch_lines);