mirror of
https://github.com/aler9/gortsplib
synced 2025-10-09 17:10:06 +08:00
improve performance
This commit is contained in:
@@ -3,8 +3,9 @@ package gortsplib
|
||||
// MultiBuffer implements software multi buffering, that allows to reuse
|
||||
// existing buffers without creating new ones, increasing performance.
|
||||
type MultiBuffer struct {
|
||||
count int
|
||||
buffers [][]byte
|
||||
curBuf int
|
||||
cur int
|
||||
}
|
||||
|
||||
// NewMultiBuffer allocates a MultiBuffer.
|
||||
@@ -15,16 +16,49 @@ func NewMultiBuffer(count int, size int) *MultiBuffer {
|
||||
}
|
||||
|
||||
return &MultiBuffer{
|
||||
count: count,
|
||||
buffers: buffers,
|
||||
}
|
||||
}
|
||||
|
||||
// Next gets the current buffer and sets the next buffer as the current one.
|
||||
func (mb *MultiBuffer) Next() []byte {
|
||||
ret := mb.buffers[mb.curBuf]
|
||||
mb.curBuf += 1
|
||||
if mb.curBuf >= len(mb.buffers) {
|
||||
mb.curBuf = 0
|
||||
ret := mb.buffers[mb.cur]
|
||||
mb.cur += 1
|
||||
if mb.cur >= mb.count {
|
||||
mb.cur = 0
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
type multiFrame struct {
|
||||
count int
|
||||
frames []*InterleavedFrame
|
||||
cur int
|
||||
}
|
||||
|
||||
func newMultiFrame(count int, bufsize int) *multiFrame {
|
||||
frames := make([]*InterleavedFrame, count)
|
||||
for i := 0; i < count; i++ {
|
||||
frames[i] = &InterleavedFrame{
|
||||
Content: make([]byte, 0, bufsize),
|
||||
}
|
||||
}
|
||||
|
||||
return &multiFrame{
|
||||
count: count,
|
||||
frames: frames,
|
||||
}
|
||||
}
|
||||
|
||||
func (mf *multiFrame) next() *InterleavedFrame {
|
||||
ret := mf.frames[mf.cur]
|
||||
mf.cur += 1
|
||||
if mf.cur >= mf.count {
|
||||
mf.cur = 0
|
||||
}
|
||||
|
||||
ret.Content = ret.Content[:cap(ret.Content)]
|
||||
|
||||
return ret
|
||||
}
|
||||
|
Reference in New Issue
Block a user