/
libout123.c
1224 lines (1115 loc) · 29.2 KB
/
libout123.c
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
audio: audio output interface
copyright ?-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Michael Hipp
*/
#include "out123_int.h"
#include "wav.h"
#ifndef NOXFERMEM
#include "buffer.h"
static int have_buffer(out123_handle *ao)
{
return (ao->buffer_pid != -1);
}
#endif
#include "stringlists.h"
#include "debug.h"
/* An output that is live and does not deal with pausing itself.
The device needs to be closed if we stop feeding. */
#define SENSITIVE_OUTPUT(ao) \
( (ao)->propflags & OUT123_PROP_LIVE \
&& !((ao)->propflags & OUT123_PROP_PERSISTENT) )
static const char *default_name = "out123";
static int modverbose(out123_handle *ao, int final)
{
debug3("modverbose: %x %x %x"
, (unsigned)ao->flags, (unsigned)ao->auxflags, (unsigned)OUT123_QUIET);
return AOQUIET
? (final ? 0 : -1)
: ao->verbose;
}
static void check_output_module( out123_handle *ao
, const char *name, const char *device, int final );
static void out123_clear_module(out123_handle *ao)
{
ao->open = NULL;
ao->get_formats = NULL;
ao->write = NULL;
ao->flush = NULL;
ao->drain = NULL;
ao->close = NULL;
ao->deinit = NULL;
ao->module = NULL;
ao->userptr = NULL;
ao->fn = -1;
/* The default is live output devices, files are the special case. */
ao->propflags = OUT123_PROP_LIVE;
}
/* Ensure that real name is not leaked, needs to be freed before any call to
ao->open(ao). One might free it on closing already, but it might be sensible
to keep it around, might still be the same after re-opening. */
static int aoopen(out123_handle *ao)
{
if(ao->realname)
{
free(ao->realname);
ao->realname = NULL;
}
return ao->open(ao);
}
out123_handle* attribute_align_arg out123_new(void)
{
out123_handle* ao = malloc( sizeof( out123_handle ) );
if(!ao)
return NULL;
ao->errcode = 0;
#ifndef NOXFERMEM
ao->buffer_pid = -1;
ao->buffer_fd[0] = -1;
ao->buffer_fd[1] = -1;
ao->buffermem = NULL;
#endif
out123_clear_module(ao);
ao->name = compat_strdup(default_name);
ao->realname = NULL;
ao->driver = NULL;
ao->device = NULL;
ao->flags = OUT123_KEEP_PLAYING;
ao->rate = -1;
ao->gain = -1;
ao->channels = -1;
ao->format = -1;
ao->framesize = 0;
ao->state = play_dead;
ao->auxflags = 0;
ao->preload = 0.;
ao->verbose = 0;
ao->device_buffer = 0.;
ao->bindir = NULL;
return ao;
}
void attribute_align_arg out123_del(out123_handle *ao)
{
debug2("[%ld]out123_del(%p)", (long)getpid(), (void*)ao);
if(!ao) return;
out123_close(ao); /* TODO: That talks to the buffer if present. */
out123_set_buffer(ao, 0);
#ifndef NOXFERMEM
if(have_buffer(ao)) buffer_exit(ao);
#endif
if(ao->name)
free(ao->name);
if(ao->bindir)
free(ao->bindir);
free(ao);
}
/* Error reporting */
/* Carefully keep that in sync with the error enum! */
/* Sizing according to contents so that we can check! */
static const char *const errstring[] =
{
"no problem"
, "out of memory"
, "bad driver name"
, "failure loading driver module"
, "no driver loaded"
, "no active audio device"
, "some device playback error"
, "failed to open device"
, "buffer (communication) error"
, "basic module system error"
, "bad function argument(s)"
, "unknown parameter code"
, "attempt to set read-only parameter"
, "invalid out123 handle"
};
const char* attribute_align_arg out123_strerror(out123_handle *ao)
{
return out123_plain_strerror(out123_errcode(ao));
}
int out123_errcode(out123_handle *ao)
{
if(!ao) return OUT123_BAD_HANDLE;
else return ao->errcode;
}
const char* attribute_align_arg out123_plain_strerror(int errcode)
{
if(errcode == OUT123_ERR)
return "some generic error";
if(errcode >= OUT123_ERRCOUNT || errcode < 0)
return "invalid error code";
/* Let's be paranoid, one _may_ forget to extend errstrings when
adding a new entry to the enum. */
if(errcode >= sizeof(errstring)/sizeof(char*))
return "outdated error list (library bug)";
return errstring[errcode];
}
static int out123_seterr(out123_handle *ao, enum out123_error errcode)
{
if(!ao)
return OUT123_ERR;
ao->errcode = errcode;
return errcode == OUT123_OK ? OUT123_OK : OUT123_ERR;
}
/* pre-playback setup */
int attribute_align_arg
out123_set_buffer(out123_handle *ao, size_t buffer_bytes)
{
debug2("out123_set_buffer(%p, %"SIZE_P")", (void*)ao, (size_p)buffer_bytes);
if(!ao)
return OUT123_ERR;
ao->errcode = 0;
/* Close any audio output module if present, also kill of buffer if present,
then start new buffer process with newly allocated storage if given
size is non-zero. */
out123_close(ao);
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_exit(ao);
if(buffer_bytes)
return buffer_init(ao, buffer_bytes);
#endif
return 0;
}
int attribute_align_arg
out123_param( out123_handle *ao, enum out123_parms code
, long value, double fvalue, const char *svalue )
{
int ret = 0;
debug4("out123_param(%p, %i, %li, %g)", (void*)ao, (int)code, value, fvalue);
if(!ao)
return OUT123_ERR;
ao->errcode = 0;
switch(code)
{
case OUT123_FLAGS:
ao->flags = (int)value;
break;
case OUT123_PRELOAD:
ao->preload = fvalue;
break;
case OUT123_GAIN:
ao->gain = value;
break;
case OUT123_VERBOSE:
ao->verbose = (int)value;
break;
case OUT123_DEVICEBUFFER:
ao->device_buffer = fvalue;
break;
case OUT123_PROPFLAGS:
ao->errcode = OUT123_SET_RO_PARAM;
ret = OUT123_ERR;
break;
case OUT123_NAME:
if(ao->name)
free(ao->name);
ao->name = compat_strdup(svalue ? svalue : default_name);
break;
case OUT123_BINDIR:
if(ao->bindir)
free(ao->bindir);
ao->bindir = compat_strdup(svalue);
break;
default:
ao->errcode = OUT123_BAD_PARAM;
if(!AOQUIET) error1("bad parameter code %i", (int)code);
ret = OUT123_ERR;
}
#ifndef NOXFERMEM
/* If there is a buffer, it needs to update its copy of parameters. */
if(have_buffer(ao))
/* No error check; if that fails, buffer is dead and we will notice
soon enough. */
buffer_sync_param(ao);
#endif
return ret;
}
int attribute_align_arg
out123_getparam( out123_handle *ao, enum out123_parms code
, long *ret_value, double *ret_fvalue, char* *ret_svalue )
{
int ret = 0;
long value = 0;
double fvalue = 0.;
char *svalue = NULL;
debug4( "out123_getparam(%p, %i, %p, %p)"
, (void*)ao, (int)code, (void*)ret_value, (void*)ret_fvalue );
if(!ao)
return OUT123_ERR;
ao->errcode = 0;
switch(code)
{
case OUT123_FLAGS:
value = ao->flags;
break;
case OUT123_PRELOAD:
fvalue = ao->preload;
break;
case OUT123_GAIN:
value = ao->gain;
break;
case OUT123_VERBOSE:
value = ao->verbose;
break;
case OUT123_DEVICEBUFFER:
fvalue = ao->device_buffer;
break;
case OUT123_PROPFLAGS:
value = ao->propflags;
break;
case OUT123_NAME:
svalue = ao->realname ? ao->realname : ao->name;
break;
case OUT123_BINDIR:
svalue = ao->bindir;
break;
default:
if(!AOQUIET) error1("bad parameter code %i", (int)code);
ao->errcode = OUT123_BAD_PARAM;
ret = OUT123_ERR;
}
if(!ret)
{
if(ret_value) *ret_value = value;
if(ret_fvalue) *ret_fvalue = fvalue;
if(ret_svalue) *ret_svalue = svalue;
}
return ret;
}
int attribute_align_arg
out123_param_from(out123_handle *ao, out123_handle* from_ao)
{
debug2("out123_param_from(%p, %p)", (void*)ao, (void*)from_ao);
if(!ao || !from_ao) return -1;
ao->flags = from_ao->flags;
ao->preload = from_ao->preload;
ao->gain = from_ao->gain;
ao->device_buffer = from_ao->device_buffer;
ao->verbose = from_ao->verbose;
if(ao->name)
free(ao->name);
ao->name = compat_strdup(from_ao->name);
if(ao->bindir)
free(ao->bindir);
ao->bindir = compat_strdup(from_ao->bindir);
return 0;
}
#ifndef NOXFERMEM
/* Serialization of tunable parameters to communicate them between
main process and buffer. Make sure these two stay in sync ... */
int write_parameters(out123_handle *ao, int who)
{
int fd = ao->buffermem->fd[who];
if(
GOOD_WRITEVAL(fd, ao->flags)
&& GOOD_WRITEVAL(fd, ao->preload)
&& GOOD_WRITEVAL(fd, ao->gain)
&& GOOD_WRITEVAL(fd, ao->device_buffer)
&& GOOD_WRITEVAL(fd, ao->verbose)
&& GOOD_WRITEVAL(fd, ao->propflags)
&& !xfer_write_string(ao, who, ao->name)
&& !xfer_write_string(ao, who, ao->bindir)
)
return 0;
else
return -1;
}
int read_parameters(out123_handle *ao
, int who, byte *prebuf, int *preoff, int presize)
{
int fd = ao->buffermem->fd[who];
#define GOOD_READVAL_BUF(fd, val) \
!read_buf(fd, &val, sizeof(val), prebuf, preoff, presize)
if(
GOOD_READVAL_BUF(fd, ao->flags)
&& GOOD_READVAL_BUF(fd, ao->preload)
&& GOOD_READVAL_BUF(fd, ao->gain)
&& GOOD_READVAL_BUF(fd, ao->device_buffer)
&& GOOD_READVAL_BUF(fd, ao->verbose)
&& GOOD_READVAL_BUF(fd, ao->propflags)
&& !xfer_read_string(ao, who, &ao->name)
&& !xfer_read_string(ao, who, &ao->bindir)
)
return 0;
else
return -1;
#undef GOOD_READVAL_BUF
}
#endif
int attribute_align_arg
out123_open(out123_handle *ao, const char* driver, const char* device)
{
debug4( "[%ld]out123_open(%p, %s, %s)", (long)getpid(), (void*)ao
, driver ? driver : "<nil>", device ? device : "<nil>" );
if(!ao)
return OUT123_ERR;
ao->errcode = 0;
out123_close(ao);
debug("out123_open() continuing");
/* Ensure that audio format is freshly set for "no format yet" mode.
In out123_start*/
ao->rate = -1;
ao->channels = -1;
ao->format = -1;
#ifndef NOXFERMEM
if(have_buffer(ao))
{
if(buffer_open(ao, driver, device))
return OUT123_ERR;
}
else
#endif
{
/* We just quickly check if the device can be accessed at all,
same as out123_encodings! */
char *nextname, *modnames;
const char *names = driver ? driver : DEFAULT_OUTPUT_MODULE;
if(!names) return out123_seterr(ao, OUT123_BAD_DRIVER_NAME);
/* It is ridiculous how these error messages are larger than the pieces
of memory they are about! */
if(device && !(ao->device = compat_strdup(device)))
{
if(!AOQUIET) error("OOM device name copy");
return out123_seterr(ao, OUT123_DOOM);
}
if(!(modnames = compat_strdup(names)))
{
out123_close(ao); /* Frees ao->device, too. */
if(!AOQUIET) error("OOM driver names");
return out123_seterr(ao, OUT123_DOOM);
}
/* Now loop over the list of possible modules to find one that works. */
nextname = strtok(modnames, ",");
while(!ao->open && nextname)
{
char *curname = nextname;
nextname = strtok(NULL, ",");
check_output_module(ao, curname, device, !nextname);
if(ao->open)
{
if(AOVERBOSE(2))
fprintf(stderr, "Chosen output module: %s\n", curname);
/* A bit redundant, but useful when it's a fake module. */
if(!(ao->driver = compat_strdup(curname)))
{
out123_close(ao);
if(!AOQUIET) error("OOM driver name");
return out123_seterr(ao, OUT123_DOOM);
}
}
}
free(modnames);
if(!ao->open) /* At least an open() routine must be present. */
{
if(!AOQUIET)
error2("Found no driver out of [%s] working with device %s."
, names, device ? device : "<default>");
/* Proper more detailed error code could be set already. */
if(ao->errcode == OUT123_OK)
ao->errcode = OUT123_BAD_DRIVER;
return OUT123_ERR;
}
}
/* Got something. */
ao->state = play_stopped;
return OUT123_OK;
}
/* Be resilient, always do cleanup work regardless of state. */
void attribute_align_arg out123_close(out123_handle *ao)
{
debug2("[%ld]out123_close(%p)", (long)getpid(), (void*)ao);
if(!ao)
return;
ao->errcode = 0;
out123_drain(ao);
out123_stop(ao);
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_close(ao);
else
#endif
{
if(ao->deinit)
ao->deinit(ao);
if(ao->module)
close_module(ao->module, modverbose(ao, 0));
/* Null module methods and pointer. */
out123_clear_module(ao);
}
/* These copies exist in addition to the ones for the buffer. */
if(ao->driver)
free(ao->driver);
ao->driver = NULL;
if(ao->device)
free(ao->device);
ao->device = NULL;
if(ao->realname)
free(ao->realname);
ao->realname = NULL;
ao->state = play_dead;
}
int attribute_align_arg
out123_start(out123_handle *ao, long rate, int channels, int encoding)
{
debug5( "[%ld]out123_start(%p, %li, %i, %i)", (long)getpid()
, (void*)ao, rate, channels, encoding );
if(!ao)
return OUT123_ERR;
ao->errcode = 0;
out123_stop(ao);
debug("out123_start() continuing");
if(ao->state != play_stopped)
return out123_seterr(ao, OUT123_NO_DRIVER);
/* Stored right away as parameters for ao->open() and also for reference.
framesize needed for out123_play(). */
ao->rate = rate;
ao->channels = channels;
ao->format = encoding;
ao->framesize = out123_encsize(encoding)*channels;
#ifndef NOXFERMEM
if(have_buffer(ao))
{
if(!buffer_start(ao))
{
ao->state = play_live;
return OUT123_OK;
}
else
return OUT123_ERR;
}
else
#endif
{
if(aoopen(ao) < 0)
return out123_seterr(ao, OUT123_DEV_OPEN);
ao->state = play_live;
return OUT123_OK;
}
}
void attribute_align_arg out123_pause(out123_handle *ao)
{
debug3( "[%ld]out123_pause(%p) %i", (long)getpid()
, (void*)ao, ao ? (int)ao->state : -1 );
if(ao && ao->state == play_live)
{
#ifndef NOXFERMEM
if(have_buffer(ao)){ debug("pause with buffer"); buffer_pause(ao); }
else
#endif
{
debug1("pause without buffer, sensitive=%d", SENSITIVE_OUTPUT(ao));
/* Close live devices to avoid underruns. */
if( SENSITIVE_OUTPUT(ao)
&& ao->close && ao->close(ao) && !AOQUIET )
error("trouble closing device");
}
ao->state = play_paused;
}
}
void attribute_align_arg out123_continue(out123_handle *ao)
{
debug3( "[%ld]out123_continue(%p) %i", (long)getpid()
, (void*)ao, ao ? (int)ao->state : -1 );
if(ao && ao->state == play_paused)
{
#ifndef NOXFERMEM
if(have_buffer(ao)) buffer_continue(ao);
else
#endif
/* Re-open live devices to avoid underruns. */
if(SENSITIVE_OUTPUT(ao) && aoopen(ao) < 0)
{
/* Will be overwritten by following out123_play() ... */
ao->errcode = OUT123_DEV_OPEN;
if(!AOQUIET)
error("failed re-opening of device after pause");
return;
}
ao->state = play_live;
}
}
void attribute_align_arg out123_stop(out123_handle *ao)
{
debug2("[%ld]out123_stop(%p)", (long)getpid(), (void*)ao);
if(!ao)
return;
ao->errcode = 0;
if(!(ao->state == play_paused || ao->state == play_live))
return;
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_stop(ao);
else
#endif
if( ao->state == play_live
|| (ao->state == play_paused && !SENSITIVE_OUTPUT(ao)) )
{
if(ao->close && ao->close(ao) && !AOQUIET)
error("trouble closing device");
}
ao->state = play_stopped;
}
size_t attribute_align_arg
out123_play(out123_handle *ao, void *bytes, size_t count)
{
size_t sum = 0;
int written;
debug5( "[%ld]out123_play(%p, %p, %"SIZE_P") (%i)", (long)getpid()
, (void*)ao, bytes, (size_p)count, ao ? (int)ao->state : -1 );
if(!ao)
return 0;
ao->errcode = 0;
/* If paused, automatically continue. Other states are an error. */
if(ao->state != play_live)
{
if(ao->state == play_paused)
out123_continue(ao);
if(ao->state != play_live)
{
ao->errcode = OUT123_NOT_LIVE;
return 0;
}
}
/* Ensure that we are writing whole PCM frames. */
count -= count % ao->framesize;
if(!count) return 0;
#ifndef NOXFERMEM
if(have_buffer(ao))
return buffer_write(ao, bytes, count);
else
#endif
do /* Playback in a loop to be able to continue after interruptions. */
{
errno = 0;
written = ao->write(ao, (unsigned char*)bytes, (int)count);
debug4( "written: %d errno: %i (%s), keep_on=%d"
, written, errno, strerror(errno)
, ao->flags & OUT123_KEEP_PLAYING );
if(written >= 0){ sum+=written; count -= written; }
else if(errno != EINTR)
{
ao->errcode = OUT123_DEV_PLAY;
if(!AOQUIET)
error1("Error in writing audio (%s?)!", strerror(errno));
/* If written < 0, this is a serious issue ending this playback round. */
break;
}
} while(count && ao->flags & OUT123_KEEP_PLAYING);
debug3( "out123_play(%p, %p, ...) = %"SIZE_P
, (void*)ao, bytes, (size_p)sum );
return sum;
}
/* Drop means to flush it down. Quickly. */
void attribute_align_arg out123_drop(out123_handle *ao)
{
debug2("[%ld]out123_drop(%p)", (long)getpid(), (void*)ao);
if(!ao)
return;
ao->errcode = 0;
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_drop(ao);
else
#endif
if(ao->state == play_live)
{
if(ao->propflags & OUT123_PROP_LIVE && ao->flush)
ao->flush(ao);
}
}
void attribute_align_arg out123_drain(out123_handle *ao)
{
debug2("[%ld]out123_drain(%p) ", (long)getpid(), (void*)ao);
if(!ao)
return;
ao->errcode = 0;
/* If paused, automatically continue. */
if(ao->state != play_live)
{
if(ao->state == play_paused)
out123_continue(ao);
if(ao->state != play_live)
return;
}
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_drain(ao);
else
#endif
{
if(ao->drain)
ao->drain(ao);
out123_pause(ao);
}
}
void attribute_align_arg out123_ndrain(out123_handle *ao, size_t bytes)
{
debug3("[%ld]out123_ndrain(%p, %"SIZE_P")", (long)getpid(), (void*)ao, (size_p)bytes);
if(!ao)
return;
ao->errcode = 0;
/* If paused, automatically continue. */
if(ao->state != play_live)
{
if(ao->state == play_paused)
out123_continue(ao);
if(ao->state != play_live)
return;
}
#ifndef NOXFERMEM
if(have_buffer(ao))
buffer_ndrain(ao, bytes);
else
#endif
{
if(ao->drain)
ao->drain(ao);
out123_pause(ao);
}
}
/* A function that does nothing and returns nothing. */
static void builtin_nothing(out123_handle *ao){}
static int test_open(out123_handle *ao)
{
debug("test_open");
return OUT123_OK;
}
static int test_get_formats(out123_handle *ao)
{
debug("test_get_formats");
return MPG123_ENC_ANY;
}
static int test_write(out123_handle *ao, unsigned char *buf, int len)
{
debug2("test_write: %i B from %p", len, (void*)buf);
return len;
}
static void test_flush(out123_handle *ao)
{
debug("test_flush");
}
static void test_drain(out123_handle *ao)
{
debug("test_drain");
}
static int test_close(out123_handle *ao)
{
debug("test_drain");
return 0;
}
/* Open one of our builtin driver modules. */
static int open_fake_module(out123_handle *ao, const char *driver)
{
if(!strcmp("test", driver))
{
ao->propflags &= ~OUT123_PROP_LIVE;
ao->open = test_open;
ao->get_formats = test_get_formats;
ao->write = test_write;
ao->flush = test_flush;
ao->drain = test_drain;
ao->close = test_close;
}
else
if(!strcmp("raw", driver))
{
ao->propflags &= ~OUT123_PROP_LIVE;
ao->open = raw_open;
ao->get_formats = raw_formats;
ao->write = wav_write;
ao->flush = builtin_nothing;
ao->drain = wav_drain;
ao->close = raw_close;
}
else
if(!strcmp("wav", driver))
{
ao->propflags &= ~OUT123_PROP_LIVE;
ao->open = wav_open;
ao->get_formats = wav_formats;
ao->write = wav_write;
ao->flush = builtin_nothing;
ao->drain = wav_drain;
ao->close = wav_close;
}
else
if(!strcmp("cdr", driver))
{
ao->propflags &= ~OUT123_PROP_LIVE;
ao->open = cdr_open;
ao->get_formats = cdr_formats;
ao->write = wav_write;
ao->flush = builtin_nothing;
ao->drain = wav_drain;
ao->close = raw_close;
}
else
if(!strcmp("au", driver))
{
ao->propflags &= ~OUT123_PROP_LIVE;
ao->open = au_open;
ao->get_formats = au_formats;
ao->write = wav_write;
ao->flush = builtin_nothing;
ao->drain = wav_drain;
ao->close = au_close;
}
else return OUT123_ERR;
return OUT123_OK;
}
/* Check if given output module is loadable and has a working device.
final flag triggers printing and storing of errors. */
static void check_output_module( out123_handle *ao
, const char *name, const char *device, int final )
{
int result;
debug3("check_output_module %p %p %p", (void*)ao, (void*)device, (void*)ao->device);
if(AOVERBOSE(1))
fprintf( stderr, "Trying output module: %s, device: %s\n"
, name, ao->device ? ao->device : "<nil>" );
/* Use internal code. */
if(open_fake_module(ao, name) == OUT123_OK)
return;
/* Open the module, initial check for availability+libraries. */
ao->module = open_module( "output", name, modverbose(ao, final), ao->bindir);
if(!ao->module)
return;
/* Check if module supports output */
if(!ao->module->init_output)
{
if(final)
error1("Module '%s' does not support audio output.", name);
goto check_output_module_cleanup;
}
/* Should I do funny stuff with stderr file descriptor instead? */
if(final)
{
if(AOVERBOSE(2))
fprintf(stderr
, "Note: %s is the last output option... showing you any error messages now.\n"
, name);
}
else ao->auxflags |= OUT123_QUIET; /* Probing, so don't spill stderr with errors. */
result = ao->module->init_output(ao);
if(result == 0)
{ /* Try to open the device. I'm only interested in actually working modules. */
ao->format = -1;
result = aoopen(ao);
debug1("ao->open() = %i", result);
if(result >= 0) /* Opening worked, close again. */
ao->close(ao);
else if(ao->deinit)
ao->deinit(ao); /* Failed, ensure that cleanup after init_output() occurs. */
}
else if(!AOQUIET)
error2("Module '%s' init failed: %i", name, result);
ao->auxflags &= ~OUT123_QUIET;
if(result >= 0)
return;
check_output_module_cleanup:
/* Only if module did not check out we get to clean up here. */
close_module(ao->module, modverbose(ao, final));
out123_clear_module(ao);
return;
}
/*
static void audio_output_dump(out123_handle *ao)
{
fprintf(stderr, "ao->fn=%d\n", ao->fn);
fprintf(stderr, "ao->userptr=%p\n", ao->userptr);
fprintf(stderr, "ao->rate=%ld\n", ao->rate);
fprintf(stderr, "ao->gain=%ld\n", ao->gain);
fprintf(stderr, "ao->device='%s'\n", ao->device);
fprintf(stderr, "ao->channels=%d\n", ao->channels);
fprintf(stderr, "ao->format=%d\n", ao->format);
}
*/
int attribute_align_arg
out123_drivers(out123_handle *ao, char ***names, char ***descr)
{
char **tmpnames;
char **tmpdescr;
int count;
int i;
if(!ao)
return -1;
debug3("out123_drivers(%p, %p, %p)", (void*)ao, (void*)names, (void*)descr);
/* Wrap the call to isolate the lower levels from the user not being
interested in both lists. it's a bit wasteful, but the code looks
ugly enough already down there. */
count = list_modules("output", &tmpnames, &tmpdescr, modverbose(ao, 0), ao->bindir);
debug1("list_modules()=%i", count);
if(count < 0)
{
if(!AOQUIET)
error("Dynamic module search failed.");
count = 0;
}
if(
stringlists_add( &tmpnames, &tmpdescr
, "raw", "raw headerless stream (builtin)", &count )
|| stringlists_add( &tmpnames, &tmpdescr
, "cdr", "compact disc digital audio stream (builtin)", &count )
|| stringlists_add( &tmpnames, &tmpdescr
, "wav", "RIFF WAVE file (builtin)", &count )
|| stringlists_add( &tmpnames, &tmpdescr
, "au", "Sun AU file (builtin)", &count )
|| stringlists_add( &tmpnames, &tmpdescr
, "test", "output into the void (builtin)", &count )
)
if(!AOQUIET)
error("OOM");
/* Return or free gathered lists of names or descriptions. */
if(names)
*names = tmpnames;
else
{
for(i=0; i<count; ++i)
free(tmpnames[i]);
free(tmpnames);
}
if(descr)
*descr = tmpdescr;
else
{
for(i=0; i<count; ++i)
free(tmpdescr[i]);
free(tmpdescr);
}
return count;
}
/* We always have ao->driver and ao->device set, also with buffer.
The latter can be positively NULL, though. */
int attribute_align_arg
out123_driver_info(out123_handle *ao, char **driver, char **device)
{
debug3( "out123_driver_info(%p, %p, %p)"
, (void*)ao, (void*)driver, (void*)device );
if(!ao)
return OUT123_ERR;
if(!ao->driver)
return out123_seterr(ao, OUT123_NO_DRIVER);
if(driver)
*driver = ao->driver;
if(device)
*device = ao->device;
return OUT123_OK;
}
int attribute_align_arg
out123_encodings(out123_handle *ao, long rate, int channels)
{
debug4( "[%ld]out123_encodings(%p, %li, %i)", (long)getpid()
, (void*)ao, rate, channels );
if(!ao)
return OUT123_ERR;
ao->errcode = OUT123_OK;
out123_stop(ao); /* That brings the buffer into waiting state, too. */
if(ao->state != play_stopped)
return out123_seterr(ao, OUT123_NO_DRIVER);