output_bt.c 5.9 KB

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