1 /* test_seeking - Seeking tester for libFLAC and libOggFLAC
2 * Copyright (C) 2004,2005,2006 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"
36 #include "OggFLAC/stream_decoder.h"
41 FLAC__uint64 total_samples;
43 unsigned bits_per_sample;
45 FLAC__bool ignore_errors;
46 FLAC__bool error_occurred;
49 static FLAC__bool stop_signal_ = false;
51 static void our_sigint_handler_(int signal)
54 printf("(caught SIGINT) ");
59 static FLAC__bool die_(const char *msg)
61 printf("ERROR: %s\n", msg);
65 static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
67 FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
70 printf("FAILED, %s", msg);
74 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
80 static FLAC__bool die_os_(const char *msg, const OggFLAC__StreamDecoder *decoder)
82 OggFLAC__StreamDecoderState state = OggFLAC__stream_decoder_get_state(decoder);
85 printf("FAILED, %s", msg);
89 printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__StreamDecoderStateString[state]);
90 if(state == OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
91 FLAC__StreamDecoderState state_ = OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(decoder);
92 printf(" FLAC stream decoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamDecoderStateString[state_]);
99 static off_t get_filesize_(const char *srcpath)
103 if(0 == stat(srcpath, &srcstat))
104 return srcstat.st_size;
109 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
111 DecoderClientData *dcd = (DecoderClientData*)client_data;
113 (void)decoder, (void)buffer;
116 printf("ERROR: client_data in write callback is NULL\n");
117 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
120 if(dcd->error_occurred)
121 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
123 if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
125 printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
127 else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) {
129 printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
133 dcd->error_occurred = true;
134 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
138 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
141 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
143 DecoderClientData *dcd = (DecoderClientData*)client_data;
148 printf("ERROR: client_data in metadata callback is NULL\n");
152 if(dcd->error_occurred)
155 if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
156 dcd->got_data = true;
157 dcd->total_samples = metadata->data.stream_info.total_samples;
158 dcd->channels = metadata->data.stream_info.channels;
159 dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
163 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
165 DecoderClientData *dcd = (DecoderClientData*)client_data;
170 printf("ERROR: client_data in error callback is NULL\n");
174 if(!dcd->ignore_errors) {
175 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
176 dcd->error_occurred = true;
180 static FLAC__bool seek_barrage_native_flac(const char *filename, off_t filesize, unsigned count)
182 FLAC__StreamDecoder *decoder;
183 DecoderClientData decoder_client_data;
187 decoder_client_data.got_data = false;
188 decoder_client_data.total_samples = 0;
189 decoder_client_data.quiet = false;
190 decoder_client_data.ignore_errors = false;
191 decoder_client_data.error_occurred = false;
193 printf("\n+++ seek test: FLAC__StreamDecoder\n\n");
195 decoder = FLAC__stream_decoder_new();
197 return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
199 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
200 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
202 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
203 return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
205 /* process until end of stream to make sure we can still seek in that state */
206 decoder_client_data.quiet = true;
207 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
208 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
209 decoder_client_data.quiet = false;
211 printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
212 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
213 return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
215 printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
216 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
217 if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
218 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
222 n = (long int)decoder_client_data.total_samples;
224 /* if we don't have a total samples count, just guess based on the file size */
226 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
227 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
228 #if !defined _MSC_VER && !defined __MINGW32__
234 printf("Begin seek barrage, count=%u\n", count);
236 for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
239 /* for the first 10, seek to the first 10 samples */
240 if (n >= 10 && i < 10) {
243 /* for the second 10, seek to the last 10 samples */
244 else if (n >= 10 && i < 20) {
245 pos = n - 1 - (i-10);
247 /* for the third 10, seek past the end and make sure we fail properly as expected */
252 #if !defined _MSC_VER && !defined __MINGW32__
253 pos = (FLAC__uint64)(random() % n);
255 /* RAND_MAX is only 32767 in my MSVC */
256 pos = (FLAC__uint64)((rand()<<15|rand()) % n);
260 printf("seek(%llu)... ", pos);
262 if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
263 if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
264 return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
265 else if(decoder_client_data.total_samples == 0)
266 printf("seek failed, assuming it was past EOF... ");
268 printf("seek past end failed as expected... ");
270 /* hack to work around a deficiency in the seek API's behavior */
271 /* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
272 /* @@@@@@ probably no longer true and we can remove this hack */
273 if(!FLAC__stream_decoder_finish(decoder))
274 return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
276 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
277 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
279 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
280 return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
283 printf("decode_frame... ");
285 if(!FLAC__stream_decoder_process_single(decoder))
286 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
288 printf("decode_frame... ");
290 if(!FLAC__stream_decoder_process_single(decoder))
291 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
298 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
299 if(!FLAC__stream_decoder_finish(decoder))
300 return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
303 printf("\nPASSED!\n");
309 static FLAC__bool seek_barrage_ogg_flac(const char *filename, off_t filesize, unsigned count)
311 OggFLAC__StreamDecoder *decoder;
312 DecoderClientData decoder_client_data;
316 decoder_client_data.got_data = false;
317 decoder_client_data.total_samples = 0;
318 decoder_client_data.quiet = false;
319 decoder_client_data.ignore_errors = false;
320 decoder_client_data.error_occurred = false;
322 printf("\n+++ seek test: OggFLAC__StreamDecoder\n\n");
324 decoder = OggFLAC__stream_decoder_new();
326 return die_("OggFLAC__stream_decoder_new() FAILED, returned NULL\n");
328 if(OggFLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
329 return die_os_("OggFLAC__stream_decoder_init_file() FAILED", decoder);
331 if(!OggFLAC__stream_decoder_process_until_end_of_metadata(decoder))
332 return die_os_("OggFLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
334 /* process until end of stream to make sure we can still seek in that state */
336 /* not necessary for the Ogg seeking method */
337 decoder_client_data.quiet = true;
338 if(!OggFLAC__stream_decoder_process_until_end_of_stream(decoder))
339 return die_os_("OggFLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
340 decoder_client_data.quiet = false;
342 printf("stream decoder state is %s\n", OggFLAC__stream_decoder_get_resolved_state_string(decoder));
343 if(OggFLAC__stream_decoder_get_state(decoder) != OggFLAC__STREAM_DECODER_END_OF_STREAM)
344 return die_os_("expected OggFLAC__STREAM_DECODER_END_OF_STREAM", decoder);
347 printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
348 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
349 if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
350 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
354 n = (long int)decoder_client_data.total_samples;
356 /* if we don't have a total samples count, just guess based on the file size */
357 /* @@@ should get it from last page's granulepos */
359 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
360 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
361 #if !defined _MSC_VER && !defined __MINGW32__
367 printf("Begin seek barrage, count=%u\n", count);
369 for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
372 /* for the first 10, seek to the first 10 samples */
373 if (n >= 10 && i < 10) {
376 /* for the second 10, seek to the last 10 samples */
377 else if (n >= 10 && i < 20) {
378 pos = n - 1 - (i-10);
380 /* for the third 10, seek past the end and make sure we fail properly as expected */
385 #if !defined _MSC_VER && !defined __MINGW32__
386 pos = (FLAC__uint64)(random() % n);
388 /* RAND_MAX is only 32767 in my MSVC */
389 pos = (FLAC__uint64)((rand()<<15|rand()) % n);
393 printf("seek(%llu)... ", pos);
395 if(!OggFLAC__stream_decoder_seek_absolute(decoder, pos)) {
396 if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
397 return die_os_("OggFLAC__stream_decoder_seek_absolute() FAILED", decoder);
398 else if(decoder_client_data.total_samples == 0)
399 printf("seek failed, assuming it was past EOF... ");
401 printf("seek past end failed as expected... ");
403 /* hack to work around a deficiency in the seek API's behavior */
404 /* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
405 /* @@@@@@ probably no longer true and we can remove this hack */
406 if(!OggFLAC__stream_decoder_finish(decoder))
407 return die_os_("OggFLAC__stream_decoder_finish() FAILED", decoder);
409 if(OggFLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
410 return die_os_("OggFLAC__stream_decoder_init_file() FAILED", decoder);
412 if(!OggFLAC__stream_decoder_process_until_end_of_metadata(decoder))
413 return die_os_("OggFLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
416 printf("decode_frame... ");
418 if(!OggFLAC__stream_decoder_process_single(decoder))
419 return die_os_("OggFLAC__stream_decoder_process_single() FAILED", decoder);
421 printf("decode_frame... ");
423 if(!OggFLAC__stream_decoder_process_single(decoder))
424 return die_os_("OggFLAC__stream_decoder_process_single() FAILED", decoder);
431 if(OggFLAC__stream_decoder_get_state(decoder) != OggFLAC__STREAM_DECODER_UNINITIALIZED) {
432 if(!OggFLAC__stream_decoder_finish(decoder))
433 return die_os_("OggFLAC__stream_decoder_finish() FAILED", decoder);
436 printf("\nPASSED!\n");
442 int main(int argc, char *argv[])
444 const char *filename;
448 static const char * const usage = "usage: test_seeking file.flac [#seeks]\n";
450 if (argc < 1 || argc > 3) {
451 fprintf(stderr, usage);
458 count = strtoul(argv[2], 0, 10);
461 fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
463 #if !defined _MSC_VER && !defined __MINGW32__
467 if (gettimeofday(&tv, 0) < 0) {
468 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
477 filesize = get_filesize_(filename);
479 fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
483 (void) signal(SIGINT, our_sigint_handler_);
487 if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
489 ok = seek_barrage_ogg_flac(filename, filesize, count);
491 fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
496 ok = seek_barrage_native_flac(filename, filesize, count);