5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
27 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
30 /* For ease of programming we use globals to keep refs for our floating
31 * src and sink pads we create; otherwise we always have to do get_pad,
32 * get_peer, and then remove references in every test function */
33 GstPad *mysrcpad, *mysinkpad;
36 #define VOLUME_CAPS_TEMPLATE_STRING \
38 "channels = (int) [ 1, MAX ], " \
39 "rate = (int) [ 1, MAX ], " \
40 "endianness = (int) BYTE_ORDER, " \
41 "width = (int) 16, " \
42 "depth = (int) 16, " \
43 "signed = (bool) TRUE"
45 #define VOLUME_CAPS_STRING \
47 "channels = (int) 1, " \
48 "rate = (int) 44100, " \
49 "endianness = (int) BYTE_ORDER, " \
50 "width = (int) 16, " \
51 "depth = (int) 16, " \
52 "signed = (bool) TRUE"
54 #define VOLUME_WRONG_CAPS_STRING \
56 "channels = (int) 1, " \
57 "rate = (int) 44100, " \
58 "endianness = (int) BYTE_ORDER, " \
59 "width = (int) 16, " \
60 "depth = (int) 16, " \
61 "signed = (bool) FALSE"
64 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
69 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
72 GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
80 GST_DEBUG ("setup_volume");
81 volume = gst_check_setup_element ("volume");
82 mysrcpad = gst_check_setup_src_pad (volume, &srctemplate, NULL);
83 mysinkpad = gst_check_setup_sink_pad (volume, &sinktemplate, NULL);
84 gst_pad_set_active (mysrcpad, TRUE);
85 gst_pad_set_active (mysinkpad, TRUE);
91 cleanup_volume (GstElement * volume)
93 GST_DEBUG ("cleanup_volume");
95 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
96 g_list_free (buffers);
99 gst_check_teardown_src_pad (volume);
100 gst_check_teardown_sink_pad (volume);
101 gst_check_teardown_element (volume);
104 GST_START_TEST (test_unity)
107 GstBuffer *inbuffer, *outbuffer;
109 gint16 in[2] = { 16384, -256 };
111 volume = setup_volume ();
112 fail_unless (gst_element_set_state (volume,
113 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
114 "could not set to playing");
116 inbuffer = gst_buffer_new_and_alloc (4);
117 memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
118 caps = gst_caps_from_string (VOLUME_CAPS_STRING);
119 gst_buffer_set_caps (inbuffer, caps);
120 gst_caps_unref (caps);
121 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
123 /* pushing gives away my reference ... */
124 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
125 /* ... but it ends up being collected on the global buffer list */
126 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
127 fail_unless_equals_int (g_list_length (buffers), 1);
128 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
129 fail_unless (inbuffer == outbuffer);
130 fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
133 cleanup_volume (volume);
138 GST_START_TEST (test_half)
142 GstBuffer *outbuffer;
144 gint16 in[2] = { 16384, -256 };
145 gint16 out[2] = { 8192, -128 };
147 volume = setup_volume ();
148 g_object_set (G_OBJECT (volume), "volume", 0.5, NULL);
149 fail_unless (gst_element_set_state (volume,
150 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
151 "could not set to playing");
153 inbuffer = gst_buffer_new_and_alloc (4);
154 memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
155 fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
156 caps = gst_caps_from_string (VOLUME_CAPS_STRING);
157 gst_buffer_set_caps (inbuffer, caps);
158 gst_caps_unref (caps);
159 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
160 /* FIXME: reffing the inbuffer should make the transformation not be
162 gst_buffer_ref (inbuffer);
165 /* pushing gives away my reference ... */
166 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
167 /* ... but it ends up being modified inplace and
168 * collected on the global buffer list */
169 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
170 fail_unless_equals_int (g_list_length (buffers), 1);
171 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
172 fail_unless (inbuffer == outbuffer);
173 fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0);
176 cleanup_volume (volume);
181 GST_START_TEST (test_double)
185 GstBuffer *outbuffer;
187 gint16 in[2] = { 16384, -256 };
188 gint16 out[2] = { 32767, -512 }; /* notice the clamped sample */
190 volume = setup_volume ();
191 g_object_set (G_OBJECT (volume), "volume", 2.0, NULL);
192 fail_unless (gst_element_set_state (volume,
193 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
194 "could not set to playing");
196 inbuffer = gst_buffer_new_and_alloc (4);
197 memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
198 fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
199 caps = gst_caps_from_string (VOLUME_CAPS_STRING);
200 gst_buffer_set_caps (inbuffer, caps);
201 gst_caps_unref (caps);
202 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
203 /* FIXME: reffing the inbuffer should make the transformation not be
205 gst_buffer_ref (inbuffer);
208 /* pushing gives away my reference ... */
209 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
210 /* ... but it ends up being modified inplace and
211 * collected on the global buffer list */
212 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
213 fail_unless_equals_int (g_list_length (buffers), 1);
214 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
215 fail_unless (inbuffer == outbuffer);
216 fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0);
219 cleanup_volume (volume);
224 GST_START_TEST (test_wrong_caps)
227 GstBuffer *inbuffer, *outbuffer;
228 gint16 in[2] = { 16384, -256 };
233 volume = setup_volume ();
234 bus = gst_bus_new ();
236 fail_unless (gst_element_set_state (volume,
237 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
238 "could not set to playing");
240 inbuffer = gst_buffer_new_and_alloc (4);
241 memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
242 caps = gst_caps_from_string (VOLUME_WRONG_CAPS_STRING);
243 gst_buffer_set_caps (inbuffer, caps);
244 gst_caps_unref (caps);
245 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
246 gst_buffer_ref (inbuffer);
248 /* set a bus here so we avoid getting state change messages */
249 gst_element_set_bus (volume, bus);
251 /* pushing gives an error because it can't negotiate with wrong caps */
252 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer),
253 GST_FLOW_NOT_NEGOTIATED);
254 /* ... and the buffer would have been lost if we didn't ref it ourselves */
255 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
256 gst_buffer_unref (inbuffer);
257 fail_unless_equals_int (g_list_length (buffers), 0);
259 /* volume_set_caps should not have been called since basetransform caught
260 * the negotiation problem */
261 fail_if ((message = gst_bus_pop (bus)) != NULL);
264 gst_element_set_bus (volume, NULL);
265 gst_object_unref (GST_OBJECT (bus));
266 cleanup_volume (volume);
275 Suite *s = suite_create ("volume");
276 TCase *tc_chain = tcase_create ("general");
278 suite_add_tcase (s, tc_chain);
279 tcase_add_test (tc_chain, test_unity);
280 tcase_add_test (tc_chain, test_half);
281 tcase_add_test (tc_chain, test_double);
282 tcase_add_test (tc_chain, test_wrong_caps);
288 main (int argc, char **argv)
292 Suite *s = volume_suite ();
293 SRunner *sr = srunner_create (s);
295 gst_check_init (&argc, &argv);
297 srunner_run_all (sr, CK_NORMAL);
298 nf = srunner_ntests_failed (sr);