Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / wavpackparse.c
1 /* GStreamer
2  *
3  * unit test for wavpackparse
4  *
5  * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.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 /* For ease of programming we use globals to keep refs for our floating
28  * src and sink pads we create; otherwise we always have to do get_pad,
29  * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *mysinkpad;
31 static GstBus *bus;
32 static GstElement *wavpackparse;
33
34 /* Wavpack file with 2 frames of silence */
35 guint8 test_file[] = {
36   0x77, 0x76, 0x70, 0x6B, 0x62, 0x00, 0x00, 0x00,       /* first frame */
37   0x04, 0x04, 0x00, 0x00, 0x00, 0xC8, 0x00, 0x00,       /* include RIFF header */
38   0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00,
39   0x05, 0x18, 0x80, 0x04, 0xFF, 0xAF, 0x80, 0x60,
40   0x21, 0x16, 0x52, 0x49, 0x46, 0x46, 0x24, 0x90,
41   0x01, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6D,
42   0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
43   0x01, 0x00, 0x44, 0xAC, 0x00, 0x00, 0x88, 0x58,
44   0x01, 0x00, 0x02, 0x00, 0x10, 0x00, 0x64, 0x61,
45   0x74, 0x61, 0x00, 0x90, 0x01, 0x00, 0x02, 0x00,
46   0x03, 0x00, 0x04, 0x00, 0x05, 0x03, 0x00, 0x00,
47   0x00, 0x00, 0x00, 0x00, 0x65, 0x02, 0x00, 0x00,
48   0x00, 0x00, 0x8A, 0x02, 0x00, 0x00, 0xFF, 0x7F,
49   0x00, 0xE4,
50   0x77, 0x76, 0x70, 0x6B, 0x2E, 0x00, 0x00, 0x00,       /* second frame */
51   0x04, 0x04, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
52   0x00, 0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00,
53   0x05, 0x18, 0x80, 0x04, 0xFF, 0xAF, 0x80, 0x60,
54   0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x03,
55   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x02,
56   0x00, 0x00, 0xFF, 0x7F, 0x00, 0xE4,
57 };
58
59 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/x-wavpack, "
63         "width = (int) 16, "
64         "channels = (int) 1, "
65         "rate = (int) 44100, " "framed = (boolean) TRUE"));
66
67 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("audio/x-wavpack"));
71
72 static void
73 wavpackparse_found_pad (GstElement * src, GstPad * pad, gpointer data)
74 {
75   GstPad *srcpad;
76
77   mysinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
78   fail_if (mysinkpad == NULL, "Couldn't create sinkpad");
79   srcpad = gst_element_get_static_pad (wavpackparse, "src");
80   fail_if (srcpad == NULL, "Failed to get srcpad from wavpackparse");
81   gst_pad_set_chain_function (mysinkpad, gst_check_chain_func);
82   fail_unless (gst_pad_link (srcpad, mysinkpad) == GST_PAD_LINK_OK,
83       "Failed to link pads");
84   gst_pad_set_active (mysinkpad, TRUE);
85   gst_object_unref (srcpad);
86 }
87
88 static void
89 setup_wavpackparse (void)
90 {
91   GstPad *sinkpad;
92
93   GST_DEBUG ("setup_wavpackparse");
94
95   wavpackparse = gst_element_factory_make ("wavpackparse", "wavpackparse");
96   fail_if (wavpackparse == NULL, "Could not create wavpackparse");
97
98   mysrcpad = gst_pad_new_from_static_template (&srctemplate, "src");
99   fail_if (mysrcpad == NULL, "Could not create srcpad");
100
101   sinkpad = gst_element_get_static_pad (wavpackparse, "sink");
102   fail_if (sinkpad == NULL, "Failed to get sinkpad from wavpackparse");
103   fail_unless (gst_pad_link (mysrcpad, sinkpad) == GST_PAD_LINK_OK,
104       "Failed to link pads");
105   gst_object_unref (sinkpad);
106
107   g_signal_connect (wavpackparse, "pad-added",
108       G_CALLBACK (wavpackparse_found_pad), NULL);
109
110   bus = gst_bus_new ();
111   gst_element_set_bus (wavpackparse, bus);
112
113   fail_unless (gst_element_set_state (wavpackparse,
114           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
115       "could not set to playing");
116 }
117
118 static void
119 cleanup_wavpackparse (void)
120 {
121   GstPad *sinkpad, *srcpad;
122
123   GST_DEBUG ("cleanup_wavpackparse");
124
125   gst_bus_set_flushing (bus, TRUE);
126   gst_element_set_bus (wavpackparse, NULL);
127   gst_object_unref (GST_OBJECT (bus));
128
129   sinkpad = gst_element_get_static_pad (wavpackparse, "sink");
130   fail_if (sinkpad == NULL, "Failed to get sinkpad from wavpackparse");
131   fail_unless (gst_pad_unlink (mysrcpad, sinkpad), "Failed to unlink pads");
132   gst_pad_set_caps (mysrcpad, NULL);
133   gst_object_unref (sinkpad);
134   gst_object_unref (mysrcpad);
135
136   srcpad = gst_element_get_static_pad (wavpackparse, "src");
137   fail_if (srcpad == NULL, "Failed to get srcpad from wavpackparse");
138   fail_unless (gst_pad_unlink (srcpad, mysinkpad), "Failed to unlink pads");
139   gst_pad_set_caps (mysinkpad, NULL);
140   gst_object_unref (srcpad);
141   gst_object_unref (mysinkpad);
142
143   fail_unless (gst_element_set_state (wavpackparse, GST_STATE_NULL) ==
144       GST_STATE_CHANGE_SUCCESS, "could not set to null");
145
146   gst_object_unref (wavpackparse);
147 }
148
149 GST_START_TEST (test_parsing_valid_frames)
150 {
151   GstBuffer *inbuffer, *outbuffer;
152   int i, num_buffers;
153   GstFormat format = GST_FORMAT_DEFAULT;
154   gint64 pos;
155
156   setup_wavpackparse ();
157
158   inbuffer = gst_buffer_new_and_alloc (sizeof (test_file));
159   memcpy (GST_BUFFER_DATA (inbuffer), test_file, sizeof (test_file));
160   gst_buffer_ref (inbuffer);
161
162   /* should decode the buffer without problems */
163   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
164   gst_buffer_unref (inbuffer);
165
166   num_buffers = g_list_length (buffers);
167   /* should get 2 buffers, each one complete wavpack frame */
168   fail_unless_equals_int (num_buffers, 2);
169
170   for (i = 0; i < num_buffers; ++i) {
171     outbuffer = GST_BUFFER (buffers->data);
172     fail_if (outbuffer == NULL);
173
174     fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), "wvpk", 4) == 0,
175         "Buffer contains no Wavpack frame");
176     fail_unless_equals_int (GST_BUFFER_DURATION (outbuffer), 580498866);
177
178     switch (i) {
179       case 0:{
180         fail_unless_equals_int (GST_BUFFER_TIMESTAMP (outbuffer), 0);
181         fail_unless_equals_int (GST_BUFFER_OFFSET (outbuffer), 0);
182         fail_unless_equals_int (GST_BUFFER_OFFSET_END (outbuffer), 25600);
183         break;
184       }
185       case 1:{
186         fail_unless_equals_int (GST_BUFFER_TIMESTAMP (outbuffer), 580498866);
187         fail_unless_equals_int (GST_BUFFER_OFFSET (outbuffer), 25600);
188         fail_unless_equals_int (GST_BUFFER_OFFSET_END (outbuffer), 51200);
189         break;
190       }
191     }
192
193     buffers = g_list_remove (buffers, outbuffer);
194
195     gst_buffer_unref (outbuffer);
196     outbuffer = NULL;
197   }
198
199   fail_unless (gst_element_query_position (wavpackparse, &format, &pos),
200       "Position query failed");
201   fail_unless_equals_int (pos, 51200);
202   fail_unless (gst_element_query_duration (wavpackparse, &format, NULL),
203       "Duration query failed");
204
205   g_list_free (buffers);
206   buffers = NULL;
207
208   cleanup_wavpackparse ();
209 }
210
211 GST_END_TEST;
212
213 GST_START_TEST (test_parsing_invalid_first_header)
214 {
215   GstBuffer *inbuffer, *outbuffer;
216   int i, num_buffers;
217
218   setup_wavpackparse ();
219
220   inbuffer = gst_buffer_new_and_alloc (sizeof (test_file));
221   memcpy (GST_BUFFER_DATA (inbuffer), test_file, sizeof (test_file));
222   GST_BUFFER_DATA (inbuffer)[0] = 'k';
223   gst_buffer_ref (inbuffer);
224
225   /* should decode the buffer without problems */
226   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
227   gst_buffer_unref (inbuffer);
228
229   num_buffers = g_list_length (buffers);
230
231   /* should get 1 buffers, the second non-broken one */
232   fail_unless_equals_int (num_buffers, 1);
233
234   for (i = 0; i < num_buffers; ++i) {
235     outbuffer = GST_BUFFER (buffers->data);
236     fail_if (outbuffer == NULL);
237
238     fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), "wvpk", 4) == 0,
239         "Buffer contains no Wavpack frame");
240     fail_unless_equals_int (GST_BUFFER_DURATION (outbuffer), 580498866);
241
242     switch (i) {
243       case 0:{
244         fail_unless_equals_int (GST_BUFFER_TIMESTAMP (outbuffer), 580498866);
245         fail_unless_equals_int (GST_BUFFER_OFFSET (outbuffer), 25600);
246         break;
247       }
248     }
249
250     buffers = g_list_remove (buffers, outbuffer);
251
252     gst_buffer_unref (outbuffer);
253     outbuffer = NULL;
254   }
255
256   g_list_free (buffers);
257   buffers = NULL;
258
259   cleanup_wavpackparse ();
260 }
261
262 GST_END_TEST;
263
264
265 static Suite *
266 wavpackparse_suite (void)
267 {
268   Suite *s = suite_create ("wavpackparse");
269   TCase *tc_chain = tcase_create ("general");
270
271   suite_add_tcase (s, tc_chain);
272   tcase_add_test (tc_chain, test_parsing_valid_frames);
273   tcase_add_test (tc_chain, test_parsing_invalid_first_header);
274
275   return s;
276 }
277
278 int
279 main (int argc, char **argv)
280 {
281   int nf;
282
283   Suite *s = wavpackparse_suite ();
284   SRunner *sr = srunner_create (s);
285
286   gst_check_init (&argc, &argv);
287
288   srunner_run_all (sr, CK_NORMAL);
289   nf = srunner_ntests_failed (sr);
290   srunner_free (sr);
291
292   return nf;
293 }