aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2017-04-12 19:36:47 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2017-08-14 19:30:15 +0200
commit7652418623942f16d6bb6ee6f8133536daa308d6 (patch)
tree46e02a9ce406e5dcc7d6dfe88637bc2e35990ab9
parente78605556770c19c7a2e52fbc67c1a89718ba9de (diff)
jibuf: Add initial implementation of Jitter Buffer
-rw-r--r--include/osmocom/netif/Makefile.am1
-rw-r--r--include/osmocom/netif/jibuf.h63
-rw-r--r--src/Makefile.am1
-rw-r--r--src/jibuf.c386
-rw-r--r--tests/Makefile.am8
-rw-r--r--tests/jibuf/jibuf_test.c385
-rw-r--r--tests/jibuf/jibuf_test.ok1
-rw-r--r--tests/jibuf/jitter.plt34
-rw-r--r--tests/testsuite.at6
9 files changed, 883 insertions, 2 deletions
diff --git a/include/osmocom/netif/Makefile.am b/include/osmocom/netif/Makefile.am
index cbaff5c..0db78fb 100644
--- a/include/osmocom/netif/Makefile.am
+++ b/include/osmocom/netif/Makefile.am
@@ -3,6 +3,7 @@ SUBDIRS = channel
osmonetif_HEADERS = amr.h \
channel.h \
datagram.h \
+ jibuf.h \
osmux.h \
ipa.h \
ipa_unit.h \
diff --git a/include/osmocom/netif/jibuf.h b/include/osmocom/netif/jibuf.h
new file mode 100644
index 0000000..f472dd5
--- /dev/null
+++ b/include/osmocom/netif/jibuf.h
@@ -0,0 +1,63 @@
+#ifndef _JIBUF_H_
+#define _JIBUF_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <time.h>
+
+#include <osmocom/core/timer.h>
+
+/*! \defgroup jibuf Osmocom Jitter Buffer
+ * @{
+ */
+
+/*! \file jibuf.h
+ * \brief Osmocom Jitter Buffer helpers
+ */
+
+typedef void (*osmo_jibuf_dequeue_cb)(struct msgb *msg, void *data);
+
+/*! \brief A structure representing a single instance of a jitter buffer */
+struct jibuf {
+ bool started;
+ struct osmo_timer_list timer;
+ struct llist_head msg_list; /* sorted by output ts */
+ uint32_t min_delay; /* in msec */
+ uint32_t max_delay; /* in msec */
+ uint32_t threshold_delay; /* in msec */
+
+ osmo_jibuf_dequeue_cb dequeue_cb;
+ void *dequeue_cb_data;
+
+ /* number of pkt drops since we last changed the buffer size */
+ uint32_t last_dropped;
+ uint32_t consecutive_drops;
+
+ uint32_t ref_rx_ts;
+ uint32_t ref_tx_ts;
+ uint16_t ref_tx_seq;
+
+ struct timeval last_enqueue_time;
+ struct timeval next_dequeue_time;
+
+ struct {
+ uint32_t total_enqueued;
+ uint64_t total_dropped;
+ } stats;
+};
+
+
+struct jibuf *osmo_jibuf_alloc();
+
+void osmo_jibuf_delete(struct jibuf *jb);
+
+int osmo_jibuf_enqueue(struct jibuf *jb, struct msgb *msg);
+
+void osmo_jibuf_set_min_delay(struct jibuf *jb, uint32_t min_delay);
+void osmo_jibuf_set_max_delay(struct jibuf *jb, uint32_t max_delay);
+
+void osmo_jibuf_set_dequeue_cb(struct jibuf *jb, osmo_jibuf_dequeue_cb dequeue_cb, void* cb_data);
+
+/*! @} */
+
+#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index 4bc481e..e1a52e4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,6 +18,7 @@ libosmonetif_la_SOURCES = amr.c \
datagram.c \
ipa.c \
ipa_unit.c \
+ jibuf.c \
osmux.c \
rs232.c \
rtp.c \
diff --git a/src/jibuf.c b/src/jibuf.c
new file mode 100644
index 0000000..b439a47
--- /dev/null
+++ b/src/jibuf.c
@@ -0,0 +1,386 @@
+/* (C) 2017 by Pau Espin Pedrol <pespin@sysmocom.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
+
+#include <osmocom/netif/amr.h>
+#include <osmocom/netif/rtp.h>
+#include <osmocom/netif/jibuf.h>
+
+#include <arpa/inet.h>
+
+/*! \addtogroup jibuf Osmocom Jitter Buffer
+ * @{
+ */
+
+/*! \file jibuf.c
+ * \brief Osmocom Jitter Buffer helpers
+ */
+
+
+/* Enable printing */
+#define JIBUF_DEBUG 1
+/* Sampling rate (in Hz) */
+/* TODO: SAMPLE RATE can be guessed from rtp.p_type */
+#define SAMPLE_RATE 8000
+
+
+/* TUNABLE PARAMETERS: */
+
+/* default {min,max}_delay values if set_{min,max}_delay() is never called */
+#define JIBUF_DEFAULT_MIN_DELAY_MS 60
+#define JIBUF_DEFAULT_MAX_DELAY_MS 200
+
+/* How frequently (num of input packets) do we reselect a new reference? */
+#define JIBUF_REFERENCE_TS_FREQ 60
+
+/* How frequently (num of input packets) do we check if we should adapt the
+ * buffer size (threshold_delay) ? */
+#define JIBUF_BUFFER_RECALC_FREQ 40
+/* How many pkts should be dropped at max every JIBUF_BUFFER_RECALC_FREQ input
+ * pkts? */
+#define JIBUF_ALLOWED_PKT_DROP 3
+/* How many consecutive pkts can be dropped before triggering a buffer size incr ? */
+#define JIBUF_ALLOWED_PKT_CONSECUTIVE_DROP 1
+/* How much do we incr/decr the buffer size every time we recalculate it? */
+#define JIBUF_BUFFER_INC_STEP 20
+#define JIBUF_BUFFER_DEC_STEP 5
+
+
+struct jibuf_msgb_cb {
+ struct timeval ts;
+ unsigned long *old_cb;
+};
+#define JIBUF_MSGB_CB(__msgb) ((struct jibuf_msgb_cb *)&((__msgb)->cb[0]))
+
+void *tall_jibuf_ctx = NULL;
+
+
+static void _ms2timeval(struct timeval *ts, uint32_t ms)
+{
+ ts->tv_sec = ms / 1000;
+ ts->tv_usec = (ms % 1000) * 1000;
+}
+
+static uint32_t _timeval2ms(const struct timeval *ts)
+{
+ return ts->tv_sec * 1000 + ts->tv_usec / 1000;
+}
+
+
+static int clock_gettime_timeval(clockid_t clk_id, struct timeval *tp)
+{
+ struct timespec now;
+ int n;
+
+ n = clock_gettime(clk_id, &now);
+ tp->tv_sec = now.tv_sec;
+ tp->tv_usec = now.tv_nsec / 1000;
+
+ return n;
+}
+
+static struct timeval *_msgb_scheduled_ts(const struct msgb *msg)
+{
+ struct jibuf_msgb_cb *jbcb = JIBUF_MSGB_CB(msg);
+ return &jbcb->ts;
+}
+
+/* Add msgb to the list sorted by its scheduled output ts */
+static void _llist_add_sorted(struct msgb *msg, struct llist_head *msg_list)
+{
+ struct msgb *cur;
+ struct timeval *msg_ts = _msgb_scheduled_ts(msg);
+
+ /* TODO: not sure if I need to use _safe here */
+ llist_for_each_entry(cur, msg_list, list) {
+ struct timeval *cur_ts = _msgb_scheduled_ts(cur);
+ if (timercmp(msg_ts, cur_ts, <)) {
+ __llist_add(&msg->list, cur->list.prev, &cur->list);
+ return;
+ }
+ }
+
+ /* we reached the end, add to the tail: */
+ llist_add_tail(&msg->list, msg_list);
+
+}
+
+static uint16_t _msg_get_sequence(struct msgb *msg)
+{
+ struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg);
+ return ntohs(rtph->sequence);
+}
+
+static uint32_t _msg_get_timestamp(struct msgb *msg)
+{
+ struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg);
+ return ntohl(rtph->timestamp);
+}
+
+static int32_t _samples2ms(int32_t samples)
+{
+ /* XXX: SAMPLE RATE can be guessed from rtp.p_type */
+ return samples * 1000 / SAMPLE_RATE;
+}
+
+/* Calculates pkt delay related to reference pkt. Similar concept to D(i,j) as
+ * defined in RFC3550 (RTP). */
+static int _calc_pkt_rel_delay(struct jibuf *jb, struct msgb *msg)
+{
+ uint32_t current_rx_ts = _timeval2ms(&jb->last_enqueue_time);
+ uint32_t current_tx_ts = _msg_get_timestamp(msg);
+
+ return _samples2ms((current_tx_ts - jb->ref_tx_ts)) - (current_rx_ts - jb->ref_rx_ts);
+}
+
+
+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);
+
+# if JIBUF_DEBUG
+ LOGP(DLMIB, LOGL_DEBUG, "New reference (seq=%"PRIu16" rx=%"PRIu32 \
+ " tx=%"PRIu32")\n", jb->ref_tx_seq, jb->ref_rx_ts, jb->ref_tx_ts);
+#endif
+}
+
+static void _dequeue_msg(struct jibuf *jb, struct msgb *msg)
+{
+ unsigned long *old_cb = JIBUF_MSGB_CB(msg)->old_cb;
+ memcpy(msg->cb, old_cb, sizeof(msg->cb));
+ talloc_free(old_cb);
+ llist_del(&msg->list);
+
+ jb->dequeue_cb(msg, jb->dequeue_cb_data);
+}
+
+static void _osmo_jibuf_timer_expired(void *data)
+{
+ struct jibuf *jb = (struct jibuf*) data;
+ struct timeval delay_ts, now;
+ struct msgb *msg, *next;
+
+ llist_for_each_entry_safe(msg, next, &jb->msg_list, list) {
+ struct timeval *msg_ts = _msgb_scheduled_ts(msg);
+ clock_gettime_timeval(CLOCK_MONOTONIC, &now);
+ if (timercmp(msg_ts, &now, >)) {
+ jb->next_dequeue_time = *msg_ts;
+ timersub(msg_ts, &now, &delay_ts);
+ osmo_timer_schedule(&jb->timer,
+ delay_ts.tv_sec, delay_ts.tv_usec);
+ return;
+ }
+
+ _dequeue_msg(jb, msg);
+ }
+
+ /* XXX: maybe try to tune the threshold based on the calculated output jitter? */
+ /* XXX: try to find holes in the list and create fake pkts to improve the
+ jitter when packets do not arrive on time */
+}
+
+
+static void _recalc_threshold_delay(struct jibuf *jb)
+{
+
+ /* Recalculate every JIBUF_RECALC_FREQ_PKTS handled packets, or if we have too
+ many consecutive drops */
+ uint32_t sum_pkts = jb->stats.total_enqueued + jb->stats.total_dropped +
+ jb->last_dropped;
+
+ if (jb->consecutive_drops <= JIBUF_ALLOWED_PKT_CONSECUTIVE_DROP &&
+ sum_pkts % JIBUF_BUFFER_RECALC_FREQ != 0)
+ return;
+
+ if (jb->consecutive_drops > JIBUF_ALLOWED_PKT_CONSECUTIVE_DROP ||
+ jb->last_dropped > JIBUF_ALLOWED_PKT_DROP)
+ jb->threshold_delay = OSMO_MIN(
+ jb->threshold_delay + JIBUF_BUFFER_INC_STEP,
+ jb->max_delay);
+ else
+ jb->threshold_delay = OSMO_MAX(
+ jb->threshold_delay - JIBUF_BUFFER_DEC_STEP,
+ jb->min_delay);
+# if JIBUF_DEBUG
+ LOGP(DLMIB, LOGL_DEBUG, "New threshold: %u ms (freq=%d dropped=%d/%d consecutive=%d/%d)\n",
+ jb->threshold_delay, JIBUF_BUFFER_RECALC_FREQ,
+ jb->last_dropped, JIBUF_ALLOWED_PKT_DROP,
+ jb->consecutive_drops, JIBUF_ALLOWED_PKT_CONSECUTIVE_DROP);
+#endif
+
+ jb->stats.total_dropped += jb->last_dropped;
+ jb->last_dropped = 0;
+
+}
+
+//----------------------------------
+
+/*! \brief Allocate a new jitter buffer instance
+ * \return the new allocated instance
+ */
+struct jibuf *osmo_jibuf_alloc()
+{
+ struct jibuf *jb;
+ jb = talloc_zero(tall_jibuf_ctx, struct jibuf);
+
+ jb->min_delay = JIBUF_DEFAULT_MIN_DELAY_MS;
+ jb->max_delay = JIBUF_DEFAULT_MAX_DELAY_MS;
+ jb->threshold_delay = jb->min_delay;
+
+ INIT_LLIST_HEAD(&jb->msg_list);
+
+ jb->timer.cb = _osmo_jibuf_timer_expired;
+ jb->timer.data = jb;
+
+ return jb;
+}
+
+/*! \brief Destroy a previously allocated jitter buffer instance
+ * \param[in] jb Previously allocated (non-null) jitter buffer instance
+ *
+ * All the queued packets are dequeued before deleting the instance.
+ */
+void osmo_jibuf_delete(struct jibuf *jb)
+{
+ struct msgb *msg, *tmp;
+ osmo_timer_del(&jb->timer);
+ llist_for_each_entry_safe(msg, tmp, &jb->msg_list, list)
+ _dequeue_msg(jb, msg);
+
+ talloc_free(jb);
+}
+
+/*! \brief Try to enqueue a packet into the jitter buffer
+ * \param[in] jb jitter buffer instance
+ * \param[in] msg msgb to enqueue, containing an RTP packet
+ * \return <0 if the packet was dropped, 0 otherwise
+ *
+ * This function calculates the delay for the enqueued packet. If the delay is
+ * bigger than the current buffer size, the function returns -1 and the caller
+ * owns the packet again and can free it if required. If the packet is enqueued,
+ * 0 is returned and the exact same packet (ownership transfer, no copy is made)
+ * will be available again through the dequeue_cb() when the queue timer for
+ * this packet expires.
+ */
+int osmo_jibuf_enqueue(struct jibuf *jb, struct msgb *msg)
+{
+ int rel_delay, delay;
+ struct timeval delay_ts, sched_ts;
+
+ clock_gettime_timeval(CLOCK_MONOTONIC, &jb->last_enqueue_time);
+
+ if (!jb->started) {
+ jb->started = true;
+ _msg_set_as_reference(jb, msg);
+ rel_delay = 0;
+ } else {
+ rel_delay = _calc_pkt_rel_delay(jb, msg);
+ }
+
+ /* Avoid time skew with sender (or drop-everything state),
+ reselect a new reference from time to time */
+ //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;
+
+ /* packet too late, let's drop it and incr buffer size if encouraged */
+ if (delay < 0) {
+ jb->last_dropped++;
+ jb->consecutive_drops++;
+#if JIBUF_DEBUG
+ LOGP(DLMIB, LOGL_DEBUG, "dropped %u > %u (seq=%"PRIu16" ts=%"PRIu32")\n",
+ rel_delay, jb->threshold_delay, _msg_get_sequence(msg),
+ _msg_get_timestamp(msg));
+#endif
+ _recalc_threshold_delay(jb);
+ return -1;
+ } else {
+ jb->consecutive_drops = 0;
+ jb->stats.total_enqueued++;
+ }
+
+ _ms2timeval(&delay_ts, (uint32_t) delay);
+ timeradd(&jb->last_enqueue_time, &delay_ts, &sched_ts);
+
+#if JIBUF_DEBUG
+ LOGP(DLMIB, LOGL_DEBUG, "enqueuing packet seq=%"PRIu16" rel=%d delay=%d" \
+ " thres=%d {%lu.%06lu -> %lu.%06lu}\n",
+ _msg_get_sequence(msg), rel_delay, delay, jb->threshold_delay,
+ jb->last_enqueue_time.tv_sec, jb->last_enqueue_time.tv_usec,
+ sched_ts.tv_sec, sched_ts.tv_usec);
+#endif
+
+ /* Add scheduled dequeue time in msg->cb so we can check it later */
+ unsigned long *old_cb = talloc_memdup(tall_jibuf_ctx, msg->cb, sizeof(msg->cb));
+ struct jibuf_msgb_cb *jbcb = JIBUF_MSGB_CB(msg);
+ jbcb->ts = sched_ts;
+ jbcb->old_cb = old_cb;
+
+ _llist_add_sorted(msg, &jb->msg_list);
+
+
+ /* See if updating the timer is needed: */
+ if (!osmo_timer_pending(&jb->timer) ||
+ timercmp(&sched_ts, &jb->next_dequeue_time, <)) {
+ jb->next_dequeue_time = sched_ts;
+ osmo_timer_schedule(&jb->timer, 0, delay * 1000);
+ }
+
+ /* Let's check packet loss stats to see if buffer_size must be changed */
+ _recalc_threshold_delay(jb);
+
+ return 0;
+}
+
+
+/*! \brief Set minimum buffer size for the jitter buffer
+ * \param[in] jb jitter buffer instance
+ * \param[in] min_delay Minimum buffer size, as in minimum delay in milliseconds
+ */
+void osmo_jibuf_set_min_delay(struct jibuf *jb, uint32_t min_delay)
+{
+ jb->min_delay = min_delay ? min_delay : JIBUF_DEFAULT_MIN_DELAY_MS;
+ jb->threshold_delay = OSMO_MAX(jb->min_delay, jb->threshold_delay);
+}
+
+/*! \brief Set maximum buffer size for the jitter buffer
+ * \param[in] jb jitter buffer instance
+ * \param[in] max_delay Maximum buffer size, as in maximum delay in milliseconds
+ */
+void osmo_jibuf_set_max_delay(struct jibuf *jb, uint32_t max_delay)
+{
+ jb->max_delay = max_delay ? max_delay : JIBUF_DEFAULT_MAX_DELAY_MS;
+ jb->threshold_delay = OSMO_MIN(jb->max_delay, jb->threshold_delay);
+}
+
+/*! \brief Set dequeue callback for the jitter buffer
+ * \param[in] jb jitter buffer instance
+ * \param[in] dequeue_cb function pointer to call back when the dequeue timer for a given packet expires
+ * \param[in] cb_data data pointer to be passed to dequeue_cb together with the msgb.
+ */
+void osmo_jibuf_set_dequeue_cb(struct jibuf *jb, osmo_jibuf_dequeue_cb
+ dequeue_cb, void* cb_data)
+{
+ jb->dequeue_cb = dequeue_cb;
+ jb->dequeue_cb_data = cb_data;
+}
+
+
+/*! @} */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f99e276..bf1db1f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,11 +1,14 @@
AM_CFLAGS = -Wall -I$(top_srcdir)/include $(LIBOSMOCORE_CFLAGS) -g
AM_LDFLAGS = $(LIBOSMOCORE_LDFLAGS)
-check_PROGRAMS = osmux/osmux_test
+check_PROGRAMS = osmux/osmux_test jibuf/jibuf_test
osmux_osmux_test_SOURCES = osmux/osmux_test.c
osmux_osmux_test_LDADD = $(LIBOSMOCORE_LIBS) $(top_builddir)/src/libosmonetif.la
+jibuf_jibuf_test_SOURCES = jibuf/jibuf_test.c
+jibuf_jibuf_test_LDADD = $(LIBOSMOCORE_LIBS) $(top_builddir)/src/libosmonetif.la
+
# The `:;' works around a Bash 3.2 bug when the output is not writeable.
$(srcdir)/package.m4: $(top_srcdir)/configure.ac
:;{ \
@@ -25,7 +28,8 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
} >'$(srcdir)/package.m4'
EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
- osmux/osmux_test.ok
+ osmux/osmux_test.ok \
+ jibuf/jibuf_test.ok
DISTCLEANFILES = atconfig
diff --git a/tests/jibuf/jibuf_test.c b/tests/jibuf/jibuf_test.c
new file mode 100644
index 0000000..35cca19
--- /dev/null
+++ b/tests/jibuf/jibuf_test.c
@@ -0,0 +1,385 @@
+/* (C) 207 by Pau Espin Pedrol <pespin@sysmocom.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <signal.h>
+#include <arpa/inet.h>
+#include <sys/time.h>
+
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/netif/jibuf.h>
+#include <osmocom/netif/rtp.h>
+
+#define DJBT 0
+
+struct log_info_cat jibuf_test_cat[] = {
+ [DJBT] = {
+ .name = "DJIBUF_TEST",
+ .description = "jibuf test",
+ .color = "\033[1;35m",
+ .enabled = 1, .loglevel = LOGL_DEBUG,
+ },
+ [DLMIB*-1 -1] = {
+ .name = "DLMIB",
+ .description = "A-bis Input Driver for B-Channels (voice)",
+ .enabled = 1, .loglevel = LOGL_ERROR,
+ },
+};
+
+const struct log_info jibuf_test_log_info = {
+ .filter_fn = NULL,
+ .cat = jibuf_test_cat,
+ .num_cat = ARRAY_SIZE(jibuf_test_cat),
+};
+
+/* RTP packet with AMR payload */
+static uint8_t rtp_pkt[] = {
+ 0x80, 0x62, 0x3f, 0xcc, 0x00, 0x01, 0xa7, 0x6f, /* RTP */
+ 0x07, 0x09, 0x00, 0x62, 0x20, 0x14, 0xff, 0xd4, /* AMR */
+ 0xf9, 0xff, 0xfb, 0xe7, 0xeb, 0xf9, 0x9f, 0xf8,
+ 0xf2, 0x26, 0x33, 0x65, 0x54,
+};
+
+static void sigalarm_handler(int foo)
+{
+ printf("FAIL: test did not run successfully\n");
+ exit(EXIT_FAILURE);
+}
+
+#define SAMPLES_PER_PKT 160
+#define RTP_FREQ_MS 20
+#define RTP_PKTS_PER_SEC (1000/RTP_FREQ_MS)
+#define NET_DELAY_MS 300
+#define GENERATED_JITTER_MS 160
+#define NUM_PACKETS_TO_SEND 1000
+
+#define TRACE_PACKE_DEBUG 0
+#define TRACE_PACKET_GNUPLOT 1
+#define TRACE_PACKET_TEST_JITTER 0
+
+struct checkpoint {
+ struct timeval ts;
+ int transit;
+ double jitter;
+};
+
+struct rtp_pkt_info {
+ struct osmo_timer_list timer;
+ struct timeval tx_prev_time;
+ struct timeval tx_time;
+ uint32_t tx_delay;
+ struct checkpoint prequeue;
+ struct checkpoint postqueue;
+};
+
+struct rtp_pkt_info_cb {
+ struct rtp_pkt_info *data;
+};
+
+struct jibuf *jb;
+uint16_t rtp_first_seq;
+uint16_t rtp_next_seq;
+uint32_t rtp_next_ts;
+uint32_t packets_sent;
+uint32_t packets_received;
+uint32_t packets_dropped;
+uint32_t packets_too_much_jitter;
+
+struct rtp_pkt_info *msgb_get_pinfo(struct msgb* msg)
+{
+ struct rtp_pkt_info_cb *cb = (struct rtp_pkt_info_cb *)&((msg)->cb[0]);
+ return cb->data;
+}
+
+static uint32_t timeval2ms(const struct timeval *ts)
+{
+ return ts->tv_sec * 1000 + ts->tv_usec / 1000;
+}
+
+int calc_relative_transmit_time(struct timeval *tx_0, struct timeval *tx_f,
+ struct timeval *rx_0, struct timeval *rx_f)
+{
+ struct timeval txdiff, rxdiff, diff;
+ timersub(rx_f, rx_0, &rxdiff);
+ timersub(tx_f, tx_0, &txdiff);
+ timersub(&rxdiff, &txdiff, &diff);
+ return timeval2ms(&diff);
+}
+
+void trace_pkt(struct msgb *msg, char* info) {
+ struct timeval now, total_delay;
+ struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg);
+ struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg);
+
+ gettimeofday(&now, NULL);
+ timersub(&now, &pinfo->tx_time, &total_delay);
+
+#if TRACE_PACKET_DEBUG
+ 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",
+ 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);
+#endif
+
+#if TRACE_PACKET_GNUPLOT
+ /* Used as input for gplot: "gnuplot -p jitter.plt -"" */
+ 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",
+ 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);
+#endif
+}
+
+void pkt_add_result(struct msgb *msg, bool dropped)
+{
+ struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg);
+
+ if (dropped) {
+ packets_dropped++;
+ trace_pkt(msg,"dropped");
+ } else {
+ packets_received++;
+ trace_pkt(msg,"received");
+
+ if (pinfo->prequeue.jitter < pinfo->postqueue.jitter) {
+ packets_too_much_jitter++;
+#if TRACE_PACKET_TEST_JITTER
+ LOGP(DJBT, LOGL_ERROR, "JITTER HIGHER THAN REF: %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 dropped=%"PRIu32 \
+ " buffer=%"PRIu32"\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,
+ packets_dropped, jb->threshold_delay);
+#endif
+ }
+ }
+}
+
+void dequeue_cb(struct msgb *msg, void *data)
+{
+ static struct checkpoint postqueue_prev;
+ static bool postqueue_started = false;
+
+ struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg);
+
+ gettimeofday(&pinfo->postqueue.ts, NULL);
+
+ if (postqueue_started) {
+ pinfo->postqueue.transit = calc_relative_transmit_time(
+ &pinfo->tx_prev_time, &pinfo->tx_time,
+ &postqueue_prev.ts, &pinfo->postqueue.ts);
+
+ uint32_t abs_transit = pinfo->postqueue.transit *
+ ( pinfo->postqueue.transit >= 0 ? 1 : -1 );
+
+ pinfo->postqueue.jitter = postqueue_prev.jitter +
+ ((double)abs_transit - postqueue_prev.jitter)/16.0;
+ } else {
+ postqueue_started = true;
+ pinfo->postqueue.transit = 0;
+ pinfo->postqueue.jitter = 0;
+ }
+
+ postqueue_prev = pinfo->postqueue;
+
+ pkt_add_result(msg, false);
+
+ osmo_timer_del(&pinfo->timer);
+ msgb_free(msg);
+}
+
+void pkt_arrived_cb(void *data)
+{
+ static struct checkpoint prequeue_prev;
+ static bool prequeue_started = false;
+
+ struct msgb *msg = (struct msgb*) data;
+ struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg);
+
+ gettimeofday(&pinfo->prequeue.ts, NULL);
+
+ if (prequeue_started) {
+ pinfo->prequeue.transit = calc_relative_transmit_time(
+ &pinfo->tx_prev_time, &pinfo->tx_time,
+ &prequeue_prev.ts, &pinfo->prequeue.ts);
+
+ uint32_t abs_transit = pinfo->prequeue.transit *
+ ( pinfo->prequeue.transit >= 0 ? 1 : -1 );
+
+ pinfo->prequeue.jitter = prequeue_prev.jitter +
+ ((double)abs_transit - prequeue_prev.jitter)/16.0;
+ } else {
+ prequeue_started = true;
+ pinfo->prequeue.transit = 0;
+ pinfo->prequeue.jitter = 0;
+ }
+
+ prequeue_prev = pinfo->prequeue;
+
+ int n = osmo_jibuf_enqueue(jb, msg);
+
+ if (n<0) {
+ pkt_add_result(msg, true);
+ osmo_timer_del(&pinfo->timer);
+ msgb_free(msg);
+ }
+}
+
+void send_rtp_packet()
+{
+ static struct timeval tx_prev_time;
+
+ struct rtp_pkt_info *pinfo;
+ struct rtp_hdr *rtph;
+ struct msgb *msg;
+
+ /* Set fake prev_time for 1st packet. Otherwise transit calculations for first
+ * packet can be really weird if they not arrive in order */
+ if (rtp_next_seq == rtp_first_seq) {
+ struct timeval now, time_rate = { .tv_sec = 0, .tv_usec = RTP_FREQ_MS * 1000};
+ gettimeofday(&now, NULL);
+ timersub(&now, &time_rate, &tx_prev_time);
+ }
+
+
+ msg = msgb_alloc(1500, "test");
+ if (!msg)
+ exit(EXIT_FAILURE);
+
+ memcpy(msg->data, rtp_pkt, sizeof(rtp_pkt));
+ msgb_put(msg, sizeof(rtp_pkt));
+
+ rtph = osmo_rtp_get_hdr(msg);
+
+ rtph->sequence = htons(rtp_next_seq);
+ rtp_next_seq++;
+
+ rtph->timestamp = htonl(rtp_next_ts);
+ rtp_next_ts += SAMPLES_PER_PKT;
+
+ pinfo = talloc_zero(msg, struct rtp_pkt_info);
+ struct rtp_pkt_info_cb *cb = (struct rtp_pkt_info_cb *)&((msg)->cb[0]);
+ cb->data = pinfo;
+
+ gettimeofday(&pinfo->tx_time, NULL);
+ pinfo->tx_prev_time = tx_prev_time;
+ memset(&pinfo->timer, 0, sizeof(struct osmo_timer_list));
+ pinfo->timer.cb = pkt_arrived_cb;
+ pinfo->timer.data = msg;
+ pinfo->tx_delay = NET_DELAY_MS + (random() % (GENERATED_JITTER_MS));
+
+ tx_prev_time = pinfo->tx_time;
+
+ /* TODO: add a random() to lose/drop packets */
+
+ osmo_timer_schedule(&pinfo->timer, 0, pinfo->tx_delay * 1000);
+}
+
+void generate_pkt_cb(void *data)
+{
+ static struct osmo_timer_list enqueue_timer = {.cb = generate_pkt_cb, .data = NULL};
+ static struct timeval last_generated;
+
+ struct timeval time_rate = { .tv_sec = 0, .tv_usec = RTP_FREQ_MS * 1000};
+ struct timeval sched_ts;
+
+ if (!packets_sent)
+ gettimeofday(&last_generated, NULL);
+
+ send_rtp_packet();
+ packets_sent++;
+
+ timeradd(&last_generated, &time_rate, &sched_ts);
+ last_generated = sched_ts;
+ if (packets_sent < NUM_PACKETS_TO_SEND) {
+ enqueue_timer.timeout = sched_ts;
+ osmo_timer_add(&enqueue_timer);
+ }
+}
+
+void check_results()
+{
+ uint32_t drop_threshold = NUM_PACKETS_TO_SEND * 5 / 100;
+ if (packets_dropped > drop_threshold) {
+ fprintf(stdout, "Too many dropped packets (%"PRIu32" > %"PRIu32")\n",
+ packets_dropped, drop_threshold);
+ exit(1);
+ }
+
+ uint32_t jitter_high_threshold = NUM_PACKETS_TO_SEND * 8 / 100;
+ if (packets_too_much_jitter > jitter_high_threshold) {
+ fprintf(stdout, "Too many packets with higher jitter (%"PRIu32" > %"PRIu32")\n",
+ packets_too_much_jitter, jitter_high_threshold);
+ exit(1);
+ }
+}
+
+int main(void)
+{
+
+ if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
+ perror("signal");
+ exit(EXIT_FAILURE);
+ }
+
+ /* This test doesn't use it, but jibuf requires it internally. */
+ osmo_init_logging(&jibuf_test_log_info);
+ log_set_category_filter(osmo_stderr_target, DLMIB, 1, LOGL_ERROR);
+ log_set_print_filename(osmo_stderr_target, 0);
+ log_set_log_level(osmo_stderr_target, LOGL_INFO);
+
+ srandom(time(NULL));
+ rtp_first_seq = (uint16_t) random();
+ rtp_next_seq = rtp_first_seq;
+ rtp_next_ts = (uint32_t) random();
+ jb = osmo_jibuf_alloc();
+
+ osmo_jibuf_set_min_delay(jb, GENERATED_JITTER_MS - RTP_FREQ_MS);
+ osmo_jibuf_set_max_delay(jb, GENERATED_JITTER_MS + RTP_FREQ_MS*2);
+
+ osmo_jibuf_set_dequeue_cb(jb, dequeue_cb, NULL);
+
+ generate_pkt_cb(NULL);
+
+ /* If the test takes longer than twice the time needed to generate the packets
+ plus 10 seconds, abort it */
+ alarm(NUM_PACKETS_TO_SEND*20/1000 +10);
+
+ while((packets_received + packets_dropped) < NUM_PACKETS_TO_SEND)
+ osmo_select_main(0);
+
+ osmo_jibuf_delete(jb);
+
+ check_results();
+
+ fprintf(stdout, "OK: Test passed\n");
+ return EXIT_SUCCESS;
+}
diff --git a/tests/jibuf/jibuf_test.ok b/tests/jibuf/jibuf_test.ok
new file mode 100644
index 0000000..0f65527
--- /dev/null
+++ b/tests/jibuf/jibuf_test.ok
@@ -0,0 +1 @@
+OK: Test passed
diff --git a/tests/jibuf/jitter.plt b/tests/jibuf/jitter.plt
new file mode 100644
index 0000000..f53b4a5
--- /dev/null
+++ b/tests/jibuf/jitter.plt
@@ -0,0 +1,34 @@
+#set terminal png size 1280,1024 enhanced font "Helvetica,20"
+#set output 'output.png'
+#set terminal qt
+#set output
+
+set datafile separator "\t"
+set datafile missing '0'
+
+#set multiplot
+
+# key/legend
+set key top right
+set key box
+set key left bottom
+set key bmargin
+
+
+set title 'Jitter Graph'
+set xlabel 'Timestamp'
+set ylabel 'trans / jitter / delay / buffer size [ms]'
+set ytics nomirror
+
+#set y2label 'delay [ms]'
+#set y2tics nomirror
+
+plot \
+ '/tmp/bla' using 3:5 with linespoints title 'pre.trans' axes x1y1, \
+ '/tmp/bla' using 4:6 with linespoints title 'post.trans' axes x1y1, \
+ '/tmp/bla' using 3:7 with linespoints title 'pre.jitter' axes x1y1, \
+ '/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:((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
diff --git a/tests/testsuite.at b/tests/testsuite.at
index d0e55f6..81d40ba 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -6,3 +6,9 @@ AT_KEYWORDS([osmux_test])
cat $abs_srcdir/osmux/osmux_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/osmux/osmux_test], [0], [expout], [ignore])
AT_CLEANUP
+
+AT_SETUP([jibuf_test])
+AT_KEYWORDS([jibuf_test])
+cat $abs_srcdir/jibuf/jibuf_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/jibuf/jibuf_test], [0], [expout], [ignore])
+AT_CLEANUP