Skip to content

Commit 4d051e0

Browse files
committed
SnapTimeSyncDynamicSinceStart
1 parent 31de58b commit 4d051e0

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

src/api/SnapTime.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include <sys/time.h>
66

77
/**
8-
* @brief We use the the sys/time functions to represent the server time.
8+
* @brief The the sys/time functions are used to represent the server time.
99
* The local time will be measured with the help of the Arduino millis() method.
10+
* This class provides the basic functionality to translate between local and server time.
1011
* @author Phil Schatzmann
1112
* @version 0.1
1213
* @date 2023-10-28
@@ -16,24 +17,9 @@ class SnapTime {
1617
public:
1718
static SnapTime &instance() {
1819
static SnapTime self;
19-
2020
return self;
2121
}
2222

23-
// Calculat the difference between 2 timeval
24-
timeval timeDifference(timeval t1, timeval t2) {
25-
timeval result;
26-
timersub(&t1, &t2, &result);
27-
return result;
28-
}
29-
30-
// Calculat the difference between 2 timeval -> result in ms
31-
uint32_t timeDifferenceMs(timeval t1, timeval t2) {
32-
timeval result;
33-
timersub(&t1, &t2, &result);
34-
return toMillis(result);
35-
}
36-
3723
/// Provides the actual time as timeval
3824
timeval time() {
3925
timeval result;
@@ -46,12 +32,6 @@ class SnapTime {
4632
return result;
4733
}
4834

49-
/// Record the last time difference between client and server
50-
void setTimeDifferenceClientServerMs(uint32_t diff) {
51-
time_diff = diff;
52-
time_update_count++;
53-
}
54-
5535
/// Provides the current server time in ms
5636
uint32_t serverMillis() {
5737
return toMillis(server_time) - server_ms + millis() -
@@ -83,12 +63,32 @@ class SnapTime {
8363
return true;
8464
}
8565

66+
/// Record the last time difference between client and server
67+
void setTimeDifferenceClientServerMs(uint32_t diff) {
68+
time_diff = diff;
69+
time_update_count++;
70+
}
71+
8672
// updates the time difference between the local and server time
8773
void updateServerTime(timeval &server) {
8874
server_ms = millis();
8975
server_time = server;
9076
}
9177

78+
// Calculat the difference between 2 timeval
79+
timeval timeDifference(timeval t1, timeval t2) {
80+
timeval result;
81+
timersub(&t1, &t2, &result);
82+
return result;
83+
}
84+
85+
// Calculat the difference between 2 timeval -> result in ms
86+
uint32_t timeDifferenceMs(timeval t1, timeval t2) {
87+
timeval result;
88+
timersub(&t1, &t2, &result);
89+
return toMillis(result);
90+
}
91+
9292
#ifdef ESP32
9393
void setupSNTPTime() {
9494
ESP_LOGD(TAG, "start");

src/api/SnapTimeSync.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include "SnapTime.h"
55

66
/**
7-
* @brief Abstract (Common) Time Synchronization Logic
7+
* @brief Abstract (Common) Time Synchronization Logic which consists of the
8+
* startup synchronization and the local to server clock synchronization which
9+
* adjusts the sampling rate.
810
* @author Phil Schatzmann
911
* @version 0.1
1012
* @date 2023-10-28
@@ -17,16 +19,19 @@ class SnapTimeSync {
1719
setProcessingLag(processingLag);
1820
}
1921

22+
/// Starts the processing
2023
void begin(int rate) {
2124
update_count = 0;
2225
}
2326

27+
/// Records the actual server time in millisecondes
2428
virtual void updateServerTime(uint32_t serverMillis) = 0;
2529

2630
/// Calculate the resampling factor: with a positive delay we play too fast
2731
/// and need to slow down
2832
virtual float getFactor() = 0;
2933

34+
/// Returns true if a synchronization (update of the sampling rate) is needed.
3035
bool isSync() {
3136
bool result = active && update_count > 2 && update_count % interval == 0;
3237
active = false;
@@ -96,7 +101,10 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
96101
if (last_idx <=1) return 1.0;
97102
float timespan_local_ms = time_points[last_idx].local_ms - time_points[0].local_ms;
98103
float timespan_server_ms = time_points[last_idx].server_ms - time_points[0].server_ms;
99-
if (timespan_local_ms == 0) return 1.0;
104+
if (timespan_local_ms == 0.0 || timespan_server_ms == 0.0) {
105+
ESP_LOGE(TAG, "Could not determine clock differences");
106+
return 1.0;
107+
}
100108
// if server time span is smaller then local, local runs faster and needs to be slowed down
101109
float result_factor = timespan_server_ms / timespan_local_ms;
102110
ESP_LOGI(TAG, "=> adjusting playback speed by factor: %f", result_factor);
@@ -130,18 +138,18 @@ class SnapTimeSyncDynamicSinceStart : public SnapTimeSync {
130138
}
131139

132140
float getFactor() {
133-
int last_idx = time_points.size()-1;
134-
if (last_idx <=1) return 1.0;
135141
float timespan_local_ms = current_time.local_ms - start_time.local_ms;
136142
float timespan_server_ms = current_time.server_ms - start_time.server_ms;
137-
if (timespan_local_ms == 0) return 1.0;
143+
if (timespan_local_ms == 0.0 || timespan_server_ms == 0.0) {
144+
ESP_LOGE(TAG, "Could not determine clock differences");
145+
return 1.0;
146+
}
138147
// if server time span is smaller then local, local runs faster and needs to be slowed down
139148
float result_factor = timespan_server_ms / timespan_local_ms;
140149
ESP_LOGI(TAG, "=> adjusting playback speed by factor: %f", result_factor);
141150
return result_factor;
142151
}
143152
protected:
144-
Vector<SnapTimePoints> time_points;
145153
SnapTimePoints start_time;
146154
SnapTimePoints current_time;
147155
};

0 commit comments

Comments
 (0)