Răsfoiți Sursa

move some staks to external memory

philippe44 5 ani în urmă
părinte
comite
7f97f621c4

+ 3 - 0
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
 MOST IMPORTANT: create the right default config file
 - make defconfig
 - make defconfig
 Then adapt the config file to your wifi/BT/I2C device (can alos be done on the command line)
 Then adapt the config file to your wifi/BT/I2C device (can alos be done on the command line)

+ 15 - 0
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)
 static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
 {
 {
     switch (event) {
     switch (event) {

+ 5 - 0
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);
 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 ...)
  * @brief     local command mode (stop, play, volume ...)
  */
  */

+ 23 - 6
components/raop/raop.c

@@ -43,6 +43,8 @@
 #include "dmap_parser.h"
 #include "dmap_parser.h"
 #include "log_util.h"
 #include "log_util.h"
 
 
+#define RTSP_STACK_SIZE (8*1024)
+
 typedef struct raop_ctx_s {
 typedef struct raop_ctx_s {
 #ifdef WIN32
 #ifdef WIN32
 	struct mdns_service *svc;
 	struct mdns_service *svc;
@@ -57,6 +59,8 @@ typedef struct raop_ctx_s {
 	pthread_t thread, search_thread;
 	pthread_t thread, search_thread;
 #else
 #else
 	TaskHandle_t thread, search_thread, joiner;
 	TaskHandle_t thread, search_thread, joiner;
+	StaticTask_t *xTaskBuffer;
+    StackType_t *xStack;
 #endif
 #endif
 	unsigned char mac[6];
 	unsigned char mac[6];
 	int latency;
 	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);
 	pthread_create(&ctx->thread, NULL, &rtsp_thread, ctx);
 
#else
 
#else
 	LOG_INFO("starting mDNS with %s", id);
 	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
 #endif
 
 
 	return ctx;
 	return ctx;
@@ -192,7 +202,11 @@ void raop_delete(struct raop_ctx_s *ctx) {
 	struct sockaddr addr;
 	struct sockaddr addr;
 	socklen_t nlen = sizeof(struct sockaddr);
 	socklen_t nlen = sizeof(struct sockaddr);
 
 
-	if (!ctx) return;
+	if (!ctx) return;
+	
+#if !defined WIN32
+	ctx->joiner = xTaskGetCurrentTaskHandle();
+#endif
 
 
 	ctx->running = false;
 	ctx->running = false;
 
 
@@ -205,8 +219,9 @@ void raop_delete(struct raop_ctx_s *ctx) {
 #ifdef WIN32
 #ifdef WIN32
 	pthread_join(ctx->thread, NULL);
 	pthread_join(ctx->thread, NULL);
 #else
 #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
 #endif
 
 
 	rtp_end(ctx->rtp);
 	rtp_end(ctx->rtp);
@@ -229,11 +244,13 @@ void raop_delete(struct raop_ctx_s *ctx) {
 	NFREE(ctx->rtsp.aeskey);
 	NFREE(ctx->rtsp.aeskey);
 	NFREE(ctx->rtsp.aesiv);
 	NFREE(ctx->rtsp.aesiv);
 	NFREE(ctx->rtsp.fmtp);
 	NFREE(ctx->rtsp.fmtp);
-
+	
 	// stop broadcasting devices
 	// stop broadcasting devices
 #ifdef WIN32
 #ifdef WIN32
 	mdns_service_remove(ctx->svr, ctx->svc);
 	mdns_service_remove(ctx->svr, ctx->svc);
 	mdnsd_stop(ctx->svr);
 	mdnsd_stop(ctx->svr);
+#else
+	mdns_service_remove("_raop", "_tcp");	
 #endif
 #endif
 
 
 	free(ctx);
 	free(ctx);

+ 8 - 0
components/raop/raop_sink.c

@@ -31,6 +31,14 @@ log_level	util_loglevel;
 static log_level *loglevel = &raop_loglevel;
 static log_level *loglevel = &raop_loglevel;
 static struct raop_ctx_s *raop;
 static struct raop_ctx_s *raop;
 
 
+/****************************************************************************************
+ * Airplay sink de-initialization
+ */
+void raop_sink_deinit(void) {
+	raop_delete(raop);
+	mdns_free();
+}	
+
 /****************************************************************************************
 /****************************************************************************************
  * Airplay sink initialization
  * Airplay sink initialization
  */
  */

+ 6 - 0
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);
 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)
  * @brief     init sink mode (need to be provided)
  */
  */

+ 18 - 5
components/raop/rtp.c

@@ -75,6 +75,8 @@
 #define MIN_LATENCY		11025
 #define MIN_LATENCY		11025
 #define MAX_LATENCY   	( (120 * RAOP_SAMPLE_RATE * 2) / 100 )
 #define MAX_LATENCY   	( (120 * RAOP_SAMPLE_RATE * 2) / 100 )
 
 
+#define RTP_STACK_SIZE	(4*1024)
+
 #define RTP_SYNC	(0x01)
 #define RTP_SYNC	(0x01)
 #define NTP_SYNC	(0x02)
 #define NTP_SYNC	(0x02)
 
 
@@ -133,9 +135,11 @@ typedef struct rtp_s {
 	seq_t ab_read, ab_write;
 	seq_t ab_read, ab_write;
 	pthread_mutex_t ab_mutex;
 	pthread_mutex_t ab_mutex;
 #ifdef WIN32
 #ifdef WIN32
-	pthread_t rtp_thread;
+	pthread_t thread;
 #else
 #else
-	TaskHandle_t rtp_thread, joiner;
+	TaskHandle_t thread, joiner;
+	StaticTask_t *xTaskBuffer;
+    StackType_t *xStack;
 #endif
 #endif
 	alac_file *alac_codec;
 	alac_file *alac_codec;
 	int flush_seqno;
 	int flush_seqno;
@@ -259,9 +263,13 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv,
 	if (rc) {
 	if (rc) {
 		ctx->running = true;
 		ctx->running = true;
 #ifdef WIN32
 #ifdef WIN32
-		pthread_create(&ctx->rtp_thread, NULL, rtp_thread_func, (void *) ctx);
+		pthread_create(&ctx->thread, NULL, rtp_thread_func, (void *) ctx);
 #else
 #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
 #endif
 	} else {
 	} else {
 		rtp_end(ctx);
 		rtp_end(ctx);
@@ -281,14 +289,19 @@ void rtp_end(rtp_t *ctx)
 	if (!ctx) return;
 	if (!ctx) return;
 
 
 	if (ctx->running) {
 	if (ctx->running) {
+#if !defined WIN32		
+		ctx->joiner = xTaskGetCurrentTaskHandle();
+#endif
 		ctx->running = false;
 		ctx->running = false;
 #ifdef WIN32
 #ifdef WIN32
 		pthread_join(ctx->rtp_thread, NULL);
 		pthread_join(ctx->rtp_thread, NULL);
 #else
 #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
 #endif
 	}
 	}
+	
 	for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock);
 	for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock);
 
 
 	delete_alac(ctx->alac_codec);
 	delete_alac(ctx->alac_codec);

+ 4 - 0
components/squeezelite/decode.c

@@ -116,6 +116,10 @@ static void *decode_thread() {
 			usleep(100000);
 			usleep(100000);
 		}
 		}
 	}
 	}
+	
+#if EMBEDDED	
+	deregister_external();
+#endif	
 
 
 	return 0;
 	return 0;
 }
 }

+ 13 - 0
components/squeezelite/decode_external.c

@@ -284,3 +284,16 @@ void register_external(void) {
 	LOG_INFO("Initializing AirPlay sink");		
 	LOG_INFO("Initializing AirPlay sink");		
 #endif
 #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
+}

+ 7 - 3
components/squeezelite/embedded.h

@@ -36,9 +36,13 @@ typedef unsigned long long u64_t;
 #define gettime_ms _gettime_ms_
 #define gettime_ms _gettime_ms_
 #define mutex_create_p(m) mutex_create(m)
 #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 *(*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
 #endif // EMBEDDED_H

+ 0 - 1
components/squeezelite/slimproto.c

@@ -631,7 +631,6 @@ static void slimproto_run() {
 #endif
 #endif
 			last = now;
 			last = now;
 
 
-
 			LOCK_S;
 			LOCK_S;
 			status.stream_full = _buf_used(streambuf);
 			status.stream_full = _buf_used(streambuf);
 			status.stream_size = streambuf->size;
 			status.stream_size = streambuf->size;

+ 1 - 0
sdkconfig.defaults

@@ -80,6 +80,7 @@ CONFIG_SPIRAM_CACHE_WORKAROUND=y
 CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=256
 CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=256
 CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536
 CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536
 CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
 CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
+CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY=y
 CONFIG_SPIRAM_OCCUPY_VSPI_HOST=y
 CONFIG_SPIRAM_OCCUPY_VSPI_HOST=y
 CONFIG_SPIRAM_BANKSWITCH_ENABLE=n
 CONFIG_SPIRAM_BANKSWITCH_ENABLE=n
 CONFIG_D0WD_PSRAM_CLK_IO=17
 CONFIG_D0WD_PSRAM_CLK_IO=17