From efc2c9515ccb6a022bce10f0498ea42856723b53 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Tue, 21 Dec 2021 23:19:02 -0800 Subject: [PATCH] BT/RAOP switch improvments + pin tasks to core 1 (pthread) --- components/raop/raop.c | 7 +++++-- components/raop/rtp.c | 5 +++-- components/raop/util.h | 2 +- components/squeezelite/decode_external.c | 3 +++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/components/raop/raop.c b/components/raop/raop.c index e419736d..05103a74 100644 --- a/components/raop/raop.c +++ b/components/raop/raop.c @@ -196,7 +196,8 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name, ESP_ERROR_CHECK( mdns_service_add(id, "_raop", "_tcp", ctx->port, txt, sizeof(txt) / sizeof(mdns_txt_item_t)) ); ctx->xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - ctx->thread = xTaskCreateStatic( (TaskFunction_t) rtsp_thread, "RTSP_thread", RTSP_STACK_SIZE, ctx, ESP_TASK_PRIO_MIN + 2, ctx->xStack, ctx->xTaskBuffer); + ctx->thread = xTaskCreateStaticPinnedToCore( (TaskFunction_t) rtsp_thread, "RTSP_thread", RTSP_STACK_SIZE, ctx, + ESP_TASK_PRIO_MIN + 2, ctx->xStack, ctx->xTaskBuffer, CONFIG_PTHREAD_TASK_CORE_DEFAULT); #endif return ctx; @@ -518,7 +519,9 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock) ctx->active_remote.running = true; ctx->active_remote.destroy_mutex = xSemaphoreCreateBinary(); ctx->active_remote.xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - ctx->active_remote.thread = xTaskCreateStatic( (TaskFunction_t) search_remote, "search_remote", SEARCH_STACK_SIZE, ctx, ESP_TASK_PRIO_MIN + 2, ctx->active_remote.xStack, ctx->active_remote.xTaskBuffer); + ctx->active_remote.thread = xTaskCreateStaticPinnedToCore( (TaskFunction_t) search_remote, "search_remote", SEARCH_STACK_SIZE, ctx, + ESP_TASK_PRIO_MIN + 2, ctx->active_remote.xStack, ctx->active_remote.xTaskBuffer, + CONFIG_PTHREAD_TASK_CORE_DEFAULT ); #endif } else if (!strcmp(method, "SETUP") && ((buf = kd_lookup(headers, "Transport")) != NULL)) { diff --git a/components/raop/rtp.c b/components/raop/rtp.c index 11218483..2aead7f2 100644 --- a/components/raop/rtp.c +++ b/components/raop/rtp.c @@ -281,8 +281,9 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv, pthread_create(&ctx->thread, NULL, rtp_thread_func, (void *) ctx); #else ctx->xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - ctx->thread = xTaskCreateStatic( (TaskFunction_t) rtp_thread_func, "RTP_thread", RTP_STACK_SIZE, ctx, - CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + 1, ctx->xStack, ctx->xTaskBuffer ); + ctx->thread = xTaskCreateStaticPinnedToCore( (TaskFunction_t) rtp_thread_func, "RTP_thread", RTP_STACK_SIZE, ctx, + CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + 1, ctx->xStack, ctx->xTaskBuffer, + CONFIG_PTHREAD_TASK_CORE_DEFAULT ); #endif // cleanup everything if we failed diff --git a/components/raop/util.h b/components/raop/util.h index 94001895..75016521 100644 --- a/components/raop/util.h +++ b/components/raop/util.h @@ -57,7 +57,7 @@ char *strlwr(char *str); // reason is that TCB might be cleanup in idle task #define SAFE_PTR_FREE(P) \ do { \ - TimerHandle_t timer = xTimerCreate("cleanup", pdMS_TO_TICKS(5000), pdFALSE, P, _delayed_free); \ + TimerHandle_t timer = xTimerCreate("cleanup", pdMS_TO_TICKS(10000), pdFALSE, P, _delayed_free); \ xTimerStart(timer, portMAX_DELAY); \ } while (0) static void inline _delayed_free(TimerHandle_t xTimer) { diff --git a/components/squeezelite/decode_external.c b/components/squeezelite/decode_external.c index 6f5f69b5..7c16e064 100644 --- a/components/squeezelite/decode_external.c +++ b/components/squeezelite/decode_external.c @@ -138,6 +138,7 @@ static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args) case BT_SINK_AUDIO_STOPPED: if (output.external == DECODE_BT) { if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED; + output.external = 0; output.stop_time = gettime_ms(); LOG_INFO("BT sink stopped"); } @@ -282,6 +283,8 @@ static bool raop_sink_cmd_handler(raop_event_t event, va_list args) output.next_sample_rate = output.current_sample_rate = RAOP_SAMPLE_RATE; break; case RAOP_STOP: + output.external = 0; + __attribute__ ((fallthrough)); case RAOP_FLUSH: LOG_INFO("%s", event == RAOP_FLUSH ? "Flush" : "Stop"); _buf_flush(outputbuf);