decode_external.c 10 KB

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