squeezelite.h 23 KB

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