From 7f97f621c4f8da77f5a542fb5aa2662485a724ff Mon Sep 17 00:00:00 2001 From: philippe44 Date: Mon, 26 Aug 2019 21:46:48 -0700 Subject: [PATCH] move some staks to external memory --- README.md | 3 +++ components/driver_bt/bt_app_sink.c | 15 ++++++++++++ components/driver_bt/bt_app_sink.h | 5 ++++ components/raop/raop.c | 29 +++++++++++++++++++----- components/raop/raop_sink.c | 8 +++++++ components/raop/raop_sink.h | 6 +++++ components/raop/rtp.c | 23 +++++++++++++++---- components/squeezelite/decode.c | 4 ++++ components/squeezelite/decode_external.c | 13 +++++++++++ components/squeezelite/embedded.h | 10 +++++--- components/squeezelite/slimproto.c | 1 - sdkconfig.defaults | 1 + 12 files changed, 103 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7a275d9a..2dcbc5d7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +TODO +- when IP changes, best is to reboot at this point + MOST IMPORTANT: create the right default config file - make defconfig Then adapt the config file to your wifi/BT/I2C device (can alos be done on the command line) diff --git a/components/driver_bt/bt_app_sink.c b/components/driver_bt/bt_app_sink.c index 3e8cd4c1..e9bb86ae 100644 --- a/components/driver_bt/bt_app_sink.c +++ b/components/driver_bt/bt_app_sink.c @@ -412,6 +412,21 @@ void bt_sink_init(bt_cmd_cb_t cmd_cb, bt_data_cb_t data_cb) } +void bt_sink_deinit(void) +{ + /* this still does not work, can't figure out how to stop properly this BT stack */ + bt_app_task_shut_down(); + ESP_LOGI(BT_AV_TAG, "bt_app_task shutdown successfully"); + if (esp_bluedroid_disable() != ESP_OK) return; + ESP_LOGI(BT_AV_TAG, "esp_bluedroid_disable called successfully"); + if (esp_bluedroid_deinit() != ESP_OK) return; + ESP_LOGI(BT_AV_TAG, "esp_bluedroid_deinit called successfully"); + if (esp_bt_controller_disable() != ESP_OK) return; + ESP_LOGI(BT_AV_TAG, "esp_bt_controller_disable called successfully"); + if (esp_bt_controller_deinit() != ESP_OK) return; + ESP_LOGI(BT_AV_TAG, "bt stopped successfully"); +} + static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) { switch (event) { diff --git a/components/driver_bt/bt_app_sink.h b/components/driver_bt/bt_app_sink.h index b3c0d71b..aa641bc8 100644 --- a/components/driver_bt/bt_app_sink.h +++ b/components/driver_bt/bt_app_sink.h @@ -22,6 +22,11 @@ typedef void (*bt_data_cb_t)(const uint8_t *data, uint32_t len); */ void bt_sink_init(bt_cmd_cb_t cmd_cb, bt_data_cb_t data_cb); +/** + * @brief deinit sink mode (need to be provided) + */ +void bt_sink_deinit(void); + /** * @brief local command mode (stop, play, volume ...) */ diff --git a/components/raop/raop.c b/components/raop/raop.c index 0f26d466..a659a724 100644 --- a/components/raop/raop.c +++ b/components/raop/raop.c @@ -43,6 +43,8 @@ #include "dmap_parser.h" #include "log_util.h" +#define RTSP_STACK_SIZE (8*1024) + typedef struct raop_ctx_s { #ifdef WIN32 struct mdns_service *svc; @@ -57,6 +59,8 @@ typedef struct raop_ctx_s { pthread_t thread, search_thread; #else TaskHandle_t thread, search_thread, joiner; + StaticTask_t *xTaskBuffer; + StackType_t *xStack; #endif unsigned char mac[6]; int latency; @@ -178,8 +182,14 @@ struct raop_ctx_s *raop_create(struct in_addr host, char *name, pthread_create(&ctx->thread, NULL, &rtsp_thread, ctx); #else LOG_INFO("starting mDNS with %s", id); - ESP_ERROR_CHECK( mdns_service_add(id, "_raop", "_tcp", ctx->port, txt, sizeof(txt) / sizeof(mdns_txt_item_t)) ); - xTaskCreate((TaskFunction_t) rtsp_thread, "RTSP_thread", 8*1024, ctx, ESP_TASK_PRIO_MIN + 1, &ctx->thread); + ESP_ERROR_CHECK( mdns_service_add(id, "_raop", "_tcp", ctx->port, txt, sizeof(txt) / sizeof(mdns_txt_item_t)) ); + + /* + xTaskCreate((TaskFunction_t) rtsp_thread, "RTSP_thread", 8*1024, ctx, ESP_TASK_PRIO_MIN + 1, &ctx->thread); + */ + ctx->xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + ctx->xStack = (StackType_t*) malloc(RTSP_STACK_SIZE); + ctx->thread = xTaskCreateStatic( (TaskFunction_t) rtsp_thread, "RTSP_thread", RTSP_STACK_SIZE, ctx, ESP_TASK_PRIO_MIN + 1, ctx->xStack, ctx->xTaskBuffer); #endif return ctx; @@ -192,7 +202,11 @@ void raop_delete(struct raop_ctx_s *ctx) { struct sockaddr addr; socklen_t nlen = sizeof(struct sockaddr); - if (!ctx) return; + if (!ctx) return; + +#if !defined WIN32 + ctx->joiner = xTaskGetCurrentTaskHandle(); +#endif ctx->running = false; @@ -205,8 +219,9 @@ void raop_delete(struct raop_ctx_s *ctx) { #ifdef WIN32 pthread_join(ctx->thread, NULL); #else - ctx->joiner = xTaskGetCurrentTaskHandle(); - xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); + xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); + free(ctx->xStack); + heap_caps_free(ctx->xTaskBuffer); #endif rtp_end(ctx->rtp); @@ -229,11 +244,13 @@ void raop_delete(struct raop_ctx_s *ctx) { NFREE(ctx->rtsp.aeskey); NFREE(ctx->rtsp.aesiv); NFREE(ctx->rtsp.fmtp); - + // stop broadcasting devices #ifdef WIN32 mdns_service_remove(ctx->svr, ctx->svc); mdnsd_stop(ctx->svr); +#else + mdns_service_remove("_raop", "_tcp"); #endif free(ctx); diff --git a/components/raop/raop_sink.c b/components/raop/raop_sink.c index bfe71328..092f1b57 100644 --- a/components/raop/raop_sink.c +++ b/components/raop/raop_sink.c @@ -31,6 +31,14 @@ log_level util_loglevel; static log_level *loglevel = &raop_loglevel; static struct raop_ctx_s *raop; +/**************************************************************************************** + * Airplay sink de-initialization + */ +void raop_sink_deinit(void) { + raop_delete(raop); + mdns_free(); +} + /**************************************************************************************** * Airplay sink initialization */ diff --git a/components/raop/raop_sink.h b/components/raop/raop_sink.h index 9f9718f2..bfe5e430 100644 --- a/components/raop/raop_sink.h +++ b/components/raop/raop_sink.h @@ -24,6 +24,12 @@ typedef void (*raop_data_cb_t)(const u8_t *data, size_t len, u32_t playtime); void raop_sink_init(raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb); +/** + * @brief deinit sink mode (need to be provided) + */ + +void raop_sink_deinit(void); + /** * @brief init sink mode (need to be provided) */ diff --git a/components/raop/rtp.c b/components/raop/rtp.c index bbd43502..8c0af127 100644 --- a/components/raop/rtp.c +++ b/components/raop/rtp.c @@ -75,6 +75,8 @@ #define MIN_LATENCY 11025 #define MAX_LATENCY ( (120 * RAOP_SAMPLE_RATE * 2) / 100 ) +#define RTP_STACK_SIZE (4*1024) + #define RTP_SYNC (0x01) #define NTP_SYNC (0x02) @@ -133,9 +135,11 @@ typedef struct rtp_s { seq_t ab_read, ab_write; pthread_mutex_t ab_mutex; #ifdef WIN32 - pthread_t rtp_thread; + pthread_t thread; #else - TaskHandle_t rtp_thread, joiner; + TaskHandle_t thread, joiner; + StaticTask_t *xTaskBuffer; + StackType_t *xStack; #endif alac_file *alac_codec; int flush_seqno; @@ -259,9 +263,13 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv, if (rc) { ctx->running = true; #ifdef WIN32 - pthread_create(&ctx->rtp_thread, NULL, rtp_thread_func, (void *) ctx); + pthread_create(&ctx->thread, NULL, rtp_thread_func, (void *) ctx); #else - xTaskCreate((TaskFunction_t) rtp_thread_func, "RTP_thread", 4096, ctx, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + 1 , &ctx->rtp_thread); + // xTaskCreate((TaskFunction_t) rtp_thread_func, "RTP_thread", RTP_TASK_SIZE, ctx, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + 1 , &ctx->thread); + ctx->xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + ctx->xStack = (StackType_t*) malloc(RTP_STACK_SIZE); + 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 ); #endif } else { rtp_end(ctx); @@ -281,14 +289,19 @@ void rtp_end(rtp_t *ctx) if (!ctx) return; if (ctx->running) { +#if !defined WIN32 + ctx->joiner = xTaskGetCurrentTaskHandle(); +#endif ctx->running = false; #ifdef WIN32 pthread_join(ctx->rtp_thread, NULL); #else - ctx->joiner = xTaskGetCurrentTaskHandle(); xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); + free(ctx->xStack); + heap_caps_free(ctx->xTaskBuffer); #endif } + for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock); delete_alac(ctx->alac_codec); diff --git a/components/squeezelite/decode.c b/components/squeezelite/decode.c index 5ef09c43..cc59f77a 100644 --- a/components/squeezelite/decode.c +++ b/components/squeezelite/decode.c @@ -116,6 +116,10 @@ static void *decode_thread() { usleep(100000); } } + +#if EMBEDDED + deregister_external(); +#endif return 0; } diff --git a/components/squeezelite/decode_external.c b/components/squeezelite/decode_external.c index 06f788f5..ee266b87 100644 --- a/components/squeezelite/decode_external.c +++ b/components/squeezelite/decode_external.c @@ -284,3 +284,16 @@ void register_external(void) { LOG_INFO("Initializing AirPlay sink"); #endif } + +void deregister_external(void) { +#ifdef CONFIG_BT_SINK + if (!strcasestr(output.device, "BT ")) { + bt_sink_deinit(); + LOG_INFO("Stopping BT sink"); + } +#endif +#ifdef CONFIG_AIRPLAY_SINK + raop_sink_deinit(); + LOG_INFO("Stopping AirPlay sink"); +#endif +} diff --git a/components/squeezelite/embedded.h b/components/squeezelite/embedded.h index 41eef2c0..8db425ab 100644 --- a/components/squeezelite/embedded.h +++ b/components/squeezelite/embedded.h @@ -36,9 +36,13 @@ typedef unsigned long long u64_t; #define gettime_ms _gettime_ms_ #define mutex_create_p(m) mutex_create(m) -uint32_t _gettime_ms_(void); -int pthread_create_name(pthread_t *thread, _CONST pthread_attr_t *attr, +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_external(void); + +// these are here as they can be #define to nothing +void register_external(void); +void deregister_external(void); #endif // EMBEDDED_H diff --git a/components/squeezelite/slimproto.c b/components/squeezelite/slimproto.c index 73407c65..42fb73b0 100644 --- a/components/squeezelite/slimproto.c +++ b/components/squeezelite/slimproto.c @@ -631,7 +631,6 @@ static void slimproto_run() { #endif last = now; - LOCK_S; status.stream_full = _buf_used(streambuf); status.stream_size = streambuf->size; diff --git a/sdkconfig.defaults b/sdkconfig.defaults index d228344c..be3817ac 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -80,6 +80,7 @@ CONFIG_SPIRAM_CACHE_WORKAROUND=y CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=256 CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536 CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y +CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY=y CONFIG_SPIRAM_OCCUPY_VSPI_HOST=y CONFIG_SPIRAM_BANKSWITCH_ENABLE=n CONFIG_D0WD_PSRAM_CLK_IO=17