@@ -129,6 +129,8 @@ type Stream struct {
129129 OggHead []byte // Store Ogg headers for Opus/Ogg streams
130130 OggHeaderOffset int64 // Absolute buffer offset where headers end
131131 LastPageOffset int64 // Absolute offset of the last valid Ogg page start
132+ PageOffsets []int64 // Circular list of last ~100 page starts
133+ PageIndex int
132134
133135 Buffer * CircularBuffer
134136 listeners map [string ]chan struct {} // Signal channel for new data
@@ -202,6 +204,7 @@ func (r *Relay) GetOrCreateStream(mount string) *Stream {
202204 ContentType : "audio/mpeg" ,
203205 Enabled : true ,
204206 CurrentSong : "N/A" ,
207+ PageOffsets : make ([]int64 , 128 ), // Track last 128 Ogg pages
205208 }
206209 r .Streams [mount ] = s
207210 return s
@@ -280,7 +283,10 @@ func (s *Stream) Broadcast(data []byte, relay *Relay) {
280283 if isOgg {
281284 for i := 0 ; i <= len (data )- 4 ; i ++ {
282285 if data [i ] == 'O' && data [i + 1 ] == 'g' && data [i + 2 ] == 'g' && data [i + 3 ] == 'S' {
283- s .LastPageOffset = s .Buffer .Head + int64 (i )
286+ offset := s .Buffer .Head + int64 (i )
287+ s .LastPageOffset = offset
288+ s .PageOffsets [s .PageIndex % len (s .PageOffsets )] = offset
289+ s .PageIndex ++
284290 }
285291 }
286292 }
@@ -317,10 +323,16 @@ func (s *Stream) Subscribe(id string, burstSize int) (int64, chan struct{}) {
317323 start = 0
318324 }
319325
320- // For Ogg/Opus, align to the last known page boundary if we are within the burst
326+ // For Ogg/Opus, align to the oldest known page boundary within the burst
321327 if strings .Contains (strings .ToLower (s .ContentType ), "ogg" ) || strings .Contains (strings .ToLower (s .ContentType ), "opus" ) {
322- if s .LastPageOffset > start {
323- start = s .LastPageOffset
328+ bestAlign := s .LastPageOffset
329+ for _ , po := range s .PageOffsets {
330+ if po >= start && po < bestAlign {
331+ bestAlign = po
332+ }
333+ }
334+ if bestAlign > 0 {
335+ start = bestAlign
324336 }
325337 }
326338
0 commit comments