aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2017-04-23 19:22:15 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2017-08-14 19:34:31 +0200
commita409ae63e3d035f7f15da07755cd392bec53e6c6 (patch)
treed068a764c13d09040c2ba6ee77d49151b0f3dea7
parentb3c1f5e0ddc718c9014a7afbe3e33874d192a4f4 (diff)
jibuf: re-sync clock out of sync timestamps
-rw-r--r--src/jibuf.c28
-rw-r--r--tests/jibuf/jibuf_test.c17
2 files changed, 38 insertions, 7 deletions
diff --git a/src/jibuf.c b/src/jibuf.c
index 6376980..d5180ea 100644
--- a/src/jibuf.c
+++ b/src/jibuf.c
@@ -35,6 +35,7 @@
#define JIBUF_DEBUG 1
/* Sampling rate (in Hz) */
/* TODO: SAMPLE RATE can be guessed from rtp.p_type */
+#define SAMPLES_PER_PKT 160
#define SAMPLE_RATE 8000
@@ -156,6 +157,26 @@ static int _calc_pkt_rel_delay(struct jibuf *jb, struct msgb *msg)
return _samples2ms((current_tx_ts - jb->ref_tx_ts)) - (current_rx_ts - jb->ref_rx_ts);
}
+static bool _pkt_is_in_sequence(struct jibuf *jb, struct msgb *msg)
+{
+ uint32_t current_tx_ts = _msg_get_timestamp(msg);
+ uint16_t current_seq = _msg_get_sequence(msg);
+ return (current_tx_ts - jb->ref_tx_ts) == (current_seq - jb->ref_tx_seq)*SAMPLES_PER_PKT;
+}
+
+
+/* If packet contains a mark -> start of talkspurt.
+ * A lot of packets may have been suppressed by the sender before it,
+ * so let's take it as a reference
+ * If packet timestamp is not aligned with sequence
+ * number, then we are most probaly starting a talkspurt */
+static bool _pkt_is_syncpoint(struct jibuf *jb, struct msgb* msg)
+{
+ bool res = _msg_get_marker(msg) || !_pkt_is_in_sequence(jb, msg);
+ if(res)
+ LOGP(DLMIB, LOGL_DEBUG, "syncpoint: %"PRIu16": marker=%d in_seq=%d\n", _msg_get_sequence(msg), _msg_get_marker(msg), _pkt_is_in_sequence(jb, msg));
+ return res;
+}
static void _msg_set_as_reference(struct jibuf *jb, struct msgb *msg)
{
@@ -294,11 +315,8 @@ int osmo_jibuf_enqueue(struct jibuf *jb, struct msgb *msg)
clock_gettime_timeval(CLOCK_MONOTONIC, &jb->last_enqueue_time);
- /* If packet contains a mark -> start of talkspurt.
- * A lot of packets may have been suppressed by the sender before it,
- * so let's take it as a reference
- */
- if (!jb->started || _msg_get_marker(msg)) {
+ /* Check if it's time to sync, ie. start of talkspurt */
+ if (!jb->started || _pkt_is_syncpoint(jb, msg)) {
jb->started = true;
_msg_set_as_reference(jb, msg);
rel_delay = 0;
diff --git a/tests/jibuf/jibuf_test.c b/tests/jibuf/jibuf_test.c
index 7be9899..2d05cdf 100644
--- a/tests/jibuf/jibuf_test.c
+++ b/tests/jibuf/jibuf_test.c
@@ -35,6 +35,7 @@ struct checkpoint {
int transit;
double jitter;
uint32_t timestamp;
+ uint16_t seq;
};
struct rtp_pkt_info {
@@ -131,6 +132,16 @@ static uint32_t timeval2ms(const struct timeval *ts)
return ts->tv_sec * 1000 + ts->tv_usec / 1000;
}
+bool pkt_is_syncpoint(struct msgb* msg, uint16_t prev_seq, uint32_t prev_timestamp)
+{
+ struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg);
+
+ uint16_t current_seq = ntohs(rtph->sequence);
+ uint32_t current_tx_ts = ntohl(rtph->timestamp);
+ bool insync = (current_tx_ts - prev_timestamp) == (current_seq - prev_seq)*SAMPLES_PER_PKT;
+ return !insync || rtph->marker;
+}
+
int32_t calc_rel_transmit_time(uint32_t tx_0, uint32_t tx_f, uint32_t rx_0, uint32_t rx_f, bool tx_is_samples, bool pre)
{
int32_t rxdiff, txdiff, res;
@@ -220,7 +231,7 @@ void dequeue_cb(struct msgb *msg, void *data)
/* If pkt->marker -> init of talkspurt, there may be missing packets before,
* better to start calculating the jitter from here */
- if (postqueue_started && !rtph->marker) {
+ if (postqueue_started && !pkt_is_syncpoint(msg, postqueue_prev.seq, postqueue_prev.timestamp)) {
/* In random test mode we now the sender time, so we get real
* jitter results using it */
if(opt_test_rand) {
@@ -248,6 +259,7 @@ void dequeue_cb(struct msgb *msg, void *data)
}
postqueue_prev = pinfo->postqueue;
+ postqueue_prev.seq = htons(rtph->sequence);
pkt_add_result(msg, false);
@@ -273,7 +285,7 @@ void pkt_arrived_cb(void *data)
/* If pkt->marker -> init of talkspurt, there may be missing packets before,
* better to start calculating the jitter from here */
- if (prequeue_started && !rtph->marker) {
+ if (prequeue_started && !pkt_is_syncpoint(msg, prequeue_prev.seq, prequeue_prev.timestamp)) {
/* In random test mode we now the sender time, so we get real
* jitter results using it */
if(opt_test_rand) {
@@ -301,6 +313,7 @@ void pkt_arrived_cb(void *data)
}
prequeue_prev = pinfo->prequeue;
+ prequeue_prev.seq = htons(rtph->sequence);
int n = osmo_jibuf_enqueue(jb, msg);