*.c: Don't cast to GST_OBJECT when reffing or unreffing. Large source-munging commit!!!
[platform/upstream/gstreamer.git] / tests / check / pipelines / cleanup.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include "../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 /* 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
40 run_pipeline (GstElement * pipe, gchar * descr,
41     GstMessageType events, GstMessageType tevent)
42 {
43   GstBus *bus;
44   GstMessageType revent;
45
46   bus = gst_element_get_bus (pipe);
47   g_assert (bus);
48   gst_element_set_state (pipe, GST_STATE_PLAYING);
49
50   while (1) {
51     revent = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 2);
52
53     /* always have to pop the message before getting back into poll */
54     if (revent != GST_MESSAGE_UNKNOWN)
55       gst_message_unref (gst_bus_pop (bus));
56
57     if (revent == tevent) {
58       break;
59     } else if (revent == GST_MESSAGE_UNKNOWN) {
60       g_critical ("Unexpected timeout in gst_bus_poll, looking for %d: %s",
61           tevent, descr);
62       break;
63     } else if (revent & events) {
64       continue;
65     }
66     g_critical ("Unexpected message received of type %d, looking for %d: %s",
67         revent, tevent, descr);
68   }
69
70   gst_element_set_state (pipe, GST_STATE_NULL);
71   gst_object_unref (pipe);
72 }
73
74 START_TEST (test_pipeline_unref)
75 {
76   gchar *s;
77   gint count;
78   GstElement *pipeline, *src, *sink;
79
80   s = "fakesrc name=src num-buffers=20 ! fakesink name=sink";
81   pipeline = setup_pipeline (s);
82   /* get_by_name takes a ref */
83   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
84   fail_if (src == NULL);
85   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
86   fail_if (sink == NULL);
87
88   run_pipeline (pipeline, s, GST_MESSAGE_STATE_CHANGED, GST_MESSAGE_EOS);
89   count = GST_OBJECT_REFCOUNT_VALUE (src);
90   fail_unless (count == 1, "src has a refcount of %d instead of 1", count);
91   count = GST_OBJECT_REFCOUNT_VALUE (sink);
92   fail_unless (count == 1, "sink has a refcount of %d instead of 1", count);
93   gst_object_unref (src);
94   gst_object_unref (sink);
95 }
96
97 END_TEST Suite *
98 cleanup_suite (void)
99 {
100   Suite *s = suite_create ("Pipeline cleanup");
101   TCase *tc_chain = tcase_create ("linear");
102
103   /* time out after 20s, not the default 3 */
104   tcase_set_timeout (tc_chain, 20);
105
106   suite_add_tcase (s, tc_chain);
107   tcase_add_test (tc_chain, test_pipeline_unref);
108   return s;
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114   int nf;
115
116   Suite *s = cleanup_suite ();
117   SRunner *sr = srunner_create (s);
118
119   gst_check_init (&argc, &argv);
120
121   srunner_run_all (sr, CK_NORMAL);
122   nf = srunner_ntests_failed (sr);
123   srunner_free (sr);
124
125   return nf;
126 }