decode_external.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "squeezelite.h"
  22. #include "bt_app_sink.h"
  23. #include "raop_sink.h"
  24. #include <math.h>
  25. #define LOCK_O mutex_lock(outputbuf->mutex)
  26. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  27. #define LOCK_D mutex_lock(decode.mutex);
  28. #define UNLOCK_D mutex_unlock(decode.mutex);
  29. enum { DECODE_BT = 1, DECODE_AIRPLAY };
  30. extern struct outputstate output;
  31. extern struct decodestate decode;
  32. extern struct buffer *outputbuf;
  33. // this is the only system-wide loglevel variable
  34. extern log_level loglevel;
  35. // not great to have these here, but they should not be in embedded.h
  36. bool enable_bt_sink = false;
  37. bool enable_airplay = false;
  38. #define RAOP_OUTPUT_SIZE (RAOP_SAMPLE_RATE * 2 * 2 * 2 * 1.2)
  39. #define SYNC_NB 5
  40. static raop_event_t raop_state;
  41. static bool raop_expect_stop = false;
  42. static struct {
  43. bool enabled, start;
  44. s32_t error[SYNC_NB];
  45. u32_t idx, len;
  46. u32_t start_time, playtime;
  47. } raop_sync;
  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. // would be better to lock decoder, but really, it does not matter
  55. if (decode.state != DECODE_STOPPED) {
  56. LOG_SDEBUG("Cannot use external sink while LMS is controlling player");
  57. return;
  58. }
  59. // there will always be room at some point
  60. while (len) {
  61. LOCK_O;
  62. bytes = min(_buf_space(outputbuf), _buf_cont_write(outputbuf));
  63. bytes = min(len, bytes);
  64. #if BYTES_PER_FRAME == 4
  65. memcpy(outputbuf->writep, data, bytes);
  66. #else
  67. {
  68. s16_t *iptr = (s16_t*) data;
  69. ISAMPLE_T *optr = (ISAMPLE_T*) outputbuf->writep;
  70. size_t n = bytes / BYTES_PER_FRAME * 2;
  71. while (n--) *optr++ = *iptr++ << 16;
  72. }
  73. #endif
  74. _buf_inc_writep(outputbuf, bytes);
  75. space = _buf_space(outputbuf);
  76. len -= bytes;
  77. data += bytes;
  78. UNLOCK_O;
  79. // allow i2s to empty the buffer if needed
  80. if (len && !space) usleep(50000);
  81. }
  82. }
  83. /****************************************************************************************
  84. * BT sink command handler
  85. */
  86. static void bt_sink_cmd_handler(bt_sink_cmd_t cmd, ...)
  87. {
  88. va_list args;
  89. LOCK_D;
  90. if (decode.state != DECODE_STOPPED) {
  91. LOG_WARN("Cannot use BT sink while LMS is controlling player");
  92. UNLOCK_D;
  93. bt_sink_cmd(BT_SINK_DISCONNECTED);
  94. return;
  95. }
  96. va_start(args, cmd);
  97. if (cmd != BT_SINK_VOLUME) LOCK_O;
  98. switch(cmd) {
  99. case BT_SINK_CONNECTED:
  100. output.external = DECODE_BT;
  101. output.state = OUTPUT_STOPPED;
  102. LOG_INFO("BT sink started");
  103. break;
  104. case BT_SINK_DISCONNECTED:
  105. if (output.external == DECODE_BT) {
  106. output.external = 0;
  107. output.state = OUTPUT_OFF;
  108. LOG_INFO("BT sink stopped");
  109. }
  110. break;
  111. case BT_SINK_PLAY:
  112. output.state = OUTPUT_RUNNING;
  113. LOG_INFO("BT sink playing");
  114. break;
  115. case BT_SINK_STOP:
  116. _buf_flush(outputbuf);
  117. case BT_SINK_PAUSE:
  118. output.state = OUTPUT_STOPPED;
  119. LOG_INFO("BT sink stopped");
  120. break;
  121. case BT_SINK_RATE:
  122. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  123. LOG_INFO("Setting BT sample rate %u", output.next_sample_rate);
  124. break;
  125. case BT_SINK_VOLUME: {
  126. u16_t volume = (u16_t) va_arg(args, u32_t);
  127. volume = 65536 * powf(volume / 128.0f, 3);
  128. set_volume(volume, volume);
  129. break;
  130. }
  131. }
  132. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  133. UNLOCK_D;
  134. va_end(args);
  135. }
  136. /****************************************************************************************
  137. * raop sink data handler
  138. */
  139. static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
  140. raop_sync.playtime = playtime;
  141. raop_sync.len = len;
  142. sink_data_handler(data, len);
  143. }
  144. /****************************************************************************************
  145. * AirPlay sink command handler
  146. */
  147. void raop_sink_cmd_handler(raop_event_t event, void *param)
  148. {
  149. LOCK_D;
  150. if (decode.state != DECODE_STOPPED) {
  151. LOG_WARN("Cannot use Airplay sink while LMS is controlling player");
  152. UNLOCK_D;
  153. return;
  154. }
  155. if (event != RAOP_VOLUME) LOCK_O;
  156. // this is async, so player might have been deleted
  157. switch (event) {
  158. case RAOP_TIMING: {
  159. u32_t ms, now = gettime_ms();
  160. s32_t sync_nb, error = 0;
  161. if (!raop_sync.enabled || output.state < OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
  162. // first must make sure we started on time
  163. if (raop_sync.start) {
  164. // how many ms have we really played
  165. ms = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / RAOP_SAMPLE_RATE;
  166. raop_sync.error[raop_sync.idx] = ms - (now - raop_sync.start_time);
  167. sync_nb = 2;
  168. LOG_INFO("backend played %u, desired %u, (delta:%d)", ms, now - raop_sync.start_time, raop_sync.error[raop_sync.idx]);
  169. } else {
  170. // in how many ms will the most recent block play
  171. ms = ((u64_t) ((_buf_used(outputbuf) - raop_sync.len) / BYTES_PER_FRAME + output.device_frames + output.frames_in_process) * 1000) / RAOP_SAMPLE_RATE - (now - output.updated);
  172. raop_sync.error[raop_sync.idx] = (raop_sync.playtime - now) - ms;
  173. sync_nb = SYNC_NB;
  174. LOG_INFO("head local:%u, remote:%u (delta:%d)", ms, raop_sync.playtime - now, raop_sync.error[raop_sync.idx]);
  175. LOG_DEBUG("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 the average error
  178. for (int i = 0; i < sync_nb; i++) error += raop_sync.error[i];
  179. error /= sync_nb;
  180. raop_sync.idx = (raop_sync.idx + 1) % sync_nb;
  181. // need at least nb_sync measures done to exit quick mode
  182. if (raop_sync.start && !raop_sync.idx && abs(error) < 10) raop_sync.start = false;
  183. // correct if needed
  184. if (error < -10) {
  185. output.skip_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
  186. output.state = OUTPUT_SKIP_FRAMES;
  187. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  188. LOG_INFO("skipping %u frames", output.skip_frames);
  189. } else if (error > 10) {
  190. output.pause_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
  191. output.state = OUTPUT_PAUSE_FRAMES;
  192. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  193. LOG_INFO("pausing for %u frames", output.pause_frames);
  194. }
  195. break;
  196. }
  197. case RAOP_SETUP:
  198. // we need a fair bit of space for RTP process
  199. _buf_resize(outputbuf, RAOP_OUTPUT_SIZE);
  200. LOG_INFO("resizing buffer %u", outputbuf->size);
  201. break;
  202. case RAOP_STREAM:
  203. LOG_INFO("Stream", NULL);
  204. raop_state = event;
  205. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  206. raop_sync.idx = 0;
  207. raop_sync.start = true;
  208. raop_sync.enabled = !strcasestr(output.device, "BT");
  209. output.external = DECODE_AIRPLAY;
  210. output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE;
  211. output.state = OUTPUT_STOPPED;
  212. break;
  213. case RAOP_STOP:
  214. LOG_INFO("Stop", NULL);
  215. output.external = 0;
  216. output.state = OUTPUT_OFF;
  217. output.frames_played = 0;
  218. raop_state = event;
  219. break;
  220. case RAOP_FLUSH:
  221. LOG_INFO("Flush", NULL);
  222. raop_expect_stop = true;
  223. raop_state = event;
  224. _buf_flush(outputbuf);
  225. output.state = OUTPUT_STOPPED;
  226. output.frames_played = 0;
  227. break;
  228. case RAOP_PLAY: {
  229. LOG_INFO("Play", NULL);
  230. if (raop_state != RAOP_PLAY) {
  231. output.state = OUTPUT_START_AT;
  232. output.start_at = *(u32_t*) param;
  233. raop_sync.start_time = output.start_at;
  234. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  235. }
  236. raop_state = event;
  237. break;
  238. }
  239. case RAOP_VOLUME: {
  240. float volume = *((float*) param);
  241. LOG_INFO("Volume[0..1] %0.4f", volume);
  242. volume = 65536 * powf(volume, 3);
  243. set_volume((u16_t) volume, (u16_t) volume);
  244. break;
  245. }
  246. default:
  247. break;
  248. }
  249. if (event != RAOP_VOLUME) UNLOCK_O;
  250. UNLOCK_D;
  251. }
  252. /****************************************************************************************
  253. * We provide the generic codec register option
  254. */
  255. void register_external(void) {
  256. if (!strcasestr(output.device, "BT ") ) {
  257. if(enable_bt_sink){
  258. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  259. LOG_INFO("Initializing BT sink");
  260. }
  261. } else {
  262. LOG_WARN("Cannot be a BT sink and source");
  263. }
  264. if(enable_airplay){
  265. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  266. LOG_INFO("Initializing AirPlay sink");
  267. }
  268. }
  269. void deregister_external(void) {
  270. if (!strcasestr(output.device, "BT ") && enable_bt_sink) {
  271. bt_sink_deinit();
  272. LOG_INFO("Stopping BT sink");
  273. }
  274. if(enable_airplay){
  275. raop_sink_deinit();
  276. LOG_INFO("Stopping AirPlay sink");
  277. }
  278. }