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 /* FIXME: also tests comment header */
98 GST_START_TEST (test_identification_header)
100 GstElement *vorbisdec;
104 GstTagList *tag_list;
107 vorbisdec = setup_vorbisdec ();
108 fail_unless (gst_element_set_state (vorbisdec,
109 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
110 "could not set to playing");
111 bus = gst_bus_new ();
113 inbuffer = gst_buffer_new_and_alloc (30);
114 memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
115 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
116 gst_buffer_ref (inbuffer);
118 gst_element_set_bus (vorbisdec, bus);
119 /* pushing gives away my reference ... */
120 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
121 /* ... and nothing ends up on the global buffer list */
122 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
123 gst_buffer_unref (inbuffer);
124 fail_unless (g_list_length (buffers) == 0);
125 fail_if ((message = gst_bus_pop (bus)) != NULL);
127 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
128 memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
129 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
130 gst_buffer_ref (inbuffer);
132 /* pushing gives away my reference ... */
133 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
134 /* ... and nothing ends up on the global buffer list */
135 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
136 gst_buffer_unref (inbuffer);
137 fail_unless (g_list_length (buffers) == 0);
138 /* there's a tag message waiting */
139 fail_if ((message = gst_bus_pop (bus)) == NULL);
140 gst_message_parse_tag (message, &tag_list);
141 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
143 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
144 fail_unless_equals_string (artist, "me");
146 fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
147 gst_tag_list_free (tag_list);
148 gst_message_unref (message);
151 gst_bus_set_flushing (bus, TRUE);
152 gst_element_set_bus (vorbisdec, NULL);
153 gst_object_unref (GST_OBJECT (bus));
154 cleanup_vorbisdec (vorbisdec);
159 static vorbis_comment vc;
160 static vorbis_dsp_state vd;
161 static vorbis_info vi;
162 static vorbis_block vb;
165 _create_codebook_header_buffer (void)
169 ogg_packet header_comm;
170 ogg_packet header_code;
172 vorbis_info_init (&vi);
173 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
174 vorbis_encode_setup_init (&vi);
175 vorbis_analysis_init (&vd, &vi);
176 vorbis_block_init (&vd, &vb);
177 vorbis_comment_init (&vc);
178 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
180 buffer = gst_buffer_new_and_alloc (header_code.bytes);
181 memcpy (GST_BUFFER_DATA (buffer), header_code.packet, header_code.bytes);
187 _create_audio_buffer (void)
191 float **vorbis_buffer;
194 vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
195 for (i = 0; i < 44100 * 1; ++i)
196 vorbis_buffer[0][i] = 0.0;
197 vorbis_analysis_wrote (&vd, 44100);
198 vorbis_analysis_blockout (&vd, &vb);
199 vorbis_analysis (&vb, NULL);
200 vorbis_bitrate_addblock (&vb);
201 vorbis_bitrate_flushpacket (&vd, &packet);
202 buffer = gst_buffer_new_and_alloc (packet.bytes);
203 memcpy (GST_BUFFER_DATA (buffer), packet.packet, packet.bytes);
205 vorbis_comment_clear (&vc);
206 vorbis_block_clear (&vb);
207 vorbis_dsp_clear (&vd);
208 vorbis_info_clear (&vi);
213 GST_START_TEST (test_empty_vorbis_packet)
215 GstElement *vorbisdec;
220 vorbisdec = setup_vorbisdec ();
221 fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
222 GST_STATE_CHANGE_SUCCESS);
224 bus = gst_bus_new ();
226 inbuffer = gst_buffer_new_and_alloc (30);
227 memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
228 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
229 gst_buffer_ref (inbuffer);
231 gst_element_set_bus (vorbisdec, bus);
233 /* pushing gives away my reference ... */
234 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
235 /* ... and nothing ends up on the global buffer list */
236 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
237 gst_buffer_unref (inbuffer);
238 fail_unless (g_list_length (buffers) == 0);
239 fail_if ((message = gst_bus_pop (bus)) != NULL);
241 inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
242 memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
243 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
244 gst_buffer_ref (inbuffer);
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);
253 /* send minimal codebook header and audio packers */
254 inbuffer = _create_codebook_header_buffer ();
255 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
257 /* now send an empty vorbis packet, which should just be skipped */
258 inbuffer = gst_buffer_new_and_alloc (0);
259 gst_buffer_ref (inbuffer);
260 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
261 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
262 gst_buffer_unref (inbuffer);
263 fail_unless (g_list_length (buffers) == 0);
265 /* create and push an encoded audio packet */
266 inbuffer = _create_audio_buffer ();
267 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
269 /* now send another empty vorbis packet, which should just be skipped */
270 inbuffer = gst_buffer_new_and_alloc (0);
271 gst_buffer_ref (inbuffer);
272 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
273 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
274 gst_buffer_unref (inbuffer);
276 /* make sure there's no error on the bus */
277 message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
278 fail_if (message != NULL);
281 gst_bus_set_flushing (bus, TRUE);
282 gst_element_set_bus (vorbisdec, NULL);
283 gst_object_unref (GST_OBJECT (bus));
284 cleanup_vorbisdec (vorbisdec);
290 vorbisdec_suite (void)
292 Suite *s = suite_create ("vorbisdec");
293 TCase *tc_chain = tcase_create ("general");
295 suite_add_tcase (s, tc_chain);
296 tcase_add_test (tc_chain, test_identification_header);
297 tcase_add_test (tc_chain, test_empty_vorbis_packet);
302 GST_CHECK_MAIN (vorbisdec);