decode_external.c 14 KB

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