squeezelite.h 22 KB

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