Skip to content

Commit 92e098a

Browse files
author
Datanoise
committed
fix: implement strict Ogg page alignment and prevent duplicate headers to resolve CRC mismatch errors
1 parent 2a721bb commit 92e098a

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

relay/relay.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,28 @@ func (s *Stream) Subscribe(id string, burstSize int) (int64, chan struct{}) {
323323
start = 0
324324
}
325325

326-
// For Ogg/Opus, align to the oldest known page boundary within the burst
326+
// For Ogg/Opus, align to the oldest known page boundary within the valid buffer range
327327
if strings.Contains(strings.ToLower(s.ContentType), "ogg") || strings.Contains(strings.ToLower(s.ContentType), "opus") {
328+
validStart := s.Buffer.Head - s.Buffer.Size
329+
if validStart < 0 { validStart = 0 }
330+
331+
if start < validStart { start = validStart }
332+
328333
bestAlign := s.LastPageOffset
334+
found := false
329335
for _, po := range s.PageOffsets {
330-
if po >= start && po < bestAlign {
336+
// Find the smallest PO that is >= start AND is still valid
337+
if po >= start && po >= validStart && po < bestAlign {
331338
bestAlign = po
339+
found = true
332340
}
333341
}
334-
if bestAlign > 0 {
342+
if found {
343+
start = bestAlign
344+
} else if bestAlign >= validStart {
335345
start = bestAlign
346+
} else {
347+
start = s.Buffer.Head // Fallback to now if nothing valid found
336348
}
337349
}
338350

server/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,20 @@ func (s *Server) serveStreamData(w http.ResponseWriter, r *http.Request, stream
913913
defer stream.Unsubscribe(id)
914914

915915
if stream.OggHead != nil {
916-
// Always send stored headers for Ogg/Opus
916+
// If we are starting from the very beginning of the stream (or burst includes start)
917+
// we don't send OggHead because it's already in the buffer at OggHeaderOffset.
918+
// BUT for simplicity and robustness, we ALWAYS send OggHead first and then
919+
// ensure our offset is correctly aligned AFTER the headers in the buffer.
917920
if _, err := w.Write(stream.OggHead); err != nil {
918921
return false
919922
}
920-
// If the burst offset is BEFORE the header end, skip the duplicate headers in the buffer
923+
924+
// If our starting offset is before where the actual audio data starts,
925+
// skip the header part of the buffer to avoid sending them twice.
921926
if offset < stream.OggHeaderOffset {
922927
offset = stream.OggHeaderOffset
923928
}
929+
logrus.Debugf("Ogg Listener %s: Sending stored headers, then starting burst at %d (HeaderEnd: %d)", id, offset, stream.OggHeaderOffset)
924930
}
925931

926932
buf := make([]byte, 16384)

0 commit comments

Comments
 (0)