output_bt.c 5.9 KB

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