summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-11-27 20:45:49 +0100
committerAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-12-12 20:22:34 +0100
commita8c2bfb9549398da8784626cbf7a01a9463c5ab1 (patch)
tree9dcbe754a66ec98369fdf96e3ad8a934b14f20aa
parent451a0103899c0dc82d7e62a003ac1979867a2278 (diff)
audio: add timing in tx thread
- added timing code in the transmit thread to pace out RTP packets at the right interval (e.g. ptime 20ms) The transmit aubuf is set to max 100ms. This ensures that if audio samples are coming too fast from the audio driver/device, the oldest samples will be dropped. Ref: https://github.com/alfredh/baresip/issues/325 cleanup
-rw-r--r--src/audio.c35
1 files changed, 30 insertions, 5 deletions
diff --git a/src/audio.c b/src/audio.c
index 44b65f6..6a6e172 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -81,6 +81,7 @@ struct autx {
struct auenc_state *enc; /**< Audio encoder state (optional) */
struct aubuf *aubuf; /**< Packetize outgoing stream */
size_t aubuf_maxsz; /**< Maximum aubuf size in [bytes] */
+ volatile bool aubuf_started;
struct auresamp resamp; /**< Optional resampler for DSP */
struct list filtl; /**< Audio filters in encoding order */
struct mbuf *mb; /**< Buffer for outgoing RTP packets */
@@ -645,6 +646,8 @@ static void ausrc_read_handler(const void *sampv, size_t sampc, void *arg)
(void)aubuf_write(tx->aubuf, sampv, num_bytes);
+ tx->aubuf_started = true;
+
if (a->cfg.txmode == AUDIO_MODE_POLL) {
unsigned i;
@@ -1118,19 +1121,41 @@ static void *tx_thread(void *arg)
{
struct audio *a = arg;
struct autx *tx = &a->tx;
- unsigned i;
+ uint64_t ts = 0;
while (a->tx.u.thr.run) {
- for (i=0; i<16; i++) {
+ uint64_t now;
- if (aubuf_cur_size(tx->aubuf) < tx->psize)
- break;
+ sys_msleep(4);
+
+ if (!tx->aubuf_started)
+ continue;
+
+ if (!a->tx.u.thr.run)
+ break;
+
+ now = tmr_jiffies();
+ if (!ts)
+ ts = now;
+
+ if (ts > now)
+ continue;
+
+ /* Now is the time to send */
+
+ if (aubuf_cur_size(tx->aubuf) >= tx->psize) {
poll_aubuf_tx(a);
}
+ else {
+ ++tx->stats.aubuf_underrun;
+
+ debug("audio: thread: tx aubuf underrun"
+ " (total %llu)\n", tx->stats.aubuf_underrun);
+ }
- sys_msleep(5);
+ ts += tx->ptime;
}
return NULL;