From 33c4e981524d8394a334b36df9ba1857e5b4f003 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 26 Apr 2021 21:39:36 -0500 Subject: [PATCH] Adjust initial audio resync drop time based on connection handshake latency --- src/AudioStream.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/AudioStream.c b/src/AudioStream.c index fd8f604..bae0f9a 100644 --- a/src/AudioStream.c +++ b/src/AudioStream.c @@ -18,6 +18,7 @@ static PPLT_CRYPTO_CONTEXT audioDecryptionCtx; static unsigned short lastSeq; static bool receivedDataFromPeer; +static uint64_t pingStartTime; #define RTP_PORT 48000 @@ -76,6 +77,15 @@ int initializeAudioStream(void) { return LastSocketFail(); } + // Track the time we started pinging. Audio samples will begin arriving + // shortly after the first ping reaches the host. However, we won't be + // ready to start playback until later on. As a result, we'll drop samples + // equivalent to the amount of time before the receive thread is ready + // to accept traffic in real-time. + // FIXME: This could also take into account the size of the recv buffer, + // since long RTSP handshakes could cause that to fill to capacity. + pingStartTime = PltGetMillis(); + // We may receive audio before our threads are started, but that's okay. We'll // drop the first 1 second of audio packets to catch up with the backlog. int err = PltCreateThread("AudioPing", UdpPingThreadProc, NULL, &udpPingThread); @@ -185,9 +195,15 @@ static void ReceiveThreadProc(void* context) { PQUEUED_AUDIO_PACKET packet; int queueStatus; bool useSelect; - int packetsToDrop = 1000 / AudioPacketDuration; + int initialResyncDelay = PltGetMillis() - pingStartTime; + int packetsToDrop; int waitingForAudioMs; + // Cap delay at 3 seconds to account for recv buffer cap + initialResyncDelay = initialResyncDelay > 3000 ? 3000 : initialResyncDelay; + packetsToDrop = initialResyncDelay / AudioPacketDuration; + Limelog("Initial audio resync period: %d milliseconds\n", initialResyncDelay); + packet = NULL; if (setNonFatalRecvTimeoutMs(rtpSocket, UDP_RECV_POLL_TIMEOUT_MS) < 0) {