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.
26 #include <vorbis/codec.h>
27 #include <vorbis/vorbisenc.h>
29 #include <gst/gsttagsetter.h>
30 #include <gst/check/gstcheck.h>
32 /* a valid first header packet */
33 static guchar identification_header[30] = {
35 'v', 'o', 'r', 'b', 'i', 's',
36 0, 0, 0, 0, /* vorbis_version */
37 2, /* audio_channels */
38 0x44, 0xac, 0, 0, /* sample_rate */
39 0xff, 0xff, 0xff, 0xff, /* bitrate_maximum */
40 0x00, 0xee, 0x02, 0x00, /* bitrate_nominal */
41 0xff, 0xff, 0xff, 0xff, /* bitrate_minimum */
42 0xb8, /* blocksize_0, blocksize_1 */
43 0x01 /* framing_flag */
46 static guchar artist_comment_header[] = {
48 'v', 'o', 'r', 'b', 'i', 's',
49 2, 0, 0, 0, /* vendor_length */
51 1, 0, 0, 0, /* user_comment_list_length */
52 9, 0, 0, 0, /* length comment[0] */
53 'A', 'R', 'T', 'I', 'S', 'T', '=', 'm', 'e',
54 0x01, /* framing bit */
57 static guchar title_comment_header[] = {
59 'v', 'o', 'r', 'b', 'i', 's',
60 2, 0, 0, 0, /* vendor_length */
62 1, 0, 0, 0, /* user_comment_list_length */
63 12, 0, 0, 0, /* length comment[0] */
64 'T', 'I', 'T', 'L', 'E', '=', 'f', 'o', 'o', 'b', 'a', 'r',
65 0x01, /* framing bit */
68 static guchar empty_comment_header[] = {
70 'v', 'o', 'r', 'b', 'i', 's',
71 2, 0, 0, 0, /* vendor_length */
73 0, 0, 0, 0, /* user_comment_list_length */
74 0x01, /* framing bit */
78 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
82 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
87 static GstPad *mysrcpad, *mysinkpad;
88 static GAsyncQueue *pending_buffers;
93 setup_vorbistag (void)
95 GstElement *vorbistag;
97 GST_DEBUG ("setup_vorbistag");
98 vorbistag = gst_check_setup_element ("vorbistag");
99 mysrcpad = gst_check_setup_src_pad (vorbistag, &srctemplate, NULL);
100 mysinkpad = gst_check_setup_sink_pad (vorbistag, &sinktemplate, NULL);
101 gst_pad_set_active (mysrcpad, TRUE);
102 gst_pad_set_active (mysinkpad, TRUE);
108 cleanup_vorbistag (GstElement * vorbistag)
110 GST_DEBUG ("cleanup_vorbistag");
111 gst_element_set_state (vorbistag, GST_STATE_NULL);
113 gst_pad_set_active (mysrcpad, FALSE);
114 gst_pad_set_active (mysinkpad, FALSE);
115 gst_check_teardown_src_pad (vorbistag);
116 gst_check_teardown_sink_pad (vorbistag);
117 gst_check_teardown_element (vorbistag);
122 buffer_probe (GstPad * pad, GstBuffer * buffer, gpointer unused)
124 g_async_queue_push (pending_buffers, gst_buffer_ref (buffer));
129 start_pipeline (GstElement * element)
131 id = gst_pad_add_buffer_probe (mysinkpad, G_CALLBACK (buffer_probe), NULL);
133 pending_buffers = g_async_queue_new ();
134 gst_element_set_state (element, GST_STATE_PLAYING);
140 return GST_BUFFER (g_async_queue_pop (pending_buffers));
144 stop_pipeline (GstElement * element)
148 while ((buf = g_async_queue_try_pop (pending_buffers)))
149 gst_buffer_unref (buf);
151 gst_pad_remove_buffer_probe (mysinkpad, (guint) id);
154 gst_element_set_state (element, GST_STATE_NULL);
156 while ((buf = g_async_queue_try_pop (pending_buffers)))
157 gst_buffer_unref (buf);
159 g_async_queue_unref (pending_buffers);
160 pending_buffers = NULL;
163 static vorbis_comment vc;
164 static vorbis_dsp_state vd;
165 static vorbis_info vi;
166 static vorbis_block vb;
169 _create_codebook_header_buffer (void)
173 ogg_packet header_comm;
174 ogg_packet header_code;
176 vorbis_info_init (&vi);
177 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
178 vorbis_encode_setup_init (&vi);
179 vorbis_analysis_init (&vd, &vi);
180 vorbis_block_init (&vd, &vb);
181 vorbis_comment_init (&vc);
182 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
184 buffer = gst_buffer_new_and_alloc (header_code.bytes);
185 memcpy (GST_BUFFER_DATA (buffer), header_code.packet, header_code.bytes);
191 _create_audio_buffer (void)
195 float **vorbis_buffer G_GNUC_UNUSED;
197 vorbis_buffer = vorbis_analysis_buffer (&vd, 0);
198 vorbis_analysis_wrote (&vd, 0);
199 vorbis_analysis_blockout (&vd, &vb);
200 vorbis_analysis (&vb, NULL);
201 vorbis_bitrate_addblock (&vb);
202 vorbis_bitrate_flushpacket (&vd, &packet);
203 buffer = gst_buffer_new_and_alloc (packet.bytes);
204 memcpy (GST_BUFFER_DATA (buffer), packet.packet, packet.bytes);
206 vorbis_comment_clear (&vc);
207 vorbis_block_clear (&vb);
208 vorbis_dsp_clear (&vd);
209 vorbis_info_clear (&vi);
215 GST_START_TEST (test_empty_tags_set)
218 GstElement *vorbistag;
219 GstBuffer *inbuffer, *outbuffer;
221 vorbistag = setup_vorbistag ();
223 tags = gst_tag_list_new ();
224 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, "foobar", NULL);
225 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
226 GST_TAG_MERGE_REPLACE);
227 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
228 GST_TAG_MERGE_KEEP_ALL);
229 gst_tag_list_free (tags);
231 start_pipeline (vorbistag);
233 /* send identification header */
234 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
235 memcpy (GST_BUFFER_DATA (inbuffer), identification_header,
236 sizeof (identification_header));
237 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
239 /* send empty comment buffer */
240 inbuffer = gst_buffer_new_and_alloc (sizeof (empty_comment_header));
241 memcpy (GST_BUFFER_DATA (inbuffer), empty_comment_header,
242 sizeof (empty_comment_header));
243 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
245 /* send minimal codebook header and audio packers */
246 inbuffer = _create_codebook_header_buffer ();
247 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
248 inbuffer = _create_audio_buffer ();
249 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
252 /* check identification header is unchanged */
253 outbuffer = get_buffer ();
254 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
255 sizeof (identification_header));
256 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
257 identification_header, sizeof (identification_header)), 0);
258 gst_buffer_unref (outbuffer);
260 /* check comment header is correct */
261 outbuffer = get_buffer ();
262 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
263 sizeof (title_comment_header));
264 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
265 title_comment_header, sizeof (title_comment_header)), 0);
266 gst_buffer_unref (outbuffer);
268 stop_pipeline (vorbistag);
269 cleanup_vorbistag (vorbistag);
275 GST_START_TEST (test_filled_tags_unset)
278 GstElement *vorbistag;
279 GstBuffer *inbuffer, *outbuffer;
281 vorbistag = setup_vorbistag ();
283 tags = gst_tag_list_new ();
284 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
285 GST_TAG_MERGE_REPLACE);
286 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
287 GST_TAG_MERGE_KEEP_ALL);
288 gst_tag_list_free (tags);
290 start_pipeline (vorbistag);
292 /* send identification header */
293 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
294 memcpy (GST_BUFFER_DATA (inbuffer), identification_header,
295 sizeof (identification_header));
296 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
298 /* send empty comment buffer */
299 inbuffer = gst_buffer_new_and_alloc (sizeof (title_comment_header));
300 memcpy (GST_BUFFER_DATA (inbuffer), title_comment_header,
301 sizeof (title_comment_header));
302 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
304 /* send minimal codebook header and audio packers */
305 inbuffer = _create_codebook_header_buffer ();
306 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
307 inbuffer = _create_audio_buffer ();
308 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
311 /* check identification header is unchanged */
312 outbuffer = get_buffer ();
313 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
314 sizeof (identification_header));
315 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
316 identification_header, sizeof (identification_header)), 0);
317 gst_buffer_unref (outbuffer);
319 /* check comment header is correct */
320 outbuffer = get_buffer ();
321 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
322 sizeof (empty_comment_header));
323 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
324 empty_comment_header, sizeof (empty_comment_header)), 0);
325 gst_buffer_unref (outbuffer);
327 stop_pipeline (vorbistag);
328 cleanup_vorbistag (vorbistag);
334 GST_START_TEST (test_filled_tags_change)
337 GstElement *vorbistag;
338 GstBuffer *inbuffer, *outbuffer;
340 vorbistag = setup_vorbistag ();
342 tags = gst_tag_list_new ();
343 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, "foobar", NULL);
344 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
345 GST_TAG_MERGE_REPLACE);
346 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
347 GST_TAG_MERGE_KEEP_ALL);
348 gst_tag_list_free (tags);
350 start_pipeline (vorbistag);
352 /* send identification header */
353 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
354 memcpy (GST_BUFFER_DATA (inbuffer), identification_header,
355 sizeof (identification_header));
356 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
358 /* send empty comment buffer */
359 inbuffer = gst_buffer_new_and_alloc (sizeof (artist_comment_header));
360 memcpy (GST_BUFFER_DATA (inbuffer), artist_comment_header,
361 sizeof (artist_comment_header));
362 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
364 /* send minimal codebook header and audio packers */
365 inbuffer = _create_codebook_header_buffer ();
366 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
367 inbuffer = _create_audio_buffer ();
368 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
371 /* check identification header is unchanged */
372 outbuffer = get_buffer ();
373 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
374 sizeof (identification_header));
375 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
376 identification_header, sizeof (identification_header)), 0);
377 gst_buffer_unref (outbuffer);
379 /* check comment header is correct */
380 outbuffer = get_buffer ();
381 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer),
382 sizeof (title_comment_header));
383 fail_unless_equals_int (memcmp (GST_BUFFER_DATA (outbuffer),
384 title_comment_header, sizeof (title_comment_header)), 0);
385 gst_buffer_unref (outbuffer);
387 stop_pipeline (vorbistag);
388 cleanup_vorbistag (vorbistag);
396 vorbistag_suite (void)
398 Suite *s = suite_create ("vorbistag");
399 TCase *tc_chain = tcase_create ("general");
401 suite_add_tcase (s, tc_chain);
402 tcase_add_test (tc_chain, test_empty_tags_set);
403 tcase_add_test (tc_chain, test_filled_tags_unset);
404 tcase_add_test (tc_chain, test_filled_tags_change);
409 GST_CHECK_MAIN (vorbistag)