• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for vorbisdec
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
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.
11  *
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.
16  *
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.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/check/gstcheck.h>
27 
28 #include <vorbis/codec.h>
29 #include <vorbis/vorbisenc.h>
30 
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;
35 
36 /* a valid first header packet */
37 static guchar identification_header[30] = {
38   1,                            /* packet_type */
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 */
48 };
49 
50 static guchar comment_header[] = {
51   3,                            /* packet_type */
52   'v', 'o', 'r', 'b', 'i', 's',
53   2, 0, 0, 0,                   /* vendor_length */
54   'm', 'e',
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 */
59 };
60 
61 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS_ANY);
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69 
70 static GstElement *
setup_vorbisdec(void)71 setup_vorbisdec (void)
72 {
73   GstElement *vorbisdec;
74   GstCaps *caps;
75 
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);
81 
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);
85 
86   gst_pad_set_active (mysinkpad, TRUE);
87 
88   return vorbisdec;
89 }
90 
91 static void
cleanup_vorbisdec(GstElement * vorbisdec)92 cleanup_vorbisdec (GstElement * vorbisdec)
93 {
94   GST_DEBUG ("cleanup_vorbisdec");
95   gst_element_set_state (vorbisdec, GST_STATE_NULL);
96 
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);
102 
103   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
104   buffers = NULL;
105 }
106 
107 /* FIXME: also tests comment header */
GST_START_TEST(test_identification_header)108 GST_START_TEST (test_identification_header)
109 {
110   GstElement *vorbisdec;
111   GstBuffer *inbuffer;
112   GstBus *bus;
113   GstMessage *message;
114 
115   vorbisdec = setup_vorbisdec ();
116 
117   fail_unless (gst_element_set_state (vorbisdec,
118           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
119       "could not set to playing");
120   bus = gst_bus_new ();
121 
122   inbuffer = gst_buffer_new_and_alloc (30);
123   gst_buffer_fill (inbuffer, 0, identification_header, 30);
124   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
125   gst_buffer_ref (inbuffer);
126 
127   gst_element_set_bus (vorbisdec, bus);
128   /* pushing gives away my reference ... */
129   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
130   /* ... and nothing ends up on the global buffer list */
131   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
132   gst_buffer_unref (inbuffer);
133   fail_unless (g_list_length (buffers) == 0);
134   fail_if ((message = gst_bus_pop (bus)) != NULL);
135 
136   inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
137   gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
138   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
139   gst_buffer_ref (inbuffer);
140 
141   /* pushing gives away my reference ... */
142   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
143   /* ... and nothing ends up on the global buffer list */
144   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
145   gst_buffer_unref (inbuffer);
146   fail_unless (g_list_length (buffers) == 0);
147 
148 #if 0
149   /* there's a tag message waiting */
150   fail_if ((message = gst_bus_pop (bus)) == NULL);
151   gst_message_parse_tag (message, &tag_list);
152   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
153       1);
154   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
155   fail_unless_equals_string (artist, "me");
156   g_free (artist);
157   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
158   gst_tag_list_unref (tag_list);
159   gst_message_unref (message);
160 #endif
161 
162   /* make sure there's no error on the bus */
163   message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR);
164   fail_if (message != NULL);
165 
166   /* cleanup */
167   gst_bus_set_flushing (bus, TRUE);
168   gst_element_set_bus (vorbisdec, NULL);
169   gst_object_unref (GST_OBJECT (bus));
170   cleanup_vorbisdec (vorbisdec);
171 }
172 
173 GST_END_TEST;
174 
175 static vorbis_comment vc;
176 static vorbis_dsp_state vd;
177 static vorbis_info vi;
178 static vorbis_block vb;
179 
180 static GstBuffer *
_create_codebook_header_buffer(void)181 _create_codebook_header_buffer (void)
182 {
183   GstBuffer *buffer;
184   ogg_packet header;
185   ogg_packet header_comm;
186   ogg_packet header_code;
187 
188   vorbis_info_init (&vi);
189   vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
190   vorbis_encode_setup_init (&vi);
191   vorbis_analysis_init (&vd, &vi);
192   vorbis_block_init (&vd, &vb);
193   vorbis_comment_init (&vc);
194   vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
195 
196   buffer = gst_buffer_new_and_alloc (header_code.bytes);
197   gst_buffer_fill (buffer, 0, header_code.packet, header_code.bytes);
198 
199   return buffer;
200 }
201 
202 static GstBuffer *
_create_audio_buffer(void)203 _create_audio_buffer (void)
204 {
205   GstBuffer *buffer;
206   ogg_packet packet;
207   float **vorbis_buffer;
208   gint i;
209 
210   vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
211   for (i = 0; i < 44100 * 1; ++i)
212     vorbis_buffer[0][i] = 0.0;
213   vorbis_analysis_wrote (&vd, 44100);
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 
221   vorbis_comment_clear (&vc);
222   vorbis_block_clear (&vb);
223   vorbis_dsp_clear (&vd);
224   vorbis_info_clear (&vi);
225 
226   return buffer;
227 }
228 
GST_START_TEST(test_empty_vorbis_packet)229 GST_START_TEST (test_empty_vorbis_packet)
230 {
231   GstElement *vorbisdec;
232   GstBuffer *inbuffer;
233   GstMessage *message;
234   GstBus *bus;
235 
236   vorbisdec = setup_vorbisdec ();
237   fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
238       GST_STATE_CHANGE_SUCCESS);
239 
240   bus = gst_bus_new ();
241 
242   inbuffer = gst_buffer_new_and_alloc (30);
243   gst_buffer_fill (inbuffer, 0, identification_header, 30);
244   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
245   gst_buffer_ref (inbuffer);
246 
247   gst_element_set_bus (vorbisdec, bus);
248 
249   /* pushing gives away my reference ... */
250   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
251   /* ... and nothing ends up on the global buffer list */
252   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
253   gst_buffer_unref (inbuffer);
254   fail_unless (g_list_length (buffers) == 0);
255   fail_if ((message = gst_bus_pop (bus)) != NULL);
256 
257   inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
258   gst_buffer_fill (inbuffer, 0, comment_header, sizeof (comment_header));
259   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
260   gst_buffer_ref (inbuffer);
261 
262   /* pushing gives away my reference ... */
263   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
264   /* ... and nothing ends up on the global buffer list */
265   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
266   gst_buffer_unref (inbuffer);
267   fail_unless (g_list_length (buffers) == 0);
268 
269   /* send minimal codebook header and audio packers */
270   inbuffer = _create_codebook_header_buffer ();
271   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
272 
273   /* now send an empty vorbis packet, which should just be skipped */
274   inbuffer = gst_buffer_new_and_alloc (0);
275   gst_buffer_ref (inbuffer);
276   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
277   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
278   gst_buffer_unref (inbuffer);
279   fail_unless (g_list_length (buffers) == 0);
280 
281   /* create and push an encoded audio packet */
282   inbuffer = _create_audio_buffer ();
283   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
284 
285   /* now send another empty vorbis packet, which should just be skipped */
286   inbuffer = gst_buffer_new_and_alloc (0);
287   gst_buffer_ref (inbuffer);
288   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
289   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
290   gst_buffer_unref (inbuffer);
291 
292   /* make sure there's no error on the bus */
293   message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
294   fail_if (message != NULL);
295 
296   /* cleanup */
297   gst_bus_set_flushing (bus, TRUE);
298   gst_element_set_bus (vorbisdec, NULL);
299   gst_object_unref (GST_OBJECT (bus));
300   cleanup_vorbisdec (vorbisdec);
301 }
302 
303 GST_END_TEST;
304 
305 static Suite *
vorbisdec_suite(void)306 vorbisdec_suite (void)
307 {
308   Suite *s = suite_create ("vorbisdec");
309   TCase *tc_chain = tcase_create ("general");
310 
311   suite_add_tcase (s, tc_chain);
312   tcase_add_test (tc_chain, test_identification_header);
313   tcase_add_test (tc_chain, test_empty_vorbis_packet);
314 
315   return s;
316 }
317 
318 GST_CHECK_MAIN (vorbisdec);
319