Skip to content

Commit 87480af

Browse files
authored
Merge pull request #39 from viruslox/fix-srt-connection-15195665643207896187
Keep SRT connection alive with ffmpeg fifo and continuous audio
2 parents 0add954 + ac757b8 commit 87480af

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

internal/modules/audiobridge/stream/mixer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func (m *Mixer) mixLoop() {
9494
attackCoef := math.Exp(-1.0 / (sampleRate * (attackTimeMs / 1000.0)))
9595
releaseCoef := math.Exp(-1.0 / (sampleRate * (releaseTimeMs / 1000.0)))
9696

97+
// Reusable zeroed chunk for sending silence
98+
silentChunk := make([]byte, chunkSize)
99+
97100
for {
98101
select {
99102
case <-ticker.C:
@@ -117,6 +120,13 @@ func (m *Mixer) mixLoop() {
117120
m.gateGain = releaseCoef * m.gateGain
118121
}
119122
}
123+
124+
// Send a chunk of silence to keep the SRT stream active
125+
select {
126+
case m.outChan <- silentChunk:
127+
default:
128+
log.Println("[AudioBridge] Warning: Mixer output channel blocked, dropping silent chunk")
129+
}
120130
continue
121131
}
122132

internal/modules/audiobridge/stream/srt.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ func (s *SRTManager) Start() error {
3838
bitrate = "128k"
3939
}
4040

41-
// Setup FFmpeg command
41+
// Setup FFmpeg command with options to keep connection alive
4242
s.cmd = exec.Command("ffmpeg",
4343
"-f", "s16le", // raw 16-bit little-endian PCM
4444
"-ar", "48000", // 48kHz sample rate
4545
"-ac", "2", // 2 channels (stereo)
4646
"-i", "pipe:0", // Read from stdin
4747
"-c:a", "libopus", // Encode to Opus
4848
"-b:a", bitrate,
49-
"-f", "mpegts", // Container
49+
// Apply fifo to abstract network output, adding reliability, dropped packets rather than blocking, and infinite reconnects
50+
"-f", "fifo",
51+
"-fifo_format", "mpegts",
52+
"-drop_pkts_on_overflow", "1",
53+
"-attempt_recovery", "1",
54+
"-recovery_wait_time", "1",
5055
s.cfg.Streaming.DestinationURL,
5156
)
5257

0 commit comments

Comments
 (0)