decode_external.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. static raop_event_t raop_state;
  40. static bool raop_expect_stop = false;
  41. static struct {
  42. bool enabled, start;
  43. s32_t error;
  44. u32_t start_time;
  45. u32_t playtime, len;
  46. } raop_sync;
  47. /****************************************************************************************
  48. * Common sink data handler
  49. */
  50. static void sink_data_handler(const uint8_t *data, uint32_t len)
  51. {
  52. size_t bytes, space;
  53. // would be better to lock decoder, but really, it does not matter
  54. if (decode.state != DECODE_STOPPED) {
  55. LOG_SDEBUG("Cannot use external sink while LMS is controlling player");
  56. return;
  57. }
  58. // there will always be room at some point
  59. while (len) {
  60. LOCK_O;
  61. bytes = min(_buf_space(outputbuf), _buf_cont_write(outputbuf));
  62. bytes = min(len, bytes);
  63. #if BYTES_PER_FRAME == 4
  64. memcpy(outputbuf->writep, data, bytes);
  65. #else
  66. {
  67. s16_t *iptr = (s16_t*) data;
  68. ISAMPLE_T *optr = (ISAMPLE_T*) outputbuf->writep;
  69. size_t n = bytes / BYTES_PER_FRAME * 2;
  70. while (n--) *optr++ = *iptr++ << 16;
  71. }
  72. #endif
  73. _buf_inc_writep(outputbuf, bytes);
  74. space = _buf_space(outputbuf);
  75. len -= bytes;
  76. data += bytes;
  77. UNLOCK_O;
  78. // allow i2s to empty the buffer if needed
  79. if (len && !space) usleep(50000);
  80. }
  81. }
  82. /****************************************************************************************
  83. * BT sink command handler
  84. */
  85. static void bt_sink_cmd_handler(bt_sink_cmd_t cmd, ...)
  86. {
  87. va_list args;
  88. LOCK_D;
  89. if (decode.state != DECODE_STOPPED) {
  90. LOG_WARN("Cannot use BT sink while LMS is controlling player");
  91. UNLOCK_D;
  92. return;
  93. }
  94. va_start(args, cmd);
  95. if (cmd != BT_SINK_VOLUME) LOCK_O;
  96. switch(cmd) {
  97. case BT_SINK_CONNECTED:
  98. output.external = DECODE_BT;
  99. output.state = OUTPUT_STOPPED;
  100. LOG_INFO("BT sink started");
  101. break;
  102. case BT_SINK_DISCONNECTED:
  103. if (output.external == DECODE_BT) {
  104. output.external = 0;
  105. output.state = OUTPUT_OFF;
  106. LOG_INFO("BT sink stopped");
  107. }
  108. break;
  109. case BT_SINK_PLAY:
  110. output.state = OUTPUT_RUNNING;
  111. LOG_INFO("BT sink playing");
  112. break;
  113. case BT_SINK_STOP:
  114. _buf_flush(outputbuf);
  115. case BT_SINK_PAUSE:
  116. output.state = OUTPUT_STOPPED;
  117. LOG_INFO("BT sink stopped");
  118. break;
  119. case BT_SINK_RATE:
  120. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  121. LOG_INFO("Setting BT sample rate %u", output.next_sample_rate);
  122. break;
  123. case BT_SINK_VOLUME: {
  124. u16_t volume = (u16_t) va_arg(args, u32_t);
  125. volume = 65536 * powf(volume / 128.0f, 3);
  126. set_volume(volume, volume);
  127. break;
  128. }
  129. }
  130. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  131. UNLOCK_D;
  132. va_end(args);
  133. }
  134. /****************************************************************************************
  135. * raop sink data handler
  136. */
  137. static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
  138. raop_sync.playtime = playtime;
  139. raop_sync.len = len;
  140. sink_data_handler(data, len);
  141. }
  142. /****************************************************************************************
  143. * AirPlay sink command handler
  144. */
  145. void raop_sink_cmd_handler(raop_event_t event, void *param)
  146. {
  147. LOCK_D;
  148. if (decode.state != DECODE_STOPPED) {
  149. LOG_WARN("Cannot use Airplay sink while LMS is controlling player");
  150. UNLOCK_D;
  151. return;
  152. }
  153. if (event != RAOP_VOLUME) LOCK_O;
  154. // this is async, so player might have been deleted
  155. switch (event) {
  156. case RAOP_TIMING: {
  157. u32_t ms, now = gettime_ms();
  158. s32_t error;
  159. if (!raop_sync.enabled || output.state < OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
  160. // first must make sure we started on time
  161. if (raop_sync.start) {
  162. // how many ms have we really played
  163. ms = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / RAOP_SAMPLE_RATE;
  164. error = ms - (now - raop_sync.start_time);
  165. LOG_DEBUG("backend played %u, desired %u, (delta:%d)", ms, now - raop_sync.start_time, error);
  166. if (abs(error) < 10 && abs(raop_sync.error) < 10) raop_sync.start = false;
  167. } else {
  168. // in how many ms will the most recent block play
  169. 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);
  170. error = (raop_sync.playtime - now) - ms;
  171. LOG_INFO("head local:%u, remote:%u (delta:%d)", ms, raop_sync.playtime - now, error);
  172. LOG_DEBUG("obuf:%u, sync_len:%u, devframes:%u, inproc:%u", _buf_used(outputbuf), raop_sync.len, output.device_frames, output.frames_in_process);
  173. }
  174. // TODO: better sync logic
  175. if (error < -10 && raop_sync.error < -10) {
  176. output.skip_frames = (abs(error + raop_sync.error) / 2 * RAOP_SAMPLE_RATE) / 1000;
  177. output.state = OUTPUT_SKIP_FRAMES;
  178. raop_sync.error = 0;
  179. LOG_INFO("skipping %u frames", output.skip_frames);
  180. } else if (error > 10 && raop_sync.error > 10) {
  181. output.pause_frames = (abs(error + raop_sync.error) / 2 * RAOP_SAMPLE_RATE) / 1000;
  182. output.state = OUTPUT_PAUSE_FRAMES;
  183. raop_sync.error = 0;
  184. LOG_INFO("pausing for %u frames", output.pause_frames);
  185. }
  186. raop_sync.error = error;
  187. break;
  188. }
  189. case RAOP_SETUP:
  190. // we need a fair bit of space for RTP process
  191. _buf_resize(outputbuf, RAOP_OUTPUT_SIZE);
  192. LOG_INFO("resizing buffer %u", outputbuf->size);
  193. break;
  194. case RAOP_STREAM:
  195. LOG_INFO("Stream", NULL);
  196. raop_state = event;
  197. raop_sync.error = 0;
  198. raop_sync.start = true;
  199. raop_sync.enabled = !strcasestr(output.device, "BT");
  200. output.external = DECODE_AIRPLAY;
  201. output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE;
  202. output.state = OUTPUT_STOPPED;
  203. break;
  204. case RAOP_STOP:
  205. LOG_INFO("Stop", NULL);
  206. output.external = 0;
  207. output.state = OUTPUT_OFF;
  208. output.frames_played = 0;
  209. raop_state = event;
  210. break;
  211. case RAOP_FLUSH:
  212. LOG_INFO("Flush", NULL);
  213. raop_expect_stop = true;
  214. raop_state = event;
  215. _buf_flush(outputbuf);
  216. output.state = OUTPUT_STOPPED;
  217. output.frames_played = 0;
  218. break;
  219. case RAOP_PLAY: {
  220. LOG_INFO("Play", NULL);
  221. if (raop_state != RAOP_PLAY) {
  222. output.state = OUTPUT_START_AT;
  223. output.start_at = *(u32_t*) param;
  224. raop_sync.start_time = output.start_at;
  225. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  226. }
  227. raop_state = event;
  228. break;
  229. }
  230. case RAOP_VOLUME: {
  231. float volume = *((float*) param);
  232. LOG_INFO("Volume[0..1] %0.4f", volume);
  233. volume = 65536 * powf(volume, 3);
  234. set_volume((u16_t) volume, (u16_t) volume);
  235. break;
  236. }
  237. default:
  238. break;
  239. }
  240. if (event != RAOP_VOLUME) UNLOCK_O;
  241. UNLOCK_D;
  242. }
  243. /****************************************************************************************
  244. * We provide the generic codec register option
  245. */
  246. void register_external(void) {
  247. if (!strcasestr(output.device, "BT ") ) {
  248. if(enable_bt_sink){
  249. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  250. LOG_INFO("Initializing BT sink");
  251. }
  252. } else {
  253. LOG_WARN("Cannot be a BT sink and source");
  254. }
  255. if(enable_airplay){
  256. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  257. LOG_INFO("Initializing AirPlay sink");
  258. }
  259. }
  260. void deregister_external(void) {
  261. if (!strcasestr(output.device, "BT ") && enable_bt_sink) {
  262. bt_sink_deinit();
  263. LOG_INFO("Stopping BT sink");
  264. }
  265. if(enable_airplay){
  266. raop_sink_deinit();
  267. LOG_INFO("Stopping AirPlay sink");
  268. }
  269. }