Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions frame_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ func (fb *FrameBuffer) AddFramesToBuffer(startFrame uint32, endFrame uint32, dat
}

batchStartIndex := (startFrame * FrameSize) % BufferSize
batchEndIndex := (endFrame*FrameSize + FrameSize) % BufferSize // `+FrameSize` because `endFrame*FrameSize` defines where the last frame starts. But what we actually meed os where it ends. i.e. the position of the last byte of the frameBatch
batchEndIndex := (endFrame*FrameSize + FrameSize) % BufferSize // `+FrameSize` because `endFrame*FrameSize` defines where the last frame starts. But what we actually need is where it ends. i.e. the position of the last byte of the frameBatch

// Fix: Handle wrap-around case for batch size validation
var batchActualSize uint32
if batchEndIndex >= batchStartIndex {
batchActualSize = batchEndIndex - batchStartIndex
} else {
// Wrapped around: from start to end of buffer + from start of buffer to end index
batchActualSize = (BufferSize - batchStartIndex) + batchEndIndex
}

if batchEndIndex-batchStartIndex != BatchSize {
if batchActualSize != BatchSize {
return errors.New("received incorrect batch size")
}

Expand All @@ -42,7 +51,16 @@ func (fb *FrameBuffer) AddFramesToBuffer(startFrame uint32, endFrame uint32, dat
return errors.New("framebuffer length is 0")
}

copy(fb.buffer[batchStartIndex:batchEndIndex], data[:])
// Fix: Handle wrap-around case for copying data
if batchEndIndex >= batchStartIndex {
// Normal case: no wrap-around
copy(fb.buffer[batchStartIndex:batchEndIndex], data[:])
} else {
// Wrap-around case: copy in two parts
firstPartSize := BufferSize - batchStartIndex
copy(fb.buffer[batchStartIndex:], data[:firstPartSize])
copy(fb.buffer[:batchEndIndex], data[firstPartSize:])
}

fb.head = max(fb.head, batchEndIndex)
log.Println("startFramePosition", batchStartIndex)
Expand All @@ -57,10 +75,21 @@ func (fb *FrameBuffer) GetFrames() []byte {
if length == 0 {
return nil
}

// Fix: Handle wrap-around case properly
if fb.head >= fb.tail {
// Normal case: no wrap-around
return fb.buffer[fb.tail%BufferSize : fb.head%BufferSize]
} else {
// Wrap-around case: concatenate two parts
// From tail to end of buffer + from start of buffer to head
part1 := fb.buffer[fb.tail:]
part2 := fb.buffer[:fb.head]
result := make([]byte, len(part1)+len(part2))
copy(result, part1)
copy(result[len(part1):], part2)
return result
}
return fb.buffer[fb.head%BufferSize : fb.tail%BufferSize]
}

func (fb *FrameBuffer) RemoveSentFramesFromBuffer() {
Expand Down