3 * unit test for vorbisdec
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 #include <vorbis/codec.h>
28 #include <vorbis/vorbisenc.h>
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 static GstPad *mysrcpad, *mysinkpad;
35 /* a valid first header packet */
36 static guchar identification_header[30] = {
38 'v', 'o', 'r', 'b', 'i', 's',
39 0, 0, 0, 0, /* vorbis_version */
40 2, /* audio_channels */
41 0x44, 0xac, 0, 0, /* sample_rate */
42 0xff, 0xff, 0xff, 0xff, /* bitrate_maximum */
43 0x00, 0xee, 0x02, 0x00, /* bitrate_nominal */
44 0xff, 0xff, 0xff, 0xff, /* bitrate_minimum */
45 0xb8, /* blocksize_0, blocksize_1 */
46 0x01, /* framing_flag */
49 static guchar comment_header[] = {
51 'v', 'o', 'r', 'b', 'i', 's',
52 2, 0, 0, 0, /* vendor_length */
54 1, 0, 0, 0, /* user_comment_list_length */
55 9, 0, 0, 0, /* length comment[0] */
56 'A', 'R', 'T', 'I', 'S', 'T', '=', 'm', 'e',
57 0x01, /* framing bit */
60 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
64 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 setup_vorbisdec (void)
72 GstElement *vorbisdec;
74 GST_DEBUG ("setup_vorbisdec");
75 vorbisdec = gst_check_setup_element ("vorbisdec");
76 mysrcpad = gst_check_setup_src_pad (vorbisdec, &srctemplate, NULL);
77 mysinkpad = gst_check_setup_sink_pad (vorbisdec, &sinktemplate, NULL);
78 gst_pad_set_active (mysrcpad, TRUE);
79 gst_pad_set_active (mysinkpad, TRUE);
85 cleanup_vorbisdec (GstElement * vorbisdec)
87 GST_DEBUG ("cleanup_vorbisdec");
88 gst_element_set_state (vorbisdec, GST_STATE_NULL);
90 gst_pad_set_active (mysrcpad, FALSE);
91 gst_pad_set_active (mysinkpad, FALSE);
92 gst_check_teardown_src_pad (vorbisdec);
93 gst_check_teardown_sink_pad (vorbisdec);
94 gst_check_teardown_element (vorbisdec);
97 GST_START_TEST (test_wrong_channels_identification_header)
99 GstElement *vorbisdec;
104 vorbisdec = setup_vorbisdec ();
105 fail_unless (gst_element_set_state (vorbisdec,
106 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107 "could not set to playing");
108 bus = gst_bus_new ();
110 inbuffer = gst_buffer_new_and_alloc (30);
111 memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
112 /* set the channel count to 7, which is not supported */
113 GST_BUFFER_DATA (inbuffer)[11] = 7;
114 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
115 gst_buffer_ref (inbuffer);
117 gst_element_set_bus (vorbisdec, bus);
118 /* pushing gives away my reference ... */
119 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
120 /* ... and nothing ends up on the global buffer list */
121 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
122 gst_buffer_unref (inbuffer);
123 fail_unless_equals_int (g_list_length (buffers), 0);
125 fail_if ((message = gst_bus_pop (bus)) == NULL);
126 fail_unless_message_error (message, STREAM, NOT_IMPLEMENTED);
127 gst_message_unref (message);
128 gst_element_set_bus (vorbisdec, NULL);
131 gst_object_unref (GST_OBJECT (bus));
132 cleanup_vorbisdec (vorbisdec);
138 GST_START_TEST (test_empty_identification_header)
140 GstElement *vorbisdec;
145 vorbisdec = setup_vorbisdec ();
146 bus = gst_bus_new ();
148 fail_unless (gst_element_set_state (vorbisdec,
149 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
150 "could not set to playing");
152 inbuffer = gst_buffer_new_and_alloc (0);
153 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
155 /* set a bus here so we avoid getting state change messages */
156 gst_element_set_bus (vorbisdec, bus);
158 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
159 /* ... but it ends up being collected on the global buffer list */
160 fail_unless_equals_int (g_list_length (buffers), 0);
162 fail_if ((message = gst_bus_pop (bus)) == NULL);
163 fail_unless_message_error (message, STREAM, DECODE);
164 gst_message_unref (message);
165 gst_element_set_bus (vorbisdec, NULL);
168 gst_object_unref (GST_OBJECT (bus));
169 cleanup_vorbisdec (vorbisdec);
174 /* FIXME: also tests comment header */
175 GST_START_TEST (test_identification_header)
177 GstElement *vorbisdec;
181 GstTagList *tag_list;
184 vorbisdec = setup_vorbisdec ();
185 fail_unless (gst_element_set_state (vorbisdec,
186 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
187 "could not set to playing");
188 bus = gst_bus_new ();
190 inbuffer = gst_buffer_new_and_alloc (30);
191 memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
192 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
193 gst_buffer_ref (inbuffer);
195 gst_element_set_bus (vorbisdec, bus);
196 /* pushing gives away my reference ... */
197 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
198 /* ... and nothing ends up on the global buffer list */
199 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
200 gst_buffer_unref (inbuffer);
201 fail_unless (g_list_length (buffers) == 0);
202 fail_if ((message = gst_bus_pop (bus)) != NULL);
204 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
205 memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
206 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
207 gst_buffer_ref (inbuffer);
209 /* pushing gives away my reference ... */
210 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
211 /* ... and nothing ends up on the global buffer list */
212 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
213 gst_buffer_unref (inbuffer);
214 fail_unless (g_list_length (buffers) == 0);
215 /* there's a tag message waiting */
216 fail_if ((message = gst_bus_pop (bus)) == NULL);
217 gst_message_parse_tag (message, &tag_list);
218 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
220 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
221 fail_unless_equals_string (artist, "me");
223 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
224 gst_tag_list_free (tag_list);
225 gst_message_unref (message);
228 gst_bus_set_flushing (bus, TRUE);
229 gst_element_set_bus (vorbisdec, NULL);
230 gst_object_unref (GST_OBJECT (bus));
231 cleanup_vorbisdec (vorbisdec);
236 static vorbis_comment vc;
237 static vorbis_dsp_state vd;
238 static vorbis_info vi;
239 static vorbis_block vb;
242 _create_codebook_header_buffer (void)
246 ogg_packet header_comm;
247 ogg_packet header_code;
249 vorbis_info_init (&vi);
250 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
251 vorbis_encode_setup_init (&vi);
252 vorbis_analysis_init (&vd, &vi);
253 vorbis_block_init (&vd, &vb);
254 vorbis_comment_init (&vc);
255 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
257 buffer = gst_buffer_new_and_alloc (header_code.bytes);
258 memcpy (GST_BUFFER_DATA (buffer), header_code.packet, header_code.bytes);
264 _create_audio_buffer (void)
268 float **vorbis_buffer;
271 vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
272 for (i = 0; i < 44100 * 1; ++i)
273 vorbis_buffer[0][i] = 0.0;
274 vorbis_analysis_wrote (&vd, 44100);
275 vorbis_analysis_blockout (&vd, &vb);
276 vorbis_analysis (&vb, NULL);
277 vorbis_bitrate_addblock (&vb);
278 vorbis_bitrate_flushpacket (&vd, &packet);
279 buffer = gst_buffer_new_and_alloc (packet.bytes);
280 memcpy (GST_BUFFER_DATA (buffer), packet.packet, packet.bytes);
282 vorbis_comment_clear (&vc);
283 vorbis_block_clear (&vb);
284 vorbis_dsp_clear (&vd);
285 vorbis_info_clear (&vi);
290 GST_START_TEST (test_empty_vorbis_packet)
292 GstElement *vorbisdec;
297 vorbisdec = setup_vorbisdec ();
298 fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
299 GST_STATE_CHANGE_SUCCESS);
301 bus = gst_bus_new ();
303 inbuffer = gst_buffer_new_and_alloc (30);
304 memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
305 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
306 gst_buffer_ref (inbuffer);
308 gst_element_set_bus (vorbisdec, bus);
310 /* pushing gives away my reference ... */
311 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
312 /* ... and nothing ends up on the global buffer list */
313 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
314 gst_buffer_unref (inbuffer);
315 fail_unless (g_list_length (buffers) == 0);
316 fail_if ((message = gst_bus_pop (bus)) != NULL);
318 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
319 memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
320 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
321 gst_buffer_ref (inbuffer);
323 /* pushing gives away my reference ... */
324 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
325 /* ... and nothing ends up on the global buffer list */
326 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
327 gst_buffer_unref (inbuffer);
328 fail_unless (g_list_length (buffers) == 0);
330 /* send minimal codebook header and audio packers */
331 inbuffer = _create_codebook_header_buffer ();
332 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
334 /* now send an empty vorbis packet, which should just be skipped */
335 inbuffer = gst_buffer_new_and_alloc (0);
336 gst_buffer_ref (inbuffer);
337 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
338 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
339 gst_buffer_unref (inbuffer);
340 fail_unless (g_list_length (buffers) == 0);
342 /* create and push an encoded audio packet */
343 inbuffer = _create_audio_buffer ();
344 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
346 /* now send another empty vorbis packet, which should just be skipped */
347 inbuffer = gst_buffer_new_and_alloc (0);
348 gst_buffer_ref (inbuffer);
349 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
350 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
351 gst_buffer_unref (inbuffer);
353 /* make sure there's no error on the bus */
354 message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
355 fail_if (message != NULL);
358 gst_bus_set_flushing (bus, TRUE);
359 gst_element_set_bus (vorbisdec, NULL);
360 gst_object_unref (GST_OBJECT (bus));
361 cleanup_vorbisdec (vorbisdec);
367 vorbisdec_suite (void)
369 Suite *s = suite_create ("vorbisdec");
370 TCase *tc_chain = tcase_create ("general");
372 suite_add_tcase (s, tc_chain);
373 tcase_add_test (tc_chain, test_empty_identification_header);
374 tcase_add_test (tc_chain, test_wrong_channels_identification_header);
375 tcase_add_test (tc_chain, test_identification_header);
376 tcase_add_test (tc_chain, test_empty_vorbis_packet);
382 main (int argc, char **argv)
386 Suite *s = vorbisdec_suite ();
387 SRunner *sr = srunner_create (s);
389 gst_check_init (&argc, &argv);
391 srunner_run_all (sr, CK_NORMAL);
392 nf = srunner_ntests_failed (sr);