7e3858ca5b3e4d05a7e53bb96291fd8e84ae7ed0
[flac.git] / src / test_seeking / main.c
1 /* test_seeking - Seeking tester for libFLAC and libOggFLAC
2  * Copyright (C) 2004  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 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #if defined _MSC_VER || defined __MINGW32__
24 #include <time.h>
25 #else
26 #include <sys/time.h>
27 #endif
28 #include "FLAC/assert.h"
29 #include "FLAC/file_decoder.h"
30 #include "OggFLAC/file_decoder.h"
31
32 typedef struct {
33         FLAC__uint64 total_samples;
34         FLAC__bool ignore_errors;
35         FLAC__bool error_occurred;
36 } decoder_client_data_struct;
37
38 static FLAC__bool stop_signal_ = false;
39
40 static void our_sigint_handler_(int signal)
41 {
42         (void)signal;
43         printf("(caught SIGINT) ");
44         fflush(stdout);
45         stop_signal_ = true;
46 }
47
48 static FLAC__bool die_(const char *msg)
49 {
50         printf("ERROR: %s\n", msg);
51         return false;
52 }
53
54 static FLAC__bool die_f_(const char *msg, const FLAC__FileDecoder *decoder)
55 {
56         FLAC__FileDecoderState state = FLAC__file_decoder_get_state(decoder);
57
58         if(msg)
59                 printf("FAILED, %s", msg);
60         else
61                 printf("FAILED");
62
63         printf(", state = %u (%s)\n", (unsigned)state, FLAC__FileDecoderStateString[state]);
64         if(state == FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
65                 FLAC__SeekableStreamDecoderState state_ = FLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
66                 printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state, FLAC__SeekableStreamDecoderStateString[state_]);
67                 if(state_ == FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
68                         FLAC__StreamDecoderState state__ = FLAC__file_decoder_get_stream_decoder_state(decoder);
69                         printf("      stream decoder state = %u (%s)\n", (unsigned)state__, FLAC__StreamDecoderStateString[state__]);
70                 }
71         }
72
73         return false;
74 }
75
76 static FLAC__bool die_of_(const char *msg, const OggFLAC__FileDecoder *decoder)
77 {
78         OggFLAC__FileDecoderState state = OggFLAC__file_decoder_get_state(decoder);
79
80         if(msg)
81                 printf("FAILED, %s", msg);
82         else
83                 printf("FAILED");
84
85         printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__SeekableStreamDecoderStateString[state]);
86         if(state == OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
87                 OggFLAC__SeekableStreamDecoderState state_ = OggFLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
88                 printf("      seekable stream decoder state = %u (%s)\n", (unsigned)state_, OggFLAC__SeekableStreamDecoderStateString[state_]);
89                 if(state_ == OggFLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
90                         OggFLAC__StreamDecoderState state__ = OggFLAC__file_decoder_get_stream_decoder_state(decoder);
91                         printf("      stream decoder state = %u (%s)\n", (unsigned)state__, OggFLAC__StreamDecoderStateString[state__]);
92                         if(state__ == OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
93                                 FLAC__StreamDecoderState state___ = OggFLAC__file_decoder_get_FLAC_stream_decoder_state(decoder);
94                                 printf("      FLAC stream decoder state = %u (%s)\n", (unsigned)state___, FLAC__StreamDecoderStateString[state___]);
95                         }
96                 }
97         }
98
99         return false;
100 }
101
102 static FLAC__StreamDecoderWriteStatus file_decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
103 {
104         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
105
106         (void)decoder, (void)buffer;
107
108         if(0 == dcd) {
109                 printf("ERROR: client_data in write callback is NULL\n");
110                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
111         }
112
113         if(dcd->error_occurred)
114                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
115
116         if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)
117                 printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
118         else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
119                 printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
120         else {
121                 FLAC__ASSERT(0);
122                 dcd->error_occurred = true;
123                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
124         }
125         fflush(stdout);
126
127         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
128 }
129
130 static void file_decoder_metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
131 {
132         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
133
134         (void)decoder;
135
136         if(0 == dcd) {
137                 printf("ERROR: client_data in metadata callback is NULL\n");
138                 return;
139         }
140
141         if(dcd->error_occurred)
142                 return;
143
144         if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
145                 dcd->total_samples = metadata->data.stream_info.total_samples;
146 }
147
148 static void file_decoder_error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
149 {
150         decoder_client_data_struct *dcd = (decoder_client_data_struct*)client_data;
151
152         (void)decoder;
153
154         if(0 == dcd) {
155                 printf("ERROR: client_data in error callback is NULL\n");
156                 return;
157         }
158
159         if(!dcd->ignore_errors) {
160                 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
161                 dcd->error_occurred = true;
162         }
163 }
164
165 static FLAC__bool seek_barrage_native_flac(const char *filename, unsigned count)
166 {
167         FLAC__FileDecoder *decoder;
168         decoder_client_data_struct decoder_client_data;
169         unsigned i;
170         long int n;
171
172         decoder_client_data.total_samples = 0;
173         decoder_client_data.ignore_errors = false;
174         decoder_client_data.error_occurred = false;
175
176         printf("\n+++ seek test: FLAC__FileDecoder\n\n");
177
178         decoder = FLAC__file_decoder_new();
179         if(0 == decoder)
180                 return die_("FLAC__file_decoder_new() FAILED, returned NULL\n");
181
182         if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
183                 return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder);
184
185         if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
186                 return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder);
187
188         if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
189                 return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder);
190
191         if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
192                 return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder);
193
194         if(!FLAC__file_decoder_set_filename(decoder, filename))
195                 return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder);
196
197         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
198                 return die_f_("FLAC__file_decoder_init() FAILED", decoder);
199
200         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
201                 return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
202
203         printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
204         if (decoder_client_data.total_samples == 0 || decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
205                 printf("ERROR: must be 0 < total_samples < %u\n", (unsigned)RAND_MAX);
206                 return false;
207         }
208         n = (long int)decoder_client_data.total_samples;
209
210         printf("Begin seek barrage, count=%u\n", count);
211
212         for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
213                 FLAC__uint64 pos;
214
215 #if !defined _MSC_VER && !defined __MINGW32__
216                 pos = (FLAC__uint64)(random() % n);
217 #else
218                 pos = (FLAC__uint64)(rand() % n);
219 #endif
220
221                 printf("seek(%llu)... ", pos);
222                 fflush(stdout);
223                 if(!FLAC__file_decoder_seek_absolute(decoder, pos))
224                         return die_f_("FLAC__file_decoder_seek_absolute() FAILED", decoder);
225
226                 printf("decode_frame... ");
227                 fflush(stdout);
228                 if(!FLAC__file_decoder_process_single(decoder))
229                         return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);
230
231                 printf("decode_frame... ");
232                 fflush(stdout);
233                 if(!FLAC__file_decoder_process_single(decoder))
234                         return die_f_("FLAC__file_decoder_process_single() FAILED", decoder);
235
236                 printf("OK\n");
237                 fflush(stdout);
238         }
239
240         printf("\nPASSED!\n");
241
242         return true;
243 }
244
245 static FLAC__bool seek_barrage_ogg_flac(const char *filename, unsigned count)
246 {
247         OggFLAC__FileDecoder *decoder;
248         decoder_client_data_struct decoder_client_data;
249         unsigned i;
250         long int n;
251
252         decoder_client_data.total_samples = 0;
253         decoder_client_data.ignore_errors = false;
254         decoder_client_data.error_occurred = false;
255
256         printf("\n+++ seek test: OggFLAC__FileDecoder\n\n");
257
258         decoder = OggFLAC__file_decoder_new();
259         if(0 == decoder)
260                 return die_("OggFLAC__file_decoder_new() FAILED, returned NULL\n");
261
262         if(!OggFLAC__file_decoder_set_write_callback(decoder, (OggFLAC__FileDecoderWriteCallback)file_decoder_write_callback_))
263                 return die_of_("OggFLAC__file_decoder_set_write_callback() FAILED", decoder);
264
265         if(!OggFLAC__file_decoder_set_metadata_callback(decoder, (OggFLAC__FileDecoderMetadataCallback)file_decoder_metadata_callback_))
266                 return die_of_("OggFLAC__file_decoder_set_metadata_callback() FAILED", decoder);
267
268         if(!OggFLAC__file_decoder_set_error_callback(decoder, (OggFLAC__FileDecoderErrorCallback)file_decoder_error_callback_))
269                 return die_of_("OggFLAC__file_decoder_set_error_callback() FAILED", decoder);
270
271         if(!OggFLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
272                 return die_of_("OggFLAC__file_decoder_set_client_data() FAILED", decoder);
273
274         if(!OggFLAC__file_decoder_set_filename(decoder, filename))
275                 return die_of_("OggFLAC__file_decoder_set_filename() FAILED", decoder);
276
277         if(OggFLAC__file_decoder_init(decoder) != OggFLAC__FILE_DECODER_OK)
278                 return die_of_("OggFLAC__file_decoder_init() FAILED", decoder);
279
280         if(!OggFLAC__file_decoder_process_until_end_of_metadata(decoder))
281                 return die_of_("OggFLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder);
282
283         printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
284         if (decoder_client_data.total_samples == 0 || decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
285                 printf("ERROR: must be 0 < total_samples < %u\n", (unsigned)RAND_MAX);
286                 return false;
287         }
288         n = (long int)decoder_client_data.total_samples;
289
290         printf("Begin seek barrage, count=%u\n", count);
291
292         for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
293                 FLAC__uint64 pos;
294
295 #if !defined _MSC_VER && !defined __MINGW32__
296                 pos = (FLAC__uint64)(random() % n);
297 #else
298                 pos = (FLAC__uint64)(rand() % n);
299 #endif
300
301                 printf("seek(%llu)... ", pos);
302                 fflush(stdout);
303                 if(!OggFLAC__file_decoder_seek_absolute(decoder, pos))
304                         return die_of_("OggFLAC__file_decoder_seek_absolute() FAILED", decoder);
305
306                 printf("decode_frame... ");
307                 fflush(stdout);
308                 if(!OggFLAC__file_decoder_process_single(decoder))
309                         return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);
310
311                 printf("decode_frame... ");
312                 fflush(stdout);
313                 if(!OggFLAC__file_decoder_process_single(decoder))
314                         return die_of_("OggFLAC__file_decoder_process_single() FAILED", decoder);
315
316                 printf("OK\n");
317                 fflush(stdout);
318         }
319
320         printf("\nPASSED!\n");
321
322         return true;
323 }
324
325 int main(int argc, char *argv[])
326 {
327         const char *filename;
328         unsigned count = 0;
329
330         static const char * const usage = "usage: test_seeking file.flac [#seeks]\n";
331
332         if (argc < 1 || argc > 3) {
333                 fprintf(stderr, usage);
334                 return 1;
335         }
336
337         filename = argv[1];
338
339         if (argc > 2)
340                 count = strtoul(argv[2], 0, 10);
341
342 #if !defined _MSC_VER && !defined __MINGW32__
343         {
344                 struct timeval tv;
345
346                 if(gettimeofday(&tv, 0) < 0) {
347                         fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
348                         tv.tv_usec = 4321;
349                 }
350                 srandom(tv.tv_usec);
351         }
352 #else
353         srand(time(0));
354 #endif
355
356         (void) signal(SIGINT, our_sigint_handler_);
357
358         {
359                 FLAC__bool ok;
360                 if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg"))
361                         ok = seek_barrage_ogg_flac(filename, count);
362                 else
363                         ok = seek_barrage_native_flac(filename, count);
364                 return ok? 0 : 2;
365         }
366 }