decode_external.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <math.h>
  12. #ifdef ESP_PLATFORM
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/timers.h"
  15. #endif
  16. #include "platform_config.h"
  17. #include "squeezelite.h"
  18. #if CONFIG_BT_SINK
  19. #include "bt_app_sink.h"
  20. static bool enable_bt_sink;
  21. #endif
  22. #if CONFIG_CSPOT_SINK
  23. #include "cspot_sink.h"
  24. static bool enable_cspot;
  25. #endif
  26. #if CONFIG_AIRPLAY_SINK
  27. #include "raop_sink.h"
  28. static bool enable_airplay;
  29. #define RAOP_OUTPUT_SIZE (((RAOP_SAMPLE_RATE * BYTES_PER_FRAME * 2 * 120) / 100) & ~BYTES_PER_FRAME)
  30. #define SYNC_WIN_SLOW 32
  31. #define SYNC_WIN_CHECK 8
  32. #define SYNC_WIN_FAST 2
  33. static raop_event_t raop_state;
  34. static EXT_RAM_ATTR struct {
  35. bool enabled;
  36. int sum, count, win, errors[SYNC_WIN_SLOW];
  37. s32_t len;
  38. u32_t start_time, playtime;
  39. } raop_sync;
  40. #endif
  41. static enum { SINK_RUNNING, SINK_ABORT, SINK_DISCARD } sink_state;
  42. #define LOCK_O mutex_lock(outputbuf->mutex)
  43. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  44. #define LOCK_D mutex_lock(decode.mutex);
  45. #define UNLOCK_D mutex_unlock(decode.mutex);
  46. enum { DECODE_BT = 1, DECODE_RAOP, DECODE_CSPOT };
  47. extern struct outputstate output;
  48. extern struct decodestate decode;
  49. extern struct buffer *outputbuf;
  50. // this is the only system-wide loglevel variable
  51. extern log_level loglevel;
  52. /****************************************************************************************
  53. * Common sink data handler
  54. */
  55. static void sink_data_handler(const uint8_t *data, uint32_t len)
  56. {
  57. size_t bytes, space;
  58. int wait = 10;
  59. // would be better to lock output, but really, it does not matter
  60. if (!output.external) {
  61. LOG_SDEBUG("Cannot use external sink while LMS is controlling player");
  62. return;
  63. }
  64. LOCK_O;
  65. if (sink_state == SINK_ABORT) sink_state = SINK_RUNNING;
  66. // there will always be room at some point
  67. while (len && wait && sink_state == SINK_RUNNING) {
  68. bytes = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / (BYTES_PER_FRAME / 4);
  69. bytes = min(len, bytes);
  70. #if BYTES_PER_FRAME == 4
  71. memcpy(outputbuf->writep, data, bytes);
  72. #else
  73. {
  74. s16_t *iptr = (s16_t*) data;
  75. ISAMPLE_T *optr = (ISAMPLE_T *) outputbuf->writep;
  76. size_t n = bytes / 2;
  77. while (n--) *optr++ = *iptr++ << 16;
  78. }
  79. #endif
  80. _buf_inc_writep(outputbuf, bytes * BYTES_PER_FRAME / 4);
  81. space = _buf_space(outputbuf);
  82. len -= bytes;
  83. data += bytes;
  84. // allow i2s to empty the buffer if needed
  85. if (len && !space) {
  86. if (output.state == OUTPUT_RUNNING) wait--;
  87. UNLOCK_O; usleep(50000); LOCK_O;
  88. }
  89. }
  90. if (!wait) {
  91. // re-align the buffer according to what we throw away
  92. _buf_inc_writep(outputbuf, outputbuf->size - (BYTES_PER_FRAME - (len % BYTES_PER_FRAME)));
  93. LOG_WARN("Waited too long, dropping frames %d", len);
  94. }
  95. UNLOCK_O;
  96. }
  97. /****************************************************************************************
  98. * BT sink command handler
  99. */
  100. #if CONFIG_BT_SINK
  101. static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args)
  102. {
  103. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  104. if (output.external != DECODE_BT && output.state > OUTPUT_STOPPED) {
  105. LOG_WARN("Cannot use BT sink while LMS/AirPlay/CSpot are controlling player %d", output.external);
  106. return false;
  107. }
  108. LOCK_D;
  109. if (cmd != BT_SINK_VOLUME) LOCK_O;
  110. switch(cmd) {
  111. case BT_SINK_AUDIO_STARTED:
  112. _buf_flush(outputbuf);
  113. _buf_limit(outputbuf, 0);
  114. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  115. output.external = DECODE_BT;
  116. output.state = OUTPUT_STOPPED;
  117. output.frames_played = 0;
  118. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  119. LOG_INFO("BT sink started");
  120. break;
  121. case BT_SINK_AUDIO_STOPPED:
  122. if (output.external == DECODE_BT) {
  123. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  124. output.external = 0;
  125. output.stop_time = gettime_ms();
  126. LOG_INFO("BT sink stopped");
  127. }
  128. break;
  129. case BT_SINK_PLAY:
  130. output.state = OUTPUT_RUNNING;
  131. LOG_INFO("BT play");
  132. break;
  133. case BT_SINK_STOP:
  134. _buf_flush(outputbuf);
  135. output.state = OUTPUT_STOPPED;
  136. output.stop_time = gettime_ms();
  137. sink_state = SINK_ABORT;
  138. LOG_INFO("BT stop");
  139. break;
  140. case BT_SINK_PAUSE:
  141. output.stop_time = gettime_ms();
  142. LOG_INFO("BT pause, just silence");
  143. break;
  144. case BT_SINK_RATE:
  145. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  146. LOG_INFO("Setting BT sample rate %u", output.next_sample_rate);
  147. break;
  148. case BT_SINK_VOLUME: {
  149. u32_t volume = va_arg(args, u32_t);
  150. volume = 65536 * powf(volume / 128.0f, 3);
  151. set_volume(volume, volume);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  158. UNLOCK_D;
  159. return true;
  160. }
  161. #endif
  162. /****************************************************************************************
  163. * raop sink data handler
  164. */
  165. #if CONFIG_AIRPLAY_SINK
  166. static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
  167. raop_sync.playtime = playtime;
  168. raop_sync.len = len;
  169. sink_data_handler(data, len);
  170. }
  171. /****************************************************************************************
  172. * AirPlay sink command handler
  173. */
  174. static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
  175. {
  176. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  177. if (output.external != DECODE_RAOP && output.state > OUTPUT_STOPPED) {
  178. LOG_WARN("Cannot use Airplay sink while LMS/BT/CSpot are controlling player %d", output.external);
  179. return false;
  180. }
  181. LOCK_D;
  182. if (event != RAOP_VOLUME) LOCK_O;
  183. // this is async, so player might have been deleted
  184. switch (event) {
  185. case RAOP_TIMING: {
  186. if (!raop_sync.enabled || output.state != OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
  187. u32_t ms, now = gettime_ms();
  188. u32_t level = _buf_used(outputbuf);
  189. int error;
  190. // in how many ms will the most recent block play
  191. ms = (((s32_t)(level - raop_sync.len) / BYTES_PER_FRAME + output.device_frames + output.frames_in_process) * 10) / (RAOP_SAMPLE_RATE / 100) - (s32_t) (now - output.updated);
  192. // when outputbuf is empty, it means we have a network black-out or something
  193. error = level ? (raop_sync.playtime - now) - ms : 0;
  194. if (loglevel == lDEBUG || !level) {
  195. LOG_INFO("head local:%d, remote:%d (delta:%d)", ms, raop_sync.playtime - now, error);
  196. LOG_INFO("obuf:%u, sync_len:%u, devframes:%u, inproc:%u", _buf_used(outputbuf), raop_sync.len, output.device_frames, output.frames_in_process);
  197. }
  198. // calculate sum, error and update sliding window
  199. raop_sync.errors[raop_sync.count++ % raop_sync.win] = error;
  200. raop_sync.sum += error;
  201. error = raop_sync.sum / min(raop_sync.count, raop_sync.win);
  202. // wait till we have enough data or there is a strong deviation
  203. if ((raop_sync.count >= raop_sync.win && abs(error) > 10) || (raop_sync.count >= SYNC_WIN_CHECK && abs(error) > 100)) {
  204. if (error < 0) {
  205. output.skip_frames = -(error * RAOP_SAMPLE_RATE) / 1000;
  206. output.state = OUTPUT_SKIP_FRAMES;
  207. LOG_INFO("skipping %u frames (count:%d)", output.skip_frames, raop_sync.count);
  208. } else {
  209. output.pause_frames = (error * RAOP_SAMPLE_RATE) / 1000;
  210. output.state = OUTPUT_PAUSE_FRAMES;
  211. LOG_INFO("pausing for %u frames (count: %d)", output.pause_frames, raop_sync.count);
  212. }
  213. raop_sync.sum = raop_sync.count = 0;
  214. memset(raop_sync.errors, 0, sizeof(raop_sync.errors));
  215. }
  216. // move to normal mode if possible
  217. if (raop_sync.win == 1) {
  218. raop_sync.win = SYNC_WIN_FAST;
  219. LOG_INFO("backend played %u, desired %u, (delta:%d)", ms, raop_sync.playtime - now, error);
  220. } else if (raop_sync.win == SYNC_WIN_FAST && raop_sync.count >= SYNC_WIN_FAST && abs(error) < 10) {
  221. raop_sync.win = SYNC_WIN_SLOW;
  222. LOG_INFO("switching to slow sync mode %u", raop_sync.win);
  223. }
  224. break;
  225. }
  226. case RAOP_SETUP: {
  227. uint8_t **buffer = va_arg(args, uint8_t**);
  228. size_t *size = va_arg(args, size_t*);
  229. // steal buffer tail from outputbuf but do not reallocate
  230. *size = _buf_limit(outputbuf, RAOP_OUTPUT_SIZE);
  231. *buffer = outputbuf->writep + RAOP_OUTPUT_SIZE;
  232. output.frames_played = 0;
  233. output.external = DECODE_RAOP;
  234. output.state = OUTPUT_STOPPED;
  235. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  236. LOG_INFO("resizing buffer %u", outputbuf->size);
  237. break;
  238. }
  239. case RAOP_STREAM:
  240. LOG_INFO("Stream", NULL);
  241. raop_state = event;
  242. raop_sync.win = 1;
  243. raop_sync.sum = raop_sync.count = 0;
  244. memset(raop_sync.errors, 0, sizeof(raop_sync.errors));
  245. raop_sync.enabled = !strcasestr(output.device, "BT");
  246. output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE;
  247. break;
  248. case RAOP_STOP:
  249. output.external = 0;
  250. __attribute__ ((fallthrough));
  251. case RAOP_FLUSH:
  252. LOG_INFO("%s", event == RAOP_FLUSH ? "Flush" : "Stop");
  253. _buf_flush(outputbuf);
  254. raop_state = event;
  255. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  256. sink_state = SINK_ABORT;
  257. output.frames_played = 0;
  258. output.stop_time = gettime_ms();
  259. break;
  260. case RAOP_PLAY: {
  261. LOG_INFO("Play", NULL);
  262. if (raop_state != RAOP_PLAY) {
  263. output.state = OUTPUT_START_AT;
  264. output.start_at = va_arg(args, u32_t);
  265. raop_sync.start_time = output.start_at;
  266. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  267. }
  268. raop_state = event;
  269. break;
  270. }
  271. case RAOP_VOLUME: {
  272. float volume = va_arg(args, double);
  273. LOG_INFO("Volume[0..1] %0.4f", volume);
  274. volume = 65536 * powf(volume, 3);
  275. set_volume(volume, volume);
  276. break;
  277. }
  278. default:
  279. break;
  280. }
  281. if (event != RAOP_VOLUME) UNLOCK_O;
  282. UNLOCK_D;
  283. return true;
  284. }
  285. #endif
  286. /****************************************************************************************
  287. * cspot sink command handler
  288. */
  289. #if CONFIG_CSPOT_SINK
  290. static bool cspot_cmd_handler(cspot_event_t cmd, va_list args)
  291. {
  292. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  293. if (output.external != DECODE_CSPOT && output.state > OUTPUT_STOPPED) {
  294. LOG_WARN("Cannot use CSpot sink while LMS/BT/Airplay are controlling player %d", output.external);
  295. return false;
  296. }
  297. LOCK_D;
  298. if (cmd != CSPOT_VOLUME) LOCK_O;
  299. switch(cmd) {
  300. case CSPOT_START:
  301. output.current_sample_rate = output.next_sample_rate = va_arg(args, u32_t);
  302. output.external = DECODE_CSPOT;
  303. output.frames_played = 0;
  304. // in 1/10 of seconds
  305. output.threshold = 25;
  306. output.state = OUTPUT_STOPPED;
  307. sink_state = SINK_ABORT;
  308. _buf_flush(outputbuf);
  309. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  310. LOG_INFO("CSpot start track");
  311. break;
  312. case CSPOT_DISC:
  313. _buf_flush(outputbuf);
  314. sink_state = SINK_ABORT;
  315. output.external = 0;
  316. output.state = OUTPUT_STOPPED;
  317. output.stop_time = gettime_ms();
  318. LOG_INFO("CSpot disconnected");
  319. break;
  320. case CSPOT_PLAY:
  321. sink_state = SINK_RUNNING;
  322. output.state = OUTPUT_RUNNING;
  323. LOG_INFO("CSpot play");
  324. break;
  325. case CSPOT_SEEK:
  326. _buf_flush(outputbuf);
  327. sink_state = SINK_ABORT;
  328. LOG_INFO("CSpot seek by %d", va_arg(args, uint32_t));
  329. break;
  330. case CSPOT_FLUSH:
  331. _buf_flush(outputbuf);
  332. sink_state = SINK_DISCARD;
  333. output.state = OUTPUT_STOPPED;
  334. LOG_INFO("CSpot flush");
  335. break;
  336. case CSPOT_PAUSE:
  337. output.state = OUTPUT_STOPPED;
  338. output.stop_time = gettime_ms();
  339. LOG_INFO("CSpot pause");
  340. break;
  341. case CSPOT_TRACK_MARK:
  342. output.track_start = outputbuf->writep;
  343. break;
  344. case CSPOT_QUERY_REMAINING: {
  345. uint32_t *remaining = va_arg(args, uint32_t*);
  346. *remaining = (_buf_used(outputbuf) * 1000) / (output.current_sample_rate * BYTES_PER_FRAME);
  347. break;
  348. }
  349. case CSPOT_QUERY_STARTED: {
  350. uint32_t *started = va_arg(args, uint32_t*);
  351. *started = output.track_started;
  352. // this is a read_and_clear event
  353. output.track_started = false;
  354. break;
  355. }
  356. case CSPOT_VOLUME: {
  357. u32_t volume = va_arg(args, u32_t);
  358. LOG_INFO("CSpot volume %u", volume);
  359. volume = 65536 * powf(volume / 65536.0f, 4);
  360. set_volume(volume, volume);
  361. break;
  362. default:
  363. break;
  364. }
  365. }
  366. if (cmd != CSPOT_VOLUME) UNLOCK_O;
  367. UNLOCK_D;
  368. return true;
  369. }
  370. #endif
  371. /****************************************************************************************
  372. * We provide the generic codec register option
  373. */
  374. void register_external(void) {
  375. char *p;
  376. #if CONFIG_BT_SINK
  377. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_bt_sink")) != NULL) {
  378. enable_bt_sink = !strcmp(p,"1") || !strcasecmp(p,"y");
  379. free(p);
  380. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  381. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  382. }
  383. }
  384. #endif
  385. #if CONFIG_AIRPLAY_SINK
  386. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_airplay")) != NULL) {
  387. enable_airplay = !strcmp(p,"1") || !strcasecmp(p,"y");
  388. free(p);
  389. if (enable_airplay){
  390. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  391. LOG_INFO("Initializing AirPlay sink");
  392. }
  393. }
  394. #endif
  395. #if CONFIG_CSPOT_SINK
  396. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_cspot")) != NULL) {
  397. enable_cspot = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
  398. free(p);
  399. if (enable_cspot){
  400. cspot_sink_init(cspot_cmd_handler, sink_data_handler);
  401. LOG_INFO("Initializing CSpot sink");
  402. }
  403. }
  404. #endif
  405. }
  406. void deregister_external(void) {
  407. #if CONFIG_BT_SINK
  408. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  409. bt_sink_deinit();
  410. }
  411. #endif
  412. #if CONFIG_AIRPLAY_SINK
  413. if (enable_airplay){
  414. LOG_INFO("Stopping AirPlay sink");
  415. raop_sink_deinit();
  416. }
  417. #endif
  418. #if CONFIG_CSPOT_SINK
  419. if (enable_cspot){
  420. LOG_INFO("Stopping CSpot sink");
  421. cspot_sink_deinit();
  422. }
  423. #endif
  424. }
  425. void decode_restore(int external) {
  426. switch (external) {
  427. #if CONFIG_BT_SINK
  428. case DECODE_BT:
  429. bt_disconnect();
  430. break;
  431. #endif
  432. #if CONFIG_AIRPLAY_SINK
  433. case DECODE_RAOP:
  434. raop_disconnect();
  435. raop_state = RAOP_STOP;
  436. break;
  437. #endif
  438. #if CONFIG_CSPOT_SINK
  439. case DECODE_CSPOT:
  440. cspot_disconnect();
  441. break;
  442. #endif
  443. }
  444. }