main.c 9.9 KB
Newer Older
1
/* test_seeking - Seeking tester for libFLAC
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2004,2005,2006  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.
 */

Josh Coalson's avatar
Josh Coalson committed
19
20
#if HAVE_CONFIG_H
#  include <config.h>
21
22
#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
#include "FLAC/assert.h"
34
#include "FLAC/stream_decoder.h"
Josh Coalson's avatar
Josh Coalson committed
35
36

typedef struct {
37
	FLAC__bool got_data;
Josh Coalson's avatar
Josh Coalson committed
38
	FLAC__uint64 total_samples;
39
40
	unsigned channels;
	unsigned bits_per_sample;
41
	FLAC__bool quiet;
Josh Coalson's avatar
Josh Coalson committed
42
43
	FLAC__bool ignore_errors;
	FLAC__bool error_occurred;
44
} DecoderClientData;
Josh Coalson's avatar
Josh Coalson committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

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;
}

62
static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
63
{
64
	FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
Josh Coalson's avatar
Josh Coalson committed
65
66
67
68
69
70

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

71
	printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
Josh Coalson's avatar
Josh Coalson committed
72
73
74
75

	return false;
}

76
77
78
79
80
81
82
83
84
85
static off_t get_filesize_(const char *srcpath)
{
	struct stat srcstat;

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

86
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
Josh Coalson's avatar
Josh Coalson committed
87
{
88
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
89
90
91
92
93
94
95
96
97
98
99

	(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;

100
101
102
103
104
105
106
107
	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
		if (!dcd->quiet)
			printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
	}
	else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) {
		if (!dcd->quiet)
			printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
	}
Josh Coalson's avatar
Josh Coalson committed
108
109
110
111
112
113
114
115
116
117
	else {
		FLAC__ASSERT(0);
		dcd->error_occurred = true;
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}
	fflush(stdout);

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

118
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
119
{
120
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
121
122
123
124
125
126
127
128
129
130
131

	(void)decoder;

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

	if(dcd->error_occurred)
		return;

132
133
	if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
		dcd->got_data = true;
Josh Coalson's avatar
Josh Coalson committed
134
		dcd->total_samples = metadata->data.stream_info.total_samples;
135
136
137
		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
138
139
}

140
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
141
{
142
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156

	(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;
	}
}

157
static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, off_t filesize, unsigned count)
Josh Coalson's avatar
Josh Coalson committed
158
{
159
160
	FLAC__StreamDecoder *decoder;
	DecoderClientData decoder_client_data;
Josh Coalson's avatar
Josh Coalson committed
161
162
163
	unsigned i;
	long int n;

164
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
165
	decoder_client_data.total_samples = 0;
166
	decoder_client_data.quiet = false;
Josh Coalson's avatar
Josh Coalson committed
167
168
169
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

170
	printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC)\n\n", is_ogg? "Ogg":"native");
Josh Coalson's avatar
Josh Coalson committed
171

172
	decoder = FLAC__stream_decoder_new();
Josh Coalson's avatar
Josh Coalson committed
173
	if(0 == decoder)
174
		return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
Josh Coalson's avatar
Josh Coalson committed
175

176
177
178
179
180
181
182
183
	if(is_ogg) {
		if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
			return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
	}
	else {
		if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
			return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
	}
Josh Coalson's avatar
Josh Coalson committed
184

185
186
	if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
		return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
187

188
	if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
189
	/* process until end of stream to make sure we can still seek in that state */
190
191
192
193
194
195
196
197
198
		decoder_client_data.quiet = true;
		if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
			return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
		decoder_client_data.quiet = false;

		printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
		if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
			return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
	}
199

Josh Coalson's avatar
Josh Coalson committed
200
	printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
201
#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
202
203
	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
204
205
		return false;
	}
Josh Coalson's avatar
Josh Coalson committed
206
#endif
Josh Coalson's avatar
Josh Coalson committed
207
208
	n = (long int)decoder_client_data.total_samples;

209
	/* if we don't have a total samples count, just guess based on the file size */
210
	/* @@@ for is_ogg we should get it from last page's granulepos */
211
212
213
	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);
Josh Coalson's avatar
Josh Coalson committed
214
#if !defined _MSC_VER && !defined __MINGW32__
215
216
		if(n > RAND_MAX)
			n = RAND_MAX;
Josh Coalson's avatar
Josh Coalson committed
217
#endif
218
219
	}

Josh Coalson's avatar
Josh Coalson committed
220
221
222
223
224
	printf("Begin seek barrage, count=%u\n", count);

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

225
226
227
228
229
230
231
232
		/* 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);
		}
233
234
235
236
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
237
		else {
Josh Coalson's avatar
Josh Coalson committed
238
#if !defined _MSC_VER && !defined __MINGW32__
Josh Coalson's avatar
Josh Coalson committed
239
			pos = (FLAC__uint64)(random() % n);
Josh Coalson's avatar
Josh Coalson committed
240
241
#else
			/* RAND_MAX is only 32767 in my MSVC */
Josh Coalson's avatar
Josh Coalson committed
242
			pos = (FLAC__uint64)((rand()<<15|rand()) % n);
Josh Coalson's avatar
Josh Coalson committed
243
#endif
244
		}
Josh Coalson's avatar
Josh Coalson committed
245
246
247

		printf("seek(%llu)... ", pos);
		fflush(stdout);
248
		if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
249
			if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
250
				return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
251
252
253
254
			else if(decoder_client_data.total_samples == 0)
				printf("seek failed, assuming it was past EOF... ");
			else
				printf("seek past end failed as expected... ");
255
256
			if(!FLAC__stream_decoder_flush(decoder))
				return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
257
258
259
260
		}
		else {
			printf("decode_frame... ");
			fflush(stdout);
261
262
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
263
264
265

			printf("decode_frame... ");
			fflush(stdout);
266
267
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
268
		}
Josh Coalson's avatar
Josh Coalson committed
269
270
271
272
273

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

274
275
276
	if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
		if(!FLAC__stream_decoder_finish(decoder))
			return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
277
278
	}

Josh Coalson's avatar
Josh Coalson committed
279
280
281
282
283
284
285
286
287
	printf("\nPASSED!\n");

	return true;
}

int main(int argc, char *argv[])
{
	const char *filename;
	unsigned count = 0;
288
	off_t filesize;
Josh Coalson's avatar
Josh Coalson committed
289
290
291

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

Josh Coalson's avatar
Josh Coalson committed
292
	if (argc < 2 || argc > 3) {
Josh Coalson's avatar
Josh Coalson committed
293
294
295
296
297
298
299
300
301
		fprintf(stderr, usage);
		return 1;
	}

	filename = argv[1];

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

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

Josh Coalson's avatar
Josh Coalson committed
305
306
307
308
#if !defined _MSC_VER && !defined __MINGW32__
	{
		struct timeval tv;

309
		if (gettimeofday(&tv, 0) < 0) {
Josh Coalson's avatar
Josh Coalson committed
310
311
312
313
314
315
316
317
318
			fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
			tv.tv_usec = 4321;
		}
		srandom(tv.tv_usec);
	}
#else
	srand(time(0));
#endif

319
320
321
322
323
324
	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
325
326
327
328
	(void) signal(SIGINT, our_sigint_handler_);

	{
		FLAC__bool ok;
329
330
		if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
#ifdef FLAC__HAS_OGG
331
			ok = seek_barrage(/*is_ogg=*/true, filename, filesize, count);
332
333
334
335
336
337
#else
			fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
			ok = false;
#endif
		}
		else {
338
			ok = seek_barrage(/*is_ogg=*/false, filename, filesize, count);
339
		}
Josh Coalson's avatar
Josh Coalson committed
340
341
342
		return ok? 0 : 2;
	}
}