decode_external.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_AIRPLAY_SINK
  23. #include "raop_sink.h"
  24. static bool enable_airplay;
  25. #define RAOP_OUTPUT_SIZE (((RAOP_SAMPLE_RATE * BYTES_PER_FRAME * 2 * 120) / 100) & ~BYTES_PER_FRAME)
  26. #define SYNC_WIN_SLOW 32
  27. #define SYNC_WIN_CHECK 8
  28. #define SYNC_WIN_FAST 2
  29. static raop_event_t raop_state;
  30. static EXT_RAM_ATTR struct {
  31. bool enabled;
  32. int sum, count, win, errors[SYNC_WIN_SLOW];
  33. s32_t len;
  34. u32_t start_time, playtime;
  35. } raop_sync;
  36. #endif
  37. static bool abort_sink ;
  38. #define LOCK_O mutex_lock(outputbuf->mutex)
  39. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  40. #define LOCK_D mutex_lock(decode.mutex);
  41. #define UNLOCK_D mutex_unlock(decode.mutex);
  42. enum { DECODE_BT = 1, DECODE_RAOP};
  43. extern struct outputstate output;
  44. extern struct decodestate decode;
  45. extern struct buffer *outputbuf;
  46. // this is the only system-wide loglevel variable
  47. extern log_level loglevel;
  48. /****************************************************************************************
  49. * Common sink data handler
  50. */
  51. static void sink_data_handler(const uint8_t *data, uint32_t len)
  52. {
  53. size_t bytes, space;
  54. int wait = 5;
  55. // would be better to lock output, but really, it does not matter
  56. if (!output.external) {
  57. LOG_SDEBUG("Cannot use external sink while LMS is controlling player");
  58. return;
  59. }
  60. LOCK_O;
  61. abort_sink = false;
  62. // there will always be room at some point
  63. while (len && wait && !abort_sink) {
  64. bytes = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / (BYTES_PER_FRAME / 4);
  65. bytes = min(len, bytes);
  66. #if BYTES_PER_FRAME == 4
  67. memcpy(outputbuf->writep, data, bytes);
  68. #else
  69. {
  70. s16_t *iptr = (s16_t*) data;
  71. ISAMPLE_T *optr = (ISAMPLE_T *) outputbuf->writep;
  72. size_t n = bytes / 2;
  73. while (n--) *optr++ = *iptr++ << 16;
  74. }
  75. #endif
  76. _buf_inc_writep(outputbuf, bytes * BYTES_PER_FRAME / 4);
  77. space = _buf_space(outputbuf);
  78. len -= bytes;
  79. data += bytes;
  80. // allow i2s to empty the buffer if needed
  81. if (len && !space) {
  82. wait--;
  83. UNLOCK_O; usleep(50000); LOCK_O;
  84. }
  85. }
  86. UNLOCK_O;
  87. if (!wait) {
  88. LOG_WARN("Waited too long, dropping frames");
  89. }
  90. }
  91. /****************************************************************************************
  92. * BT sink command handler
  93. */
  94. #if CONFIG_BT_SINK
  95. static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args)
  96. {
  97. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  98. if (output.external != DECODE_BT && output.state > OUTPUT_STOPPED) {
  99. LOG_WARN("Cannot use BT sink while LMS/AirPlay are controlling player");
  100. return false;
  101. }
  102. LOCK_D;
  103. if (cmd != BT_SINK_VOLUME) LOCK_O;
  104. switch(cmd) {
  105. case BT_SINK_AUDIO_STARTED:
  106. _buf_flush(outputbuf);
  107. _buf_limit(outputbuf, 0);
  108. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  109. output.external = DECODE_BT;
  110. output.state = OUTPUT_STOPPED;
  111. output.frames_played = 0;
  112. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  113. LOG_INFO("BT sink started");
  114. break;
  115. case BT_SINK_AUDIO_STOPPED:
  116. if (output.external == DECODE_BT) {
  117. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  118. output.external = 0;
  119. output.stop_time = gettime_ms();
  120. LOG_INFO("BT sink stopped");
  121. }
  122. break;
  123. case BT_SINK_PLAY:
  124. output.state = OUTPUT_RUNNING;
  125. LOG_INFO("BT play");
  126. break;
  127. case BT_SINK_STOP:
  128. _buf_flush(outputbuf);
  129. output.state = OUTPUT_STOPPED;
  130. output.stop_time = gettime_ms();
  131. abort_sink = true;
  132. LOG_INFO("BT stop");
  133. break;
  134. case BT_SINK_PAUSE:
  135. output.stop_time = gettime_ms();
  136. LOG_INFO("BT pause, just silence");
  137. break;
  138. case BT_SINK_RATE:
  139. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  140. LOG_INFO("Setting BT sample rate %u", output.next_sample_rate);
  141. break;
  142. case BT_SINK_VOLUME: {
  143. u32_t volume = va_arg(args, u32_t);
  144. volume = 65536 * powf(volume / 128.0f, 3);
  145. set_volume(volume, volume);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  152. UNLOCK_D;
  153. return true;
  154. }
  155. #endif
  156. /****************************************************************************************
  157. * raop sink data handler
  158. */
  159. #if CONFIG_AIRPLAY_SINK
  160. static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
  161. raop_sync.playtime = playtime;
  162. raop_sync.len = len;
  163. sink_data_handler(data, len);
  164. }
  165. /****************************************************************************************
  166. * AirPlay sink command handler
  167. */
  168. static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
  169. {
  170. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  171. if (output.external != DECODE_RAOP && output.state > OUTPUT_STOPPED) {
  172. LOG_WARN("Cannot use Airplay sink while LMS/BT are controlling player");
  173. return false;
  174. }
  175. LOCK_D;
  176. if (event != RAOP_VOLUME) LOCK_O;
  177. // this is async, so player might have been deleted
  178. switch (event) {
  179. case RAOP_TIMING: {
  180. if (!raop_sync.enabled || output.state != OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
  181. u32_t ms, now = gettime_ms();
  182. u32_t level = _buf_used(outputbuf);
  183. int error;
  184. // in how many ms will the most recent block play
  185. 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);
  186. // when outputbuf is empty, it means we have a network black-out or something
  187. error = level ? (raop_sync.playtime - now) - ms : 0;
  188. if (loglevel == lDEBUG || !level) {
  189. LOG_INFO("head local:%d, remote:%d (delta:%d)", ms, raop_sync.playtime - now, error);
  190. LOG_INFO("obuf:%u, sync_len:%u, devframes:%u, inproc:%u", _buf_used(outputbuf), raop_sync.len, output.device_frames, output.frames_in_process);
  191. }
  192. // calculate sum, error and update sliding window
  193. raop_sync.errors[raop_sync.count++ % raop_sync.win] = error;
  194. raop_sync.sum += error;
  195. error = raop_sync.sum / min(raop_sync.count, raop_sync.win);
  196. // wait till we have enough data or there is a strong deviation
  197. if ((raop_sync.count >= raop_sync.win && abs(error) > 10) || (raop_sync.count >= SYNC_WIN_CHECK && abs(error) > 100)) {
  198. if (error < 0) {
  199. output.skip_frames = -(error * RAOP_SAMPLE_RATE) / 1000;
  200. output.state = OUTPUT_SKIP_FRAMES;
  201. LOG_INFO("skipping %u frames (count:%d)", output.skip_frames, raop_sync.count);
  202. } else {
  203. output.pause_frames = (error * RAOP_SAMPLE_RATE) / 1000;
  204. output.state = OUTPUT_PAUSE_FRAMES;
  205. LOG_INFO("pausing for %u frames (count: %d)", output.pause_frames, raop_sync.count);
  206. }
  207. raop_sync.sum = raop_sync.count = 0;
  208. memset(raop_sync.errors, 0, sizeof(raop_sync.errors));
  209. }
  210. // move to normal mode if possible
  211. if (raop_sync.win == 1) {
  212. raop_sync.win = SYNC_WIN_FAST;
  213. LOG_INFO("backend played %u, desired %u, (delta:%d)", ms, raop_sync.playtime - now, error);
  214. } else if (raop_sync.win == SYNC_WIN_FAST && raop_sync.count >= SYNC_WIN_FAST && abs(error) < 10) {
  215. raop_sync.win = SYNC_WIN_SLOW;
  216. LOG_INFO("switching to slow sync mode %u", raop_sync.win);
  217. }
  218. break;
  219. }
  220. case RAOP_SETUP: {
  221. uint8_t **buffer = va_arg(args, uint8_t**);
  222. size_t *size = va_arg(args, size_t*);
  223. // steal buffer tail from outputbuf but do not reallocate
  224. *size = _buf_limit(outputbuf, RAOP_OUTPUT_SIZE);
  225. *buffer = outputbuf->writep + RAOP_OUTPUT_SIZE;
  226. output.frames_played = 0;
  227. output.external = DECODE_RAOP;
  228. output.state = OUTPUT_STOPPED;
  229. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  230. LOG_INFO("resizing buffer %u", outputbuf->size);
  231. break;
  232. }
  233. case RAOP_STREAM:
  234. LOG_INFO("Stream", NULL);
  235. raop_state = event;
  236. raop_sync.win = 1;
  237. raop_sync.sum = raop_sync.count = 0;
  238. memset(raop_sync.errors, 0, sizeof(raop_sync.errors));
  239. raop_sync.enabled = !strcasestr(output.device, "BT");
  240. output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE;
  241. break;
  242. case RAOP_STOP:
  243. output.external = 0;
  244. __attribute__ ((fallthrough));
  245. case RAOP_FLUSH:
  246. LOG_INFO("%s", event == RAOP_FLUSH ? "Flush" : "Stop");
  247. _buf_flush(outputbuf);
  248. raop_state = event;
  249. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  250. abort_sink = true;
  251. output.frames_played = 0;
  252. output.stop_time = gettime_ms();
  253. break;
  254. case RAOP_PLAY: {
  255. LOG_INFO("Play", NULL);
  256. if (raop_state != RAOP_PLAY) {
  257. output.state = OUTPUT_START_AT;
  258. output.start_at = va_arg(args, u32_t);
  259. raop_sync.start_time = output.start_at;
  260. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  261. }
  262. raop_state = event;
  263. break;
  264. }
  265. case RAOP_VOLUME: {
  266. float volume = va_arg(args, double);
  267. LOG_INFO("Volume[0..1] %0.4f", volume);
  268. volume = 65536 * powf(volume, 3);
  269. set_volume(volume, volume);
  270. break;
  271. }
  272. default:
  273. break;
  274. }
  275. if (event != RAOP_VOLUME) UNLOCK_O;
  276. UNLOCK_D;
  277. return true;
  278. }
  279. #endif
  280. /****************************************************************************************
  281. * We provide the generic codec register option
  282. */
  283. void register_external(void) {
  284. char *p;
  285. #if CONFIG_BT_SINK
  286. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_bt_sink")) != NULL) {
  287. enable_bt_sink = !strcmp(p,"1") || !strcasecmp(p,"y");
  288. free(p);
  289. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  290. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  291. }
  292. }
  293. #endif
  294. #if CONFIG_AIRPLAY_SINK
  295. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_airplay")) != NULL) {
  296. enable_airplay = !strcmp(p,"1") || !strcasecmp(p,"y");
  297. free(p);
  298. if (enable_airplay){
  299. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  300. LOG_INFO("Initializing AirPlay sink");
  301. }
  302. }
  303. #endif
  304. }
  305. void deregister_external(void) {
  306. #if CONFIG_BT_SINK
  307. if (!strcasestr(output.device, "BT") && enable_bt_sink) {
  308. bt_sink_deinit();
  309. }
  310. #endif
  311. #if CONFIG_AIRPLAY_SINK
  312. if (enable_airplay){
  313. LOG_INFO("Stopping AirPlay sink");
  314. raop_sink_deinit();
  315. }
  316. #endif
  317. }
  318. void decode_restore(int external) {
  319. switch (external) {
  320. #if CONFIG_BT_SINK
  321. case DECODE_BT:
  322. bt_disconnect();
  323. break;
  324. #endif
  325. #if CONFIG_AIRPLAY_SINK
  326. case DECODE_RAOP:
  327. raop_disconnect();
  328. raop_state = RAOP_STOP;
  329. break;
  330. #endif
  331. }
  332. }