tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / elements / vorbisdec.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27 #include <vorbis/codec.h>
28 #include <vorbis/vorbisenc.h>
29
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;
34
35 /* a valid first header packet */
36 static guchar identification_header[30] = {
37   1,                            /* packet_type */
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 */
47 };
48
49 static guchar comment_header[] = {
50   3,                            /* packet_type */
51   'v', 'o', 'r', 'b', 'i', 's',
52   2, 0, 0, 0,                   /* vendor_length */
53   'm', 'e',
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 */
58 };
59
60 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS_ANY);
64 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 static GstElement *
70 setup_vorbisdec (void)
71 {
72   GstElement *vorbisdec;
73
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);
80
81   return vorbisdec;
82 }
83
84 static void
85 cleanup_vorbisdec (GstElement * vorbisdec)
86 {
87   GST_DEBUG ("cleanup_vorbisdec");
88   gst_element_set_state (vorbisdec, GST_STATE_NULL);
89
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);
95 }
96
97 /* FIXME: also tests comment header */
98 GST_START_TEST (test_identification_header)
99 {
100   GstElement *vorbisdec;
101   GstBuffer *inbuffer;
102   GstBus *bus;
103   GstMessage *message;
104   GstTagList *tag_list;
105   gchar *artist;
106
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 ();
112
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);
117
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);
126
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);
131
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),
142       1);
143   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
144   fail_unless_equals_string (artist, "me");
145   g_free (artist);
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);
149
150   /* cleanup */
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);
155 }
156
157 GST_END_TEST;
158
159 static vorbis_comment vc;
160 static vorbis_dsp_state vd;
161 static vorbis_info vi;
162 static vorbis_block vb;
163
164 static GstBuffer *
165 _create_codebook_header_buffer (void)
166 {
167   GstBuffer *buffer;
168   ogg_packet header;
169   ogg_packet header_comm;
170   ogg_packet header_code;
171
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);
179
180   buffer = gst_buffer_new_and_alloc (header_code.bytes);
181   memcpy (GST_BUFFER_DATA (buffer), header_code.packet, header_code.bytes);
182
183   return buffer;
184 }
185
186 static GstBuffer *
187 _create_audio_buffer (void)
188 {
189   GstBuffer *buffer;
190   ogg_packet packet;
191   float **vorbis_buffer;
192   gint i;
193
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);
204
205   vorbis_comment_clear (&vc);
206   vorbis_block_clear (&vb);
207   vorbis_dsp_clear (&vd);
208   vorbis_info_clear (&vi);
209
210   return buffer;
211 }
212
213 GST_START_TEST (test_empty_vorbis_packet)
214 {
215   GstElement *vorbisdec;
216   GstBuffer *inbuffer;
217   GstMessage *message;
218   GstBus *bus;
219
220   vorbisdec = setup_vorbisdec ();
221   fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
222       GST_STATE_CHANGE_SUCCESS);
223
224   bus = gst_bus_new ();
225
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);
230
231   gst_element_set_bus (vorbisdec, bus);
232
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);
240
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);
245
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
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);
256
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);
264
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);
268
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);
275
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);
279
280   /* cleanup */
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);
285 }
286
287 GST_END_TEST;
288
289 static Suite *
290 vorbisdec_suite (void)
291 {
292   Suite *s = suite_create ("vorbisdec");
293   TCase *tc_chain = tcase_create ("general");
294
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);
298
299   return s;
300 }
301
302 GST_CHECK_MAIN (vorbisdec);