Skip to content

Commit 83bca34

Browse files
author
Datanoise
committed
fix: refactor Ogg alignment and header injection to ensure perfect bitstream continuity
1 parent 066a54f commit 83bca34

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

relay/relay.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ func (s *Stream) Subscribe(id string, burstSize int) (int64, chan struct{}) {
334334
validStart := s.Buffer.Head - s.Buffer.Size
335335
if validStart < 0 { validStart = 0 }
336336

337+
// If we have an OggHead persistent storage, we want to start reading
338+
// from the Buffer AFTER the initial headers to avoid duplicates.
339+
if s.OggHeaderOffset > start {
340+
start = s.OggHeaderOffset
341+
}
342+
337343
if start < validStart { start = validStart }
338344

339345
bestAlign := s.LastPageOffset
@@ -347,7 +353,7 @@ func (s *Stream) Subscribe(id string, burstSize int) (int64, chan struct{}) {
347353
}
348354
if found {
349355
start = bestAlign
350-
} else if bestAlign >= validStart {
356+
} else if bestAlign >= validStart && bestAlign > 0 {
351357
start = bestAlign
352358
} else {
353359
start = s.Buffer.Head // Fallback to now if nothing valid found

relay/transcode.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,17 @@ func (tm *TranscoderManager) encodeOpus(ctx context.Context, inst *TranscoderIns
227227
serial := uint32(time.Now().UnixNano())
228228
pw := ogg.NewPacketWriter(writer, serial)
229229

230-
// ID Header
230+
// 1. ID Header
231231
head := ogg.OpusHead{
232232
Version: 1,
233233
Channels: uint8(channels),
234234
InputSampleRate: uint32(sampleRate),
235235
}
236236
headPacket, _ := ogg.BuildOpusHeadPacket(head)
237237
pw.WritePacket(headPacket, 0, true, false)
238+
pw.Flush()
238239

239-
// Tags Header
240+
// 2. Tags Header
240241
tags := ogg.OpusTags{Vendor: "tinyice-opus"}
241242
tagsPacket, _ := ogg.BuildOpusTagsPacket(tags)
242243
pw.WritePacket(tagsPacket, 0, false, false)

server/server.go

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

915915
if stream.OggHead != nil {
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.
916+
// Always send stored headers for Ogg/Opus
920917
if _, err := w.Write(stream.OggHead); err != nil {
921918
return false
922919
}
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.
926-
if offset < stream.OggHeaderOffset {
927-
offset = stream.OggHeaderOffset
928-
}
929-
logrus.Debugf("Ogg Listener %s: Sending stored headers, then starting burst at %d (HeaderEnd: %d)", id, offset, stream.OggHeaderOffset)
920+
// The 'offset' returned by Subscribe is already intelligently aligned
921+
// AFTER the headers in the buffer if OggHeaderOffset was set.
922+
logrus.Debugf("Ogg Listener %s: Sending stored headers (%d bytes), then starting burst at %d", id, len(stream.OggHead), offset)
930923
}
931924

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

0 commit comments

Comments
 (0)