[cbeced]: ogg123 / ogg123.c Maximize Restore History

Download this file

ogg123.c    632 lines (487 with data), 16.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 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
 86
 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
112
113
114
115
116
117
118
119
120
121
122
123
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* ogg123.c by Kenneth Arnold <ogg123@arnoldnet.net> */
/* Maintained by Stan Seibert <volsung@xiph.org> */
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
* THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
* PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001 *
* by Kenneth C. Arnold <ogg@arnoldnet.net> AND OTHER CONTRIBUTORS *
* http://www.xiph.org/ *
* *
********************************************************************
last mod: $Id: ogg123.c,v 1.71 2003/11/27 19:38:29 volsung Exp $
********************************************************************/
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include "audio.h"
#include "buffer.h"
#include "callbacks.h"
#include "cfgfile_options.h"
#include "cmdline_options.h"
#include "format.h"
#include "transport.h"
#include "status.h"
#include "playlist.h"
#include "compat.h"
#include "ogg123.h"
#include "i18n.h"
void exit_cleanup ();
void play (char *source_string);
/* take buffer out of the data segment, not the stack */
#define AUDIO_CHUNK_SIZE 4096
unsigned char convbuffer[AUDIO_CHUNK_SIZE];
int convsize = AUDIO_CHUNK_SIZE;
ogg123_options_t options;
stat_format_t *stat_format;
buf_t *audio_buffer;
audio_play_arg_t audio_play_arg;
/* ------------------------- config file options -------------------------- */
/* This macro is used to create some dummy variables to hold default values
for the options. */
#define INIT(type, value) type type##_##value = value
INIT(int, 1);
INIT(int, 0);
file_option_t file_opts[] = {
/* found, name, description, type, ptr, default */
{0, "default_device", N_("default output device"), opt_type_string,
&options.default_device, NULL},
{0, "shuffle", N_("shuffle playlist"), opt_type_bool,
&options.shuffle, &int_0},
{0, NULL, NULL, 0, NULL, NULL}
};
/* Flags set by the signal handler to control the threads */
signal_request_t sig_request = {0, 0, 0, 0, 0};
/* ------------------------------- signal handler ------------------------- */
void signal_handler (int signo)
{
struct timeval tv;
ogg_int64_t now;
switch (signo) {
case SIGINT:
gettimeofday(&tv, 0);
/* Units of milliseconds (need the cast to force 64 arithmetics) */
now = (ogg_int64_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
if ( (now - sig_request.last_ctrl_c) <= options.delay)
sig_request.exit = 1;
else
sig_request.skipfile = 1;
sig_request.cancel = 1;
sig_request.last_ctrl_c = now;
break;
case SIGTERM:
sig_request.exit = 1;
break;
case SIGTSTP:
sig_request.pause = 1;
/* buffer_Pause (Options.outputOpts.buffer);
buffer_WaitForPaused (Options.outputOpts.buffer);
}
if (Options.outputOpts.devicesOpen == 0) {
close_audio_devices (Options.outputOpts.devices);
Options.outputOpts.devicesOpen = 0;
}
*/
/* open_audio_devices();
if (Options.outputOpts.buffer) {
buffer_Unpause (Options.outputOpts.buffer);
}
*/
break;
case SIGCONT:
break; /* Don't need to do anything special to resume */
}
}
/* -------------------------- util functions ---------------------------- */
void options_init (ogg123_options_t *opts)
{
opts->verbosity = 2;
opts->shuffle = 0;
opts->delay = 500;
opts->nth = 1;
opts->ntimes = 1;
opts->seekpos = 0.0;
opts->endpos = -1.0; /* Mark as unset */
opts->buffer_size = 128 * 1024;
opts->prebuffer = 0.0f;
opts->input_buffer_size = 64 * 1024;
opts->input_prebuffer = 50.0f;
opts->default_device = NULL;
opts->status_freq = 10.0;
opts->playlist = NULL;
}
/* This function selects which statistics to display for our
particular configuration. This does not have anything to do with
verbosity, but rather with which stats make sense to display. */
void select_stats (stat_format_t *stats, ogg123_options_t *opts,
data_source_t *source, decoder_t *decoder,
buf_t *audio_buffer)
{
data_source_stats_t *data_source_stats;
if (audio_buffer != NULL) {
/* Turn on output buffer stats */
stats[8].enabled = 1; /* Fill */
stats[9].enabled = 1; /* State */
} else {
stats[8].enabled = 0;
stats[9].enabled = 0;
}
data_source_stats = source->transport->statistics(source);
if (data_source_stats->input_buffer_used) {
/* Turn on input buffer stats */
stats[6].enabled = 1; /* Fill */
stats[7].enabled = 1; /* State */
} else {
stats[6].enabled = 0;
stats[7].enabled = 0;
}
/* Assume we need total time display, and let display_statistics()
determine at what point it should be turned off during playback */
stats[2].enabled = 1; /* Remaining playback time */
stats[3].enabled = 1; /* Total playback time */
}
/* Handles printing statistics depending upon whether or not we have
buffering going on */
void display_statistics (stat_format_t *stat_format,
buf_t *audio_buffer,
data_source_t *source,
decoder_t *decoder)
{
print_statistics_arg_t *pstats_arg;
buffer_stats_t *buffer_stats;
pstats_arg = new_print_statistics_arg(stat_format,
source->transport->statistics(source),
decoder->format->statistics(decoder));
/* Disable/Enable statistics as needed */
if (pstats_arg->decoder_statistics->total_time <
pstats_arg->decoder_statistics->current_time) {
stat_format[2].enabled = 0; /* Remaining playback time */
stat_format[3].enabled = 0; /* Total playback time */
}
if (pstats_arg->data_source_statistics->input_buffer_used) {
stat_format[6].enabled = 1; /* Input buffer fill % */
stat_format[7].enabled = 1; /* Input buffer state */
}
if (audio_buffer) {
/* Place a status update into the buffer */
buffer_append_action_at_end(audio_buffer,
&print_statistics_action,
pstats_arg);
/* And if we are not playing right now, do an immediate
update just the output buffer */
buffer_stats = buffer_statistics(audio_buffer);
if (buffer_stats->paused || buffer_stats->prebuffering) {
pstats_arg = new_print_statistics_arg(stat_format,
NULL,
NULL);
print_statistics_action(audio_buffer, pstats_arg);
}
free(buffer_stats);
} else
print_statistics_action(NULL, pstats_arg);
}
void display_statistics_quick (stat_format_t *stat_format,
buf_t *audio_buffer,
data_source_t *source,
decoder_t *decoder)
{
print_statistics_arg_t *pstats_arg;
pstats_arg = new_print_statistics_arg(stat_format,
source->transport->statistics(source),
decoder->format->statistics(decoder));
if (audio_buffer) {
print_statistics_action(audio_buffer, pstats_arg);
} else
print_statistics_action(NULL, pstats_arg);
}
double current_time (decoder_t *decoder)
{
decoder_stats_t *stats;
double ret;
stats = decoder->format->statistics(decoder);
ret = stats->current_time;
free(stats);
return ret;
}
void print_audio_devices_info(audio_device_t *d)
{
ao_info *info;
while (d != NULL) {
info = ao_driver_info(d->driver_id);
status_message(2, _("\nAudio Device: %s"), info->name);
status_message(3, _("Author: %s"), info->author);
status_message(3, _("Comments: %s"), info->comment);
status_message(2, "");
d = d->next_device;
}
}
/* --------------------------- main code -------------------------------- */
int main(int argc, char **argv)
{
int optind;
char **playlist_array;
int items;
struct stat stat_buf;
int i;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
ao_initialize();
stat_format = stat_format_create();
options_init(&options);
file_options_init(file_opts);
parse_std_configs(file_opts);
options.playlist = playlist_create();
optind = parse_cmdline_options(argc, argv, &options, file_opts);
audio_play_arg.devices = options.devices;
audio_play_arg.stat_format = stat_format;
/* Add remaining arguments to playlist */
for (i = optind; i < argc; i++) {
if (stat(argv[i], &stat_buf) == 0) {
if (S_ISDIR(stat_buf.st_mode)) {
if (playlist_append_directory(options.playlist, argv[i]) == 0)
fprintf(stderr,
_("Warning: Could not read directory %s.\n"), argv[i]);
} else {
playlist_append_file(options.playlist, argv[i]);
}
} else /* If we can't stat it, it might be a non-disk source */
playlist_append_file(options.playlist, argv[i]);
}
/* Do we have anything left to play? */
if (playlist_length(options.playlist) == 0) {
cmdline_usage();
exit(1);
} else {
playlist_array = playlist_to_array(options.playlist, &items);
playlist_destroy(options.playlist);
options.playlist = NULL;
}
/* Don't use status_message until after this point! */
status_set_verbosity(options.verbosity);
print_audio_devices_info(options.devices);
/* Setup buffer */
if (options.buffer_size > 0) {
audio_buffer = buffer_create(options.buffer_size,
options.buffer_size * options.prebuffer / 100,
audio_play_callback, &audio_play_arg,
AUDIO_CHUNK_SIZE);
if (audio_buffer == NULL) {
status_error(_("Error: Could not create audio buffer.\n"));
exit(1);
}
} else
audio_buffer = NULL;
/* Shuffle playlist */
if (options.shuffle) {
int i;
srandom(time(NULL));
for (i = 0; i < items; i++) {
int j = i + random() % (items - i);
char *temp = playlist_array[i];
playlist_array[i] = playlist_array[j];
playlist_array[j] = temp;
}
}
/* Setup signal handlers and callbacks */
ATEXIT (exit_cleanup);
signal (SIGINT, signal_handler);
signal (SIGTSTP, signal_handler);
signal (SIGCONT, signal_handler);
signal (SIGTERM, signal_handler);
/* Play the files/streams */
i = 0;
while (i < items && !sig_request.exit) {
play(playlist_array[i]);
i++;
}
playlist_array_destroy(playlist_array, items);
exit (0);
}
void play (char *source_string)
{
transport_t *transport;
format_t *format;
data_source_t *source;
decoder_t *decoder;
decoder_callbacks_t decoder_callbacks;
void *decoder_callbacks_arg;
/* Preserve between calls so we only open the audio device when we
have to */
static audio_format_t old_audio_fmt = { 0, 0, 0, 0, 0 };
audio_format_t new_audio_fmt;
audio_reopen_arg_t *reopen_arg;
/* Flags and counters galore */
int eof = 0, eos = 0, ret;
int nthc = 0, ntimesc = 0;
int next_status = 0;
static int status_interval = 0;
/* Reset all of the signal flags */
sig_request.cancel = 0;
sig_request.skipfile = 0;
sig_request.exit = 0;
sig_request.pause = 0;
/* Set preferred audio format (used by decoder) */
new_audio_fmt.big_endian = ao_is_big_endian();
new_audio_fmt.signed_sample = 1;
new_audio_fmt.word_size = 2;
/* Select appropriate callbacks */
if (audio_buffer != NULL) {
decoder_callbacks.printf_error = &decoder_buffered_error_callback;
decoder_callbacks.printf_metadata = &decoder_buffered_metadata_callback;
decoder_callbacks_arg = audio_buffer;
} else {
decoder_callbacks.printf_error = &decoder_error_callback;
decoder_callbacks.printf_metadata = &decoder_metadata_callback;
decoder_callbacks_arg = NULL;
}
/* Locate and use transport for this data source */
if ( (transport = select_transport(source_string)) == NULL ) {
status_error(_("No module could be found to read from %s.\n"), source_string);
return;
}
if ( (source = transport->open(source_string, &options)) == NULL ) {
status_error(_("Cannot open %s.\n"), source_string);
return;
}
/* Detect the file format and initialize a decoder */
if ( (format = select_format(source)) == NULL ) {
status_error(_("The file format of %s is not supported.\n"), source_string);
return;
}
if ( (decoder = format->init(source, &options, &new_audio_fmt,
&decoder_callbacks,
decoder_callbacks_arg)) == NULL ) {
/* We may have failed because of user command */
if (!sig_request.cancel)
status_error(_("Error opening %s using the %s module."
" The file may be corrupted.\n"), source_string,
format->name);
return;
}
/* Decide which statistics are valid */
select_stats(stat_format, &options, source, decoder, audio_buffer);
/* Start the audio playback thread before we begin sending data */
if (audio_buffer != NULL) {
/* First reset mutexes and other synchronization variables */
buffer_reset (audio_buffer);
buffer_thread_start (audio_buffer);
}
/* Show which file we are playing */
decoder_callbacks.printf_metadata(decoder_callbacks_arg, 1,
_("Playing: %s"), source_string);
/* Skip over audio */
if (options.seekpos > 0.0) {
if (!format->seek(decoder, options.seekpos, DECODER_SEEK_START)) {
status_error(_("Could not skip %f seconds of audio."), options.seekpos);
if (audio_buffer != NULL)
buffer_thread_kill(audio_buffer);
return;
}
}
/* Main loop: Iterates over all of the logical bitstreams in the file */
while (!eof && !sig_request.exit) {
/* Loop through data within a logical bitstream */
eos = 0;
while (!eos && !sig_request.exit) {
/* Check signals */
if (sig_request.skipfile) {
eof = eos = 1;
break;
}
if (sig_request.pause) {
if (audio_buffer)
buffer_thread_pause (audio_buffer);
kill (getpid(), SIGSTOP); /* We block here until we unpause */
/* Done pausing */
if (audio_buffer)
buffer_thread_unpause (audio_buffer);
sig_request.pause = 0;
}
/* Read another block of audio data */
ret = format->read(decoder, convbuffer, convsize, &eos, &new_audio_fmt);
/* Bail if we need to */
if (ret == 0) {
eof = eos = 1;
break;
} else if (ret < 0) {
status_error(_("Error: Decoding failure.\n"));
break;
}
/* Check to see if the audio format has changed */
if (!audio_format_equal(&new_audio_fmt, &old_audio_fmt)) {
old_audio_fmt = new_audio_fmt;
/* Update our status printing interval */
status_interval = new_audio_fmt.word_size * new_audio_fmt.channels *
new_audio_fmt.rate / options.status_freq;
next_status = 0;
reopen_arg = new_audio_reopen_arg(options.devices, &new_audio_fmt);
if (audio_buffer)
buffer_insert_action_at_end(audio_buffer, &audio_reopen_action,
reopen_arg);
else
audio_reopen_action(NULL, reopen_arg);
}
/* Update statistics display if needed */
if (next_status <= 0) {
display_statistics(stat_format, audio_buffer, source, decoder);
next_status = status_interval;
} else
next_status -= ret;
if (options.endpos > 0.0 && options.endpos <= current_time(decoder)) {
eof = eos = 1;
break;
}
/* Write audio data block to output, skipping or repeating chunks
as needed */
do {
if (nthc-- == 0) {
if (audio_buffer)
buffer_submit_data(audio_buffer, convbuffer, ret);
else
audio_play_callback(convbuffer, ret, eos, &audio_play_arg);
nthc = options.nth - 1;
}
} while (!sig_request.exit && !sig_request.skipfile &&
++ntimesc < options.ntimes);
ntimesc = 0;
} /* End of data loop */
} /* End of logical bitstream loop */
/* Done playing this logical bitstream. Clean up house. */
if (audio_buffer) {
if (!sig_request.exit && !sig_request.skipfile) {
buffer_mark_eos(audio_buffer);
buffer_wait_for_empty(audio_buffer);
}
buffer_thread_kill(audio_buffer);
}
/* Print final stats */
display_statistics_quick(stat_format, audio_buffer, source, decoder);
format->cleanup(decoder);
transport->close(source);
status_reset_output_lock(); /* In case we were killed mid-output */
status_message(1, _("Done."));
if (sig_request.exit)
exit (0);
}
void exit_cleanup ()
{
if (audio_buffer != NULL) {
buffer_destroy (audio_buffer);
audio_buffer = NULL;
}
ao_onexit (options.devices);
}