decode_external.c 13 KB

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