mirror of
https://github.com/aler9/gortsplib
synced 2025-10-16 04:00:46 +08:00
move multibuffer into dedicated folder
This commit is contained in:
@@ -3,6 +3,8 @@ package gortsplib
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/aler9/gortsplib/multibuffer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type connClientUDPListener struct {
|
type connClientUDPListener struct {
|
||||||
@@ -10,7 +12,7 @@ type connClientUDPListener struct {
|
|||||||
remoteIp net.IP
|
remoteIp net.IP
|
||||||
remoteZone string
|
remoteZone string
|
||||||
remotePort int
|
remotePort int
|
||||||
udpFrameReadBuf *MultiBuffer
|
udpFrameReadBuf *multibuffer.MultiBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConnClientUDPListener(conf ConnClientConf, port int) (*connClientUDPListener, error) {
|
func newConnClientUDPListener(conf ConnClientConf, port int) (*connClientUDPListener, error) {
|
||||||
@@ -21,7 +23,7 @@ func newConnClientUDPListener(conf ConnClientConf, port int) (*connClientUDPList
|
|||||||
|
|
||||||
return &connClientUDPListener{
|
return &connClientUDPListener{
|
||||||
pc: pc,
|
pc: pc,
|
||||||
udpFrameReadBuf: NewMultiBuffer(conf.ReadBufferCount, 2048),
|
udpFrameReadBuf: multibuffer.New(conf.ReadBufferCount, 2048),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,68 +0,0 @@
|
|||||||
package gortsplib
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/aler9/gortsplib/base"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MultiBuffer implements software multi buffering, that allows to reuse
|
|
||||||
// existing buffers without creating new ones, increasing performance.
|
|
||||||
type MultiBuffer struct {
|
|
||||||
count int
|
|
||||||
buffers [][]byte
|
|
||||||
cur int
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMultiBuffer allocates a MultiBuffer.
|
|
||||||
func NewMultiBuffer(count int, size int) *MultiBuffer {
|
|
||||||
buffers := make([][]byte, count)
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
buffers[i] = make([]byte, size)
|
|
||||||
}
|
|
||||||
|
|
||||||
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.cur]
|
|
||||||
mb.cur += 1
|
|
||||||
if mb.cur >= mb.count {
|
|
||||||
mb.cur = 0
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
type multiFrame struct {
|
|
||||||
count int
|
|
||||||
frames []*base.InterleavedFrame
|
|
||||||
cur int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMultiFrame(count int, bufsize int) *multiFrame {
|
|
||||||
frames := make([]*base.InterleavedFrame, count)
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
frames[i] = &base.InterleavedFrame{
|
|
||||||
Content: make([]byte, 0, bufsize),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &multiFrame{
|
|
||||||
count: count,
|
|
||||||
frames: frames,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mf *multiFrame) next() *base.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
|
|
||||||
}
|
|
33
multibuffer/multibuffer.go
Normal file
33
multibuffer/multibuffer.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// Package multibuffer implements MultiBuffer.
|
||||||
|
package multibuffer
|
||||||
|
|
||||||
|
// MultiBuffer implements software multi buffering, that allows to reuse
|
||||||
|
// existing buffers without creating new ones, increasing performance.
|
||||||
|
type MultiBuffer struct {
|
||||||
|
count int
|
||||||
|
buffers [][]byte
|
||||||
|
cur int
|
||||||
|
}
|
||||||
|
|
||||||
|
// New allocates a MultiBuffer.
|
||||||
|
func New(count int, size int) *MultiBuffer {
|
||||||
|
buffers := make([][]byte, count)
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
buffers[i] = make([]byte, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.cur]
|
||||||
|
mb.cur += 1
|
||||||
|
if mb.cur >= mb.count {
|
||||||
|
mb.cur = 0
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
37
multiframe.go
Normal file
37
multiframe.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package gortsplib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/aler9/gortsplib/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
type multiFrame struct {
|
||||||
|
count int
|
||||||
|
frames []*base.InterleavedFrame
|
||||||
|
cur int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMultiFrame(count int, bufsize int) *multiFrame {
|
||||||
|
frames := make([]*base.InterleavedFrame, count)
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
frames[i] = &base.InterleavedFrame{
|
||||||
|
Content: make([]byte, 0, bufsize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &multiFrame{
|
||||||
|
count: count,
|
||||||
|
frames: frames,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mf *multiFrame) next() *base.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