cspot dsconnect when paused handling

This commit is contained in:
philippe44
2023-09-24 00:18:30 -07:00
parent 90f53db953
commit 53fa83b2dd
3 changed files with 32 additions and 13 deletions

View File

@@ -92,8 +92,7 @@ size_t cspotPlayer::pcmWrite(uint8_t *pcm, size_t bytes, std::string_view trackI
trackHandler(); trackHandler();
} }
dataHandler(pcm, bytes); return dataHandler(pcm, bytes);
return bytes;
} }
extern "C" { extern "C" {

View File

@@ -25,7 +25,7 @@ typedef enum { CSPOT_START, CSPOT_DISC, CSPOT_FLUSH, CSPOT_STOP, CSPOT_PLAY, CS
typedef bool (*cspot_cmd_cb_t)(cspot_event_t event, ...); typedef bool (*cspot_cmd_cb_t)(cspot_event_t event, ...);
typedef bool (*cspot_cmd_vcb_t)(cspot_event_t event, va_list args); typedef bool (*cspot_cmd_vcb_t)(cspot_event_t event, va_list args);
typedef void (*cspot_data_cb_t)(const uint8_t *data, size_t len); typedef uint32_t (*cspot_data_cb_t)(const uint8_t *data, size_t len);
/** /**
* @brief init sink mode (need to be provided) * @brief init sink mode (need to be provided)

View File

@@ -65,15 +65,16 @@ extern log_level loglevel;
/**************************************************************************************** /****************************************************************************************
* Common sink data handler * Common sink data handler
*/ */
static void sink_data_handler(const uint8_t *data, uint32_t len) static uint32_t sink_data_handler(const uint8_t *data, uint32_t len, int retries)
{ {
size_t bytes, space; size_t bytes, space;
int wait = 10; uint32_t written = 0;
int wait = retries + 1;
// would be better to lock output, but really, it does not matter // would be better to lock output, but really, it does not matter
if (!output.external) { if (!output.external) {
LOG_SDEBUG("Cannot use external sink while LMS is controlling player"); LOG_SDEBUG("Cannot use external sink while LMS is controlling player");
return; return 0;
} }
LOCK_O; LOCK_O;
@@ -98,27 +99,38 @@ static void sink_data_handler(const uint8_t *data, uint32_t len)
len -= bytes; len -= bytes;
data += bytes; data += bytes;
written += bytes;
// allow i2s to empty the buffer if needed // allow i2s to empty the buffer if needed
if (len && !space) { if (len && !space) {
if (output.state == OUTPUT_RUNNING) wait--; if (!retries) break;
wait--;
UNLOCK_O; usleep(50000); LOCK_O; UNLOCK_O; usleep(50000); LOCK_O;
} }
} }
if (!wait) { if (!wait) {
// re-align the buffer according to what we throw away // re-align the buffer according to what we threw away
_buf_inc_writep(outputbuf, outputbuf->size - (BYTES_PER_FRAME - (len % BYTES_PER_FRAME))); _buf_inc_writep(outputbuf, outputbuf->size - (BYTES_PER_FRAME - (len % BYTES_PER_FRAME)));
LOG_WARN("Waited too long, dropping frames %d", len); LOG_WARN("Waited too long, dropping frames %d", len);
} }
UNLOCK_O; UNLOCK_O;
return written;
}
/****************************************************************************************
* BT sink data handler
*/
#if CONFIG_BT_SINK
static void bt_sink_data_handler(const uint8_t *data, uint32_t len) {
sink_data_handler(data, len, 10);
} }
/**************************************************************************************** /****************************************************************************************
* BT sink command handler * BT sink command handler
*/ */
#if CONFIG_BT_SINK
static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args) static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args)
{ {
// don't LOCK_O as there is always a chance that LMS takes control later anyway // don't LOCK_O as there is always a chance that LMS takes control later anyway
@@ -195,7 +207,7 @@ static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t play
raop_sync.playtime = playtime; raop_sync.playtime = playtime;
raop_sync.len = len; raop_sync.len = len;
sink_data_handler(data, len); sink_data_handler(data, len, 10);
} }
/**************************************************************************************** /****************************************************************************************
@@ -332,9 +344,17 @@ static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
#endif #endif
/**************************************************************************************** /****************************************************************************************
* cspot sink command handler * cspot sink data handler
*/ */
#if CONFIG_CSPOT_SINK #if CONFIG_CSPOT_SINK
static uint32_t cspot_sink_data_handler(const uint8_t *data, uint32_t len) {
return sink_data_handler(data, len, 0);
}
/****************************************************************************************
* cspot sink command handler
*/
static bool cspot_cmd_handler(cspot_event_t cmd, va_list args) static bool cspot_cmd_handler(cspot_event_t cmd, va_list args)
{ {
// don't LOCK_O as there is always a chance that LMS takes control later anyway // don't LOCK_O as there is always a chance that LMS takes control later anyway
@@ -433,7 +453,7 @@ void register_external(void) {
enable_bt_sink = !strcmp(p,"1") || !strcasecmp(p,"y"); enable_bt_sink = !strcmp(p,"1") || !strcasecmp(p,"y");
free(p); free(p);
if (!strcasestr(output.device, "BT") && enable_bt_sink) { if (!strcasestr(output.device, "BT") && enable_bt_sink) {
bt_sink_init(bt_sink_cmd_handler, sink_data_handler); bt_sink_init(bt_sink_cmd_handler, bt_sink_data_handler);
} }
} }
#endif #endif
@@ -454,7 +474,7 @@ void register_external(void) {
enable_cspot = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0; enable_cspot = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
free(p); free(p);
if (enable_cspot){ if (enable_cspot){
cspot_sink_init(cspot_cmd_handler, sink_data_handler); cspot_sink_init(cspot_cmd_handler, cspot_sink_data_handler);
LOG_INFO("Initializing CSpot sink"); LOG_INFO("Initializing CSpot sink");
} }
} }