decode_external.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "config.h"
  22. #include "squeezelite.h"
  23. #include "bt_app_sink.h"
  24. #include "raop_sink.h"
  25. #include <math.h>
  26. #define LOCK_O mutex_lock(outputbuf->mutex)
  27. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  28. #define LOCK_D mutex_lock(decode.mutex);
  29. #define UNLOCK_D mutex_unlock(decode.mutex);
  30. enum { DECODE_BT = 1, DECODE_RAOP };
  31. extern struct outputstate output;
  32. extern struct decodestate decode;
  33. extern struct buffer *outputbuf;
  34. // this is the only system-wide loglevel variable
  35. extern log_level loglevel;
  36. static bool enable_bt_sink;
  37. static bool enable_airplay;
  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 struct {
  42. bool enabled, start;
  43. s32_t error[SYNC_NB];
  44. u32_t idx, len;
  45. u32_t start_time, playtime;
  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 output, but really, it does not matter
  54. if (!output.external) {
  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 bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args)
  86. {
  87. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  88. if (output.external != DECODE_BT && output.state > OUTPUT_STOPPED) {
  89. LOG_WARN("Cannot use BT sink while LMS/AirPlay is controlling player");
  90. return false;
  91. }
  92. LOCK_D;
  93. if (cmd != BT_SINK_VOLUME) LOCK_O;
  94. switch(cmd) {
  95. case BT_SINK_AUDIO_STARTED:
  96. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  97. output.external = DECODE_BT;
  98. output.state = OUTPUT_STOPPED;
  99. output.frames_played = 0;
  100. _buf_flush(outputbuf);
  101. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  102. LOG_INFO("BT sink started");
  103. break;
  104. case BT_SINK_AUDIO_STOPPED:
  105. if (output.external == DECODE_BT) {
  106. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  107. LOG_INFO("BT sink stopped");
  108. }
  109. break;
  110. case BT_SINK_PLAY:
  111. output.state = OUTPUT_RUNNING;
  112. LOG_INFO("BT sink 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 sink stopped");
  119. break;
  120. case BT_SINK_PAUSE:
  121. LOG_INFO("BT sink paused, just silence");
  122. break;
  123. case BT_SINK_RATE:
  124. output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
  125. LOG_INFO("Setting BT sample rate %u", output.next_sample_rate);
  126. break;
  127. case BT_SINK_VOLUME: {
  128. u16_t volume = (u16_t) va_arg(args, u32_t);
  129. volume = 65536 * powf(volume / 128.0f, 3);
  130. set_volume(volume, volume);
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  137. UNLOCK_D;
  138. return true;
  139. }
  140. /****************************************************************************************
  141. * raop sink data handler
  142. */
  143. static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
  144. raop_sync.playtime = playtime;
  145. raop_sync.len = len;
  146. sink_data_handler(data, len);
  147. }
  148. /****************************************************************************************
  149. * AirPlay sink command handler
  150. */
  151. static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
  152. {
  153. // don't LOCK_O as there is always a chance that LMS takes control later anyway
  154. if (output.external != DECODE_RAOP && output.state > OUTPUT_STOPPED) {
  155. LOG_WARN("Cannot use Airplay sink while LMS/BT is controlling player");
  156. return false;
  157. }
  158. LOCK_D;
  159. if (event != RAOP_VOLUME) LOCK_O;
  160. // this is async, so player might have been deleted
  161. switch (event) {
  162. case RAOP_TIMING: {
  163. u32_t ms, now = gettime_ms();
  164. s32_t sync_nb, error = 0;
  165. if (!raop_sync.enabled || output.state < OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
  166. // first must make sure we started on time
  167. if (raop_sync.start) {
  168. // how many ms have we really played
  169. ms = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / RAOP_SAMPLE_RATE;
  170. raop_sync.error[raop_sync.idx] = ms - (now - raop_sync.start_time);
  171. sync_nb = 2;
  172. LOG_INFO("backend played %u, desired %u, (delta:%d)", ms, now - raop_sync.start_time, raop_sync.error[raop_sync.idx]);
  173. } else {
  174. // in how many ms will the most recent block play
  175. 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);
  176. raop_sync.error[raop_sync.idx] = (raop_sync.playtime - now) - ms;
  177. sync_nb = SYNC_NB;
  178. LOG_DEBUG("head local:%u, remote:%u (delta:%d)", ms, raop_sync.playtime - now, raop_sync.error[raop_sync.idx]);
  179. LOG_DEBUG("obuf:%u, sync_len:%u, devframes:%u, inproc:%u", _buf_used(outputbuf), raop_sync.len, output.device_frames, output.frames_in_process);
  180. }
  181. // calculate the average error
  182. for (int i = 0; i < sync_nb; i++) error += raop_sync.error[i];
  183. error /= sync_nb;
  184. raop_sync.idx = (raop_sync.idx + 1) % sync_nb;
  185. // need at least nb_sync measures done to exit quick mode
  186. if (raop_sync.start && !raop_sync.idx && abs(error) < 10) raop_sync.start = false;
  187. // correct if needed
  188. if (error < -10) {
  189. output.skip_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
  190. output.state = OUTPUT_SKIP_FRAMES;
  191. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  192. LOG_INFO("skipping %u frames", output.skip_frames);
  193. } else if (error > 10) {
  194. output.pause_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
  195. output.state = OUTPUT_PAUSE_FRAMES;
  196. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  197. LOG_INFO("pausing for %u frames", output.pause_frames);
  198. }
  199. break;
  200. }
  201. case RAOP_SETUP:
  202. // we need a fair bit of space for RTP process
  203. _buf_resize(outputbuf, RAOP_OUTPUT_SIZE);
  204. output.frames_played = 0;
  205. output.external = DECODE_RAOP;
  206. output.state = OUTPUT_STOPPED;
  207. if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
  208. LOG_INFO("resizing buffer %u", outputbuf->size);
  209. break;
  210. case RAOP_STREAM:
  211. LOG_INFO("Stream", NULL);
  212. raop_state = event;
  213. memset(raop_sync.error, 0, sizeof(raop_sync.error));
  214. raop_sync.idx = 0;
  215. raop_sync.start = true;
  216. raop_sync.enabled = !strcasestr(output.device, "BT");
  217. output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE;
  218. break;
  219. case RAOP_STOP:
  220. case RAOP_FLUSH:
  221. if (event == RAOP_FLUSH) { LOG_INFO("Flush", NULL); }
  222. else { LOG_INFO("Stop", NULL); }
  223. raop_state = event;
  224. _buf_flush(outputbuf);
  225. if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
  226. output.frames_played = 0;
  227. output.stop_time = gettime_ms();
  228. break;
  229. case RAOP_PLAY: {
  230. LOG_INFO("Play", NULL);
  231. if (raop_state != RAOP_PLAY) {
  232. output.state = OUTPUT_START_AT;
  233. output.start_at = va_arg(args, u32_t);
  234. raop_sync.start_time = output.start_at;
  235. LOG_INFO("Starting at %u (in %d ms)", output.start_at, output.start_at - gettime_ms());
  236. }
  237. raop_state = event;
  238. break;
  239. }
  240. case RAOP_VOLUME: {
  241. float volume = va_arg(args, double);
  242. LOG_INFO("Volume[0..1] %0.4f", volume);
  243. volume = 65536 * powf(volume, 3);
  244. set_volume((u16_t) volume, (u16_t) volume);
  245. break;
  246. }
  247. default:
  248. break;
  249. }
  250. if (event != RAOP_VOLUME) UNLOCK_O;
  251. UNLOCK_D;
  252. return true;
  253. }
  254. /****************************************************************************************
  255. * We provide the generic codec register option
  256. */
  257. void register_external(void) {
  258. char *p;
  259. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_bt_sink")) != NULL) {
  260. enable_bt_sink = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
  261. free(p);
  262. }
  263. if ((p = config_alloc_get(NVS_TYPE_STR, "enable_airplay")) != NULL) {
  264. enable_airplay = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
  265. free(p);
  266. }
  267. if (!strcasestr(output.device, "BT ") ) {
  268. if(enable_bt_sink){
  269. bt_sink_init(bt_sink_cmd_handler, sink_data_handler);
  270. LOG_INFO("Initializing BT sink");
  271. }
  272. } else {
  273. LOG_WARN("Cannot be a BT sink and source");
  274. }
  275. if (enable_airplay){
  276. raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
  277. LOG_INFO("Initializing AirPlay sink");
  278. }
  279. }
  280. void deregister_external(void) {
  281. if (!strcasestr(output.device, "BT ") && enable_bt_sink) {
  282. bt_sink_deinit();
  283. LOG_INFO("Stopping BT sink");
  284. }
  285. if (enable_airplay){
  286. raop_sink_deinit();
  287. LOG_INFO("Stopping AirPlay sink");
  288. }
  289. }
  290. void decode_restore(int external) {
  291. switch (external) {
  292. case DECODE_BT:
  293. bt_disconnect();
  294. break;
  295. case DECODE_RAOP:
  296. raop_disconnect();
  297. raop_state = RAOP_STOP;
  298. break;
  299. }
  300. }