Selaa lähdekoodia

json button mapping config

Sebastien 5 vuotta sitten
vanhempi
commit
c85d2afbaf

+ 29 - 5
.cproject

@@ -489,43 +489,67 @@
         		
         <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
         		
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cygwin.base.58932738;cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.1067614858;cdt.managedbuild.tool.gnu.c.compiler.cygwin.base.1865841553;cdt.managedbuild.tool.gnu.c.compiler.input.cygwin.1383814557">
+            			
+            <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+            		
+        </scannerConfigBuildInfo>
+        		
         <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736.;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1907231332;cdt.managedbuild.tool.gnu.cpp.compiler.input.604467026">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1722031516;cdt.managedbuild.tool.gnu.cpp.compiler.input.2073997022">
+            			
+            <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+            		
+        </scannerConfigBuildInfo>
+        		
         <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.;cdt.managedbuild.tool.gnu.cross.c.compiler.1302011176;cdt.managedbuild.tool.gnu.c.compiler.input.539301587">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
-        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1943329896;cdt.managedbuild.toolchain.gnu.cross.base.1943329896.30011915;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1749746745;cdt.managedbuild.tool.gnu.cpp.compiler.input.1914005798">
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cygwin.base.58932738;cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.1067614858;cdt.managedbuild.tool.gnu.cpp.compiler.cygwin.base.410547198;cdt.managedbuild.tool.gnu.cpp.compiler.input.cygwin.1499974240">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
-        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1476804786;cdt.managedbuild.toolchain.gnu.cross.base.1476804786.1800826258;cdt.managedbuild.tool.gnu.cross.cpp.compiler.254690821;cdt.managedbuild.tool.gnu.cpp.compiler.input.1365876654">
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736.;cdt.managedbuild.tool.gnu.cross.c.compiler.824219909;cdt.managedbuild.tool.gnu.c.compiler.input.644208200">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
-        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1943329896;cdt.managedbuild.toolchain.gnu.cross.base.1943329896.30011915;cdt.managedbuild.tool.gnu.cross.c.compiler.2083405506;cdt.managedbuild.tool.gnu.c.compiler.input.404320567">
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707;cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707.;cdt.managedbuild.tool.gnu.c.compiler.cygwin.base.211315976;cdt.managedbuild.tool.gnu.c.compiler.input.cygwin.857914729">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
-        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736.;cdt.managedbuild.tool.gnu.cross.c.compiler.824219909;cdt.managedbuild.tool.gnu.c.compiler.input.644208200">
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1943329896;cdt.managedbuild.toolchain.gnu.cross.base.1943329896.30011915;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1749746745;cdt.managedbuild.tool.gnu.cpp.compiler.input.1914005798">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		
         </scannerConfigBuildInfo>
         		
-        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291;cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.;cdt.managedbuild.tool.gnu.cross.cpp.compiler.1722031516;cdt.managedbuild.tool.gnu.cpp.compiler.input.2073997022">
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1476804786;cdt.managedbuild.toolchain.gnu.cross.base.1476804786.1800826258;cdt.managedbuild.tool.gnu.cross.cpp.compiler.254690821;cdt.managedbuild.tool.gnu.cpp.compiler.input.1365876654">
+            			
+            <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+            		
+        </scannerConfigBuildInfo>
+        		
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cross.base.1943329896;cdt.managedbuild.toolchain.gnu.cross.base.1943329896.30011915;cdt.managedbuild.tool.gnu.cross.c.compiler.2083405506;cdt.managedbuild.tool.gnu.c.compiler.input.404320567">
+            			
+            <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+            		
+        </scannerConfigBuildInfo>
+        		
+        <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707;cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707.;cdt.managedbuild.tool.gnu.cpp.compiler.cygwin.base.145410566;cdt.managedbuild.tool.gnu.cpp.compiler.input.cygwin.1181636367">
             			
             <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
             		

+ 1 - 0
.gitignore

@@ -66,3 +66,4 @@ libs/
 /cdump.cmd
 /_*
 sdkconfig
+squeezelite-esp32-jsonblob.zip

+ 22 - 0
.settings/org.eclipse.cdt.core.prefs

@@ -92,3 +92,25 @@ environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_
 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_VER/value=custom
 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/append=true
 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/appendContributed=true
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/BATCH_BUILD/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/BATCH_BUILD/operation=append
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/BATCH_BUILD/value=1
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/IDF_PATH/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/IDF_PATH/operation=replace
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/IDF_PATH/value=c\:/msys32/opt/esp-idf
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/PATH/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/PATH/operation=replace
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/PATH/value=C\:\\msys32\\usr\\bin;C\:\\msys32\\mingw32\\bin;C\:\\msys32\\opt\\xtensa-esp32-elf\\bin;C\:\\Python27
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/append=true
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/appendContributed=false
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/BATCH_BUILD/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/BATCH_BUILD/operation=append
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/BATCH_BUILD/value=1
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/IDF_PATH/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/IDF_PATH/operation=replace
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/IDF_PATH/value=c\:/msys32/opt/esp-idf
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/PATH/delimiter=;
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/PATH/operation=replace
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/PATH/value=C\:\\msys32\\usr\\bin;C\:\\msys32\\mingw32\\bin;C\:\\msys32\\opt\\xtensa-esp32-elf\\bin;C\:\\Python27
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/append=true
+environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738/appendContributed=false

+ 954 - 954
components/raop/raop.c

@@ -1,954 +1,954 @@
-/*
- *
- *  (c) Philippe 2019, philippe_44@outlook.com
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <stdio.h>
-
-#include "platform.h"
-
-#ifdef WIN32
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#include <openssl/rsa.h>
-#include <openssl/pem.h>
-#include <openssl/engine.h>
-#include "mdns.h"
-#include "mdnsd.h"
-#include "mdnssd-itf.h"
-#else
-#include "esp_pthread.h"
-#include "mdns.h"
-#include "mbedtls/version.h"
-#include <mbedtls/x509.h>
-#endif
-
-#include "util.h"
-#include "raop.h"
-#include "rtp.h"
-#include "dmap_parser.h"
-#include "log_util.h"
-
-#define RTSP_STACK_SIZE 	(8*1024)
-#define SEARCH_STACK_SIZE	(2*1048)
-
-typedef struct raop_ctx_s {
-#ifdef WIN32
-	struct mdns_service *svc;
-	struct mdnsd *svr;
-#endif
-	struct in_addr host;	// IP of bridge
-	short unsigned port;    // RTSP port for AirPlay
-	int sock;               // socket of the above
-	struct in_addr peer;	// IP of the iDevice (airplay sender)
-	bool running;
-#ifdef WIN32
-	pthread_t thread, search_thread;
-#else
-	TaskHandle_t thread, search_thread, joiner;
-	StaticTask_t *xTaskBuffer;
-	StackType_t xStack[RTSP_STACK_SIZE] __attribute__ ((aligned (4)));
-#endif
-	unsigned char mac[6];
-	int latency;
-	struct {
-		char *aesiv, *aeskey;
-		char *fmtp;
-	} rtsp;
-	struct rtp_s *rtp;
-	raop_cmd_cb_t	cmd_cb;
-	raop_data_cb_t	data_cb;
-	struct {
-		char				DACPid[32], id[32];
-		struct in_addr		host;
-		u16_t				port;
-#ifdef WIN32
-		struct mDNShandle_s *handle;
-#else
-		bool running;
-		TaskHandle_t thread, joiner;
-		StaticTask_t *xTaskBuffer;
-		StackType_t xStack[SEARCH_STACK_SIZE] __attribute__ ((aligned (4)));;
-#endif
-	} active_remote;
-	void *owner;
-} raop_ctx_t;
-
-extern struct mdnsd* glmDNSServer;
-extern log_level	raop_loglevel;
-static log_level 	*loglevel = &raop_loglevel;
-
-static void*	rtsp_thread(void *arg);
-static bool 	handle_rtsp(raop_ctx_t *ctx, int sock);
-
-static char*	rsa_apply(unsigned char *input, int inlen, int *outlen, int mode);
-static int  	base64_pad(char *src, char **padded);
-static int 		base64_encode(const void *data, int size, char **str);
-static int 		base64_decode(const char *str, void *data);
-static void* 	search_remote(void *args);
-
-extern char private_key[];
-
enum { RSA_MODE_KEY, RSA_MODE_AUTH };
-
-static void on_dmap_string(void *ctx, const char *code, const char *name, const char *buf, size_t len);
-
-/*----------------------------------------------------------------------------*/
-struct raop_ctx_s *raop_create(struct in_addr host, char *name,
-						unsigned char mac[6], int latency,
-						raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) {
-	struct raop_ctx_s *ctx = malloc(sizeof(struct raop_ctx_s));
-	struct sockaddr_in addr;
-	char id[64];
-
#ifdef WIN32
-	socklen_t nlen = sizeof(struct sockaddr);
-	char *txt[] = { "am=airesp32", "tp=UDP", "sm=false", "sv=false", "ek=1",
-					"et=0,1", "md=0,1,2", "cn=0,1", "ch=2",
-					"ss=16", "sr=44100", "vn=3", "txtvers=1",
-					NULL };
-#else
-	mdns_txt_item_t txt[] = {
-		{"am", "airesp32"},
-		{"tp", "UDP"},
-		{"sm","false"},
-		{"sv","false"},
-		{"ek","1"},
-		{"et","0,1"},
-		{"md","0,1,2"},
-		{"cn","0,1"},
-		{"ch","2"},
-		{"ss","16"},
-		{"sr","44100"},
-		{"vn","3"},
-		{"txtvers","1"},
-	};
-
-#endif
-
-	if (!ctx) return NULL;
-
-	// make sure we have a clean context
-	memset(ctx, 0, sizeof(raop_ctx_t));
-
-#ifdef WIN32
-	ctx->svr = glmDNSServer;
-#endif
-	ctx->host = host;
-	ctx->sock = socket(AF_INET, SOCK_STREAM, 0);
-	ctx->cmd_cb = cmd_cb;
-	ctx->data_cb = data_cb;
-	ctx->latency = min(latency, 88200);
-	if (ctx->sock == -1) {
-		LOG_ERROR("Cannot create listening socket", NULL);
-		free(ctx);
-		return NULL;
-	}
-
-	memset(&addr, 0, sizeof(addr));
-	addr.sin_addr.s_addr = host.s_addr;
-	addr.sin_family = AF_INET;
-#ifdef WIN32
-	ctx->port = 0;
-	addr.sin_port = htons(ctx->port);
-#else
-	ctx->port = 5000;
-	addr.sin_port = htons(ctx->port);
-#endif
-
-	if (bind(ctx->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0 || listen(ctx->sock, 1)) {
-		LOG_ERROR("Cannot bind or listen RTSP listener: %s", strerror(errno));
-		free(ctx);
-		closesocket(ctx->sock);
-		return NULL;
-	}
-
-#ifdef WIN32
-	getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
-	ctx->port = ntohs(addr.sin_port);
-#endif
-
	ctx->running = true;
-
	memcpy(ctx->mac, mac, 6);
-	snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s",  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name);
-
#ifdef WIN32
-	// seems that Windows snprintf does not add NULL char if actual size > max
-	id[63] = '\0';
-	ctx->svc = mdnsd_register_svc(ctx->svr, id, "_raop._tcp.local", ctx->port, NULL, (const char**) txt);
-	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)) );
-	
-    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 + 1, ctx->xStack, ctx->xTaskBuffer);
-#endif
-
-	return ctx;
-}
-
-/*----------------------------------------------------------------------------*/
-void raop_delete(struct raop_ctx_s *ctx) {
-#ifdef WIN32
-	int sock;
-	struct sockaddr addr;
-	socklen_t nlen = sizeof(struct sockaddr);
-#endif
-	
-if (!ctx) return;
-
-#ifdef WIN32
-	ctx->running = false;
-
-	// wake-up thread by connecting socket, needed for freeBSD
-	sock = socket(AF_INET, SOCK_STREAM, 0);
-	getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
-	connect(sock, (struct sockaddr*) &addr, sizeof(addr));
-	closesocket(sock);
-
-	pthread_join(ctx->thread, NULL);
-
-	rtp_end(ctx->rtp);
-
-	shutdown(ctx->sock, SD_BOTH);
-	closesocket(ctx->sock);
-
-	// terminate search, but do not reclaim memory of pthread if never launched
-	if (ctx->active_remote.handle) {
-		close_mDNS(ctx->active_remote.handle);
-		pthread_join(ctx->search_thread, NULL);
-	}
-
-	// stop broadcasting devices
-	mdns_service_remove(ctx->svr, ctx->svc);
-	mdnsd_stop(ctx->svr);
-#else 
-	// first stop the search task if any
-	if (ctx->active_remote.running) {
-		ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
-		ctx->active_remote.running = false;
-
-		vTaskResume(ctx->active_remote.thread);
-		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
-		vTaskDelete(ctx->active_remote.thread);
-
-		heap_caps_free(ctx->active_remote.xTaskBuffer);
-	}
-
-	// then the RTSP task
-	ctx->joiner = xTaskGetCurrentTaskHandle();
-	ctx->running = false;
-
-	ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
-	vTaskDelete(ctx->thread);
-	heap_caps_free(ctx->xTaskBuffer);
-
-	rtp_end(ctx->rtp);
-
-	shutdown(ctx->sock, SHUT_RDWR);
-	closesocket(ctx->sock);
-		
-	mdns_service_remove("_raop", "_tcp");	
-#endif
-
-	NFREE(ctx->rtsp.aeskey);
-	NFREE(ctx->rtsp.aesiv);
-	NFREE(ctx->rtsp.fmtp);
-
-	free(ctx);
-}
-
-/*----------------------------------------------------------------------------*/
-void raop_cmd(struct raop_ctx_s *ctx, raop_event_t event, void *param) {
-	struct sockaddr_in addr;
-	int sock;
-	char *command = NULL;
-
-	// first notify the remote controller (if any)
-	switch(event) {
-		case RAOP_REW:
-			command = strdup("beginrew");
-			break;
-		case RAOP_FWD:
-			command = strdup("beginff");
-			break;
-		case RAOP_PREV:
-			command = strdup("previtem");
-			break;
-		case RAOP_NEXT:
-			command = strdup("nextitem");
-			break;
-		case RAOP_TOGGLE:
-			command = strdup("playpause");
-			break;
-		case RAOP_PAUSE:
-			command = strdup("pause");
-			break;
-		case RAOP_PLAY:
-			command = strdup("play");
-			break;
-		case RAOP_RESUME:
-			command = strdup("playresume");
-			break;
-		case RAOP_STOP:
-			command = strdup("stop");
-			break;
-		case RAOP_VOLUME_UP:
-			command = strdup("volumeup");
-			break;
-		case RAOP_VOLUME_DOWN:
-			command = strdup("volumedown");
-			break;
-		case RAOP_VOLUME: {
-			float Volume = *((float*) param);
-			Volume = Volume ? (Volume - 1) * 30 : -144;
-			asprintf(&command,"setproperty?dmcp.device-volume=%0.4lf", Volume);
-			break;
-		}
-		default:
-			break;
-	}
-
-	// no command to send to remote or no remote found yet
-	if (!command || !ctx->active_remote.port) {
-		NFREE(command);
-		return;
-	}
-
-	sock = socket(AF_INET, SOCK_STREAM, 0);
-
-	memset(&addr, 0, sizeof(addr));
-	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = S_ADDR(ctx->active_remote.host);
-	addr.sin_port = htons(ctx->active_remote.port);
-
-	if (!connect(sock, (struct sockaddr*) &addr, sizeof(addr))) {
-		char *method, *buf, resp[512] = "";
-		int len;
-		key_data_t headers[4] = { {NULL, NULL} };
-
-		asprintf(&method, "GET /ctrl-int/1/%s HTTP/1.0", command);
-		kd_add(headers, "Active-Remote", ctx->active_remote.id);
-		kd_add(headers, "Connection", "close");
-
-		buf = http_send(sock, method, headers);
-		len = recv(sock, resp, 512, 0);
-		if (len > 0) resp[len-1] = '\0';
-		LOG_INFO("[%p]: sending airplay remote\n%s<== received ==>\n%s", ctx, buf, resp);
-
-		NFREE(method);
-		NFREE(buf);
-		kd_free(headers);
-	}
-
-	free(command);
-	closesocket(sock);
-}
-
-/*----------------------------------------------------------------------------*/
-static void *rtsp_thread(void *arg) {
-	raop_ctx_t *ctx = (raop_ctx_t*) arg;
-	int  sock = -1;
-
-	while (ctx->running) {
-		fd_set rfds;
-		struct timeval timeout = {0, 100*1000};
-		int n;
-		bool res = false;
-
-		if (sock == -1) {
-			struct sockaddr_in peer;
-			socklen_t addrlen = sizeof(struct sockaddr_in);
-
-			sock = accept(ctx->sock, (struct sockaddr*) &peer, &addrlen);
-			ctx->peer.s_addr = peer.sin_addr.s_addr;
-
-			if (sock != -1 && ctx->running) {
-				LOG_INFO("got RTSP connection %u", sock);
-			} else continue;
-		}
-
-		FD_ZERO(&rfds);
-		FD_SET(sock, &rfds);
-
-		n = select(sock + 1, &rfds, NULL, NULL, &timeout);
-
-		if (!n) continue;
-
-		if (n > 0) res = handle_rtsp(ctx, sock);
-
-		if (n < 0 || !res) {
-			closesocket(sock);
-			LOG_INFO("RTSP close %u", sock);
-			sock = -1;
-		}
-	}
-
-	if (sock != -1) closesocket(sock);
-
-#ifndef WIN32
-	xTaskNotifyGive(ctx->joiner);
-	vTaskSuspend(NULL);
-#endif
-
-	return NULL;
-}
-
-
-/*----------------------------------------------------------------------------*/
-static bool handle_rtsp(raop_ctx_t *ctx, int sock)
-{
-	char *buf = NULL, *body = NULL, method[16] = "";
-	key_data_t headers[16], resp[8] = { {NULL, NULL} };
-	int len;
-	bool success = true;
-	
-	if (!http_parse(sock, method, headers, &body, &len)) {
-		NFREE(body);
-		kd_free(headers);
-		return false;
-	}
-	
-	if (strcmp(method, "OPTIONS")) {
-		LOG_INFO("[%p]: received %s", ctx, method);
-	}
-
-	if ((buf = kd_lookup(headers, "Apple-Challenge")) != NULL) {
-		int n;
-		char *buf_pad, *p, *data_b64 = NULL, data[32];
-
-		LOG_INFO("[%p]: challenge %s", ctx, buf);
-
-		// need to pad the base64 string as apple device don't
-		base64_pad(buf, &buf_pad);
-
-		p = data + min(base64_decode(buf_pad, data), 32-10);
-		p = (char*) memcpy(p, &S_ADDR(ctx->host), 4) + 4;
-		p = (char*) memcpy(p, ctx->mac, 6) + 6;
-		memset(p, 0, 32 - (p - data));
-		p = rsa_apply((unsigned char*) data, 32, &n, RSA_MODE_AUTH);
-		n = base64_encode(p, n, &data_b64);
-
-		// remove padding as well (seems to be optional now)
-		for (n = strlen(data_b64) - 1; n > 0 && data_b64[n] == '='; data_b64[n--] = '\0');
-
-		kd_add(resp, "Apple-Response", data_b64);
-
-		NFREE(p);
-		NFREE(buf_pad);
-		NFREE(data_b64);
-	}
-	
-	if (!strcmp(method, "OPTIONS")) {
-
-		kd_add(resp, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
-
-	} else if (!strcmp(method, "ANNOUNCE")) {
-		char *padded, *p;
-
-		NFREE(ctx->rtsp.aeskey);
-		NFREE(ctx->rtsp.aesiv);
-		NFREE(ctx->rtsp.fmtp);
-		
-		// LMS might has taken over the player, leaving us with a running RTP session (should not happen)
-		if (ctx->rtp) {
-			LOG_WARN("[%p]: closing unfinished RTP session", ctx);
-			rtp_end(ctx->rtp);
-		}	
-
-		// same, should not happen unless we have missed a teardown ...
-		if (ctx->active_remote.running) {
-			ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
-			ctx->active_remote.running = false;
-			
-			vTaskResume(ctx->active_remote.thread);
-			ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
-			vTaskDelete(ctx->active_remote.thread);
-
-			heap_caps_free(ctx->active_remote.xTaskBuffer);
-			memset(&ctx->active_remote, 0, sizeof(ctx->active_remote));
-
-			LOG_WARN("[%p]: closing unfinished mDNS search", ctx);
-		}
-
-		if ((p = strcasestr(body, "rsaaeskey")) != NULL) {
-			unsigned char *aeskey;
-			int len, outlen;
-
-			p = strextract(p, ":", "\r\n");
-			base64_pad(p, &padded);
-			aeskey = malloc(strlen(padded));
-			len = base64_decode(padded, aeskey);
-			ctx->rtsp.aeskey = rsa_apply(aeskey, len, &outlen, RSA_MODE_KEY);
-
-			NFREE(p);
-			NFREE(aeskey);
-			NFREE(padded);
-		}
-
-		if ((p = strcasestr(body, "aesiv")) != NULL) {
-			p = strextract(p, ":", "\r\n");
-			base64_pad(p, &padded);
-			ctx->rtsp.aesiv = malloc(strlen(padded));
-			base64_decode(padded, ctx->rtsp.aesiv);
-
-			NFREE(p);
-			NFREE(padded);
-		}
-
-		if ((p = strcasestr(body, "fmtp")) != NULL) {
-			p = strextract(p, ":", "\r\n");
-			ctx->rtsp.fmtp = strdup(p);
-			NFREE(p);
-		}
-
-		// on announce, search remote
-		if ((buf = kd_lookup(headers, "DACP-ID")) != NULL) strcpy(ctx->active_remote.DACPid, buf);
-		if ((buf = kd_lookup(headers, "Active-Remote")) != NULL) strcpy(ctx->active_remote.id, buf);
-
-#ifdef WIN32	
-		ctx->active_remote.handle = init_mDNS(false, ctx->host);
-		pthread_create(&ctx->search_thread, NULL, &search_remote, ctx);
-#else
-		ctx->active_remote.running = true;
-		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 + 1, ctx->active_remote.xStack, ctx->active_remote.xTaskBuffer);
-#endif		
-
-	} else if (!strcmp(method, "SETUP") && ((buf = kd_lookup(headers, "Transport")) != NULL)) {
-		char *p;
-		rtp_resp_t rtp = { 0 };
-		short unsigned tport = 0, cport = 0;
-
-		// we are about to stream, do something if needed
-		success = ctx->cmd_cb(RAOP_SETUP, NULL);
-
-		if ((p = strcasestr(buf, "timing_port")) != NULL) sscanf(p, "%*[^=]=%hu", &tport);
-		if ((p = strcasestr(buf, "control_port")) != NULL) sscanf(p, "%*[^=]=%hu", &cport);
-
-		rtp = rtp_init(ctx->peer, ctx->latency,	ctx->rtsp.aeskey, ctx->rtsp.aesiv,
-					   ctx->rtsp.fmtp, cport, tport, ctx->cmd_cb, ctx->data_cb);
-						
-		ctx->rtp = rtp.ctx;
-		
-		if (cport * tport * rtp.cport * rtp.tport * rtp.aport && rtp.ctx) {
-			char *transport;
-			asprintf(&transport, "RTP/AVP/UDP;unicast;mode=record;control_port=%u;timing_port=%u;server_port=%u", rtp.cport, rtp.tport, rtp.aport);
-			LOG_DEBUG("[%p]: audio=(%hu:%hu), timing=(%hu:%hu), control=(%hu:%hu)", ctx, 0, rtp.aport, tport, rtp.tport, cport, rtp.cport);
-			kd_add(resp, "Transport", transport);
-			kd_add(resp, "Session", "DEADBEEF");
-			free(transport);
-		} else {
-			success = false;
-			LOG_INFO("[%p]: cannot start session, missing ports", ctx);
-		}
-
-	} else if (!strcmp(method, "RECORD")) {
-		unsigned short seqno = 0;
-		unsigned rtptime = 0;
-		char *p;
-
-		if (ctx->latency) {
-			char latency[6];
-			snprintf(latency, 6, "%u", ctx->latency);
-			kd_add(resp, "Audio-Latency", latency);
-		}
-
-		buf = kd_lookup(headers, "RTP-Info");
-		if (buf && (p = strcasestr(buf, "seq")) != NULL) sscanf(p, "%*[^=]=%hu", &seqno);
-		if (buf && (p = strcasestr(buf, "rtptime")) != NULL) sscanf(p, "%*[^=]=%u", &rtptime);
-
-		if (ctx->rtp) rtp_record(ctx->rtp, seqno, rtptime);
-
-		success = ctx->cmd_cb(RAOP_STREAM, NULL);
-
-	}  else if (!strcmp(method, "FLUSH")) {
-		unsigned short seqno = 0;
-		unsigned rtptime = 0;
-		char *p;
-
-		buf = kd_lookup(headers, "RTP-Info");
-		if ((p = strcasestr(buf, "seq")) != NULL) sscanf(p, "%*[^=]=%hu", &seqno);
-		if ((p = strcasestr(buf, "rtptime")) != NULL) sscanf(p, "%*[^=]=%u", &rtptime);
-
-		// only send FLUSH if useful (discards frames above buffer head and top)
-		if (ctx->rtp && rtp_flush(ctx->rtp, seqno, rtptime))
-			success = ctx->cmd_cb(RAOP_FLUSH, NULL);
-
-	}  else if (!strcmp(method, "TEARDOWN")) {
-
-		rtp_end(ctx->rtp);
-
-		ctx->rtp = NULL;
-
-		// need to make sure no search is on-going and reclaim pthread memory
-#ifdef WIN32
-		if (ctx->active_remote.handle) close_mDNS(ctx->active_remote.handle);
-		pthread_join(ctx->search_thread, NULL);
-#else
-		ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
-		ctx->active_remote.running = false;
-
-		// task might not need to be resumed anyway
-		vTaskResume(ctx->active_remote.thread);
-		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
-		vTaskDelete(ctx->active_remote.thread);
-
-		heap_caps_free(ctx->active_remote.xTaskBuffer);
-		
-		LOG_INFO("[%p]: mDNS search task terminated", ctx);
-#endif
-
-		memset(&ctx->active_remote, 0, sizeof(ctx->active_remote));
-		NFREE(ctx->rtsp.aeskey);
-		NFREE(ctx->rtsp.aesiv);
-		NFREE(ctx->rtsp.fmtp);
-
-		success = ctx->cmd_cb(RAOP_STOP, NULL);
-
-	} else if (!strcmp(method, "SET_PARAMETER")) {
-		char *p;
-
-		if (body && (p = strcasestr(body, "volume")) != NULL) {
-			float volume;
-
-			sscanf(p, "%*[^:]:%f", &volume);
-			LOG_INFO("[%p]: SET PARAMETER volume %f", ctx, volume);
-			volume = (volume == -144.0) ? 0 : (1 + volume / 30);
-			success = ctx->cmd_cb(RAOP_VOLUME, &volume);
-		}
-/*
-		if (body && ((p = kd_lookup(headers, "Content-Type")) != NULL) && !strcasecmp(p, "application/x-dmap-tagged")) {
-			struct metadata_s metadata;
-			dmap_settings settings = {
-				NULL, NULL, NULL, NULL,	NULL, NULL,	NULL, on_dmap_string, NULL,
-				NULL
-			};
-
-			settings.ctx = &metadata;
-			memset(&metadata, 0, sizeof(struct metadata_s));
-			if (!dmap_parse(&settings, body, len)) {
-				LOG_INFO("[%p]: received metadata\n\tartist: %s\n\talbum:  %s\n\ttitle:  %s",
-						 ctx, metadata.artist, metadata.album, metadata.title);
-				free_metadata(&metadata);
-			}
-		}
-*/
-	}
-
-	// don't need to free "buf" because kd_lookup return a pointer, not a strdup
-	kd_add(resp, "Audio-Jack-Status", "connected; type=analog");
-	kd_add(resp, "CSeq", kd_lookup(headers, "CSeq"));
-
-	if (success) buf = http_send(sock, "RTSP/1.0 200 OK", resp);
-	else buf = http_send(sock, "RTSP/1.0 500 ERROR", NULL);
-
-	if (strcmp(method, "OPTIONS")) {
-		LOG_INFO("[%p]: responding:\n%s", ctx, buf ? buf : "<void>");
-	}
-
-	NFREE(body);
-	NFREE(buf);
-	kd_free(resp);
-	kd_free(headers);
-
-	return true;
-}
-
-/*----------------------------------------------------------------------------*/
-#ifdef WIN32
-bool search_remote_cb(mDNSservice_t *slist, void *cookie, bool *stop) {
-	mDNSservice_t *s;
-	raop_ctx_t *ctx = (raop_ctx_t*) cookie;
-
-	// see if we have found an active remote for our ID
-	for (s = slist; s; s = s->next) {
-		if (strcasestr(s->name, ctx->active_remote.DACPid)) {
-			ctx->active_remote.host = s->addr;
-			ctx->active_remote.port = s->port;
-			LOG_INFO("[%p]: found ActiveRemote for %s at %s:%u", ctx, ctx->active_remote.DACPid,
-								inet_ntoa(ctx->active_remote.host), ctx->active_remote.port);
-			*stop = true;
-			break;
-		}
-	}
-
-	// let caller clear list
-	return false;
-}
-
-
-/*----------------------------------------------------------------------------*/
-static void* search_remote(void *args) {
-	raop_ctx_t *ctx = (raop_ctx_t*) args;
-
-	query_mDNS(ctx->active_remote.handle, "_dacp._tcp.local", 0, 0, &search_remote_cb, (void*) ctx);
-
-	return NULL;
-}
-
-#else
-
-/*----------------------------------------------------------------------------*/
-static void* search_remote(void *args) {
-	raop_ctx_t *ctx = (raop_ctx_t*) args;
-	bool found = false;
-	
-	LOG_INFO("starting remote search");
-
-	while (ctx->active_remote.running && !found) {
-		mdns_result_t *results = NULL;
-		mdns_result_t *r;
-		mdns_ip_addr_t *a;
-
-		if (mdns_query_ptr("_dacp", "_tcp", 3000, 32,  &results)) {
-			LOG_ERROR("mDNS active remote query Failed");
-			continue;
-		}
-
-		for (r = results; r && !strcasestr(r->instance_name, ctx->active_remote.DACPid); r = r->next);
-		if (r) {
-			for (a = r->addr; a && a->addr.type != IPADDR_TYPE_V4; a = a->next);
-			if (a) {
-				found = true;
-				ctx->active_remote.host.s_addr = a->addr.u_addr.ip4.addr;
-				ctx->active_remote.port = r->port;
-				LOG_INFO("found remote %s %s:%hu", r->instance_name, inet_ntoa(ctx->active_remote.host), ctx->active_remote.port);
-			}
-		}
-	
-		mdns_query_results_free(results);
-	}
-
-	/*
-	for some reason which is beyond me, if that tasks gives the semaphore 
-	before the RTSP tasks waits for it, then freeRTOS crashes in queue 
-	management caused by LWIP stack once the RTSP socket is closed. I have 
-	no clue why, but so we'll suspend the tasks as soon as we're done with 
-	search and wait for the resume then give the semaphore
-	*/
-	// PS: I know this is not fully race-condition free
-	if (ctx->active_remote.running) vTaskSuspend(NULL);
-	xTaskNotifyGive(ctx->active_remote.joiner);
-
-	// now our context will be deleted
-	vTaskSuspend(NULL);
-
-	return NULL;
- }
-#endif
-
-
/*----------------------------------------------------------------------------*/
-static char *rsa_apply(unsigned char *input, int inlen, int *outlen, int mode)
-{
-	static char super_secret_key[] =
-	"-----BEGIN RSA PRIVATE KEY-----\n"
-	"MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt\n"
-	"wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U\n"
-	"wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf\n"
-	"/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/\n"
-	"UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW\n"
-	"BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa\n"
-	"LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5\n"
-	"NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm\n"
-	"lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz\n"
-	"aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu\n"
-	"a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM\n"
-	"oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z\n"
-	"oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+\n"
-	"k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL\n"
-	"AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA\n"
-	"cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf\n"
-	"54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov\n"
-	"17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc\n"
-	"1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI\n"
-	"LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ\n"
-	"2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=\n"
-	"-----END RSA PRIVATE KEY-----";
-#ifdef WIN32
-	unsigned char *out;
-	RSA *rsa;
-
-	BIO *bmem = BIO_new_mem_buf(super_secret_key, -1);
-	rsa = PEM_read_bio_RSAPrivateKey(bmem, NULL, NULL, NULL);
-	BIO_free(bmem);
-
-	out = malloc(RSA_size(rsa));
-	switch (mode) {
-		case RSA_MODE_AUTH:
-			*outlen = RSA_private_encrypt(inlen, input, out, rsa,
-										  RSA_PKCS1_PADDING);
-			break;
-		case RSA_MODE_KEY:
-			*outlen = RSA_private_decrypt(inlen, input, out, rsa,
-										  RSA_PKCS1_OAEP_PADDING);
-			break;
-	}
-
-	RSA_free(rsa);
-
-	return (char*) out;
-#else
-	mbedtls_pk_context pkctx;
-	mbedtls_rsa_context *trsa;
-	size_t olen;
-	
-	/*
-	we should do entropy initialization & pass a rng function but this
-	consumes a ton of stack and there is no security concern here. Anyway,
-	mbedtls takes a lot of stack, unfortunately ...
-	*/
-
-	mbedtls_pk_init(&pkctx);
-	mbedtls_pk_parse_key(&pkctx, (unsigned char *)super_secret_key,
-		sizeof(super_secret_key), NULL, 0);
-
-	uint8_t *outbuf = NULL;
-	trsa = mbedtls_pk_rsa(pkctx);
-
-	switch (mode) {
-	case RSA_MODE_AUTH:
-		mbedtls_rsa_set_padding(trsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
-		outbuf = malloc(trsa->len);
-		mbedtls_rsa_pkcs1_encrypt(trsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, inlen, input, outbuf);
-		*outlen = trsa->len;
-		break;
-	case RSA_MODE_KEY:
-		mbedtls_rsa_set_padding(trsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1);
-		outbuf = malloc(trsa->len);
-		mbedtls_rsa_pkcs1_decrypt(trsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, &olen, input, outbuf, trsa->len);
-		*outlen = olen;
-		break;
-	}
-
-	mbedtls_pk_free(&pkctx);
-
-	return (char*) outbuf;
-#endif
-}
-
-#define DECODE_ERROR 0xffffffff
-
-static char base64_chars[] =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-/*----------------------------------------------------------------------------*/
-static int  base64_pad(char *src, char **padded)
-{
-	int n;
-
-	n = strlen(src) + strlen(src) % 4;
-	*padded = malloc(n + 1);
-	memset(*padded, '=', n);
-	memcpy(*padded, src, strlen(src));
-	(*padded)[n] = '\0';
-
-	return strlen(*padded);
-}
-
-/*----------------------------------------------------------------------------*/
-static int pos(char c)
-{
-	char *p;
-	for (p = base64_chars; *p; p++)
-	if (*p == c)
-		return p - base64_chars;
-	return -1;
-}
-
-/*----------------------------------------------------------------------------*/
-static int base64_encode(const void *data, int size, char **str)
-{
-	char *s, *p;
-	int i;
-	int c;
-	const unsigned char *q;
-
-	p = s = (char *) malloc(size * 4 / 3 + 4);
-	if (p == NULL) return -1;
-	q = (const unsigned char *) data;
-	i = 0;
-	for (i = 0; i < size;) {
-		c = q[i++];
-		c *= 256;
-		if (i < size) c += q[i];
-		i++;
-		c *= 256;
-		if (i < size) c += q[i];
-		i++;
-		p[0] = base64_chars[(c & 0x00fc0000) >> 18];
-		p[1] = base64_chars[(c & 0x0003f000) >> 12];
-		p[2] = base64_chars[(c & 0x00000fc0) >> 6];
-		p[3] = base64_chars[(c & 0x0000003f) >> 0];
-		if (i > size) p[3] = '=';
-		if (i > size + 1) p[2] = '=';
-		p += 4;
-	}
-	*p = 0;
-	*str = s;
-	return strlen(s);
-}
-
-/*----------------------------------------------------------------------------*/
-static unsigned int token_decode(const char *token)
-{
-	int i;
-	unsigned int val = 0;
-	int marker = 0;
-	if (strlen(token) < 4)
-	return DECODE_ERROR;
-	for (i = 0; i < 4; i++) {
-	val *= 64;
-	if (token[i] == '=')
-		marker++;
-	else if (marker > 0)
-		return DECODE_ERROR;
-	else
-		val += pos(token[i]);
-	}
-	if (marker > 2)
-	return DECODE_ERROR;
-	return (marker << 24) | val;
-}
-
-/*----------------------------------------------------------------------------*/
-static int base64_decode(const char *str, void *data)
-{
-	const char *p;
-	unsigned char *q;
-
-	q = data;
-	for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
-	unsigned int val = token_decode(p);
-	unsigned int marker = (val >> 24) & 0xff;
-	if (val == DECODE_ERROR)
-		return -1;
-	*q++ = (val >> 16) & 0xff;
-	if (marker < 2)
-		*q++ = (val >> 8) & 0xff;
-	if (marker < 1)
-		*q++ = val & 0xff;
-	}
-	return q - (unsigned char *) data;
-}
-
-/*----------------------------------------------------------------------------*/
-static void on_dmap_string(void *ctx, const char *code, const char *name, const char *buf, size_t len) {
-	struct metadata_s *metadata = (struct metadata_s *) ctx;
-
-	if (!strcasecmp(code, "asar")) metadata->artist = strndup(buf, len);
-	else if (!strcasecmp(code, "asal")) metadata->album = strndup(buf, len);
-	else if (!strcasecmp(code, "minm")) metadata->title = strndup(buf, len);
-}
-
+/*
+ *
+ *  (c) Philippe 2019, philippe_44@outlook.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdio.h>
+
+#include "platform.h"
+
+#ifdef WIN32
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#include <openssl/rsa.h>
+#include <openssl/pem.h>
+#include <openssl/engine.h>
+#include "mdns.h"
+#include "mdnsd.h"
+#include "mdnssd-itf.h"
+#else
+#include "esp_pthread.h"
+#include "mdns.h"
+#include "mbedtls/version.h"
+#include <mbedtls/x509.h>
+#endif
+
+#include "util.h"
+#include "raop.h"
+#include "rtp.h"
+#include "dmap_parser.h"
+#include "log_util.h"
+
+#define RTSP_STACK_SIZE 	(8*1024)
+#define SEARCH_STACK_SIZE	(2*1048)
+
+typedef struct raop_ctx_s {
+#ifdef WIN32
+	struct mdns_service *svc;
+	struct mdnsd *svr;
+#endif
+	struct in_addr host;	// IP of bridge
+	short unsigned port;    // RTSP port for AirPlay
+	int sock;               // socket of the above
+	struct in_addr peer;	// IP of the iDevice (airplay sender)
+	bool running;
+#ifdef WIN32
+	pthread_t thread, search_thread;
+#else
+	TaskHandle_t thread, search_thread, joiner;
+	StaticTask_t *xTaskBuffer;
+	StackType_t xStack[RTSP_STACK_SIZE] __attribute__ ((aligned (4)));
+#endif
+	unsigned char mac[6];
+	int latency;
+	struct {
+		char *aesiv, *aeskey;
+		char *fmtp;
+	} rtsp;
+	struct rtp_s *rtp;
+	raop_cmd_cb_t	cmd_cb;
+	raop_data_cb_t	data_cb;
+	struct {
+		char				DACPid[32], id[32];
+		struct in_addr		host;
+		u16_t				port;
+#ifdef WIN32
+		struct mDNShandle_s *handle;
+#else
+		bool running;
+		TaskHandle_t thread, joiner;
+		StaticTask_t *xTaskBuffer;
+		StackType_t xStack[SEARCH_STACK_SIZE] __attribute__ ((aligned (4)));;
+#endif
+	} active_remote;
+	void *owner;
+} raop_ctx_t;
+
+extern struct mdnsd* glmDNSServer;
+extern log_level	raop_loglevel;
+static log_level 	*loglevel = &raop_loglevel;
+
+static void*	rtsp_thread(void *arg);
+static bool 	handle_rtsp(raop_ctx_t *ctx, int sock);
+
+static char*	rsa_apply(unsigned char *input, int inlen, int *outlen, int mode);
+static int  	base64_pad(char *src, char **padded);
+static int 		base64_encode(const void *data, int size, char **str);
+static int 		base64_decode(const char *str, void *data);
+static void* 	search_remote(void *args);
+
+extern char private_key[];
+
enum { RSA_MODE_KEY, RSA_MODE_AUTH };
+
+static void on_dmap_string(void *ctx, const char *code, const char *name, const char *buf, size_t len);
+
+/*----------------------------------------------------------------------------*/
+struct raop_ctx_s *raop_create(struct in_addr host, char *name,
+						unsigned char mac[6], int latency,
+						raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) {
+	struct raop_ctx_s *ctx = malloc(sizeof(struct raop_ctx_s));
+	struct sockaddr_in addr;
+	char id[64];
+
#ifdef WIN32
+	socklen_t nlen = sizeof(struct sockaddr);
+	char *txt[] = { "am=airesp32", "tp=UDP", "sm=false", "sv=false", "ek=1",
+					"et=0,1", "md=0,1,2", "cn=0,1", "ch=2",
+					"ss=16", "sr=44100", "vn=3", "txtvers=1",
+					NULL };
+#else
+	mdns_txt_item_t txt[] = {
+		{"am", "airesp32"},
+		{"tp", "UDP"},
+		{"sm","false"},
+		{"sv","false"},
+		{"ek","1"},
+		{"et","0,1"},
+		{"md","0,1,2"},
+		{"cn","0,1"},
+		{"ch","2"},
+		{"ss","16"},
+		{"sr","44100"},
+		{"vn","3"},
+		{"txtvers","1"},
+	};
+
+#endif
+
+	if (!ctx) return NULL;
+
+	// make sure we have a clean context
+	memset(ctx, 0, sizeof(raop_ctx_t));
+
+#ifdef WIN32
+	ctx->svr = glmDNSServer;
+#endif
+	ctx->host = host;
+	ctx->sock = socket(AF_INET, SOCK_STREAM, 0);
+	ctx->cmd_cb = cmd_cb;
+	ctx->data_cb = data_cb;
+	ctx->latency = min(latency, 88200);
+	if (ctx->sock == -1) {
+		LOG_ERROR("Cannot create listening socket", NULL);
+		free(ctx);
+		return NULL;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sin_addr.s_addr = host.s_addr;
+	addr.sin_family = AF_INET;
+#ifdef WIN32
+	ctx->port = 0;
+	addr.sin_port = htons(ctx->port);
+#else
+	ctx->port = 5000;
+	addr.sin_port = htons(ctx->port);
+#endif
+
+	if (bind(ctx->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0 || listen(ctx->sock, 1)) {
+		LOG_ERROR("Cannot bind or listen RTSP listener: %s", strerror(errno));
+		free(ctx);
+		closesocket(ctx->sock);
+		return NULL;
+	}
+
+#ifdef WIN32
+	getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
+	ctx->port = ntohs(addr.sin_port);
+#endif
+
	ctx->running = true;
+
	memcpy(ctx->mac, mac, 6);
+	snprintf(id, 64, "%02X%02X%02X%02X%02X%02X@%s",  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], name);
+
#ifdef WIN32
+	// seems that Windows snprintf does not add NULL char if actual size > max
+	id[63] = '\0';
+	ctx->svc = mdnsd_register_svc(ctx->svr, id, "_raop._tcp.local", ctx->port, NULL, (const char**) txt);
+	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)) );
+	
+    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 + 1, ctx->xStack, ctx->xTaskBuffer);
+#endif
+
+	return ctx;
+}
+
+/*----------------------------------------------------------------------------*/
+void raop_delete(struct raop_ctx_s *ctx) {
+#ifdef WIN32
+	int sock;
+	struct sockaddr addr;
+	socklen_t nlen = sizeof(struct sockaddr);
+#endif
+	
+if (!ctx) return;
+
+#ifdef WIN32
+	ctx->running = false;
+
+	// wake-up thread by connecting socket, needed for freeBSD
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	getsockname(ctx->sock, (struct sockaddr *) &addr, &nlen);
+	connect(sock, (struct sockaddr*) &addr, sizeof(addr));
+	closesocket(sock);
+
+	pthread_join(ctx->thread, NULL);
+
+	rtp_end(ctx->rtp);
+
+	shutdown(ctx->sock, SD_BOTH);
+	closesocket(ctx->sock);
+
+	// terminate search, but do not reclaim memory of pthread if never launched
+	if (ctx->active_remote.handle) {
+		close_mDNS(ctx->active_remote.handle);
+		pthread_join(ctx->search_thread, NULL);
+	}
+
+	// stop broadcasting devices
+	mdns_service_remove(ctx->svr, ctx->svc);
+	mdnsd_stop(ctx->svr);
+#else 
+	// first stop the search task if any
+	if (ctx->active_remote.running) {
+		ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
+		ctx->active_remote.running = false;
+
+		vTaskResume(ctx->active_remote.thread);
+		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
+		vTaskDelete(ctx->active_remote.thread);
+
+		heap_caps_free(ctx->active_remote.xTaskBuffer);
+	}
+
+	// then the RTSP task
+	ctx->joiner = xTaskGetCurrentTaskHandle();
+	ctx->running = false;
+
+	ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
+	vTaskDelete(ctx->thread);
+	heap_caps_free(ctx->xTaskBuffer);
+
+	rtp_end(ctx->rtp);
+
+	shutdown(ctx->sock, SHUT_RDWR);
+	closesocket(ctx->sock);
+		
+	mdns_service_remove("_raop", "_tcp");	
+#endif
+
+	NFREE(ctx->rtsp.aeskey);
+	NFREE(ctx->rtsp.aesiv);
+	NFREE(ctx->rtsp.fmtp);
+
+	free(ctx);
+}
+
+/*----------------------------------------------------------------------------*/
+void raop_cmd(struct raop_ctx_s *ctx, raop_event_t event, void *param) {
+	struct sockaddr_in addr;
+	int sock;
+	char *command = NULL;
+
+	// first notify the remote controller (if any)
+	switch(event) {
+		case RAOP_REW:
+			command = strdup("beginrew");
+			break;
+		case RAOP_FWD:
+			command = strdup("beginff");
+			break;
+		case RAOP_PREV:
+			command = strdup("previtem");
+			break;
+		case RAOP_NEXT:
+			command = strdup("nextitem");
+			break;
+		case RAOP_TOGGLE:
+			command = strdup("playpause");
+			break;
+		case RAOP_PAUSE:
+			command = strdup("pause");
+			break;
+		case RAOP_PLAY:
+			command = strdup("play");
+			break;
+		case RAOP_RESUME:
+			command = strdup("playresume");
+			break;
+		case RAOP_STOP:
+			command = strdup("stop");
+			break;
+		case RAOP_VOLUME_UP:
+			command = strdup("volumeup");
+			break;
+		case RAOP_VOLUME_DOWN:
+			command = strdup("volumedown");
+			break;
+		case RAOP_VOLUME: {
+			float Volume = *((float*) param);
+			Volume = Volume ? (Volume - 1) * 30 : -144;
+			asprintf(&command,"setproperty?dmcp.device-volume=%0.4lf", Volume);
+			break;
+		}
+		default:
+			break;
+	}
+
+	// no command to send to remote or no remote found yet
+	if (!command || !ctx->active_remote.port) {
+		NFREE(command);
+		return;
+	}
+
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = S_ADDR(ctx->active_remote.host);
+	addr.sin_port = htons(ctx->active_remote.port);
+
+	if (!connect(sock, (struct sockaddr*) &addr, sizeof(addr))) {
+		char *method, *buf, resp[512] = "";
+		int len;
+		key_data_t headers[4] = { {NULL, NULL} };
+
+		asprintf(&method, "GET /ctrl-int/1/%s HTTP/1.0", command);
+		kd_add(headers, "Active-Remote", ctx->active_remote.id);
+		kd_add(headers, "Connection", "close");
+
+		buf = http_send(sock, method, headers);
+		len = recv(sock, resp, 512, 0);
+		if (len > 0) resp[len-1] = '\0';
+		LOG_INFO("[%p]: sending airplay remote\n%s<== received ==>\n%s", ctx, buf, resp);
+
+		NFREE(method);
+		NFREE(buf);
+		kd_free(headers);
+	}
+
+	free(command);
+	closesocket(sock);
+}
+
+/*----------------------------------------------------------------------------*/
+static void *rtsp_thread(void *arg) {
+	raop_ctx_t *ctx = (raop_ctx_t*) arg;
+	int  sock = -1;
+
+	while (ctx->running) {
+		fd_set rfds;
+		struct timeval timeout = {0, 100*1000};
+		int n;
+		bool res = false;
+
+		if (sock == -1) {
+			struct sockaddr_in peer;
+			socklen_t addrlen = sizeof(struct sockaddr_in);
+
+			sock = accept(ctx->sock, (struct sockaddr*) &peer, &addrlen);
+			ctx->peer.s_addr = peer.sin_addr.s_addr;
+
+			if (sock != -1 && ctx->running) {
+				LOG_INFO("got RTSP connection %u", sock);
+			} else continue;
+		}
+
+		FD_ZERO(&rfds);
+		FD_SET(sock, &rfds);
+
+		n = select(sock + 1, &rfds, NULL, NULL, &timeout);
+
+		if (!n) continue;
+
+		if (n > 0) res = handle_rtsp(ctx, sock);
+
+		if (n < 0 || !res) {
+			closesocket(sock);
+			LOG_INFO("RTSP close %u", sock);
+			sock = -1;
+		}
+	}
+
+	if (sock != -1) closesocket(sock);
+
+#ifndef WIN32
+	xTaskNotifyGive(ctx->joiner);
+	vTaskSuspend(NULL);
+#endif
+
+	return NULL;
+}
+
+
+/*----------------------------------------------------------------------------*/
+static bool handle_rtsp(raop_ctx_t *ctx, int sock)
+{
+	char *buf = NULL, *body = NULL, method[16] = "";
+	key_data_t headers[16], resp[8] = { {NULL, NULL} };
+	int len;
+	bool success = true;
+	
+	if (!http_parse(sock, method, headers, &body, &len)) {
+		NFREE(body);
+		kd_free(headers);
+		return false;
+	}
+	
+	if (strcmp(method, "OPTIONS")) {
+		LOG_INFO("[%p]: received %s", ctx, method);
+	}
+
+	if ((buf = kd_lookup(headers, "Apple-Challenge")) != NULL) {
+		int n;
+		char *buf_pad, *p, *data_b64 = NULL, data[32];
+
+		LOG_INFO("[%p]: challenge %s", ctx, buf);
+
+		// need to pad the base64 string as apple device don't
+		base64_pad(buf, &buf_pad);
+
+		p = data + min(base64_decode(buf_pad, data), 32-10);
+		p = (char*) memcpy(p, &S_ADDR(ctx->host), 4) + 4;
+		p = (char*) memcpy(p, ctx->mac, 6) + 6;
+		memset(p, 0, 32 - (p - data));
+		p = rsa_apply((unsigned char*) data, 32, &n, RSA_MODE_AUTH);
+		n = base64_encode(p, n, &data_b64);
+
+		// remove padding as well (seems to be optional now)
+		for (n = strlen(data_b64) - 1; n > 0 && data_b64[n] == '='; data_b64[n--] = '\0');
+
+		kd_add(resp, "Apple-Response", data_b64);
+
+		NFREE(p);
+		NFREE(buf_pad);
+		NFREE(data_b64);
+	}
+	
+	if (!strcmp(method, "OPTIONS")) {
+
+		kd_add(resp, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER");
+
+	} else if (!strcmp(method, "ANNOUNCE")) {
+		char *padded, *p;
+
+		NFREE(ctx->rtsp.aeskey);
+		NFREE(ctx->rtsp.aesiv);
+		NFREE(ctx->rtsp.fmtp);
+		
+		// LMS might has taken over the player, leaving us with a running RTP session (should not happen)
+		if (ctx->rtp) {
+			LOG_WARN("[%p]: closing unfinished RTP session", ctx);
+			rtp_end(ctx->rtp);
+		}	
+
+		// same, should not happen unless we have missed a teardown ...
+		if (ctx->active_remote.running) {
+			ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
+			ctx->active_remote.running = false;
+			
+			vTaskResume(ctx->active_remote.thread);
+			ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
+			vTaskDelete(ctx->active_remote.thread);
+
+			heap_caps_free(ctx->active_remote.xTaskBuffer);
+			memset(&ctx->active_remote, 0, sizeof(ctx->active_remote));
+
+			LOG_WARN("[%p]: closing unfinished mDNS search", ctx);
+		}
+
+		if ((p = strcasestr(body, "rsaaeskey")) != NULL) {
+			unsigned char *aeskey;
+			int len, outlen;
+
+			p = strextract(p, ":", "\r\n");
+			base64_pad(p, &padded);
+			aeskey = malloc(strlen(padded));
+			len = base64_decode(padded, aeskey);
+			ctx->rtsp.aeskey = rsa_apply(aeskey, len, &outlen, RSA_MODE_KEY);
+
+			NFREE(p);
+			NFREE(aeskey);
+			NFREE(padded);
+		}
+
+		if ((p = strcasestr(body, "aesiv")) != NULL) {
+			p = strextract(p, ":", "\r\n");
+			base64_pad(p, &padded);
+			ctx->rtsp.aesiv = malloc(strlen(padded));
+			base64_decode(padded, ctx->rtsp.aesiv);
+
+			NFREE(p);
+			NFREE(padded);
+		}
+
+		if ((p = strcasestr(body, "fmtp")) != NULL) {
+			p = strextract(p, ":", "\r\n");
+			ctx->rtsp.fmtp = strdup(p);
+			NFREE(p);
+		}
+
+		// on announce, search remote
+		if ((buf = kd_lookup(headers, "DACP-ID")) != NULL) strcpy(ctx->active_remote.DACPid, buf);
+		if ((buf = kd_lookup(headers, "Active-Remote")) != NULL) strcpy(ctx->active_remote.id, buf);
+
+#ifdef WIN32	
+		ctx->active_remote.handle = init_mDNS(false, ctx->host);
+		pthread_create(&ctx->search_thread, NULL, &search_remote, ctx);
+#else
+		ctx->active_remote.running = true;
+		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 + 1, ctx->active_remote.xStack, ctx->active_remote.xTaskBuffer);
+#endif		
+
+	} else if (!strcmp(method, "SETUP") && ((buf = kd_lookup(headers, "Transport")) != NULL)) {
+		char *p;
+		rtp_resp_t rtp = { 0 };
+		short unsigned tport = 0, cport = 0;
+
+		// we are about to stream, do something if needed
+		success = ctx->cmd_cb(RAOP_SETUP, NULL);
+
+		if ((p = strcasestr(buf, "timing_port")) != NULL) sscanf(p, "%*[^=]=%hu", &tport);
+		if ((p = strcasestr(buf, "control_port")) != NULL) sscanf(p, "%*[^=]=%hu", &cport);
+
+		rtp = rtp_init(ctx->peer, ctx->latency,	ctx->rtsp.aeskey, ctx->rtsp.aesiv,
+					   ctx->rtsp.fmtp, cport, tport, ctx->cmd_cb, ctx->data_cb);
+						
+		ctx->rtp = rtp.ctx;
+		
+		if (cport * tport * rtp.cport * rtp.tport * rtp.aport && rtp.ctx) {
+			char *transport;
+			asprintf(&transport, "RTP/AVP/UDP;unicast;mode=record;control_port=%u;timing_port=%u;server_port=%u", rtp.cport, rtp.tport, rtp.aport);
+			LOG_DEBUG("[%p]: audio=(%hu:%hu), timing=(%hu:%hu), control=(%hu:%hu)", ctx, 0, rtp.aport, tport, rtp.tport, cport, rtp.cport);
+			kd_add(resp, "Transport", transport);
+			kd_add(resp, "Session", "DEADBEEF");
+			free(transport);
+		} else {
+			success = false;
+			LOG_INFO("[%p]: cannot start session, missing ports", ctx);
+		}
+
+	} else if (!strcmp(method, "RECORD")) {
+		unsigned short seqno = 0;
+		unsigned rtptime = 0;
+		char *p;
+
+		if (ctx->latency) {
+			char latency[6];
+			snprintf(latency, 6, "%u", ctx->latency);
+			kd_add(resp, "Audio-Latency", latency);
+		}
+
+		buf = kd_lookup(headers, "RTP-Info");
+		if (buf && (p = strcasestr(buf, "seq")) != NULL) sscanf(p, "%*[^=]=%hu", &seqno);
+		if (buf && (p = strcasestr(buf, "rtptime")) != NULL) sscanf(p, "%*[^=]=%u", &rtptime);
+
+		if (ctx->rtp) rtp_record(ctx->rtp, seqno, rtptime);
+
+		success = ctx->cmd_cb(RAOP_STREAM, NULL);
+
+	}  else if (!strcmp(method, "FLUSH")) {
+		unsigned short seqno = 0;
+		unsigned rtptime = 0;
+		char *p;
+
+		buf = kd_lookup(headers, "RTP-Info");
+		if ((p = strcasestr(buf, "seq")) != NULL) sscanf(p, "%*[^=]=%hu", &seqno);
+		if ((p = strcasestr(buf, "rtptime")) != NULL) sscanf(p, "%*[^=]=%u", &rtptime);
+
+		// only send FLUSH if useful (discards frames above buffer head and top)
+		if (ctx->rtp && rtp_flush(ctx->rtp, seqno, rtptime))
+			success = ctx->cmd_cb(RAOP_FLUSH, NULL);
+
+	}  else if (!strcmp(method, "TEARDOWN")) {
+
+		rtp_end(ctx->rtp);
+
+		ctx->rtp = NULL;
+
+		// need to make sure no search is on-going and reclaim pthread memory
+#ifdef WIN32
+		if (ctx->active_remote.handle) close_mDNS(ctx->active_remote.handle);
+		pthread_join(ctx->search_thread, NULL);
+#else
+		ctx->active_remote.joiner = xTaskGetCurrentTaskHandle();
+		ctx->active_remote.running = false;
+
+		// task might not need to be resumed anyway
+		vTaskResume(ctx->active_remote.thread);
+		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
+		vTaskDelete(ctx->active_remote.thread);
+
+		heap_caps_free(ctx->active_remote.xTaskBuffer);
+		
+		LOG_INFO("[%p]: mDNS search task terminated", ctx);
+#endif
+
+		memset(&ctx->active_remote, 0, sizeof(ctx->active_remote));
+		NFREE(ctx->rtsp.aeskey);
+		NFREE(ctx->rtsp.aesiv);
+		NFREE(ctx->rtsp.fmtp);
+
+		success = ctx->cmd_cb(RAOP_STOP, NULL);
+
+	} else if (!strcmp(method, "SET_PARAMETER")) {
+		char *p;
+
+		if (body && (p = strcasestr(body, "volume")) != NULL) {
+			float volume;
+
+			sscanf(p, "%*[^:]:%f", &volume);
+			LOG_INFO("[%p]: SET PARAMETER volume %f", ctx, volume);
+			volume = (volume == -144.0) ? 0 : (1 + volume / 30);
+			success = ctx->cmd_cb(RAOP_VOLUME, &volume);
+		}
+/*
+		if (body && ((p = kd_lookup(headers, "Content-Type")) != NULL) && !strcasecmp(p, "application/x-dmap-tagged")) {
+			struct metadata_s metadata;
+			dmap_settings settings = {
+				NULL, NULL, NULL, NULL,	NULL, NULL,	NULL, on_dmap_string, NULL,
+				NULL
+			};
+
+			settings.ctx = &metadata;
+			memset(&metadata, 0, sizeof(struct metadata_s));
+			if (!dmap_parse(&settings, body, len)) {
+				LOG_INFO("[%p]: received metadata\n\tartist: %s\n\talbum:  %s\n\ttitle:  %s",
+						 ctx, metadata.artist, metadata.album, metadata.title);
+				free_metadata(&metadata);
+			}
+		}
+*/
+	}
+
+	// don't need to free "buf" because kd_lookup return a pointer, not a strdup
+	kd_add(resp, "Audio-Jack-Status", "connected; type=analog");
+	kd_add(resp, "CSeq", kd_lookup(headers, "CSeq"));
+
+	if (success) buf = http_send(sock, "RTSP/1.0 200 OK", resp);
+	else buf = http_send(sock, "RTSP/1.0 500 ERROR", NULL);
+
+	if (strcmp(method, "OPTIONS")) {
+		LOG_INFO("[%p]: responding:\n%s", ctx, buf ? buf : "<void>");
+	}
+
+	NFREE(body);
+	NFREE(buf);
+	kd_free(resp);
+	kd_free(headers);
+
+	return true;
+}
+
+/*----------------------------------------------------------------------------*/
+#ifdef WIN32
+bool search_remote_cb(mDNSservice_t *slist, void *cookie, bool *stop) {
+	mDNSservice_t *s;
+	raop_ctx_t *ctx = (raop_ctx_t*) cookie;
+
+	// see if we have found an active remote for our ID
+	for (s = slist; s; s = s->next) {
+		if (strcasestr(s->name, ctx->active_remote.DACPid)) {
+			ctx->active_remote.host = s->addr;
+			ctx->active_remote.port = s->port;
+			LOG_INFO("[%p]: found ActiveRemote for %s at %s:%u", ctx, ctx->active_remote.DACPid,
+								inet_ntoa(ctx->active_remote.host), ctx->active_remote.port);
+			*stop = true;
+			break;
+		}
+	}
+
+	// let caller clear list
+	return false;
+}
+
+
+/*----------------------------------------------------------------------------*/
+static void* search_remote(void *args) {
+	raop_ctx_t *ctx = (raop_ctx_t*) args;
+
+	query_mDNS(ctx->active_remote.handle, "_dacp._tcp.local", 0, 0, &search_remote_cb, (void*) ctx);
+
+	return NULL;
+}
+
+#else
+
+/*----------------------------------------------------------------------------*/
+static void* search_remote(void *args) {
+	raop_ctx_t *ctx = (raop_ctx_t*) args;
+	bool found = false;
+	
+	LOG_INFO("starting remote search");
+
+	while (ctx->active_remote.running && !found) {
+		mdns_result_t *results = NULL;
+		mdns_result_t *r;
+		mdns_ip_addr_t *a;
+
+		if (mdns_query_ptr("_dacp", "_tcp", 3000, 32,  &results)) {
+			LOG_ERROR("mDNS active remote query Failed");
+			continue;
+		}
+
+		for (r = results; r && !strcasestr(r->instance_name, ctx->active_remote.DACPid); r = r->next);
+		if (r) {
+			for (a = r->addr; a && a->addr.type != IPADDR_TYPE_V4; a = a->next);
+			if (a) {
+				found = true;
+				ctx->active_remote.host.s_addr = a->addr.u_addr.ip4.addr;
+				ctx->active_remote.port = r->port;
+				LOG_INFO("found remote %s %s:%hu", r->instance_name, inet_ntoa(ctx->active_remote.host), ctx->active_remote.port);
+			}
+		}
+	
+		mdns_query_results_free(results);
+	}
+
+	/*
+	for some reason which is beyond me, if that tasks gives the semaphore 
+	before the RTSP tasks waits for it, then freeRTOS crashes in queue 
+	management caused by LWIP stack once the RTSP socket is closed. I have 
+	no clue why, but so we'll suspend the tasks as soon as we're done with 
+	search and wait for the resume then give the semaphore
+	*/
+	// PS: I know this is not fully race-condition free
+	if (ctx->active_remote.running) vTaskSuspend(NULL);
+	xTaskNotifyGive(ctx->active_remote.joiner);
+
+	// now our context will be deleted
+	vTaskSuspend(NULL);
+
+	return NULL;
+ }
+#endif
+
+
/*----------------------------------------------------------------------------*/
+static char *rsa_apply(unsigned char *input, int inlen, int *outlen, int mode)
+{
+	static char super_secret_key[] =
+	"-----BEGIN RSA PRIVATE KEY-----\n"
+	"MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt\n"
+	"wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U\n"
+	"wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf\n"
+	"/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/\n"
+	"UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW\n"
+	"BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa\n"
+	"LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5\n"
+	"NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm\n"
+	"lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz\n"
+	"aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu\n"
+	"a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM\n"
+	"oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z\n"
+	"oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+\n"
+	"k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL\n"
+	"AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA\n"
+	"cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf\n"
+	"54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov\n"
+	"17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc\n"
+	"1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI\n"
+	"LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ\n"
+	"2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=\n"
+	"-----END RSA PRIVATE KEY-----";
+#ifdef WIN32
+	unsigned char *out;
+	RSA *rsa;
+
+	BIO *bmem = BIO_new_mem_buf(super_secret_key, -1);
+	rsa = PEM_read_bio_RSAPrivateKey(bmem, NULL, NULL, NULL);
+	BIO_free(bmem);
+
+	out = malloc(RSA_size(rsa));
+	switch (mode) {
+		case RSA_MODE_AUTH:
+			*outlen = RSA_private_encrypt(inlen, input, out, rsa,
+										  RSA_PKCS1_PADDING);
+			break;
+		case RSA_MODE_KEY:
+			*outlen = RSA_private_decrypt(inlen, input, out, rsa,
+										  RSA_PKCS1_OAEP_PADDING);
+			break;
+	}
+
+	RSA_free(rsa);
+
+	return (char*) out;
+#else
+	mbedtls_pk_context pkctx;
+	mbedtls_rsa_context *trsa;
+	size_t olen;
+	
+	/*
+	we should do entropy initialization & pass a rng function but this
+	consumes a ton of stack and there is no security concern here. Anyway,
+	mbedtls takes a lot of stack, unfortunately ...
+	*/
+
+	mbedtls_pk_init(&pkctx);
+	mbedtls_pk_parse_key(&pkctx, (unsigned char *)super_secret_key,
+		sizeof(super_secret_key), NULL, 0);
+
+	uint8_t *outbuf = NULL;
+	trsa = mbedtls_pk_rsa(pkctx);
+
+	switch (mode) {
+	case RSA_MODE_AUTH:
+		mbedtls_rsa_set_padding(trsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
+		outbuf = malloc(trsa->len);
+		mbedtls_rsa_pkcs1_encrypt(trsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, inlen, input, outbuf);
+		*outlen = trsa->len;
+		break;
+	case RSA_MODE_KEY:
+		mbedtls_rsa_set_padding(trsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1);
+		outbuf = malloc(trsa->len);
+		mbedtls_rsa_pkcs1_decrypt(trsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, &olen, input, outbuf, trsa->len);
+		*outlen = olen;
+		break;
+	}
+
+	mbedtls_pk_free(&pkctx);
+
+	return (char*) outbuf;
+#endif
+}
+
+#define DECODE_ERROR 0xffffffff
+
+static char base64_chars[] =
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+/*----------------------------------------------------------------------------*/
+static int  base64_pad(char *src, char **padded)
+{
+	int n;
+
+	n = strlen(src) + strlen(src) % 4;
+	*padded = malloc(n + 1);
+	memset(*padded, '=', n);
+	memcpy(*padded, src, strlen(src));
+	(*padded)[n] = '\0';
+
+	return strlen(*padded);
+}
+
+/*----------------------------------------------------------------------------*/
+static int pos(char c)
+{
+	char *p;
+	for (p = base64_chars; *p; p++)
+	if (*p == c)
+		return p - base64_chars;
+	return -1;
+}
+
+/*----------------------------------------------------------------------------*/
+static int base64_encode(const void *data, int size, char **str)
+{
+	char *s, *p;
+	int i;
+	int c;
+	const unsigned char *q;
+
+	p = s = (char *) malloc(size * 4 / 3 + 4);
+	if (p == NULL) return -1;
+	q = (const unsigned char *) data;
+	i = 0;
+	for (i = 0; i < size;) {
+		c = q[i++];
+		c *= 256;
+		if (i < size) c += q[i];
+		i++;
+		c *= 256;
+		if (i < size) c += q[i];
+		i++;
+		p[0] = base64_chars[(c & 0x00fc0000) >> 18];
+		p[1] = base64_chars[(c & 0x0003f000) >> 12];
+		p[2] = base64_chars[(c & 0x00000fc0) >> 6];
+		p[3] = base64_chars[(c & 0x0000003f) >> 0];
+		if (i > size) p[3] = '=';
+		if (i > size + 1) p[2] = '=';
+		p += 4;
+	}
+	*p = 0;
+	*str = s;
+	return strlen(s);
+}
+
+/*----------------------------------------------------------------------------*/
+static unsigned int token_decode(const char *token)
+{
+	int i;
+	unsigned int val = 0;
+	int marker = 0;
+	if (strlen(token) < 4)
+	return DECODE_ERROR;
+	for (i = 0; i < 4; i++) {
+	val *= 64;
+	if (token[i] == '=')
+		marker++;
+	else if (marker > 0)
+		return DECODE_ERROR;
+	else
+		val += pos(token[i]);
+	}
+	if (marker > 2)
+	return DECODE_ERROR;
+	return (marker << 24) | val;
+}
+
+/*----------------------------------------------------------------------------*/
+static int base64_decode(const char *str, void *data)
+{
+	const char *p;
+	unsigned char *q;
+
+	q = data;
+	for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
+	unsigned int val = token_decode(p);
+	unsigned int marker = (val >> 24) & 0xff;
+	if (val == DECODE_ERROR)
+		return -1;
+	*q++ = (val >> 16) & 0xff;
+	if (marker < 2)
+		*q++ = (val >> 8) & 0xff;
+	if (marker < 1)
+		*q++ = val & 0xff;
+	}
+	return q - (unsigned char *) data;
+}
+
+/*----------------------------------------------------------------------------*/
+static void on_dmap_string(void *ctx, const char *code, const char *name, const char *buf, size_t len) {
+	struct metadata_s *metadata = (struct metadata_s *) ctx;
+
+	if (!strcasecmp(code, "asar")) metadata->artist = strndup(buf, len);
+	else if (!strcasecmp(code, "asal")) metadata->album = strndup(buf, len);
+	else if (!strcasecmp(code, "minm")) metadata->title = strndup(buf, len);
+}
+

+ 783 - 783
components/raop/rtp.c

@@ -1,783 +1,783 @@
-
-/*
- * HairTunes - RAOP packet handler and slave-clocked replay engine
- * Copyright (c) James Laird 2011
- * All rights reserved.
- *
- * Modularisation: philippe_44@outlook.com, 2019
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <sys/types.h>
-#include <pthread.h>
-#include <math.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdint.h>
-#include <fcntl.h>
-#include <assert.h>
-
-#include "platform.h"
-#include "rtp.h"
-#include "raop_sink.h"
-#include "log_util.h"
-#include "util.h"
-
-#ifdef WIN32
-#include <openssl/aes.h>
-#include "alac_wrapper.h"
-#else
-#include "esp_pthread.h"
-#include "esp_system.h"
-#include <mbedtls/version.h>
-#include <mbedtls/aes.h>
-#include "alac_wrapper.h"
-#endif
-
-#define NTP2MS(ntp) ((((ntp) >> 10) * 1000L) >> 22)
-#define MS2NTP(ms) (((((u64_t) (ms)) << 22) / 1000) << 10)
-#define NTP2TS(ntp, rate) ((((ntp) >> 16) * (rate)) >> 16)
-#define TS2NTP(ts, rate)  (((((u64_t) (ts)) << 16) / (rate)) << 16)
-#define MS2TS(ms, rate) ((((u64_t) (ms)) * (rate)) / 1000)
-#define TS2MS(ts, rate) NTP2MS(TS2NTP(ts,rate))
-
-
extern log_level 	raop_loglevel;
-
static log_level 	*loglevel = &raop_loglevel;
-
-//#define __RTP_STORE
-
-// default buffer size
-#define BUFFER_FRAMES 	( (150 * RAOP_SAMPLE_RATE * 2) / (352 * 100) )
-#define MAX_PACKET       1408
-#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)
-
-#define RESEND_TO	200
-
-enum { DATA = 0, CONTROL, TIMING };
-
-static const u8_t silence_frame[MAX_PACKET] = { 0 };
-
-typedef u16_t seq_t;
-typedef struct audio_buffer_entry {   // decoded audio packets
-	int ready;
-	u32_t rtptime, last_resend;
-	s16_t *data;
-	int len;
-} abuf_t;
-
-typedef struct rtp_s {
-#ifdef __RTP_STORE
-	FILE *rtpIN, *rtpOUT;
-#endif
-	bool running;
-	unsigned char aesiv[16];
-#ifdef WIN32
-	AES_KEY aes;
-#else
-	mbedtls_aes_context aes;
-#endif
-	bool decrypt;
-	u8_t *decrypt_buf;
-	u32_t frame_size, frame_duration;
-	u32_t in_frames, out_frames;
-	struct in_addr host;
-	struct sockaddr_in rtp_host;
-	struct {
-		unsigned short rport, lport;
-		int sock;
-	} rtp_sockets[3]; 					 // data, control, timing
-	struct timing_s {
-		u64_t local, remote;
-	} timing;
-	struct {
-		u32_t 	rtp, time;
-		u8_t  	status;
-	} synchro;
-	struct {
-		u32_t time;
-		seq_t seqno;
-		u32_t rtptime;
-	} record;
-	int latency;			// rtp hold depth in samples
-	u32_t resent_req, resent_rec;	// total resent + recovered frames
-	u32_t silent_frames;	// total silence frames
-	u32_t discarded;
-	abuf_t audio_buffer[BUFFER_FRAMES];
-	seq_t ab_read, ab_write;
-	pthread_mutex_t ab_mutex;
-#ifdef WIN32
-	pthread_t thread;
-#else
-	TaskHandle_t thread, joiner;
-	StaticTask_t *xTaskBuffer;
-    StackType_t xStack[RTP_STACK_SIZE] __attribute__ ((aligned (4)));
-#endif
-
-	struct alac_codec_s *alac_codec;
-	int flush_seqno;
-	bool playing;
-	raop_data_cb_t data_cb;
-	raop_cmd_cb_t cmd_cb;
-} rtp_t;
-
-
-#define BUFIDX(seqno) ((seq_t)(seqno) % BUFFER_FRAMES)
-static void 	buffer_alloc(abuf_t *audio_buffer, int size);
-static void 	buffer_release(abuf_t *audio_buffer);
-static void 	buffer_reset(abuf_t *audio_buffer);
-static void 	buffer_push_packet(rtp_t *ctx);
-static bool 	rtp_request_resend(rtp_t *ctx, seq_t first, seq_t last);
-static bool 	rtp_request_timing(rtp_t *ctx);
-static void*	rtp_thread_func(void *arg);
-static int	  	seq_order(seq_t a, seq_t b);
-
-/*---------------------------------------------------------------------------*/
-static struct alac_codec_s* alac_init(int fmtp[32]) {
-	struct alac_codec_s *alac;
-	unsigned sample_rate;
-	unsigned char sample_size, channels;
-	struct {
-		uint32_t	frameLength;
-		uint8_t		compatibleVersion;
-		uint8_t		bitDepth;
-		uint8_t		pb;
-		uint8_t		mb;
-		uint8_t		kb;
-		uint8_t		numChannels;
-		uint16_t	maxRun;
-		uint32_t	maxFrameBytes;
-		uint32_t	avgBitRate;
-		uint32_t	sampleRate;
-	} config;
-
-	config.frameLength = htonl(fmtp[1]);
-	config.compatibleVersion = fmtp[2];
-	config.bitDepth = fmtp[3];
-	config.pb = fmtp[4];
-	config.mb = fmtp[5];
-	config.kb = fmtp[6];
-	config.numChannels = fmtp[7];
-	config.maxRun = htons(fmtp[8]);
-	config.maxFrameBytes = htonl(fmtp[9]);
-	config.avgBitRate = htonl(fmtp[10]);
-	config.sampleRate = htonl(fmtp[11]);
-
-	alac = alac_create_decoder(sizeof(config), (unsigned char*) &config, &sample_size, &sample_rate, &channels);
-	if (!alac) {
-		LOG_ERROR("cannot create alac codec", NULL);
-		return NULL;
-	}
-
-	return alac;
-}
-
-/*---------------------------------------------------------------------------*/
-rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv, char *fmtpstr,
-								short unsigned pCtrlPort, short unsigned pTimingPort,
-								raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb)
-{
-	int i = 0;
-	char *arg;
-	int fmtp[12];
-	bool rc = true;
-	rtp_t *ctx = calloc(1, sizeof(rtp_t));
-	rtp_resp_t resp = { 0, 0, 0, NULL };
-
-	if (!ctx) return resp;
-	
-	ctx->host = host;
-	ctx->decrypt = false;
-	ctx->cmd_cb = cmd_cb;
-	ctx->data_cb = data_cb;
-	ctx->rtp_host.sin_family = AF_INET;
-	ctx->rtp_host.sin_addr.s_addr = INADDR_ANY;
-	pthread_mutex_init(&ctx->ab_mutex, 0);
-	ctx->flush_seqno = -1;
-	ctx->latency = latency;
-	ctx->ab_read = ctx->ab_write;
-
-#ifdef __RTP_STORE
-	ctx->rtpIN = fopen("airplay.rtpin", "wb");
-	ctx->rtpOUT = fopen("airplay.rtpout", "wb");
-#endif
-
-	ctx->rtp_sockets[CONTROL].rport = pCtrlPort;
-	ctx->rtp_sockets[TIMING].rport = pTimingPort;
-
-	if (aesiv && aeskey) {
-		memcpy(ctx->aesiv, aesiv, 16);
-#ifdef WIN32
-		AES_set_decrypt_key((unsigned char*) aeskey, 128, &ctx->aes);
-#else
-		memset(&ctx->aes, 0, sizeof(mbedtls_aes_context));
-		mbedtls_aes_setkey_dec(&ctx->aes, (unsigned char*) aeskey, 128);
-#endif
-		ctx->decrypt = true;
-		ctx->decrypt_buf = malloc(MAX_PACKET);
-	}
-
-	memset(fmtp, 0, sizeof(fmtp));
-	while ((arg = strsep(&fmtpstr, " \t")) != NULL) fmtp[i++] = atoi(arg);
-
-	ctx->frame_size = fmtp[1];
-	ctx->frame_duration = (ctx->frame_size * 1000) / RAOP_SAMPLE_RATE;
-
-	// alac decoder
-	ctx->alac_codec = alac_init(fmtp);
-	rc &= ctx->alac_codec != NULL;
-
-	buffer_alloc(ctx->audio_buffer, ctx->frame_size*4);
-
-	// create rtp ports
-	for (i = 0; i < 3; i++) {
-		ctx->rtp_sockets[i].sock = bind_socket(&ctx->rtp_sockets[i].lport, SOCK_DGRAM);
-		rc &= ctx->rtp_sockets[i].sock > 0;
-	}
-
-	// create http port and start listening
-	resp.cport = ctx->rtp_sockets[CONTROL].lport;
-	resp.tport = ctx->rtp_sockets[TIMING].lport;
-	resp.aport = ctx->rtp_sockets[DATA].lport;
-	
-	if (rc) {
-		ctx->running = true;
-#ifdef WIN32
-		pthread_create(&ctx->thread, NULL, rtp_thread_func, (void *) ctx);
-#else
-		// 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->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 {
-		LOG_ERROR("[%p]: cannot start RTP", ctx);
-		rtp_end(ctx);
-		ctx = NULL;
-	}
-
-	resp.ctx = ctx;
-
-	return resp;
-}
-
-/*---------------------------------------------------------------------------*/
-void rtp_end(rtp_t *ctx)
-{
-	int i;
-
-	if (!ctx) return;
-
-	if (ctx->running) {
-#if !defined WIN32		
-		ctx->joiner = xTaskGetCurrentTaskHandle();
-#endif
-		ctx->running = false;
-#ifdef WIN32
-		pthread_join(ctx->thread, NULL);
-#else
-		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
-		vTaskDelete(ctx->thread);
-		heap_caps_free(ctx->xTaskBuffer);
-#endif
-	}
-	
-	for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock);
-
-	if (ctx->alac_codec) alac_delete_decoder(ctx->alac_codec);
-	if (ctx->decrypt_buf) free(ctx->decrypt_buf);
-	
-	pthread_mutex_destroy(&ctx->ab_mutex);
-	buffer_release(ctx->audio_buffer);
-	
-	free(ctx);
-
-#ifdef __RTP_STORE
-	fclose(ctx->rtpIN);
-	fclose(ctx->rtpOUT);
-#endif
-}
-
-/*---------------------------------------------------------------------------*/
-bool rtp_flush(rtp_t *ctx, unsigned short seqno, unsigned int rtptime)
-{
-	bool rc = true;
-	u32_t now = gettime_ms();
-
-	if (now < ctx->record.time + 250 || (ctx->record.seqno == seqno && ctx->record.rtptime == rtptime)) {
-		rc = false;
-		LOG_ERROR("[%p]: FLUSH ignored as same as RECORD (%hu - %u)", ctx, seqno, rtptime);
-	} else {
-		pthread_mutex_lock(&ctx->ab_mutex);
-		buffer_reset(ctx->audio_buffer);
-		ctx->playing = false;
-		ctx->flush_seqno = seqno;
-		pthread_mutex_unlock(&ctx->ab_mutex);
-	}
-
-	LOG_INFO("[%p]: flush %hu %u", ctx, seqno, rtptime);
-
-	return rc;
-}
-
-/*---------------------------------------------------------------------------*/
-void rtp_record(rtp_t *ctx, unsigned short seqno, unsigned rtptime)
-{
-	ctx->record.seqno = seqno;
-	ctx->record.rtptime = rtptime;
-	ctx->record.time = gettime_ms();
-
-	LOG_INFO("[%p]: record %hu %u", ctx, seqno, rtptime);
-}
-
-/*---------------------------------------------------------------------------*/
-static void buffer_alloc(abuf_t *audio_buffer, int size) {
-	int i;
-	for (i = 0; i < BUFFER_FRAMES; i++) {
-		audio_buffer[i].data = malloc(size);
-		audio_buffer[i].ready = 0;
-	}
-}
-
-/*---------------------------------------------------------------------------*/
-static void buffer_release(abuf_t *audio_buffer) {
-	int i;
-	for (i = 0; i < BUFFER_FRAMES; i++) {
-		free(audio_buffer[i].data);
-	}
-}
-
-/*---------------------------------------------------------------------------*/
-static void buffer_reset(abuf_t *audio_buffer) {
-	int i;
-	for (i = 0; i < BUFFER_FRAMES; i++) audio_buffer[i].ready = 0;
-}
-
-/*---------------------------------------------------------------------------*/
-// the sequence numbers will wrap pretty often.
-// this returns true if the second arg is after the first
-static int seq_order(seq_t a, seq_t b) {
-	s16_t d = b - a;
-	return d > 0;
-}
-
-/*---------------------------------------------------------------------------*/
-static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsize) {
-	unsigned char iv[16];
-	int aeslen;
-	assert(len<=MAX_PACKET);
-
-	if (ctx->decrypt) {
-		aeslen = len & ~0xf;
-		memcpy(iv, ctx->aesiv, sizeof(iv));
-#ifdef WIN32
-		AES_cbc_encrypt((unsigned char*)buf, ctx->decrypt_buf, aeslen, &ctx->aes, iv, AES_DECRYPT);
-#else
-		mbedtls_aes_crypt_cbc(&ctx->aes, MBEDTLS_AES_DECRYPT, aeslen, iv, (unsigned char*) buf, ctx->decrypt_buf);
-#endif
-		memcpy(ctx->decrypt_buf+aeslen, buf+aeslen, len-aeslen);
-		alac_to_pcm(ctx->alac_codec, (unsigned char*) ctx->decrypt_buf, (unsigned char*) dest, 2, (unsigned int*) outsize);
-	} else {
-		alac_to_pcm(ctx->alac_codec, (unsigned char*) buf, (unsigned char*) dest, 2, (unsigned int*) outsize);
-	}	
-	
-	*outsize *= 4;
-}
-
-
-/*---------------------------------------------------------------------------*/
-static void buffer_put_packet(rtp_t *ctx, seq_t seqno, unsigned rtptime, bool first, char *data, int len) {
-	abuf_t *abuf = NULL;
-	u32_t playtime;
-
-	pthread_mutex_lock(&ctx->ab_mutex);
-
-	if (!ctx->playing) {
-		if ((ctx->flush_seqno == -1 || seq_order(ctx->flush_seqno, seqno)) &&
-		   (ctx->synchro.status & RTP_SYNC) && (ctx->synchro.status & NTP_SYNC)) {
-			ctx->ab_write = seqno-1;
-			ctx->ab_read = seqno;
-			ctx->flush_seqno = -1;
-			ctx->playing = true;
-			ctx->resent_req = ctx->resent_rec = ctx->silent_frames = ctx->discarded = 0;
-			playtime = ctx->synchro.time + (((s32_t)(rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
-			ctx->cmd_cb(RAOP_PLAY, &playtime);
-		} else {
-			pthread_mutex_unlock(&ctx->ab_mutex);
-			return;
-		}
-	}
-
-	if (seqno == (u16_t) (ctx->ab_write+1)) {
-		// expected packet
-		abuf = ctx->audio_buffer + BUFIDX(seqno);
-		ctx->ab_write = seqno;
-		LOG_SDEBUG("packet expected seqno:%hu rtptime:%u (W:%hu R:%hu)", seqno, rtptime, ctx->ab_write, ctx->ab_read);
-
-	} else if (seq_order(ctx->ab_write, seqno)) {
-		// newer than expected
-		if (ctx->latency && seq_order(ctx->latency / ctx->frame_size, seqno - ctx->ab_write - 1)) {
-			// only get rtp latency-1 frames back (last one is seqno)
-			LOG_WARN("[%p] too many missing frames %hu seq: %hu, (W:%hu R:%hu)", ctx, seqno - ctx->ab_write - 1, seqno, ctx->ab_write, ctx->ab_read);
-			ctx->ab_write = seqno - ctx->latency / ctx->frame_size;
-		}
-		if (ctx->latency && seq_order(ctx->latency / ctx->frame_size, seqno - ctx->ab_read)) {
-			// if ab_read is lagging more than http latency, advance it
-			LOG_WARN("[%p] on hold for too long %hu (W:%hu R:%hu)", ctx, seqno - ctx->ab_read + 1, ctx->ab_write, ctx->ab_read);
-			ctx->ab_read = seqno - ctx->latency / ctx->frame_size + 1;
-		}
-		if (rtp_request_resend(ctx, ctx->ab_write + 1, seqno-1)) {
-			seq_t i;
-			u32_t now = gettime_ms();
-			for (i = ctx->ab_write + 1; seq_order(i, seqno); i++) {
-				ctx->audio_buffer[BUFIDX(i)].rtptime = rtptime - (seqno-i)*ctx->frame_size;
-				ctx->audio_buffer[BUFIDX(i)].last_resend = now;
-			}
-		}
-		LOG_DEBUG("[%p]: packet newer seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
-		abuf = ctx->audio_buffer + BUFIDX(seqno);
-		ctx->ab_write = seqno;
-	} else if (seq_order(ctx->ab_read, seqno + 1)) {
-		// recovered packet, not yet sent
-		abuf = ctx->audio_buffer + BUFIDX(seqno);
-		ctx->resent_rec++;
-		LOG_DEBUG("[%p]: packet recovered seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
-	} else {
-		// too late
-		LOG_DEBUG("[%p]: packet too late seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
-	}
-
-	if (ctx->in_frames++ > 1000) {
-		LOG_INFO("[%p]: fill [level:%hu rec:%u] [W:%hu R:%hu]", ctx, ctx->ab_write - ctx->ab_read, ctx->resent_rec, ctx->ab_write, ctx->ab_read);
-		ctx->in_frames = 0;
-	}
-
-	if (abuf) {
-		alac_decode(ctx, abuf->data, data, len, &abuf->len);
-		abuf->ready = 1;
-		// this is the local rtptime when this frame is expected to play
-		abuf->rtptime = rtptime;
-		buffer_push_packet(ctx);
-
-#ifdef __RTP_STORE
-		fwrite(data, len, 1, ctx->rtpIN);
-		fwrite(abuf->data, abuf->len, 1, ctx->rtpOUT);
-#endif
-	}
-
-	pthread_mutex_unlock(&ctx->ab_mutex);
-}
-
-/*---------------------------------------------------------------------------*/
-// push as many frames as possible through callback
-static void buffer_push_packet(rtp_t *ctx) {
-	abuf_t *curframe = NULL;
-	u32_t now, playtime, hold = max((ctx->latency * 1000) / (8 * RAOP_SAMPLE_RATE), 100);
-	int i;
-
-	// not ready to play yet
-	if (!ctx->playing ||  ctx->synchro.status != (RTP_SYNC | NTP_SYNC)) return;
-
-	// maybe re-evaluate time in loop in case data callback blocks ...
-	now = gettime_ms();
-
-	// there is always at least one frame in the buffer
-	do {
-
-		curframe = ctx->audio_buffer + BUFIDX(ctx->ab_read);
-		playtime = ctx->synchro.time + (((s32_t)(curframe->rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
-
-		if (now > playtime) {
-			LOG_DEBUG("[%p]: discarded frame now:%u missed by:%d (W:%hu R:%hu)", ctx, now, now - playtime, ctx->ab_write, ctx->ab_read);
-			ctx->discarded++;
-			curframe->ready = 0;
-		} else if (curframe->ready) {
-			ctx->data_cb((const u8_t*) curframe->data, curframe->len, playtime);
-			curframe->ready = 0;
-		} else if (playtime - now <= hold) {
-			LOG_DEBUG("[%p]: created zero frame (W:%hu R:%hu)", ctx, ctx->ab_write, ctx->ab_read);
-			ctx->data_cb(silence_frame, ctx->frame_size * 4, playtime);
-			ctx->silent_frames++;
-		} else break;
-
-		ctx->ab_read++;
-		ctx->out_frames++;
-
-	} while (seq_order(ctx->ab_read, ctx->ab_write));
-
-	if (ctx->out_frames > 1000) {
-		LOG_INFO("[%p]: drain [level:%hd head:%d ms] [W:%hu R:%hu] [req:%u sil:%u dis:%u]",
-				ctx, ctx->ab_write - ctx->ab_read, playtime - now, ctx->ab_write, ctx->ab_read,
-				ctx->resent_req, ctx->silent_frames, ctx->discarded);
-		ctx->out_frames = 0;
-	}
-
-	LOG_SDEBUG("playtime %u %d [W:%hu R:%hu] %d", playtime, playtime - now, ctx->ab_write, ctx->ab_read, curframe->ready);
-
-	// each missing packet will be requested up to (latency_frames / 16) times
-	for (i = 0; seq_order(ctx->ab_read + i, ctx->ab_write); i += 16) {
-		abuf_t *frame = ctx->audio_buffer + BUFIDX(ctx->ab_read + i);
-		if (!frame->ready && now - frame->last_resend > RESEND_TO) {
-			rtp_request_resend(ctx, ctx->ab_read + i, ctx->ab_read + i);
-			frame->last_resend = now;
-		}
-	}
-
}
-
-
-/*---------------------------------------------------------------------------*/
-static void *rtp_thread_func(void *arg) {
-	fd_set fds;
-	int i, sock = -1;
-	int count = 0;
-	bool ntp_sent;
-	char *packet = malloc(MAX_PACKET);
-	rtp_t *ctx = (rtp_t*) arg;
-
-	for (i = 0; i < 3; i++) {
-		if (ctx->rtp_sockets[i].sock > sock) sock = ctx->rtp_sockets[i].sock;
-		// send synchro requets 3 times
-		ntp_sent = rtp_request_timing(ctx);
-	}
-
-	while (ctx->running) {
-		ssize_t plen;
-		char type;
-		socklen_t rtp_client_len = sizeof(struct sockaddr_storage);
-		int idx = 0;
-		char *pktp = packet;
-		struct timeval timeout = {0, 100*1000};
-
-		FD_ZERO(&fds);
-		for (i = 0; i < 3; i++)	{ FD_SET(ctx->rtp_sockets[i].sock, &fds); }
-
-		if (select(sock + 1, &fds, NULL, NULL, &timeout) <= 0) continue;
-
-		for (i = 0; i < 3; i++)
-			if (FD_ISSET(ctx->rtp_sockets[i].sock, &fds)) idx = i;
-
-		plen = recvfrom(ctx->rtp_sockets[idx].sock, packet, MAX_PACKET, 0, (struct sockaddr*) &ctx->rtp_host, &rtp_client_len);
-
-		if (!ntp_sent) {
-			LOG_WARN("[%p]: NTP request not send yet", ctx);
-			ntp_sent = rtp_request_timing(ctx);
-		}
-
-		if (plen < 0) continue;
-		assert(plen <= MAX_PACKET);
-
-		type = packet[1] & ~0x80;
-		pktp = packet;
-
-		switch (type) {
-			seq_t seqno;
-			unsigned rtptime;
-
-			// re-sent packet
-			case 0x56: {
-				pktp += 4;
-				plen -= 4;
-			}
-
-			// data packet
-			case 0x60: {
-				seqno = ntohs(*(u16_t*)(pktp+2));
-				rtptime = ntohl(*(u32_t*)(pktp+4));
-
-				// adjust pointer and length
-				pktp += 12;
-				plen -= 12;
-
-				LOG_SDEBUG("[%p]: seqno:%hu rtp:%u (type: %x, first: %u)", ctx, seqno, rtptime, type, packet[1] & 0x80);
-
-				// check if packet contains enough content to be reasonable
-				if (plen < 16) break;
-
-				if ((packet[1] & 0x80) && (type != 0x56)) {
-					LOG_INFO("[%p]: 1st audio packet received", ctx);
-				}
-
-				buffer_put_packet(ctx, seqno, rtptime, packet[1] & 0x80, pktp, plen);
-
-				break;
-			}
-
-			// sync packet
-			case 0x54: {
-				u32_t rtp_now_latency = ntohl(*(u32_t*)(pktp+4));
-				u64_t remote = (((u64_t) ntohl(*(u32_t*)(pktp+8))) << 32) + ntohl(*(u32_t*)(pktp+12));
-				u32_t rtp_now = ntohl(*(u32_t*)(pktp+16));
-				u16_t flags = ntohs(*(u16_t*)(pktp+2));
-				u32_t remote_gap = NTP2MS(remote - ctx->timing.remote);
-				
-				if (remote_gap > 10000) {
-					LOG_WARN("discarding remote timing information %u", remote_gap);
-					break;
-				}
-
-				pthread_mutex_lock(&ctx->ab_mutex);
-
-				// re-align timestamp and expected local playback time (and magic 11025 latency)
-				ctx->latency = rtp_now - rtp_now_latency;
-				if (flags == 7 || flags == 4) ctx->latency += 11025;
-				if (ctx->latency < MIN_LATENCY) ctx->latency = MIN_LATENCY;
-				else if (ctx->latency > MAX_LATENCY) ctx->latency = MAX_LATENCY;
-				ctx->synchro.rtp = rtp_now - ctx->latency;
-				ctx->synchro.time = ctx->timing.local + remote_gap;
-
-				// now we are synced on RTP frames
-				ctx->synchro.status |= RTP_SYNC;
-
-				// 1st sync packet received (signals a restart of playback)
-				if (packet[0] & 0x10) {
-					LOG_INFO("[%p]: 1st sync packet received", ctx);
-				}
-
-				pthread_mutex_unlock(&ctx->ab_mutex);
-
-				LOG_DEBUG("[%p]: sync packet latency:%d rtp_latency:%u rtp:%u remote ntp:%llx, local time:%u local rtp:%u (now:%u)",
-						  ctx, ctx->latency, rtp_now_latency, rtp_now, remote, ctx->synchro.time, ctx->synchro.rtp, gettime_ms());
-
-				if (!count--) {
-					rtp_request_timing(ctx);
-					count = 3;
-				}
-
-				if ((ctx->synchro.status & RTP_SYNC) && (ctx->synchro.status & NTP_SYNC)) ctx->cmd_cb(RAOP_TIMING, NULL);
-
-				break;
-			}
-
-			// NTP timing packet
-			case 0x53: {
-				u64_t expected;
-				u32_t reference   = ntohl(*(u32_t*)(pktp+12)); // only low 32 bits in our case
-				u64_t remote 	  =(((u64_t) ntohl(*(u32_t*)(pktp+16))) << 32) + ntohl(*(u32_t*)(pktp+20));
-				u32_t roundtrip   = gettime_ms() - reference;
-
-				// better discard sync packets when roundtrip is suspicious
-				if (roundtrip > 100) {
-					LOG_WARN("[%p]: discarding NTP roundtrip of %u ms", ctx, roundtrip);
-					break;
-				}
-
-				/*
-				  The expected elapsed remote time should be exactly the same as
-				  elapsed local time between the two request, corrected by the
-				  drifting
-				*/
-				expected = ctx->timing.remote + MS2NTP(reference - ctx->timing.local);
-
-				ctx->timing.remote = remote;
-				ctx->timing.local = reference;
-
-				// now we are synced on NTP (mutex not needed)
-				ctx->synchro.status |= NTP_SYNC;
-
-				LOG_DEBUG("[%p]: Timing references local:%llu, remote:%llx (delta:%lld, sum:%lld, adjust:%lld, gaps:%d)",
-						  ctx, ctx->timing.local, ctx->timing.remote);
-
-				break;
-			}
-		}
-	}
-
-	free(packet);
-	LOG_INFO("[%p]: terminating", ctx);
-
-#ifndef WIN32
-	xTaskNotifyGive(ctx->joiner);
-	vTaskSuspend(NULL);
-#endif
-
-	return NULL;
-}
-
-/*---------------------------------------------------------------------------*/
-static bool rtp_request_timing(rtp_t *ctx) {
-	unsigned char req[32];
-	u32_t now = gettime_ms();
-	int i;
-	struct sockaddr_in host;
-
-	LOG_DEBUG("[%p]: timing request now:%u (port: %hu)", ctx, now, ctx->rtp_sockets[TIMING].rport);
-
-	req[0] = 0x80;
-	req[1] = 0x52|0x80;
-	*(u16_t*)(req+2) = htons(7);
-	*(u32_t*)(req+4) = htonl(0);  // dummy
-	for (i = 0; i < 16; i++) req[i+8] = 0;
-	*(u32_t*)(req+24) = 0;
-	*(u32_t*)(req+28) = htonl(now); // this is not a real NTP, but a 32 ms counter in the low part of the NTP
-
-	if (ctx->host.s_addr != INADDR_ANY) {
-		host.sin_family = AF_INET;
-		host.sin_addr =	ctx->host;
-	} else host = ctx->rtp_host;
-
-	// no address from sender, need to wait for 1st packet to be received
-	if (host.sin_addr.s_addr == INADDR_ANY) return false;
-
-	host.sin_port = htons(ctx->rtp_sockets[TIMING].rport);
-
-	if (sizeof(req) != sendto(ctx->rtp_sockets[TIMING].sock, req, sizeof(req), 0, (struct sockaddr*) &host, sizeof(host))) {
-		LOG_WARN("[%p]: SENDTO failed (%s)", ctx, strerror(errno));
-	}
-
-	return true;
-}
-
-/*---------------------------------------------------------------------------*/
-static bool rtp_request_resend(rtp_t *ctx, seq_t first, seq_t last) {
-	unsigned char req[8];    // *not* a standard RTCP NACK
-
-	// do not request silly ranges (happens in case of network large blackouts)
-	if (seq_order(last, first) || last - first > BUFFER_FRAMES / 2) return false;
-	
-	ctx->resent_req += last - first + 1;
-
-	LOG_DEBUG("resend request [W:%hu R:%hu first=%hu last=%hu]", ctx->ab_write, ctx->ab_read, first, last);
-
-	req[0] = 0x80;
-	req[1] = 0x55|0x80;  // Apple 'resend'
-	*(u16_t*)(req+2) = htons(1);  // our seqnum
-	*(u16_t*)(req+4) = htons(first);  // missed seqnum
-	*(u16_t*)(req+6) = htons(last-first+1);  // count
-
-	ctx->rtp_host.sin_port = htons(ctx->rtp_sockets[CONTROL].rport);
-
-	if (sizeof(req) != sendto(ctx->rtp_sockets[CONTROL].sock, req, sizeof(req), 0, (struct sockaddr*) &ctx->rtp_host, sizeof(ctx->rtp_host))) {
-		LOG_WARN("[%p]: SENDTO failed (%s)", ctx, strerror(errno));
-	}
-
-	return true;
-}
-
+
+/*
+ * HairTunes - RAOP packet handler and slave-clocked replay engine
+ * Copyright (c) James Laird 2011
+ * All rights reserved.
+ *
+ * Modularisation: philippe_44@outlook.com, 2019
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/types.h>
+#include <pthread.h>
+#include <math.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <assert.h>
+
+#include "platform.h"
+#include "rtp.h"
+#include "raop_sink.h"
+#include "log_util.h"
+#include "util.h"
+
+#ifdef WIN32
+#include <openssl/aes.h>
+#include "alac_wrapper.h"
+#else
+#include "esp_pthread.h"
+#include "esp_system.h"
+#include <mbedtls/version.h>
+#include <mbedtls/aes.h>
+#include "alac_wrapper.h"
+#endif
+
+#define NTP2MS(ntp) ((((ntp) >> 10) * 1000L) >> 22)
+#define MS2NTP(ms) (((((u64_t) (ms)) << 22) / 1000) << 10)
+#define NTP2TS(ntp, rate) ((((ntp) >> 16) * (rate)) >> 16)
+#define TS2NTP(ts, rate)  (((((u64_t) (ts)) << 16) / (rate)) << 16)
+#define MS2TS(ms, rate) ((((u64_t) (ms)) * (rate)) / 1000)
+#define TS2MS(ts, rate) NTP2MS(TS2NTP(ts,rate))
+
+
extern log_level 	raop_loglevel;
+
static log_level 	*loglevel = &raop_loglevel;
+
+//#define __RTP_STORE
+
+// default buffer size
+#define BUFFER_FRAMES 	( (150 * RAOP_SAMPLE_RATE * 2) / (352 * 100) )
+#define MAX_PACKET       1408
+#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)
+
+#define RESEND_TO	200
+
+enum { DATA = 0, CONTROL, TIMING };
+
+static const u8_t silence_frame[MAX_PACKET] = { 0 };
+
+typedef u16_t seq_t;
+typedef struct audio_buffer_entry {   // decoded audio packets
+	int ready;
+	u32_t rtptime, last_resend;
+	s16_t *data;
+	int len;
+} abuf_t;
+
+typedef struct rtp_s {
+#ifdef __RTP_STORE
+	FILE *rtpIN, *rtpOUT;
+#endif
+	bool running;
+	unsigned char aesiv[16];
+#ifdef WIN32
+	AES_KEY aes;
+#else
+	mbedtls_aes_context aes;
+#endif
+	bool decrypt;
+	u8_t *decrypt_buf;
+	u32_t frame_size, frame_duration;
+	u32_t in_frames, out_frames;
+	struct in_addr host;
+	struct sockaddr_in rtp_host;
+	struct {
+		unsigned short rport, lport;
+		int sock;
+	} rtp_sockets[3]; 					 // data, control, timing
+	struct timing_s {
+		u64_t local, remote;
+	} timing;
+	struct {
+		u32_t 	rtp, time;
+		u8_t  	status;
+	} synchro;
+	struct {
+		u32_t time;
+		seq_t seqno;
+		u32_t rtptime;
+	} record;
+	int latency;			// rtp hold depth in samples
+	u32_t resent_req, resent_rec;	// total resent + recovered frames
+	u32_t silent_frames;	// total silence frames
+	u32_t discarded;
+	abuf_t audio_buffer[BUFFER_FRAMES];
+	seq_t ab_read, ab_write;
+	pthread_mutex_t ab_mutex;
+#ifdef WIN32
+	pthread_t thread;
+#else
+	TaskHandle_t thread, joiner;
+	StaticTask_t *xTaskBuffer;
+    StackType_t xStack[RTP_STACK_SIZE] __attribute__ ((aligned (4)));
+#endif
+
+	struct alac_codec_s *alac_codec;
+	int flush_seqno;
+	bool playing;
+	raop_data_cb_t data_cb;
+	raop_cmd_cb_t cmd_cb;
+} rtp_t;
+
+
+#define BUFIDX(seqno) ((seq_t)(seqno) % BUFFER_FRAMES)
+static void 	buffer_alloc(abuf_t *audio_buffer, int size);
+static void 	buffer_release(abuf_t *audio_buffer);
+static void 	buffer_reset(abuf_t *audio_buffer);
+static void 	buffer_push_packet(rtp_t *ctx);
+static bool 	rtp_request_resend(rtp_t *ctx, seq_t first, seq_t last);
+static bool 	rtp_request_timing(rtp_t *ctx);
+static void*	rtp_thread_func(void *arg);
+static int	  	seq_order(seq_t a, seq_t b);
+
+/*---------------------------------------------------------------------------*/
+static struct alac_codec_s* alac_init(int fmtp[32]) {
+	struct alac_codec_s *alac;
+	unsigned sample_rate;
+	unsigned char sample_size, channels;
+	struct {
+		uint32_t	frameLength;
+		uint8_t		compatibleVersion;
+		uint8_t		bitDepth;
+		uint8_t		pb;
+		uint8_t		mb;
+		uint8_t		kb;
+		uint8_t		numChannels;
+		uint16_t	maxRun;
+		uint32_t	maxFrameBytes;
+		uint32_t	avgBitRate;
+		uint32_t	sampleRate;
+	} config;
+
+	config.frameLength = htonl(fmtp[1]);
+	config.compatibleVersion = fmtp[2];
+	config.bitDepth = fmtp[3];
+	config.pb = fmtp[4];
+	config.mb = fmtp[5];
+	config.kb = fmtp[6];
+	config.numChannels = fmtp[7];
+	config.maxRun = htons(fmtp[8]);
+	config.maxFrameBytes = htonl(fmtp[9]);
+	config.avgBitRate = htonl(fmtp[10]);
+	config.sampleRate = htonl(fmtp[11]);
+
+	alac = alac_create_decoder(sizeof(config), (unsigned char*) &config, &sample_size, &sample_rate, &channels);
+	if (!alac) {
+		LOG_ERROR("cannot create alac codec", NULL);
+		return NULL;
+	}
+
+	return alac;
+}
+
+/*---------------------------------------------------------------------------*/
+rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv, char *fmtpstr,
+								short unsigned pCtrlPort, short unsigned pTimingPort,
+								raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb)
+{
+	int i = 0;
+	char *arg;
+	int fmtp[12];
+	bool rc = true;
+	rtp_t *ctx = calloc(1, sizeof(rtp_t));
+	rtp_resp_t resp = { 0, 0, 0, NULL };
+
+	if (!ctx) return resp;
+	
+	ctx->host = host;
+	ctx->decrypt = false;
+	ctx->cmd_cb = cmd_cb;
+	ctx->data_cb = data_cb;
+	ctx->rtp_host.sin_family = AF_INET;
+	ctx->rtp_host.sin_addr.s_addr = INADDR_ANY;
+	pthread_mutex_init(&ctx->ab_mutex, 0);
+	ctx->flush_seqno = -1;
+	ctx->latency = latency;
+	ctx->ab_read = ctx->ab_write;
+
+#ifdef __RTP_STORE
+	ctx->rtpIN = fopen("airplay.rtpin", "wb");
+	ctx->rtpOUT = fopen("airplay.rtpout", "wb");
+#endif
+
+	ctx->rtp_sockets[CONTROL].rport = pCtrlPort;
+	ctx->rtp_sockets[TIMING].rport = pTimingPort;
+
+	if (aesiv && aeskey) {
+		memcpy(ctx->aesiv, aesiv, 16);
+#ifdef WIN32
+		AES_set_decrypt_key((unsigned char*) aeskey, 128, &ctx->aes);
+#else
+		memset(&ctx->aes, 0, sizeof(mbedtls_aes_context));
+		mbedtls_aes_setkey_dec(&ctx->aes, (unsigned char*) aeskey, 128);
+#endif
+		ctx->decrypt = true;
+		ctx->decrypt_buf = malloc(MAX_PACKET);
+	}
+
+	memset(fmtp, 0, sizeof(fmtp));
+	while ((arg = strsep(&fmtpstr, " \t")) != NULL) fmtp[i++] = atoi(arg);
+
+	ctx->frame_size = fmtp[1];
+	ctx->frame_duration = (ctx->frame_size * 1000) / RAOP_SAMPLE_RATE;
+
+	// alac decoder
+	ctx->alac_codec = alac_init(fmtp);
+	rc &= ctx->alac_codec != NULL;
+
+	buffer_alloc(ctx->audio_buffer, ctx->frame_size*4);
+
+	// create rtp ports
+	for (i = 0; i < 3; i++) {
+		ctx->rtp_sockets[i].sock = bind_socket(&ctx->rtp_sockets[i].lport, SOCK_DGRAM);
+		rc &= ctx->rtp_sockets[i].sock > 0;
+	}
+
+	// create http port and start listening
+	resp.cport = ctx->rtp_sockets[CONTROL].lport;
+	resp.tport = ctx->rtp_sockets[TIMING].lport;
+	resp.aport = ctx->rtp_sockets[DATA].lport;
+	
+	if (rc) {
+		ctx->running = true;
+#ifdef WIN32
+		pthread_create(&ctx->thread, NULL, rtp_thread_func, (void *) ctx);
+#else
+		// 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->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 {
+		LOG_ERROR("[%p]: cannot start RTP", ctx);
+		rtp_end(ctx);
+		ctx = NULL;
+	}
+
+	resp.ctx = ctx;
+
+	return resp;
+}
+
+/*---------------------------------------------------------------------------*/
+void rtp_end(rtp_t *ctx)
+{
+	int i;
+
+	if (!ctx) return;
+
+	if (ctx->running) {
+#if !defined WIN32		
+		ctx->joiner = xTaskGetCurrentTaskHandle();
+#endif
+		ctx->running = false;
+#ifdef WIN32
+		pthread_join(ctx->thread, NULL);
+#else
+		ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
+		vTaskDelete(ctx->thread);
+		heap_caps_free(ctx->xTaskBuffer);
+#endif
+	}
+	
+	for (i = 0; i < 3; i++) closesocket(ctx->rtp_sockets[i].sock);
+
+	if (ctx->alac_codec) alac_delete_decoder(ctx->alac_codec);
+	if (ctx->decrypt_buf) free(ctx->decrypt_buf);
+	
+	pthread_mutex_destroy(&ctx->ab_mutex);
+	buffer_release(ctx->audio_buffer);
+	
+	free(ctx);
+
+#ifdef __RTP_STORE
+	fclose(ctx->rtpIN);
+	fclose(ctx->rtpOUT);
+#endif
+}
+
+/*---------------------------------------------------------------------------*/
+bool rtp_flush(rtp_t *ctx, unsigned short seqno, unsigned int rtptime)
+{
+	bool rc = true;
+	u32_t now = gettime_ms();
+
+	if (now < ctx->record.time + 250 || (ctx->record.seqno == seqno && ctx->record.rtptime == rtptime)) {
+		rc = false;
+		LOG_ERROR("[%p]: FLUSH ignored as same as RECORD (%hu - %u)", ctx, seqno, rtptime);
+	} else {
+		pthread_mutex_lock(&ctx->ab_mutex);
+		buffer_reset(ctx->audio_buffer);
+		ctx->playing = false;
+		ctx->flush_seqno = seqno;
+		pthread_mutex_unlock(&ctx->ab_mutex);
+	}
+
+	LOG_INFO("[%p]: flush %hu %u", ctx, seqno, rtptime);
+
+	return rc;
+}
+
+/*---------------------------------------------------------------------------*/
+void rtp_record(rtp_t *ctx, unsigned short seqno, unsigned rtptime)
+{
+	ctx->record.seqno = seqno;
+	ctx->record.rtptime = rtptime;
+	ctx->record.time = gettime_ms();
+
+	LOG_INFO("[%p]: record %hu %u", ctx, seqno, rtptime);
+}
+
+/*---------------------------------------------------------------------------*/
+static void buffer_alloc(abuf_t *audio_buffer, int size) {
+	int i;
+	for (i = 0; i < BUFFER_FRAMES; i++) {
+		audio_buffer[i].data = malloc(size);
+		audio_buffer[i].ready = 0;
+	}
+}
+
+/*---------------------------------------------------------------------------*/
+static void buffer_release(abuf_t *audio_buffer) {
+	int i;
+	for (i = 0; i < BUFFER_FRAMES; i++) {
+		free(audio_buffer[i].data);
+	}
+}
+
+/*---------------------------------------------------------------------------*/
+static void buffer_reset(abuf_t *audio_buffer) {
+	int i;
+	for (i = 0; i < BUFFER_FRAMES; i++) audio_buffer[i].ready = 0;
+}
+
+/*---------------------------------------------------------------------------*/
+// the sequence numbers will wrap pretty often.
+// this returns true if the second arg is after the first
+static int seq_order(seq_t a, seq_t b) {
+	s16_t d = b - a;
+	return d > 0;
+}
+
+/*---------------------------------------------------------------------------*/
+static void alac_decode(rtp_t *ctx, s16_t *dest, char *buf, int len, int *outsize) {
+	unsigned char iv[16];
+	int aeslen;
+	assert(len<=MAX_PACKET);
+
+	if (ctx->decrypt) {
+		aeslen = len & ~0xf;
+		memcpy(iv, ctx->aesiv, sizeof(iv));
+#ifdef WIN32
+		AES_cbc_encrypt((unsigned char*)buf, ctx->decrypt_buf, aeslen, &ctx->aes, iv, AES_DECRYPT);
+#else
+		mbedtls_aes_crypt_cbc(&ctx->aes, MBEDTLS_AES_DECRYPT, aeslen, iv, (unsigned char*) buf, ctx->decrypt_buf);
+#endif
+		memcpy(ctx->decrypt_buf+aeslen, buf+aeslen, len-aeslen);
+		alac_to_pcm(ctx->alac_codec, (unsigned char*) ctx->decrypt_buf, (unsigned char*) dest, 2, (unsigned int*) outsize);
+	} else {
+		alac_to_pcm(ctx->alac_codec, (unsigned char*) buf, (unsigned char*) dest, 2, (unsigned int*) outsize);
+	}	
+	
+	*outsize *= 4;
+}
+
+
+/*---------------------------------------------------------------------------*/
+static void buffer_put_packet(rtp_t *ctx, seq_t seqno, unsigned rtptime, bool first, char *data, int len) {
+	abuf_t *abuf = NULL;
+	u32_t playtime;
+
+	pthread_mutex_lock(&ctx->ab_mutex);
+
+	if (!ctx->playing) {
+		if ((ctx->flush_seqno == -1 || seq_order(ctx->flush_seqno, seqno)) &&
+		   (ctx->synchro.status & RTP_SYNC) && (ctx->synchro.status & NTP_SYNC)) {
+			ctx->ab_write = seqno-1;
+			ctx->ab_read = seqno;
+			ctx->flush_seqno = -1;
+			ctx->playing = true;
+			ctx->resent_req = ctx->resent_rec = ctx->silent_frames = ctx->discarded = 0;
+			playtime = ctx->synchro.time + (((s32_t)(rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
+			ctx->cmd_cb(RAOP_PLAY, &playtime);
+		} else {
+			pthread_mutex_unlock(&ctx->ab_mutex);
+			return;
+		}
+	}
+
+	if (seqno == (u16_t) (ctx->ab_write+1)) {
+		// expected packet
+		abuf = ctx->audio_buffer + BUFIDX(seqno);
+		ctx->ab_write = seqno;
+		LOG_SDEBUG("packet expected seqno:%hu rtptime:%u (W:%hu R:%hu)", seqno, rtptime, ctx->ab_write, ctx->ab_read);
+
+	} else if (seq_order(ctx->ab_write, seqno)) {
+		// newer than expected
+		if (ctx->latency && seq_order(ctx->latency / ctx->frame_size, seqno - ctx->ab_write - 1)) {
+			// only get rtp latency-1 frames back (last one is seqno)
+			LOG_WARN("[%p] too many missing frames %hu seq: %hu, (W:%hu R:%hu)", ctx, seqno - ctx->ab_write - 1, seqno, ctx->ab_write, ctx->ab_read);
+			ctx->ab_write = seqno - ctx->latency / ctx->frame_size;
+		}
+		if (ctx->latency && seq_order(ctx->latency / ctx->frame_size, seqno - ctx->ab_read)) {
+			// if ab_read is lagging more than http latency, advance it
+			LOG_WARN("[%p] on hold for too long %hu (W:%hu R:%hu)", ctx, seqno - ctx->ab_read + 1, ctx->ab_write, ctx->ab_read);
+			ctx->ab_read = seqno - ctx->latency / ctx->frame_size + 1;
+		}
+		if (rtp_request_resend(ctx, ctx->ab_write + 1, seqno-1)) {
+			seq_t i;
+			u32_t now = gettime_ms();
+			for (i = ctx->ab_write + 1; seq_order(i, seqno); i++) {
+				ctx->audio_buffer[BUFIDX(i)].rtptime = rtptime - (seqno-i)*ctx->frame_size;
+				ctx->audio_buffer[BUFIDX(i)].last_resend = now;
+			}
+		}
+		LOG_DEBUG("[%p]: packet newer seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
+		abuf = ctx->audio_buffer + BUFIDX(seqno);
+		ctx->ab_write = seqno;
+	} else if (seq_order(ctx->ab_read, seqno + 1)) {
+		// recovered packet, not yet sent
+		abuf = ctx->audio_buffer + BUFIDX(seqno);
+		ctx->resent_rec++;
+		LOG_DEBUG("[%p]: packet recovered seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
+	} else {
+		// too late
+		LOG_DEBUG("[%p]: packet too late seqno:%hu rtptime:%u (W:%hu R:%hu)", ctx, seqno, rtptime, ctx->ab_write, ctx->ab_read);
+	}
+
+	if (ctx->in_frames++ > 1000) {
+		LOG_INFO("[%p]: fill [level:%hu rec:%u] [W:%hu R:%hu]", ctx, ctx->ab_write - ctx->ab_read, ctx->resent_rec, ctx->ab_write, ctx->ab_read);
+		ctx->in_frames = 0;
+	}
+
+	if (abuf) {
+		alac_decode(ctx, abuf->data, data, len, &abuf->len);
+		abuf->ready = 1;
+		// this is the local rtptime when this frame is expected to play
+		abuf->rtptime = rtptime;
+		buffer_push_packet(ctx);
+
+#ifdef __RTP_STORE
+		fwrite(data, len, 1, ctx->rtpIN);
+		fwrite(abuf->data, abuf->len, 1, ctx->rtpOUT);
+#endif
+	}
+
+	pthread_mutex_unlock(&ctx->ab_mutex);
+}
+
+/*---------------------------------------------------------------------------*/
+// push as many frames as possible through callback
+static void buffer_push_packet(rtp_t *ctx) {
+	abuf_t *curframe = NULL;
+	u32_t now, playtime, hold = max((ctx->latency * 1000) / (8 * RAOP_SAMPLE_RATE), 100);
+	int i;
+
+	// not ready to play yet
+	if (!ctx->playing ||  ctx->synchro.status != (RTP_SYNC | NTP_SYNC)) return;
+
+	// maybe re-evaluate time in loop in case data callback blocks ...
+	now = gettime_ms();
+
+	// there is always at least one frame in the buffer
+	do {
+
+		curframe = ctx->audio_buffer + BUFIDX(ctx->ab_read);
+		playtime = ctx->synchro.time + (((s32_t)(curframe->rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
+
+		if (now > playtime) {
+			LOG_DEBUG("[%p]: discarded frame now:%u missed by:%d (W:%hu R:%hu)", ctx, now, now - playtime, ctx->ab_write, ctx->ab_read);
+			ctx->discarded++;
+			curframe->ready = 0;
+		} else if (curframe->ready) {
+			ctx->data_cb((const u8_t*) curframe->data, curframe->len, playtime);
+			curframe->ready = 0;
+		} else if (playtime - now <= hold) {
+			LOG_DEBUG("[%p]: created zero frame (W:%hu R:%hu)", ctx, ctx->ab_write, ctx->ab_read);
+			ctx->data_cb(silence_frame, ctx->frame_size * 4, playtime);
+			ctx->silent_frames++;
+		} else break;
+
+		ctx->ab_read++;
+		ctx->out_frames++;
+
+	} while (seq_order(ctx->ab_read, ctx->ab_write));
+
+	if (ctx->out_frames > 1000) {
+		LOG_INFO("[%p]: drain [level:%hd head:%d ms] [W:%hu R:%hu] [req:%u sil:%u dis:%u]",
+				ctx, ctx->ab_write - ctx->ab_read, playtime - now, ctx->ab_write, ctx->ab_read,
+				ctx->resent_req, ctx->silent_frames, ctx->discarded);
+		ctx->out_frames = 0;
+	}
+
+	LOG_SDEBUG("playtime %u %d [W:%hu R:%hu] %d", playtime, playtime - now, ctx->ab_write, ctx->ab_read, curframe->ready);
+
+	// each missing packet will be requested up to (latency_frames / 16) times
+	for (i = 0; seq_order(ctx->ab_read + i, ctx->ab_write); i += 16) {
+		abuf_t *frame = ctx->audio_buffer + BUFIDX(ctx->ab_read + i);
+		if (!frame->ready && now - frame->last_resend > RESEND_TO) {
+			rtp_request_resend(ctx, ctx->ab_read + i, ctx->ab_read + i);
+			frame->last_resend = now;
+		}
+	}
+
}
+
+
+/*---------------------------------------------------------------------------*/
+static void *rtp_thread_func(void *arg) {
+	fd_set fds;
+	int i, sock = -1;
+	int count = 0;
+	bool ntp_sent;
+	char *packet = malloc(MAX_PACKET);
+	rtp_t *ctx = (rtp_t*) arg;
+
+	for (i = 0; i < 3; i++) {
+		if (ctx->rtp_sockets[i].sock > sock) sock = ctx->rtp_sockets[i].sock;
+		// send synchro requets 3 times
+		ntp_sent = rtp_request_timing(ctx);
+	}
+
+	while (ctx->running) {
+		ssize_t plen;
+		char type;
+		socklen_t rtp_client_len = sizeof(struct sockaddr_storage);
+		int idx = 0;
+		char *pktp = packet;
+		struct timeval timeout = {0, 100*1000};
+
+		FD_ZERO(&fds);
+		for (i = 0; i < 3; i++)	{ FD_SET(ctx->rtp_sockets[i].sock, &fds); }
+
+		if (select(sock + 1, &fds, NULL, NULL, &timeout) <= 0) continue;
+
+		for (i = 0; i < 3; i++)
+			if (FD_ISSET(ctx->rtp_sockets[i].sock, &fds)) idx = i;
+
+		plen = recvfrom(ctx->rtp_sockets[idx].sock, packet, MAX_PACKET, 0, (struct sockaddr*) &ctx->rtp_host, &rtp_client_len);
+
+		if (!ntp_sent) {
+			LOG_WARN("[%p]: NTP request not send yet", ctx);
+			ntp_sent = rtp_request_timing(ctx);
+		}
+
+		if (plen < 0) continue;
+		assert(plen <= MAX_PACKET);
+
+		type = packet[1] & ~0x80;
+		pktp = packet;
+
+		switch (type) {
+			seq_t seqno;
+			unsigned rtptime;
+
+			// re-sent packet
+			case 0x56: {
+				pktp += 4;
+				plen -= 4;
+			}
+
+			// data packet
+			case 0x60: {
+				seqno = ntohs(*(u16_t*)(pktp+2));
+				rtptime = ntohl(*(u32_t*)(pktp+4));
+
+				// adjust pointer and length
+				pktp += 12;
+				plen -= 12;
+
+				LOG_SDEBUG("[%p]: seqno:%hu rtp:%u (type: %x, first: %u)", ctx, seqno, rtptime, type, packet[1] & 0x80);
+
+				// check if packet contains enough content to be reasonable
+				if (plen < 16) break;
+
+				if ((packet[1] & 0x80) && (type != 0x56)) {
+					LOG_INFO("[%p]: 1st audio packet received", ctx);
+				}
+
+				buffer_put_packet(ctx, seqno, rtptime, packet[1] & 0x80, pktp, plen);
+
+				break;
+			}
+
+			// sync packet
+			case 0x54: {
+				u32_t rtp_now_latency = ntohl(*(u32_t*)(pktp+4));
+				u64_t remote = (((u64_t) ntohl(*(u32_t*)(pktp+8))) << 32) + ntohl(*(u32_t*)(pktp+12));
+				u32_t rtp_now = ntohl(*(u32_t*)(pktp+16));
+				u16_t flags = ntohs(*(u16_t*)(pktp+2));
+				u32_t remote_gap = NTP2MS(remote - ctx->timing.remote);
+				
+				if (remote_gap > 10000) {
+					LOG_WARN("discarding remote timing information %u", remote_gap);
+					break;
+				}
+
+				pthread_mutex_lock(&ctx->ab_mutex);
+
+				// re-align timestamp and expected local playback time (and magic 11025 latency)
+				ctx->latency = rtp_now - rtp_now_latency;
+				if (flags == 7 || flags == 4) ctx->latency += 11025;
+				if (ctx->latency < MIN_LATENCY) ctx->latency = MIN_LATENCY;
+				else if (ctx->latency > MAX_LATENCY) ctx->latency = MAX_LATENCY;
+				ctx->synchro.rtp = rtp_now - ctx->latency;
+				ctx->synchro.time = ctx->timing.local + remote_gap;
+
+				// now we are synced on RTP frames
+				ctx->synchro.status |= RTP_SYNC;
+
+				// 1st sync packet received (signals a restart of playback)
+				if (packet[0] & 0x10) {
+					LOG_INFO("[%p]: 1st sync packet received", ctx);
+				}
+
+				pthread_mutex_unlock(&ctx->ab_mutex);
+
+				LOG_DEBUG("[%p]: sync packet latency:%d rtp_latency:%u rtp:%u remote ntp:%llx, local time:%u local rtp:%u (now:%u)",
+						  ctx, ctx->latency, rtp_now_latency, rtp_now, remote, ctx->synchro.time, ctx->synchro.rtp, gettime_ms());
+
+				if (!count--) {
+					rtp_request_timing(ctx);
+					count = 3;
+				}
+
+				if ((ctx->synchro.status & RTP_SYNC) && (ctx->synchro.status & NTP_SYNC)) ctx->cmd_cb(RAOP_TIMING, NULL);
+
+				break;
+			}
+
+			// NTP timing packet
+			case 0x53: {
+				u64_t expected;
+				u32_t reference   = ntohl(*(u32_t*)(pktp+12)); // only low 32 bits in our case
+				u64_t remote 	  =(((u64_t) ntohl(*(u32_t*)(pktp+16))) << 32) + ntohl(*(u32_t*)(pktp+20));
+				u32_t roundtrip   = gettime_ms() - reference;
+
+				// better discard sync packets when roundtrip is suspicious
+				if (roundtrip > 100) {
+					LOG_WARN("[%p]: discarding NTP roundtrip of %u ms", ctx, roundtrip);
+					break;
+				}
+
+				/*
+				  The expected elapsed remote time should be exactly the same as
+				  elapsed local time between the two request, corrected by the
+				  drifting
+				*/
+				expected = ctx->timing.remote + MS2NTP(reference - ctx->timing.local);
+
+				ctx->timing.remote = remote;
+				ctx->timing.local = reference;
+
+				// now we are synced on NTP (mutex not needed)
+				ctx->synchro.status |= NTP_SYNC;
+
+				LOG_DEBUG("[%p]: Timing references local:%llu, remote:%llx (delta:%lld, sum:%lld, adjust:%lld, gaps:%d)",
+						  ctx, ctx->timing.local, ctx->timing.remote);
+
+				break;
+			}
+		}
+	}
+
+	free(packet);
+	LOG_INFO("[%p]: terminating", ctx);
+
+#ifndef WIN32
+	xTaskNotifyGive(ctx->joiner);
+	vTaskSuspend(NULL);
+#endif
+
+	return NULL;
+}
+
+/*---------------------------------------------------------------------------*/
+static bool rtp_request_timing(rtp_t *ctx) {
+	unsigned char req[32];
+	u32_t now = gettime_ms();
+	int i;
+	struct sockaddr_in host;
+
+	LOG_DEBUG("[%p]: timing request now:%u (port: %hu)", ctx, now, ctx->rtp_sockets[TIMING].rport);
+
+	req[0] = 0x80;
+	req[1] = 0x52|0x80;
+	*(u16_t*)(req+2) = htons(7);
+	*(u32_t*)(req+4) = htonl(0);  // dummy
+	for (i = 0; i < 16; i++) req[i+8] = 0;
+	*(u32_t*)(req+24) = 0;
+	*(u32_t*)(req+28) = htonl(now); // this is not a real NTP, but a 32 ms counter in the low part of the NTP
+
+	if (ctx->host.s_addr != INADDR_ANY) {
+		host.sin_family = AF_INET;
+		host.sin_addr =	ctx->host;
+	} else host = ctx->rtp_host;
+
+	// no address from sender, need to wait for 1st packet to be received
+	if (host.sin_addr.s_addr == INADDR_ANY) return false;
+
+	host.sin_port = htons(ctx->rtp_sockets[TIMING].rport);
+
+	if (sizeof(req) != sendto(ctx->rtp_sockets[TIMING].sock, req, sizeof(req), 0, (struct sockaddr*) &host, sizeof(host))) {
+		LOG_WARN("[%p]: SENDTO failed (%s)", ctx, strerror(errno));
+	}
+
+	return true;
+}
+
+/*---------------------------------------------------------------------------*/
+static bool rtp_request_resend(rtp_t *ctx, seq_t first, seq_t last) {
+	unsigned char req[8];    // *not* a standard RTCP NACK
+
+	// do not request silly ranges (happens in case of network large blackouts)
+	if (seq_order(last, first) || last - first > BUFFER_FRAMES / 2) return false;
+	
+	ctx->resent_req += last - first + 1;
+
+	LOG_DEBUG("resend request [W:%hu R:%hu first=%hu last=%hu]", ctx->ab_write, ctx->ab_read, first, last);
+
+	req[0] = 0x80;
+	req[1] = 0x55|0x80;  // Apple 'resend'
+	*(u16_t*)(req+2) = htons(1);  // our seqnum
+	*(u16_t*)(req+4) = htons(first);  // missed seqnum
+	*(u16_t*)(req+6) = htons(last-first+1);  // count
+
+	ctx->rtp_host.sin_port = htons(ctx->rtp_sockets[CONTROL].rport);
+
+	if (sizeof(req) != sendto(ctx->rtp_sockets[CONTROL].sock, req, sizeof(req), 0, (struct sockaddr*) &ctx->rtp_host, sizeof(ctx->rtp_host))) {
+		LOG_WARN("[%p]: SENDTO failed (%s)", ctx, strerror(errno));
+	}
+
+	return true;
+}
+

+ 1 - 1
components/services/CMakeLists.txt

@@ -1,4 +1,4 @@
-idf_component_register(SRCS "led.c" 
+idf_component_register(SRCS "led.c" "audio_controls.c" "buttons.c" "services.c" "monitor.c"INCLUDE_DIRS 
 						INCLUDE_DIRS . ../tools/
                    
 )

+ 141 - 2
components/services/audio_controls.c

@@ -24,11 +24,17 @@
 #include <unistd.h>
 #include "esp_system.h"
 #include "esp_log.h"
+#include "cJSON.h"
 #include "buttons.h"
 #include "audio_controls.h"
+// BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
+static const char * actrls_action_s[ ] = { "ACTRLS_VOLUP","ACTRLS_VOLDOWN","ACTRLS_TOGGLE","ACTRLS_PLAY",
+									"ACTRLS_PAUSE","ACTRLS_STOP","ACTRLS_REW","ACTRLS_FWD","ACTRLS_PREV","ACTRLS_NEXT",
+									"BCTRLS_PUSH", "BCTRLS_UP","BCTRLS_DOWN","BCTRLS_LEFT","BCTRLS_RIGHT", ""} ;
 
-static const char * TAG = "audio controls";
 
+static const char * TAG = "audio controls";
+static actrls_config_t *json_config;
 static actrls_t default_controls, current_controls;
 
 static void control_handler(void *id, button_event_e event, button_press_e press, bool long_press) {
@@ -86,15 +92,148 @@ void down(void *id, button_event_e event, button_press_e press, bool longpress)
 /****************************************************************************************
  * 
  */
-void actrls_init(int n, const actrls_config_t *config) {
+esp_err_t actrls_init(int n, const actrls_config_t *config) {
 	for (int i = 0; i < n; i++) {
 		button_create((void*) (config + i), config[i].gpio, config[i].type, config[i].pull, control_handler, config[i].long_press, config[i].shifter_gpio);
 	}
+	return ESP_OK;
+}
+
+actrls_action_e actrls_parse_action_json(const char * name){
+
+	for(int i=0;actrls_action_s[i][0]!='\0' && i<ACTRLS_MAX;i++){
+		if(!strcmp(actrls_action_s[i], name)){
+			return (actrls_action_e) i;
+		}
+	}
+	return ACTRLS_NONE;
+}
+esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_config){
+	const actrls_config_map_t * map=NULL;
+	esp_err_t err=ESP_OK;
+	char *config_pointer = (char*) cur_config;
+	cJSON *button_action;
+
+	char * string = cJSON_Print(member);
+	ESP_LOGD(TAG, "Processing structure member json : %s", string);
+	free(string);
+
+	for(int i = 0;!map && actrls_config_map[i].type!=ACTRLS_MAP_END ;i++){
+		if(!strcmp(member->string, actrls_config_map[i].member)){
+			map = &actrls_config_map[i];
+		}
+	}
+
+	if(!map){
+		ESP_LOGE(TAG,"Unknown structure member [%s]", member->string?member->string:"");
+		return ESP_FAIL;
+	}
+
+	ESP_LOGD(TAG,
+			"Found map type %d at structure offset %u",
+			map->type, map->offset);
+	void *value = (config_pointer + map->offset);
+	switch (map->type) {
+	case ACTRLS_MAP_TYPE:
+		if (member->child != NULL) {
+			*(int*) value =
+					!strcmp(member->child->string,
+							"BUTTON_LOW") ?
+							BUTTON_LOW : BUTTON_HIGH;
+		} else {
+			ESP_LOGE(TAG,
+					"Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
+		}
+		break;
+	case ACTRLS_MAP_INT:
+		*(int*) value = member->valueint;
+		break;
+	case ACTRLS_MAP_BOOL:
+		*(bool*) value = cJSON_IsTrue(member);
+		break;
+	case ACTRLS_MAP_ACTION:
+		button_action= cJSON_GetObjectItemCaseSensitive(
+				member, "pressed");
+		if (button_action != NULL) {
+			((actrls_action_e*) value)[0] =
+					actrls_parse_action_json(
+							button_action->string);
+		}
+		button_action = cJSON_GetObjectItemCaseSensitive(
+				member, "released");
+		if (button_action != NULL) {
+			((actrls_action_e*) value)[1] =
+					actrls_parse_action_json(
+							button_action->string);
+		}
+		break;
+
+	default:
+		break;
+	}
+
+
+	if (err == ESP_OK) {
+		button_create((void*) cur_config, cur_config->gpio,
+				cur_config->type, cur_config->pull,
+				control_handler, cur_config->long_press,
+				cur_config->shifter_gpio);
+	}
+
+	return err;
 }
 
+
 /****************************************************************************************
  * 
  */
+esp_err_t actrls_init_json(const char *config) {
+	esp_err_t err = ESP_OK;
+
+	actrls_config_t *cur_config;
+
+	cJSON *buttons = cJSON_Parse(config);
+	if (buttons != NULL) {
+		if (cJSON_IsArray(buttons)) {
+			int member_count = 0;
+			const cJSON *button;
+			const cJSON *member;
+			cJSON_ArrayForEach(button, buttons)
+			{
+				member_count++;
+			}
+			ESP_LOGD(TAG, "config contains %u button definitions",
+					member_count);
+			if (member_count == 0) {
+				return ESP_FAIL;
+			}
+			json_config = malloc(sizeof(actrls_config_t) * member_count);
+			memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
+			cur_config = json_config;
+
+			cJSON_ArrayForEach(button, buttons)
+			{
+				esp_err_t loc_err = ESP_OK;
+				cJSON_ArrayForEach(member, button)
+				{
+					actrls_parse_config_map(member, cur_config);
+					err = err == ESP_OK ? loc_err : err;
+				}
+				cur_config++;
+			}
+		}
+
+		cJSON_Delete(buttons);
+	} else {
+		err = ESP_FAIL;
+	}
+
+	return err;
+}
+
+/****************************************************************************************
+ *
+ */
 void actrls_set_default(const actrls_t controls) {
 	memcpy(default_controls, controls, sizeof(actrls_t));
 	memcpy(current_controls, default_controls, sizeof(actrls_t));

+ 30 - 1
components/services/audio_controls.h

@@ -26,6 +26,12 @@ typedef enum { 	ACTRLS_NONE = -1, ACTRLS_VOLUP, ACTRLS_VOLDOWN, ACTRLS_TOGGLE, A
 				BCTRLS_PUSH, BCTRLS_UP, BCTRLS_DOWN, BCTRLS_LEFT, BCTRLS_RIGHT,
 				ACTRLS_MAX 
 		} actrls_action_e;
+typedef enum {
+	ACTRLS_MAP_INT, ACTRLS_MAP_BOOL, ACTRLS_MAP_ACTION,ACTRLS_MAP_TYPE,ACTRLS_MAP_END
+} actrls_action_map_element_type_e;
+
+
+
 typedef void (*actrls_handler)(void);
 typedef actrls_handler actrls_t[ACTRLS_MAX - ACTRLS_NONE - 1];
 typedef struct {
@@ -37,7 +43,30 @@ typedef struct {
 	actrls_action_e normal[2], longpress[2], shifted[2], longshifted[2];	// [0] keypressed, [1] keyreleased
 } actrls_config_t;
 
-void actrls_init(int n, const actrls_config_t *config);
+
+
+typedef struct {
+	char * member;
+	uint32_t offset;
+	actrls_action_map_element_type_e type;
+} actrls_config_map_t;
+
+static const actrls_config_map_t actrls_config_map[] =
+		{
+			{"gpio", offsetof(actrls_config_t,gpio), ACTRLS_MAP_INT},
+			{"type", offsetof(actrls_config_t,type),ACTRLS_MAP_TYPE},
+			{"pull", offsetof(actrls_config_t,pull),ACTRLS_MAP_BOOL},
+			{"long_press", offsetof(actrls_config_t,long_press),ACTRLS_MAP_INT},
+			{"shifter_gpio", offsetof(actrls_config_t,shifter_gpio),ACTRLS_MAP_INT},
+			{"normal", offsetof(actrls_config_t,normal), ACTRLS_MAP_ACTION},
+			{"longpress", offsetof(actrls_config_t,longpress), ACTRLS_MAP_ACTION},
+			{"longshifted", offsetof(actrls_config_t,longshifted), ACTRLS_MAP_ACTION},
+			{"", 0,ACTRLS_MAP_END}
+		};
+
+
+esp_err_t actrls_init(int n, const actrls_config_t *config);
+esp_err_t actrls_init_json(const char * config);
 void actrls_set_default(const actrls_t controls);
 void actrls_set(const actrls_t controls);
 void actrls_unset(void);

+ 4 - 0
components/services/battery.c

@@ -32,14 +32,18 @@ static struct {
 /****************************************************************************************
  * 
  */
+#ifdef CONFIG_SQUEEZEAMP
 static void battery_callback(TimerHandle_t xTimer) {
+
 	battery.sum += adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1;
 	if (++battery.count == 30) {
 		battery.avg = battery.sum / battery.count;
 		battery.sum = battery.count = 0;
 		ESP_LOGI(TAG, "Voltage %.2fV", battery.avg);
 	}	
+
 }
+#endif
 
 /****************************************************************************************
  * 

BIN
full_squeezelite.bin


+ 26 - 26
main/esp_app_main.c

@@ -48,6 +48,11 @@
 #include "config.h"
 #include "audio_controls.h"
 
+// todo:  this should be moved to the build scripts definitions
+static const char * actrls_brd1 = "[{\"gpio\":4,\"type\":\"BUTTON_LOW\",\"pull\":true,\"long_press\":1000,\"shifter_gpio\":-1,\"normal\":{\"pressed\":\"ACTRLS_VOLUP\",\"released\":\"ACTRLS_NONE\"},\"longpress\":{\"pressed\":\"ACTRLS_PREV\",\"released\":\"ACTRLS_NONE\"},\"shifted\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"},\"longshifted\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"}},{\"gpio\":5,\"type\":\"BUTTON_LOW\",\"pull\":true,\"long_press\":1000,\"shifter_gpio\":4,\"normal\":{\"pressed\":\"ACTRLS_VOLDOWN\",\"released\":\"ACTRLS_NONE\"},\"longpress\":{\"pressed\":\"ACTRLS_NEXT\",\"released\":\"ACTRLS_NONE\"},\"shifted\":{\"pressed\":\"ACTRLS_TOGGLE\",\"released\":\"ACTRLS_NONE\"},\"longshifted\":{\"pressed\":\"ACTRLS_NEXT\",\"released\":\"ACTRLS_NONE\"}}]";
+static const char * actrls_brd2 ="[{\"gpio\":21,\"type\":\"BUTTON_LOW\",\"pull\":true,\"long_press\":1000,\"shifter_gpio\":-1,\"normal\":{\"pressed\":\"ACTRLS_TOGGLE\",\"released\":\"ACTRLS_NONE\"},\"longpress\":{\"pressed\":\"ACTRLS_STOP\",\"released\":\"ACTRLS_NONE\"},\"shifted\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"},\"longshifted\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"}},{\"gpio\":18,\"type\":\"BUTTON_LOW\",\"pull\":true,\"long_press\":1000,\"shifter_gpio\":21,\"normal\":{\"pressed\":\"ACTRLS_VOLUP\",\"released\":\"ACTRLS_NONE\"},\"longpress\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"},\"shifted\":{\"pressed\":\"ACTRLS_NEXT\",\"released\":\"ACTRLS_NONE\"},\"longshifted\":{\"pressed\":\"ACTRLS_FWD\",\"released\":\"ACTRLS_PLAY\"}},{\"gpio\":19,\"type\":\"BUTTON_LOW\",\"pull\":true,\"long_press\":1000,\"shifter_gpio\":21,\"normal\":{\"pressed\":\"ACTRLS_VOLDOWN\",\"released\":\"ACTRLS_NONE\"},\"longpress\":{\"pressed\":\"ACTRLS_NONE\",\"released\":\"ACTRLS_NONE\"},\"shifted\":{\"pressed\":\"ACTRLS_PREV\",\"released\":\"ACTRLS_NONE\"},\"longshifted\":{\"pressed\":\"ACTRLS_REW\",\"released\":\"ACTRLS_PLAY\"}}]";
+
+
 extern bool enable_bt_sink;
 extern bool enable_airplay;
 extern bool jack_mutes_amp;
@@ -78,26 +83,6 @@ extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
 
 extern void services_init(void);
 
-static const actrls_config_t board_1[] = {
-	//								normal 							long						shifted						long shifted											
-	{ 4, BUTTON_LOW, true, 1000, -1, {ACTRLS_VOLUP, ACTRLS_NONE}, 	{ACTRLS_PREV, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE} },
-	{ 5, BUTTON_LOW, true, 1000, 4,  {ACTRLS_VOLDOWN, ACTRLS_NONE}, {ACTRLS_NEXT, ACTRLS_NONE}, {ACTRLS_TOGGLE, ACTRLS_NONE}, {BCTRLS_DOWN, ACTRLS_NONE} },
-};
-
-static const actrls_config_t board_2[] = {
-	//								normal 							long						shifted						long shifted											
-	{ 21, BUTTON_LOW, true, 1000, -1, 	{ACTRLS_TOGGLE, ACTRLS_NONE}, {ACTRLS_STOP, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE} },
-	{ 18, BUTTON_LOW, true, 1000, 21, {ACTRLS_VOLUP, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE}, {ACTRLS_NEXT, ACTRLS_NONE}, 	{ACTRLS_FWD, ACTRLS_PLAY} },
-	{ 19, BUTTON_LOW, true, 1000, 21, {ACTRLS_VOLDOWN, ACTRLS_NONE}, {ACTRLS_NONE, ACTRLS_NONE}, {ACTRLS_PREV, ACTRLS_NONE}, {ACTRLS_REW, ACTRLS_PLAY} },
-};
-
-static const struct {
-	int n;
-	const actrls_config_t *config;
-} board_configs[] = {
-	{ 2, board_1 },
-	{ 3, board_2 },
-};	
 
 /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
 void cb_connection_got_ip(void *pvParameter){
@@ -308,8 +293,14 @@ void register_default_nvs(){
 	ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bypass_wm", "0");
 	config_set_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
 
-	ESP_LOGD(TAG,"Registering default value for key %s, value %s", "test_num", "0");
+	ESP_LOGD(TAG,"Registering Audio control board type %s, value %s","actrls_brd1",actrls_brd1);
+	config_set_default(NVS_TYPE_STR, "actrls_brd1", actrls_brd1, 0);
 
+	ESP_LOGD(TAG,"Registering Audio control board type %s, value %s","actrls_brd2", actrls_brd1);
+	config_set_default(NVS_TYPE_STR, "actrls_brd2", actrls_brd2, 0);
+
+	ESP_LOGD(TAG,"Registering Defalt Audio control board type %s, value ","actrls_brd");
+	config_set_default(NVS_TYPE_STR, "actrls_brd", "", 0);
 
 	char number_buffer[101] = {};
 	snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_FLASH_ERASE_BLOCK);
@@ -402,11 +393,20 @@ void app_main()
 	ESP_LOGD(TAG,"Configuring Red led");
 	led_config(LED_RED, LED_RED_GPIO, 0);
 
-	char *board_index = config_alloc_get_default(NVS_TYPE_STR, "board_index", NULL, 0);
-	if (board_index) {
-		ESP_LOGD(TAG,"Initializing audio control buttons index %u", atoi(board_index));
-		actrls_init(board_configs[atoi(board_index)].n, (actrls_config_t*) board_configs[atoi(board_index)].config);
-		free(board_index);
+	char *actrls_brd = config_alloc_get_default(NVS_TYPE_STR, "actrls_brd", NULL, 0);
+	if (actrls_brd) {
+		if(actrls_brd[0] !='\0'){
+			ESP_LOGD(TAG,"Initializing audio control buttons board type %s", actrls_brd);
+			char *actrls_brd_json = config_alloc_get_default(NVS_TYPE_STR, actrls_brd, NULL, 0);
+			if(actrls_brd_json){
+				actrls_init_json(actrls_brd_json);
+				free(actrls_brd_json);
+			}
+			else {
+				ESP_LOGE(TAG,"Audio controls board type %s could not be found",actrls_brd);
+			}
+		}
+		free(actrls_brd);
 	}
 
 	/* start the wifi manager */