output_bt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, u8_t flags,
  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, u8_t flags,
  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. _apply_gain(outputbuf, out_frames, gainL, gainR, flags);
  76. #if BYTES_PER_FRAME == 4
  77. memcpy(btout + oframes * BYTES_PER_FRAME, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  78. #else
  79. {
  80. frames_t count = out_frames;
  81. s32_t *_iptr = (s32_t*) outputbuf->readp;
  82. s16_t *_optr = (s16_t*) (btout + oframes * BYTES_PER_FRAME);
  83. while (count--) {
  84. *_optr++ = *_iptr++ >> 16;
  85. *_optr++ = *_iptr++ >> 16;
  86. }
  87. }
  88. #endif
  89. } else {
  90. u8_t *buf = silencebuf;
  91. memcpy(btout + oframes * BYTES_PER_FRAME, buf, out_frames * BYTES_PER_FRAME);
  92. }
  93. output_visu_export(btout + oframes * BYTES_PER_FRAME, out_frames, output.current_sample_rate, silence, (gainL + gainR) / 2);
  94. oframes += out_frames;
  95. return (int)out_frames;
  96. }
  97. int32_t output_bt_data(uint8_t *data, int32_t len) {
  98. int32_t iframes = len / BYTES_PER_FRAME, start_timer = 0;
  99. if (iframes <= 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. SET_MIN_MAX(len,req);
  107. TIME_MEASUREMENT_START(start_timer);
  108. LOCK;
  109. SET_MIN_MAX_SIZED(_buf_used(outputbuf),bt,outputbuf->size);
  110. output.device_frames = 0;
  111. output.updated = gettime_ms();
  112. output.frames_played_dmp = output.frames_played;
  113. _output_frames(iframes);
  114. output.frames_in_process = oframes;
  115. UNLOCK;
  116. equalizer_process(data, oframes * BYTES_PER_FRAME, output.current_sample_rate);
  117. SET_MIN_MAX(TIME_MEASUREMENT_GET(start_timer),lock_out_time);
  118. SET_MIN_MAX((len-oframes*BYTES_PER_FRAME), rec);
  119. TIME_MEASUREMENT_START(start_timer);
  120. return oframes * BYTES_PER_FRAME;
  121. }
  122. void output_bt_tick(void) {
  123. static time_t lastTime=0;
  124. if (!running) return;
  125. LOCK_S;
  126. SET_MIN_MAX_SIZED(_buf_used(streambuf), stream_buf, streambuf->size);
  127. UNLOCK_S;
  128. if (stats && lastTime <= gettime_ms() )
  129. {
  130. lastTime = gettime_ms() + STATS_REPORT_DELAY_MS;
  131. LOG_INFO("Statistics over %u secs. " , STATS_REPORT_DELAY_MS/1000);
  132. LOG_INFO(" +==========+==========+================+=====+================+");
  133. LOG_INFO(" | max | min | average | avg | count |");
  134. LOG_INFO(" | (bytes) | (bytes) | (bytes) | pct | |");
  135. LOG_INFO(" +==========+==========+================+=====+================+");
  136. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("stream avl",stream_buf));
  137. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output avl",bt));
  138. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("requested",req));
  139. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  140. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("underrun",under));
  141. LOG_INFO( " +==========+==========+================+=====+================+");
  142. LOG_INFO("\n");
  143. LOG_INFO(" ==========+==========+===========+===========+ ");
  144. LOG_INFO(" max (us) | min (us) | avg(us) | count | ");
  145. LOG_INFO(" ==========+==========+===========+===========+ ");
  146. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Out Buf Lock",lock_out_time));
  147. LOG_INFO(" ==========+==========+===========+===========+");
  148. RESET_ALL_MIN_MAX;
  149. }
  150. }