decode_external.c 8.5 KB

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