diff --git a/main/main.c b/main/main.c index a5e5f77..59777ca 100644 --- a/main/main.c +++ b/main/main.c @@ -41,6 +41,12 @@ #include + + +uint64_t wirechnkCnt = 0; +uint64_t pcmchnkCnt = 0; + + #define CONFIG_USE_SNTP 0 #define DAC_OUT_BUFFER_TIME_US 0//3 * 1526LL //TODO: not sure about this... // @48kHz, 16bit samples, 2 channels and a DMA buffer length of 300 Byte and 3 buffers. 300 Byte / (48000 * 2 Byte * 2 channels) @@ -54,7 +60,7 @@ char *codecString = NULL; // configMAX_PRIORITIES - 1 // TODO: what are the best values here? -#define SYNC_TASK_PRIORITY 12 +#define SYNC_TASK_PRIORITY 6 #define SYNC_TASK_CORE_ID 1 #define HTTP_TASK_PRIORITY 6 @@ -63,11 +69,14 @@ char *codecString = NULL; #define I2S_TASK_PRIORITY 0 #define I2S_TASK_CORE_ID 1 +#define FLAC_DECODER_PRIORITY 6 +#define FLAC_DECODER_CORE_ID 1 + #define AGE_THRESHOLD 50LL // in µs QueueHandle_t timestampQueueHandle; -#define TIMESTAMP_QUEUE_LENGTH 150 // TODO: what's the minimum value needed here, although probably not that important because we create queue using xQueueCreate() +#define TIMESTAMP_QUEUE_LENGTH 5 // TODO: what's the minimum value needed here, although probably not that important because we create queue using xQueueCreate() static StaticQueue_t timestampQueue; uint8_t timestampQueueStorageArea[ TIMESTAMP_QUEUE_LENGTH * sizeof(tv_t) ]; @@ -107,11 +116,9 @@ audio_board_handle_t board_handle; /* Constants that aren't configurable in menuconfig */ #define HOST "192.168.1.6" #define PORT 1704 -#define BUFF_LEN 5000 +#define BUFF_LEN 10000 unsigned int addr; uint32_t port = 0; -/* Logging tag */ -//static const char *TAG = "SNAPCAST"; /* FreeRTOS event group to signal when we are connected & ready to make a request */ //static EventGroupHandle_t wifi_event_group; @@ -448,14 +455,14 @@ static void snapcast_sync_task(void *pvParameters) { while(1) { if (chnk == NULL) { - ret = xQueueReceive(pcmChunkQueueHandle, &chnk, portMAX_DELAY ); + ret = xQueueReceive(pcmChunkQueueHandle, &chnk, pdMS_TO_TICKS(3000) ); } else { ret = pdPASS; } if( ret == pdPASS ) { - if (initial_sync > 5) { // hard sync was successfull? + if (initial_sync > 5) { // hard sync was successful? if (server_now(&serverNow) >= 0) { age = ((int64_t)serverNow.tv_sec * 1000000LL + (int64_t)serverNow.tv_usec) - ((int64_t)chnk->timestamp.sec * 1000000LL + (int64_t)chnk->timestamp.usec) - @@ -500,7 +507,14 @@ static void snapcast_sync_task(void *pvParameters) { // ESP_LOGI(TAG, "after age: %lldus", age); // } - raw_stream_write(*(taskCfg->p_raw_stream_writer), chnk->payload, chnk->size); + int bytesWritten = 0; + while ( bytesWritten < chnk->size ) { + bytesWritten += raw_stream_write(*(taskCfg->p_raw_stream_writer), chnk->payload, chnk->size); + if (bytesWritten < chnk->size) { + ESP_LOGE(TAG, "i2s raw writer ring buf full"); + vTaskDelay(100); + } + } free(chnk->payload); free(chnk); @@ -548,6 +562,9 @@ static void snapcast_sync_task(void *pvParameters) { vTaskDelay( pdMS_TO_TICKS(10) ); } } + else { + ESP_LOGE(TAG, "Couldn't get PCM chunk"); + } } } @@ -789,6 +806,17 @@ static void http_get_task(void *pvParameters) { // TODO: maybe restart the whole thing if a new codec header is received while stream session is ongoing raw_stream_write(*p_raw_stream_writer, codec_header_message.payload, size); + +// printf("\r\n"); +// for (int i=0; ipayload); free(pcm_chunk_message); ret = AEL_IO_FAIL; } + else { + pcmchnkCnt++; + //if ((wirechnkCnt - pcmchnkCnt) > 3) + { + //ESP_LOGW(TAG, "diff: %lld", wirechnkCnt - pcmchnkCnt); + } + } } else { ESP_LOGW(TAG, "flac_decoder_write_cb: failed to get timestamp for it"); @@ -1204,16 +1262,20 @@ void app_main(void) { ESP_LOGI(TAG, "Create audio pipeline for decoding"); audio_pipeline_cfg_t flac_dec_pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG(); flacDecodePipeline = audio_pipeline_init(&flac_dec_pipeline_cfg); + flac_dec_pipeline_cfg.rb_size = 16 * 4096; // TODO: how much is really needed? AUDIO_NULL_CHECK(TAG, flacDecodePipeline, return); ESP_LOGI(TAG, "Create raw stream to write data from snapserver to decoder"); raw_stream_cfg_t raw_1_cfg = RAW_STREAM_CFG_DEFAULT(); raw_1_cfg.type = AUDIO_STREAM_WRITER; - raw_1_cfg.out_rb_size = 8 * 4096; // TODO: how much is really needed? + raw_1_cfg.out_rb_size = 16 * 4096; // TODO: how much is really needed? raw_stream_writer_to_decoder = raw_stream_init(&raw_1_cfg); ESP_LOGI(TAG, "Create flac decoder to decode flac file and set custom write callback"); flac_decoder_cfg_t flac_cfg = DEFAULT_FLAC_DECODER_CONFIG(); + flac_cfg.task_prio = FLAC_DECODER_PRIORITY; + flac_cfg.task_core = FLAC_DECODER_CORE_ID; + flac_cfg.out_rb_size = 16 * 4096; // TODO: how much is really needed? decoder = flac_decoder_init(&flac_cfg); audio_element_set_write_cb(decoder, flac_decoder_write_cb, NULL); @@ -1227,6 +1289,7 @@ void app_main(void) { ESP_LOGI(TAG, "Create audio pipeline for playback"); audio_pipeline_cfg_t playback_pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG(); + playback_pipeline_cfg.rb_size = 16 * 4096; // TODO: how much is really needed? playbackPipeline = audio_pipeline_init(&playback_pipeline_cfg); AUDIO_NULL_CHECK(TAG, playbackPipeline, return); @@ -1240,9 +1303,9 @@ void app_main(void) { i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT(); //i2s_cfg.task_stack = I2S_STREAM_TASK_STACK * 2; i2s_cfg.i2s_config.sample_rate = 48000; - //i2s_cfg.i2s_config.dma_buf_count = 8; - //i2s_cfg.i2s_config.dma_buf_len = 480; - //i2s_cfg.out_rb_size = 1024; + i2s_cfg.i2s_config.dma_buf_count = 4; + i2s_cfg.i2s_config.dma_buf_len = 410; + i2s_cfg.out_rb_size = 4 * 410; i2s_cfg.task_core = I2S_TASK_CORE_ID; //i2s_cfg.task_prio = I2S_TASK_PRIORITY; i2s_stream_writer = i2s_stream_init(&i2s_cfg); @@ -1324,8 +1387,8 @@ void app_main(void) { audio_element_getinfo(decoder, &music_info); if (codecString != NULL) { - ESP_LOGI(TAG, "[ * ] Receive music info from %s decoder, sample_rates=%d, bits=%d, ch=%d", - codecString, music_info.sample_rates, music_info.bits, music_info.channels); + ESP_LOGI(TAG, "[ * ] Receive music info from %s decoder, sample_rates=%d, bits=%d, ch=%d, total_bytes: %lld, byte_pos:%lld", + codecString, music_info.sample_rates, music_info.bits, music_info.channels, music_info.total_bytes, music_info.byte_pos); } else { ESP_LOGI(TAG, "[ * ] Receive music info from decoder, sample_rates=%d, bits=%d, ch=%d", diff --git a/sdkconfig b/sdkconfig index ce33cf0..56a80bc 100644 --- a/sdkconfig +++ b/sdkconfig @@ -121,8 +121,8 @@ CONFIG_PARTITION_TABLE_MD5=y # # Access Point configuration # -CONFIG_ESP_WIFI_SSID="" -CONFIG_ESP_WIFI_PASSWORD="" +CONFIG_ESP_WIFI_SSID="zuhause" +CONFIG_ESP_WIFI_PASSWORD="dErtischlEr" # end of Access Point configuration #