2019-04-19 11:47:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
client.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-04-19 12:10:15 +03:00
|
|
|
client.go provides an RTP client, Client, that will receive RTP on a UDP
|
|
|
|
connection, perform an operation on the packets and then store in a ringBuffer
|
|
|
|
that can be read from Read, an implemntation of io.Reader.
|
2019-04-19 11:47:06 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-04-19 12:10:15 +03:00
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
2019-04-19 11:47:06 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2019-04-19 12:10:15 +03:00
|
|
|
for more details.
|
2019-04-19 11:47:06 +03:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2019-04-19 12:10:15 +03:00
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2019-04-19 11:47:06 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
package rtp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2019-04-21 16:10:08 +03:00
|
|
|
"time"
|
2019-04-19 11:47:06 +03:00
|
|
|
|
2019-04-21 16:10:08 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-04-19 11:47:06 +03:00
|
|
|
"bitbucket.org/ausocean/utils/ring"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
chanSize = 10
|
2019-04-21 16:10:08 +03:00
|
|
|
pkg = "rtp: "
|
2019-04-19 11:47:06 +03:00
|
|
|
)
|
|
|
|
|
2019-04-20 14:35:28 +03:00
|
|
|
// RingBuffer consts.
|
|
|
|
const (
|
2019-05-01 07:45:39 +03:00
|
|
|
ringBufferSize = 100
|
2019-04-20 14:35:28 +03:00
|
|
|
ringBufferElementSize = 4096
|
|
|
|
)
|
|
|
|
|
2019-04-21 16:10:08 +03:00
|
|
|
type log func(lvl int8, msg string, args ...interface{})
|
|
|
|
|
2019-04-19 12:10:15 +03:00
|
|
|
// Client describes an RTP client that can receive an RTP stream and implements
|
|
|
|
// io.Reader.
|
2019-04-19 11:47:06 +03:00
|
|
|
type Client struct {
|
2019-05-01 07:45:39 +03:00
|
|
|
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.
|
|
|
|
ring *ring.Buffer // Processed data from RTP packets will be stored here.
|
|
|
|
err chan error // Errors encountered during recv will be sent to this chan.
|
|
|
|
rt time.Duration // Read timeout used when reading from the ringbuffer.
|
2019-04-21 16:10:08 +03:00
|
|
|
log
|
2019-04-19 11:47:06 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 12:10:15 +03:00
|
|
|
// NewClient returns a pointer to a new Client.
|
|
|
|
//
|
|
|
|
// addr is the address of form <ip>:<port> that we expect to receive
|
|
|
|
// RTP at. op is a function, if non nil, that will be used to perform an
|
|
|
|
// operation on each received RTP packet. For example, the op func may parse
|
|
|
|
// out the RTP payload. The result of the operation is then what is stored
|
2019-04-21 16:10:08 +03:00
|
|
|
// 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
|
|
|
|
// reading from the client ringbuffer.
|
2019-05-01 07:45:39 +03:00
|
|
|
func NewClient(addr string, l log, rt time.Duration) (*Client, error) {
|
2019-04-19 11:47:06 +03:00
|
|
|
c := &Client{
|
2019-04-29 20:07:18 +03:00
|
|
|
done: make(chan struct{}, chanSize),
|
|
|
|
ring: ring.NewBuffer(ringBufferSize, ringBufferElementSize, 0),
|
|
|
|
log: l,
|
|
|
|
err: make(chan error),
|
|
|
|
rt: rt,
|
2019-04-19 11:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
a, err := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.conn, err = net.ListenUDP("udp", a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:10:15 +03:00
|
|
|
// Start will start the recv routine.
|
2019-04-19 11:47:06 +03:00
|
|
|
func (c *Client) Start() {
|
2019-04-21 16:10:08 +03:00
|
|
|
c.log(logger.Info, pkg+"starting client")
|
2019-04-19 11:47:06 +03:00
|
|
|
c.wg.Add(1)
|
|
|
|
go c.recv()
|
|
|
|
}
|
|
|
|
|
2019-04-21 16:10:08 +03:00
|
|
|
// Stop will send a done signal to the receive routine, and also close the
|
|
|
|
// connection.
|
|
|
|
func (c *Client) Stop() {
|
|
|
|
c.log(logger.Info, pkg+"stopping client")
|
|
|
|
c.conn.Close()
|
|
|
|
close(c.done)
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:08:38 +03:00
|
|
|
// Err returns the client err channel as receive only.
|
2019-04-29 20:07:18 +03:00
|
|
|
func (c *Client) Err() <-chan error {
|
|
|
|
return c.err
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:10:15 +03:00
|
|
|
// recv will read RTP packets from the UDP connection, perform the operation
|
|
|
|
// on them given at creation of the Client and then store the result in the
|
|
|
|
// ringBuffer for Reading.
|
2019-04-19 11:47:06 +03:00
|
|
|
func (c *Client) recv() {
|
|
|
|
defer c.wg.Done()
|
2019-04-20 14:35:28 +03:00
|
|
|
buf := make([]byte, ringBufferElementSize)
|
2019-04-19 11:47:06 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.done:
|
2019-04-21 16:10:08 +03:00
|
|
|
c.log(logger.Debug, pkg+"done signal received")
|
2019-04-19 11:47:06 +03:00
|
|
|
return
|
|
|
|
default:
|
2019-04-21 16:10:08 +03:00
|
|
|
c.log(logger.Debug, pkg+"waiting for packet")
|
2019-04-19 11:47:06 +03:00
|
|
|
n, _, err := c.conn.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
2019-04-29 20:07:18 +03:00
|
|
|
c.err <- err
|
2019-04-19 11:47:06 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-04-21 16:10:08 +03:00
|
|
|
c.log(logger.Debug, pkg+"packet received", "packet", buf[:n])
|
2019-04-20 14:35:28 +03:00
|
|
|
|
2019-05-01 07:45:39 +03:00
|
|
|
_, err = c.ring.Write(buf[:n])
|
2019-04-19 11:47:06 +03:00
|
|
|
c.ring.Flush()
|
|
|
|
if err != nil {
|
2019-04-29 20:07:18 +03:00
|
|
|
c.err <- err
|
2019-04-19 11:47:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:10:15 +03:00
|
|
|
// Read implements io.Reader.
|
|
|
|
//
|
|
|
|
// Read will get the next chunk from the ringBuffer and copy the bytes to p.
|
2019-04-19 11:47:06 +03:00
|
|
|
func (c *Client) Read(p []byte) (int, error) {
|
2019-04-21 16:37:12 +03:00
|
|
|
c.log(logger.Debug, pkg+"user reading data")
|
2019-04-21 16:10:08 +03:00
|
|
|
chunk, err := c.ring.Next(c.rt)
|
2019-04-19 11:47:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n := copy(p, chunk.Bytes())
|
|
|
|
chunk.Close()
|
|
|
|
chunk = nil
|
|
|
|
return n, nil
|
|
|
|
}
|