Romain Vimont 2021-07-05 23:08:16 +02:00
commit 01af228dd5
2 changed files with 53 additions and 25 deletions

View file

@ -12,17 +12,23 @@ static void
sc_clock_history_init(struct sc_clock_history *history) { sc_clock_history_init(struct sc_clock_history *history) {
history->count = 0; history->count = 0;
history->head = 0; history->head = 0;
history->system_sum = 0; history->system_left_sum = 0;
history->stream_sum = 0; history->stream_left_sum = 0;
history->system_right_sum = 0;
history->stream_right_sum = 0;
memset(history->points, 0, sizeof(history->points));
} }
static void static void
sc_clock_history_push(struct sc_clock_history *history, sc_clock_history_push(struct sc_clock_history *history,
sc_tick system, sc_tick stream) { sc_tick system, sc_tick stream) {
struct sc_clock_point *point = &history->points[history->head]; struct sc_clock_point *point = &history->points[history->head];
struct sc_clock_point *mid_point = &history->points[(history->head + SC_CLOCK_RANGE/2) % SC_CLOCK_RANGE];
history->system_left_sum += mid_point->system - point->system;
history->stream_left_sum += mid_point->stream - point->stream;
history->system_right_sum -= mid_point->system;
history->stream_right_sum -= mid_point->stream;
if (history->count == SC_CLOCK_RANGE) { if (history->count == SC_CLOCK_RANGE) {
history->system_sum -= point->system;
history->stream_sum -= point->stream;
} else { } else {
++history->count; ++history->count;
} }
@ -30,22 +36,40 @@ sc_clock_history_push(struct sc_clock_history *history,
point->system = system; point->system = system;
point->stream = stream; point->stream = stream;
history->system_sum += system; history->system_right_sum += system;
history->stream_sum += stream; history->stream_right_sum += stream;
LOGD("#### %ld %ld\n", history->stream_sum / history->count, history->system_sum / history->count);
history->head = (history->head + 1) % SC_CLOCK_RANGE; history->head = (history->head + 1) % SC_CLOCK_RANGE;
} }
static void static void
sc_clock_history_get_average_point(struct sc_clock_history *history, sc_clock_history_get_eq(struct sc_clock_history *history, double *coeff,
struct sc_clock_point *point) { sc_tick *offset) {
assert(history->count); struct sc_clock_point p1 = {
point->system = history->system_sum / history->count; .system = history->system_left_sum * 2 / SC_CLOCK_RANGE,
point->stream = history->stream_sum / history->count; .stream = history->stream_left_sum * 2 / SC_CLOCK_RANGE,
};
struct sc_clock_point p2 = {
.system = history->system_right_sum * 2 / SC_CLOCK_RANGE,
.stream = history->stream_right_sum * 2 / SC_CLOCK_RANGE,
};
double a = (double) (p2.system - p1.system) / (p2.stream - p1.stream);
fprintf(stderr, "%ld %ld\n", history->system_left_sum, history->system_right_sum);
sc_tick b = (history->system_left_sum + history->system_right_sum) / SC_CLOCK_RANGE
- (sc_tick) ((history->stream_left_sum + history->stream_right_sum) / SC_CLOCK_RANGE * a);
*coeff = a;
*offset = b;
} }
//static void
//sc_clock_history_get_average_point(struct sc_clock_history *history,
// struct sc_clock_point *point) {
// assert(history->count);
// point->system = history->system_sum / history->count;
// point->stream = history->stream_sum / history->count;
//}
static void static void
sc_clock_init(struct sc_clock *clock) { sc_clock_init(struct sc_clock *clock) {
clock->coeff = 1; clock->coeff = 1;
@ -82,15 +106,16 @@ sc_clock_update(struct sc_clock *clock, sc_tick now, sc_tick stream_ts) {
++clock->weight; ++clock->weight;
} }
// (1-t) * avg + t * new sc_clock_history_get_eq(&clock->history, &clock->coeff, &clock->offset);
clock->coeff = ((clock->weight - 1) * clock->coeff + instant_coeff) // // (1-t) * avg + t * new
/ clock->weight; // clock->coeff = ((clock->weight - 1) * clock->coeff + instant_coeff)
// / clock->weight;
struct sc_clock_point center; //
sc_clock_history_get_average_point(&clock->history, &center); // struct sc_clock_point center;
// sc_clock_history_get_average_point(&clock->history, &center);
clock->offset = center.system - (sc_tick) (center.stream * clock->coeff); //
// clock->offset = center.system - (sc_tick) (center.stream * clock->coeff);
//
LOGD("%g x + %ld", clock->coeff, clock->offset); LOGD("%g x + %ld", clock->coeff, clock->offset);
clock->last.system = now; clock->last.system = now;
@ -160,7 +185,7 @@ run_buffering(void *data) {
sc_queue_take(&vb->b.queue, next, &vb_frame); sc_queue_take(&vb->b.queue, next, &vb_frame);
sc_tick now = sc_tick_now(); sc_tick now = sc_tick_now();
int64_t pts = vb_frame->frame->pts / 1000; int64_t pts = vb_frame->frame->pts;
LOGD("==== pts = %ld", pts); LOGD("==== pts = %ld", pts);
bool timed_out = false; bool timed_out = false;

View file

@ -32,8 +32,11 @@ struct sc_clock_history {
unsigned count; unsigned count;
unsigned head; unsigned head;
sc_tick system_sum; sc_tick system_left_sum;
sc_tick stream_sum; sc_tick stream_left_sum;
sc_tick system_right_sum;
sc_tick stream_right_sum;
}; };
struct sc_clock { struct sc_clock {