Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 GST_START_TEST (test_empty_identification_header)
98 {
99   GstElement *vorbisdec;
100   GstBuffer *inbuffer;
101   GstBus *bus;
102   GstMessage *message;
103
104   vorbisdec = setup_vorbisdec ();
105   bus = gst_bus_new ();
106
107   fail_unless (gst_element_set_state (vorbisdec,
108           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
109       "could not set to playing");
110
111   inbuffer = gst_buffer_new_and_alloc (0);
112   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
113
114   /* set a bus here so we avoid getting state change messages */
115   gst_element_set_bus (vorbisdec, bus);
116
117   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
118   /* ... but it ends up being collected on the global buffer list */
119   fail_unless_equals_int (g_list_length (buffers), 0);
120
121   fail_if ((message = gst_bus_pop (bus)) == NULL);
122   fail_unless_message_error (message, STREAM, DECODE);
123   gst_message_unref (message);
124   gst_element_set_bus (vorbisdec, NULL);
125
126   /* cleanup */
127   gst_object_unref (GST_OBJECT (bus));
128   cleanup_vorbisdec (vorbisdec);
129 }
130
131 GST_END_TEST;
132
133 /* FIXME: also tests comment header */
134 GST_START_TEST (test_identification_header)
135 {
136   GstElement *vorbisdec;
137   GstBuffer *inbuffer;
138   GstBus *bus;
139   GstMessage *message;
140   GstTagList *tag_list;
141   gchar *artist;
142
143   vorbisdec = setup_vorbisdec ();
144   fail_unless (gst_element_set_state (vorbisdec,
145           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
146       "could not set to playing");
147   bus = gst_bus_new ();
148
149   inbuffer = gst_buffer_new_and_alloc (30);
150   memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
151   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
152   gst_buffer_ref (inbuffer);
153
154   gst_element_set_bus (vorbisdec, bus);
155   /* pushing gives away my reference ... */
156   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
157   /* ... and nothing ends up on the global buffer list */
158   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
159   gst_buffer_unref (inbuffer);
160   fail_unless (g_list_length (buffers) == 0);
161   fail_if ((message = gst_bus_pop (bus)) != NULL);
162
163   inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
164   memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
165   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
166   gst_buffer_ref (inbuffer);
167
168   /* pushing gives away my reference ... */
169   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
170   /* ... and nothing ends up on the global buffer list */
171   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
172   gst_buffer_unref (inbuffer);
173   fail_unless (g_list_length (buffers) == 0);
174   /* there's a tag message waiting */
175   fail_if ((message = gst_bus_pop (bus)) == NULL);
176   gst_message_parse_tag (message, &tag_list);
177   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
178       1);
179   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
180   fail_unless_equals_string (artist, "me");
181   g_free (artist);
182   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
183   gst_tag_list_free (tag_list);
184   gst_message_unref (message);
185
186   /* cleanup */
187   gst_bus_set_flushing (bus, TRUE);
188   gst_element_set_bus (vorbisdec, NULL);
189   gst_object_unref (GST_OBJECT (bus));
190   cleanup_vorbisdec (vorbisdec);
191 }
192
193 GST_END_TEST;
194
195 static vorbis_comment vc;
196 static vorbis_dsp_state vd;
197 static vorbis_info vi;
198 static vorbis_block vb;
199
200 static GstBuffer *
201 _create_codebook_header_buffer (void)
202 {
203   GstBuffer *buffer;
204   ogg_packet header;
205   ogg_packet header_comm;
206   ogg_packet header_code;
207
208   vorbis_info_init (&vi);
209   vorbis_encode_setup_vbr (&vi, 1, 44000, 0.5);
210   vorbis_encode_setup_init (&vi);
211   vorbis_analysis_init (&vd, &vi);
212   vorbis_block_init (&vd, &vb);
213   vorbis_comment_init (&vc);
214   vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);
215
216   buffer = gst_buffer_new_and_alloc (header_code.bytes);
217   memcpy (GST_BUFFER_DATA (buffer), header_code.packet, header_code.bytes);
218
219   return buffer;
220 }
221
222 static GstBuffer *
223 _create_audio_buffer (void)
224 {
225   GstBuffer *buffer;
226   ogg_packet packet;
227   float **vorbis_buffer;
228   gint i;
229
230   vorbis_buffer = vorbis_analysis_buffer (&vd, 44100);
231   for (i = 0; i < 44100 * 1; ++i)
232     vorbis_buffer[0][i] = 0.0;
233   vorbis_analysis_wrote (&vd, 44100);
234   vorbis_analysis_blockout (&vd, &vb);
235   vorbis_analysis (&vb, NULL);
236   vorbis_bitrate_addblock (&vb);
237   vorbis_bitrate_flushpacket (&vd, &packet);
238   buffer = gst_buffer_new_and_alloc (packet.bytes);
239   memcpy (GST_BUFFER_DATA (buffer), packet.packet, packet.bytes);
240
241   vorbis_comment_clear (&vc);
242   vorbis_block_clear (&vb);
243   vorbis_dsp_clear (&vd);
244   vorbis_info_clear (&vi);
245
246   return buffer;
247 }
248
249 GST_START_TEST (test_empty_vorbis_packet)
250 {
251   GstElement *vorbisdec;
252   GstBuffer *inbuffer;
253   GstMessage *message;
254   GstBus *bus;
255
256   vorbisdec = setup_vorbisdec ();
257   fail_unless_equals_int (gst_element_set_state (vorbisdec, GST_STATE_PLAYING),
258       GST_STATE_CHANGE_SUCCESS);
259
260   bus = gst_bus_new ();
261
262   inbuffer = gst_buffer_new_and_alloc (30);
263   memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
264   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
265   gst_buffer_ref (inbuffer);
266
267   gst_element_set_bus (vorbisdec, bus);
268
269   /* pushing gives away my reference ... */
270   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
271   /* ... and nothing ends up on the global buffer list */
272   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
273   gst_buffer_unref (inbuffer);
274   fail_unless (g_list_length (buffers) == 0);
275   fail_if ((message = gst_bus_pop (bus)) != NULL);
276
277   inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
278   memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
279   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
280   gst_buffer_ref (inbuffer);
281
282   /* pushing gives away my reference ... */
283   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
284   /* ... and nothing ends up on the global buffer list */
285   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
286   gst_buffer_unref (inbuffer);
287   fail_unless (g_list_length (buffers) == 0);
288
289   /* send minimal codebook header and audio packers */
290   inbuffer = _create_codebook_header_buffer ();
291   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
292
293   /* now send an empty vorbis packet, which should just be skipped */
294   inbuffer = gst_buffer_new_and_alloc (0);
295   gst_buffer_ref (inbuffer);
296   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
297   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
298   gst_buffer_unref (inbuffer);
299   fail_unless (g_list_length (buffers) == 0);
300
301   /* create and push an encoded audio packet */
302   inbuffer = _create_audio_buffer ();
303   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
304
305   /* now send another empty vorbis packet, which should just be skipped */
306   inbuffer = gst_buffer_new_and_alloc (0);
307   gst_buffer_ref (inbuffer);
308   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
309   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
310   gst_buffer_unref (inbuffer);
311
312   /* make sure there's no error on the bus */
313   message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
314   fail_if (message != NULL);
315
316   /* cleanup */
317   gst_bus_set_flushing (bus, TRUE);
318   gst_element_set_bus (vorbisdec, NULL);
319   gst_object_unref (GST_OBJECT (bus));
320   cleanup_vorbisdec (vorbisdec);
321 }
322
323 GST_END_TEST;
324
325 static Suite *
326 vorbisdec_suite (void)
327 {
328   Suite *s = suite_create ("vorbisdec");
329   TCase *tc_chain = tcase_create ("general");
330
331   suite_add_tcase (s, tc_chain);
332   tcase_add_test (tc_chain, test_empty_identification_header);
333   tcase_add_test (tc_chain, test_identification_header);
334   tcase_add_test (tc_chain, test_empty_vorbis_packet);
335
336   return s;
337 }
338
339 GST_CHECK_MAIN (vorbisdec);