From 338c0492542ffb3e342355e21781f4ed17a515dd Mon Sep 17 00:00:00 2001 From: Karl Osterseher Date: Fri, 23 Dec 2022 23:16:40 +0100 Subject: [PATCH] - improve latency measurement and make sync almost perfect Signed-off-by: Karl Osterseher --- components/libmedian/MedianFilter.c | 6 ++--- components/lightsnapcast/include/player.h | 13 ++++++----- components/lightsnapcast/player.c | 5 ++-- main/main.c | 28 ++++++++++++++++++----- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/components/libmedian/MedianFilter.c b/components/libmedian/MedianFilter.c index f432999..334c56c 100644 --- a/components/libmedian/MedianFilter.c +++ b/components/libmedian/MedianFilter.c @@ -54,9 +54,9 @@ int64_t MEDIANFILTER_Insert(sMedianFilter_t *medianFilter, int64_t sample) { unsigned int i; sMedianNode_t *newNode, *it; - if (medianFilter->ageHead == - medianFilter->valueHead) { // if oldest node is also the smallest node, - // increment value head + // if oldest node is also the smallest node, + // increment value head + if (medianFilter->ageHead == medianFilter->valueHead) { medianFilter->valueHead = medianFilter->valueHead->nextValue; } diff --git a/components/lightsnapcast/include/player.h b/components/lightsnapcast/include/player.h index c8b25c5..8ac2648 100644 --- a/components/lightsnapcast/include/player.h +++ b/components/lightsnapcast/include/player.h @@ -14,12 +14,13 @@ #define CHNK_CTRL_CNT 2 #define LATENCY_MEDIAN_FILTER_LEN 199 // 299 //499 // 199 // 29 // 99 -#define LATENCY_MEDIAN_AVG_DIVISOR \ - 0 // set to 0 if you do not wish to be the median an average around actual - // median average will be (LATENCY_MEDIAN_FILTER_LEN / - // LATENCY_MEDIAN_AVG_DIVISOR) + 1 samples around median. e.g. if n=4 then - // 2 samples above and below will be added plus the actual median. So in - // reality n+1 samples will be averaged + +// set to 0 if you do not wish to be the median an average around actual +// median average will be (LATENCY_MEDIAN_FILTER_LEN / +// LATENCY_MEDIAN_AVG_DIVISOR) + 1 samples around median. e.g. if n=4 then +// 2 samples above and below will be added plus the actual median. So in +// reality n+1 samples will be averaged +#define LATENCY_MEDIAN_AVG_DIVISOR 0 #define SHORT_BUFFER_LEN 199 // 99 #define MINI_BUFFER_LEN 39 // 19 diff --git a/components/lightsnapcast/player.c b/components/lightsnapcast/player.c index 90fe7b9..2dbd052 100644 --- a/components/lightsnapcast/player.c +++ b/components/lightsnapcast/player.c @@ -1386,8 +1386,9 @@ static void player_task(void *pvParameters) { usec = usec % 1000; // ESP_LOGI (TAG, "%d, %lldus, %lldus %llds, %lld.%lldms", dir, age, // avg, sec, msec, usec); - ESP_LOGI(TAG, "%d, %lldus, %lldus, %lldus", dir, avg, shortMedian, - miniMedian); + // ESP_LOGI(TAG, "%d, %lldus, %lldus, %lldus", dir, avg, + // shortMedian, + // miniMedian); } dir = 0; diff --git a/main/main.c b/main/main.c index dd20cf9..aca955d 100644 --- a/main/main.c +++ b/main/main.c @@ -75,8 +75,8 @@ SemaphoreHandle_t decoderWriteSemaphore = NULL; const char *VERSION_STRING = "0.0.2"; -#define HTTP_TASK_PRIORITY 9 -#define HTTP_TASK_CORE_ID tskNO_AFFINITY // 1 // tskNO_AFFINITY +#define HTTP_TASK_PRIORITY (configMAX_PRIORITIES - 2) // 9 +#define HTTP_TASK_CORE_ID 1 // 1 // tskNO_AFFINITY #define OTA_TASK_PRIORITY 6 #define OTA_TASK_CORE_ID tskNO_AFFINITY // 1 // tskNO_AFFINITY @@ -138,8 +138,10 @@ void time_sync_msg_cb(void *args); static char base_message_serialized[BASE_MESSAGE_SIZE]; static char time_message_serialized[TIME_MESSAGE_SIZE]; -static const esp_timer_create_args_t tSyncArgs = {.callback = &time_sync_msg_cb, - .name = "tSyncMsg"}; +static const esp_timer_create_args_t tSyncArgs = { + .callback = &time_sync_msg_cb, + .dispatch_method = ESP_TIMER_TASK, + .name = "tSyncMsg"}; struct netconn *lwipNetconn; @@ -198,6 +200,7 @@ void time_sync_msg_cb(void *args) { return; } +#if 0 rc1 = netconn_write(lwipNetconn, base_message_serialized, BASE_MESSAGE_SIZE, NETCONN_NOCOPY); if (rc1 != ERR_OK) { @@ -213,6 +216,20 @@ void time_sync_msg_cb(void *args) { return; } +#else + uint8_t *p_pkt = (uint8_t *)malloc(BASE_MESSAGE_SIZE + TIME_MESSAGE_SIZE); + memcpy(&p_pkt[0], base_message_serialized, BASE_MESSAGE_SIZE); + memcpy(&p_pkt[BASE_MESSAGE_SIZE], time_message_serialized, TIME_MESSAGE_SIZE); + + rc1 = netconn_write(lwipNetconn, p_pkt, BASE_MESSAGE_SIZE + TIME_MESSAGE_SIZE, + NETCONN_NOCOPY); + if (rc1 != ERR_OK) { + ESP_LOGW(TAG, "error writing timesync msg"); + + return; + } + free(p_pkt); +#endif // ESP_LOGI(TAG, "%s: sent time sync message", __func__); @@ -2332,8 +2349,7 @@ static void http_get_task(void *pvParameters) { player_latency_insert(tmpDiffToServer); - // ESP_LOGI(TAG, "Current latency: %lld", - // tmpDiffToServer); + ESP_LOGI(TAG, "Current latency: %lld", tmpDiffToServer); // store current time lastTimeSync = now;