minor syntax
[flac.git] / src / test_seeking / main.c
1 /* test_seeking - Seeking tester for libFLAC and libOggFLAC
2  * Copyright (C) 2004,2005,2006  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #if defined _MSC_VER || defined __MINGW32__
28 #include <time.h>
29 #else
30 #include <sys/time.h>
31 #endif
32 #include <sys/stat.h> /* for stat() */
33 #include "FLAC/assert.h"
34 #include "FLAC/file_decoder.h"
35 #ifdef FLAC__HAS_OGG
36 #include "OggFLAC/file_decoder.h"
37 #endif
38
39 typedef struct {
40         FLAC__bool got_data;
41         FLAC__uint64 total_samples;
42         unsigned channels;
43         unsigned bits_per_sample;
44         FLAC__bool ignore_errors;
45         FLAC__bool error_occurred;
46 } decoder_client_data_struct;
47
48 static FLAC__bool stop_signal_ = false;
49
50 static void our_sigint_handler_(int signal)
51 {
52         (void)signal;
53         printf("(caught SIGINT) ");
54         fflush(stdout);
55         stop_signal_ = true;
56 }
57
58 static FLAC__bool die_(const char *msg)
59 {
60         printf("ERROR: %s\n", msg);
61         return false;
62 }
63
64 static FLAC__bool die_f_(const char *msg, const FLAC__FileDecoder *decoder)
65 {
66         FLAC__FileDecoderState state = FLAC__file_decoder_get_state(decoder);
67
68         if(msg)
69                 printf("FAILED, %s", msg);
70         else
71                 printf("FAILED");
72
73         printf(", state = %u (%s)\n", (unsigned)state, FLAC__FileDecoderStateString[state]);
74         if(state == FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
75                 FLAC__SeekableStreamDecoderState state_ = FLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
76                 printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state, FLAC__SeekableStreamDecoderStateString[state_]);
77                 if(state_ == FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
78                         FLAC__StreamDecoderState state__ = FLAC__file_decoder_get_stream_decoder_state(decoder);
79                         printf("      stream decoder state = %u (%s)\n", (unsigned)state__, FLAC__StreamDecoderStateString[state__]);
80                 }
81         }
82
83         return false;
84 }
85
86 #ifdef FLAC__HAS_OGG
87 static FLAC__bool die_of_(const char *msg, const OggFLAC__FileDecoder *decoder)
88 {
89         OggFLAC__FileDecoderState state = OggFLAC__file_decoder_get_state(decoder);
90
91         if(msg)
92                 printf("FAILED, %s", msg);
93         else
94                 printf("FAILED");
95
96         printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__SeekableStreamDecoderStateString[state]);
97         if(state == OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
98                 OggFLAC__SeekableStreamDecoderState state_ = OggFLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
99                 printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state_, OggFLAC__SeekableStreamDecoderStateString[state_]);
100                 if(state_ == OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
101                         OggFLAC__StreamDecoderState state__ = OggFLAC__file_decoder_get_stream_decoder_state(decoder);
102                         printf("      stream decoder state = %u (%s)\n", (unsigned)state__, OggFLAC__StreamDecoderStateString[state__]);
103                         if(state__ == OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
104                                 FLAC__StreamDecoderState state___ = OggFLAC__file_decoder_get_FLAC_stream_decoder_state(decoder);
105                                 printf("      FLAC stream decoder state = %u (%s)\n", (unsigned)state___, FLAC__StreamDecoderStateString[state___]);
106                         }
107                 }
108         }
109
110         return false;
111 }
112 #endif
113
114 static off_t get_filesize_(const char *srcpath)
115 {
116         struct stat srcstat;
117
118         if(0 == stat(srcpath, &srcstat))
119                 return srcstat.st_size;
120         else
121                 return -1;
122 }
123
124 static FLAC__StreamDecoderWriteStatus file_decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
125 {
126         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
127
128         (void)decoder, (void)buffer;
129
130         if(0 == dcd) {
131                 printf("ERROR: client_data in write callback is NULL\n");
132                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
133         }
134
135         if(dcd->error_occurred)
136                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
137
138         if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)
139                 printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
140         else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
141                 printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
142         else {
143                 FLAC__ASSERT(0);
144                 dcd->error_occurred = true;
145                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
146         }
147         fflush(stdout);
148
149         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
150 }
151
152 static void file_decoder_metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
153 {
154         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
155
156         (void)decoder;
157
158         if(0 == dcd) {
159                 printf("ERROR: client_data in metadata callback is NULL\n");
160                 return;
161         }
162
163         if(dcd->error_occurred)
164                 return;
165
166         if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
167                 dcd->got_data = true;
168                 dcd->total_samples = metadata->data.stream_info.total_samples;
169                 dcd->channels = metadata->data.stream_info.channels;
170                 dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
171         }
172 }
173
174 static void file_decoder_error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
175 {
176         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
177
178         (void)decoder;
179
180         if(0 == dcd) {
181                 printf("ERROR: client_data in error callback is NULL\n");
182                 return;
183         }
184
185         if(!dcd->ignore_errors) {
186                 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
187                 dcd->error_occurred = true;
188         }
189 }
190
191 static FLAC__bool seek_barrage_native_flac(const char *filename, off_t filesize, unsigned count)
192 {
193         FLAC__FileDecoder *decoder;
194         decoder_client_data_struct decoder_client_data;
195         unsigned i;
196         long int n;
197
198         decoder_client_data.got_data = false;
199         decoder_client_data.total_samples = 0;
200         decoder_client_data.ignore_errors = false;
201         decoder_client_data.error_occurred = false;
202
203         printf("\n+++ seek test: FLAC__FileDecoder\n\n");
204
205         decoder = FLAC__file_decoder_new();
206         if(0 == decoder)
207                 return die_("FLAC__file_decoder_new() FAILED, returned NULL\n");
208
209         if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
210                 return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder);
211
212         if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
213                 return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder);
214
215         if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
216                 return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder);
217
218         if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
219                 return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder);
220
221         if(!FLAC__file_decoder_set_filename(decoder, filename))
222                 return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder);
223
224         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
225                 return die_f_("FLAC__file_decoder_init() FAILED", decoder);
226
227         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
228                 return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
229
230         printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
231 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
232         if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
233                 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
234                 return false;
235         }
236 #endif
237         n = (long int)decoder_client_data.total_samples;
238
239         /* if we don't have a total samples count, just guess based on the file size */
240         /* @@@ should get it from last page's granulepos */
241         if(n == 0) {
242                 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
243                 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
244 #if !defined _MSC_VER && !defined __MINGW32__
245                 if(n > RAND_MAX)
246                         n = RAND_MAX;
247 #endif
248         }
249
250         printf("Begin seek barrage, count=%u\n", count);
251
252         for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
253                 FLAC__uint64 pos;
254
255                 /* for the first 10, seek to the first 10 samples */
256                 if (n >= 10 && i < 10) {
257                         pos = i;
258                 }
259                 /* for the second 10, seek to the last 10 samples */
260                 else if (n >= 10 && i < 20) {
261                         pos = n - 1 - (i-10);
262                 }
263                 /* for the third 10, seek past the end and make sure we fail properly as expected */
264                 else if (i < 30) {
265                         pos = n + (i-20);
266                 }
267                 else {
268 #if !defined _MSC_VER && !defined __MINGW32__
269                         pos = (FLAC__uint64)(random() % n);
270 #else
271                         /* RAND_MAX is only 32767 in my MSVC */
272                         pos = (FLAC__uint64)((rand()<<15|rand()) % n);
273 #endif
274                 }
275
276                 printf("seek(%llu)... ", pos);
277                 fflush(stdout);
278                 if(!FLAC__file_decoder_seek_absolute(decoder, pos)) {
279                         if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
280                                 return die_f_("FLAC__file_decoder_seek_absolute() FAILED", decoder);
281                         else if(decoder_client_data.total_samples == 0)
282                                 printf("seek failed, assuming it was past EOF... ");
283                         else
284                                 printf("seek past end failed as expected... ");
285
286                         /* hack to work around a deficiency in the seek API's behavior */
287                         /* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
288                         if(!FLAC__file_decoder_finish(decoder))
289                                 return die_f_("FLAC__file_decoder_finish() FAILED", decoder);
290
291                         if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
292                                 return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder);
293
294                         if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
295                                 return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder);
296
297                         if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
298                                 return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder);
299
300                         if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
301                                 return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder);
302
303                         if(!FLAC__file_decoder_set_filename(decoder, filename))
304                                 return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder);
305
306                         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
307                                 return die_f_("FLAC__file_decoder_init() FAILED", decoder);
308
309                         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
310                                 return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
311                 }
312                 else {
313                         printf("decode_frame... ");
314                         fflush(stdout);
315                         if(!FLAC__file_decoder_process_single(decoder))
316                                 return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);
317
318                         printf("decode_frame... ");
319                         fflush(stdout);
320                         if(!FLAC__file_decoder_process_single(decoder))
321                                 return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);
322                 }
323
324                 printf("OK\n");
325                 fflush(stdout);
326         }
327
328         if(FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED) {
329                 if(!FLAC__file_decoder_finish(decoder))
330                         return die_f_("FLAC__file_decoder_finish() FAILED", decoder);
331         }
332
333         printf("\nPASSED!\n");
334
335         return true;
336 }
337
338 #ifdef FLAC__HAS_OGG
339 static FLAC__bool seek_barrage_ogg_flac(const char *filename, off_t filesize, unsigned count)
340 {
341         OggFLAC__FileDecoder *decoder;
342         decoder_client_data_struct decoder_client_data;
343         unsigned i;
344         long int n;
345
346         decoder_client_data.got_data = false;
347         decoder_client_data.total_samples = 0;
348         decoder_client_data.ignore_errors = false;
349         decoder_client_data.error_occurred = false;
350
351         printf("\n+++ seek test: OggFLAC__FileDecoder\n\n");
352
353         decoder = OggFLAC__file_decoder_new();
354         if(0 == decoder)
355                 return die_("OggFLAC__file_decoder_new() FAILED, returned NULL\n");
356
357         if(!OggFLAC__file_decoder_set_write_callback(decoder, (OggFLAC__FileDecoderWriteCallback)file_decoder_write_callback_))
358                 return die_of_("OggFLAC__file_decoder_set_write_callback() FAILED", decoder);
359
360         if(!OggFLAC__file_decoder_set_metadata_callback(decoder, (OggFLAC__FileDecoderMetadataCallback)file_decoder_metadata_callback_))
361                 return die_of_("OggFLAC__file_decoder_set_metadata_callback() FAILED", decoder);
362
363         if(!OggFLAC__file_decoder_set_error_callback(decoder, (OggFLAC__FileDecoderErrorCallback)file_decoder_error_callback_))
364                 return die_of_("OggFLAC__file_decoder_set_error_callback() FAILED", decoder);
365
366         if(!OggFLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
367                 return die_of_("OggFLAC__file_decoder_set_client_data() FAILED", decoder);
368
369         if(!OggFLAC__file_decoder_set_filename(decoder, filename))
370                 return die_of_("OggFLAC__file_decoder_set_filename() FAILED", decoder);
371
372         if(OggFLAC__file_decoder_init(decoder) != OggFLAC__FILE_DECODER_OK)
373                 return die_of_("OggFLAC__file_decoder_init() FAILED", decoder);
374
375         if(!OggFLAC__file_decoder_process_until_end_of_metadata(decoder))
376                 return die_of_("OggFLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
377
378         printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
379 #if !defined _MSC_VER && !defined __MINGW32__
380         if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
381                 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
382                 return false;
383         }
384 #endif
385         n = (long int)decoder_client_data.total_samples;
386
387         /* if we don't have a total samples count, just guess based on the file size */
388         if(n == 0) {
389                 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
390                 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
391 #if !defined _MSC_VER && !defined __MINGW32__
392                 if(n > RAND_MAX)
393                         n = RAND_MAX;
394 #endif
395         }
396
397         printf("Begin seek barrage, count=%u\n", count);
398
399         for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
400                 FLAC__uint64 pos;
401
402                 /* for the first 10, seek to the first 10 samples */
403                 if (n >= 10 && i < 10) {
404                         pos = i;
405                 }
406                 /* for the second 10, seek to the last 10 samples */
407                 else if (n >= 10 && i < 20) {
408                         pos = n - 1 - (i-10);
409                 }
410                 /* for the third 10, seek past the end and make sure we fail properly as expected */
411                 else if (i < 30) {
412                         pos = n + (i-20);
413                 }
414                 else {
415 #if !defined _MSC_VER && !defined __MINGW32__
416                         pos = (FLAC__uint64)(random() % n);
417 #else
418                         /* RAND_MAX is only 32767 in my MSVC */
419                         pos = (FLAC__uint64)((rand()<<15|rand()) % n);
420 #endif
421                 }
422
423                 printf("seek(%llu)... ", pos);
424                 fflush(stdout);
425                 if(!OggFLAC__file_decoder_seek_absolute(decoder, pos)) {
426                         if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
427                                 return die_of_("OggFLAC__file_decoder_seek_absolute() FAILED", decoder);
428                         else if(decoder_client_data.total_samples == 0)
429                                 printf("seek failed, assuming it was past EOF... ");
430                         else
431                                 printf("seek past end failed as expected... ");
432
433                         /* hack to work around a deficiency in the seek API's behavior */
434                         /* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
435                         if(!OggFLAC__file_decoder_finish(decoder))
436                                 return die_of_("OggFLAC__file_decoder_finish() FAILED", decoder);
437
438                         if(!OggFLAC__file_decoder_set_write_callback(decoder, (OggFLAC__FileDecoderWriteCallback)file_decoder_write_callback_))
439                                 return die_of_("OggFLAC__file_decoder_set_write_callback() FAILED", decoder);
440
441                         if(!OggFLAC__file_decoder_set_metadata_callback(decoder, (OggFLAC__FileDecoderMetadataCallback)file_decoder_metadata_callback_))
442                                 return die_of_("OggFLAC__file_decoder_set_metadata_callback() FAILED", decoder);
443
444                         if(!OggFLAC__file_decoder_set_error_callback(decoder, (OggFLAC__FileDecoderErrorCallback)file_decoder_error_callback_))
445                                 return die_of_("OggFLAC__file_decoder_set_error_callback() FAILED", decoder);
446
447                         if(!OggFLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
448                                 return die_of_("OggFLAC__file_decoder_set_client_data() FAILED", decoder);
449
450                         if(!OggFLAC__file_decoder_set_filename(decoder, filename))
451                                 return die_of_("OggFLAC__file_decoder_set_filename() FAILED", decoder);
452
453                         if(OggFLAC__file_decoder_init(decoder) != OggFLAC__FILE_DECODER_OK)
454                                 return die_of_("OggFLAC__file_decoder_init() FAILED", decoder);
455
456                         if(!OggFLAC__file_decoder_process_until_end_of_metadata(decoder))
457                                 return die_of_("OggFLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
458                 }
459                 else {
460                         printf("decode_frame... ");
461                         fflush(stdout);
462                         if(!OggFLAC__file_decoder_process_single(decoder))
463                                 return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);
464
465                         printf("decode_frame... ");
466                         fflush(stdout);
467                         if(!OggFLAC__file_decoder_process_single(decoder))
468                                 return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);
469                 }
470
471                 printf("OK\n");
472                 fflush(stdout);
473         }
474
475         if(OggFLAC__file_decoder_get_state(decoder) != OggFLAC__FILE_DECODER_UNINITIALIZED) {
476                 if(!OggFLAC__file_decoder_finish(decoder))
477                         return die_of_("OggFLAC__file_decoder_finish() FAILED", decoder);
478         }
479
480         printf("\nPASSED!\n");
481
482         return true;
483 }
484 #endif
485
486 int main(int argc, char *argv[])
487 {
488         const char *filename;
489         unsigned count = 0;
490         off_t filesize;
491
492         static const char * const usage = "usage: test_seeking file.flac [#seeks]\n";
493
494         if (argc < 1 || argc > 3) {
495                 fprintf(stderr, usage);
496                 return 1;
497         }
498
499         filename = argv[1];
500
501         if (argc > 2)
502                 count = strtoul(argv[2], 0, 10);
503
504         if (count < 30)
505                 fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
506
507 #if !defined _MSC_VER && !defined __MINGW32__
508         {
509                 struct timeval tv;
510
511                 if (gettimeofday(&tv, 0) < 0) {
512                         fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
513                         tv.tv_usec = 4321;
514                 }
515                 srandom(tv.tv_usec);
516         }
517 #else
518         srand(time(0));
519 #endif
520
521         filesize = get_filesize_(filename);
522         if (filesize < 0) {
523                 fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
524                 return 1;
525         }
526
527         (void) signal(SIGINT, our_sigint_handler_);
528
529         {
530                 FLAC__bool ok;
531                 if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
532 #ifdef FLAC__HAS_OGG
533                         ok = seek_barrage_ogg_flac(filename, filesize, count);
534 #else
535                         fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
536                         ok = false;
537 #endif
538                 }
539                 else {
540                         ok = seek_barrage_native_flac(filename, filesize, count);
541                 }
542                 return ok? 0 : 2;
543         }
544 }