2021-07-04 15:24:51 +02:00
|
|
|
#include "clock.h"
|
|
|
|
|
|
2023-03-27 02:12:59 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2021-07-13 22:32:05 +02:00
|
|
|
#include "util/log.h"
|
|
|
|
|
|
2024-09-11 11:29:00 +02:00
|
|
|
//#define SC_CLOCK_DEBUG // uncomment to debug
|
2021-07-13 22:32:05 +02:00
|
|
|
|
2023-03-27 02:12:59 +02:00
|
|
|
#define SC_CLOCK_RANGE 32
|
|
|
|
|
|
2021-07-04 15:24:51 +02:00
|
|
|
void
|
|
|
|
|
sc_clock_init(struct sc_clock *clock) {
|
2023-03-27 02:12:59 +02:00
|
|
|
clock->range = 0;
|
|
|
|
|
clock->offset = 0;
|
2021-07-04 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
|
2023-03-27 02:12:59 +02:00
|
|
|
if (clock->range < SC_CLOCK_RANGE) {
|
|
|
|
|
++clock->range;
|
2021-07-04 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 02:12:59 +02:00
|
|
|
sc_tick offset = system - stream;
|
2024-09-12 10:53:44 +02:00
|
|
|
unsigned clock_weight = clock->range - 1;
|
|
|
|
|
unsigned value_weight = SC_CLOCK_RANGE - clock->range + 1;
|
|
|
|
|
clock->offset = (clock->offset * clock_weight + offset * value_weight)
|
|
|
|
|
/ SC_CLOCK_RANGE;
|
2021-07-13 22:32:05 +02:00
|
|
|
|
2024-09-11 11:29:00 +02:00
|
|
|
#ifdef SC_CLOCK_DEBUG
|
2023-03-27 02:12:59 +02:00
|
|
|
LOGD("Clock estimation: pts + %" PRItick, clock->offset);
|
2021-07-13 22:32:05 +02:00
|
|
|
#endif
|
2021-07-04 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sc_tick
|
|
|
|
|
sc_clock_to_system_time(struct sc_clock *clock, sc_tick stream) {
|
2023-03-27 02:12:59 +02:00
|
|
|
assert(clock->range); // sc_clock_update() must have been called
|
|
|
|
|
return stream + clock->offset;
|
2021-07-04 15:24:51 +02:00
|
|
|
}
|