1 /* GStreamer
2 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3 *
4 * cleanup.c: Unit test for cleanup of 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22
23 #include <gst/check/gstcheck.h>
24
25
26 static GstElement *
setup_pipeline(const gchar * pipe_descr)27 setup_pipeline (const 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 /* events is a mask of expected events. tevent is the expected terminal event.
37 the poll call will time out after half a second.
38 */
39 static void
run_pipeline(GstElement * pipeline,const gchar * descr,GstMessageType events,GstMessageType tevent)40 run_pipeline (GstElement * pipeline, const gchar * descr,
41 GstMessageType events, GstMessageType tevent)
42 {
43 GstBus *bus;
44 GstMessageType revent;
45
46 bus = gst_element_get_bus (pipeline);
47 g_assert (bus);
48 gst_element_set_state (pipeline, GST_STATE_PLAYING);
49
50 while (1) {
51 GstMessage *message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 2);
52
53 if (message) {
54 revent = GST_MESSAGE_TYPE (message);
55 gst_message_unref (message);
56 } else {
57 revent = GST_MESSAGE_UNKNOWN;
58 }
59
60 if (revent == tevent) {
61 break;
62 } else if (revent == GST_MESSAGE_UNKNOWN) {
63 g_critical ("Unexpected timeout in gst_bus_poll, looking for %d: %s",
64 tevent, descr);
65 break;
66 } else if (revent & events) {
67 continue;
68 }
69 g_critical
70 ("Unexpected message received of type %d, '%s', looking for %d: %s",
71 revent, gst_message_type_get_name (revent), tevent, descr);
72 }
73
74 gst_element_set_state (pipeline, GST_STATE_NULL);
75 gst_object_unref (pipeline);
76 gst_object_unref (bus);
77 }
78
GST_START_TEST(test_pipeline_unref)79 GST_START_TEST (test_pipeline_unref)
80 {
81 const gchar *s;
82 GstElement *pipeline, *src, *sink;
83
84 s = "fakesrc name=src num-buffers=20 ! fakesink name=sink";
85 pipeline = setup_pipeline (s);
86 /* get_by_name takes a ref */
87 src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
88 fail_if (src == NULL);
89 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
90 fail_if (sink == NULL);
91
92 run_pipeline (pipeline, s,
93 GST_MESSAGE_NEW_CLOCK | GST_MESSAGE_STATE_CHANGED |
94 GST_MESSAGE_STREAM_STATUS | GST_MESSAGE_ASYNC_DONE |
95 GST_MESSAGE_STREAM_START | GST_MESSAGE_LATENCY, GST_MESSAGE_EOS);
96 while (GST_OBJECT_REFCOUNT_VALUE (src) > 1)
97 THREAD_SWITCH ();
98 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
99 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
100 gst_object_unref (src);
101 gst_object_unref (sink);
102 }
103
104 GST_END_TEST;
105
106 static Suite *
cleanup_suite(void)107 cleanup_suite (void)
108 {
109 Suite *s = suite_create ("Pipeline cleanup");
110 TCase *tc_chain = tcase_create ("linear");
111
112 /* time out after 20s, not the default 3 */
113 tcase_set_timeout (tc_chain, 20);
114
115 suite_add_tcase (s, tc_chain);
116 tcase_add_test (tc_chain, test_pipeline_unref);
117 return s;
118 }
119
120 GST_CHECK_MAIN (cleanup);
121