decode_external.c 10 KB

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