expand tabs
[platform/upstream/gstreamer.git] / tests / check / elements / volume.c
1 /* GStreamer
2  *
3  * unit test for volume
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
29
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;
34
35
36 #define VOLUME_CAPS_TEMPLATE_STRING     \
37     "audio/x-raw-int, "                 \
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"
44
45 #define VOLUME_CAPS_STRING              \
46     "audio/x-raw-int, "                 \
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"
53
54 #define VOLUME_WRONG_CAPS_STRING        \
55     "audio/x-raw-int, "                 \
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"
62
63
64 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
68     );
69 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
73     );
74
75 GstElement *
76 setup_volume ()
77 {
78   GstElement *volume;
79
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);
86
87   return volume;
88 }
89
90 void
91 cleanup_volume (GstElement * volume)
92 {
93   GST_DEBUG ("cleanup_volume");
94
95   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
96   g_list_free (buffers);
97   buffers = NULL;
98
99   gst_check_teardown_src_pad (volume);
100   gst_check_teardown_sink_pad (volume);
101   gst_check_teardown_element (volume);
102 }
103
104 GST_START_TEST (test_unity)
105 {
106   GstElement *volume;
107   GstBuffer *inbuffer, *outbuffer;
108   GstCaps *caps;
109   gint16 in[2] = { 16384, -256 };
110
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");
115
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);
122
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);
131
132   /* cleanup */
133   cleanup_volume (volume);
134 }
135
136 GST_END_TEST;
137
138 GST_START_TEST (test_half)
139 {
140   GstElement *volume;
141   GstBuffer *inbuffer;
142   GstBuffer *outbuffer;
143   GstCaps *caps;
144   gint16 in[2] = { 16384, -256 };
145   gint16 out[2] = { 8192, -128 };
146
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");
152
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
161    * inplace
162    gst_buffer_ref (inbuffer);
163    */
164
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);
174
175   /* cleanup */
176   cleanup_volume (volume);
177 }
178
179 GST_END_TEST;
180
181 GST_START_TEST (test_double)
182 {
183   GstElement *volume;
184   GstBuffer *inbuffer;
185   GstBuffer *outbuffer;
186   GstCaps *caps;
187   gint16 in[2] = { 16384, -256 };
188   gint16 out[2] = { 32767, -512 };      /* notice the clamped sample */
189
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");
195
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
204    * inplace
205    gst_buffer_ref (inbuffer);
206    */
207
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);
217
218   /* cleanup */
219   cleanup_volume (volume);
220 }
221
222 GST_END_TEST;
223
224 GST_START_TEST (test_wrong_caps)
225 {
226   GstElement *volume;
227   GstBuffer *inbuffer, *outbuffer;
228   gint16 in[2] = { 16384, -256 };
229   GstBus *bus;
230   GstMessage *message;
231   GstCaps *caps;
232
233   volume = setup_volume ();
234   bus = gst_bus_new ();
235
236   fail_unless (gst_element_set_state (volume,
237           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
238       "could not set to playing");
239
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);
247
248   /* set a bus here so we avoid getting state change messages */
249   gst_element_set_bus (volume, bus);
250
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);
258
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);
262
263   /* cleanup */
264   gst_element_set_bus (volume, NULL);
265   gst_object_unref (GST_OBJECT (bus));
266   cleanup_volume (volume);
267 }
268
269 GST_END_TEST;
270
271
272 Suite *
273 volume_suite (void)
274 {
275   Suite *s = suite_create ("volume");
276   TCase *tc_chain = tcase_create ("general");
277
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);
283
284   return s;
285 }
286
287 int
288 main (int argc, char **argv)
289 {
290   int nf;
291
292   Suite *s = volume_suite ();
293   SRunner *sr = srunner_create (s);
294
295   gst_check_init (&argc, &argv);
296
297   srunner_run_all (sr, CK_NORMAL);
298   nf = srunner_ntests_failed (sr);
299   srunner_free (sr);
300
301   return nf;
302 }