decode_bt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define LOCK_O mutex_lock(outputbuf->mutex)
  24. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  25. #define LOCK_D mutex_lock(decode.mutex);
  26. #define UNLOCK_D mutex_unlock(decode.mutex);
  27. extern struct outputstate output;
  28. extern struct decodestate decode;
  29. extern struct buffer *outputbuf;
  30. // this is the only system-wide loglevel variable
  31. extern log_level loglevel;
  32. /****************************************************************************************
  33. * BT sink data handler
  34. */
  35. static void bt_sink_data_handler(const uint8_t *data, uint32_t len)
  36. {
  37. size_t bytes;
  38. // would be better to lock decoder, but really, it does not matter
  39. if (decode.state != DECODE_STOPPED) {
  40. LOG_SDEBUG("Cannot use BT sink while LMS is controlling player");
  41. return;
  42. }
  43. // there will always be room at some point
  44. while (len) {
  45. LOCK_O;
  46. bytes = min(len, _buf_cont_write(outputbuf));
  47. #if BYTES_PER_FRAME == 4
  48. memcpy(outputbuf->writep, data, bytes);
  49. #else
  50. {
  51. s16_t *iptr = (s16_t*) data;
  52. ISAMPLE_T *optr = (ISAMPLE_T*) outputbuf->writep;
  53. size_t n = bytes / BYTES_PER_FRAME * 2;
  54. while (n--) *optr++ = *iptr++ << 16;
  55. }
  56. #endif
  57. _buf_inc_writep(outputbuf, bytes);
  58. len -= bytes;
  59. data += bytes;
  60. UNLOCK_O;
  61. // allow i2s to empty the buffer if needed
  62. if (len) usleep(50000);
  63. }
  64. }
  65. /****************************************************************************************
  66. * BT sink command handler
  67. */
  68. static void bt_sink_cmd_handler(bt_sink_cmd_t cmd, ...)
  69. {
  70. va_list args;
  71. LOCK_D;
  72. if (decode.state != DECODE_STOPPED) {
  73. LOG_WARN("Cannot use BT sink while LMS is controlling player");
  74. UNLOCK_D;
  75. return;
  76. }
  77. va_start(args, cmd);
  78. if (cmd != BT_SINK_VOLUME) LOCK_O;
  79. switch(cmd) {
  80. case BT_SINK_CONNECTED:
  81. output.state = OUTPUT_STOPPED;
  82. LOG_INFO("BT sink started");
  83. break;
  84. case BT_SINK_DISCONNECTED:
  85. output.state = OUTPUT_OFF;
  86. LOG_INFO("BT sink stopped");
  87. break;
  88. case BT_SINK_PLAY:
  89. output.state = OUTPUT_EXTERNAL;
  90. LOG_INFO("BT sink playing");
  91. break;
  92. case BT_SINK_PAUSE:
  93. case BT_SINK_STOP:
  94. output.state = OUTPUT_STOPPED;
  95. LOG_INFO("BT sink stopped");
  96. break;
  97. case BT_SINK_RATE:
  98. output.current_sample_rate = va_arg(args, u32_t);
  99. LOG_INFO("Setting BT sample rate %u", output.current_sample_rate);
  100. break;
  101. case BT_SINK_VOLUME: {
  102. u16_t volume = (u16_t) va_arg(args, u32_t);
  103. volume *= 65536 / 128;
  104. set_volume(volume, volume);
  105. break;
  106. }
  107. }
  108. if (cmd != BT_SINK_VOLUME) UNLOCK_O;
  109. UNLOCK_D;
  110. va_end(args);
  111. }
  112. /****************************************************************************************
  113. * We provide the generic codec register option
  114. */
  115. void register_other(void) {
  116. #ifdef CONFIG_BT_SINK
  117. if (!strcasestr(output.device, "BT ")) {
  118. bt_sink_init(bt_sink_cmd_handler, bt_sink_data_handler);
  119. LOG_INFO("Initializing BT sink");
  120. } else {
  121. LOG_WARN("Cannot be a BT sink and source");
  122. }
  123. #endif
  124. }