add BT sink

This commit is contained in:
philippe44
2019-08-12 21:19:14 -07:00
parent cf6d9c0911
commit d97d9fb7c9
11 changed files with 682 additions and 7 deletions

View File

@@ -12,7 +12,8 @@ CFLAGS += -O3 -DLINKALL -DLOOPBACK -DNO_FAAD -DRESAMPLE16 -DEMBEDDED -DTREMOR_ON
-I$(COMPONENT_PATH)/../codecs/inc/resample16 \
-I$(COMPONENT_PATH)/../tools \
-I$(COMPONENT_PATH)/../codecs/inc/opus \
-I$(COMPONENT_PATH)/../codecs/inc/opusfile
-I$(COMPONENT_PATH)/../codecs/inc/opusfile \
-I$(COMPONENT_PATH)/../driver_bt
# -I$(COMPONENT_PATH)/../codecs/inc/faad2

View File

@@ -198,6 +198,10 @@ void decode_init(log_level level, const char *include_codecs, const char *exclud
else if (!(strstr(exclude_codecs, "mp3") || strstr(exclude_codecs, "mpg")) &&
(!include_codecs || (order_codecs = strstr(include_codecs, "mp3")) || (order_codecs = strstr(include_codecs, "mpg"))))
sort_codecs((include_codecs ? order_codecs - include_codecs : i), register_mpg());
#if EMBEDDED
register_other();
#endif
LOG_DEBUG("include codecs: %s exclude codecs: %s", include_codecs ? include_codecs : "", exclude_codecs);

View File

@@ -0,0 +1,141 @@
/*
* Squeezelite for esp32
*
* (c) Sebastien 2019
* Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "squeezelite.h"
#include "bt_app_sink.h"
#define LOCK_O mutex_lock(outputbuf->mutex)
#define UNLOCK_O mutex_unlock(outputbuf->mutex)
#define LOCK_D mutex_lock(decode.mutex);
#define UNLOCK_D mutex_unlock(decode.mutex);
extern struct outputstate output;
extern struct decodestate decode;
extern struct buffer *outputbuf;
// this is the only system-wide loglevel variable
extern log_level loglevel;
/****************************************************************************************
* BT sink data handler
*/
static void bt_sink_data_handler(const uint8_t *data, uint32_t len)
{
size_t bytes;
// would be better to lock decoder, but really, it does not matter
if (decode.state != DECODE_STOPPED) {
LOG_WARN("Cannot use BT sink while LMS is controlling player");
return;
}
// there will always be room at some point
while (len) {
LOCK_O;
bytes = min(len, _buf_cont_write(outputbuf));
#if BYTES_PER_FRAME == 4
memcpy(outputbuf->writep, data, bytes);
#else
{
s16_t *iptr = (s16_t*) data;
ISAMPLE_T *optr = (ISAMPLE_T*) outputbuf->writep;
size_t n = bytes / BYTES_PER_FRAME * 2;
while (n--) *optr++ = *iptr++ << 16;
}
#endif
_buf_inc_writep(outputbuf, bytes);
len -= bytes;
data += bytes;
UNLOCK_O;
// allow i2s to empty the buffer if needed
if (len) usleep(50000);
}
}
/****************************************************************************************
* BT sink command handler
*/
static void bt_sink_cmd_handler(bt_sink_cmd_t cmd, ...)
{
va_list args;
LOCK_D;
if (decode.state != DECODE_STOPPED) {
LOG_WARN("Cannot use BT sink while LMS is controlling player");
UNLOCK_D;
return;
}
va_start(args, cmd);
if (cmd != BT_SINK_VOLUME) LOCK_O;
switch(cmd) {
case BT_SINK_CONNECTED:
output.state = OUTPUT_STOPPED;
LOG_INFO("BT sink started");
break;
case BT_SINK_DISCONNECTED:
output.state = OUTPUT_OFF;
LOG_INFO("BT sink stopped");
break;
case BT_SINK_PLAY:
output.state = OUTPUT_EXTERNAL;
LOG_INFO("BT sink playing");
break;
case BT_SINK_PAUSE:
case BT_SINK_STOP:
output.state = OUTPUT_STOPPED;
LOG_INFO("BT sink stopped");
break;
case BT_SINK_RATE:
output.current_sample_rate = va_arg(args, u32_t);
LOG_INFO("Setting BT sample rate %u", output.current_sample_rate);
break;
case BT_SINK_VOLUME: {
u16_t volume = (u16_t) va_arg(args, u32_t);
volume *= 65536 / 128;
set_volume(volume, volume);
break;
}
}
if (cmd != BT_SINK_VOLUME) UNLOCK_O;
UNLOCK_D;
va_end(args);
}
/****************************************************************************************
* We provide the generic codec register option
*/
void register_other(void) {
#ifdef CONFIG_BT_SINK
if (!strcasestr(output.device, "BT ")) {
bt_sink_init(bt_sink_cmd_handler, bt_sink_data_handler);
LOG_INFO("Initializing BT sink");
} else {
LOG_WARN("Cannot be a BT sink and source");
}
#endif
}

View File

@@ -39,5 +39,6 @@ typedef unsigned long long u64_t;
uint32_t _gettime_ms_(void);
int pthread_create_name(pthread_t *thread, _CONST pthread_attr_t *attr,
void *(*start_routine)( void * ), void *arg, char *name);
void register_other(void);
#endif // EMBEDDED_H

View File

@@ -37,6 +37,10 @@ extern void output_init_i2s(log_level level, char *device, unsigned output_buf_s
extern bool output_volume_i2s(unsigned left, unsigned right);
extern void output_close_i2s(void);
#ifdef CONFIG_BT_SINK
extern void decode_bt_init(void);
#endif
static log_level loglevel;
static bool (*volume_cb)(unsigned left, unsigned right);
@@ -52,7 +56,7 @@ void output_init_embedded(log_level level, char *device, unsigned output_buf_siz
output.start_frames = FRAME_BLOCK;
output.rate_delay = rate_delay;
if (strstr(device, "BT ")) {
if (strcasestr(device, "BT ")) {
LOG_INFO("init Bluetooth");
close_cb = &output_close_bt;
output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);

View File

@@ -414,7 +414,7 @@ static void *output_thread_i2s() {
// manage led display
if (state != output.state) {
LOG_INFO("Output state is %u", output.state);
LOG_INFO("Output state is %d", output.state);
if (output.state == OUTPUT_OFF) led_blink(LED_GREEN, 100, 2500);
else if (output.state == OUTPUT_STOPPED) led_blink(LED_GREEN, 200, 1000);
else if (output.state == OUTPUT_RUNNING) led_on(LED_GREEN);
@@ -604,7 +604,6 @@ extern const u16_t spdif_bmclookup[256];
*/
void spdif_convert(ISAMPLE_T *src, size_t frames, u32_t *dst, size_t *count) {
u16_t hi, lo, aux;
size_t pos;
// frames are 2 channels of 16 bits
frames *= 2;

View File

@@ -634,7 +634,7 @@ bool resample_init(char *opt);
// output.c output_alsa.c output_pa.c output_pack.c
typedef enum { OUTPUT_OFF = -1, OUTPUT_STOPPED = 0, OUTPUT_BUFFER, OUTPUT_RUNNING,
OUTPUT_PAUSE_FRAMES, OUTPUT_SKIP_FRAMES, OUTPUT_START_AT } output_state;
OUTPUT_PAUSE_FRAMES, OUTPUT_SKIP_FRAMES, OUTPUT_START_AT, OUTPUT_EXTERNAL } output_state;
#if DSD
typedef enum { PCM, DOP, DSD_U8, DSD_U16_LE, DSD_U32_LE, DSD_U16_BE, DSD_U32_BE, DOP_S24_LE, DOP_S24_3LE } dsd_format;