squeezelite.h 21 KB

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