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_empty_identification_header)
99 GstElement *vorbisdec;
104 vorbisdec = setup_vorbisdec ();
105 bus = gst_bus_new ();
107 fail_unless (gst_element_set_state (vorbisdec,
108 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
109 "could not set to playing");
111 inbuffer = gst_buffer_new_and_alloc (0);
112 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
114 /* set a bus here so we avoid getting state change messages */
115 gst_element_set_bus (vorbisdec, bus);
117 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
118 /* ... but it ends up being collected on the global buffer list */
119 fail_unless_equals_int (g_list_length (buffers), 0);
121 fail_if ((message = gst_bus_pop (bus)) == NULL);
122 fail_unless_message_error (message, STREAM, DECODE);
123 gst_message_unref (message);
124 gst_element_set_bus (vorbisdec, NULL);
127 gst_object_unref (GST_OBJECT (bus));
128 cleanup_vorbisdec (vorbisdec);
133 /* FIXME: also tests comment header */
134 GST_START_TEST (test_identification_header)
136 GstElement *vorbisdec;
140 GstTagList *tag_list;
143 vorbisdec = setup_vorbisdec ();
144 fail_unless (gst_element_set_state (vorbisdec,
145 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
146 "could not set to playing");
147 bus = gst_bus_new ();
149 inbuffer = gst_buffer_new_and_alloc (30);
150 gst_buffer_fill (inbuffer, 0, identification_header, 30);
151 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
152 gst_buffer_ref (inbuffer);
154 gst_element_set_bus (vorbisdec, bus);
155 /* pushing gives away my reference ... */
156 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
157 /* ... and nothing ends up on the global buffer list */
158 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
159 gst_buffer_unref (inbuffer);
160 fail_unless (g_list_length (buffers) == 0);
161 fail_if ((message = gst_bus_pop (bus)) != NULL);
163 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
164 gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
165 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
166 gst_buffer_ref (inbuffer);
168 /* pushing gives away my reference ... */
169 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
170 /* ... and nothing ends up on the global buffer list */
171 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
172 gst_buffer_unref (inbuffer);
173 fail_unless (g_list_length (buffers) == 0);
174 /* there's a tag message waiting */
175 fail_if ((message = gst_bus_pop (bus)) == NULL);
176 gst_message_parse_tag (message, &tag_list);
177 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
179 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
180 fail_unless_equals_string (artist, "me");
182 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
183 gst_tag_list_free (tag_list);
184 gst_message_unref (message);
187 gst_bus_set_flushing (bus, TRUE);
188 gst_element_set_bus (vorbisdec, NULL);
189 gst_object_unref (GST_OBJECT (bus));
190 cleanup_vorbisdec (vorbisdec);
195 static vorbis_comment vc;
196 static vorbis_dsp_state vd;
197 static vorbis_info vi;
198 static vorbis_block vb;
201 _create_codebook_header_buffer (void)
205 ogg_packet header_comm;
206 ogg_packet header_code;
208 vorbis_info_init (&vi);
209 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
210 vorbis_encode_setup_init (&vi);
211 vorbis_analysis_init (&vd, &vi);
212 vorbis_block_init (&vd, &vb);
213 vorbis_comment_init (&vc);
214 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
216 buffer = gst_buffer_new_and_alloc (header_code.bytes);
217 gst_buffer_fill (buffer, 0, header_code.packet, header_code.bytes);
223 _create_audio_buffer (void)
227 float **vorbis_buffer;
230 vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
231 for (i = 0; i < 44100 * 1; ++i)
232 vorbis_buffer[0][i] = 0.0;
233 vorbis_analysis_wrote (&vd, 44100);
234 vorbis_analysis_blockout (&vd, &vb);
235 vorbis_analysis (&vb, NULL);
236 vorbis_bitrate_addblock (&vb);
237 vorbis_bitrate_flushpacket (&vd, &packet);
238 buffer = gst_buffer_new_and_alloc (packet.bytes);
239 gst_buffer_fill (buffer, 0, packet.packet, packet.bytes);
241 vorbis_comment_clear (&vc);
242 vorbis_block_clear (&vb);
243 vorbis_dsp_clear (&vd);
244 vorbis_info_clear (&vi);
249 GST_START_TEST (test_empty_vorbis_packet)
251 GstElement *vorbisdec;
256 vorbisdec = setup_vorbisdec ();
257 fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
258 GST_STATE_CHANGE_SUCCESS);
260 bus = gst_bus_new ();
262 inbuffer = gst_buffer_new_and_alloc (30);
263 gst_buffer_fill (inbuffer, 0, identification_header, 30);
264 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
265 gst_buffer_ref (inbuffer);
267 gst_element_set_bus (vorbisdec, bus);
269 /* pushing gives away my reference ... */
270 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
271 /* ... and nothing ends up on the global buffer list */
272 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
273 gst_buffer_unref (inbuffer);
274 fail_unless (g_list_length (buffers) == 0);
275 fail_if ((message = gst_bus_pop (bus)) != NULL);
277 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
278 gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
279 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
280 gst_buffer_ref (inbuffer);
282 /* pushing gives away my reference ... */
283 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
284 /* ... and nothing ends up on the global buffer list */
285 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
286 gst_buffer_unref (inbuffer);
287 fail_unless (g_list_length (buffers) == 0);
289 /* send minimal codebook header and audio packers */
290 inbuffer = _create_codebook_header_buffer ();
291 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
293 /* now send an empty vorbis packet, which should just be skipped */
294 inbuffer = gst_buffer_new_and_alloc (0);
295 gst_buffer_ref (inbuffer);
296 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
297 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
298 gst_buffer_unref (inbuffer);
299 fail_unless (g_list_length (buffers) == 0);
301 /* create and push an encoded audio packet */
302 inbuffer = _create_audio_buffer ();
303 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
305 /* now send another empty vorbis packet, which should just be skipped */
306 inbuffer = gst_buffer_new_and_alloc (0);
307 gst_buffer_ref (inbuffer);
308 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
309 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
310 gst_buffer_unref (inbuffer);
312 /* make sure there's no error on the bus */
313 message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
314 fail_if (message != NULL);
317 gst_bus_set_flushing (bus, TRUE);
318 gst_element_set_bus (vorbisdec, NULL);
319 gst_object_unref (GST_OBJECT (bus));
320 cleanup_vorbisdec (vorbisdec);
326 vorbisdec_suite (void)
328 Suite *s = suite_create ("vorbisdec");
329 TCase *tc_chain = tcase_create ("general");
331 suite_add_tcase (s, tc_chain);
332 tcase_add_test (tc_chain, test_empty_identification_header);
333 tcase_add_test (tc_chain, test_identification_header);
334 tcase_add_test (tc_chain, test_empty_vorbis_packet);
339 GST_CHECK_MAIN (vorbisdec);