3 * unit test for wavpackparse
5 * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
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.
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.
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.
25 #include <gst/check/gstcheck.h>
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;
32 static GstElement *wavpackparse;
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,
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,
59 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("audio/x-wavpack, "
64 "channels = (int) 1, "
65 "rate = (int) 44100, " "framed = (boolean) TRUE"));
67 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("audio/x-wavpack"));
73 wavpackparse_found_pad (GstElement * src, GstPad * pad, gpointer data)
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);
89 setup_wavpackparse (void)
93 GST_DEBUG ("setup_wavpackparse");
95 wavpackparse = gst_element_factory_make ("wavpackparse", "wavpackparse");
96 fail_if (wavpackparse == NULL, "Could not create wavpackparse");
98 mysrcpad = gst_pad_new_from_static_template (&srctemplate, "src");
99 fail_if (mysrcpad == NULL, "Could not create srcpad");
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);
107 g_signal_connect (wavpackparse, "pad-added",
108 G_CALLBACK (wavpackparse_found_pad), NULL);
110 bus = gst_bus_new ();
111 gst_element_set_bus (wavpackparse, bus);
113 fail_unless (gst_element_set_state (wavpackparse,
114 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
115 "could not set to playing");
119 cleanup_wavpackparse (void)
121 GstPad *sinkpad, *srcpad;
123 GST_DEBUG ("cleanup_wavpackparse");
125 gst_bus_set_flushing (bus, TRUE);
126 gst_element_set_bus (wavpackparse, NULL);
127 gst_object_unref (GST_OBJECT (bus));
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);
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);
143 fail_unless (gst_element_set_state (wavpackparse, GST_STATE_NULL) ==
144 GST_STATE_CHANGE_SUCCESS, "could not set to null");
146 gst_object_unref (wavpackparse);
149 GST_START_TEST (test_parsing_valid_frames)
151 GstBuffer *inbuffer, *outbuffer;
153 GstFormat format = GST_FORMAT_DEFAULT;
156 setup_wavpackparse ();
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);
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);
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);
170 for (i = 0; i < num_buffers; ++i) {
171 outbuffer = GST_BUFFER (buffers->data);
172 fail_if (outbuffer == NULL);
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);
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);
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);
193 buffers = g_list_remove (buffers, outbuffer);
195 gst_buffer_unref (outbuffer);
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");
205 g_list_free (buffers);
208 cleanup_wavpackparse ();
213 GST_START_TEST (test_parsing_invalid_first_header)
215 GstBuffer *inbuffer, *outbuffer;
218 setup_wavpackparse ();
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);
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);
229 num_buffers = g_list_length (buffers);
231 /* should get 1 buffers, the second non-broken one */
232 fail_unless_equals_int (num_buffers, 1);
234 for (i = 0; i < num_buffers; ++i) {
235 outbuffer = GST_BUFFER (buffers->data);
236 fail_if (outbuffer == NULL);
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);
244 fail_unless_equals_int (GST_BUFFER_TIMESTAMP (outbuffer), 580498866);
245 fail_unless_equals_int (GST_BUFFER_OFFSET (outbuffer), 25600);
250 buffers = g_list_remove (buffers, outbuffer);
252 gst_buffer_unref (outbuffer);
256 g_list_free (buffers);
259 cleanup_wavpackparse ();
266 wavpackparse_suite (void)
268 Suite *s = suite_create ("wavpackparse");
269 TCase *tc_chain = tcase_create ("general");
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);
279 main (int argc, char **argv)
283 Suite *s = wavpackparse_suite ();
284 SRunner *sr = srunner_create (s);
286 gst_check_init (&argc, &argv);
288 srunner_run_all (sr, CK_NORMAL);
289 nf = srunner_ntests_failed (sr);