Reduce audio stream latency

- fflags nobuffer + flags low_delay: ffmpeg Input-Buffer deaktiviert
- frame_duration 20ms + page_duration 20ms: kleinste Ogg-Pages
- flush_packets 1: sofortiges Schreiben nach jedem Frame
- HTTP Header no-cache + X-Accel-Buffering no: kein Proxy/Flask-Buffering

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Julian Vollmer 2026-05-18 18:19:34 +02:00
parent a53eceb83a
commit fa9888f83e
1 changed files with 10 additions and 0 deletions

View File

@ -115,12 +115,18 @@ AUDIO_SAMPLERATE = 16000
def generate_audio(): def generate_audio():
cmd = [ cmd = [
"ffmpeg", "ffmpeg",
"-fflags", "nobuffer",
"-flags", "low_delay",
"-f", "alsa", "-f", "alsa",
"-thread_queue_size", "128",
"-i", AUDIO_DEVICE, "-i", AUDIO_DEVICE,
"-ar", str(AUDIO_SAMPLERATE), "-ar", str(AUDIO_SAMPLERATE),
"-ac", "1", "-ac", "1",
"-c:a", "libopus", "-c:a", "libopus",
"-b:a", "24k", "-b:a", "24k",
"-frame_duration", "20",
"-page_duration", "20000",
"-flush_packets", "1",
"-f", "ogg", "-f", "ogg",
"-", "-",
] ]
@ -142,6 +148,10 @@ def audio():
return Response( return Response(
generate_audio(), generate_audio(),
mimetype="audio/ogg; codecs=opus", mimetype="audio/ogg; codecs=opus",
headers={
"Cache-Control": "no-cache, no-store",
"X-Accel-Buffering": "no",
},
) )