squeezelite.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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_WARN(fmt, ...) if (loglevel >= lWARN) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  351. #define LOG_INFO(fmt, ...) if (loglevel >= lINFO) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  352. #define LOG_DEBUG(fmt, ...) if (loglevel >= lDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  353. #define LOG_SDEBUG(fmt, ...) if (loglevel >= lSDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  354. typedef uint32_t frames_t;
  355. typedef int sockfd;
  356. #if EMBEDDED
  357. #include "embedded.h"
  358. #endif
  359. #ifndef LOG_ERROR
  360. #define LOG_ERROR(fmt, ...) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  361. #endif
  362. #if !defined(MSG_NOSIGNAL)
  363. #define MSG_NOSIGNAL 0
  364. #endif
  365. #if EVENTFD
  366. #include <sys/eventfd.h>
  367. #define event_event int
  368. #define event_handle struct pollfd
  369. #define wake_create(e) e = eventfd(0, 0)
  370. #define wake_signal(e) eventfd_write(e, 1)
  371. #define wake_clear(e) eventfd_t val; eventfd_read(e, &val)
  372. #define wake_close(e) close(e)
  373. #endif
  374. #if SELFPIPE
  375. #define event_handle struct pollfd
  376. #define event_event struct wake
  377. #define wake_create(e) pipe(e.fds); set_nonblock(e.fds[0]); set_nonblock(e.fds[1])
  378. #define wake_signal(e) write(e.fds[1], ".", 1)
  379. #define wake_clear(e) char c[10]; read(e, &c, 10)
  380. #define wake_close(e) close(e.fds[0]); close(e.fds[1])
  381. struct wake {
  382. int fds[2];
  383. };
  384. #endif
  385. #if LOOPBACK
  386. #define event_handle struct pollfd
  387. #define event_event struct wake
  388. #define wake_create(e) _wake_create(&e)
  389. #define wake_signal(e) send(e.fds[1], ".", 1, 0)
  390. #define wake_clear(e) char c; recv(e, &c, 1, 0)
  391. #define wake_close(e) closesocket(e.mfds); closesocket(e.fds[0]); closesocket(e.fds[1])
  392. struct wake {
  393. int mfds;
  394. int fds[2];
  395. };
  396. void _wake_create(event_event*);
  397. #endif
  398. #if WINEVENT
  399. #define event_event HANDLE
  400. #define event_handle HANDLE
  401. #define wake_create(e) e = CreateEvent(NULL, FALSE, FALSE, NULL)
  402. #define wake_signal(e) SetEvent(e)
  403. #define wake_close(e) CloseHandle(e)
  404. #endif
  405. #ifndef EXT_BSS
  406. #define EXT_BSS
  407. #endif
  408. // printf/scanf formats for u64_t
  409. #if (LINUX && __WORDSIZE == 64) || (FREEBSD && __LP64__)
  410. #define FMT_u64 "%lu"
  411. #define FMT_x64 "%lx"
  412. #elif __GLIBC_HAVE_LONG_LONG || defined __GNUC__ || WIN || SUN
  413. #define FMT_u64 "%llu"
  414. #define FMT_x64 "%llx"
  415. #else
  416. #error can not support u64_t
  417. #endif
  418. #define MAX_SILENCE_FRAMES 2048
  419. #define FIXED_ONE 0x10000
  420. #ifndef BYTES_PER_FRAME
  421. #define BYTES_PER_FRAME 8
  422. #endif
  423. #if BYTES_PER_FRAME == 8
  424. #define ISAMPLE_T s32_t
  425. #else
  426. #define ISAMPLE_T s16_t
  427. #endif
  428. #define min(a,b) (((a) < (b)) ? (a) : (b))
  429. // utils.c (non logging)
  430. typedef enum { EVENT_TIMEOUT = 0, EVENT_READ, EVENT_WAKE } event_type;
  431. #if WIN && USE_SSL
  432. char* strcasestr(const char *haystack, const char *needle);
  433. #endif
  434. char *next_param(char *src, char c);
  435. u32_t gettime_ms(void);
  436. void get_mac(u8_t *mac);
  437. void set_nonblock(sockfd s);
  438. int connect_timeout(sockfd sock, const struct sockaddr *addr, socklen_t addrlen, int timeout);
  439. void server_addr(char *server, in_addr_t *ip_ptr, unsigned *port_ptr);
  440. void set_readwake_handles(event_handle handles[], sockfd s, event_event e);
  441. event_type wait_readwake(event_handle handles[], int timeout);
  442. void packN(u32_t *dest, u32_t val);
  443. void packn(u16_t *dest, u16_t val);
  444. u32_t unpackN(u32_t *src);
  445. u16_t unpackn(u16_t *src);
  446. #if OSX
  447. void set_nosigpipe(sockfd s);
  448. #else
  449. #define set_nosigpipe(s)
  450. #endif
  451. #if SUN
  452. void init_daemonize(void);
  453. int daemon(int,int);
  454. #endif
  455. #if WIN
  456. void winsock_init(void);
  457. void winsock_close(void);
  458. void *dlopen(const char *filename, int flag);
  459. void *dlsym(void *handle, const char *symbol);
  460. char *dlerror(void);
  461. int poll(struct pollfd *fds, unsigned long numfds, int timeout);
  462. #endif
  463. #if LINUX || FREEBSD
  464. void touch_memory(u8_t *buf, size_t size);
  465. #endif
  466. // buffer.c
  467. struct buffer {
  468. u8_t *buf;
  469. u8_t *readp;
  470. u8_t *writep;
  471. u8_t *wrap;
  472. size_t size;
  473. size_t base_size;
  474. size_t true_size;
  475. mutex_type mutex;
  476. };
  477. // _* called with mutex locked
  478. unsigned _buf_used(struct buffer *buf);
  479. unsigned _buf_space(struct buffer *buf);
  480. unsigned _buf_cont_read(struct buffer *buf);
  481. unsigned _buf_cont_write(struct buffer *buf);
  482. void _buf_inc_readp(struct buffer *buf, unsigned by);
  483. void _buf_inc_writep(struct buffer *buf, unsigned by);
  484. void buf_flush(struct buffer *buf);
  485. void _buf_flush(struct buffer *buf);
  486. void _buf_unwrap(struct buffer *buf, size_t cont);
  487. void buf_adjust(struct buffer *buf, size_t mod);
  488. void _buf_resize(struct buffer *buf, size_t size);
  489. size_t _buf_limit(struct buffer *buf, size_t limit);
  490. void buf_init(struct buffer *buf, size_t size);
  491. void buf_destroy(struct buffer *buf);
  492. // slimproto.c
  493. void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate);
  494. void slimproto_stop(void);
  495. void wake_controller(void);
  496. void slimproto_send_packet(u8_t *packet, size_t len);
  497. #define send_packet(p, s) slimproto_send_packet(p,s)
  498. // stream.c
  499. typedef enum { STOPPED = 0, DISCONNECT, STREAMING_WAIT,
  500. STREAMING_BUFFERING, STREAMING_FILE, STREAMING_HTTP, SEND_HEADERS, RECV_HEADERS } stream_state;
  501. typedef enum { DISCONNECT_OK = 0, LOCAL_DISCONNECT = 1, REMOTE_DISCONNECT = 2, UNREACHABLE = 3, TIMEOUT = 4 } disconnect_code;
  502. struct streamstate {
  503. stream_state state;
  504. disconnect_code disconnect;
  505. char *header;
  506. size_t header_len;
  507. bool sent_headers;
  508. bool cont_wait;
  509. u64_t bytes;
  510. unsigned threshold;
  511. u32_t meta_interval;
  512. u32_t meta_next;
  513. u32_t meta_left;
  514. bool meta_send;
  515. };
  516. void stream_init(log_level level, unsigned stream_buf_size);
  517. void stream_close(void);
  518. void stream_file(const char *header, size_t header_len, unsigned threshold);
  519. void stream_sock(u32_t ip, u16_t port, bool use_ssl, bool use_ogg, const char *header, size_t header_len, unsigned threshold, bool cont_wait);
  520. bool stream_disconnect(void);
  521. // decode.c
  522. typedef enum { DECODE_STOPPED = 0, DECODE_READY, DECODE_RUNNING, DECODE_COMPLETE, DECODE_ERROR } decode_state;
  523. struct decodestate {
  524. decode_state state;
  525. bool new_stream;
  526. mutex_type mutex;
  527. #if PROCESS
  528. bool direct;
  529. bool process;
  530. #endif
  531. };
  532. #if PROCESS
  533. struct processstate {
  534. u8_t *inbuf, *outbuf;
  535. unsigned max_in_frames, max_out_frames;
  536. unsigned in_frames, out_frames;
  537. unsigned in_sample_rate, out_sample_rate;
  538. unsigned long total_in, total_out;
  539. };
  540. #endif
  541. struct codec {
  542. char id;
  543. char *types;
  544. unsigned min_read_bytes;
  545. unsigned min_space;
  546. void (*open)(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);
  547. void (*close)(void);
  548. decode_state (*decode)(void);
  549. };
  550. void decode_init(log_level level, const char *include_codecs, const char *exclude_codecs);
  551. void decode_close(void);
  552. void decode_flush(bool close);
  553. unsigned decode_newstream(unsigned sample_rate, unsigned supported_rates[]);
  554. void codec_open(u8_t format, u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);
  555. #if PROCESS
  556. // process.c
  557. void process_samples(void);
  558. void process_drain(void);
  559. void process_flush(void);
  560. unsigned process_newstream(bool *direct, unsigned raw_sample_rate, unsigned supported_rates[]);
  561. void process_init(char *opt);
  562. #endif
  563. #if RESAMPLE || RESAMPLE16
  564. // resample.c
  565. void resample_samples(struct processstate *process);
  566. bool resample_drain(struct processstate *process);
  567. bool resample_newstream(struct processstate *process, unsigned raw_sample_rate, unsigned supported_rates[]);
  568. void resample_flush(void);
  569. bool resample_init(char *opt);
  570. #endif
  571. // output.c output_alsa.c output_pa.c output_pack.c
  572. typedef enum { OUTPUT_OFF = -1, OUTPUT_STOPPED = 0, OUTPUT_BUFFER, OUTPUT_RUNNING,
  573. OUTPUT_PAUSE_FRAMES, OUTPUT_SKIP_FRAMES, OUTPUT_START_AT } output_state;
  574. #if DSD
  575. 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;
  576. typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE, U8, U16_LE, U16_BE, U32_LE, U32_BE } output_format;
  577. #else
  578. typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE, S24_BE, S24_3BE, S16_BE, S8_BE } output_format;
  579. #endif
  580. typedef enum { FADE_INACTIVE = 0, FADE_DUE, FADE_ACTIVE } fade_state;
  581. typedef enum { FADE_UP = 1, FADE_DOWN, FADE_CROSS } fade_dir;
  582. typedef enum { FADE_NONE = 0, FADE_CROSSFADE, FADE_IN, FADE_OUT, FADE_INOUT } fade_mode;
  583. #define MONO_RIGHT 0x02
  584. #define MONO_LEFT 0x01
  585. #define MAX_SUPPORTED_SAMPLERATES 18
  586. #define TEST_RATES = { 768000, 705600, 384000, 352800, 192000, 176400, 96000, 88200, 48000, 44100, 32000, 24000, 22500, 16000, 12000, 11025, 8000, 0 }
  587. struct outputstate {
  588. output_state state;
  589. output_format format;
  590. u8_t channels;
  591. const char *device;
  592. int external;
  593. #if ALSA
  594. unsigned buffer;
  595. unsigned period;
  596. #endif
  597. bool track_started;
  598. #if PORTAUDIO
  599. bool pa_reopen;
  600. unsigned latency;
  601. int pa_hostapi_option;
  602. #endif
  603. 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);
  604. unsigned start_frames;
  605. unsigned frames_played;
  606. unsigned frames_played_dmp;// frames played at the point delay is measured
  607. unsigned current_sample_rate;
  608. unsigned supported_rates[MAX_SUPPORTED_SAMPLERATES]; // ordered largest first so [0] is max_rate
  609. unsigned default_sample_rate;
  610. bool error_opening;
  611. unsigned device_frames;
  612. unsigned frames_in_process;
  613. u32_t updated;
  614. u32_t track_start_time;
  615. u32_t current_replay_gain;
  616. union {
  617. u32_t pause_frames;
  618. u32_t skip_frames;
  619. u32_t start_at;
  620. };
  621. unsigned next_sample_rate; // set in decode thread
  622. u8_t *track_start; // set in decode thread
  623. u32_t gainL; // set by slimproto
  624. u32_t gainR; // set by slimproto
  625. bool invert; // set by slimproto
  626. u32_t next_replay_gain; // set by slimproto
  627. unsigned threshold; // set by slimproto
  628. fade_state fade;
  629. u8_t *fade_start;
  630. u8_t *fade_end;
  631. fade_dir fade_dir;
  632. fade_mode fade_mode; // set by slimproto
  633. unsigned fade_secs; // set by slimproto
  634. unsigned rate_delay;
  635. bool delay_active;
  636. u32_t stop_time;
  637. u32_t idle_to;
  638. #if DSD
  639. dsd_format next_fmt; // set in decode thread
  640. dsd_format outfmt;
  641. dsd_format dsdfmt; // set in dsd_init - output for DSD: DOP, DSD_U8, ...
  642. unsigned dsd_delay; // set in dsd_init - delay in ms switching to/from dop
  643. #endif
  644. };
  645. void output_init_common(log_level level, const char *device, unsigned output_buf_size, unsigned rates[], unsigned idle);
  646. void output_close_common(void);
  647. void output_flush(void);
  648. bool output_flush_streaming(void);
  649. // _* called with mutex locked
  650. frames_t _output_frames(frames_t avail);
  651. void _checkfade(bool);
  652. // output_alsa.c
  653. #if ALSA
  654. void list_devices(void);
  655. void list_mixers(const char *output_device);
  656. void set_volume(unsigned left, unsigned right);
  657. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  658. 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);
  659. void output_close_alsa(void);
  660. #endif
  661. // output_pa.c
  662. #if PORTAUDIO
  663. void list_devices(void);
  664. void set_volume(unsigned left, unsigned right);
  665. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  666. void output_init_pa(log_level level, const char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
  667. void output_close_pa(void);
  668. void _pa_open(void);
  669. #endif
  670. // output_embedded.c
  671. #if EMBEDDED
  672. void set_volume(unsigned left, unsigned right);
  673. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  674. void output_init_embedded(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
  675. void output_close_embedded(void);
  676. #else
  677. // output_stdout.c
  678. void output_init_stdout(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay);
  679. void output_close_stdout(void);
  680. #endif
  681. // output_pack.c
  682. 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);
  683. void _apply_cross(struct buffer *outputbuf, frames_t out_frames, s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  684. void _apply_gain(struct buffer *outputbuf, frames_t count, s32_t gainL, s32_t gainR, u8_t flags);
  685. s32_t gain(s32_t gain, s32_t sample);
  686. s32_t to_gain(float f);
  687. // output_vis.c
  688. #if VISEXPORT
  689. void _vis_export(struct buffer *outputbuf, struct outputstate *output, frames_t out_frames, bool silence);
  690. void output_vis_init(log_level level, u8_t *mac);
  691. void vis_stop(void);
  692. #else
  693. #define _vis_export(...)
  694. #define vis_stop()
  695. #endif
  696. // dop.c
  697. #if DSD
  698. bool is_stream_dop(u8_t *lptr, u8_t *rptr, int step, frames_t frames);
  699. void update_dop(u32_t *ptr, frames_t frames, bool invert);
  700. void dsd_silence_frames(u32_t *ptr, frames_t frames);
  701. void dsd_invert(u32_t *ptr, frames_t frames);
  702. void dsd_init(dsd_format format, unsigned delay);
  703. #endif
  704. // codecs
  705. #define MAX_CODECS 9
  706. struct codec *register_flac(void);
  707. struct codec *register_pcm(void);
  708. struct codec *register_mad(void);
  709. struct codec *register_mpg(void);
  710. struct codec *register_vorbis(void);
  711. struct codec *register_faad(void);
  712. struct codec *register_helixaac(void);
  713. struct codec *register_dsd(void);
  714. struct codec *register_alac(void);
  715. struct codec *register_ff(const char *codec);
  716. struct codec *register_opus(void);
  717. //gpio.c
  718. #if GPIO
  719. void relay( int state);
  720. void relay_script(int state);
  721. int gpio_pin;
  722. bool gpio_active_low;
  723. bool gpio_active;
  724. char *power_script;
  725. // my amp state
  726. int ampstate;
  727. #endif
  728. // ir.c
  729. #if IR
  730. struct irstate {
  731. mutex_type mutex;
  732. u32_t code;
  733. u32_t ts;
  734. };
  735. void ir_init(log_level level, char *lircrc);
  736. void ir_close(void);
  737. #endif
  738. // sslsym.c
  739. #if USE_SSL && !LINKALL && !NO_SSLSYM
  740. bool load_ssl_symbols(void);
  741. void free_ssl_symbols(void);
  742. bool ssl_loaded;
  743. #endif