Debian Codegraph @

src/libout123/libout123.c

raw
1 /*
2 audio: audio output interface
3
4 copyright ?-2024 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
7 */
8
9 #define _XOPEN_SOURCE 600
10 #define _POSIX_C_SOURCE 200112L
11
12 #include "out123_int.h"
13
14 #ifdef HAVE_SYS_TIME_H
15 #include <sys/time.h>
16 #endif
17
18 #ifdef _WIN32
19 // for Sleep()
20 #include <windows.h>
21 #endif
22
23 // unistd.h sets those
24 #ifdef HAVE_CLOCK_GETTIME
25 #if _POSIX_TIMERS > 0
26 #include <time.h>
27 #ifdef _POSIX_MONSLEEP_CLOCKOTONIC_CLOCK
28 #define SLEEP_CLOCK CLOCK_MONOTONIC
29 #else
30 #define SLEEP_CLOCK CLOCK_REALTIME
31 #endif
32 #endif
33 #endif
34
35 #include "../version.h"
36 #include "wav.h"
37 #include "hextxt.h"
38 #ifndef NOXFERMEM
39 #include "buffer.h"
40 static int have_buffer(out123_handle *ao)
41 {
42 return (ao->buffer_pid != -1);
43 }
44 #endif
45 #include "stringlists.h"
46
47 #include "../common/debug.h"
48
49 const char * attribute_align_arg out123_distversion(unsigned int *major, unsigned int *minor, unsigned int *patch)
50 {
51 if(major)
52 *major = MPG123_MAJOR;
53 if(minor)
54 *minor = MPG123_MINOR;
55 if(patch)
56 *patch = MPG123_PATCH;
57 return MPG123_VERSION;
58 }
59
60 unsigned int attribute_align_arg out123_libversion(unsigned int *patch)
61 {
62 if(patch)
63 *patch = OUT123_PATCHLEVEL;
64 return OUT123_API_VERSION;
65 }
66
67 /* An output that is live and does not deal with pausing itself.
68 The device needs to be closed if we stop feeding. */
69 #define SENSITIVE_OUTPUT(ao) \
70 ( (ao)->propflags & OUT123_PROP_LIVE \
71 && !((ao)->propflags & OUT123_PROP_PERSISTENT) )
72
73 static const char *default_name = "out123";
74
75 static int modverbose(out123_handle *ao, int final)
76 {
77 mdebug( "modverbose: %x %x %x %d %d"
78 , (unsigned)ao->flags, (unsigned)ao->auxflags, (unsigned)OUT123_QUIET
79 , final, ao->verbose );
80 return final ? (AOQUIET ? 0 : ao->verbose) : -1;
81 }
82
83 static void check_output_module( out123_handle *ao
84 , const char *name, const char *device, int final );
85
86 static void out123_clear_module(out123_handle *ao)
87 {
88 ao->open = NULL;
89 ao->get_formats = NULL;
90 ao->write = NULL;
91 ao->flush = NULL;
92 ao->drain = NULL;
93 ao->close = NULL;
94 ao->deinit = NULL;
95 ao->enumerate = NULL;
96
97 ao->module = NULL;
98 ao->userptr = NULL;
99 ao->fn = -1;
100 /* The default is live output devices, files are the special case. */
101 ao->propflags = OUT123_PROP_LIVE;
102 }
103
104 /* Ensure that real name is not leaked, needs to be freed before any call to
105 ao->open(ao). One might free it on closing already, but it might be sensible
106 to keep it around, might still be the same after re-opening. */
107 static int aoopen(out123_handle *ao)
108 {
109 if(ao->realname)
110 {
111 free(ao->realname);
112 ao->realname = NULL;
113 }
114 return ao->open(ao);
115 }
116
117 out123_handle* attribute_align_arg out123_new(void)
118 {
119 out123_handle* ao = malloc( sizeof( out123_handle ) );
120 if(!ao)
121 return NULL;
122 ao->errcode = 0;
123 #ifndef NOXFERMEM
124 ao->buffer_pid = -1;
125 ao->buffer_fd[0] = -1;
126 ao->buffer_fd[1] = -1;
127 ao->buffermem = NULL;
128 #endif
129
130 out123_clear_module(ao);
131 ao->name = INT123_compat_strdup(default_name);
132 ao->realname = NULL;
133 ao->driver = NULL;
134 ao->device = NULL;
135
136 ao->flags = OUT123_KEEP_PLAYING;
137 ao->rate = -1;
138 ao->gain = -1;
139 ao->channels = -1;
140 ao->format = -1;
141 ao->framesize = 0;
142 memset(ao->zerosample, 0, 8);
143 ao->state = play_dead;
144 ao->auxflags = 0;
145 ao->preload = 0.;
146 ao->verbose = 0;
147 ao->device_buffer = 0.;
148 ao->bindir = NULL;
149 return ao;
150 }
151
152 void attribute_align_arg out123_del(out123_handle *ao)
153 {
154 debug2("[%ld]out123_del(%p)", (long)getpid(), (void*)ao);
155 if(!ao) return;
156
157 out123_close(ao); /* TODO: That talks to the buffer if present. */
158 out123_set_buffer(ao, 0);
159 #ifndef NOXFERMEM
160 if(have_buffer(ao)) INT123_buffer_exit(ao);
161 #endif
162 if(ao->name)
163 free(ao->name);
164 if(ao->bindir)
165 free(ao->bindir);
166 free(ao);
167 }
168
169 void attribute_align_arg out123_free(void *ptr)
170 {
171 free(ptr);
172 }
173
174 /* Error reporting */
175
176 /* Carefully keep that in sync with the error enum! */
177 /* Sizing according to contents so that we can check! */
178 static const char *const errstring[] =
179 {
180 "no problem"
181 , "out of memory"
182 , "bad driver name"
183 , "failure loading driver module"
184 , "no driver loaded"
185 , "no active audio device"
186 , "some device playback error"
187 , "failed to open device"
188 , "buffer (communication) error"
189 , "basic module system error"
190 , "bad function argument(s)"
191 , "unknown parameter code"
192 , "attempt to set read-only parameter"
193 , "invalid out123 handle"
194 , "operation not supported"
195 , "device enumeration failed"
196 };
197
198 const char* attribute_align_arg out123_strerror(out123_handle *ao)
199 {
200 return out123_plain_strerror(out123_errcode(ao));
201 }
202
203 int out123_errcode(out123_handle *ao)
204 {
205 if(!ao) return OUT123_BAD_HANDLE;
206 else return ao->errcode;
207 }
208
209 const char* attribute_align_arg out123_plain_strerror(int errcode)
210 {
211 if(errcode == OUT123_ERR)
212 return "some generic error";
213 if(errcode >= OUT123_ERRCOUNT || errcode < 0)
214 return "invalid error code";
215
216 /* Let's be paranoid, one _may_ forget to extend errstrings when
217 adding a new entry to the enum. */
218 if(errcode >= sizeof(errstring)/sizeof(char*))
219 return "outdated error list (library bug)";
220
221 return errstring[errcode];
222 }
223
224 static int out123_seterr(out123_handle *ao, enum out123_error errcode)
225 {
226 if(!ao)
227 return OUT123_ERR;
228 ao->errcode = errcode;
229 return errcode == OUT123_OK ? OUT123_OK : OUT123_ERR;
230 }
231
232 /* pre-playback setup */
233
234 int attribute_align_arg
235 out123_set_buffer(out123_handle *ao, size_t buffer_bytes)
236 {
237 debug2("out123_set_buffer(%p, %zu)", (void*)ao, buffer_bytes);
238 if(!ao)
239 return OUT123_ERR;
240 ao->errcode = 0;
241 /* Close any audio output module if present, also kill of buffer if present,
242 then start new buffer process with newly allocated storage if given
243 size is non-zero. */
244 out123_close(ao);
245 #ifndef NOXFERMEM
246 if(have_buffer(ao))
247 INT123_buffer_exit(ao);
248 if(buffer_bytes)
249 return INT123_buffer_init(ao, buffer_bytes);
250 #endif
251 return 0;
252 }
253
254 int attribute_align_arg
255 out123_param2( out123_handle *ao, int code
256 , long value, double fvalue, const char *svalue )
257 {
258 return out123_param(ao, code, value, fvalue, svalue);
259 }
260
261 int attribute_align_arg
262 out123_param( out123_handle *ao, enum out123_parms code
263 , long value, double fvalue, const char *svalue )
264 {
265 int ret = 0;
266
267 debug4("out123_param(%p, %i, %li, %g)", (void*)ao, (int)code, value, fvalue);
268 if(!ao)
269 return OUT123_ERR;
270 ao->errcode = 0;
271
272 switch(code)
273 {
274 case OUT123_FLAGS:
275 ao->flags = (int)value;
276 break;
277 case OUT123_ADD_FLAGS:
278 ao->flags |= (int)value;
279 break;
280 case OUT123_REMOVE_FLAGS:
281 ao->flags &= ~((int)value);
282 break;
283 case OUT123_PRELOAD:
284 ao->preload = fvalue;
285 break;
286 case OUT123_GAIN:
287 ao->gain = value;
288 break;
289 case OUT123_VERBOSE:
290 ao->verbose = (int)value;
291 break;
292 case OUT123_DEVICEBUFFER:
293 ao->device_buffer = fvalue;
294 break;
295 case OUT123_PROPFLAGS:
296 ao->errcode = OUT123_SET_RO_PARAM;
297 ret = OUT123_ERR;
298 break;
299 case OUT123_NAME:
300 if(ao->name)
301 free(ao->name);
302 ao->name = INT123_compat_strdup(svalue ? svalue : default_name);
303 break;
304 case OUT123_BINDIR:
305 if(ao->bindir)
306 free(ao->bindir);
307 ao->bindir = INT123_compat_strdup(svalue);
308 break;
309 default:
310 ao->errcode = OUT123_BAD_PARAM;
311 if(!AOQUIET) error1("bad parameter code %i", (int)code);
312 ret = OUT123_ERR;
313 }
314 #ifndef NOXFERMEM
315 /* If there is a buffer, it needs to update its copy of parameters. */
316 if(have_buffer(ao))
317 /* No error check; if that fails, buffer is dead and we will notice
318 soon enough. */
319 INT123_buffer_sync_param(ao);
320 #endif
321 return ret;
322 }
323
324 int attribute_align_arg
325 out123_getparam2( out123_handle *ao, int code
326 , long *ret_value, double *ret_fvalue, char* *ret_svalue )
327 {
328 return out123_getparam(ao, code, ret_value, ret_fvalue, ret_svalue);
329 }
330
331 int attribute_align_arg
332 out123_getparam( out123_handle *ao, enum out123_parms code
333 , long *ret_value, double *ret_fvalue, char* *ret_svalue )
334 {
335 int ret = 0;
336 long value = 0;
337 double fvalue = 0.;
338 char *svalue = NULL;
339
340 debug4( "out123_getparam(%p, %i, %p, %p)"
341 , (void*)ao, (int)code, (void*)ret_value, (void*)ret_fvalue );
342 if(!ao)
343 return OUT123_ERR;
344 ao->errcode = 0;
345
346 switch(code)
347 {
348 case OUT123_FLAGS:
349 case OUT123_ADD_FLAGS:
350 value = ao->flags;
351 break;
352 case OUT123_PRELOAD:
353 fvalue = ao->preload;
354 break;
355 case OUT123_GAIN:
356 value = ao->gain;
357 break;
358 case OUT123_VERBOSE:
359 value = ao->verbose;
360 break;
361 case OUT123_DEVICEBUFFER:
362 fvalue = ao->device_buffer;
363 break;
364 case OUT123_PROPFLAGS:
365 value = ao->propflags;
366 break;
367 case OUT123_NAME:
368 svalue = ao->realname ? ao->realname : ao->name;
369 break;
370 case OUT123_BINDIR:
371 svalue = ao->bindir;
372 break;
373 default:
374 if(!AOQUIET) error1("bad parameter code %i", (int)code);
375 ao->errcode = OUT123_BAD_PARAM;
376 ret = OUT123_ERR;
377 }
378 if(!ret)
379 {
380 if(ret_value) *ret_value = value;
381 if(ret_fvalue) *ret_fvalue = fvalue;
382 if(ret_svalue) *ret_svalue = svalue;
383 }
384 return ret;
385 }
386
387 int attribute_align_arg
388 out123_param_from(out123_handle *ao, out123_handle* from_ao)
389 {
390 debug2("out123_param_from(%p, %p)", (void*)ao, (void*)from_ao);
391 if(!ao || !from_ao) return -1;
392
393 ao->flags = from_ao->flags;
394 ao->preload = from_ao->preload;
395 ao->gain = from_ao->gain;
396 ao->device_buffer = from_ao->device_buffer;
397 ao->verbose = from_ao->verbose;
398 if(ao->name)
399 free(ao->name);
400 ao->name = INT123_compat_strdup(from_ao->name);
401 if(ao->bindir)
402 free(ao->bindir);
403 ao->bindir = INT123_compat_strdup(from_ao->bindir);
404
405 return 0;
406 }
407
408 int attribute_align_arg
409 out123_open(out123_handle *ao, const char* driver, const char* device)
410 {
411 debug4( "[%ld]out123_open(%p, %s, %s)", (long)getpid(), (void*)ao
412 , driver ? driver : "<nil>", device ? device : "<nil>" );
413 if(!ao)
414 return OUT123_ERR;
415 ao->errcode = 0;
416
417 out123_close(ao);
418 debug("out123_open() continuing");
419
420 /* Ensure that audio format is freshly set for "no format yet" mode.
421 In out123_start*/
422 ao->rate = -1;
423 ao->channels = -1;
424 ao->format = -1;
425
426 #ifndef NOXFERMEM
427 if(have_buffer(ao))
428 {
429 if(INT123_buffer_open(ao, driver, device))
430 return OUT123_ERR;
431 }
432 else
433 #endif
434 {
435 /* We just quickly check if the device can be accessed at all,
436 same as out123_encodings! */
437 char *nextname, *modnames;
438 const char *names = driver ? driver : DEFAULT_OUTPUT_MODULE;
439
440 if(!names) return out123_seterr(ao, OUT123_BAD_DRIVER_NAME);
441
442 /* It is ridiculous how these error messages are larger than the pieces
443 of memory they are about! */
444 if(device && !(ao->device = INT123_compat_strdup(device)))
445 {
446 if(!AOQUIET) error("OOM device name copy");
447 return out123_seterr(ao, OUT123_DOOM);
448 }
449
450 if(!(modnames = INT123_compat_strdup(names)))
451 {
452 out123_close(ao); /* Frees ao->device, too. */
453 if(!AOQUIET) error("OOM driver names");
454 return out123_seterr(ao, OUT123_DOOM);
455 }
456
457 /* Now loop over the list of possible modules to find one that works. */
458 char *toksave = NULL;
459 nextname = INT123_compat_strtok(modnames, ",", &toksave);
460 while(!ao->open && nextname)
461 {
462 char *curname = nextname;
463 nextname = INT123_compat_strtok(NULL, ",", &toksave);
464 check_output_module(ao, curname, device, !nextname);
465 if(ao->open)
466 {
467 if(AOVERBOSE(2))
468 fprintf(stderr, "Chosen output module: %s\n", curname);
469 /* A bit redundant, but useful when it's a fake module. */
470 if(!(ao->driver = INT123_compat_strdup(curname)))
471 {
472 out123_close(ao);
473 if(!AOQUIET) error("OOM driver name");
474 return out123_seterr(ao, OUT123_DOOM);
475 }
476 }
477 }
478
479 free(modnames);
480
481 if(!ao->open) /* At least an open() routine must be present. */
482 {
483 if(!AOQUIET)
484 error2("Found no driver out of [%s] working with device %s."
485 , names, device ? device : "<default>");
486 /* Proper more detailed error code could be set already. */
487 if(ao->errcode == OUT123_OK)
488 ao->errcode = OUT123_BAD_DRIVER;
489 return OUT123_ERR;
490 }
491 }
492 /* Got something. */
493 ao->state = play_stopped;
494 return OUT123_OK;
495 }
496
497 /* Be resilient, always do cleanup work regardless of state. */
498 void attribute_align_arg out123_close(out123_handle *ao)
499 {
500 debug2("[%ld]out123_close(%p)", (long)getpid(), (void*)ao);
501 if(!ao)
502 return;
503 ao->errcode = 0;
504
505 out123_drain(ao);
506 out123_stop(ao);
507
508 #ifndef NOXFERMEM
509 if(have_buffer(ao))
510 INT123_buffer_close(ao);
511 else
512 #endif
513 {
514 if(ao->deinit)
515 ao->deinit(ao);
516 if(ao->module)
517 INT123_close_module(ao->module, modverbose(ao, 0));
518 /* Null module methods and pointer. */
519 out123_clear_module(ao);
520 }
521
522 /* These copies exist in addition to the ones for the buffer. */
523 if(ao->driver)
524 free(ao->driver);
525 ao->driver = NULL;
526 if(ao->device)
527 free(ao->device);
528 ao->device = NULL;
529 if(ao->realname)
530 free(ao->realname);
531 ao->realname = NULL;
532
533 ao->state = play_dead;
534 }
535
536 int attribute_align_arg
537 out123_start(out123_handle *ao, long rate, int channels, int encoding)
538 {
539 debug5( "[%ld]out123_start(%p, %li, %i, %i)", (long)getpid()
540 , (void*)ao, rate, channels, encoding );
541 if(!ao)
542 return OUT123_ERR;
543 ao->errcode = 0;
544
545 out123_stop(ao);
546 debug("out123_start() continuing");
547 if(ao->state != play_stopped)
548 return out123_seterr(ao, OUT123_NO_DRIVER);
549
550 /* Stored right away as parameters for ao->open() and also for reference.
551 framesize needed for out123_play(). */
552 ao->rate = rate;
553 ao->channels = channels;
554 ao->format = encoding;
555 int samplesize = out123_encsize(encoding);
556 ao->framesize = samplesize*channels;
557 // The most convoluted way to say nothing at all.
558 for(int i=0; i<samplesize; ++i)
559 #ifdef WORDS_BIGENDIAN
560 ao->zerosample[samplesize-1-i] =
561 #else
562 ao->zerosample[i] =
563 #endif
564 MPG123_ZEROSAMPLE(ao->format, samplesize, i);
565
566 #ifndef NOXFERMEM
567 if(have_buffer(ao))
568 {
569 if(!INT123_buffer_start(ao))
570 {
571 ao->state = play_live;
572 return OUT123_OK;
573 }
574 else
575 return OUT123_ERR;
576 }
577 else
578 #endif
579 {
580 if(aoopen(ao) < 0)
581 return out123_seterr(ao, OUT123_DEV_OPEN);
582 ao->state = play_live;
583 return OUT123_OK;
584 }
585 }
586
587 void attribute_align_arg out123_pause(out123_handle *ao)
588 {
589 debug3( "[%ld]out123_pause(%p) %i", (long)getpid()
590 , (void*)ao, ao ? (int)ao->state : -1 );
591 if(ao && ao->state == play_live)
592 {
593 #ifndef NOXFERMEM
594 if(have_buffer(ao)){ debug("pause with buffer"); INT123_buffer_pause(ao); }
595 else
596 #endif
597 {
598 debug1("pause without buffer, sensitive=%d", SENSITIVE_OUTPUT(ao));
599 /* Close live devices to avoid underruns. */
600 if( SENSITIVE_OUTPUT(ao)
601 && ao->close && ao->close(ao) && !AOQUIET )
602 error("trouble closing device");
603 }
604 ao->state = play_paused;
605 }
606 }
607
608 void attribute_align_arg out123_continue(out123_handle *ao)
609 {
610 debug3( "[%ld]out123_continue(%p) %i", (long)getpid()
611 , (void*)ao, ao ? (int)ao->state : -1 );
612 if(ao && ao->state == play_paused)
613 {
614 #ifndef NOXFERMEM
615 if(have_buffer(ao)) INT123_buffer_continue(ao);
616 else
617 #endif
618 /* Re-open live devices to avoid underruns. */
619 if(SENSITIVE_OUTPUT(ao) && aoopen(ao) < 0)
620 {
621 /* Will be overwritten by following out123_play() ... */
622 ao->errcode = OUT123_DEV_OPEN;
623 if(!AOQUIET)
624 error("failed re-opening of device after pause");
625 return;
626 }
627 ao->state = play_live;
628 }
629 }
630
631 void attribute_align_arg out123_stop(out123_handle *ao)
632 {
633 debug2("[%ld]out123_stop(%p)", (long)getpid(), (void*)ao);
634 if(!ao)
635 return;
636 ao->errcode = 0;
637 if(!(ao->state == play_paused || ao->state == play_live))
638 return;
639 #ifndef NOXFERMEM
640 if(have_buffer(ao))
641 INT123_buffer_stop(ao);
642 else
643 #endif
644 if( ao->state == play_live
645 || (ao->state == play_paused && !SENSITIVE_OUTPUT(ao)) )
646 {
647 if(ao->close && ao->close(ao) && !AOQUIET)
648 error("trouble closing device");
649 }
650 ao->state = play_stopped;
651 }
652
653 // Replace the data in a given block of audio data with zeroes
654 // in the correct encoding.
655 static void mute_block( unsigned char *bytes, int count
656 , unsigned char* zerosample, int samplesize )
657 {
658 // The count is expected to be a multiple of samplesize,
659 // this is just to ensure that the loop ends properly, should be noop.
660 count -= count % samplesize;
661 if(!count)
662 return;
663 // Initialize with one zero sample, then multiply that
664 // to eventually cover the whole buffer.
665 memcpy(bytes, zerosample, samplesize);
666 int offset = samplesize;
667 count -= samplesize;
668 while(count)
669 {
670 int block = offset > count ? count : offset;
671 memcpy(bytes+offset, bytes, block);
672 offset += block;
673 count -= block;
674 }
675 }
676
677 size_t attribute_align_arg
678 out123_play(out123_handle *ao, void *bytes, size_t count)
679 {
680 size_t sum = 0;
681 int written;
682
683 debug5( "[%ld]out123_play(%p, %p, %zu) (%i)", (long)getpid()
684 , (void*)ao, bytes, count, ao ? (int)ao->state : -1 );
685 if(!ao)
686 return 0;
687 ao->errcode = 0;
688 /* If paused, automatically continue. Other states are an error. */
689 if(ao->state != play_live)
690 {
691 if(ao->state == play_paused)
692 out123_continue(ao);
693 if(ao->state != play_live)
694 {
695 ao->errcode = OUT123_NOT_LIVE;
696 return 0;
697 }
698 }
699
700 /* Ensure that we are writing whole PCM frames. */
701 count -= count % ao->framesize;
702 if(!count) return 0;
703
704 #ifndef NOXFERMEM
705 if(have_buffer(ao))
706 return INT123_buffer_write(ao, bytes, count);
707 else
708 #endif
709 {
710 // Write 16K in a piece as maximum, as I've seen random short
711 // writes of big blocks with ALSA.
712 int maxcount = 1<<14;
713 maxcount -= maxcount % ao->framesize;
714 if(maxcount < 1)
715 maxcount = ao->framesize;
716 if(ao->flags & OUT123_MUTE)
717 mute_block( bytes, count, ao->zerosample
718 , MPG123_SAMPLESIZE(ao->format) );
719 do /* Playback in a loop to be able to continue after interruptions. */
720 {
721 errno = 0;
722 int block = count > (size_t)maxcount ? maxcount : (int)count;
723 written = ao->write(ao, bytes, block);
724 debug4( "written: %d errno: %i (%s), keep_on=%d"
725 , written, errno, INT123_strerror(errno)
726 , ao->flags & OUT123_KEEP_PLAYING );
727 if(written > 0)
728 {
729 if(written > block)
730 written = block; // Safeguard against sloppy output modules.
731 bytes = (char*)bytes+written;
732 sum += written;
733 count -= written;
734 }
735 if(written < block && errno != EINTR
736 && errno != EAGAIN
737 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
738 // Not all platforms define it (or only in more modern POSIX modes).
739 // Standard says it is supposed to be a macro, so simple check here.
740 && errno != EWOULDBLOCK
741 #endif
742 ){
743 ao->errcode = OUT123_DEV_PLAY;
744 if(!AOQUIET)
745 merror( "Error in writing audio, wrote only %d of %d (%s?)!"
746 , written, block, INT123_strerror(errno) );
747 /* This is a serious issue ending this playback round. */
748 break;
749 }
750 } while(count && ao->flags & OUT123_KEEP_PLAYING);
751 }
752 debug3( "out123_play(%p, %p, ...) = %zu"
753 , (void*)ao, bytes, sum );
754 return sum;
755 }
756
757 /* Drop means to flush it down. Quickly. */
758 void attribute_align_arg out123_drop(out123_handle *ao)
759 {
760 debug2("[%ld]out123_drop(%p)", (long)getpid(), (void*)ao);
761 if(!ao)
762 return;
763 ao->errcode = 0;
764 #ifndef NOXFERMEM
765 if(have_buffer(ao))
766 INT123_buffer_drop(ao);
767 else
768 #endif
769 if(ao->state == play_live)
770 {
771 if(ao->propflags & OUT123_PROP_LIVE && ao->flush)
772 ao->flush(ao);
773 }
774 }
775
776 void attribute_align_arg out123_drain(out123_handle *ao)
777 {
778 debug2("[%ld]out123_drain(%p) ", (long)getpid(), (void*)ao);
779 if(!ao)
780 return;
781 ao->errcode = 0;
782 /* If paused, automatically continue. */
783 if(ao->state != play_live)
784 {
785 if(ao->state == play_paused)
786 out123_continue(ao);
787 if(ao->state != play_live)
788 return;
789 }
790 #ifndef NOXFERMEM
791 if(have_buffer(ao))
792 INT123_buffer_drain(ao);
793 else
794 #endif
795 {
796 if(ao->drain)
797 ao->drain(ao);
798 out123_pause(ao);
799 }
800 }
801
802 void attribute_align_arg out123_ndrain(out123_handle *ao, size_t bytes)
803 {
804 debug3("[%ld]out123_ndrain(%p, %zu)", (long)getpid(), (void*)ao, bytes);
805 if(!ao)
806 return;
807 ao->errcode = 0;
808 /* If paused, automatically continue. */
809 if(ao->state != play_live)
810 {
811 if(ao->state == play_paused)
812 out123_continue(ao);
813 if(ao->state != play_live)
814 return;
815 }
816 #ifndef NOXFERMEM
817 if(have_buffer(ao))
818 INT123_buffer_ndrain(ao, bytes);
819 else
820 #endif
821 {
822 if(ao->drain)
823 ao->drain(ao);
824 out123_pause(ao);
825 }
826 }
827
828
829 /* A function that does nothing and returns nothing. */
830 static void builtin_nothing(out123_handle *ao){}
831 static int test_open(out123_handle *ao)
832 {
833 debug("test_open");
834 return OUT123_OK;
835 }
836 static int test_get_formats(out123_handle *ao)
837 {
838 debug("test_get_formats");
839 return MPG123_ENC_ANY;
840 }
841 static int test_write(out123_handle *ao, unsigned char *buf, int len)
842 {
843 debug2("test_write: %i B from %p", len, (void*)buf);
844 return len;
845 }
846 static void test_flush(out123_handle *ao)
847 {
848 debug("test_flush");
849 }
850 static void test_drain(out123_handle *ao)
851 {
852 debug("test_drain");
853 }
854 static int test_close(out123_handle *ao)
855 {
856 debug("test_drain");
857 return 0;
858 }
859
860 #ifdef SLEEP_CLOCK
861 // Sleep output sleeps for the proper time, but
862 // in a way that still allows overlap with actual
863 // computation, keeping this amount of milliseconds
864 // in a pretend buffer and returning. This has to be less
865 // than 1000(less than a second).
866 static long sleep_buf = 500;
867 // Otherwise, coarse sleep()ing is used.
868 static const long billion = 1000000000;
869
870 // zero-saturating subtraction of time, paranoid about time_t being signed
871 static void ts_sub_zero(struct timespec *a, const struct timespec *b)
872 {
873 long long nsec = a->tv_nsec - b->tv_nsec;
874 long long sec = a->tv_sec - b->tv_sec;
875 sec += nsec/billion;
876 nsec = nsec%billion;
877 if(nsec < 0)
878 {
879 sec -= 1;
880 nsec = billion+nsec;
881 }
882 if(sec < 0)
883 {
884 sec = 0;
885 nsec = 0;
886 }
887 a->tv_sec = (time_t)sec;
888 a->tv_nsec = (long)nsec;
889 }
890
891 static void ts_update(struct timespec *mytime)
892 {
893 struct timespec now, passed;
894 if(clock_gettime(SLEEP_CLOCK, &now))
895 {
896 mytime[1].tv_sec = 0;
897 mytime[1].tv_nsec = 0;
898 return;
899 }
900 passed = now;
901 ts_sub_zero(&passed, mytime);
902 mytime[0] = now;
903 ts_sub_zero(mytime+1, &passed);
904 }
905 #else
906 // Sleep coarslely. Proper operation only with clock.
907 static void sleep_seconds(unsigned long s)
908 {
909 #ifdef _WIN32
910 Sleep(s*1000);
911 #else
912 sleep(s);
913 #endif
914 }
915 #endif
916
917
918 static int sleep_open(out123_handle *ao)
919 {
920 if(!ao)
921 return OUT123_ERR;
922 if(ao->format < 0)
923 {
924 ao->rate = 44100;
925 ao->channels = 2;
926 ao->format = MPG123_ENC_SIGNED_16;
927 return 0;
928 }
929 if(ao->rate < 1)
930 return OUT123_ERR;
931 #ifdef SLEEP_CLOCK
932 // Two time counters:
933 // 0: last time we played something
934 // 1: remaining time in buffer
935 struct timespec *mytime = malloc(2*sizeof(struct timespec));
936 ao->userptr = mytime;
937 if(mytime)
938 {
939 mytime[0].tv_sec = 0;
940 mytime[0].tv_nsec = 0;
941 mytime[1].tv_sec = 0;
942 mytime[1].tv_nsec = 0;
943 // Check once if clock_gettime() actually works.
944 if(clock_gettime(SLEEP_CLOCK, mytime))
945 {
946 free(mytime);
947 ao->userptr = NULL;
948 }
949 }
950 #else
951 unsigned long *buffer_ms = malloc(sizeof(unsigned long));
952 ao->userptr = buffer_ms;
953 if(buffer_ms)
954 *buffer_ms = 0;
955 #endif
956 return (ao->userptr ? OUT123_OK : OUT123_ERR);
957 }
958
959 static int sleep_close(out123_handle *ao)
960 {
961 if(!ao)
962 return -1;
963 if(ao->userptr)
964 free(ao->userptr);
965 ao->userptr = NULL;
966 return 0;
967 }
968
969 static int sleep_write(out123_handle *ao, unsigned char *buf, int len)
970 {
971 if(!ao)
972 return -1;
973 double duration = (double)len/((double)ao->framesize*ao->rate);
974 #ifdef SLEEP_CLOCK
975 struct timespec *mytime = ao->userptr;
976 mytime[1].tv_sec += (time_t)duration;
977 mytime[1].tv_nsec += (long)((duration-(time_t)duration)*billion);
978 ts_update(mytime);
979 while(mytime[1].tv_sec > 0 || mytime[1].tv_nsec > sleep_buf*1000000)
980 {
981 useconds_t sleep_ms;
982 if(mytime[1].tv_sec > 0)
983 sleep_ms = 1000 - sleep_buf;
984 else
985 sleep_ms = mytime[1].tv_nsec/1000000 - sleep_buf;
986 usleep(sleep_ms);
987 ts_update(mytime);
988 }
989 #else
990 // Just sleep off the whole seconds;
991 unsigned long *ms = ao->userptr;
992 *ms += (unsigned long)(duration*1000);
993 sleep_seconds(*ms/1000);
994 *ms %= 1000;
995 #endif
996 return len;
997 }
998
999 static void sleep_drain(out123_handle *ao)
1000 {
1001 if(!ao || !ao->userptr)
1002 return;
1003 #ifdef SLEEP_CLOCK
1004 struct timespec *mytime = ao->userptr;
1005 ts_update(mytime);
1006 while(mytime[1].tv_sec || mytime[1].tv_nsec)
1007 {
1008 usleep(mytime[1].tv_sec ? 1000000 : mytime[1].tv_nsec/1000000);
1009 ts_update(mytime);
1010 }
1011 #else
1012 unsigned long *ms = ao->userptr;
1013 sleep_seconds(*ms/1000);
1014 if(*ms%1000 > 500)
1015 sleep_seconds(1);
1016 *ms = 0;
1017 #endif
1018 }
1019
1020 /* Open one of our builtin driver modules. */
1021 static int open_fake_module(out123_handle *ao, const char *driver)
1022 {
1023 if(!strcmp("test", driver))
1024 {
1025 ao->propflags &= ~OUT123_PROP_LIVE;
1026 ao->open = test_open;
1027 ao->get_formats = test_get_formats;
1028 ao->write = test_write;
1029 ao->flush = test_flush;
1030 ao->drain = test_drain;
1031 ao->close = test_close;
1032 }
1033 else
1034 if(!strcmp("sleep", driver))
1035 {
1036 ao->propflags |= OUT123_PROP_LIVE|OUT123_PROP_PERSISTENT;
1037 ao->open = sleep_open;
1038 ao->close = sleep_close;
1039 ao->get_formats = test_get_formats;
1040 ao->write = sleep_write;
1041 ao->flush = builtin_nothing;
1042 ao->drain = sleep_drain;
1043 ao->close = sleep_close;
1044 }
1045 else
1046 if(!strcmp("raw", driver))
1047 {
1048 ao->propflags &= ~OUT123_PROP_LIVE;
1049 ao->open = INT123_raw_open;
1050 ao->get_formats = INT123_raw_formats;
1051 ao->write = INT123_wav_write;
1052 ao->flush = builtin_nothing;
1053 ao->drain = INT123_wav_drain;
1054 ao->close = INT123_raw_close;
1055 }
1056 else
1057 if(!strcmp("wav", driver))
1058 {
1059 ao->propflags &= ~OUT123_PROP_LIVE;
1060 ao->open = INT123_wav_open;
1061 ao->get_formats = INT123_wav_formats;
1062 ao->write = INT123_wav_write;
1063 ao->flush = builtin_nothing;
1064 ao->drain = INT123_wav_drain;
1065 ao->close = INT123_wav_close;
1066 }
1067 else
1068 if(!strcmp("cdr", driver))
1069 {
1070 ao->propflags &= ~OUT123_PROP_LIVE;
1071 ao->open = INT123_cdr_open;
1072 ao->get_formats = INT123_cdr_formats;
1073 ao->write = INT123_wav_write;
1074 ao->flush = builtin_nothing;
1075 ao->drain = INT123_wav_drain;
1076 ao->close = INT123_raw_close;
1077 }
1078 else
1079 if(!strcmp("au", driver))
1080 {
1081 ao->propflags &= ~OUT123_PROP_LIVE;
1082 ao->open = INT123_au_open;
1083 ao->get_formats = INT123_au_formats;
1084 ao->write = INT123_wav_write;
1085 ao->flush = builtin_nothing;
1086 ao->drain = INT123_wav_drain;
1087 ao->close = INT123_au_close;
1088 }
1089 else
1090 if(!strcmp("hex", driver))
1091 {
1092 ao->propflags &= ~OUT123_PROP_LIVE;
1093 ao->open = hex_open;
1094 ao->get_formats = hex_formats;
1095 ao->write = hex_write;
1096 ao->flush = builtin_nothing;
1097 ao->drain = hextxt_drain;
1098 ao->close = hextxt_close;
1099 }
1100 else
1101 if(!strcmp("txt", driver))
1102 {
1103 ao->propflags &= ~OUT123_PROP_LIVE;
1104 ao->open = txt_open;
1105 ao->get_formats = txt_formats;
1106 ao->write = txt_write;
1107 ao->flush = builtin_nothing;
1108 ao->drain = hextxt_drain;
1109 ao->close = hextxt_close;
1110 }
1111 else return OUT123_ERR;
1112
1113 return OUT123_OK;
1114 }
1115
1116 /* Check if given output module is loadable and has a working device.
1117 final flag triggers printing and storing of errors. */
1118 static void check_output_module( out123_handle *ao
1119 , const char *name, const char *device, int final )
1120 {
1121 int result;
1122
1123 debug3("check_output_module %p %p %p", (void*)ao, (void*)device, (void*)ao->device);
1124 if(AOVERBOSE(1))
1125 fprintf( stderr, "Trying output module: %s, device: %s\n"
1126 , name, ao->device ? ao->device : "<nil>" );
1127
1128 /* Use internal code. */
1129 if(open_fake_module(ao, name) == OUT123_OK)
1130 return;
1131
1132 /* Open the module, initial check for availability+libraries. */
1133 ao->module = INT123_open_module( "output", name, modverbose(ao, final), ao->bindir);
1134 if(!ao->module)
1135 return;
1136 /* Check if module supports output */
1137 if(!ao->module->init_output)
1138 {
1139 if(final && !AOQUIET)
1140 error1("Module '%s' does not support audio output.", name);
1141 goto check_output_module_cleanup;
1142 }
1143
1144 /* Should I do funny stuff with stderr file descriptor instead? */
1145 if(final)
1146 {
1147 if(AOVERBOSE(2))
1148 fprintf(stderr
1149 , "Note: %s is the last output option... showing you any error messages now.\n"
1150 , name);
1151 }
1152 else ao->auxflags |= OUT123_QUIET; /* Probing, so don't spill stderr with errors. */
1153 result = ao->module->init_output(ao);
1154 if(result == 0)
1155 { /* Try to open the device. I'm only interested in actually working modules. */
1156 ao->format = -1;
1157 result = aoopen(ao);
1158 debug1("ao->open() = %i", result);
1159 if(result >= 0) /* Opening worked, close again. */
1160 ao->close(ao);
1161 else
1162 {
1163 if(!AOQUIET)
1164 merror("Module '%s' device open failed.", name);
1165 if(ao->deinit)
1166 ao->deinit(ao); /* Failed, ensure that cleanup after init_output() occurs. */
1167 }
1168 }
1169 else if(!AOQUIET)
1170 error2("Module '%s' init failed: %i", name, result);
1171
1172 ao->auxflags &= ~OUT123_QUIET;
1173
1174 if(result >= 0)
1175 return;
1176
1177 check_output_module_cleanup:
1178 /* Only if module did not check out we get to clean up here. */
1179 INT123_close_module(ao->module, modverbose(ao, final));
1180 out123_clear_module(ao);
1181 return;
1182 }
1183
1184 /*
1185 static void audio_output_dump(out123_handle *ao)
1186 {
1187 fprintf(stderr, "ao->fn=%d\n", ao->fn);
1188 fprintf(stderr, "ao->userptr=%p\n", ao->userptr);
1189 fprintf(stderr, "ao->rate=%ld\n", ao->rate);
1190 fprintf(stderr, "ao->gain=%ld\n", ao->gain);
1191 fprintf(stderr, "ao->device='%s'\n", ao->device);
1192 fprintf(stderr, "ao->channels=%d\n", ao->channels);
1193 fprintf(stderr, "ao->format=%d\n", ao->format);
1194 }
1195 */
1196
1197 int attribute_align_arg
1198 out123_drivers(out123_handle *ao, char ***names, char ***descr)
1199 {
1200 char **tmpnames;
1201 char **tmpdescr;
1202 int count;
1203
1204 if(!ao)
1205 return -1;
1206
1207 debug3("out123_drivers(%p, %p, %p)", (void*)ao, (void*)names, (void*)descr);
1208 /* Wrap the call to isolate the lower levels from the user not being
1209 interested in both lists. it's a bit wasteful, but the code looks
1210 ugly enough already down there. */
1211 count = INT123_list_modules("output", &tmpnames, &tmpdescr, modverbose(ao, 0), ao->bindir);
1212 debug1("INT123_list_modules()=%i", count);
1213 if(count < 0)
1214 {
1215 if(!AOQUIET)
1216 error("Dynamic module search failed.");
1217 count = 0;
1218 }
1219
1220 if(
1221 INT123_stringlists_add( &tmpnames, &tmpdescr
1222 , "raw", "raw headerless stream (builtin)", &count )
1223 || INT123_stringlists_add( &tmpnames, &tmpdescr
1224 , "cdr", "compact disc digital audio stream (builtin)", &count )
1225 || INT123_stringlists_add( &tmpnames, &tmpdescr
1226 , "wav", "RIFF WAVE file (builtin)", &count )
1227 || INT123_stringlists_add( &tmpnames, &tmpdescr
1228 , "au", "Sun AU file (builtin)", &count )
1229 || INT123_stringlists_add( &tmpnames, &tmpdescr
1230 , "test", "output into the void (builtin)", &count )
1231 || INT123_stringlists_add( &tmpnames, &tmpdescr
1232 , "sleep", "output into the void that takes its time (builtin)", &count )
1233 || INT123_stringlists_add( &tmpnames, &tmpdescr
1234 , "hex", "interleaved hex printout (builtin)", &count )
1235 || INT123_stringlists_add( &tmpnames, &tmpdescr
1236 , "txt", "plain text printout, a column per channel (builtin)", &count )
1237 )
1238 if(!AOQUIET)
1239 error("OOM");
1240
1241 /* Return or free gathered lists of names or descriptions. */
1242 if(names)
1243 {
1244 *names = tmpnames;
1245 tmpnames = NULL;
1246 }
1247 if(descr)
1248 {
1249 *descr = tmpdescr;
1250 tmpdescr = NULL;
1251 }
1252 out123_stringlists_free(tmpnames, tmpdescr, count);
1253 return count;
1254 }
1255
1256 struct devlist
1257 {
1258 int count;
1259 char **names;
1260 char **descr;
1261 };
1262
1263 static int devlist_add(void *dll, const char *name, const char *descr)
1264 {
1265 struct devlist *dl = (struct devlist*)dll;
1266 return dl
1267 ? INT123_stringlists_add(&(dl->names), &(dl->descr), name, descr, &(dl->count))
1268 : -1;
1269 }
1270
1271 int out123_devices( out123_handle *ao, const char *driver, char ***names, char ***descr
1272 , char **active_driver )
1273 {
1274 int ret = 0;
1275 struct devlist dl = {0, NULL, NULL};
1276 char *realdrv = NULL;
1277 debug("");
1278 if(!ao)
1279 return -1;
1280 #ifndef NOXFERMEM
1281 if(have_buffer(ao))
1282 return out123_seterr(ao, OUT123_NOT_SUPPORTED);
1283 #endif
1284
1285 ao->errcode = OUT123_OK;
1286 // If the driver is a single word, not a list with commas.
1287 // Then don't try to open drivers just to know which we are talking about.
1288 if(driver && strchr(driver, ',') == NULL)
1289 realdrv = INT123_compat_strdup(driver);
1290 else
1291 {
1292 mdebug("need to find a driver from: %s", driver ? driver : DEFAULT_OUTPUT_MODULE);
1293 if(out123_open(ao, driver, NULL) != OUT123_OK)
1294 return out123_seterr(ao, OUT123_BAD_DRIVER);
1295 mdebug("deduced driver: %s", ao->driver);
1296 realdrv = INT123_compat_strdup(ao->driver);
1297 }
1298 if(realdrv == NULL)
1299 return out123_seterr(ao, OUT123_DOOM);
1300
1301 out123_close(ao);
1302
1303 if(open_fake_module(ao, realdrv) != OUT123_OK)
1304 {
1305 ao->module = INT123_open_module( "output", realdrv
1306 , modverbose(ao, 0), ao->bindir );
1307 /* Open the module, initial check for availability+libraries. */
1308 if( !ao->module || !ao->module->init_output
1309 || ao->module->init_output(ao) )
1310 ret = out123_seterr(ao, OUT123_BAD_DRIVER);
1311 }
1312
1313 if(!ret && ao->enumerate)
1314 {
1315 if(!ao->enumerate(ao, devlist_add, &dl))
1316 {
1317 ret = dl.count;
1318 if(names)
1319 {
1320 *names = dl.names;
1321 dl.names = NULL;
1322 }
1323 if(descr)
1324 {
1325 *descr = dl.descr;
1326 dl.descr = NULL;
1327 }
1328 if(active_driver)
1329 {
1330 *active_driver = realdrv;
1331 realdrv = NULL;
1332 }
1333 } else
1334 ret = out123_seterr(ao, OUT123_DEV_ENUMERATE);
1335 out123_stringlists_free(dl.names, dl.descr, dl.count);
1336 if(ao->deinit)
1337 ao->deinit(ao);
1338 } else if(!ret)
1339 ret = out123_seterr(ao, OUT123_NOT_SUPPORTED);
1340
1341 free(realdrv);
1342 if(ao->module)
1343 INT123_close_module(ao->module, modverbose(ao, 0));
1344 out123_clear_module(ao);
1345 return ret;
1346 }
1347
1348 /* We always have ao->driver and ao->device set, also with buffer.
1349 The latter can be positively NULL, though. */
1350 int attribute_align_arg
1351 out123_driver_info(out123_handle *ao, char **driver, char **device)
1352 {
1353 debug3( "out123_driver_info(%p, %p, %p)"
1354 , (void*)ao, (void*)driver, (void*)device );
1355 if(!ao)
1356 return OUT123_ERR;
1357 if(!ao->driver)
1358 return out123_seterr(ao, OUT123_NO_DRIVER);
1359
1360 if(driver)
1361 *driver = ao->driver;
1362 if(device)
1363 *device = ao->device;
1364 return OUT123_OK;
1365 }
1366
1367 int attribute_align_arg
1368 out123_encodings(out123_handle *ao, long rate, int channels)
1369 {
1370 debug4( "[%ld]out123_encodings(%p, %li, %i)", (long)getpid()
1371 , (void*)ao, rate, channels );
1372 if(!ao)
1373 return OUT123_ERR;
1374 ao->errcode = OUT123_OK;
1375
1376 out123_stop(ao); /* That brings the buffer into waiting state, too. */
1377
1378 if(ao->state != play_stopped)
1379 return out123_seterr(ao, OUT123_NO_DRIVER);
1380
1381 ao->channels = channels;
1382 ao->rate = rate;
1383 #ifndef NOXFERMEM
1384 if(have_buffer(ao))
1385 return INT123_buffer_encodings(ao);
1386 else
1387 #endif
1388 {
1389 int enc = 0;
1390 /* This tells outputs to choose a fitting format so that ao->open() succeeds
1391 They possibly set a sample rate and channel count they like best.
1392 We should add API to retrieve those defaults, too. */
1393 ao->format = -1;
1394 if(aoopen(ao) >= 0)
1395 {
1396 /* Need to reset those since the choose-your-format open
1397 call might have changed them. */
1398 ao->channels = channels;
1399 ao->rate = rate;
1400 enc = ao->get_formats(ao);
1401 ao->close(ao);
1402 return enc;
1403 }
1404 else
1405 return out123_seterr(ao, (ao->errcode != OUT123_OK
1406 ? ao->errcode
1407 : OUT123_DEV_OPEN));
1408 }
1409 }
1410
1411 int attribute_align_arg out123_encsize(int encoding)
1412 {
1413 return MPG123_SAMPLESIZE(encoding);
1414 }
1415
1416 int attribute_align_arg
1417 out123_formats( out123_handle *ao, const long *rates, int ratecount
1418 , int minchannels, int maxchannels
1419 , struct mpg123_fmt **fmtlist )
1420 {
1421 debug7( "[%ld]out123_formats(%p, %p, %i, %i, %i, %p)", (long)getpid()
1422 , (void*)ao, (void*)rates, ratecount, minchannels, maxchannels
1423 , (void*)fmtlist );
1424 if(!ao)
1425 return OUT123_ERR;
1426 ao->errcode = OUT123_OK;
1427
1428 out123_stop(ao); /* That brings the buffer into waiting state, too. */
1429
1430 if(ao->state != play_stopped)
1431 return out123_seterr(ao, OUT123_NO_DRIVER);
1432
1433 if(ratecount > 0 && !rates)
1434 return out123_seterr(ao, OUT123_ARG_ERROR);
1435 if(!fmtlist || minchannels > maxchannels)
1436 return out123_seterr(ao, OUT123_ARG_ERROR);
1437 *fmtlist = NULL; /* Initialize so free(fmtlist) is always allowed. */
1438
1439 #ifndef NOXFERMEM
1440 if(have_buffer(ao))
1441 return INT123_buffer_formats( ao, rates, ratecount
1442 , minchannels, maxchannels, fmtlist );
1443 else
1444 #endif
1445 {
1446 /* This tells outputs to choose a fitting format so that ao->open()
1447 succeeds. */
1448 ao->format = -1;
1449 ao->rate = -1;
1450 ao->channels = -1;
1451 if(aoopen(ao) >= 0)
1452 {
1453 struct mpg123_fmt *fmts;
1454 int ri, ch;
1455 int fi = 0;
1456 int fmtcount = 1; /* Always the default format. */
1457 if(ratecount > 0)
1458 fmtcount += ratecount*(maxchannels-minchannels+1);
1459 if(!(fmts = malloc(sizeof(*fmts)*fmtcount)))
1460 {
1461 ao->close(ao);
1462 return out123_seterr(ao, OUT123_DOOM);
1463 }
1464 /* Store default format if present. */
1465 if(ao->format > 0 && ao->channels > 0 && ao->rate > 0)
1466 {
1467 fmts[0].rate = ao->rate;
1468 fmts[0].channels = ao->channels;
1469 fmts[0].encoding = ao->format;
1470 }
1471 else
1472 { /* Ensure consistent -1 in all entries. */
1473 fmts[0].rate = -1;
1474 fmts[0].channels = -1;
1475 fmts[0].encoding = -1;
1476 }
1477 /* Test all combinations of rate and channel count. */
1478 for(ri=0; ri<ratecount; ++ri)
1479 for(ch=minchannels; ch<=maxchannels; ++ch)
1480 {
1481 ++fi;
1482 ao->rate = rates[ri];
1483 ao->channels = ch;
1484 fmts[fi].rate = ao->rate;
1485 fmts[fi].channels = ao->channels;
1486 fmts[fi].encoding = ao->get_formats(ao);
1487 }
1488 ao->close(ao);
1489
1490 *fmtlist = fmts;
1491 return fmtcount;
1492 }
1493 else
1494 return out123_seterr(ao, (ao->errcode != OUT123_OK
1495 ? ao->errcode
1496 : OUT123_DEV_OPEN));
1497 }
1498 }
1499
1500
1501 size_t attribute_align_arg out123_buffered(out123_handle *ao)
1502 {
1503 debug2("[%ld]out123_buffered(%p)", (long)getpid(), (void*)ao);
1504 if(!ao)
1505 return 0;
1506 ao->errcode = 0;
1507 #ifndef NOXFERMEM
1508 if(have_buffer(ao))
1509 {
1510 size_t fill = INT123_buffer_fill(ao);
1511 debug2("out123_buffered(%p) = %zu", (void*)ao, fill);
1512 return fill;
1513 }
1514 else
1515 #endif
1516 return 0;
1517 }
1518
1519 int attribute_align_arg out123_getformat( out123_handle *ao
1520 , long *rate, int *channels, int *encoding, int *framesize )
1521 {
1522 if(!ao)
1523 return OUT123_ERR;
1524
1525 if(!(ao->state == play_paused || ao->state == play_live))
1526 return out123_seterr(ao, OUT123_NOT_LIVE);
1527
1528 if(rate)
1529 *rate = ao->rate;
1530 if(channels)
1531 *channels = ao->channels;
1532 if(encoding)
1533 *encoding = ao->format;
1534 if(framesize)
1535 *framesize = ao->framesize;
1536 return OUT123_OK;
1537 }
1538
1539 struct enc_desc
1540 {
1541 int code; /* MPG123_ENC_SOMETHING */
1542 const char *longname; /* signed bla bla */
1543 const char *name; /* sXX, short name */
1544 };
1545
1546 static const struct enc_desc encdesc[] =
1547 {
1548 { MPG123_ENC_SIGNED_16, "signed 16 bit", "s16" }
1549 , { MPG123_ENC_UNSIGNED_16, "unsigned 16 bit", "u16" }
1550 , { MPG123_ENC_SIGNED_32, "signed 32 bit", "s32" }
1551 , { MPG123_ENC_UNSIGNED_32, "unsigned 32 bit", "u32" }
1552 , { MPG123_ENC_SIGNED_24, "signed 24 bit", "s24" }
1553 , { MPG123_ENC_UNSIGNED_24, "unsigned 24 bit", "u24" }
1554 , { MPG123_ENC_FLOAT_32, "float (32 bit)", "f32" }
1555 , { MPG123_ENC_FLOAT_64, "float (64 bit)", "f64" }
1556 , { MPG123_ENC_SIGNED_8, "signed 8 bit", "s8" }
1557 , { MPG123_ENC_UNSIGNED_8, "unsigned 8 bit", "u8" }
1558 , { MPG123_ENC_ULAW_8, "mu-law (8 bit)", "ulaw" }
1559 , { MPG123_ENC_ALAW_8, "a-law (8 bit)", "alaw" }
1560 };
1561 #define KNOWN_ENCS (sizeof(encdesc)/sizeof(struct enc_desc))
1562
1563 int attribute_align_arg out123_enc_list(int **enclist)
1564 {
1565 int i;
1566 if(!enclist)
1567 return OUT123_ERR;
1568 *enclist = malloc(sizeof(int)*KNOWN_ENCS);
1569 if(!(*enclist))
1570 return OUT123_ERR;
1571 for(i=0;i<KNOWN_ENCS;++i)
1572 (*enclist)[i] = encdesc[i].code;
1573 return KNOWN_ENCS;
1574 }
1575
1576 int attribute_align_arg out123_enc_byname(const char *name)
1577 {
1578 int i;
1579 if(!name)
1580 return OUT123_ERR;
1581 for(i=0; i<KNOWN_ENCS; ++i) if(
1582 !strcasecmp(encdesc[i].name, name)
1583 || !strcasecmp(encdesc[i].longname, name)
1584 )
1585 return encdesc[i].code;
1586 return OUT123_ERR;
1587 }
1588
1589 const char* attribute_align_arg out123_enc_name(int encoding)
1590 {
1591 int i;
1592 for(i=0; i<KNOWN_ENCS; ++i) if(encdesc[i].code == encoding)
1593 return encdesc[i].name;
1594 return NULL;
1595 }
1596
1597 const char* attribute_align_arg out123_enc_longname(int encoding)
1598 {
1599 int i;
1600 for(i=0; i<KNOWN_ENCS; ++i) if(encdesc[i].code == encoding)
1601 return encdesc[i].longname;
1602 return NULL;
1603 }