output_bt.c 6.0 KB

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