Skip to content

Commit 19d088e

Browse files
committed
fix: improve audio timestamp handling in buffered mode to reduce jitter
1 parent bcc0309 commit 19d088e

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

src/receiver.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,25 @@ static uint64_t next_audio_timestamp(struct irl_source *ctx, int base_samples,
329329
ctx->audio_sys_base = (uint64_t)(base > 0 ? base : 0);
330330
audio_ts = now;
331331
}
332-
} else if (drift > 30000000LL) {
333-
ctx->audio_pll_corrections++;
334-
ctx->audio_pll_offset_ns -= frame_ns;
335-
audio_ts -= frame_ns;
336-
} else if (drift < -30000000LL) {
337-
ctx->audio_pll_corrections++;
338-
ctx->audio_sys_base += (uint64_t)frame_ns;
339-
audio_ts += frame_ns;
332+
} else {
333+
/* Buffered mode should keep a small amount of audio queued
334+
* inside OBS, otherwise OBS sees a just-in-time stream and
335+
* grows its own async buffer on the first jitter spike. */
336+
int64_t target_lead_ns = frame_ns * 2;
337+
int64_t min_drift = target_lead_ns - 30000000LL;
338+
int64_t max_drift = target_lead_ns + 30000000LL;
339+
340+
if (drift < min_drift) {
341+
ctx->audio_pll_corrections++;
342+
int64_t target_ts = (int64_t)now + target_lead_ns;
343+
int64_t base = target_ts - ctx->audio_pll_offset_ns;
344+
ctx->audio_sys_base = (uint64_t)(base > 0 ? base : 0);
345+
audio_ts = (uint64_t)(target_ts > 0 ? target_ts : 0);
346+
} else if (drift > max_drift) {
347+
ctx->audio_pll_corrections++;
348+
ctx->audio_pll_offset_ns -= frame_ns;
349+
audio_ts -= frame_ns;
350+
}
340351
}
341352

342353
drift = (int64_t)audio_ts - (int64_t)now;
@@ -801,11 +812,9 @@ static void handle_audio_frame(struct irl_source *ctx, AVFrame *frame)
801812
* next_audio_ts_min because both advance by exactly
802813
* base_samples / sample_rate per push.
803814
*
804-
* Gentle PLL: when buffered mode drifts more than
805-
* ~30ms ahead or behind wall clock, slew by one
806-
* frame so OBS sees a continuous timeline instead
807-
* of a large jump. Low-latency mode still snaps
808-
* directly to wall clock. */
815+
* Buffered mode keeps roughly two chunks queued in OBS
816+
* so async audio doesn't immediately grow its own buffer.
817+
* Low-latency mode still snaps directly to wall clock. */
809818
uint32_t frames_out =
810819
(uint32_t)(got /
811820
(out_channels * bytes_per_sample));

0 commit comments

Comments
 (0)