squeezelite.h 22 KB

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