squeezelite.h 21 KB

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