main.c 17.6 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* test_seeking - Seeking tester for libFLAC and libOggFLAC
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2004,2005  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

19
20
21
22
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
23
24
25
26
27
28
29
30
31
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined _MSC_VER || defined __MINGW32__
#include <time.h>
#else
#include <sys/time.h>
#endif
32
#include <sys/stat.h> /* for stat() */
Josh Coalson's avatar
Josh Coalson committed
33
34
#include "FLAC/assert.h"
#include "FLAC/file_decoder.h"
35
#ifdef FLAC__HAS_OGG
Josh Coalson's avatar
Josh Coalson committed
36
#include "OggFLAC/file_decoder.h"
37
#endif
Josh Coalson's avatar
Josh Coalson committed
38
39

typedef struct {
40
	FLAC__bool got_data;
Josh Coalson's avatar
Josh Coalson committed
41
	FLAC__uint64 total_samples;
42
43
	unsigned channels;
	unsigned bits_per_sample;
Josh Coalson's avatar
Josh Coalson committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	FLAC__bool ignore_errors;
	FLAC__bool error_occurred;
} decoder_client_data_struct;

static FLAC__bool stop_signal_ = false;

static void our_sigint_handler_(int signal)
{
	(void)signal;
	printf("(caught SIGINT) ");
	fflush(stdout);
	stop_signal_ = true;
}

static FLAC__bool die_(const char *msg)
{
	printf("ERROR: %s\n", msg);
	return false;
}

static FLAC__bool die_f_(const char *msg, const FLAC__FileDecoder *decoder)
{
	FLAC__FileDecoderState state = FLAC__file_decoder_get_state(decoder);

	if(msg)
		printf("FAILED, %s", msg);
	else
		printf("FAILED");

	printf(", state = %u (%s)\n", (unsigned)state, FLAC__FileDecoderStateString[state]);
	if(state == FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
		FLAC__SeekableStreamDecoderState state_ = FLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
		printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state, FLAC__SeekableStreamDecoderStateString[state_]);
		if(state_ == FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
			FLAC__StreamDecoderState state__ = FLAC__file_decoder_get_stream_decoder_state(decoder);
			printf("      stream decoder state = %u (%s)\n", (unsigned)state__, FLAC__StreamDecoderStateString[state__]);
		}
	}

	return false;
}

86
#ifdef FLAC__HAS_OGG
Josh Coalson's avatar
Josh Coalson committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
static FLAC__bool die_of_(const char *msg, const OggFLAC__FileDecoder *decoder)
{
	OggFLAC__FileDecoderState state = OggFLAC__file_decoder_get_state(decoder);

	if(msg)
		printf("FAILED, %s", msg);
	else
		printf("FAILED");

	printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__SeekableStreamDecoderStateString[state]);
	if(state == OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
		OggFLAC__SeekableStreamDecoderState state_ = OggFLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
		printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state_, OggFLAC__SeekableStreamDecoderStateString[state_]);
		if(state_ == OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
			OggFLAC__StreamDecoderState state__ = OggFLAC__file_decoder_get_stream_decoder_state(decoder);
			printf("      stream decoder state = %u (%s)\n", (unsigned)state__, OggFLAC__StreamDecoderStateString[state__]);
			if(state__ == OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
				FLAC__StreamDecoderState state___ = OggFLAC__file_decoder_get_FLAC_stream_decoder_state(decoder);
				printf("      FLAC stream decoder state = %u (%s)\n", (unsigned)state___, FLAC__StreamDecoderStateString[state___]);
			}
		}
	}

	return false;
}
112
#endif
Josh Coalson's avatar
Josh Coalson committed
113

114
115
116
117
118
119
120
121
122
123
static off_t get_filesize_(const char *srcpath)
{
	struct stat srcstat;

	if(0 == stat(srcpath, &srcstat))
		return srcstat.st_size;
	else
		return -1;
}

Josh Coalson's avatar
Josh Coalson committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
static FLAC__StreamDecoderWriteStatus file_decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
	decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;

	(void)decoder, (void)buffer;

	if(0 == dcd) {
		printf("ERROR: client_data in write callback is NULL\n");
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}

	if(dcd->error_occurred)
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)
		printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
	else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
		printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
	else {
		FLAC__ASSERT(0);
		dcd->error_occurred = true;
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}
	fflush(stdout);

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

static void file_decoder_metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
	decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;

	(void)decoder;

	if(0 == dcd) {
		printf("ERROR: client_data in metadata callback is NULL\n");
		return;
	}

	if(dcd->error_occurred)
		return;

166
167
	if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
		dcd->got_data = true;
Josh Coalson's avatar
Josh Coalson committed
168
		dcd->total_samples = metadata->data.stream_info.total_samples;
169
170
171
		dcd->channels = metadata->data.stream_info.channels;
		dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
	}
Josh Coalson's avatar
Josh Coalson committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
}

static void file_decoder_error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;

	(void)decoder;

	if(0 == dcd) {
		printf("ERROR: client_data in error callback is NULL\n");
		return;
	}

	if(!dcd->ignore_errors) {
		printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
		dcd->error_occurred = true;
	}
}

191
static FLAC__bool seek_barrage_native_flac(const char *filename, off_t filesize, unsigned count)
Josh Coalson's avatar
Josh Coalson committed
192
193
194
195
196
197
{
	FLAC__FileDecoder *decoder;
	decoder_client_data_struct decoder_client_data;
	unsigned i;
	long int n;

198
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	decoder_client_data.total_samples = 0;
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

	printf("\n+++ seek test: FLAC__FileDecoder\n\n");

	decoder = FLAC__file_decoder_new();
	if(0 == decoder)
		return die_("FLAC__file_decoder_new() FAILED, returned NULL\n");

	if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
		return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder);

	if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
		return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder);

	if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
		return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder);

	if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
		return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder);

	if(!FLAC__file_decoder_set_filename(decoder, filename))
		return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder);

	if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
		return die_f_("FLAC__file_decoder_init() FAILED", decoder);

	if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
		return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);

	printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
231
232
	if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
		printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
Josh Coalson's avatar
Josh Coalson committed
233
234
235
236
		return false;
	}
	n = (long int)decoder_client_data.total_samples;

237
238
239
240
241
242
243
244
245
	/* if we don't have a total samples count, just guess based on the file size */
	/* @@@ should get it from last page's granulepos */
	if(n == 0) {
		/* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
		n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
		if(n > RAND_MAX)
			n = RAND_MAX;
	}

Josh Coalson's avatar
Josh Coalson committed
246
247
248
249
250
	printf("Begin seek barrage, count=%u\n", count);

	for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
		FLAC__uint64 pos;

251
252
253
254
255
256
257
258
		/* for the first 10, seek to the first 10 samples */
		if (n >= 10 && i < 10) {
			pos = i;
		}
		/* for the second 10, seek to the last 10 samples */
		else if (n >= 10 && i < 20) {
			pos = n - 1 - (i-10);
		}
259
260
261
262
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
263
		else {
Josh Coalson's avatar
Josh Coalson committed
264
#if !defined _MSC_VER && !defined __MINGW32__
265
			pos = (FLAC__uint64)(random() % n);
Josh Coalson's avatar
Josh Coalson committed
266
#else
267
			pos = (FLAC__uint64)(rand() % n);
Josh Coalson's avatar
Josh Coalson committed
268
#endif
269
		}
Josh Coalson's avatar
Josh Coalson committed
270
271
272

		printf("seek(%llu)... ", pos);
		fflush(stdout);
273
274
275
276
277
278
279
		if(!FLAC__file_decoder_seek_absolute(decoder, pos)) {
			if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
				return die_f_("FLAC__file_decoder_seek_absolute() FAILED", decoder);
			else if(decoder_client_data.total_samples == 0)
				printf("seek failed, assuming it was past EOF... ");
			else
				printf("seek past end failed as expected... ");
Josh Coalson's avatar
Josh Coalson committed
280

281
282
283
284
			/* hack to work around a deficiency in the seek API's bahavior */
			/* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
			if(!FLAC__file_decoder_finish(decoder))
				return die_f_("FLAC__file_decoder_finish() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
285

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
			if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
				return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder);

			if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
				return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder);

			if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
				return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder);

			if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
				return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder);

			if(!FLAC__file_decoder_set_filename(decoder, filename))
				return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder);

			if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
				return die_f_("FLAC__file_decoder_init() FAILED", decoder);

			if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
				return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
		}
		else {
			printf("decode_frame... ");
			fflush(stdout);
			if(!FLAC__file_decoder_process_single(decoder))
				return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);

			printf("decode_frame... ");
			fflush(stdout);
			if(!FLAC__file_decoder_process_single(decoder))
				return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);
		}
Josh Coalson's avatar
Josh Coalson committed
318
319
320
321
322
323
324
325
326
327

		printf("OK\n");
		fflush(stdout);
	}

	printf("\nPASSED!\n");

	return true;
}

328
#ifdef FLAC__HAS_OGG
329
static FLAC__bool seek_barrage_ogg_flac(const char *filename, off_t filesize, unsigned count)
Josh Coalson's avatar
Josh Coalson committed
330
331
332
333
334
335
{
	OggFLAC__FileDecoder *decoder;
	decoder_client_data_struct decoder_client_data;
	unsigned i;
	long int n;

336
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	decoder_client_data.total_samples = 0;
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

	printf("\n+++ seek test: OggFLAC__FileDecoder\n\n");

	decoder = OggFLAC__file_decoder_new();
	if(0 == decoder)
		return die_("OggFLAC__file_decoder_new() FAILED, returned NULL\n");

	if(!OggFLAC__file_decoder_set_write_callback(decoder, (OggFLAC__FileDecoderWriteCallback)file_decoder_write_callback_))
		return die_of_("OggFLAC__file_decoder_set_write_callback() FAILED", decoder);

	if(!OggFLAC__file_decoder_set_metadata_callback(decoder, (OggFLAC__FileDecoderMetadataCallback)file_decoder_metadata_callback_))
		return die_of_("OggFLAC__file_decoder_set_metadata_callback() FAILED", decoder);

	if(!OggFLAC__file_decoder_set_error_callback(decoder, (OggFLAC__FileDecoderErrorCallback)file_decoder_error_callback_))
		return die_of_("OggFLAC__file_decoder_set_error_callback() FAILED", decoder);

	if(!OggFLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
		return die_of_("OggFLAC__file_decoder_set_client_data() FAILED", decoder);

	if(!OggFLAC__file_decoder_set_filename(decoder, filename))
		return die_of_("OggFLAC__file_decoder_set_filename() FAILED", decoder);

	if(OggFLAC__file_decoder_init(decoder) != OggFLAC__FILE_DECODER_OK)
		return die_of_("OggFLAC__file_decoder_init() FAILED", decoder);

	if(!OggFLAC__file_decoder_process_until_end_of_metadata(decoder))
		return die_of_("OggFLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);

	printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
369
370
	if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
		printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
Josh Coalson's avatar
Josh Coalson committed
371
372
373
374
		return false;
	}
	n = (long int)decoder_client_data.total_samples;

375
376
377
378
379
380
381
382
	/* if we don't have a total samples count, just guess based on the file size */
	if(n == 0) {
		/* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
		n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
		if(n > RAND_MAX)
			n = RAND_MAX;
	}

Josh Coalson's avatar
Josh Coalson committed
383
384
385
386
387
	printf("Begin seek barrage, count=%u\n", count);

	for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
		FLAC__uint64 pos;

388
389
390
391
392
393
394
395
		/* for the first 10, seek to the first 10 samples */
		if (n >= 10 && i < 10) {
			pos = i;
		}
		/* for the second 10, seek to the last 10 samples */
		else if (n >= 10 && i < 20) {
			pos = n - 1 - (i-10);
		}
396
397
398
399
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
400
		else {
Josh Coalson's avatar
Josh Coalson committed
401
#if !defined _MSC_VER && !defined __MINGW32__
402
			pos = (FLAC__uint64)(random() % n);
Josh Coalson's avatar
Josh Coalson committed
403
#else
404
			pos = (FLAC__uint64)(rand() % n);
Josh Coalson's avatar
Josh Coalson committed
405
#endif
406
		}
Josh Coalson's avatar
Josh Coalson committed
407
408
409

		printf("seek(%llu)... ", pos);
		fflush(stdout);
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
		if(!OggFLAC__file_decoder_seek_absolute(decoder, pos)) {
			if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
				return die_of_("OggFLAC__file_decoder_seek_absolute() FAILED", decoder);
			else if(decoder_client_data.total_samples == 0)
				printf("seek failed, assuming it was past EOF... ");
			else
				printf("seek past end failed as expected... ");
			/* hack to work around a deficiency in the seek API's bahavior */
			/* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
			if(!OggFLAC__file_decoder_finish(decoder))
				return die_of_("OggFLAC__file_decoder_finish() FAILED", decoder);

			if(!OggFLAC__file_decoder_set_write_callback(decoder, (OggFLAC__FileDecoderWriteCallback)file_decoder_write_callback_))
				return die_of_("OggFLAC__file_decoder_set_write_callback() FAILED", decoder);

			if(!OggFLAC__file_decoder_set_metadata_callback(decoder, (OggFLAC__FileDecoderMetadataCallback)file_decoder_metadata_callback_))
				return die_of_("OggFLAC__file_decoder_set_metadata_callback() FAILED", decoder);

			if(!OggFLAC__file_decoder_set_error_callback(decoder, (OggFLAC__FileDecoderErrorCallback)file_decoder_error_callback_))
				return die_of_("OggFLAC__file_decoder_set_error_callback() FAILED", decoder);

			if(!OggFLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
				return die_of_("OggFLAC__file_decoder_set_client_data() FAILED", decoder);

			if(!OggFLAC__file_decoder_set_filename(decoder, filename))
				return die_of_("OggFLAC__file_decoder_set_filename() FAILED", decoder);

			if(OggFLAC__file_decoder_init(decoder) != OggFLAC__FILE_DECODER_OK)
				return die_of_("OggFLAC__file_decoder_init() FAILED", decoder);

			if(!OggFLAC__file_decoder_process_until_end_of_metadata(decoder))
				return die_of_("OggFLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
		}
		else {
			printf("decode_frame... ");
			fflush(stdout);
			if(!OggFLAC__file_decoder_process_single(decoder))
				return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);

			printf("decode_frame... ");
			fflush(stdout);
			if(!OggFLAC__file_decoder_process_single(decoder))
				return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);
		}
Josh Coalson's avatar
Josh Coalson committed
454
455
456
457
458
459
460
461
462

		printf("OK\n");
		fflush(stdout);
	}

	printf("\nPASSED!\n");

	return true;
}
463
#endif
Josh Coalson's avatar
Josh Coalson committed
464
465
466
467
468

int main(int argc, char *argv[])
{
	const char *filename;
	unsigned count = 0;
469
	off_t filesize;
Josh Coalson's avatar
Josh Coalson committed
470
471
472
473
474
475
476
477
478
479
480
481
482

	static const char * const usage = "usage: test_seeking file.flac [#seeks]\n";

	if (argc < 1 || argc > 3) {
		fprintf(stderr, usage);
		return 1;
	}

	filename = argv[1];

	if (argc > 2)
		count = strtoul(argv[2], 0, 10);

483
484
	if (count < 30)
		fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
485

Josh Coalson's avatar
Josh Coalson committed
486
487
488
489
#if !defined _MSC_VER && !defined __MINGW32__
	{
		struct timeval tv;

490
		if (gettimeofday(&tv, 0) < 0) {
Josh Coalson's avatar
Josh Coalson committed
491
492
493
494
495
496
497
498
499
			fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
			tv.tv_usec = 4321;
		}
		srandom(tv.tv_usec);
	}
#else
	srand(time(0));
#endif

500
501
502
503
504
505
	filesize = get_filesize_(filename);
	if (filesize < 0) {
		fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
		return 1;
	}

Josh Coalson's avatar
Josh Coalson committed
506
507
508
509
	(void) signal(SIGINT, our_sigint_handler_);

	{
		FLAC__bool ok;
510
511
		if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
#ifdef FLAC__HAS_OGG
512
			ok = seek_barrage_ogg_flac(filename, filesize, count);
513
514
515
516
517
518
#else
			fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
			ok = false;
#endif
		}
		else {
519
			ok = seek_barrage_native_flac(filename, filesize, count);
520
		}
Josh Coalson's avatar
Josh Coalson committed
521
522
523
		return ok? 0 : 2;
	}
}