squeezelite.h 23 KB

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