2
0

squeezelite.h 23 KB

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