decode_external.c 8.4 KB

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