2 * Copyright (C) 2009 Thomas Vander Stichele <thomas at apestaart dot org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <gst/check/gstcheck.h>
21 #include <glib/gstdio.h>
24 buffer_get_first_sample (GstBuffer * buf)
29 fail_unless (buf != NULL, "NULL buffer");
30 fail_unless (GST_BUFFER_CAPS (buf) != NULL, "buffer without caps");
32 /* log buffer details */
33 GST_DEBUG ("buffer with size=%u, caps=%" GST_PTR_FORMAT,
34 GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf));
35 GST_MEMDUMP ("buffer data from decoder", GST_BUFFER_DATA (buf),
36 GST_BUFFER_SIZE (buf));
38 /* make sure it's the format we expect */
39 s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
40 fail_unless_equals_string (gst_structure_get_name (s), "audio/x-raw-int");
41 fail_unless (gst_structure_get_int (s, "width", &w));
42 fail_unless_equals_int (w, 16);
43 fail_unless (gst_structure_get_int (s, "depth", &d));
44 fail_unless_equals_int (d, 16);
45 fail_unless (gst_structure_get_int (s, "rate", &r));
46 fail_unless_equals_int (r, 44100);
47 fail_unless (gst_structure_get_int (s, "channels", &c));
48 fail_unless_equals_int (c, 1);
49 fail_unless (gst_structure_get_int (s, "endianness", &e));
50 if (e == G_BIG_ENDIAN)
51 return GST_READ_UINT16_BE (GST_BUFFER_DATA (buf));
53 return GST_READ_UINT16_LE (GST_BUFFER_DATA (buf));
56 GST_START_TEST (test_decode)
60 GstBuffer *buffer = NULL;
61 guint16 first_sample = 0;
64 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
66 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
69 pipeline = gst_parse_launch (pipe_desc, NULL);
70 fail_unless (pipeline != NULL);
75 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
76 fail_unless (appsink != NULL);
78 gst_element_set_state (pipeline, GST_STATE_PLAYING);
81 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
84 if (first_sample == 0)
85 first_sample = buffer_get_first_sample (buffer);
86 GST_DEBUG ("buffer: %d\n", buffer->size);
87 GST_DEBUG ("buffer: %04x\n", buffer_get_first_sample (buffer));
90 gst_buffer_unref (buffer);
95 /* audiotestsrc with samplesperbuffer 1024 and 10 num-buffers */
96 fail_unless_equals_int (size, 20480);
97 fail_unless_equals_int (first_sample, 0x066a);
99 gst_element_set_state (pipeline, GST_STATE_NULL);
100 g_object_unref (pipeline);
101 g_object_unref (appsink);
106 GST_START_TEST (test_decode_seek_full)
108 GstElement *pipeline;
111 GstBuffer *buffer = NULL;
112 guint16 first_sample = 0;
115 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
117 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
120 pipeline = gst_parse_launch (pipe_desc, NULL);
121 fail_unless (pipeline != NULL);
126 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
127 fail_unless (appsink != NULL);
129 gst_element_set_state (pipeline, GST_STATE_PAUSED);
130 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
132 /* do a seek that should give us the complete output */
133 event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
134 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 20480);
135 fail_unless (gst_element_send_event (appsink, event));
137 gst_element_set_state (pipeline, GST_STATE_PLAYING);
140 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
143 if (first_sample == 0)
144 first_sample = buffer_get_first_sample (buffer);
145 size += buffer->size;
147 gst_buffer_unref (buffer);
152 /* file was generated with audiotestsrc
153 * with 1024 samplesperbuffer and 10 num-buffers in 16 bit audio */
154 fail_unless_equals_int (size, 20480);
155 fail_unless_equals_int (first_sample, 0x066a);
157 gst_element_set_state (pipeline, GST_STATE_NULL);
159 g_object_unref (pipeline);
160 g_object_unref (appsink);
165 GST_START_TEST (test_decode_seek_partial)
167 GstElement *pipeline;
170 GstBuffer *buffer = NULL;
172 guint16 first_sample = 0;
174 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
176 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
179 pipeline = gst_parse_launch (pipe_desc, NULL);
180 fail_unless (pipeline != NULL);
185 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
186 fail_unless (appsink != NULL);
188 gst_element_set_state (pipeline, GST_STATE_PAUSED);
189 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
191 /* do a partial seek to get the first 1024 samples or 2048 bytes */
192 event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
193 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 1024);
194 GST_DEBUG ("seeking");
195 fail_unless (gst_element_send_event (appsink, event));
196 GST_DEBUG ("seeked");
198 gst_element_set_state (pipeline, GST_STATE_PLAYING);
201 GST_DEBUG ("pulling buffer");
202 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
203 GST_DEBUG ("pulled buffer %p", buffer);
206 if (first_sample == 0) {
207 fail_unless_equals_int (GST_BUFFER_OFFSET (buffer), 0L);
208 first_sample = buffer_get_first_sample (buffer);
210 size += buffer->size;
212 gst_buffer_unref (buffer);
217 fail_unless_equals_int (size, 2048);
218 fail_unless_equals_int (first_sample, 0x066a);
220 gst_element_set_state (pipeline, GST_STATE_NULL);
222 g_object_unref (pipeline);
223 g_object_unref (appsink);
232 Suite *s = suite_create ("flacdec");
234 TCase *tc_chain = tcase_create ("linear");
236 /* time out after 60s, not the default 3 */
237 tcase_set_timeout (tc_chain, 60);
239 suite_add_tcase (s, tc_chain);
240 tcase_add_test (tc_chain, test_decode);
241 tcase_add_test (tc_chain, test_decode_seek_full);
242 tcase_add_test (tc_chain, test_decode_seek_partial);
247 GST_CHECK_MAIN (flacdec);