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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 #include <gst/check/gstcheck.h>
28 #include <vorbis/codec.h>
29 #include <vorbis/vorbisenc.h>
31 /* For ease of programming we use globals to keep refs for our floating
32 * src and sink pads we create; otherwise we always have to do get_pad,
33 * get_peer, and then remove references in every test function */
34 static GstPad *mysrcpad, *mysinkpad;
36 /* a valid first header packet */
37 static guchar identification_header[30] = {
39 'v', 'o', 'r', 'b', 'i', 's',
40 0, 0, 0, 0, /* vorbis_version */
41 2, /* audio_channels */
42 0x44, 0xac, 0, 0, /* sample_rate */
43 0xff, 0xff, 0xff, 0xff, /* bitrate_maximum */
44 0x00, 0xee, 0x02, 0x00, /* bitrate_nominal */
45 0xff, 0xff, 0xff, 0xff, /* bitrate_minimum */
46 0xb8, /* blocksize_0, blocksize_1 */
47 0x01, /* framing_flag */
50 static guchar comment_header[] = {
52 'v', 'o', 'r', 'b', 'i', 's',
53 2, 0, 0, 0, /* vendor_length */
55 1, 0, 0, 0, /* user_comment_list_length */
56 9, 0, 0, 0, /* length comment[0] */
57 'A', 'R', 'T', 'I', 'S', 'T', '=', 'm', 'e',
58 0x01, /* framing bit */
61 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
71 setup_vorbisdec (void)
73 GstElement *vorbisdec;
76 GST_DEBUG ("setup_vorbisdec");
77 vorbisdec = gst_check_setup_element ("vorbisdec");
78 mysrcpad = gst_check_setup_src_pad (vorbisdec, &srctemplate);
79 mysinkpad = gst_check_setup_sink_pad (vorbisdec, &sinktemplate);
80 gst_pad_set_active (mysrcpad, TRUE);
82 caps = gst_caps_new_empty_simple ("audio/x-vorbis");
83 gst_check_setup_events (mysrcpad, vorbisdec, caps, GST_FORMAT_TIME);
84 gst_caps_unref (caps);
86 gst_pad_set_active (mysinkpad, TRUE);
92 cleanup_vorbisdec (GstElement * vorbisdec)
94 GST_DEBUG ("cleanup_vorbisdec");
95 gst_element_set_state (vorbisdec, GST_STATE_NULL);
97 gst_pad_set_active (mysrcpad, FALSE);
98 gst_pad_set_active (mysinkpad, FALSE);
99 gst_check_teardown_src_pad (vorbisdec);
100 gst_check_teardown_sink_pad (vorbisdec);
101 gst_check_teardown_element (vorbisdec);
104 /* FIXME: also tests comment header */
105 GST_START_TEST (test_identification_header)
107 GstElement *vorbisdec;
112 vorbisdec = setup_vorbisdec ();
114 fail_unless (gst_element_set_state (vorbisdec,
115 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
116 "could not set to playing");
117 bus = gst_bus_new ();
119 inbuffer = gst_buffer_new_and_alloc (30);
120 gst_buffer_fill (inbuffer, 0, identification_header, 30);
121 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
122 gst_buffer_ref (inbuffer);
124 gst_element_set_bus (vorbisdec, bus);
125 /* pushing gives away my reference ... */
126 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
127 /* ... and nothing ends up on the global buffer list */
128 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
129 gst_buffer_unref (inbuffer);
130 fail_unless (g_list_length (buffers) == 0);
131 fail_if ((message = gst_bus_pop (bus)) != NULL);
133 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
134 gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
135 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
136 gst_buffer_ref (inbuffer);
138 /* pushing gives away my reference ... */
139 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
140 /* ... and nothing ends up on the global buffer list */
141 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
142 gst_buffer_unref (inbuffer);
143 fail_unless (g_list_length (buffers) == 0);
146 /* there's a tag message waiting */
147 fail_if ((message = gst_bus_pop (bus)) == NULL);
148 gst_message_parse_tag (message, &tag_list);
149 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
151 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
152 fail_unless_equals_string (artist, "me");
154 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
155 gst_tag_list_unref (tag_list);
156 gst_message_unref (message);
159 /* make sure there's no error on the bus */
160 message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR);
161 fail_if (message != NULL);
164 gst_bus_set_flushing (bus, TRUE);
165 gst_element_set_bus (vorbisdec, NULL);
166 gst_object_unref (GST_OBJECT (bus));
167 cleanup_vorbisdec (vorbisdec);
172 static vorbis_comment vc;
173 static vorbis_dsp_state vd;
174 static vorbis_info vi;
175 static vorbis_block vb;
178 _create_codebook_header_buffer (void)
182 ogg_packet header_comm;
183 ogg_packet header_code;
185 vorbis_info_init (&vi);
186 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
187 vorbis_encode_setup_init (&vi);
188 vorbis_analysis_init (&vd, &vi);
189 vorbis_block_init (&vd, &vb);
190 vorbis_comment_init (&vc);
191 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
193 buffer = gst_buffer_new_and_alloc (header_code.bytes);
194 gst_buffer_fill (buffer, 0, header_code.packet, header_code.bytes);
200 _create_audio_buffer (void)
204 float **vorbis_buffer;
207 vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
208 for (i = 0; i < 44100 * 1; ++i)
209 vorbis_buffer[0][i] = 0.0;
210 vorbis_analysis_wrote (&vd, 44100);
211 vorbis_analysis_blockout (&vd, &vb);
212 vorbis_analysis (&vb, NULL);
213 vorbis_bitrate_addblock (&vb);
214 vorbis_bitrate_flushpacket (&vd, &packet);
215 buffer = gst_buffer_new_and_alloc (packet.bytes);
216 gst_buffer_fill (buffer, 0, packet.packet, packet.bytes);
218 vorbis_comment_clear (&vc);
219 vorbis_block_clear (&vb);
220 vorbis_dsp_clear (&vd);
221 vorbis_info_clear (&vi);
226 GST_START_TEST (test_empty_vorbis_packet)
228 GstElement *vorbisdec;
233 vorbisdec = setup_vorbisdec ();
234 fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
235 GST_STATE_CHANGE_SUCCESS);
237 bus = gst_bus_new ();
239 inbuffer = gst_buffer_new_and_alloc (30);
240 gst_buffer_fill (inbuffer, 0, identification_header, 30);
241 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
242 gst_buffer_ref (inbuffer);
244 gst_element_set_bus (vorbisdec, bus);
246 /* pushing gives away my reference ... */
247 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
248 /* ... and nothing ends up on the global buffer list */
249 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
250 gst_buffer_unref (inbuffer);
251 fail_unless (g_list_length (buffers) == 0);
252 fail_if ((message = gst_bus_pop (bus)) != NULL);
254 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
255 gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
256 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
257 gst_buffer_ref (inbuffer);
259 /* pushing gives away my reference ... */
260 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
261 /* ... and nothing ends up on the global buffer list */
262 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
263 gst_buffer_unref (inbuffer);
264 fail_unless (g_list_length (buffers) == 0);
266 /* send minimal codebook header and audio packers */
267 inbuffer = _create_codebook_header_buffer ();
268 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
270 /* now send an empty vorbis packet, which should just be skipped */
271 inbuffer = gst_buffer_new_and_alloc (0);
272 gst_buffer_ref (inbuffer);
273 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
274 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
275 gst_buffer_unref (inbuffer);
276 fail_unless (g_list_length (buffers) == 0);
278 /* create and push an encoded audio packet */
279 inbuffer = _create_audio_buffer ();
280 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
282 /* now send another empty vorbis packet, which should just be skipped */
283 inbuffer = gst_buffer_new_and_alloc (0);
284 gst_buffer_ref (inbuffer);
285 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
286 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
287 gst_buffer_unref (inbuffer);
289 /* make sure there's no error on the bus */
290 message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
291 fail_if (message != NULL);
294 gst_bus_set_flushing (bus, TRUE);
295 gst_element_set_bus (vorbisdec, NULL);
296 gst_object_unref (GST_OBJECT (bus));
297 cleanup_vorbisdec (vorbisdec);
303 vorbisdec_suite (void)
305 Suite *s = suite_create ("vorbisdec");
306 TCase *tc_chain = tcase_create ("general");
308 suite_add_tcase (s, tc_chain);
309 tcase_add_test (tc_chain, test_identification_header);
310 tcase_add_test (tc_chain, test_empty_vorbis_packet);
315 GST_CHECK_MAIN (vorbisdec);