2019-04-16 10:15:44 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtcp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
rtcp.go contains structs to describe RTCP packets, and functionality to form
|
|
|
|
[]bytes of these structs.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
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
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-04-09 06:02:12 +03:00
|
|
|
package rtcp
|
|
|
|
|
|
|
|
import (
|
2019-04-10 08:49:45 +03:00
|
|
|
"encoding/binary"
|
2019-04-09 06:02:12 +03:00
|
|
|
)
|
|
|
|
|
2019-04-10 10:24:01 +03:00
|
|
|
// RTCP packet types.
|
|
|
|
const (
|
2019-04-13 13:18:20 +03:00
|
|
|
typeSenderReport = 200
|
2019-04-12 04:44:07 +03:00
|
|
|
typeReceiverReport = 201
|
|
|
|
typeSourceDescription = 202
|
|
|
|
)
|
|
|
|
|
|
|
|
// SDES Item types.
|
|
|
|
const (
|
|
|
|
typeCName = 1
|
2019-04-10 10:24:01 +03:00
|
|
|
)
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// MISC.
|
2019-04-10 08:49:45 +03:00
|
|
|
const (
|
2019-04-15 09:23:06 +03:00
|
|
|
reportBlockSize = 6
|
|
|
|
senderReportSize = 28
|
2019-04-10 08:49:45 +03:00
|
|
|
)
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// ReceiverReport describes an RTCP receiver report packet.
|
2019-04-10 08:49:45 +03:00
|
|
|
type ReceiverReport struct {
|
2019-04-15 07:31:38 +03:00
|
|
|
Header // Standard RTCP packet header.
|
|
|
|
SenderSSRC uint32 // SSRC of the sender of this report.
|
|
|
|
Blocks []ReportBlock // Report blocks.
|
|
|
|
Extensions [][4]byte // Contains any extensions to the packet.
|
2019-04-10 08:49:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// Bytes returns a []byte of the ReceiverReport r.
|
2019-04-15 10:26:52 +03:00
|
|
|
func (r *ReceiverReport) Bytes(buf []byte) []byte {
|
2019-04-10 08:49:45 +03:00
|
|
|
l := 8 + 4*reportBlockSize*len(r.Blocks) + 4*len(r.Extensions)
|
2019-04-15 10:26:52 +03:00
|
|
|
if buf == nil || cap(buf) < l {
|
|
|
|
buf = make([]byte, l)
|
|
|
|
}
|
|
|
|
buf = buf[:l]
|
2019-04-10 08:49:45 +03:00
|
|
|
l = 1 + reportBlockSize*len(r.Blocks) + len(r.Extensions)
|
2019-04-10 09:59:40 +03:00
|
|
|
r.writeHeader(buf, l)
|
2019-04-12 04:44:07 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[4:], r.SenderSSRC)
|
2019-04-10 08:49:45 +03:00
|
|
|
|
|
|
|
idx := 8
|
|
|
|
for _, b := range r.Blocks {
|
|
|
|
binary.BigEndian.PutUint32(buf[idx:], b.SSRC)
|
2019-04-16 10:46:13 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[idx+4:], b.PacketsLost)
|
|
|
|
buf[idx+4] = b.FractionLost
|
|
|
|
binary.BigEndian.PutUint32(buf[idx+8:], b.HighestSequence)
|
|
|
|
binary.BigEndian.PutUint32(buf[idx+12:], b.Jitter)
|
|
|
|
binary.BigEndian.PutUint32(buf[idx+16:], b.LSR)
|
|
|
|
binary.BigEndian.PutUint32(buf[idx+20:], b.DLSR)
|
|
|
|
idx += 24
|
2019-04-10 08:49:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range r.Extensions {
|
2019-04-10 09:59:40 +03:00
|
|
|
copy(buf[idx:], e[:])
|
2019-04-10 08:49:45 +03:00
|
|
|
idx += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// ReportBlock describes an RTCP report block used in Sender/Receiver Reports.
|
2019-04-13 13:18:20 +03:00
|
|
|
type ReportBlock struct {
|
|
|
|
SSRC uint32 // Source identifier.
|
|
|
|
FractionLost uint8 // Fraction of packets lost.
|
|
|
|
PacketsLost uint32 // Cumulative number of packets lost.
|
|
|
|
HighestSequence uint32 // Extended highest sequence number received.
|
|
|
|
Jitter uint32 // Interarrival jitter.
|
|
|
|
LSR uint32 // Last sender report timestamp.
|
|
|
|
DLSR uint32 // Delay since last sender report.
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// SourceDescription describes a source description RTCP packet.
|
2019-04-10 08:49:45 +03:00
|
|
|
type SourceDescription struct {
|
2019-04-15 07:31:38 +03:00
|
|
|
Header // Standard RTCP packet header.
|
|
|
|
Chunks []Chunk // Chunks to describe items of each SSRC.
|
2019-04-10 08:49:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// Bytes returns an []byte of the SourceDescription d.
|
2019-04-15 10:26:52 +03:00
|
|
|
func (d *SourceDescription) Bytes(buf []byte) []byte {
|
2019-04-10 09:59:40 +03:00
|
|
|
bodyLen := d.bodyLen()
|
2019-04-12 04:44:07 +03:00
|
|
|
rem := bodyLen % 4
|
|
|
|
if rem != 0 {
|
|
|
|
bodyLen += 4 - rem
|
|
|
|
}
|
2019-04-15 10:26:52 +03:00
|
|
|
|
2019-04-12 04:44:07 +03:00
|
|
|
l := 4 + bodyLen
|
2019-04-15 10:26:52 +03:00
|
|
|
if buf == nil || cap(buf) < l {
|
|
|
|
buf = make([]byte, l)
|
|
|
|
}
|
|
|
|
buf = buf[:l]
|
|
|
|
|
2019-04-10 09:59:40 +03:00
|
|
|
d.writeHeader(buf, bodyLen/4)
|
2019-04-12 04:44:07 +03:00
|
|
|
idx := 4
|
2019-04-10 09:59:40 +03:00
|
|
|
for _, c := range d.Chunks {
|
|
|
|
binary.BigEndian.PutUint32(buf[idx:], c.SSRC)
|
|
|
|
idx += 4
|
|
|
|
for _, i := range c.Items {
|
|
|
|
buf[idx] = i.Type
|
2019-04-16 10:46:13 +03:00
|
|
|
buf[idx+1] = byte(len(i.Text))
|
|
|
|
idx += 2
|
2019-04-10 09:59:40 +03:00
|
|
|
copy(buf[idx:], i.Text)
|
|
|
|
idx += len(i.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
2019-04-10 08:49:45 +03:00
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// bodyLen calculates the body length of a source description packet in bytes.
|
2019-04-10 09:59:40 +03:00
|
|
|
func (d *SourceDescription) bodyLen() int {
|
2019-04-19 07:45:43 +03:00
|
|
|
var l int
|
2019-04-10 09:59:40 +03:00
|
|
|
for _, c := range d.Chunks {
|
|
|
|
l += c.len()
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:23:06 +03:00
|
|
|
// SenderReport describes an RTCP sender report.
|
|
|
|
type SenderReport struct {
|
|
|
|
Header // Standard RTCP header.
|
|
|
|
SSRC uint32 // SSRC of sender.
|
|
|
|
TimestampMSW uint32 // Most significant word of timestamp.
|
|
|
|
TimestampLSW uint32 // Least significant word of timestamp.
|
|
|
|
RTPTimestamp uint32 // Current RTP timestamp.
|
|
|
|
PacketCount uint32 // Senders packet count.
|
|
|
|
OctetCount uint32 // Senders octet count.
|
|
|
|
|
|
|
|
// Report blocks (unimplemented)
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns a []byte of the SenderReport.
|
|
|
|
func (r *SenderReport) Bytes() []byte {
|
|
|
|
buf := make([]byte, senderReportSize)
|
|
|
|
r.writeHeader(buf, senderReportSize-1)
|
|
|
|
for i, w := range []uint32{
|
|
|
|
r.SSRC,
|
|
|
|
r.TimestampMSW,
|
|
|
|
r.TimestampLSW,
|
|
|
|
r.RTPTimestamp,
|
|
|
|
r.PacketCount,
|
|
|
|
r.OctetCount,
|
|
|
|
} {
|
2019-04-16 06:03:58 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[i+4:], w)
|
2019-04-15 09:23:06 +03:00
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// Header describes a standard RTCP packet header.
|
2019-04-10 09:59:40 +03:00
|
|
|
type Header struct {
|
2019-04-12 04:44:07 +03:00
|
|
|
Version uint8 // RTCP version.
|
|
|
|
Padding bool // Padding indicator.
|
|
|
|
ReportCount uint8 // Number of reports contained.
|
|
|
|
Type uint8 // Type of RTCP packet.
|
2019-04-10 09:59:40 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// SDESItem describes a source description item.
|
2019-04-10 09:59:40 +03:00
|
|
|
type SDESItem struct {
|
2019-04-15 07:31:38 +03:00
|
|
|
Type uint8 // Type of item.
|
|
|
|
Text []byte // Item text.
|
2019-04-10 09:59:40 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// Chunk describes a source description chunk for a given SSRC.
|
2019-04-10 09:59:40 +03:00
|
|
|
type Chunk struct {
|
2019-04-15 07:31:38 +03:00
|
|
|
SSRC uint32 // SSRC of the source being described by the below items.
|
|
|
|
Items []SDESItem // Items describing the source.
|
2019-04-10 09:59:40 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// len returns the len of a chunk in bytes.
|
2019-04-10 09:59:40 +03:00
|
|
|
func (c *Chunk) len() int {
|
|
|
|
tot := 4
|
|
|
|
for _, i := range c.Items {
|
|
|
|
tot += 2 + len(i.Text)
|
|
|
|
}
|
|
|
|
return tot
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:31:38 +03:00
|
|
|
// writeHeader writes the standard RTCP header given a buffer to write to and l
|
|
|
|
// the RTCP body length that needs to be encoded into the header.
|
2019-04-10 09:59:40 +03:00
|
|
|
func (h Header) writeHeader(buf []byte, l int) {
|
|
|
|
buf[0] = h.Version<<6 | asByte(h.Padding)<<5 | 0x1f&h.ReportCount
|
|
|
|
buf[1] = h.Type
|
|
|
|
binary.BigEndian.PutUint16(buf[2:], uint16(l))
|
2019-04-10 08:49:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func asByte(b bool) byte {
|
|
|
|
if b {
|
|
|
|
return 0x01
|
|
|
|
}
|
|
|
|
return 0x00
|
|
|
|
}
|