2019-04-12 09:59:35 +03:00
|
|
|
package rtcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-04-16 06:03:58 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-04-12 09:59:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
senderSSRC = 3605043418
|
|
|
|
defaultClientName = "client"
|
|
|
|
delayUnit = 1.0 / 65536.0
|
2019-04-16 06:03:58 +03:00
|
|
|
pkg = "rtcp: "
|
2019-04-12 09:59:35 +03:00
|
|
|
)
|
|
|
|
|
2019-04-16 06:03:58 +03:00
|
|
|
type log func(lvl int8, msg string, args ...interface{})
|
|
|
|
|
2019-04-12 09:59:35 +03:00
|
|
|
// client is an rtcp client that will hadle receiving SenderReports from a server
|
|
|
|
// and sending out ReceiverReports.
|
|
|
|
type client struct {
|
|
|
|
ErrChan chan error
|
|
|
|
cAddr *net.UDPAddr
|
|
|
|
sAddr *net.UDPAddr
|
|
|
|
name string
|
|
|
|
sourceSSRC uint32
|
|
|
|
mu sync.Mutex
|
|
|
|
sequence uint32
|
|
|
|
senderTs [64]byte
|
|
|
|
interval time.Duration
|
|
|
|
receiveTime time.Time
|
2019-04-15 10:26:52 +03:00
|
|
|
buf [200]byte
|
2019-04-16 06:03:58 +03:00
|
|
|
conn *net.UDPConn
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quitSend chan struct{}
|
|
|
|
quitRecv chan struct{}
|
|
|
|
log
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a pointer to a new client.
|
2019-04-16 06:03:58 +03:00
|
|
|
func NewClient(clientAddress, serverAddress, name string, sendInterval time.Duration, rtpSSRC uint32, l log) (*client, error) {
|
2019-04-12 09:59:35 +03:00
|
|
|
if name == "" {
|
|
|
|
name = defaultClientName
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &client{
|
|
|
|
name: name,
|
2019-04-16 06:03:58 +03:00
|
|
|
ErrChan: make(chan error, 2),
|
|
|
|
quitSend: make(chan struct{}),
|
|
|
|
quitRecv: make(chan struct{}),
|
2019-04-12 09:59:35 +03:00
|
|
|
interval: sendInterval,
|
|
|
|
sourceSSRC: rtpSSRC,
|
2019-04-16 06:03:58 +03:00
|
|
|
log: l,
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
c.cAddr, err = net.ResolveUDPAddr("udp", clientAddress)
|
|
|
|
if err != nil {
|
2019-04-16 06:03:58 +03:00
|
|
|
return nil, errors.New(fmt.Sprintf("can't resolve client address, failed with error: %v\n", err))
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c.sAddr, err = net.ResolveUDPAddr("udp", serverAddress)
|
|
|
|
if err != nil {
|
2019-04-16 06:03:58 +03:00
|
|
|
return nil, errors.New(fmt.Sprintf("can't resolve server address, failed with error: %v\n", err))
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
|
2019-04-16 06:03:58 +03:00
|
|
|
c.conn, err = net.DialUDP("udp", c.cAddr, c.sAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("can't dial, failed with error: %v\n", err))
|
|
|
|
}
|
2019-04-12 09:59:35 +03:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the listen and send routines. This will start the process of
|
2019-04-15 07:31:38 +03:00
|
|
|
// receiving and parsing sender reports, and the process of sending receiver
|
2019-04-12 09:59:35 +03:00
|
|
|
// reports to the server.
|
|
|
|
func (c *client) Start() {
|
2019-04-16 06:03:58 +03:00
|
|
|
c.log(logger.Debug, pkg+"client is starting")
|
|
|
|
c.wg.Add(1)
|
|
|
|
go c.recv()
|
|
|
|
c.wg.Add(1)
|
2019-04-12 09:59:35 +03:00
|
|
|
go c.send()
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:03:58 +03:00
|
|
|
// Stop sends a quit signal to the send and receive routines and closes the
|
|
|
|
// udp connection. It will wait until both routines have returned.
|
|
|
|
func (c *client) Stop() {
|
|
|
|
c.log(logger.Debug, pkg+"client is stopping")
|
|
|
|
close(c.quitSend)
|
|
|
|
close(c.quitRecv)
|
|
|
|
c.conn.Close()
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// listen reads from the UDP connection and parses SenderReports.
|
2019-04-16 06:03:58 +03:00
|
|
|
func (c *client) recv() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
c.log(logger.Debug, pkg+"client is receiving")
|
2019-04-12 09:59:35 +03:00
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
2019-04-16 06:03:58 +03:00
|
|
|
select {
|
|
|
|
case <-c.quitRecv:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
n, _, err := c.conn.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
|
|
|
c.ErrChan <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.log(logger.Debug, pkg+"sender report received", "report", buf[:n])
|
|
|
|
c.parse(buf[:n])
|
|
|
|
}
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// send writes receiver reports to the server.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) send() {
|
2019-04-16 06:03:58 +03:00
|
|
|
defer c.wg.Done()
|
|
|
|
c.log(logger.Debug, pkg+"client is sending")
|
2019-04-12 09:59:35 +03:00
|
|
|
for {
|
2019-04-16 06:03:58 +03:00
|
|
|
select {
|
|
|
|
case <-c.quitSend:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
time.Sleep(c.interval)
|
|
|
|
|
|
|
|
report := ReceiverReport{
|
|
|
|
Header: Header{
|
|
|
|
Version: 2,
|
|
|
|
Padding: false,
|
|
|
|
ReportCount: 1,
|
|
|
|
Type: typeReceiverReport,
|
2019-04-12 09:59:35 +03:00
|
|
|
},
|
2019-04-16 06:03:58 +03:00
|
|
|
SenderSSRC: senderSSRC,
|
|
|
|
Blocks: []ReportBlock{
|
|
|
|
ReportBlock{
|
|
|
|
SSRC: c.sourceSSRC,
|
|
|
|
FractionLost: 0,
|
|
|
|
PacketsLost: math.MaxUint32,
|
|
|
|
HighestSequence: c.highestSequence(),
|
|
|
|
Jitter: c.jitter(),
|
|
|
|
LSR: c.lastSenderTs(),
|
|
|
|
DLSR: c.delay(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extensions: nil,
|
|
|
|
}
|
2019-04-12 09:59:35 +03:00
|
|
|
|
2019-04-16 06:03:58 +03:00
|
|
|
description := SourceDescription{
|
|
|
|
Header: Header{
|
|
|
|
Version: 2,
|
|
|
|
Padding: false,
|
|
|
|
ReportCount: 1,
|
|
|
|
Type: typeSourceDescription,
|
|
|
|
},
|
|
|
|
Chunks: []Chunk{
|
|
|
|
Chunk{
|
|
|
|
SSRC: senderSSRC,
|
|
|
|
Items: []SDESItem{
|
|
|
|
SDESItem{
|
|
|
|
Type: typeCName,
|
|
|
|
Text: []byte(c.name),
|
|
|
|
},
|
2019-04-12 09:59:35 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-16 06:03:58 +03:00
|
|
|
}
|
2019-04-12 09:59:35 +03:00
|
|
|
|
2019-04-16 06:03:58 +03:00
|
|
|
c.log(logger.Debug, pkg+"sending receiver report")
|
|
|
|
_, err := c.conn.Write(c.formPayload(&report, &description))
|
|
|
|
if err != nil {
|
|
|
|
c.ErrChan <- err
|
|
|
|
}
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 11:05:36 +03:00
|
|
|
// formPayload takes a pointer to a ReceiverReport and a pointer to a
|
|
|
|
// Source Description and calls Bytes on both, writing to the underlying client
|
|
|
|
// buf. A slice to the combined writtem memory is returned.
|
2019-04-15 10:26:52 +03:00
|
|
|
func (c *client) formPayload(r *ReceiverReport, d *SourceDescription) []byte {
|
|
|
|
rl := len(r.Bytes(c.buf[:]))
|
|
|
|
dl := len(d.Bytes(c.buf[rl:]))
|
|
|
|
t := rl + dl
|
|
|
|
if t > cap(c.buf) {
|
|
|
|
panic("client buf not big enough")
|
|
|
|
}
|
|
|
|
return c.buf[:t]
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:59:35 +03:00
|
|
|
// parse will read important statistics from sender reports.
|
|
|
|
func (c *client) parse(buf []byte) {
|
|
|
|
c.received()
|
2019-04-13 13:18:20 +03:00
|
|
|
msw, lsw, err := Timestamp(buf)
|
2019-04-12 09:59:35 +03:00
|
|
|
if err != nil {
|
2019-04-13 13:18:20 +03:00
|
|
|
c.ErrChan <- errors.New(fmt.Sprintf("could not get timestamp from sender report, failed with error: %v", err))
|
2019-04-12 09:59:35 +03:00
|
|
|
}
|
|
|
|
c.setSenderTs(msw, lsw)
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// UpdateSequence will allow updating of the highest sequence number received
|
|
|
|
// through an rtp stream.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) UpdateSequence(s uint32) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.sequence = s
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// highestSequence will return the highest sequence number received through rtp.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) highestSequence() uint32 {
|
|
|
|
var s uint32
|
|
|
|
c.mu.Lock()
|
|
|
|
s = c.sequence
|
|
|
|
c.mu.Unlock()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// jitter returns the interarrival jitter as described by RTCP specifications:
|
|
|
|
// https://tools.ietf.org/html/rfc3550
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) jitter() uint32 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// setSenderTs allows us to safely set the current sender report timestamp.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) setSenderTs(msw, lsw uint32) {
|
|
|
|
c.mu.Lock()
|
|
|
|
binary.BigEndian.PutUint32(c.senderTs[:], msw)
|
|
|
|
binary.BigEndian.PutUint32(c.senderTs[4:], lsw)
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// lastSenderTs returns the timestamp of the most recent sender report.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) lastSenderTs() uint32 {
|
|
|
|
var ts uint32
|
|
|
|
c.mu.Lock()
|
|
|
|
ts = binary.BigEndian.Uint32(c.senderTs[2:])
|
|
|
|
c.mu.Unlock()
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// delay returns the duration between the receive time of the last sender report
|
|
|
|
// and now. This is called when forming a receiver report.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) delay() uint32 {
|
|
|
|
var receiveTime time.Time
|
|
|
|
c.mu.Lock()
|
|
|
|
receiveTime = c.receiveTime
|
|
|
|
c.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
return uint32(now.Sub(receiveTime).Seconds() / delayUnit)
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// received is called when a sender report is received to mark the receive time.
|
2019-04-12 09:59:35 +03:00
|
|
|
func (c *client) received() {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.receiveTime = time.Now()
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|