Skip to content

Commit 9e803c4

Browse files
author
Datanoise
committed
fix: ensure Ogg stream continuity and improve WebRTC synchronization robustness
1 parent e7f0eaf commit 9e803c4

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

relay/transcode.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,32 +211,33 @@ func (tm *TranscoderManager) encodeOpus(ctx context.Context, inst *TranscoderIns
211211
// Ogg encapsulation
212212
writer := &streamWriter{stream: output, relay: tm.relay, inst: inst}
213213
serial := uint32(time.Now().UnixNano())
214-
215-
// We need to capture the raw Ogg page bytes for the headers
214+
pw := ogg.NewPacketWriter(writer, serial)
215+
216+
// 1. Capture headers in a buffer using a temporary writer with SAME serial
216217
var headerBuf bytes.Buffer
217-
headerWriter := io.MultiWriter(writer, &headerBuf)
218-
pw := ogg.NewPacketWriter(headerWriter, serial)
218+
headerPW := ogg.NewPacketWriter(&headerBuf, serial)
219219

220-
// ID Header
221220
head := ogg.OpusHead{
222221
Version: 1,
223222
Channels: uint8(channels),
224223
InputSampleRate: uint32(sampleRate),
225224
}
226225
headPacket, _ := ogg.BuildOpusHeadPacket(head)
227-
pw.WritePacket(headPacket, 0, true, false)
226+
headerPW.WritePacket(headPacket, 0, true, false)
228227

229-
// Tags Header
230228
tags := ogg.OpusTags{Vendor: "tinyice-opus"}
231229
tagsPacket, _ := ogg.BuildOpusTagsPacket(tags)
232-
pw.WritePacket(tagsPacket, 0, false, false)
233-
pw.Flush()
230+
headerPW.WritePacket(tagsPacket, 0, false, false)
231+
headerPW.Flush()
234232

235-
// Store the full Ogg pages (headers) for new listeners
233+
// Store for mid-stream listeners
236234
output.OggHead = headerBuf.Bytes()
237235

238-
// Re-create packet writer for audio data
239-
pw = ogg.NewPacketWriter(writer, serial)
236+
// 2. Now write the same packets to the ACTUAL stream using the main writer
237+
// This ensures main writer 'seq' starts correctly at 0, 1...
238+
pw.WritePacket(headPacket, 0, true, false)
239+
pw.WritePacket(tagsPacket, 0, false, false)
240+
pw.Flush()
240241

241242
pcmBuf := make([]byte, frameSize*channels*2)
242243
pcmSamples := make([]int16, frameSize*channels)

relay/webrtc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (wm *WebRTCManager) streamToTrack(pc *webrtc.PeerConnection, track *webrtc.
116116
if n == 0 {
117117
continue
118118
}
119+
// Use a robust sliding window to find "OggS"
119120
for i := 0; i <= n-4; i++ {
120121
if syncBuf[i] == 'O' && syncBuf[i+1] == 'g' && syncBuf[i+2] == 'g' && syncBuf[i+3] == 'S' {
121122
offset += int64(i)
@@ -124,7 +125,8 @@ func (wm *WebRTCManager) streamToTrack(pc *webrtc.PeerConnection, track *webrtc.
124125
}
125126
}
126127
if !foundSync {
127-
offset = next
128+
// Keep the last 3 bytes in case "OggS" is split between reads
129+
offset = next - 3
128130
searched += int64(n)
129131
}
130132
}

0 commit comments

Comments
 (0)