decode_external.c 5.5 KB

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