decode_external.c 8.3 KB

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