2021-07-04 21:24:51 +08:00
|
|
|
#include "clock.h"
|
|
|
|
|
2023-03-27 08:12:59 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
2021-07-14 04:32:05 +08:00
|
|
|
#include "util/log.h"
|
|
|
|
|
|
|
|
#define SC_CLOCK_NDEBUG // comment to debug
|
|
|
|
|
2023-03-27 08:12:59 +08:00
|
|
|
#define SC_CLOCK_RANGE 32
|
|
|
|
|
2021-07-04 21:24:51 +08:00
|
|
|
void
|
|
|
|
sc_clock_init(struct sc_clock *clock) {
|
2023-03-27 08:12:59 +08:00
|
|
|
clock->range = 0;
|
|
|
|
clock->offset = 0;
|
2021-07-04 21:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
|
2023-03-27 08:12:59 +08:00
|
|
|
if (clock->range < SC_CLOCK_RANGE) {
|
|
|
|
++clock->range;
|
2021-07-04 21:24:51 +08:00
|
|
|
}
|
|
|
|
|
2023-03-27 08:12:59 +08:00
|
|
|
sc_tick offset = system - stream;
|
|
|
|
clock->offset = ((clock->range - 1) * clock->offset + offset)
|
|
|
|
/ clock->range;
|
2021-07-14 04:32:05 +08:00
|
|
|
|
|
|
|
#ifndef SC_CLOCK_NDEBUG
|
2023-03-27 08:12:59 +08:00
|
|
|
LOGD("Clock estimation: pts + %" PRItick, clock->offset);
|
2021-07-14 04:32:05 +08:00
|
|
|
#endif
|
2021-07-04 21:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sc_tick
|
|
|
|
sc_clock_to_system_time(struct sc_clock *clock, sc_tick stream) {
|
2023-03-27 08:12:59 +08:00
|
|
|
assert(clock->range); // sc_clock_update() must have been called
|
|
|
|
return stream + clock->offset;
|
2021-07-04 21:24:51 +08:00
|
|
|
}
|