output_bt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "driver/gpio.h"
  22. #include "squeezelite.h"
  23. #include "perf_trace.h"
  24. extern struct outputstate output;
  25. extern struct buffer *outputbuf;
  26. extern struct buffer *streambuf;
  27. extern u8_t *silencebuf;
  28. #define LOCK mutex_lock(outputbuf->mutex)
  29. #define UNLOCK mutex_unlock(outputbuf->mutex)
  30. #define LOCK_S mutex_lock(streambuf->mutex)
  31. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  32. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  33. #define STATS_REPORT_DELAY_MS 15000
  34. extern void hal_bluetooth_init(const char * options);
  35. extern void hal_bluetooth_stop(void);
  36. extern u8_t config_spdif_gpio;
  37. static log_level loglevel;
  38. static bool running = false;
  39. static uint8_t *btout;
  40. static frames_t oframes;
  41. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  42. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  43. #define DECLARE_ALL_MIN_MAX \
  44. DECLARE_MIN_MAX(req);\
  45. DECLARE_MIN_MAX(rec);\
  46. DECLARE_MIN_MAX(bt);\
  47. DECLARE_MIN_MAX(under);\
  48. DECLARE_MIN_MAX(stream_buf);\
  49. DECLARE_MIN_MAX_DURATION(lock_out_time)
  50. #define RESET_ALL_MIN_MAX \
  51. RESET_MIN_MAX(bt); \
  52. RESET_MIN_MAX(req); \
  53. RESET_MIN_MAX(rec); \
  54. RESET_MIN_MAX(under); \
  55. RESET_MIN_MAX(stream_buf); \
  56. RESET_MIN_MAX_DURATION(lock_out_time)
  57. DECLARE_ALL_MIN_MAX;
  58. void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  59. #ifdef CONFIG_SQUEEZEAMP
  60. gpio_pad_select_gpio(config_spdif_gpio);
  61. gpio_set_direction(config_spdif_gpio, GPIO_MODE_OUTPUT);
  62. gpio_set_level(config_spdif_gpio, 0);
  63. #endif
  64. loglevel = level;
  65. running = true;
  66. output.write_cb = &_write_frames;
  67. hal_bluetooth_init(device);
  68. }
  69. void output_close_bt(void) {
  70. LOCK;
  71. running = false;
  72. UNLOCK;
  73. hal_bluetooth_stop();
  74. }
  75. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  76. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  77. assert(btout != NULL);
  78. if (!silence ) {
  79. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  80. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  81. }
  82. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  83. _apply_gain(outputbuf, out_frames, gainL, gainR);
  84. }
  85. #if BYTES_PER_FRAME == 4
  86. memcpy(btout + oframes * BYTES_PER_FRAME, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  87. #else
  88. {
  89. frames_t count = out_frames;
  90. s32_t *_iptr = (s32_t*) outputbuf->readp;
  91. s16_t *_optr = (s16_t*) (btout + oframes * BYTES_PER_FRAME);
  92. while (count--) {
  93. *_optr++ = *_iptr++ >> 16;
  94. *_optr++ = *_iptr++ >> 16;
  95. }
  96. }
  97. #endif
  98. } else {
  99. u8_t *buf = silencebuf;
  100. memcpy(btout + oframes * BYTES_PER_FRAME, buf, out_frames * BYTES_PER_FRAME);
  101. }
  102. return (int)out_frames;
  103. }
  104. int32_t output_bt_data(uint8_t *data, int32_t len) {
  105. int32_t avail_data = 0, wanted_len = 0, start_timer = 0;
  106. if (len < 0 || data == NULL || !running) {
  107. return 0;
  108. }
  109. btout = data;
  110. oframes = 0;
  111. // This is how the BTC layer calculates the number of bytes to
  112. // for us to send. (BTC_SBC_DEC_PCM_DATA_LEN * sizeof(OI_INT16) - availPcmBytes
  113. wanted_len=len;
  114. SET_MIN_MAX(len,req);
  115. TIME_MEASUREMENT_START(start_timer);
  116. LOCK;
  117. output.device_frames = 0; // todo: check if this is the right way do to this.
  118. output.updated = gettime_ms();
  119. output.frames_played_dmp = output.frames_played;
  120. SET_MIN_MAX_SIZED(_buf_used(outputbuf),bt,outputbuf->size);
  121. do {
  122. avail_data = _output_frames( wanted_len/BYTES_PER_FRAME )*BYTES_PER_FRAME; // Keep the transfer buffer full
  123. wanted_len-=avail_data;
  124. } while (wanted_len > 0 && avail_data != 0);
  125. if (wanted_len > 0) {
  126. SET_MIN_MAX(wanted_len, under);
  127. }
  128. output.frames_in_process = len-wanted_len;
  129. UNLOCK;
  130. SET_MIN_MAX(TIME_MEASUREMENT_GET(start_timer),lock_out_time);
  131. SET_MIN_MAX((len-wanted_len), rec);
  132. TIME_MEASUREMENT_START(start_timer);
  133. return len-wanted_len;
  134. }
  135. void output_bt_tick(void) {
  136. static time_t lastTime=0;
  137. if (!running) return;
  138. LOCK_S;
  139. SET_MIN_MAX_SIZED(_buf_used(streambuf), stream_buf, streambuf->size);
  140. UNLOCK_S;
  141. if (lastTime <= gettime_ms() )
  142. {
  143. lastTime = gettime_ms() + STATS_REPORT_DELAY_MS;
  144. LOG_INFO("Statistics over %u secs. " , STATS_REPORT_DELAY_MS/1000);
  145. LOG_INFO(" +==========+==========+================+=====+================+");
  146. LOG_INFO(" | max | min | average | avg | count |");
  147. LOG_INFO(" | (bytes) | (bytes) | (bytes) | pct | |");
  148. LOG_INFO(" +==========+==========+================+=====+================+");
  149. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("stream avl",stream_buf));
  150. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output avl",bt));
  151. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("requested",req));
  152. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  153. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("underrun",under));
  154. LOG_INFO( " +==========+==========+================+=====+================+");
  155. LOG_INFO("\n");
  156. LOG_INFO(" ==========+==========+===========+===========+ ");
  157. LOG_INFO(" max (us) | min (us) | avg(us) | count | ");
  158. LOG_INFO(" ==========+==========+===========+===========+ ");
  159. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Out Buf Lock",lock_out_time));
  160. LOG_INFO(" ==========+==========+===========+===========+");
  161. RESET_ALL_MIN_MAX;
  162. }
  163. }