output_bt.c 5.7 KB

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