From 7ad47b8b307f888741ca74cc0ce2144c5cd954d0 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 4 Oct 2020 17:43:13 +0200 Subject: [PATCH] move multibuffer into dedicated folder --- connclientudpl.go | 6 ++-- multibuffer.go | 68 -------------------------------------- multibuffer/multibuffer.go | 33 ++++++++++++++++++ multiframe.go | 37 +++++++++++++++++++++ 4 files changed, 74 insertions(+), 70 deletions(-) delete mode 100644 multibuffer.go create mode 100644 multibuffer/multibuffer.go create mode 100644 multiframe.go diff --git a/connclientudpl.go b/connclientudpl.go index e705c748..c390e2ff 100644 --- a/connclientudpl.go +++ b/connclientudpl.go @@ -3,6 +3,8 @@ package gortsplib import ( "net" "strconv" + + "github.com/aler9/gortsplib/multibuffer" ) type connClientUDPListener struct { @@ -10,7 +12,7 @@ type connClientUDPListener struct { remoteIp net.IP remoteZone string remotePort int - udpFrameReadBuf *MultiBuffer + udpFrameReadBuf *multibuffer.MultiBuffer } func newConnClientUDPListener(conf ConnClientConf, port int) (*connClientUDPListener, error) { @@ -21,7 +23,7 @@ func newConnClientUDPListener(conf ConnClientConf, port int) (*connClientUDPList return &connClientUDPListener{ pc: pc, - udpFrameReadBuf: NewMultiBuffer(conf.ReadBufferCount, 2048), + udpFrameReadBuf: multibuffer.New(conf.ReadBufferCount, 2048), }, nil } diff --git a/multibuffer.go b/multibuffer.go deleted file mode 100644 index 94561441..00000000 --- a/multibuffer.go +++ /dev/null @@ -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 -} diff --git a/multibuffer/multibuffer.go b/multibuffer/multibuffer.go new file mode 100644 index 00000000..1aee2988 --- /dev/null +++ b/multibuffer/multibuffer.go @@ -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 +} diff --git a/multiframe.go b/multiframe.go new file mode 100644 index 00000000..9ad5f090 --- /dev/null +++ b/multiframe.go @@ -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 +}