squeezelite.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Additions (c) Paul Hermann, 2015-2017 under the same license terms
  21. * -Control of Raspberry pi GPIO for amplifier power
  22. * -Launch script on power status change from LMS
  23. */
  24. // make may define: PORTAUDIO, SELFPIPE, RESAMPLE, RESAMPLE_MP, VISEXPORT, GPIO, IR, DSD, LINKALL to influence build
  25. #define MAJOR_VERSION "1.9"
  26. #define MINOR_VERSION "2"
  27. #define MICRO_VERSION "1145"
  28. #if defined(CUSTOM_VERSION)
  29. #define VERSION "v" MAJOR_VERSION "." MINOR_VERSION "-" MICRO_VERSION STR(CUSTOM_VERSION)
  30. #else
  31. #define VERSION "v" MAJOR_VERSION "." MINOR_VERSION "-" MICRO_VERSION
  32. #endif
  33. #if !defined(MODEL_NAME)
  34. #define MODEL_NAME SqueezeLite
  35. #endif
  36. #define QUOTE(name) #name
  37. #define STR(macro) QUOTE(macro)
  38. #define MODEL_NAME_STRING STR(MODEL_NAME)
  39. #if defined(EMBEDDED)
  40. #define POSIX 1
  41. #include "embedded.h"
  42. #endif
  43. // build detection
  44. #if defined(linux)
  45. #define LINUX 1
  46. #define OSX 0
  47. #define WIN 0
  48. #define FREEBSD 0
  49. #elif defined (__APPLE__)
  50. #define LINUX 0
  51. #define OSX 1
  52. #define WIN 0
  53. #define FREEBSD 0
  54. #elif defined (_MSC_VER)
  55. #define LINUX 0
  56. #define OSX 0
  57. #define WIN 1
  58. #define FREEBSD 0
  59. #elif defined(__FreeBSD__)
  60. #define LINUX 0
  61. #define OSX 0
  62. #define WIN 0
  63. #define FREEBSD 1
  64. #elif defined (__sun)
  65. #define SUN 1
  66. #define LINUX 1
  67. #define PORTAUDIO 1
  68. #define PA18API 1
  69. #define OSX 0
  70. #define WIN 0
  71. #elif defined (POSIX)
  72. #undef POSIX
  73. #define POSIX 1
  74. #else
  75. #error unknown target
  76. #endif
  77. #if defined(CONFIG_DACAUDIO)
  78. #undef CONFIG_DACAUDIO
  79. #define CONFIG_DACAUDIO 1
  80. #elif defined(CONFIG_BTAUDIO)
  81. #undef CONFIG_BTAUDIO
  82. #define CONFIG_BTAUDIO 1
  83. #elif LINUX && !defined(PORTAUDIO)
  84. #define ALSA 1
  85. #define PORTAUDIO 0
  86. #else
  87. #define ALSA 0
  88. #define PORTAUDIO 1
  89. #endif
  90. #if !defined(LOOPBACK)
  91. #if SUN
  92. #define EVENTFD 0
  93. #define WINEVENT 0
  94. #define SELFPIPE 1
  95. #elif LINUX && !defined(SELFPIPE)
  96. #define EVENTFD 1
  97. #define SELFPIPE 0
  98. #define WINEVENT 0
  99. #endif
  100. #if (LINUX && !EVENTFD) || OSX || FREEBSD
  101. #define EVENTFD 0
  102. #define SELFPIPE 1
  103. #define WINEVENT 0
  104. #endif
  105. #if WIN
  106. #define EVENTFD 0
  107. #define SELFPIPE 0
  108. #define WINEVENT 1
  109. #endif
  110. #else
  111. #define EVENTFD 0
  112. #define SELFPIPE 0
  113. #define WINEVENT 0
  114. #undef LOOPBACK
  115. #define LOOPBACK 1
  116. #endif
  117. #if defined(RESAMPLE) || defined(RESAMPLE_MP)
  118. #undef RESAMPLE
  119. #define RESAMPLE 1 // resampling
  120. #define PROCESS 1 // any sample processing (only resampling at present)
  121. #else
  122. #define RESAMPLE 0
  123. #define PROCESS 0
  124. #endif
  125. #if defined(RESAMPLE_MP)
  126. #undef RESAMPLE_MP
  127. #define RESAMPLE_MP 1
  128. #else
  129. #define RESAMPLE_MP 0
  130. #endif
  131. #if defined(FFMPEG)
  132. #undef FFMPEG
  133. #define FFMPEG 1
  134. #else
  135. #define FFMPEG 0
  136. #endif
  137. #if (LINUX || OSX) && defined(VISEXPORT)
  138. #undef VISEXPORT
  139. #define VISEXPORT 1 // visulizer export support uses linux shared memory
  140. #else
  141. #define VISEXPORT 0
  142. #endif
  143. #if LINUX && defined(IR)
  144. #undef IR
  145. #define IR 1
  146. #else
  147. #define IR 0
  148. #endif
  149. #if defined(DSD)
  150. #undef DSD
  151. #define DSD 1
  152. #define IF_DSD(x) { x }
  153. #else
  154. #undef DSD
  155. #define DSD 0
  156. #define IF_DSD(x)
  157. #endif
  158. #if defined(LINKALL)
  159. #undef LINKALL
  160. #define LINKALL 1 // link all libraries at build time - requires all to be available at run time
  161. #else
  162. #define LINKALL 0
  163. #endif
  164. #if defined (USE_SSL)
  165. #undef USE_SSL
  166. #define USE_SSL 1
  167. #else
  168. #define USE_SSL 0
  169. #endif
  170. #if !LINKALL
  171. // dynamically loaded libraries at run time
  172. #if LINUX
  173. #define LIBFLAC "libFLAC.so.8"
  174. #define LIBMAD "libmad.so.0"
  175. #define LIBMPG "libmpg123.so.0"
  176. #define LIBVORBIS "libvorbisfile.so.3"
  177. #define LIBTREMOR "libvorbisidec.so.1"
  178. #define LIBFAAD "libfaad.so.2"
  179. #define LIBAVUTIL "libavutil.so.%d"
  180. #define LIBAVCODEC "libavcodec.so.%d"
  181. #define LIBAVFORMAT "libavformat.so.%d"
  182. #define LIBSOXR "libsoxr.so.0"
  183. #define LIBLIRC "liblirc_client.so.0"
  184. #endif
  185. #if OSX
  186. #define LIBFLAC "libFLAC.8.dylib"
  187. #define LIBMAD "libmad.0.dylib"
  188. #define LIBMPG "libmpg123.0.dylib"
  189. #define LIBVORBIS "libvorbisfile.3.dylib"
  190. #define LIBTREMOR "libvorbisidec.1.dylib"
  191. #define LIBFAAD "libfaad.2.dylib"
  192. #define LIBAVUTIL "libavutil.%d.dylib"
  193. #define LIBAVCODEC "libavcodec.%d.dylib"
  194. #define LIBAVFORMAT "libavformat.%d.dylib"
  195. #define LIBSOXR "libsoxr.0.dylib"
  196. #endif
  197. #if WIN
  198. #define LIBFLAC "libFLAC.dll"
  199. #define LIBMAD "libmad-0.dll"
  200. #define LIBMPG "libmpg123-0.dll"
  201. #define LIBVORBIS "libvorbisfile.dll"
  202. #define LIBTREMOR "libvorbisidec.dll"
  203. #define LIBFAAD "libfaad2.dll"
  204. #define LIBAVUTIL "avutil-%d.dll"
  205. #define LIBAVCODEC "avcodec-%d.dll"
  206. #define LIBAVFORMAT "avformat-%d.dll"
  207. #define LIBSOXR "libsoxr.dll"
  208. #endif
  209. #if FREEBSD
  210. #define LIBFLAC "libFLAC.so.11"
  211. #define LIBMAD "libmad.so.2"
  212. #define LIBMPG "libmpg123.so.0"
  213. #define LIBVORBIS "libvorbisfile.so.6"
  214. #define LIBTREMOR "libvorbisidec.so.1"
  215. #define LIBFAAD "libfaad.so.2"
  216. #define LIBAVUTIL "libavutil.so.%d"
  217. #define LIBAVCODEC "libavcodec.so.%d"
  218. #define LIBAVFORMAT "libavformat.so.%d"
  219. #endif
  220. #endif // !LINKALL
  221. // config options
  222. #define STREAMBUF_SIZE (2 * 1024 * 1024)
  223. #define OUTPUTBUF_SIZE (44100 * 8 * 10)
  224. #define OUTPUTBUF_SIZE_CROSSFADE (OUTPUTBUF_SIZE * 12 / 10)
  225. #define MAX_HEADER 4096 // do not reduce as icy-meta max is 4080
  226. #if ALSA
  227. #define ALSA_BUFFER_TIME 40
  228. #define ALSA_PERIOD_COUNT 4
  229. #define OUTPUT_RT_PRIORITY 45
  230. #endif
  231. #define SL_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  232. #if SUN || OSXPPC
  233. #undef SL_LITTLE_ENDIAN
  234. #endif
  235. #include <stdio.h>
  236. #include <stdlib.h>
  237. #include <stdarg.h>
  238. #include <string.h>
  239. #include <errno.h>
  240. #include <limits.h>
  241. #include <sys/types.h>
  242. #if LINUX || OSX || FREEBSD || POSIX
  243. #include <unistd.h>
  244. #include <stdbool.h>
  245. #include <netinet/in.h>
  246. #include <arpa/inet.h>
  247. #include <sys/time.h>
  248. #include <sys/socket.h>
  249. #if POSIX
  250. #include <sys/poll.h>
  251. #else
  252. #include <poll.h>
  253. #endif
  254. #if !LINKALL
  255. #include <dlfcn.h>
  256. #endif
  257. #include <pthread.h>
  258. #include <signal.h>
  259. #if SUN
  260. #include <sys/types.h>
  261. #endif /* SUN */
  262. #ifndef PTHREAD_STACK_MIN
  263. #define PTHREAD_STACK_MIN 256
  264. #endif
  265. #define STREAM_THREAD_STACK_SIZE 8 * 1024
  266. #define DECODE_THREAD_STACK_SIZE 20 * 1024
  267. #define OUTPUT_THREAD_STACK_SIZE 8 * 1024
  268. #define IR_THREAD_STACK_SIZE 8 * 1024
  269. #if !OSX
  270. #define thread_t pthread_t;
  271. #endif
  272. #define closesocket(s) close(s)
  273. #define last_error() errno
  274. #define ERROR_WOULDBLOCK EWOULDBLOCK
  275. #ifdef SUN
  276. typedef uint8_t u8_t;
  277. typedef uint16_t u16_t;
  278. typedef uint32_t u32_t;
  279. typedef uint64_t u64_t;
  280. #elif POSIX
  281. typedef unsigned long long u64_t;
  282. #else
  283. typedef u_int8_t u8_t;
  284. typedef u_int16_t u16_t;
  285. typedef u_int32_t u32_t;
  286. typedef u_int64_t u64_t;
  287. #endif /* SUN */
  288. typedef int16_t s16_t;
  289. typedef int32_t s32_t;
  290. typedef int64_t s64_t;
  291. #define mutex_type pthread_mutex_t
  292. #define mutex_create(m) pthread_mutex_init(&m, NULL)
  293. #if POSIX
  294. #define mutex_create_p(m) mutex_create(m)
  295. #else
  296. #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)
  297. #endif
  298. #define mutex_lock(m) pthread_mutex_lock(&m)
  299. #define mutex_unlock(m) pthread_mutex_unlock(&m)
  300. #define mutex_destroy(m) pthread_mutex_destroy(&m)
  301. #define thread_type pthread_t
  302. #endif
  303. #if WIN
  304. #include <winsock2.h>
  305. #include <ws2tcpip.h>
  306. #include <io.h>
  307. #define STREAM_THREAD_STACK_SIZE (1024 * 64)
  308. #define DECODE_THREAD_STACK_SIZE (1024 * 128)
  309. #define OUTPUT_THREAD_STACK_SIZE (1024 * 64)
  310. typedef unsigned __int8 u8_t;
  311. typedef unsigned __int16 u16_t;
  312. typedef unsigned __int32 u32_t;
  313. typedef unsigned __int64 u64_t;
  314. typedef __int16 s16_t;
  315. typedef __int32 s32_t;
  316. typedef __int64 s64_t;
  317. typedef BOOL bool;
  318. #define true TRUE
  319. #define false FALSE
  320. #define inline __inline
  321. #define mutex_type HANDLE
  322. #define mutex_create(m) m = CreateMutex(NULL, FALSE, NULL)
  323. #define mutex_create_p mutex_create
  324. #define mutex_lock(m) WaitForSingleObject(m, INFINITE)
  325. #define mutex_unlock(m) ReleaseMutex(m)
  326. #define mutex_destroy(m) CloseHandle(m)
  327. #define thread_type HANDLE
  328. #define usleep(x) Sleep(x/1000)
  329. #define sleep(x) Sleep(x*1000)
  330. #define last_error() WSAGetLastError()
  331. #define ERROR_WOULDBLOCK WSAEWOULDBLOCK
  332. #define open _open
  333. #define read _read
  334. #define snprintf _snprintf
  335. #define in_addr_t u32_t
  336. #define socklen_t int
  337. #define ssize_t int
  338. #define RTLD_NOW 0
  339. #endif
  340. #if !defined(MSG_NOSIGNAL)
  341. #define MSG_NOSIGNAL 0
  342. #endif
  343. typedef u32_t frames_t;
  344. typedef int sockfd;
  345. #if EVENTFD
  346. #include <sys/eventfd.h>
  347. #define event_event int
  348. #define event_handle struct pollfd
  349. #define wake_create(e) e = eventfd(0, 0)
  350. #define wake_signal(e) eventfd_write(e, 1)
  351. #define wake_clear(e) eventfd_t val; eventfd_read(e, &val)
  352. #define wake_close(e) close(e)
  353. #endif
  354. #if SELFPIPE
  355. #define event_handle struct pollfd
  356. #define event_event struct wake
  357. #define wake_create(e) pipe(e.fds); set_nonblock(e.fds[0]); set_nonblock(e.fds[1])
  358. #define wake_signal(e) write(e.fds[1], ".", 1)
  359. #define wake_clear(e) char c[10]; read(e, &c, 10)
  360. #define wake_close(e) close(e.fds[0]); close(e.fds[1])
  361. struct wake {
  362. int fds[2];
  363. };
  364. #endif
  365. #if LOOPBACK
  366. #define event_handle struct pollfd
  367. #define event_event struct wake
  368. #define wake_create(e) _wake_create(&e)
  369. #define wake_signal(e) send(e.fds[1], ".", 1, 0)
  370. #define wake_clear(e) char c; recv(e, &c, 1, 0)
  371. #define wake_close(e) closesocket(e.mfds); closesocket(e.fds[0]); closesocket(e.fds[1])
  372. struct wake {
  373. int mfds;
  374. int fds[2];
  375. };
  376. void _wake_create(event_event*);
  377. #endif
  378. #if WINEVENT
  379. #define event_event HANDLE
  380. #define event_handle HANDLE
  381. #define wake_create(e) e = CreateEvent(NULL, FALSE, FALSE, NULL)
  382. #define wake_signal(e) SetEvent(e)
  383. #define wake_close(e) CloseHandle(e)
  384. #endif
  385. // printf/scanf formats for u64_t
  386. #if (LINUX && __WORDSIZE == 64) || (FREEBSD && __LP64__)
  387. #define FMT_u64 "%lu"
  388. #define FMT_x64 "%lx"
  389. #elif __GLIBC_HAVE_LONG_LONG || defined __GNUC__ || WIN || SUN
  390. #define FMT_u64 "%llu"
  391. #define FMT_x64 "%llx"
  392. #else
  393. #error can not support u64_t
  394. #endif
  395. #define MAX_SILENCE_FRAMES 2048
  396. #define FIXED_ONE 0x10000
  397. #ifndef BYTES_PER_FRAME
  398. #define BYTES_PER_FRAME 8
  399. #endif
  400. #if BYTES_PER_FRAME == 8
  401. #define ISAMPLE_T s32_t
  402. #else
  403. #define ISAMPLE_T s16_t
  404. #endif
  405. #define min(a,b) (((a) < (b)) ? (a) : (b))
  406. // logging
  407. typedef enum { lERROR = 0, lWARN, lINFO, lDEBUG, lSDEBUG } log_level;
  408. const char *logtime(void);
  409. void logprint(const char *fmt, ...);
  410. #define LOG_ERROR(fmt, ...) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  411. #define LOG_WARN(fmt, ...) if (loglevel >= lWARN) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  412. #define LOG_INFO(fmt, ...) if (loglevel >= lINFO) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  413. #define LOG_DEBUG(fmt, ...) if (loglevel >= lDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  414. #define LOG_SDEBUG(fmt, ...) if (loglevel >= lSDEBUG) logprint("%s %s:%d " fmt "\n", logtime(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  415. static inline void DEBUG_LOG_TIMED(uint32_t delayms, char * strFmt, ...)
  416. {
  417. static log_level loglevel;
  418. va_list args;
  419. va_start(args, strFmt);
  420. static uint32_t nextDebugLog=0;
  421. if(esp_timer_get_time()>nextDebugLog)
  422. {
  423. if (loglevel >= lDEBUG)
  424. {
  425. logprint("%s %s:%d ", logtime(), __FUNCTION__, __LINE__);
  426. logprint(strFmt , args);
  427. logprint("\n");
  428. }
  429. nextDebugLog=esp_timer_get_time()+delayms*1000;
  430. }
  431. }
  432. // utils.c (non logging)
  433. typedef enum { EVENT_TIMEOUT = 0, EVENT_READ, EVENT_WAKE } event_type;
  434. #if WIN && USE_SSL
  435. char* strcasestr(const char *haystack, const char *needle);
  436. #endif
  437. char *next_param(char *src, char c);
  438. u32_t gettime_ms(void);
  439. void get_mac(u8_t *mac);
  440. void set_nonblock(sockfd s);
  441. int connect_timeout(sockfd sock, const struct sockaddr *addr, socklen_t addrlen, int timeout);
  442. void server_addr(char *server, in_addr_t *ip_ptr, unsigned *port_ptr);
  443. void set_readwake_handles(event_handle handles[], sockfd s, event_event e);
  444. event_type wait_readwake(event_handle handles[], int timeout);
  445. void packN(u32_t *dest, u32_t val);
  446. void packn(u16_t *dest, u16_t val);
  447. u32_t unpackN(u32_t *src);
  448. u16_t unpackn(u16_t *src);
  449. #if OSX
  450. void set_nosigpipe(sockfd s);
  451. #else
  452. #define set_nosigpipe(s)
  453. #endif
  454. #if SUN
  455. void init_daemonize(void);
  456. int daemon(int,int);
  457. #endif
  458. #if WIN
  459. void winsock_init(void);
  460. void winsock_close(void);
  461. void *dlopen(const char *filename, int flag);
  462. void *dlsym(void *handle, const char *symbol);
  463. char *dlerror(void);
  464. int poll(struct pollfd *fds, unsigned long numfds, int timeout);
  465. #endif
  466. #if LINUX || FREEBSD
  467. void touch_memory(u8_t *buf, size_t size);
  468. #endif
  469. // buffer.c
  470. struct buffer {
  471. u8_t *buf;
  472. u8_t *readp;
  473. u8_t *writep;
  474. u8_t *wrap;
  475. size_t size;
  476. size_t base_size;
  477. mutex_type mutex;
  478. };
  479. // _* called with mutex locked
  480. unsigned _buf_used(struct buffer *buf);
  481. unsigned _buf_space(struct buffer *buf);
  482. unsigned _buf_cont_read(struct buffer *buf);
  483. unsigned _buf_cont_write(struct buffer *buf);
  484. void _buf_inc_readp(struct buffer *buf, unsigned by);
  485. void _buf_inc_writep(struct buffer *buf, unsigned by);
  486. void buf_flush(struct buffer *buf);
  487. void buf_adjust(struct buffer *buf, size_t mod);
  488. void _buf_resize(struct buffer *buf, size_t size);
  489. void buf_init(struct buffer *buf, size_t size);
  490. void buf_destroy(struct buffer *buf);
  491. // slimproto.c
  492. void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate);
  493. void slimproto_stop(void);
  494. void wake_controller(void);
  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
  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. #elif CONFIG_DACAUDIO
  575. typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE, S24_BE, S24_3BE, S16_BE, S8_BE } output_format;
  576. #else
  577. typedef enum { S32_LE, S24_LE, S24_3LE, S16_LE } output_format;
  578. #endif
  579. extern uint8_t get_bytes_per_frame(output_format fmt);
  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 MAX_SUPPORTED_SAMPLERATES 18
  584. #define TEST_RATES = { 768000, 705600, 384000, 352800, 192000, 176400, 96000, 88200, 48000, 44100, 32000, 24000, 22500, 16000, 12000, 11025, 8000, 0 }
  585. struct outputstate {
  586. output_state state;
  587. output_format format;
  588. const char *device;
  589. #if ALSA
  590. unsigned buffer;
  591. unsigned period;
  592. #endif
  593. bool track_started;
  594. #if PORTAUDIO
  595. bool pa_reopen;
  596. unsigned latency;
  597. int pa_hostapi_option;
  598. #endif
  599. 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);
  600. unsigned start_frames;
  601. unsigned frames_played;
  602. unsigned frames_played_dmp;// frames played at the point delay is measured
  603. unsigned current_sample_rate;
  604. unsigned supported_rates[MAX_SUPPORTED_SAMPLERATES]; // ordered largest first so [0] is max_rate
  605. unsigned default_sample_rate;
  606. bool error_opening;
  607. unsigned device_frames;
  608. u32_t updated;
  609. u32_t track_start_time;
  610. u32_t current_replay_gain;
  611. union {
  612. u32_t pause_frames;
  613. u32_t skip_frames;
  614. u32_t start_at;
  615. };
  616. unsigned next_sample_rate; // set in decode thread
  617. u8_t *track_start; // set in decode thread
  618. u32_t gainL; // set by slimproto
  619. u32_t gainR; // set by slimproto
  620. bool invert; // set by slimproto
  621. u32_t next_replay_gain; // set by slimproto
  622. unsigned threshold; // set by slimproto
  623. fade_state fade;
  624. u8_t *fade_start;
  625. u8_t *fade_end;
  626. fade_dir fade_dir;
  627. fade_mode fade_mode; // set by slimproto
  628. unsigned fade_secs; // set by slimproto
  629. unsigned rate_delay;
  630. bool delay_active;
  631. u32_t stop_time;
  632. u32_t idle_to;
  633. #if DSD
  634. dsd_format next_fmt; // set in decode thread
  635. dsd_format outfmt;
  636. dsd_format dsdfmt; // set in dsd_init - output for DSD: DOP, DSD_U8, ...
  637. unsigned dsd_delay; // set in dsd_init - delay in ms switching to/from dop
  638. #endif
  639. };
  640. void output_init_common(log_level level, const char *device, unsigned output_buf_size, unsigned rates[], unsigned idle);
  641. void output_close_common(void);
  642. void output_flush(void);
  643. // _* called with mutex locked
  644. frames_t _output_frames(frames_t avail);
  645. void _checkfade(bool);
  646. // output_alsa.c
  647. #if ALSA
  648. void list_devices(void);
  649. void list_mixers(const char *output_device);
  650. void set_volume(unsigned left, unsigned right);
  651. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  652. 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);
  653. void output_close_alsa(void);
  654. #endif
  655. // output_pa.c
  656. #if PORTAUDIO
  657. void list_devices(void);
  658. void set_volume(unsigned left, unsigned right);
  659. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  660. void output_init_pa(log_level level, const char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
  661. void output_close_pa(void);
  662. void _pa_open(void);
  663. #endif
  664. // output_dac.c
  665. #if CONFIG_DACAUDIO
  666. void set_volume_dac(unsigned left, unsigned right);
  667. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  668. void output_init_dac(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
  669. void output_close_dac(void);
  670. void hal_bluetooth_init(log_level loglevel);
  671. #endif
  672. //output_bt.c
  673. #if CONFIG_BTAUDIO
  674. void set_volume_bt(unsigned left, unsigned right);
  675. bool test_open(const char *device, unsigned rates[], bool userdef_rates);
  676. void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle);
  677. void output_close_bt(void);
  678. void hal_bluetooth_init(log_level loglevel);
  679. #endif
  680. // output_stdout.c
  681. void output_init_stdout(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay);
  682. void output_close_stdout(void);
  683. // output_pack.c
  684. void _scale_and_pack_frames(void *outputptr, s32_t *inputptr, frames_t cnt, s32_t gainL, s32_t gainR, output_format format);
  685. void _apply_cross(struct buffer *outputbuf, frames_t out_frames, s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  686. void _apply_gain(struct buffer *outputbuf, frames_t count, s32_t gainL, s32_t gainR);
  687. s32_t gain(s32_t gain, s32_t sample);
  688. s32_t to_gain(float f);
  689. // output_vis.c
  690. #if VISEXPORT
  691. void _vis_export(struct buffer *outputbuf, struct outputstate *output, frames_t out_frames, bool silence);
  692. void output_vis_init(log_level level, u8_t *mac);
  693. void vis_stop(void);
  694. #else
  695. #define _vis_export(...)
  696. #define vis_stop()
  697. #endif
  698. // dop.c
  699. #if DSD
  700. bool is_stream_dop(u8_t *lptr, u8_t *rptr, int step, frames_t frames);
  701. void update_dop(u32_t *ptr, frames_t frames, bool invert);
  702. void dsd_silence_frames(u32_t *ptr, frames_t frames);
  703. void dsd_invert(u32_t *ptr, frames_t frames);
  704. void dsd_init(dsd_format format, unsigned delay);
  705. #endif
  706. // codecs
  707. #define MAX_CODECS 9
  708. struct codec *register_flac(void);
  709. struct codec *register_pcm(void);
  710. struct codec *register_mad(void);
  711. struct codec *register_mpg(void);
  712. struct codec *register_vorbis(void);
  713. struct codec *register_faad(void);
  714. struct codec *register_helixaac(void);
  715. struct codec *register_dsd(void);
  716. struct codec *register_alac(void);
  717. struct codec *register_ff(const char *codec);
  718. //gpio.c
  719. #if GPIO
  720. void relay( int state);
  721. void relay_script(int state);
  722. int gpio_pin;
  723. bool gpio_active_low;
  724. bool gpio_active;
  725. char *power_script;
  726. // my amp state
  727. int ampstate;
  728. #endif
  729. // ir.c
  730. #if IR
  731. struct irstate {
  732. mutex_type mutex;
  733. u32_t code;
  734. u32_t ts;
  735. };
  736. void ir_init(log_level level, char *lircrc);
  737. void ir_close(void);
  738. #endif
  739. // sslsym.c
  740. #if USE_SSL && !LINKALL
  741. bool load_ssl_symbols(void);
  742. void free_ssl_symbols(void);
  743. bool ssl_loaded;
  744. #endif