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 <gst/audio/audio.h>
22 #include <glib/gstdio.h>
25 _get_first_sample (GstSample * sample)
34 fail_unless (sample != NULL, "NULL sample");
36 caps = gst_sample_get_caps (sample);
37 fail_unless (caps != NULL, "sample without caps");
39 buf = gst_sample_get_buffer (sample);
40 GST_DEBUG ("buffer with size=%u, caps=%" GST_PTR_FORMAT,
41 gst_buffer_get_size (buf), caps);
43 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
44 /* log buffer details */
45 GST_MEMDUMP ("buffer data from decoder", data, size);
47 /* make sure it's the format we expect */
48 fail_unless (gst_audio_info_from_caps (&info, caps));
50 fail_unless_equals_int (GST_AUDIO_INFO_WIDTH (&info), 16);
51 fail_unless_equals_int (GST_AUDIO_INFO_DEPTH (&info), 16);
52 fail_unless_equals_int (GST_AUDIO_INFO_RATE (&info), 44100);
53 fail_unless_equals_int (GST_AUDIO_INFO_CHANNELS (&info), 1);
55 if (GST_AUDIO_INFO_IS_LITTLE_ENDIAN (&info))
56 res = GST_READ_UINT16_LE (data);
58 res = GST_READ_UINT16_BE (data);
60 gst_buffer_unmap (buf, data, size);
65 GST_START_TEST (test_decode)
69 GstSample *sample = NULL;
70 guint16 first_sample = 0;
73 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
75 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
78 pipeline = gst_parse_launch (pipe_desc, NULL);
79 fail_unless (pipeline != NULL);
84 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
85 fail_unless (appsink != NULL);
87 gst_element_set_state (pipeline, GST_STATE_PLAYING);
90 g_signal_emit_by_name (appsink, "pull-sample", &sample);
93 if (first_sample == 0)
94 first_sample = _get_first_sample (sample);
96 size += gst_buffer_get_size (gst_sample_get_buffer (sample));
98 gst_sample_unref (sample);
103 /* audiotestsrc with samplesperbuffer 1024 and 10 num-buffers */
104 fail_unless_equals_int (size, 20480);
105 fail_unless_equals_int (first_sample, 0x066a);
107 gst_element_set_state (pipeline, GST_STATE_NULL);
108 g_object_unref (pipeline);
109 g_object_unref (appsink);
114 GST_START_TEST (test_decode_seek_full)
116 GstElement *pipeline;
119 GstSample *sample = NULL;
120 guint16 first_sample = 0;
123 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
125 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
128 pipeline = gst_parse_launch (pipe_desc, NULL);
129 fail_unless (pipeline != NULL);
134 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
135 fail_unless (appsink != NULL);
137 gst_element_set_state (pipeline, GST_STATE_PAUSED);
138 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
140 /* do a seek that should give us the complete output */
141 event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
142 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 20480);
143 fail_unless (gst_element_send_event (appsink, event));
145 gst_element_set_state (pipeline, GST_STATE_PLAYING);
148 g_signal_emit_by_name (appsink, "pull-sample", &sample);
151 if (first_sample == 0)
152 first_sample = _get_first_sample (sample);
153 size += gst_buffer_get_size (gst_sample_get_buffer (sample));
155 gst_sample_unref (sample);
160 /* file was generated with audiotestsrc
161 * with 1024 samplesperbuffer and 10 num-buffers in 16 bit audio */
162 fail_unless_equals_int (size, 20480);
163 fail_unless_equals_int (first_sample, 0x066a);
165 gst_element_set_state (pipeline, GST_STATE_NULL);
167 g_object_unref (pipeline);
168 g_object_unref (appsink);
173 GST_START_TEST (test_decode_seek_partial)
175 GstElement *pipeline;
178 GstSample *sample = NULL;
180 guint16 first_sample = 0;
182 g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
184 g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
187 pipeline = gst_parse_launch (pipe_desc, NULL);
188 fail_unless (pipeline != NULL);
193 appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
194 fail_unless (appsink != NULL);
196 gst_element_set_state (pipeline, GST_STATE_PAUSED);
197 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
199 /* do a partial seek to get the first 1024 samples or 2048 bytes */
200 event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
201 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 1024);
202 GST_DEBUG ("seeking");
203 fail_unless (gst_element_send_event (appsink, event));
204 GST_DEBUG ("seeked");
206 gst_element_set_state (pipeline, GST_STATE_PLAYING);
209 GST_DEBUG ("pulling sample");
210 g_signal_emit_by_name (appsink, "pull-sample", &sample);
211 GST_DEBUG ("pulled sample %p", sample);
214 if (first_sample == 0) {
215 // fail_unless_equals_int (GST_BUFFER_OFFSET (buffer), 0L);
216 first_sample = _get_first_sample (sample);
218 size += gst_buffer_get_size (gst_sample_get_buffer (sample));
220 gst_sample_unref (sample);
225 fail_unless_equals_int (size, 2048);
226 fail_unless_equals_int (first_sample, 0x066a);
228 gst_element_set_state (pipeline, GST_STATE_NULL);
230 g_object_unref (pipeline);
231 g_object_unref (appsink);
240 Suite *s = suite_create ("flacdec");
242 TCase *tc_chain = tcase_create ("linear");
244 /* time out after 60s, not the default 3 */
245 tcase_set_timeout (tc_chain, 60);
247 suite_add_tcase (s, tc_chain);
248 tcase_add_test (tc_chain, test_decode);
249 tcase_add_test (tc_chain, test_decode_seek_full);
250 tcase_add_test (tc_chain, test_decode_seek_partial);
255 GST_CHECK_MAIN (flacdec);