1 /*
2  *  Squeezelite - lightweight headless squeezebox emulator
3  *
4  *  (c) Adrian Smith 2012-2015, triode1@btinternet.com
5  *      Ralph Irving 2015-2017, ralph_irving@hotmail.com
6  *
get_shell_ffi() -> Option<String>7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Additions (c) Paul Hermann, 2015-2017 under the same license terms
21  *   -Control of Raspberry pi GPIO for amplifier power
22  *   -Launch script on power status change from LMS
23  */
24 
25 // make may define: PORTAUDIO, SELFPIPE, RESAMPLE, RESAMPLE_MP, VISEXPORT, GPIO, IR, DSD, LINKALL to influence build
26 
27 #define MAJOR_VERSION "1.9"
28 #define MINOR_VERSION "7"
29 #define MICRO_VERSION "1253"
30 
31 #if defined(CUSTOM_VERSION)
32 #define VERSION "v" MAJOR_VERSION "." MINOR_VERSION "-" MICRO_VERSION STR(CUSTOM_VERSION)
33 #else
34 #define VERSION "v" MAJOR_VERSION "." MINOR_VERSION "-" MICRO_VERSION
35 #endif
36 
37 
38 #if !defined(MODEL_NAME)
39 #define MODEL_NAME SqueezeLite
40 #endif
41 
42 #define QUOTE(name) #name
43 #define STR(macro)  QUOTE(macro)
44 #define MODEL_NAME_STRING STR(MODEL_NAME)
45 
46 // build detection
47 #if defined(linux)
48 #define LINUX     1
49 #define OSX       0
50 #define WIN       0
51 #define FREEBSD   0
52 #elif defined (__APPLE__)
53 #define LINUX     0
54 #define OSX       1
55 #define WIN       0
56 #define PORTAUDIO 1
57 #define FREEBSD   0
58 #elif defined (_MSC_VER)
59 #define LINUX     0
60 #define OSX       0
61 #define WIN       1
62 #define PORTAUDIO 1
63 #define FREEBSD   0
64 #elif defined(__FreeBSD__) || defined(__DragonFly__)
65 #define LINUX     0
66 #define OSX       0
67 #define WIN       0
68 #define PORTAUDIO 1
69 #define FREEBSD   1
70 #elif defined (__sun)
71 #define SUN       1
72 #define LINUX     1
73 #define PORTAUDIO 1
74 #define PA18API   1
75 #define OSX       0
76 #define WIN       0
77 #else
78 #error unknown target
79 #endif
80 
81 #if LINUX && !defined(PORTAUDIO) && !defined(PULSEAUDIO)
82 #define ALSA       1
83 #else
84 #define ALSA       0
85 #endif
86 
87 #if SUN
88 #define EVENTFD   0
89 #define WINEVENT  0
90 #define SELFPIPE  1
91 #elif LINUX && !defined(SELFPIPE)
92 #define EVENTFD   1
93 #define SELFPIPE  0
94 #define WINEVENT  0
95 #endif
96 #if (LINUX && !EVENTFD) || OSX || FREEBSD
97 #define EVENTFD   0
98 #define SELFPIPE  1
99 #define WINEVENT  0
100 #endif
101 #if WIN
102 #define EVENTFD   0
103 #define SELFPIPE  0
104 #define WINEVENT  1
105 #endif
106 
107 #if defined(RESAMPLE) || defined(RESAMPLE_MP)
108 #undef  RESAMPLE
109 #define RESAMPLE  1 // resampling
110 #define PROCESS   1 // any sample processing (only resampling at present)
111 #else
112 #define RESAMPLE  0
113 #define PROCESS   0
114 #endif
115 #if defined(RESAMPLE_MP)
116 #undef RESAMPLE_MP
117 #define RESAMPLE_MP 1
118 #else
119 #define RESAMPLE_MP 0
120 #endif
121 
122 #if defined(ALAC)
123 #undef ALAC
124 #define ALAC    1
125 #else
126 #define ALAC    0
127 #endif
128 
129 #if defined(FFMPEG)
130 #undef FFMPEG
131 #define FFMPEG    1
132 #else
133 #define FFMPEG    0
134 #endif
135 
136 #if defined(OPUS)
137 #undef OPUS
138 #define OPUS    1
139 #else
140 #define OPUS    0
141 #endif
142 
143 #if (LINUX || OSX) && defined(VISEXPORT)
144 #undef VISEXPORT
145 #define VISEXPORT 1 // visulizer export support uses linux shared memory
146 #else
147 #define VISEXPORT 0
148 #endif
149 
150 #if LINUX && defined(IR)
151 #undef IR
152 #define IR 1
153 #else
154 #define IR 0
155 #endif
156 
157 #if defined(DSD)
158 #undef DSD
159 #define DSD       1
160 #define IF_DSD(x) { x }
161 #else
162 #undef DSD
163 #define DSD       0
164 #define IF_DSD(x)
165 #endif
166 
167 #if defined(LINKALL)
168 #undef LINKALL
169 #define LINKALL   1 // link all libraries at build time - requires all to be available at run time
170 #else
171 #define LINKALL   0
172 #endif
173 
174 #if defined (USE_SSL)
175 #undef USE_SSL
176 #define USE_SSL 1
177 #else
178 #define USE_SSL 0
179 #endif
180 
181 #if defined (NO_SSLSYM)
182 #undef NO_SSLSYM
183 #define NO_SSLSYM 1
184 #else
185 #define NO_SSLSYM 0
186 #endif
187 
188 #if !LINKALL
189 
190 // dynamically loaded libraries at run time
191 
192 #if LINUX
193 #define LIBFLAC "libFLAC.so.8"
194 #define LIBMPG "libmpg123.so.0"
195 #define LIBVORBIS "libvorbisfile.so.3"
196 #define LIBOPUS "libopusfile.so.0"
197 #define LIBTREMOR "libvorbisidec.so.1"
198 #define LIBFAAD "libfaad.so.2"
199 #define LIBAVUTIL   "libavutil.so.%d"
200 #define LIBAVCODEC  "libavcodec.so.%d"
201 #define LIBAVFORMAT "libavformat.so.%d"
202 #define LIBSOXR "libsoxr.so.0"
203 #define LIBLIRC "liblirc_client.so.0"
204 #endif
205 
206 #if OSX
207 #define LIBFLAC "libFLAC.8.dylib"
208 #define LIBMPG "libmpg123.0.dylib"
209 #define LIBVORBIS "libvorbisfile.3.dylib"
210 #define LIBTREMOR "libvorbisidec.1.dylib"
211 #define LIBOPUS "libopusfile.0.dylib"
212 #define LIBFAAD "libfaad.2.dylib"
213 #define LIBAVUTIL   "libavutil.%d.dylib"
214 #define LIBAVCODEC  "libavcodec.%d.dylib"
215 #define LIBAVFORMAT "libavformat.%d.dylib"
216 #define LIBSOXR "libsoxr.0.dylib"
217 #endif
218 
219 #if WIN
220 #define LIBFLAC "libFLAC.dll"
221 #define LIBMPG "libmpg123-0.dll"
222 #define LIBVORBIS "libvorbisfile.dll"
223 #define LIBOPUS "libopusfile-0.dll"
224 #define LIBTREMOR "libvorbisidec.dll"
225 #define LIBFAAD "libfaad2.dll"
226 #define LIBAVUTIL   "avutil-%d.dll"
227 #define LIBAVCODEC  "avcodec-%d.dll"
228 #define LIBAVFORMAT "avformat-%d.dll"
229 #define LIBSOXR "libsoxr.dll"
230 #endif
231 
232 #if FREEBSD
233 #define LIBFLAC "libFLAC.so.8"
234 #define LIBMPG "libmpg123.so.0"
235 #define LIBVORBIS "libvorbisfile.so.3"
236 #define LIBTREMOR "libvorbisidec.so.1"
237 #define LIBOPUS "libopusfile.so.1"
238 #define LIBFAAD "libfaad.so.2"
239 #define LIBAVUTIL   "libavutil.so.%d"
240 #define LIBAVCODEC  "libavcodec.so.%d"
241 #define LIBAVFORMAT "libavformat.so.%d"
242 #define LIBSOXR "libsoxr.so.0"
243 #endif
244 
245 #endif // !LINKALL
246 
247 // config options
248 #define STREAMBUF_SIZE (2 * 1024 * 1024)
249 #define OUTPUTBUF_SIZE (44100 * 8 * 10)
250 #define OUTPUTBUF_SIZE_CROSSFADE (OUTPUTBUF_SIZE * 12 / 10)
251 
252 #define MAX_HEADER 4096 // do not reduce as icy-meta max is 4080
253 
254 #if ALSA
255 #define ALSA_BUFFER_TIME  40
256 #define ALSA_PERIOD_COUNT 4
257 #define OUTPUT_RT_PRIORITY 45
258 #endif
259 
260 #define SL_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
261 
262 #if SUN || OSXPPC
263 #undef SL_LITTLE_ENDIAN
264 #endif
265 
266 #include <stdio.h>
267 #include <stdlib.h>
268 #include <stdarg.h>
269 #include <string.h>
270 #include <errno.h>
271 #include <limits.h>
272 #include <sys/types.h>
273 
274 #if LINUX || OSX || FREEBSD
275 #include <unistd.h>
276 #include <stdbool.h>
277 #include <netinet/in.h>
278 #include <arpa/inet.h>
279 #include <sys/time.h>
280 #include <sys/socket.h>
281 #include <poll.h>
282 #include <dlfcn.h>
283 #include <pthread.h>
284 #include <signal.h>
285 #if SUN
286 #include <sys/types.h>
287 #endif /* SUN */
288 
289 #define STREAM_THREAD_STACK_SIZE  64 * 1024
290 #define DECODE_THREAD_STACK_SIZE 128 * 1024
291 #define OUTPUT_THREAD_STACK_SIZE  64 * 1024
292 #define IR_THREAD_STACK_SIZE      64 * 1024
293 #if !OSX
294 #define thread_t pthread_t;
295 #endif
296 #define closesocket(s) close(s)
297 #define last_error() errno
298 #define ERROR_WOULDBLOCK EWOULDBLOCK
299 
300 #ifdef SUN
301 typedef uint8_t  u8_t;
302 typedef uint16_t u16_t;
303 typedef uint32_t u32_t;
304 typedef uint64_t u64_t;
305 #else
306 typedef u_int8_t  u8_t;
307 typedef u_int16_t u16_t;
308 typedef u_int32_t u32_t;
309 typedef u_int64_t u64_t;
310 #endif /* SUN */
311 typedef int16_t   s16_t;
312 typedef int32_t   s32_t;
313 typedef int64_t   s64_t;
314 
315 #define mutex_type pthread_mutex_t
316 #define mutex_create(m) pthread_mutex_init(&m, NULL)
317 #define mutex_create_p(m) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); pthread_mutex_init(&m, &attr); pthread_mutexattr_destroy(&attr)
318 #define mutex_lock(m) pthread_mutex_lock(&m)
319 #define mutex_unlock(m) pthread_mutex_unlock(&m)
320 #define mutex_destroy(m) pthread_mutex_destroy(&m)
321 #define thread_type pthread_t
322 
323 #endif
324 
325 #if WIN
326 
327 #include <winsock2.h>
328 #include <ws2tcpip.h>
329 #include <io.h>
330 
331 #define STREAM_THREAD_STACK_SIZE (1024 * 64)
332 #define DECODE_THREAD_STACK_SIZE (1024 * 128)
333 #define OUTPUT_THREAD_STACK_SIZE (1024 * 64)
334 
335 typedef unsigned __int8  u8_t;
336 typedef unsigned __int16 u16_t;
337 typedef unsigned __int32 u32_t;
338 typedef unsigned __int64 u64_t;
339 typedef __int16 s16_t;
340 typedef __int32 s32_t;
341 typedef __int64 s64_t;
342 
343 typedef BOOL bool;
344 #define true TRUE
345 #define false FALSE
346 
347 #define inline __inline
348 
349 #define mutex_type HANDLE
350 #define mutex_create(m) m = CreateMutex(NULL, FALSE, NULL)
351 #define mutex_create_p mutex_create
352 #define mutex_lock(m) WaitForSingleObject(m, INFINITE)
353 #define mutex_unlock(m) ReleaseMutex(m)
354 #define mutex_destroy(m) CloseHandle(m)
355 #define thread_type HANDLE
356 
357 #define usleep(x) Sleep(x/1000)
358 #define sleep(x) Sleep(x*1000)
359 #define last_error() WSAGetLastError()
360 #define ERROR_WOULDBLOCK WSAEWOULDBLOCK
361 #define open _open
362 #define read _read
363 #define snprintf _snprintf
364 
365 #define in_addr_t u32_t
366 #define socklen_t int
367 #define ssize_t int
368 
369 #define RTLD_NOW 0
370 
371 #endif
372 
373 #if !defined(MSG_NOSIGNAL)
374 #define MSG_NOSIGNAL 0
375 #endif
376 
377 typedef u32_t frames_t;
378 typedef int sockfd;
379 
380 #if EVENTFD
381 #include <sys/eventfd.h>
382 #define event_event int
383 #define event_handle struct pollfd
384 #define wake_create(e) e = eventfd(0, 0)
385 #define wake_signal(e) eventfd_write(e, 1)
386 #define wake_clear(e) eventfd_t val; eventfd_read(e, &val)
387 #define wake_close(e) close(e)
388 #endif
389 
390 #if SELFPIPE
391 #define event_handle struct pollfd
392 #define event_event struct wake
393 #define wake_create(e) pipe(e.fds); set_nonblock(e.fds[0]); set_nonblock(e.fds[1])
394 #define wake_signal(e) write(e.fds[1], ".", 1)
395 #define wake_clear(e) char c[10]; read(e, &c, 10)
396 #define wake_close(e) close(e.fds[0]); close(e.fds[1])
397 struct wake {
398 	int fds[2];
399 };
400 #endif
401 
402 #if WINEVENT
403 #define event_event HANDLE
404 #define event_handle HANDLE
405 #define wake_create(e) e = CreateEvent(NULL, FALSE, FALSE, NULL)
406 #define wake_signal(e) SetEvent(e)
407 #define wake_close(e) CloseHandle(e)
408 #endif
409 
410 // printf/scanf formats for u64_t
411 #if (LINUX && __WORDSIZE == 64) || (FREEBSD && __LP64__)
412 #define FMT_u64 "%lu"
413 #define FMT_x64 "%lx"
414 #elif __GLIBC_HAVE_LONG_LONG || defined __GNUC__ || WIN || SUN
415 #define FMT_u64 "%llu"
416 #define FMT_x64 "%llx"
417 #else
418 #error can not support u64_t
419 #endif
420 
421 #define MAX_SILENCE_FRAMES 2048
422 
423 #define FIXED_ONE 0x10000
424 
425 #define BYTES_PER_FRAME 8
426 
427 #if BYTES_PER_FRAME == 8
428 #define ISAMPLE_T 		s32_t
429 #else
430 #define ISAMPLE_T		s16_t
431 #endif
432 
433 #define min(a,b) (((a) < (b)) ? (a) : (b))
434 
435 // logging
436 typedef enum { lERROR = 0, lWARN, lINFO, lDEBUG, lSDEBUG } log_level;
437 
438 const char *logtime(void);
439 void logprint(const char *fmt, ...);
440 
441 #define LOG_ERROR(fmt, ...) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
442 #define LOG_WARN(fmt, ...)  if (loglevel >= lWARN)  logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
443 #define LOG_INFO(fmt, ...)  if (loglevel >= lINFO)  logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
444 #define LOG_DEBUG(fmt, ...) if (loglevel >= lDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
445 #define LOG_SDEBUG(fmt, ...) if (loglevel >= lSDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
446 
447 // utils.c (non logging)
448 typedef enum { EVENT_TIMEOUT = 0, EVENT_READ, EVENT_WAKE } event_type;
449 #if WIN && USE_SSL
450 char* strcasestr(const char *haystack, const char *needle);
451 #endif
452 
453 char *next_param(char *src, char c);
454 u32_t gettime_ms(void);
455 void get_mac(u8_t *mac);
456 void set_nonblock(sockfd s);
457 int connect_timeout(sockfd sock, const struct sockaddr *addr, socklen_t addrlen, int timeout);
458 void server_addr(char *server, in_addr_t *ip_ptr, unsigned *port_ptr);
459 void set_readwake_handles(event_handle handles[], sockfd s, event_event e);
460 event_type wait_readwake(event_handle handles[], int timeout);
461 void packN(u32_t *dest, u32_t val);
462 void packn(u16_t *dest, u16_t val);
463 u32_t unpackN(u32_t *src);
464 u16_t unpackn(u16_t *src);
465 #if OSX
466 void set_nosigpipe(sockfd s);
467 #else
468 #define set_nosigpipe(s)
469 #endif
470 #if SUN
471 void init_daemonize(void);
472 int daemon(int,int);
473 #endif
474 #if WIN
475 void winsock_init(void);
476 void winsock_close(void);
477 void *dlopen(const char *filename, int flag);
478 void *dlsym(void *handle, const char *symbol);
479 char *dlerror(void);
480 int poll(struct pollfd *fds, unsigned long numfds, int timeout);
481 #endif
482 #if LINUX || FREEBSD
483 void touch_memory(u8_t *buf, size_t size);
484 #endif
485 
486 // buffer.c
487 struct buffer {
488 	u8_t *buf;
489 	u8_t *readp;
490 	u8_t *writep;
491 	u8_t *wrap;
492 	size_t size;
493 	size_t base_size;
494 	mutex_type mutex;
495 };
496 
497 // _* called with mutex locked
498 unsigned _buf_used(struct buffer *buf);
499 unsigned _buf_space(struct buffer *buf);
500 unsigned _buf_cont_read(struct buffer *buf);
501 unsigned _buf_cont_write(struct buffer *buf);
502 void _buf_inc_readp(struct buffer *buf, unsigned by);
503 void _buf_inc_writep(struct buffer *buf, unsigned by);
504 void buf_flush(struct buffer *buf);
505 void buf_adjust(struct buffer *buf, size_t mod);
506 void _buf_resize(struct buffer *buf, size_t size);
507 void buf_init(struct buffer *buf, size_t size);
508 void buf_destroy(struct buffer *buf);
509 
510 // slimproto.c
511 void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate);
512 void slimproto_stop(void);
513 void wake_controller(void);
514 
515 // stream.c
516 typedef enum { STOPPED = 0, DISCONNECT, STREAMING_WAIT,
517 			   STREAMING_BUFFERING, STREAMING_FILE, STREAMING_HTTP, SEND_HEADERS, RECV_HEADERS } stream_state;
518 typedef enum { DISCONNECT_OK = 0, LOCAL_DISCONNECT = 1, REMOTE_DISCONNECT = 2, UNREACHABLE = 3, TIMEOUT = 4 } disconnect_code;
519 
520 struct streamstate {
521 	stream_state state;
522 	disconnect_code disconnect;
523 	char *header;
524 	size_t header_len;
525 	bool sent_headers;
526 	bool cont_wait;
527 	u64_t bytes;
528 	unsigned threshold;
529 	u32_t meta_interval;
530 	u32_t meta_next;
531 	u32_t meta_left;
532 	bool  meta_send;
533 };
534 
535 void stream_init(log_level level, unsigned stream_buf_size);
536 void stream_close(void);
537 void stream_file(const char *header, size_t header_len, unsigned threshold);
538 void stream_sock(u32_t ip, u16_t port, const char *header, size_t header_len, unsigned threshold, bool cont_wait);
539 bool stream_disconnect(void);
540 
541 // decode.c
542 typedef enum { DECODE_STOPPED = 0, DECODE_READY, DECODE_RUNNING, DECODE_COMPLETE, DECODE_ERROR } decode_state;
543 
544 struct decodestate {
545 	decode_state state;
546 	bool new_stream;
547 	mutex_type mutex;
548 #if PROCESS
549 	bool direct;
550 	bool process;
551 #endif
552 };
553 
554 #if PROCESS
555 struct processstate {
556 	u8_t *inbuf, *outbuf;
557 	unsigned max_in_frames, max_out_frames;
558 	unsigned in_frames, out_frames;
559 	unsigned in_sample_rate, out_sample_rate;
560 	unsigned long total_in, total_out;
561 };
562 #endif
563 
564 struct codec {
565 	char id;
566 	char *types;
567 	unsigned min_read_bytes;
568 	unsigned min_space;
569 	void (*open)(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);
570 	void (*close)(void);
571 	decode_state (*decode)(void);
572 };
573 
574 void decode_init(log_level level, const char *include_codecs, const char *exclude_codecs);
575 void decode_close(void);
576 void decode_flush(void);
577 unsigned decode_newstream(unsigned sample_rate, unsigned supported_rates[]);
578 void codec_open(u8_t format, u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);
579 
580 #if PROCESS
581 // process.c
582 void process_samples(void);
583 void process_drain(void);
584 void process_flush(void);
585 unsigned process_newstream(bool *direct, unsigned raw_sample_rate, unsigned supported_rates[]);
586 void process_init(char *opt);
587 #endif
588 
589 #if RESAMPLE
590 // resample.c
591 void resample_samples(struct processstate *process);
592 bool resample_drain(struct processstate *process);
593 bool resample_newstream(struct processstate *process, unsigned raw_sample_rate, unsigned supported_rates[]);
594 void resample_flush(void);
595 bool resample_init(char *opt);
596 #endif
597 
598 // output.c output_alsa.c output_pa.c output_pack.c
599 typedef enum { OUTPUT_OFF = -1, OUTPUT_STOPPED = 0, OUTPUT_BUFFER, OUTPUT_RUNNING,
600 			   OUTPUT_PAUSE_FRAMES, OUTPUT_SKIP_FRAMES, OUTPUT_START_AT } output_state;
601 
602 #if DSD
603 typedef enum { PCM, DOP, DSD_U8, DSD_U16_LE, DSD_U32_LE, DSD_U16_BE, DSD_U32_BE, DOP_S24_LE, DOP_S24_3LE } dsd_format;
604 typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE, U8, U16_LE, U16_BE, U32_LE, U32_BE } output_format;
605 #else
606 typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE } output_format;
607 #endif
608 
609 typedef enum { FADE_INACTIVE = 0, FADE_DUE, FADE_ACTIVE } fade_state;
610 typedef enum { FADE_UP = 1, FADE_DOWN, FADE_CROSS } fade_dir;
611 typedef enum { FADE_NONE = 0, FADE_CROSSFADE, FADE_IN, FADE_OUT, FADE_INOUT } fade_mode;
612 
613 #define MAX_SUPPORTED_SAMPLERATES 18
614 #define TEST_RATES = { 768000, 705600, 384000, 352800, 192000, 176400, 96000, 88200, 48000, 44100, 32000, 24000, 22500, 16000, 12000, 11025, 8000, 0 }
615 
616 struct outputstate {
617 	output_state state;
618 	output_format format;
619 	const char *device;
620 #if ALSA
621 	unsigned buffer;
622 	unsigned period;
623 #endif
624 	bool  track_started;
625 #if PORTAUDIO
626 	bool  pa_reopen;
627 	unsigned latency;
628 	int pa_hostapi_option;
629 #endif
630 	int (* write_cb)(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR, s32_t cross_gain_in, s32_t cross_gain_out, s32_t **cross_ptr);
631 	unsigned start_frames;
632 	unsigned frames_played;
633 	unsigned frames_played_dmp;// frames played at the point delay is measured
634 	unsigned current_sample_rate;
635 	unsigned supported_rates[MAX_SUPPORTED_SAMPLERATES]; // ordered largest first so [0] is max_rate
636 	unsigned default_sample_rate;
637 	bool error_opening;
638 	unsigned device_frames;
639 	u32_t updated;
640 	u32_t track_start_time;
641 	u32_t current_replay_gain;
642 	union {
643 		u32_t pause_frames;
644 		u32_t skip_frames;
645 		u32_t start_at;
646 	};
647 	unsigned next_sample_rate; // set in decode thread
648 	u8_t  *track_start;        // set in decode thread
649 	u32_t gainL;               // set by slimproto
650 	u32_t gainR;               // set by slimproto
651 	bool  invert;              // set by slimproto
652 	u32_t next_replay_gain;    // set by slimproto
653 	unsigned threshold;        // set by slimproto
654 	fade_state fade;
655 	u8_t *fade_start;
656 	u8_t *fade_end;
657 	fade_dir fade_dir;
658 	fade_mode fade_mode;       // set by slimproto
659 	unsigned fade_secs;        // set by slimproto
660 	unsigned rate_delay;
661 	bool delay_active;
662 	u32_t stop_time;
663 	u32_t idle_to;
664 #if DSD
665 	dsd_format next_fmt;       // set in decode thread
666 	dsd_format outfmt;
667 	dsd_format dsdfmt;	       // set in dsd_init - output for DSD: DOP, DSD_U8, ...
668 	unsigned dsd_delay;		   // set in dsd_init - delay in ms switching to/from dop
669 #endif
670 };
671 
672 void output_init_common(log_level level, const char *device, unsigned output_buf_size, unsigned rates[], unsigned idle);
673 void output_close_common(void);
674 void output_flush(void);
675 // _* called with mutex locked
676 frames_t _output_frames(frames_t avail);
677 void _checkfade(bool);
678 
679 // output_alsa.c
680 #if ALSA
681 void list_devices(void);
682 void list_mixers(const char *output_device);
683 void set_volume(unsigned left, unsigned right);
684 bool test_open(const char *device, unsigned rates[], bool userdef_rates);
685 void output_init_alsa(log_level level, const char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned rt_priority, unsigned idle, char *mixer_device, char *volume_mixer, bool mixer_unmute, bool mixer_linear);
686 void output_close_alsa(void);
687 #endif
688 
689 // output_pa.c
690 #if PORTAUDIO
691 void list_devices(void);
692 void set_volume(unsigned left, unsigned right);
693 bool test_open(const char *device, unsigned rates[], bool userdef_rates);
694 void output_init_pa(log_level level, const char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
695 void output_close_pa(void);
696 void _pa_open(void);
697 #endif
698 
699 // output_pulse.c
700 #if PULSEAUDIO
701 void list_devices(void);
702 void set_volume(unsigned left, unsigned right);
703 bool test_open(const char *device, unsigned rates[], bool userdef_rates);
704 void output_init_pulse(log_level level, const char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
705 void output_close_pulse(void);
706 #endif
707 
708 // output_stdout.c
709 void output_init_stdout(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay);
710 void output_close_stdout(void);
711 
712 // output_pack.c
713 void _scale_and_pack_frames(void *outputptr, s32_t *inputptr, frames_t cnt, s32_t gainL, s32_t gainR, output_format format);
714 void _apply_cross(struct buffer *outputbuf, frames_t out_frames, s32_t cross_gain_in, s32_t cross_gain_out, s32_t **cross_ptr);
715 void _apply_gain(struct buffer *outputbuf, frames_t count, s32_t gainL, s32_t gainR);
716 s32_t gain(s32_t gain, s32_t sample);
717 s32_t to_gain(float f);
718 
719 // output_vis.c
720 #if VISEXPORT
721 void _vis_export(struct buffer *outputbuf, struct outputstate *output, frames_t out_frames, bool silence);
722 void output_vis_init(log_level level, u8_t *mac);
723 void vis_stop(void);
724 #else
725 #define _vis_export(...)
726 #define vis_stop()
727 #endif
728 
729 // dop.c
730 #if DSD
731 bool is_stream_dop(u8_t *lptr, u8_t *rptr, int step, frames_t frames);
732 void update_dop(u32_t *ptr, frames_t frames, bool invert);
733 void dsd_silence_frames(u32_t *ptr, frames_t frames);
734 void dsd_invert(u32_t *ptr, frames_t frames);
735 void dsd_init(dsd_format format, unsigned delay);
736 #endif
737 
738 // codecs
739 #define MAX_CODECS 10
740 
741 struct codec *register_flac(void);
742 struct codec *register_pcm(void);
743 struct codec *register_mad(void);
744 struct codec *register_mpg(void);
745 struct codec *register_vorbis(void);
746 #if ALAC
747 struct codec *register_alac(void);
748 #endif
749 struct codec *register_faad(void);
750 struct codec *register_dsd(void);
751 struct codec *register_ff(const char *codec);
752 #if OPUS
753 struct codec *register_opus(void);
754 #endif
755 
756 //gpio.c
757 #if GPIO
758 void relay( int state);
759 void relay_script(int state);
760 int gpio_pin;
761 bool gpio_active_low;
762 bool gpio_active;
763 char *power_script;
764 //  my amp state
765 int ampstate;
766 #if RPI
767 #define PI_INPUT  0
768 #define PI_OUTPUT 1
769 #define PI_LOW 0
770 #define PI_HIGH 1
771 void gpioSetMode(unsigned gpio, unsigned mode);
772 void gpioWrite(unsigned gpio, unsigned level);
773 int gpioInitialise(void);
774 #endif
775 #endif
776 
777 // ir.c
778 #if IR
779 struct irstate {
780 	mutex_type mutex;
781 	u32_t code;
782 	u32_t ts;
783 };
784 
785 void ir_init(log_level level, char *lircrc);
786 void ir_close(void);
787 #endif
788 
789 // sslsym.c
790 #if USE_SSL && !LINKALL && !NO_SSLSYM
791 bool load_ssl_symbols(void);
792 void free_ssl_symbols(void);
793 bool ssl_loaded;
794 #endif
795 
796