decode_external.c 7.0 KB

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