protocol/rtp: consts for chan and ringbuffer sizes

This commit is contained in:
Saxon 2019-04-20 21:05:28 +09:30
parent 190d546c58
commit 00db293b44
1 changed files with 13 additions and 4 deletions

View File

@ -37,16 +37,23 @@ import (
"bitbucket.org/ausocean/utils/ring"
)
// Misc consts.
const (
chanSize = 10
)
// RingBuffer consts.
const (
ringBufferSize = 10
ringBufferElementSize = 4096
)
// Client describes an RTP client that can receive an RTP stream and implements
// io.Reader.
type Client struct {
conn *net.UDPConn // The UDP connection RTP packets will be read from.
wg sync.WaitGroup // Used to wait for recv routine to finish.
done chan struct{} // Used to terminate the recv routine.
done chan struct{} // Used to terminate the recv routine.
ring *ring.Buffer // Processed data from RTP packets will be stored here.
op func([]byte) ([]byte, error) // The operation to perform on received RTP packets before storing.
ErrChan chan error // Errors encountered during recv will be sent to this chan.
@ -61,8 +68,8 @@ type Client struct {
// in the ringBuffer for reading.
func NewClient(addr string, op func([]byte) ([]byte, error)) (*Client, error) {
c := &Client{
done: make(chan struct{}, 10),
ring: ring.NewBuffer(10, 4096, 0),
done: make(chan struct{}, chanSize),
ring: ring.NewBuffer(ringBufferSize, ringBufferElementSize, 0),
op: op,
}
@ -90,7 +97,7 @@ func (c *Client) Start() {
// ringBuffer for Reading.
func (c *Client) recv() {
defer c.wg.Done()
buf := make([]byte, 4096)
buf := make([]byte, ringBufferElementSize)
for {
select {
case <-c.done:
@ -101,6 +108,7 @@ func (c *Client) recv() {
c.ErrChan <- err
continue
}
var _buf []byte
switch c.op {
case nil:
@ -112,6 +120,7 @@ func (c *Client) recv() {
continue
}
}
_, err = c.ring.Write(_buf)
c.ring.Flush()
if err != nil {