decode_external.c 6.7 KB

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