|
|
|
|
@@ -8,67 +8,7 @@
|
|
|
|
|
|
|
|
|
|
#include "util/log.h"
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sc_clock_history_init(struct sc_clock_history *history) {
|
|
|
|
|
history->count = 0;
|
|
|
|
|
history->head = 0;
|
|
|
|
|
history->system_left_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
|
|
|
|
|
sc_clock_history_push(struct sc_clock_history *history,
|
|
|
|
|
sc_tick system, sc_tick stream) {
|
|
|
|
|
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) {
|
|
|
|
|
} else {
|
|
|
|
|
++history->count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
point->system = system;
|
|
|
|
|
point->stream = stream;
|
|
|
|
|
|
|
|
|
|
history->system_right_sum += system;
|
|
|
|
|
history->stream_right_sum += stream;
|
|
|
|
|
|
|
|
|
|
history->head = (history->head + 1) % SC_CLOCK_RANGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sc_clock_history_get_eq(struct sc_clock_history *history, double *coeff,
|
|
|
|
|
sc_tick *offset) {
|
|
|
|
|
struct sc_clock_point p1 = {
|
|
|
|
|
.system = history->system_left_sum * 2 / SC_CLOCK_RANGE,
|
|
|
|
|
.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;
|
|
|
|
|
//}
|
|
|
|
|
#define SC_CLOCK_AVERAGE_RANGE 32
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sc_clock_init(struct sc_clock *clock) {
|
|
|
|
|
@@ -78,8 +18,6 @@ sc_clock_init(struct sc_clock *clock) {
|
|
|
|
|
|
|
|
|
|
clock->last.system = 0;
|
|
|
|
|
clock->last.stream = 0;
|
|
|
|
|
|
|
|
|
|
sc_clock_history_init(&clock->history);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sc_tick
|
|
|
|
|
@@ -91,33 +29,34 @@ sc_clock_to_system_ts(struct sc_clock *clock, sc_tick stream_ts) {
|
|
|
|
|
static void
|
|
|
|
|
sc_clock_update(struct sc_clock *clock, sc_tick now, sc_tick stream_ts) {
|
|
|
|
|
double instant_coeff;
|
|
|
|
|
sc_tick mad;
|
|
|
|
|
if (clock->weight) {
|
|
|
|
|
sc_tick system_delta = now - clock->last.system;
|
|
|
|
|
sc_tick stream_delta = stream_ts - clock->last.stream;
|
|
|
|
|
instant_coeff = (double) system_delta / stream_delta;
|
|
|
|
|
sc_tick system_pts = sc_clock_to_system_ts(clock, stream_ts);
|
|
|
|
|
mad = llabs(now - system_pts);
|
|
|
|
|
} else {
|
|
|
|
|
// This is the first update, we cannot compute delta
|
|
|
|
|
instant_coeff = 1;
|
|
|
|
|
mad = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sc_clock_history_push(&clock->history, now, stream_ts);
|
|
|
|
|
|
|
|
|
|
if (clock->weight < SC_CLOCK_RANGE) {
|
|
|
|
|
if (clock->weight < SC_CLOCK_AVERAGE_RANGE) {
|
|
|
|
|
++clock->weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sc_clock_history_get_eq(&clock->history, &clock->coeff, &clock->offset);
|
|
|
|
|
// // (1-t) * avg + t * new
|
|
|
|
|
// clock->coeff = ((clock->weight - 1) * clock->coeff + instant_coeff)
|
|
|
|
|
// / clock->weight;
|
|
|
|
|
//
|
|
|
|
|
// struct sc_clock_point center;
|
|
|
|
|
// sc_clock_history_get_average_point(&clock->history, ¢er);
|
|
|
|
|
//
|
|
|
|
|
// clock->offset = center.system - (sc_tick) (center.stream * clock->coeff);
|
|
|
|
|
//
|
|
|
|
|
// (1-t) * avg + t * new
|
|
|
|
|
clock->coeff = ((clock->weight - 1) * clock->coeff + instant_coeff)
|
|
|
|
|
/ clock->weight;
|
|
|
|
|
|
|
|
|
|
// FIXME it cannot change at every frame!
|
|
|
|
|
clock->offset = now - (sc_tick) (stream_ts * clock->coeff);
|
|
|
|
|
|
|
|
|
|
LOGD("%g x + %ld", clock->coeff, clock->offset);
|
|
|
|
|
|
|
|
|
|
clock->mad = ((clock->weight - 1) * clock->mad + mad) / clock->weight;
|
|
|
|
|
|
|
|
|
|
clock->last.system = now;
|
|
|
|
|
clock->last.stream = stream_ts;
|
|
|
|
|
}
|
|
|
|
|
@@ -185,17 +124,21 @@ run_buffering(void *data) {
|
|
|
|
|
sc_queue_take(&vb->b.queue, next, &vb_frame);
|
|
|
|
|
|
|
|
|
|
sc_tick now = sc_tick_now();
|
|
|
|
|
int64_t pts = vb_frame->frame->pts;
|
|
|
|
|
// FIXME time_base units
|
|
|
|
|
int64_t pts = vb_frame->frame->pts; // micros to millis
|
|
|
|
|
LOGD("==== pts = %ld", pts);
|
|
|
|
|
sc_tick system_pts = sc_clock_to_system_ts(&vb->b.clock, pts);
|
|
|
|
|
if (now + vb->buffering_ms < system_pts) {
|
|
|
|
|
system_pts = now + vb->buffering_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sc_tick deadline = system_pts + vb->buffering_ms;
|
|
|
|
|
|
|
|
|
|
LOGD("==== %ld %ld %ld\n", now, system_pts, deadline);
|
|
|
|
|
|
|
|
|
|
LOGD("[WAITING FOR] %ld ", deadline-now);
|
|
|
|
|
bool timed_out = false;
|
|
|
|
|
while (!vb->b.stopped && !timed_out) {
|
|
|
|
|
sc_tick deadline = sc_clock_to_system_ts(&vb->b.clock, pts)
|
|
|
|
|
+ vb->buffering_ms;
|
|
|
|
|
if (deadline > now + vb->buffering_ms) {
|
|
|
|
|
deadline = now + vb->buffering_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timed_out =
|
|
|
|
|
!sc_cond_timedwait(&vb->b.wait_cond, &vb->b.mutex, deadline);
|
|
|
|
|
}
|
|
|
|
|
@@ -333,7 +276,6 @@ sc_video_buffer_push(struct sc_video_buffer *vb, const AVFrame *frame) {
|
|
|
|
|
sc_mutex_lock(&vb->b.mutex);
|
|
|
|
|
sc_queue_push(&vb->b.queue, next, vb_frame);
|
|
|
|
|
sc_cond_signal(&vb->b.queue_cond);
|
|
|
|
|
sc_cond_signal(&vb->b.wait_cond);
|
|
|
|
|
sc_mutex_unlock(&vb->b.mutex);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|