protocol/rtp: removed op from Client i.e. what is read from Client are RTP packets.

This commit is contained in:
Saxon 2019-05-01 14:15:39 +09:30
parent d358f70585
commit 80a7d41d8a
2 changed files with 85 additions and 107 deletions

View File

@ -47,7 +47,7 @@ const (
// RingBuffer consts. // RingBuffer consts.
const ( const (
ringBufferSize = 10 ringBufferSize = 100
ringBufferElementSize = 4096 ringBufferElementSize = 4096
) )
@ -60,7 +60,6 @@ type Client struct {
wg sync.WaitGroup // Used to wait for recv routine to finish. 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. 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.
err chan error // Errors encountered during recv will be sent to this chan. err chan error // Errors encountered during recv will be sent to this chan.
rt time.Duration // Read timeout used when reading from the ringbuffer. rt time.Duration // Read timeout used when reading from the ringbuffer.
log log
@ -75,11 +74,10 @@ type Client struct {
// in the ringBuffer for reading. l is a logging function defined by the // in the ringBuffer for reading. l is a logging function defined by the
// signuture of the log type defined above. rt is the read timeout used when // signuture of the log type defined above. rt is the read timeout used when
// reading from the client ringbuffer. // reading from the client ringbuffer.
func NewClient(addr string, op func([]byte) ([]byte, error), l log, rt time.Duration) (*Client, error) { func NewClient(addr string, l log, rt time.Duration) (*Client, error) {
c := &Client{ c := &Client{
done: make(chan struct{}, chanSize), done: make(chan struct{}, chanSize),
ring: ring.NewBuffer(ringBufferSize, ringBufferElementSize, 0), ring: ring.NewBuffer(ringBufferSize, ringBufferElementSize, 0),
op: op,
log: l, log: l,
err: make(chan error), err: make(chan error),
rt: rt, rt: rt,
@ -139,19 +137,7 @@ func (c *Client) recv() {
} }
c.log(logger.Debug, pkg+"packet received", "packet", buf[:n]) c.log(logger.Debug, pkg+"packet received", "packet", buf[:n])
var _buf []byte _, err = c.ring.Write(buf[:n])
switch c.op {
case nil:
_buf = buf[:n]
default:
_buf, err = c.op(buf[:n])
if err != nil {
c.err <- err
continue
}
}
_, err = c.ring.Write(_buf)
c.ring.Flush() c.ring.Flush()
if err != nil { if err != nil {
c.err <- err c.err <- err

View File

@ -75,7 +75,6 @@ func TestReceive(t *testing.T) {
packetsToSend = 20 packetsToSend = 20
) )
for _, op := range []func([]byte) ([]byte, error){nil, Payload} {
testErr := make(chan error) testErr := make(chan error)
serverErr := make(chan error) serverErr := make(chan error)
done := make(chan struct{}) done := make(chan struct{})
@ -86,7 +85,7 @@ func TestReceive(t *testing.T) {
go func() { go func() {
// Create and start the client. // Create and start the client.
var err error var err error
c, err = NewClient(clientAddr, op, (*dummyLogger)(t).log, 1*time.Millisecond) c, err = NewClient(clientAddr, (*dummyLogger)(t).log, 1*time.Millisecond)
if err != nil { if err != nil {
testErr <- fmt.Errorf("could not create client, failed with error: %v\n", err) testErr <- fmt.Errorf("could not create client, failed with error: %v\n", err)
} }
@ -108,12 +107,6 @@ func TestReceive(t *testing.T) {
// Create expected data and apply operation if there is one. // Create expected data and apply operation if there is one.
expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil) expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
if op != nil {
expect, err = op(expect)
if err != nil {
testErr <- fmt.Errorf("unexpected error when applying op: %v\n", err)
}
}
// Compare. // Compare.
got := buf[:n] got := buf[:n]
@ -165,4 +158,3 @@ func TestReceive(t *testing.T) {
} }
} }
} }
}