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_STALLED:
  249. case RAOP_STOP:
  250. output.external = 0;
  251. __attribute__ ((fallthrough));
  252. case RAOP_FLUSH:
  253. LOG_INFO("%s", event == RAOP_FLUSH ? "Flush" : "Stop");
  254. _buf_flush(outputbuf);
  255. raop_state = event;
  256. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  257. sink_state = SINK_ABORT;
  258. output.frames_played = 0;
  259. output.stop_time = gettime_ms();
  260. break;
  261. case RAOP_PLAY: {
  262. LOG_INFO("Play", NULL);
  263. if (raop_state != RAOP_PLAY) {
  264. output.state = OUTPUT_START_AT;
  265. output.start_at = va_arg(args, u32_t);
  266. raop_sync.start_time = output.start_at;
  267. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  268. }
  269. raop_state = event;
  270. break;
  271. }
  272. case RAOP_VOLUME: {
  273. float volume = va_arg(args, double);
  274. LOG_INFO("Volume[0..1] %0.4f", volume);
  275. volume = 65536 * powf(volume, 3);
  276. set_volume(volume, volume);
  277. break;
  278. }
  279. default:
  280. break;
  281. }
  282. if (event != RAOP_VOLUME) UNLOCK_O;
  283. UNLOCK_D;
  284. return true;
  285. }
  286. #endif
  287. /****************************************************************************************
  288. * cspot sink command handler
  289. */
  290. #if CONFIG_CSPOT_SINK
  291. static bool cspot_cmd_handler(cspot_event_t cmd, va_list args)
  292. {
  293. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  294. if (output.external != DECODE_CSPOT && output.state > OUTPUT_STOPPED) {
  295. LOG_WARN("Cannot use CSpot sink while LMS/BT/Airplay are controlling player %d", output.external);
  296. return false;
  297. }
  298. LOCK_D;
  299. if (cmd != CSPOT_VOLUME) LOCK_O;
  300. switch(cmd) {
  301. case CSPOT_START:
  302. output.current_sample_rate = output.next_sample_rate = va_arg(args, u32_t);
  303. output.external = DECODE_CSPOT;
  304. output.frames_played = 0;
  305. // in 1/10 of seconds
  306. output.threshold = 25;
  307. output.state = OUTPUT_STOPPED;
  308. sink_state = SINK_ABORT;
  309. _buf_flush(outputbuf);
  310. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  311. LOG_INFO("CSpot start track");
  312. break;
  313. case CSPOT_DISC:
  314. _buf_flush(outputbuf);
  315. sink_state = SINK_ABORT;
  316. output.external = 0;
  317. output.state = OUTPUT_STOPPED;
  318. output.stop_time = gettime_ms();
  319. LOG_INFO("CSpot disconnected");
  320. break;
  321. case CSPOT_PLAY:
  322. sink_state = SINK_RUNNING;
  323. output.state = OUTPUT_RUNNING;
  324. LOG_INFO("CSpot play");
  325. break;
  326. case CSPOT_SEEK:
  327. _buf_flush(outputbuf);
  328. sink_state = SINK_ABORT;
  329. LOG_INFO("CSpot seek by %d", va_arg(args, uint32_t));
  330. break;
  331. case CSPOT_FLUSH:
  332. _buf_flush(outputbuf);
  333. sink_state = SINK_DISCARD;
  334. output.state = OUTPUT_STOPPED;
  335. LOG_INFO("CSpot flush");
  336. break;
  337. case CSPOT_PAUSE:
  338. output.state = OUTPUT_STOPPED;
  339. output.stop_time = gettime_ms();
  340. LOG_INFO("CSpot pause");
  341. break;
  342. case CSPOT_TRACK_MARK:
  343. output.track_start = outputbuf->writep;
  344. break;
  345. case CSPOT_QUERY_REMAINING: {
  346. uint32_t *remaining = va_arg(args, uint32_t*);
  347. *remaining = (_buf_used(outputbuf) * 1000) / (output.current_sample_rate * BYTES_PER_FRAME);
  348. break;
  349. }
  350. case CSPOT_QUERY_STARTED: {
  351. uint32_t *started = va_arg(args, uint32_t*);
  352. *started = output.track_started;
  353. // this is a read_and_clear event
  354. output.track_started = false;
  355. break;
  356. }
  357. case CSPOT_VOLUME: {
  358. u32_t volume = va_arg(args, u32_t);
  359. LOG_INFO("CSpot volume %u", volume);
  360. volume = 65536 * powf(volume / 65536.0f, 4);
  361. set_volume(volume, volume);
  362. break;
  363. default:
  364. break;
  365. }
  366. }
  367. if (cmd != CSPOT_VOLUME) UNLOCK_O;
  368. UNLOCK_D;
  369. return true;
  370. }
  371. #endif
  372. /****************************************************************************************
  373. * We provide the generic codec register option
  374. */
  375. void register_external(void) {
  376. char *p;
  377. #if CONFIG_BT_SINK
  378. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_bt_sink")) != NULL) {
  379. enable_bt_sink = !strcmp(p,"1") || !strcasecmp(p,"y");
  380. free(p);
  381. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  382. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  383. }
  384. }
  385. #endif
  386. #if CONFIG_AIRPLAY_SINK
  387. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_airplay")) != NULL) {
  388. enable_airplay = !strcmp(p,"1") || !strcasecmp(p,"y");
  389. free(p);
  390. if (enable_airplay){
  391. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  392. LOG_INFO("Initializing AirPlay sink");
  393. }
  394. }
  395. #endif
  396. #if CONFIG_CSPOT_SINK
  397. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_cspot")) != NULL) {
  398. enable_cspot = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
  399. free(p);
  400. if (enable_cspot){
  401. cspot_sink_init(cspot_cmd_handler, sink_data_handler);
  402. LOG_INFO("Initializing CSpot sink");
  403. }
  404. }
  405. #endif
  406. }
  407. void deregister_external(void) {
  408. #if CONFIG_BT_SINK
  409. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  410. bt_sink_deinit();
  411. }
  412. #endif
  413. #if CONFIG_AIRPLAY_SINK
  414. if (enable_airplay){
  415. LOG_INFO("Stopping AirPlay sink");
  416. raop_sink_deinit();
  417. }
  418. #endif
  419. #if CONFIG_CSPOT_SINK
  420. if (enable_cspot){
  421. LOG_INFO("Stopping CSpot sink");
  422. cspot_sink_deinit();
  423. }
  424. #endif
  425. }
  426. void decode_restore(int external) {
  427. switch (external) {
  428. #if CONFIG_BT_SINK
  429. case DECODE_BT:
  430. bt_disconnect();
  431. break;
  432. #endif
  433. #if CONFIG_AIRPLAY_SINK
  434. case DECODE_RAOP:
  435. raop_disconnect();
  436. break;
  437. #endif
  438. #if CONFIG_CSPOT_SINK
  439. case DECODE_CSPOT:
  440. cspot_disconnect();
  441. break;
  442. #endif
  443. }
  444. }