1 /* GStreamer
2  *
3  * unit test for autoconvert element
4  * Copyright (C) 2009 Jan Schmidt <thaytan@noraisin.net>
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/gst.h>
27 #include <gst/check/gstcheck.h>
28 
29 /* Define 2 element factories for testing with */
30 typedef GstBin TestElement1;
31 typedef GstBinClass TestElement1Class;
32 typedef GstBin TestElement2;
33 typedef GstBinClass TestElement2Class;
34 
35 GType test_element1_get_type (void);
36 G_DEFINE_TYPE (TestElement1, test_element1, GST_TYPE_BIN);
37 GType test_element2_get_type (void);
38 G_DEFINE_TYPE (TestElement2, test_element2, GST_TYPE_BIN);
39 
40 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC, GST_PAD_ALWAYS,
42     GST_STATIC_CAPS ("test/caps,type=(int)[1,2]"));
43 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK, GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("test/caps,type=(int)[1,2]"));
46 
47 static void
setup(void)48 setup (void)
49 {
50   /* Register our test elements */
51   fail_unless (gst_element_register (NULL, "testelement1", GST_RANK_NONE,
52           test_element1_get_type ()));
53   fail_unless (gst_element_register (NULL, "testelement2", GST_RANK_NONE,
54           test_element2_get_type ()));
55 }
56 
57 static void
teardown(void)58 teardown (void)
59 {
60 }
61 
62 static void
set_autoconvert_factories(GstElement * autoconvert)63 set_autoconvert_factories (GstElement * autoconvert)
64 {
65   const gchar *desired_features[] = { "testelement1", "testelement2" };
66   GstElementFactory *feature;
67   GList *factories = NULL;
68   gint i;
69 
70   for (i = 0; i < G_N_ELEMENTS (desired_features); i++) {
71     feature =
72         GST_ELEMENT_FACTORY_CAST (gst_registry_find_feature
73         (gst_registry_get (), desired_features[i], GST_TYPE_ELEMENT_FACTORY));
74     fail_if (feature == NULL, "Test element %s was not found in registry",
75         desired_features[i]);
76     factories = g_list_prepend (factories, feature);
77   }
78 
79   g_object_set (G_OBJECT (autoconvert), "factories", factories, NULL);
80 
81   g_list_free_full (factories, gst_object_unref);
82 }
83 
GST_START_TEST(test_autoconvert_simple)84 GST_START_TEST (test_autoconvert_simple)
85 {
86   GstPad *test_src_pad, *test_sink_pad;
87   GstElement *autoconvert = gst_check_setup_element ("autoconvert");
88   GstBus *bus = gst_bus_new ();
89   GstCaps *caps;
90   guint i;
91 
92   set_autoconvert_factories (autoconvert);
93 
94   test_src_pad = gst_check_setup_src_pad (autoconvert, &src_factory);
95   gst_pad_set_active (test_src_pad, TRUE);
96   test_sink_pad = gst_check_setup_sink_pad (autoconvert, &sink_factory);
97   gst_pad_set_active (test_sink_pad, TRUE);
98 
99   gst_element_set_state (GST_ELEMENT_CAST (autoconvert), GST_STATE_PLAYING);
100 
101   /* Setting original caps */
102   caps = gst_caps_from_string ("test/caps,type=(int)1");
103   gst_check_setup_events (test_src_pad, autoconvert, caps, GST_FORMAT_BYTES);
104   gst_caps_unref (caps);
105 
106   /* Push 10 items */
107   for (i = 0; i < 10; i++) {
108     GST_LOG ("Pushing test buffer %d, caps 1", i);
109     fail_unless (gst_pad_push (test_src_pad, gst_buffer_new_and_alloc (4096))
110         == GST_FLOW_OK);
111   }
112 
113   GST_LOG ("Changing caps to caps 2");
114   caps = gst_caps_from_string ("test/caps,type=(int)2");
115   fail_unless (gst_pad_set_caps (test_src_pad, caps));
116   gst_caps_unref (caps);
117 
118   /* Push 10 more items */
119   for (i = 0; i < 10; i++) {
120     GST_LOG ("Pushing test buffer %d, caps 2", i);
121     fail_unless (gst_pad_push (test_src_pad, gst_buffer_new_and_alloc (4096))
122         == GST_FLOW_OK);
123   }
124 
125   /* Check all the items arrived */
126   fail_unless_equals_int (g_list_length (buffers), 20);
127 
128   while (TRUE) {
129     GstMessage *msg = gst_bus_pop (bus);
130     if (!msg)
131       break;
132 
133     GST_DEBUG ("got message %s",
134         gst_message_type_get_name (GST_MESSAGE_TYPE (msg)));
135     fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
136   }
137 
138   gst_element_set_state ((GstElement *) autoconvert, GST_STATE_NULL);
139 
140   gst_bus_set_flushing (bus, TRUE);
141   gst_object_unref (bus);
142 
143   gst_check_drop_buffers ();
144   gst_pad_set_active (test_src_pad, FALSE);
145   gst_pad_set_active (test_sink_pad, FALSE);
146   gst_check_teardown_src_pad (autoconvert);
147   gst_check_teardown_sink_pad (autoconvert);
148   gst_check_teardown_element (autoconvert);
149 }
150 
151 GST_END_TEST;
152 
153 static Suite *
autoconvert_suite(void)154 autoconvert_suite (void)
155 {
156   Suite *s = suite_create ("autoconvert");
157   TCase *tc_basic = tcase_create ("general");
158 
159   suite_add_tcase (s, tc_basic);
160   tcase_add_checked_fixture (tc_basic, setup, teardown);
161   tcase_add_test (tc_basic, test_autoconvert_simple);
162 
163   return s;
164 }
165 
166 /* Implementation of the test elements */
167 
168 static void
configure_test_element(GstBin * bin,const gchar * capsfilter)169 configure_test_element (GstBin * bin, const gchar * capsfilter)
170 {
171   GstElement *filter;
172   GstElement *identity;
173   GstPad *pad, *ghostpad;
174   GstPadTemplate *test_static_templ;
175 
176   filter = gst_element_factory_make ("capsfilter", NULL);
177   fail_unless (filter != NULL);
178   gst_util_set_object_arg (G_OBJECT (filter), "caps", capsfilter);
179 
180   identity = gst_element_factory_make ("identity", NULL);
181   fail_unless (identity != NULL);
182 
183   gst_bin_add_many (bin, filter, identity, NULL);
184   fail_unless (gst_element_link_many (filter, identity, NULL) == TRUE);
185 
186 
187   test_static_templ = gst_static_pad_template_get (&sink_factory);
188   pad = gst_element_get_static_pad (filter, "sink");
189   ghostpad = gst_ghost_pad_new_from_template ("sink", pad, test_static_templ);
190   gst_element_add_pad (GST_ELEMENT_CAST (bin), ghostpad);
191   gst_object_unref (pad);
192   gst_object_unref (test_static_templ);
193 
194   test_static_templ = gst_static_pad_template_get (&src_factory);
195   pad = gst_element_get_static_pad (identity, "src");
196   ghostpad = gst_ghost_pad_new_from_template ("src", pad, test_static_templ);
197   gst_element_add_pad (GST_ELEMENT_CAST (bin), ghostpad);
198   gst_object_unref (pad);
199   gst_object_unref (test_static_templ);
200 }
201 
202 static void
test_element1_class_init(TestElement1Class * klass)203 test_element1_class_init (TestElement1Class * klass)
204 {
205   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
206 
207   gst_element_class_add_static_pad_template (element_class, &src_factory);
208   gst_element_class_add_static_pad_template (element_class, &sink_factory);
209 }
210 
211 static void
test_element1_init(TestElement1 * elem)212 test_element1_init (TestElement1 * elem)
213 {
214   configure_test_element (GST_BIN_CAST (elem), "test/caps,type=(int)1");
215 }
216 
217 static void
test_element2_class_init(TestElement2Class * klass)218 test_element2_class_init (TestElement2Class * klass)
219 {
220   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
221 
222   gst_element_class_add_static_pad_template (element_class, &src_factory);
223 
224   gst_element_class_add_static_pad_template (element_class, &sink_factory);
225 }
226 
227 static void
test_element2_init(TestElement2 * elem)228 test_element2_init (TestElement2 * elem)
229 {
230   configure_test_element (GST_BIN_CAST (elem), "test/caps,type=(int)2");
231 }
232 
233 GST_CHECK_MAIN (autoconvert);
234