1 /* test_seeking - Seeking tester for libFLAC
2 * Copyright (C) 2004,2005,2006,2007 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #if defined _MSC_VER || defined __MINGW32__
32 #include <sys/stat.h> /* for stat() */
33 #include "FLAC/assert.h"
34 #include "FLAC/stream_decoder.h"
38 FLAC__uint64 total_samples;
40 unsigned bits_per_sample;
42 FLAC__bool ignore_errors;
43 FLAC__bool error_occurred;
46 static FLAC__bool stop_signal_ = false;
48 static void our_sigint_handler_(int signal)
51 printf("(caught SIGINT) ");
56 static FLAC__bool die_(const char *msg)
58 printf("ERROR: %s\n", msg);
62 static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
64 FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
67 printf("FAILED, %s", msg);
71 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
76 static off_t get_filesize_(const char *srcpath)
80 if(0 == stat(srcpath, &srcstat))
81 return srcstat.st_size;
86 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
88 DecoderClientData *dcd = (DecoderClientData*)client_data;
90 (void)decoder, (void)buffer;
93 printf("ERROR: client_data in write callback is NULL\n");
94 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
97 if(dcd->error_occurred)
98 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
100 if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
102 printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
104 else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) {
107 printf("frame@%I64u(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
109 printf("frame@%llu(%u)... ", (unsigned long long)frame->header.number.sample_number, frame->header.blocksize);
114 dcd->error_occurred = true;
115 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
119 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
122 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
124 DecoderClientData *dcd = (DecoderClientData*)client_data;
129 printf("ERROR: client_data in metadata callback is NULL\n");
133 if(dcd->error_occurred)
136 if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
137 dcd->got_data = true;
138 dcd->total_samples = metadata->data.stream_info.total_samples;
139 dcd->channels = metadata->data.stream_info.channels;
140 dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
144 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
146 DecoderClientData *dcd = (DecoderClientData*)client_data;
151 printf("ERROR: client_data in error callback is NULL\n");
155 if(!dcd->ignore_errors) {
156 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
157 dcd->error_occurred = true;
162 * 0 - no read after seek
166 static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, off_t filesize, unsigned count, FLAC__int64 total_samples, unsigned read_mode)
168 FLAC__StreamDecoder *decoder;
169 DecoderClientData decoder_client_data;
173 decoder_client_data.got_data = false;
174 decoder_client_data.total_samples = 0;
175 decoder_client_data.quiet = false;
176 decoder_client_data.ignore_errors = false;
177 decoder_client_data.error_occurred = false;
179 printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC, read_mode=%u)\n\n", is_ogg? "Ogg":"native", read_mode);
181 decoder = FLAC__stream_decoder_new();
183 return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
186 if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
187 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
190 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
191 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
194 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
195 return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
197 if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
198 /* process until end of stream to make sure we can still seek in that state */
199 decoder_client_data.quiet = true;
200 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
201 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
202 decoder_client_data.quiet = false;
204 printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
205 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
206 return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
210 printf("file's total_samples is %I64u\n", decoder_client_data.total_samples);
212 printf("file's total_samples is %llu\n", (unsigned long long)decoder_client_data.total_samples);
214 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
215 if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
216 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
220 n = (long int)decoder_client_data.total_samples;
222 if(n == 0 && total_samples >= 0)
223 n = (long int)total_samples;
225 /* if we don't have a total samples count, just guess based on the file size */
226 /* @@@ for is_ogg we should get it from last page's granulepos */
228 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
229 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
230 #if !defined _MSC_VER && !defined __MINGW32__
236 printf("Begin seek barrage, count=%u\n", count);
238 for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
241 /* for the first 10, seek to the first 10 samples */
242 if (n >= 10 && i < 10) {
245 /* for the second 10, seek to the last 10 samples */
246 else if (n >= 10 && i < 20) {
247 pos = n - 1 - (i-10);
249 /* for the third 10, seek past the end and make sure we fail properly as expected */
254 #if !defined _MSC_VER && !defined __MINGW32__
255 pos = (FLAC__uint64)(random() % n);
257 /* RAND_MAX is only 32767 in my MSVC */
258 pos = (FLAC__uint64)((rand()<<15|rand()) % n);
263 printf("seek(%I64u)... ", pos);
265 printf("seek(%llu)... ", (unsigned long long)pos);
268 if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
269 if(pos >= (FLAC__uint64)n)
270 printf("seek past end failed as expected... ");
271 else if(decoder_client_data.total_samples == 0 && total_samples <= 0)
272 printf("seek failed, assuming it was past EOF... ");
274 return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
275 if(!FLAC__stream_decoder_flush(decoder))
276 return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
278 else if(read_mode == 1) {
279 printf("decode_frame... ");
281 if(!FLAC__stream_decoder_process_single(decoder))
282 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
284 printf("decode_frame... ");
286 if(!FLAC__stream_decoder_process_single(decoder))
287 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
289 else if(read_mode == 2) {
290 printf("decode_all... ");
292 decoder_client_data.quiet = true;
293 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
294 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
295 decoder_client_data.quiet = false;
302 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
303 if(!FLAC__stream_decoder_finish(decoder))
304 return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
307 FLAC__stream_decoder_delete(decoder);
308 printf("\nPASSED!\n");
314 /* There's no strtoull() in MSVC6 so we just write a specialized one */
315 static FLAC__uint64 local__strtoull(const char *src)
317 FLAC__uint64 ret = 0;
319 FLAC__ASSERT(0 != src);
320 while(0 != (c = *src++)) {
323 ret = (ret * 10) + c;
331 int main(int argc, char *argv[])
333 const char *filename;
334 unsigned count = 0, read_mode;
335 FLAC__int64 samples = -1;
338 static const char * const usage = "usage: test_seeking file.flac [#seeks] [#samples-in-file.flac]\n";
340 if (argc < 2 || argc > 4) {
341 fprintf(stderr, usage);
348 count = strtoul(argv[2], 0, 10);
351 samples = local__strtoull(argv[3]);
353 samples = strtoull(argv[3], 0, 10);
357 fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
359 #if !defined _MSC_VER && !defined __MINGW32__
363 if (gettimeofday(&tv, 0) < 0) {
364 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
370 srand((unsigned)time(0));
373 filesize = get_filesize_(filename);
375 fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
379 (void) signal(SIGINT, our_sigint_handler_);
381 for (read_mode = 0; read_mode <= 2; read_mode++) {
383 if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
385 ok = seek_barrage(/*is_ogg=*/true, filename, filesize, count, samples, read_mode);
387 fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
392 ok = seek_barrage(/*is_ogg=*/false, filename, filesize, count, samples, read_mode);