@@ -99,7 +99,7 @@ class SnapProcessor {
99
99
uint32_t client_state_muted = 0 ;
100
100
char *start = nullptr ;
101
101
int size = 0 ;
102
- struct timeval now,tdif ;
102
+ timeval now;
103
103
uint64_t last_time_sync = 0 ;
104
104
int id_counter = 0 ;
105
105
IPAddress server_ip;
@@ -108,6 +108,7 @@ class SnapProcessor {
108
108
bool output_start = true ;
109
109
bool http_task_start = true ;
110
110
bool header_received = false ;
111
+ bool is_time_set = false ;
111
112
SnapTime& snap_time = SnapTime::instance();
112
113
113
114
void processLoop (void *pvParameters = nullptr ) {
@@ -129,11 +130,7 @@ class SnapProcessor {
129
130
return true ;
130
131
}
131
132
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 ();
137
134
138
135
if (!writeHallo ())
139
136
return false ;
@@ -210,7 +207,7 @@ class SnapProcessor {
210
207
211
208
bool connectClient () {
212
209
ESP_LOGD (TAG, " start" );
213
- // p_client->setTimeout(CONFIG_CLIENT_TIMEOUT_SEC);
210
+ p_client->setTimeout (CONFIG_CLIENT_TIMEOUT_SEC);
214
211
if (!p_client->connect (server_ip, server_port)) {
215
212
ESP_LOGE (TAG, " ... socket connect failed errno=%d" , errno);
216
213
delay (4000 );
@@ -279,14 +276,11 @@ class SnapProcessor {
279
276
size = p_client->readBytes (&send_receive_buffer[0 ], BASE_MESSAGE_SIZE);
280
277
ESP_LOGD (TAG, " Bytes read: %d" , size);
281
278
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 ();
287
280
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);
290
284
if (result) {
291
285
ESP_LOGW (TAG, " Failed to read base message: %d" , result);
292
286
return false ;
@@ -327,7 +321,7 @@ class SnapProcessor {
327
321
size = codec_header_message.size ;
328
322
start = codec_header_message.payload ;
329
323
if (strcmp (codec_header_message.codec , " opus" ) == 0 ) {
330
- if (!processMessageCodecHeaderExt (OPUS))
324
+ if (!processMessageCodecHeaderOpus (OPUS))
331
325
return false ;
332
326
} else if (strcmp (codec_header_message.codec , " flac" ) == 0 ) {
333
327
if (!processMessageCodecHeaderExt (FLAC))
@@ -336,7 +330,7 @@ class SnapProcessor {
336
330
if (!processMessageCodecHeaderExt (VORBIS))
337
331
return false ;
338
332
} else if (strcmp (codec_header_message.codec , " pcm" ) == 0 ) {
339
- if (!processMessageCodecHeaderPCM ( ))
333
+ if (!processMessageCodecHeaderExt (PCM ))
340
334
return false ;
341
335
} else {
342
336
ESP_LOGI (TAG, " Codec : %s not supported" , codec_header_message.codec );
@@ -350,7 +344,7 @@ class SnapProcessor {
350
344
return true ;
351
345
}
352
346
353
- bool processMessageCodecHeaderExt (codec_type codecType) {
347
+ bool processMessageCodecHeaderOpus (codec_type codecType) {
354
348
ESP_LOGD (TAG, " start" );
355
349
uint32_t rate;
356
350
memcpy (&rate, start + 4 , sizeof (rate));
@@ -363,9 +357,9 @@ class SnapProcessor {
363
357
return true ;
364
358
}
365
359
366
- bool processMessageCodecHeaderPCM ( ) {
360
+ bool processMessageCodecHeaderExt (codec_type codecType ) {
367
361
ESP_LOGD (TAG, " start" );
368
- codec_from_server = PCM ;
362
+ codec_from_server = codecType ;
369
363
return true ;
370
364
}
371
365
@@ -428,6 +422,9 @@ class SnapProcessor {
428
422
ESP_LOGI (TAG, " Mute: %d" , server_settings_message.muted );
429
423
ESP_LOGI (TAG, " Setting volume: %d" , server_settings_message.volume );
430
424
425
+ // define the start delay
426
+ snap_time.setDelay (server_settings_message.buffer_ms );
427
+
431
428
// set volume
432
429
if (header_received){
433
430
setMute (server_settings_message.muted );
@@ -447,29 +444,34 @@ class SnapProcessor {
447
444
ESP_LOGI (TAG, " Failed to deserialize time message" );
448
445
return false ;
449
446
}
450
- // Calculate TClienctx.tdif : Trx-Tsend-Tnetdelay/2
447
+
448
+ // // Calculate TClienctx.tdif : Trx-Tsend-Tnetdelay/2
451
449
struct timeval ttx, trx;
452
450
ttx.tv_sec = base_message.sent .sec ;
453
451
ttx.tv_usec = base_message.sent .usec ;
454
452
trx.tv_sec = base_message.received .sec ;
455
453
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
+ }
460
468
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);
467
469
return true ;
468
470
}
469
471
470
472
bool writeTimedMessage () {
471
473
ESP_LOGD (TAG, " start" );
472
- int time_ms = millis () - last_time_sync;
474
+ uint32_t time_ms = millis () - last_time_sync;
473
475
if (time_ms >= 1000 ) {
474
476
last_time_sync = millis ();
475
477
if (!writeMessage ()) {
@@ -481,9 +483,8 @@ class SnapProcessor {
481
483
482
484
bool writeMessage () {
483
485
ESP_LOGD (TAG, " start" );
484
- if (gettimeofday (&now, NULL )!=0 ){
485
- ESP_LOGE (TAG, " gettimeofday" );
486
- }
486
+
487
+ now = snap_time.time ();
487
488
488
489
base_message.type = SNAPCAST_MESSAGE_TIME;
489
490
base_message.id = id_counter++;
0 commit comments