BT, EMBEDDED, thread joining

Fix a few BT issues (detection & sink name)
End threads on any server missing for too long (except if server IP set
manually)
Still cannot end threads when BT active (works wit I2S)
Thread lokc-up still happens when switching tracks quickly
This commit is contained in:
philippe44
2019-06-30 23:29:34 -07:00
parent e492b57b3b
commit 83f9f3320e
9 changed files with 73 additions and 26 deletions

View File

@@ -234,7 +234,7 @@ void decode_close(void) {
}
running = false;
UNLOCK_D;
#if LINUX || OSX || FREEBSD
#if LINUX || OSX || FREEBSD || EMBEDDED
pthread_join(thread, NULL);
#endif
mutex_destroy(decode.mutex);

View File

@@ -20,6 +20,7 @@ typedef int32_t s32_t;
typedef int64_t s64_t;
typedef unsigned long long u64_t;
// all exit() calls are made from main thread (or a function called in main thread)
#define exit(code) { int ret = code; pthread_exit(&ret); }
int pthread_setname_np(pthread_t thread, const char *name);

View File

@@ -37,8 +37,10 @@ extern u8_t *silencebuf;
#define STATS_REPORT_DELAY_MS 15000
extern void hal_bluetooth_init(const char * options);
extern void hal_bluetooth_stop(void);
static log_level loglevel;
static bool running = false;
uint8_t * btout;
static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
@@ -64,10 +66,18 @@ DECLARE_ALL_MIN_MAX;
void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
loglevel = level;
hal_bluetooth_init(device);
running = true;
output.write_cb = &_write_frames;
hal_bluetooth_init(device);
}
void output_close_bt(void) {
LOCK;
running = false;
UNLOCK;
hal_bluetooth_stop();
}
static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
@@ -109,7 +119,7 @@ static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t g
int32_t output_bt_data(uint8_t *data, int32_t len) {
int32_t avail_data = 0, wanted_len = 0, start_timer = 0;
if (len < 0 || data == NULL ) {
if (len < 0 || data == NULL || !running) {
return 0;
}
@@ -145,6 +155,8 @@ int32_t output_bt_data(uint8_t *data, int32_t len) {
void output_bt_tick(void) {
static time_t lastTime=0;
if (!running) return;
LOCK_S;
SET_MIN_MAX_SIZED(_buf_used(streambuf), stream_buf, streambuf->size);
UNLOCK_S;
@@ -171,5 +183,4 @@ void output_bt_tick(void) {
LOG_INFO(" ==========+==========+===========+===========+");
RESET_ALL_MIN_MAX;
}
}
}

View File

@@ -33,6 +33,7 @@ extern void output_init_bt(log_level level, char *device, unsigned output_buf_si
extern void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params,
unsigned rates[], unsigned rate_delay, unsigned idle);
extern void output_close_i2s(void);
extern void output_close_bt(void);
static log_level loglevel;
@@ -46,15 +47,16 @@ void output_init_embedded(log_level level, char *device, unsigned output_buf_siz
memset(&output, 0, sizeof(output));
output_init_common(level, device, output_buf_size, rates, idle);
output.start_frames = FRAME_BLOCK;
output.start_frames = FRAME_BLOCK;
output.rate_delay = rate_delay;
if (strstr(device, "BT ")) {
LOG_INFO("init Bluetooth");
close_cb = &output_close_bt;
output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);
} else {
LOG_INFO("init I2S");
close_cb = output_close_i2s;
close_cb = &output_close_i2s;
output_init_i2s(level, device, output_buf_size, params, rates, rate_delay, idle);
}
@@ -63,8 +65,8 @@ void output_init_embedded(log_level level, char *device, unsigned output_buf_siz
void output_close_embedded(void) {
LOG_INFO("close output");
output_close_common();
if (close_cb) (*close_cb)();
output_close_common();
}
void set_volume(unsigned left, unsigned right) {

View File

@@ -167,6 +167,11 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
* Terminate DAC output
*/
void output_close_i2s(void) {
LOCK;
running = false;
UNLOCK;
pthread_join(thread, NULL);
pthread_join(stats_thread, NULL);
i2s_driver_uninstall(CONFIG_I2S_NUM);
free(obuf);
}

View File

@@ -775,6 +775,11 @@ in_addr_t discover_server(char *default_server) {
char *buf;
struct pollfd pollinfo;
unsigned port;
int attempts = 0;
#if EMBEDDED
attempts = 5;
#endif
int disc_sock = socket(AF_INET, SOCK_DGRAM, 0);
@@ -811,7 +816,7 @@ in_addr_t discover_server(char *default_server) {
server_addr(default_server, &s.sin_addr.s_addr, &port);
}
} while (s.sin_addr.s_addr == 0 && running);
} while (s.sin_addr.s_addr == 0 && running && (!attempts || --attempts));
closesocket(disc_sock);
@@ -896,7 +901,7 @@ void slimproto(log_level level, char *server, u8_t mac[6], const char *name, con
new_server = 0;
while (running) {
while (running && slimproto_ip) {
if (new_server) {
previous_server = slimproto_ip;

View File

@@ -428,7 +428,7 @@ void stream_close(void) {
LOCK;
running = false;
UNLOCK;
#if LINUX || OSX || FREEBSD || POSIX
#if LINUX || OSX || FREEBSD || EMBEDDED
pthread_join(thread, NULL);
#endif
free(stream.header);