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);
100 mysinkpad = gst_check_setup_sink_pad (vorbistag, &sinktemplate);
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);
121 static GstPadProbeReturn
122 buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer unused)
124 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
126 g_async_queue_push (pending_buffers, gst_buffer_ref (buffer));
128 return GST_PAD_PROBE_OK;
132 start_pipeline (GstElement * element)
134 id = gst_pad_add_probe (mysinkpad, GST_PAD_PROBE_TYPE_BUFFER,
135 (GstPadProbeCallback) buffer_probe, NULL, NULL);
137 pending_buffers = g_async_queue_new ();
138 gst_element_set_state (element, GST_STATE_PLAYING);
144 return GST_BUFFER (g_async_queue_pop (pending_buffers));
148 stop_pipeline (GstElement * element)
152 while ((buf = g_async_queue_try_pop (pending_buffers)))
153 gst_buffer_unref (buf);
155 gst_pad_remove_probe (mysinkpad, id);
158 gst_element_set_state (element, GST_STATE_NULL);
160 while ((buf = g_async_queue_try_pop (pending_buffers)))
161 gst_buffer_unref (buf);
163 g_async_queue_unref (pending_buffers);
164 pending_buffers = NULL;
168 compare_buffer (GstBuffer * buf, const guint8 * data, gsize size)
172 gst_buffer_map (buf, &map, GST_MAP_READ);
173 fail_unless_equals_int (map.size, size);
174 fail_unless_equals_int (memcmp (map.data, data, size), 0);
175 gst_buffer_unmap (buf, &map);
178 static vorbis_comment vc;
179 static vorbis_dsp_state vd;
180 static vorbis_info vi;
181 static vorbis_block vb;
184 _create_codebook_header_buffer (void)
188 ogg_packet header_comm;
189 ogg_packet header_code;
191 vorbis_info_init (&vi);
192 vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
193 vorbis_encode_setup_init (&vi);
194 vorbis_analysis_init (&vd, &vi);
195 vorbis_block_init (&vd, &vb);
196 vorbis_comment_init (&vc);
197 vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
199 buffer = gst_buffer_new_and_alloc (header_code.bytes);
200 gst_buffer_fill (buffer, 0, header_code.packet, header_code.bytes);
206 _create_audio_buffer (void)
210 float **vorbis_buffer G_GNUC_UNUSED;
212 vorbis_buffer = vorbis_analysis_buffer (&vd, 0);
213 vorbis_analysis_wrote (&vd, 0);
214 vorbis_analysis_blockout (&vd, &vb);
215 vorbis_analysis (&vb, NULL);
216 vorbis_bitrate_addblock (&vb);
217 vorbis_bitrate_flushpacket (&vd, &packet);
218 buffer = gst_buffer_new_and_alloc (packet.bytes);
219 gst_buffer_fill (buffer, 0, packet.packet, packet.bytes);
220 GST_DEBUG ("%p %d", packet.packet, packet.bytes);
222 vorbis_comment_clear (&vc);
223 vorbis_block_clear (&vb);
224 vorbis_dsp_clear (&vd);
225 vorbis_info_clear (&vi);
231 GST_START_TEST (test_empty_tags_set)
234 GstElement *vorbistag;
235 GstBuffer *inbuffer, *outbuffer;
237 vorbistag = setup_vorbistag ();
239 tags = gst_tag_list_new_empty ();
240 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, "foobar", NULL);
241 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
242 GST_TAG_MERGE_REPLACE);
243 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
244 GST_TAG_MERGE_KEEP_ALL);
245 gst_tag_list_free (tags);
247 start_pipeline (vorbistag);
249 /* send identification header */
250 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
251 gst_buffer_fill (inbuffer, 0, identification_header,
252 sizeof (identification_header));
253 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
255 /* send empty comment buffer */
256 inbuffer = gst_buffer_new_and_alloc (sizeof (empty_comment_header));
257 gst_buffer_fill (inbuffer, 0, empty_comment_header,
258 sizeof (empty_comment_header));
259 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
261 /* send minimal codebook header and audio packers */
262 inbuffer = _create_codebook_header_buffer ();
263 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
264 inbuffer = _create_audio_buffer ();
265 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
268 /* check identification header is unchanged */
269 outbuffer = get_buffer ();
270 compare_buffer (outbuffer, identification_header,
271 sizeof (identification_header));
272 gst_buffer_unref (outbuffer);
274 /* check comment header is correct */
275 outbuffer = get_buffer ();
276 compare_buffer (outbuffer, title_comment_header,
277 sizeof (title_comment_header));
278 gst_buffer_unref (outbuffer);
280 stop_pipeline (vorbistag);
281 cleanup_vorbistag (vorbistag);
287 GST_START_TEST (test_filled_tags_unset)
290 GstElement *vorbistag;
291 GstBuffer *inbuffer, *outbuffer;
293 vorbistag = setup_vorbistag ();
295 tags = gst_tag_list_new_empty ();
296 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
297 GST_TAG_MERGE_REPLACE);
298 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
299 GST_TAG_MERGE_KEEP_ALL);
300 gst_tag_list_free (tags);
302 start_pipeline (vorbistag);
304 /* send identification header */
305 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
306 gst_buffer_fill (inbuffer, 0, identification_header,
307 sizeof (identification_header));
308 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
310 /* send empty comment buffer */
311 inbuffer = gst_buffer_new_and_alloc (sizeof (title_comment_header));
312 gst_buffer_fill (inbuffer, 0, title_comment_header,
313 sizeof (title_comment_header));
314 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
316 /* send minimal codebook header and audio packers */
317 inbuffer = _create_codebook_header_buffer ();
318 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
319 inbuffer = _create_audio_buffer ();
320 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
323 /* check identification header is unchanged */
324 outbuffer = get_buffer ();
325 compare_buffer (outbuffer, identification_header,
326 sizeof (identification_header));
327 gst_buffer_unref (outbuffer);
329 /* check comment header is correct */
330 outbuffer = get_buffer ();
331 compare_buffer (outbuffer, empty_comment_header,
332 sizeof (empty_comment_header));
333 gst_buffer_unref (outbuffer);
335 stop_pipeline (vorbistag);
336 cleanup_vorbistag (vorbistag);
342 GST_START_TEST (test_filled_tags_change)
345 GstElement *vorbistag;
346 GstBuffer *inbuffer, *outbuffer;
348 vorbistag = setup_vorbistag ();
350 tags = gst_tag_list_new_empty ();
351 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, "foobar", NULL);
352 gst_tag_setter_merge_tags (GST_TAG_SETTER (vorbistag), tags,
353 GST_TAG_MERGE_REPLACE);
354 gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (vorbistag),
355 GST_TAG_MERGE_KEEP_ALL);
356 gst_tag_list_free (tags);
358 start_pipeline (vorbistag);
360 /* send identification header */
361 inbuffer = gst_buffer_new_and_alloc (sizeof (identification_header));
362 gst_buffer_fill (inbuffer, 0, identification_header,
363 sizeof (identification_header));
364 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
366 /* send empty comment buffer */
367 inbuffer = gst_buffer_new_and_alloc (sizeof (artist_comment_header));
368 gst_buffer_fill (inbuffer, 0, artist_comment_header,
369 sizeof (artist_comment_header));
370 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
372 /* send minimal codebook header and audio packers */
373 inbuffer = _create_codebook_header_buffer ();
374 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
375 inbuffer = _create_audio_buffer ();
376 fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
379 /* check identification header is unchanged */
380 outbuffer = get_buffer ();
381 compare_buffer (outbuffer, identification_header,
382 sizeof (identification_header));
383 gst_buffer_unref (outbuffer);
385 /* check comment header is correct */
386 outbuffer = get_buffer ();
387 compare_buffer (outbuffer, title_comment_header,
388 sizeof (title_comment_header));
389 gst_buffer_unref (outbuffer);
391 stop_pipeline (vorbistag);
392 cleanup_vorbistag (vorbistag);
400 vorbistag_suite (void)
402 Suite *s = suite_create ("vorbistag");
403 TCase *tc_chain = tcase_create ("general");
405 suite_add_tcase (s, tc_chain);
406 tcase_add_test (tc_chain, test_empty_tags_set);
407 tcase_add_test (tc_chain, test_filled_tags_unset);
408 tcase_add_test (tc_chain, test_filled_tags_change);
413 GST_CHECK_MAIN (vorbistag)