decode_external.c 10 KB

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