aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2017-06-09 14:04:31 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2017-08-14 19:34:31 +0200
commit89180efea12f2f748517156bc5ee13c01d104524 (patch)
tree8fe26d74013a6de657f3d7515fe315f93ba06c44
parent76e852d2a6217c49849deb1a5f9cb8402e509ee5 (diff)
jibuf: Estimate src clock skew
-rw-r--r--include/osmocom/netif/jibuf.h2
-rw-r--r--src/jibuf.c12
-rw-r--r--tests/jibuf/jibuf_test.c13
-rw-r--r--tests/jibuf/jitter.plt2
4 files changed, 24 insertions, 5 deletions
diff --git a/include/osmocom/netif/jibuf.h b/include/osmocom/netif/jibuf.h
index ffa1f33..9a321ef 100644
--- a/include/osmocom/netif/jibuf.h
+++ b/include/osmocom/netif/jibuf.h
@@ -40,6 +40,8 @@ struct jibuf {
struct timeval last_enqueue_time;
struct timeval next_dequeue_time;
+ int32_t skew; /* src clock skew, in msec */
+
struct {
uint32_t total_enqueued;
uint64_t total_dropped;
diff --git a/src/jibuf.c b/src/jibuf.c
index d5180ea..0b38297 100644
--- a/src/jibuf.c
+++ b/src/jibuf.c
@@ -60,6 +60,9 @@
#define JIBUF_BUFFER_INC_STEP 20
#define JIBUF_BUFFER_DEC_STEP 5
+/* weight of each new packet in calculation of clock skew */
+#define JIBUF_SKEW_WEIGHT ((double)1/32)
+
struct jibuf_msgb_cb {
struct timeval ts;
@@ -183,6 +186,7 @@ static void _msg_set_as_reference(struct jibuf *jb, struct msgb *msg)
jb->ref_rx_ts = _timeval2ms(&jb->last_enqueue_time);
jb->ref_tx_ts = _msg_get_timestamp(msg);
jb->ref_tx_seq = _msg_get_sequence(msg);
+ jb->skew = 0;
# if JIBUF_DEBUG
LOGP(DLMIB, LOGL_DEBUG, "New reference (seq=%"PRIu16" rx=%"PRIu32 \
@@ -226,6 +230,11 @@ static void _osmo_jibuf_timer_expired(void *data)
}
+static void _recalc_clock_skew(struct jibuf *jb, int32_t rel_delay)
+{
+ jb->skew = (int32_t) (rel_delay * JIBUF_SKEW_WEIGHT + jb->skew * (1.0 - JIBUF_SKEW_WEIGHT));
+}
+
static void _recalc_threshold_delay(struct jibuf *jb)
{
@@ -322,6 +331,7 @@ int osmo_jibuf_enqueue(struct jibuf *jb, struct msgb *msg)
rel_delay = 0;
} else {
rel_delay = _calc_pkt_rel_delay(jb, msg);
+ _recalc_clock_skew(jb, rel_delay);
}
/* Avoid time skew with sender (or drop-everything state),
@@ -329,7 +339,7 @@ int osmo_jibuf_enqueue(struct jibuf *jb, struct msgb *msg)
//if ((int)(_msg_get_sequence(msg) - jb->ref_tx_seq) > JIBUF_REFERENCE_TS_FREQ)
// _msg_set_as_reference(jb, msg);
- delay = jb->threshold_delay + rel_delay;
+ delay = jb->threshold_delay + rel_delay - jb->skew;
/* packet too late, let's drop it and incr buffer size if encouraged */
if (delay < 0) {
diff --git a/tests/jibuf/jibuf_test.c b/tests/jibuf/jibuf_test.c
index 872b98c..b77ae68 100644
--- a/tests/jibuf/jibuf_test.c
+++ b/tests/jibuf/jibuf_test.c
@@ -43,6 +43,7 @@ struct rtp_pkt_info {
struct timeval tx_prev_time;
struct timeval tx_time;
uint32_t tx_delay;
+ int32_t skew;
struct checkpoint prequeue;
struct checkpoint postqueue;
};
@@ -167,12 +168,12 @@ void trace_pkt(struct msgb *msg, char* info) {
if (opt_debug_human) {
uint32_t total_delay_ms = timeval2ms(&total_delay);
LOGP(DJBT, LOGL_DEBUG, "%s: seq=%"PRIu16" ts=%"PRIu32" (%ld.%06ld) tx_delay=%"PRIu32 \
- " end_delay=%"PRIu32" pre_trans=%d pre_jitter=%f post_trans=%d post_jitter=%f\n",
+ " end_delay=%"PRIu32" pre_trans=%d pre_jitter=%f post_trans=%d post_jitter=%f skew=%"PRId32"\n",
info, ntohs(rtph->sequence), ntohl(rtph->timestamp),
pinfo->tx_time.tv_sec, pinfo->tx_time.tv_usec,
pinfo->tx_delay, total_delay_ms,
pinfo->prequeue.transit, pinfo->prequeue.jitter,
- pinfo->postqueue.transit, pinfo->postqueue.jitter);
+ pinfo->postqueue.transit, pinfo->postqueue.jitter, pinfo->skew);
if (pinfo->prequeue.jitter < pinfo->postqueue.jitter)
LOGP(DJBT, LOGL_ERROR, "JITTER HIGHER THAN REF: seq=%"PRIu16" ts=%"PRIu32 \
@@ -192,12 +193,12 @@ void trace_pkt(struct msgb *msg, char* info) {
uint32_t tx_time_ms = timeval2ms(&pinfo->tx_time);
uint32_t prequeue_time_ms = timeval2ms(&pinfo->prequeue.ts);
uint32_t postqueue_time_ms = timeval2ms(&pinfo->postqueue.ts);
- fprintf(stderr, "%"PRIu16"\t%"PRIu32"\t%"PRIu32"\t%"PRIu32"\t%d\t%d\t%f\t%f\t%"PRIu32"\t%"PRIu32"\n",
+ fprintf(stderr, "%"PRIu16"\t%"PRIu32"\t%"PRIu32"\t%"PRIu32"\t%d\t%d\t%f\t%f\t%"PRIu32"\t%"PRIu32"\t%"PRId32"\n",
ntohs(rtph->sequence), tx_time_ms,
prequeue_time_ms, postqueue_time_ms,
pinfo->prequeue.transit, pinfo->postqueue.transit,
pinfo->prequeue.jitter, pinfo->postqueue.jitter,
- packets_dropped, jb->threshold_delay);
+ packets_dropped, jb->threshold_delay, jb->skew);
}
}
@@ -319,6 +320,10 @@ void pkt_arrived_cb(void *data)
int n = osmo_jibuf_enqueue(jb, msg);
+ /* skew has been updated with this new packet. We pick the updated one
+ * as it's the one applied to this packet. */
+ pinfo->skew = jb->skew;
+
if (n<0) {
pkt_add_result(msg, true);
osmo_timer_del(&pinfo->timer);
diff --git a/tests/jibuf/jitter.plt b/tests/jibuf/jitter.plt
index ef96ec7..21edf72 100644
--- a/tests/jibuf/jitter.plt
+++ b/tests/jibuf/jitter.plt
@@ -33,6 +33,7 @@ if (pcap) {
'/tmp/bla' using 4:8 with linespoints title 'post.jitter' axes x1y1, \
'/tmp/bla' using 3:9 with linespoints title 'pre.dropped' axes x1y1, \
'/tmp/bla' using 3:10 with linespoints title 'pre.buffer' axes x1y1, \
+ '/tmp/bla' using 3:11 with linespoints title 'pre.skew' axes x1y1, \
'/tmp/bla' using 4:((column(4)-column(2))) with linespoints title 'post.delay' axes x1y1
} else {
@@ -43,6 +44,7 @@ if (pcap) {
'/tmp/bla' using 4:8 with linespoints title 'post.jitter' axes x1y1, \
'/tmp/bla' using 3:9 with linespoints title 'pre.dropped' axes x1y1, \
'/tmp/bla' using 3:10 with linespoints title 'pre.buffer' axes x1y1, \
+ '/tmp/bla' using 3:11 with linespoints title 'pre.skew' axes x1y1, \
'/tmp/bla' using 3:((column(3)-column(2))) with linespoints title 'pre.delay' axes x1y1, \
'/tmp/bla' using 4:((column(4)-column(2))) with linespoints title 'post.delay' axes x1y1
}