@@ -99,7 +99,7 @@ class SnapProcessor {
9999 uint32_t client_state_muted = 0 ;
100100 char *start = nullptr ;
101101 int size = 0 ;
102- struct timeval now,tdif ;
102+ timeval now;
103103 uint64_t last_time_sync = 0 ;
104104 int id_counter = 0 ;
105105 IPAddress server_ip;
@@ -108,6 +108,7 @@ class SnapProcessor {
108108 bool output_start = true ;
109109 bool http_task_start = true ;
110110 bool header_received = false ;
111+ bool is_time_set = false ;
111112 SnapTime& snap_time = SnapTime::instance();
112113
113114 void processLoop (void *pvParameters = nullptr ) {
@@ -129,11 +130,7 @@ class SnapProcessor {
129130 return true ;
130131 }
131132
132- int result = gettimeofday (&now, NULL );
133- if (result) {
134- ESP_LOGI (TAG, " Failed to gettimeofday" );
135- return false ;
136- }
133+ now = snap_time.time ();
137134
138135 if (!writeHallo ())
139136 return false ;
@@ -210,7 +207,7 @@ class SnapProcessor {
210207
211208 bool connectClient () {
212209 ESP_LOGD (TAG, " start" );
213- // p_client->setTimeout(CONFIG_CLIENT_TIMEOUT_SEC);
210+ p_client->setTimeout (CONFIG_CLIENT_TIMEOUT_SEC);
214211 if (!p_client->connect (server_ip, server_port)) {
215212 ESP_LOGE (TAG, " ... socket connect failed errno=%d" , errno);
216213 delay (4000 );
@@ -279,14 +276,11 @@ class SnapProcessor {
279276 size = p_client->readBytes (&send_receive_buffer[0 ], BASE_MESSAGE_SIZE);
280277 ESP_LOGD (TAG, " Bytes read: %d" , size);
281278
282- int result = gettimeofday (&now, NULL );
283- if (result) {
284- ESP_LOGW (TAG, " Failed to gettimeofday" );
285- return false ;
286- }
279+ now = snap_time.time ();
287280
288- result =
289- base_message_deserialize (&base_message, &send_receive_buffer[0 ], size);
281+ int result = base_message_deserialize (&base_message,
282+ &send_receive_buffer[0 ],
283+ size);
290284 if (result) {
291285 ESP_LOGW (TAG, " Failed to read base message: %d" , result);
292286 return false ;
@@ -327,7 +321,7 @@ class SnapProcessor {
327321 size = codec_header_message.size ;
328322 start = codec_header_message.payload ;
329323 if (strcmp (codec_header_message.codec , " opus" ) == 0 ) {
330- if (!processMessageCodecHeaderExt (OPUS))
324+ if (!processMessageCodecHeaderOpus (OPUS))
331325 return false ;
332326 } else if (strcmp (codec_header_message.codec , " flac" ) == 0 ) {
333327 if (!processMessageCodecHeaderExt (FLAC))
@@ -336,7 +330,7 @@ class SnapProcessor {
336330 if (!processMessageCodecHeaderExt (VORBIS))
337331 return false ;
338332 } else if (strcmp (codec_header_message.codec , " pcm" ) == 0 ) {
339- if (!processMessageCodecHeaderPCM ( ))
333+ if (!processMessageCodecHeaderExt (PCM ))
340334 return false ;
341335 } else {
342336 ESP_LOGI (TAG, " Codec : %s not supported" , codec_header_message.codec );
@@ -350,7 +344,7 @@ class SnapProcessor {
350344 return true ;
351345 }
352346
353- bool processMessageCodecHeaderExt (codec_type codecType) {
347+ bool processMessageCodecHeaderOpus (codec_type codecType) {
354348 ESP_LOGD (TAG, " start" );
355349 uint32_t rate;
356350 memcpy (&rate, start + 4 , sizeof (rate));
@@ -363,9 +357,9 @@ class SnapProcessor {
363357 return true ;
364358 }
365359
366- bool processMessageCodecHeaderPCM ( ) {
360+ bool processMessageCodecHeaderExt (codec_type codecType ) {
367361 ESP_LOGD (TAG, " start" );
368- codec_from_server = PCM ;
362+ codec_from_server = codecType ;
369363 return true ;
370364 }
371365
@@ -428,6 +422,9 @@ class SnapProcessor {
428422 ESP_LOGI (TAG, " Mute: %d" , server_settings_message.muted );
429423 ESP_LOGI (TAG, " Setting volume: %d" , server_settings_message.volume );
430424
425+ // define the start delay
426+ snap_time.setDelay (server_settings_message.buffer_ms );
427+
431428 // set volume
432429 if (header_received){
433430 setMute (server_settings_message.muted );
@@ -447,29 +444,34 @@ class SnapProcessor {
447444 ESP_LOGI (TAG, " Failed to deserialize time message" );
448445 return false ;
449446 }
450- // Calculate TClienctx.tdif : Trx-Tsend-Tnetdelay/2
447+
448+ // // Calculate TClienctx.tdif : Trx-Tsend-Tnetdelay/2
451449 struct timeval ttx, trx;
452450 ttx.tv_sec = base_message.sent .sec ;
453451 ttx.tv_usec = base_message.sent .usec ;
454452 trx.tv_sec = base_message.received .sec ;
455453 trx.tv_usec = base_message.received .usec ;
456- snap_time.setTime (trx, ttx);
457-
458- timersub (&trx, &ttx, &tdif);
459- uint32_t usec = tdif.tv_usec ;
454+
455+ snap_time.updateServerTime (trx);
456+ if (!is_time_set){
457+ // set time from server
458+ snap_time.setTime (trx);
459+ last_time_sync = 0 ;
460+ is_time_set = true ;
461+ } else {
462+ int64_t time_diff = snap_time.timeDifferenceMs (trx, ttx);
463+ int time_diff_int = time_diff;
464+ assert (time_diff_int==time_diff);
465+ ESP_LOGI (TAG, " Time Difference to Server: %ld ms" , time_diff);
466+ snap_time.setTimeDifferenceClientServerMs (time_diff);
467+ }
460468
461- float time_diff = time_message.latency .usec / 1000 +
462- base_message.received .usec / 1000 -
463- base_message.sent .usec / 1000 ;
464- time_diff = (time_diff > 1000 ) ? time_diff - 1000 : time_diff;
465- ESP_LOGI (TAG, " TM loopback latency: %03.1f ms" , time_diff);
466- snap_time.addLatency (time_diff);
467469 return true ;
468470 }
469471
470472 bool writeTimedMessage () {
471473 ESP_LOGD (TAG, " start" );
472- int time_ms = millis () - last_time_sync;
474+ uint32_t time_ms = millis () - last_time_sync;
473475 if (time_ms >= 1000 ) {
474476 last_time_sync = millis ();
475477 if (!writeMessage ()) {
@@ -481,9 +483,8 @@ class SnapProcessor {
481483
482484 bool writeMessage () {
483485 ESP_LOGD (TAG, " start" );
484- if (gettimeofday (&now, NULL )!=0 ){
485- ESP_LOGE (TAG, " gettimeofday" );
486- }
486+
487+ now = snap_time.time ();
487488
488489 base_message.type = SNAPCAST_MESSAGE_TIME;
489490 base_message.id = id_counter++;
0 commit comments