2f9396e24a
The slope encodes the drift between the device clock and the computer clock. Its real value is expected very close to 1. To estimate it, just assume it is exactly 1. Since the clock is used to estimate very close points in the future, the error caused by clock drift is totally negligible, and in practice it is way lower than the slope estimation error. Therefore, only estimate the offset.
36 lines
795 B
C
36 lines
795 B
C
#include "clock.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "util/log.h"
|
|
|
|
#define SC_CLOCK_NDEBUG // comment to debug
|
|
|
|
#define SC_CLOCK_RANGE 32
|
|
|
|
void
|
|
sc_clock_init(struct sc_clock *clock) {
|
|
clock->range = 0;
|
|
clock->offset = 0;
|
|
}
|
|
|
|
void
|
|
sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
|
|
if (clock->range < SC_CLOCK_RANGE) {
|
|
++clock->range;
|
|
}
|
|
|
|
sc_tick offset = system - stream;
|
|
clock->offset = ((clock->range - 1) * clock->offset + offset)
|
|
/ clock->range;
|
|
|
|
#ifndef SC_CLOCK_NDEBUG
|
|
LOGD("Clock estimation: pts + %" PRItick, clock->offset);
|
|
#endif
|
|
}
|
|
|
|
sc_tick
|
|
sc_clock_to_system_time(struct sc_clock *clock, sc_tick stream) {
|
|
assert(clock->range); // sc_clock_update() must have been called
|
|
return stream + clock->offset;
|
|
}
|