check/Makefile.am: have some tests be disabled for valgrinding
[platform/upstream/gst-plugins-base.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 GList *buffers = NULL;
28
29 /* For ease of programming we use globals to keep refs for our floating
30  * src and sink pads we create; otherwise we always have to do get_pad,
31  * get_peer, and then remove references in every test function */
32 GstPad *mysrcpad, *mysinkpad;
33
34 /* a valid first header packet */
35 guchar identification_header[30] = {
36   1,                            /* packet_type */
37   'v', 'o', 'r', 'b', 'i', 's',
38   0, 0, 0, 0,                   /* vorbis_version */
39   2,                            /* audio_channels */
40   0x44, 0xac, 0, 0,             /* sample_rate */
41   0xff, 0xff, 0xff, 0xff,       /* bitrate_maximum */
42   0x00, 0xee, 0x02, 0x00,       /* bitrate_nominal */
43   0xff, 0xff, 0xff, 0xff,       /* bitrate_minimum */
44   0xb8,                         /* blocksize_0, blocksize_1 */
45   0x01,                         /* framing_flag */
46 };
47
48 guchar comment_header[] = {
49   3,                            /* packet_type */
50   'v', 'o', 'r', 'b', 'i', 's',
51   2, 0, 0, 0,                   /* vendor_length */
52   'm', 'e',
53   1, 0, 0, 0,                   /* user_comment_list_length */
54   9, 0, 0, 0,                   /* length comment[0] */
55   'A', 'R', 'T', 'I', 'S', 'T', '=', 'm', 'e',
56   0x01,                         /* framing bit */
57 };
58
59 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS_ANY);
63 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS_ANY);
67
68 GstElement *
69 setup_vorbisdec ()
70 {
71   GstElement *vorbisdec;
72
73   GST_DEBUG ("setup_vorbisdec");
74   vorbisdec = gst_check_setup_element ("vorbisdec");
75   mysrcpad = gst_check_setup_src_pad (vorbisdec, &srctemplate, NULL);
76   mysinkpad = gst_check_setup_sink_pad (vorbisdec, &sinktemplate, NULL);
77
78   return vorbisdec;
79 }
80
81 void
82 cleanup_vorbisdec (GstElement * vorbisdec)
83 {
84   GST_DEBUG ("cleanup_vorbisdec");
85   gst_element_set_state (vorbisdec, GST_STATE_NULL);
86
87   gst_check_teardown_src_pad (vorbisdec);
88   gst_check_teardown_sink_pad (vorbisdec);
89   gst_check_teardown_element (vorbisdec);
90 }
91
92 GST_START_TEST (test_wrong_channels_identification_header)
93 {
94   GstElement *vorbisdec;
95   GstBuffer *inbuffer, *outbuffer;
96   GstBus *bus;
97   GstMessage *message;
98
99   vorbisdec = setup_vorbisdec ();
100   fail_unless (gst_element_set_state (vorbisdec,
101           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
102       "could not set to playing");
103   bus = gst_bus_new ();
104
105   inbuffer = gst_buffer_new_and_alloc (30);
106   memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
107   /* set the channel count to 7, which is not supported */
108   GST_BUFFER_DATA (inbuffer)[11] = 7;
109   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
110   gst_buffer_ref (inbuffer);
111
112   gst_element_set_bus (vorbisdec, bus);
113   /* pushing gives away my reference ... */
114   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
115   /* ... and nothing ends up on the global buffer list */
116   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
117   gst_buffer_unref (inbuffer);
118   fail_unless_equals_int (g_list_length (buffers), 0);
119
120   fail_if ((message = gst_bus_pop (bus)) == NULL);
121   fail_unless_message_error (message, STREAM, NOT_IMPLEMENTED);
122   gst_message_unref (message);
123   gst_element_set_bus (vorbisdec, NULL);
124
125   /* cleanup */
126   gst_object_unref (GST_OBJECT (bus));
127   cleanup_vorbisdec (vorbisdec);
128 }
129
130 GST_END_TEST;
131
132
133 GST_START_TEST (test_empty_identification_header)
134 {
135   GstElement *vorbisdec;
136   GstBuffer *inbuffer, *outbuffer;
137   GstBus *bus;
138   GstMessage *message;
139
140   vorbisdec = setup_vorbisdec ();
141   bus = gst_bus_new ();
142
143   fail_unless (gst_element_set_state (vorbisdec,
144           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
145       "could not set to playing");
146
147   inbuffer = gst_buffer_new_and_alloc (0);
148   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
149
150   /* set a bus here so we avoid getting state change messages */
151   gst_element_set_bus (vorbisdec, bus);
152
153   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_ERROR);
154   /* ... but it ends up being collected on the global buffer list */
155   fail_unless_equals_int (g_list_length (buffers), 0);
156
157   fail_if ((message = gst_bus_pop (bus)) == NULL);
158   fail_unless_message_error (message, STREAM, DECODE);
159   gst_message_unref (message);
160   gst_element_set_bus (vorbisdec, NULL);
161
162   /* cleanup */
163   gst_object_unref (GST_OBJECT (bus));
164   cleanup_vorbisdec (vorbisdec);
165 }
166
167 GST_END_TEST;
168
169 /* FIXME: also tests comment header */
170 GST_START_TEST (test_identification_header)
171 {
172   GstElement *vorbisdec;
173   GstBuffer *inbuffer, *outbuffer;
174   GstBus *bus;
175   GstMessage *message;
176   GstTagList *tag_list;
177   gchar *artist;
178
179   vorbisdec = setup_vorbisdec ();
180   fail_unless (gst_element_set_state (vorbisdec,
181           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
182       "could not set to playing");
183   bus = gst_bus_new ();
184
185   inbuffer = gst_buffer_new_and_alloc (30);
186   memcpy (GST_BUFFER_DATA (inbuffer), identification_header, 30);
187   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
188   gst_buffer_ref (inbuffer);
189
190   gst_element_set_bus (vorbisdec, bus);
191   /* pushing gives away my reference ... */
192   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
193   /* ... and nothing ends up on the global buffer list */
194   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
195   gst_buffer_unref (inbuffer);
196   fail_unless (g_list_length (buffers) == 0);
197   fail_if ((message = gst_bus_pop (bus)) != NULL);
198
199   inbuffer = gst_buffer_new_and_alloc (sizeof (comment_header));
200   memcpy (GST_BUFFER_DATA (inbuffer), comment_header, sizeof (comment_header));
201   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
202   gst_buffer_ref (inbuffer);
203
204   /* pushing gives away my reference ... */
205   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
206   /* ... and nothing ends up on the global buffer list */
207   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
208   gst_buffer_unref (inbuffer);
209   fail_unless (g_list_length (buffers) == 0);
210   /* there's a tag message waiting */
211   fail_if ((message = gst_bus_pop (bus)) == NULL);
212   gst_message_parse_tag (message, &tag_list);
213   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, GST_TAG_ARTIST),
214       1);
215   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
216   fail_unless_equals_string (artist, "me");
217   g_free (artist);
218   fail_unless_equals_int (gst_tag_list_get_tag_size (tag_list, "album"), 0);
219   gst_tag_list_free (tag_list);
220   gst_message_unref (message);
221
222   /* cleanup */
223   gst_bus_set_flushing (bus, TRUE);
224   gst_element_set_bus (vorbisdec, NULL);
225   gst_object_unref (GST_OBJECT (bus));
226   cleanup_vorbisdec (vorbisdec);
227 }
228
229 GST_END_TEST;
230
231 Suite *
232 vorbisdec_suite (void)
233 {
234   Suite *s = suite_create ("vorbisdec");
235   TCase *tc_chain = tcase_create ("general");
236
237   suite_add_tcase (s, tc_chain);
238   tcase_add_test (tc_chain, test_empty_identification_header);
239   tcase_add_test (tc_chain, test_wrong_channels_identification_header);
240   tcase_add_test (tc_chain, test_identification_header);
241
242   return s;
243 }
244
245 int
246 main (int argc, char **argv)
247 {
248   int nf;
249
250   Suite *s = vorbisdec_suite ();
251   SRunner *sr = srunner_create (s);
252
253   gst_check_init (&argc, &argv);
254
255   srunner_run_all (sr, CK_NORMAL);
256   nf = srunner_ntests_failed (sr);
257   srunner_free (sr);
258
259   return nf;
260 }