2019-04-19 11:47:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
client.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-05-08 10:27:07 +03:00
|
|
|
client.go provides an RTP client.
|
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 (
|
2019-10-22 15:34:33 +03:00
|
|
|
"fmt"
|
2019-04-19 11:47:06 +03:00
|
|
|
"net"
|
2019-05-20 11:44:23 +03:00
|
|
|
"sync"
|
2019-10-22 15:34:33 +03:00
|
|
|
"time"
|
2019-04-20 14:35:28 +03:00
|
|
|
)
|
|
|
|
|
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-20 11:44:23 +03:00
|
|
|
r *PacketReader
|
|
|
|
ssrc uint32
|
|
|
|
mu sync.Mutex
|
|
|
|
sequence uint16
|
|
|
|
cycles uint16
|
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
|
2019-05-08 10:24:02 +03:00
|
|
|
// RTP at.
|
|
|
|
func NewClient(addr string) (*Client, error) {
|
2019-05-11 15:34:37 +03:00
|
|
|
c := &Client{r: &PacketReader{}}
|
2019-04-19 11:47:06 +03:00
|
|
|
|
|
|
|
a, err := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-11 15:31:03 +03:00
|
|
|
c.r.PacketConn, err = net.ListenUDP("udp", a)
|
2019-04-19 11:47:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-22 15:34:33 +03:00
|
|
|
|
2019-04-19 11:47:06 +03:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 11:44:23 +03:00
|
|
|
// SSRC returns the identified for the source from which the RTP packets being
|
|
|
|
// received are coming from.
|
|
|
|
func (c *Client) SSRC() uint32 {
|
|
|
|
return c.ssrc
|
|
|
|
}
|
|
|
|
|
2019-05-11 15:31:03 +03:00
|
|
|
// Read implements io.Reader.
|
2019-04-19 11:47:06 +03:00
|
|
|
func (c *Client) Read(p []byte) (int, error) {
|
2019-05-20 11:44:23 +03:00
|
|
|
n, err := c.r.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2019-05-20 14:19:50 +03:00
|
|
|
if c.ssrc == 0 {
|
|
|
|
c.ssrc, _ = SSRC(p[:n])
|
|
|
|
}
|
2019-05-20 11:44:23 +03:00
|
|
|
s, _ := Sequence(p[:n])
|
|
|
|
c.setSequence(s)
|
2019-05-20 14:19:50 +03:00
|
|
|
return n, err
|
2019-05-11 15:31:03 +03:00
|
|
|
}
|
|
|
|
|
2019-10-22 15:34:33 +03:00
|
|
|
// Close will close the RTP client's connection.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.r.PacketConn.Close()
|
|
|
|
}
|
|
|
|
|
2019-05-20 11:44:23 +03:00
|
|
|
// setSequence sets the most recently received sequence number, and updates the
|
|
|
|
// cycles count if the sequence number has rolled over.
|
|
|
|
func (c *Client) setSequence(s uint16) {
|
|
|
|
c.mu.Lock()
|
|
|
|
if s < c.sequence {
|
|
|
|
c.cycles++
|
|
|
|
}
|
|
|
|
c.sequence = s
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sequence returns the most recent RTP packet sequence number received.
|
|
|
|
func (c *Client) Sequence() uint16 {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.sequence
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cycles returns the number of RTP sequence number cycles that have been received.
|
|
|
|
func (c *Client) Cycles() uint16 {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.cycles
|
|
|
|
}
|
|
|
|
|
2019-05-13 07:33:11 +03:00
|
|
|
// PacketReader provides an io.Reader interface to an underlying UDP PacketConn.
|
2019-05-11 15:31:03 +03:00
|
|
|
type PacketReader struct {
|
|
|
|
net.PacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements io.Reader.
|
|
|
|
func (r PacketReader) Read(b []byte) (int, error) {
|
2019-10-22 15:34:33 +03:00
|
|
|
const readTimeout = time.Second
|
|
|
|
err := r.PacketConn.SetReadDeadline(time.Now().Add(readTimeout))
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not set read deadline for PacketConn: %w", err)
|
|
|
|
}
|
2019-05-11 15:31:03 +03:00
|
|
|
n, _, err := r.PacketConn.ReadFrom(b)
|
|
|
|
return n, err
|
2019-04-19 11:47:06 +03:00
|
|
|
}
|