2018-11-16 12:05:19 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2020-04-23 06:43:37 +03:00
|
|
|
rtp.go
|
2018-11-16 12:05:19 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
2020-04-23 08:12:34 +03:00
|
|
|
See https://tools.ietf.org/html/rfc6184 and https://tools.ietf.org/html/rfc3550
|
2020-04-23 08:31:05 +03:00
|
|
|
for rtp-h264 and rtp standards.
|
2020-04-23 06:43:37 +03:00
|
|
|
|
2018-11-16 12:05:19 +03:00
|
|
|
AUTHOR
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
rtp.go is Copyright (C) 2018 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
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2020-04-23 06:43:37 +03:00
|
|
|
// Package rtp provides a data structure intended to encapsulate the properties
|
|
|
|
// of an rtp packet and also functions to allow manipulation of these packets.
|
2018-11-16 12:05:19 +03:00
|
|
|
package rtp
|
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
2018-11-16 12:05:19 +03:00
|
|
|
const (
|
2019-04-07 08:24:24 +03:00
|
|
|
rtpVer = 2 // Version of RTP that this package is compatible with.
|
|
|
|
defaultHeadSize = 12 // Header size of an rtp packet.
|
2019-04-05 06:59:04 +03:00
|
|
|
defPayloadSize = sendSize // Default payload size for the rtp packet.
|
|
|
|
defPktSize = defaultHeadSize + defPayloadSize // Default packet size is header size + payload size.
|
|
|
|
optionalFieldIdx = 12 // This is the idx of optional fields including CSRC and extension header in an RTP packet.
|
2018-11-16 12:05:19 +03:00
|
|
|
)
|
|
|
|
|
2018-11-21 12:58:40 +03:00
|
|
|
// Pkt provides fields consistent with RFC3550 definition of an rtp packet
|
2018-11-24 04:52:17 +03:00
|
|
|
// The padding indicator does not need to be set manually, only the padding length
|
2019-05-08 09:15:08 +03:00
|
|
|
type Packet struct {
|
2020-05-20 06:28:53 +03:00
|
|
|
Version uint8 // Version (currently 2).
|
|
|
|
PaddingFlag bool // Padding indicator (0 => padding, 1 => padding).
|
|
|
|
ExtHeadFlag bool // Extension header indicator.
|
|
|
|
CSRCCount uint8 // CSRC count.
|
|
|
|
Marker bool // Marker bit.
|
|
|
|
PacketType uint8 // Packet type.
|
|
|
|
Sync uint16 // Sync number.
|
|
|
|
Timestamp uint32 // Timestamp.
|
|
|
|
SSRC uint32 // Synchronisation source identifier.
|
|
|
|
CSRC [][4]byte // Contributing source identifier.
|
|
|
|
Extension ExtensionHeader // Header extension.
|
|
|
|
Payload []byte // Payload data.
|
|
|
|
Padding []byte // No of bytes of padding.
|
2019-03-29 08:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExtensionHeader header provides fields for an RTP packet extension header.
|
|
|
|
type ExtensionHeader struct {
|
|
|
|
ID uint16
|
|
|
|
Header [][4]byte
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:58:40 +03:00
|
|
|
// Bytes provides a byte slice of the packet
|
2019-05-08 09:15:08 +03:00
|
|
|
func (p *Packet) Bytes(buf []byte) []byte {
|
2019-03-29 08:18:34 +03:00
|
|
|
// Calculate the required length for the RTP packet.
|
|
|
|
headerExtensionLen := 0
|
2020-05-20 06:28:53 +03:00
|
|
|
if p.ExtHeadFlag {
|
2019-04-02 14:19:11 +03:00
|
|
|
headerExtensionLen = int(4 + 4*len(p.Extension.Header))
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
2020-05-20 06:28:53 +03:00
|
|
|
requiredPktLen := defaultHeadSize + int(4*p.CSRCCount) + headerExtensionLen + len(p.Payload) + len(p.Padding)
|
2018-11-16 12:05:19 +03:00
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
// Create new space if no buffer is given, or it doesn't have sufficient capacity.
|
2019-04-09 09:14:18 +03:00
|
|
|
if buf == nil || requiredPktLen > cap(buf) {
|
2019-03-29 08:18:34 +03:00
|
|
|
buf = make([]byte, requiredPktLen, defPktSize)
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
2019-03-29 08:18:34 +03:00
|
|
|
buf = buf[:requiredPktLen]
|
|
|
|
|
|
|
|
// Start encoding fields into the buffer.
|
2020-05-20 06:28:53 +03:00
|
|
|
buf[0] = p.Version<<6 | asByte(p.PaddingFlag)<<5 | asByte(p.ExtHeadFlag)<<4 | p.CSRCCount
|
|
|
|
buf[1] = asByte(p.Marker)<<7 | p.PacketType
|
|
|
|
binary.BigEndian.PutUint16(buf[2:4], p.Sync)
|
|
|
|
binary.BigEndian.PutUint32(buf[4:8], p.Timestamp)
|
2019-03-29 08:18:34 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[8:12], p.SSRC)
|
2018-11-16 12:05:19 +03:00
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
// If there is a CSRC count, add the CSRC to the buffer.
|
2020-05-20 06:28:53 +03:00
|
|
|
if p.CSRCCount != 0 {
|
|
|
|
if p.CSRCCount != uint8(len(p.CSRC)) {
|
2019-03-29 08:18:34 +03:00
|
|
|
panic("CSRC count in RTP packet is incorrect")
|
|
|
|
}
|
2020-05-20 06:28:53 +03:00
|
|
|
for i := 0; i < int(p.CSRCCount); i++ {
|
2019-03-29 08:18:34 +03:00
|
|
|
copy(buf[12+i*4:], p.CSRC[i][:])
|
|
|
|
}
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
// This is our current index for writing to the buffer.
|
2020-05-20 06:28:53 +03:00
|
|
|
idx := int(12 + 4*p.CSRCCount)
|
2019-03-29 08:18:34 +03:00
|
|
|
|
|
|
|
// If there is an extension field, add this to the buffer.
|
2020-05-20 06:28:53 +03:00
|
|
|
if p.ExtHeadFlag {
|
2019-03-29 08:18:34 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[idx:idx+2], p.Extension.ID)
|
|
|
|
idx += 2
|
2019-04-02 14:19:11 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[idx:idx+2], uint16(len(p.Extension.Header)))
|
2019-03-29 08:18:34 +03:00
|
|
|
idx += 2
|
2019-04-02 14:19:11 +03:00
|
|
|
for i := 0; i < len(p.Extension.Header); i++ {
|
2019-03-29 08:18:34 +03:00
|
|
|
copy(buf[idx+i*4:], p.Extension.Header[i][:])
|
|
|
|
}
|
2019-04-02 14:19:11 +03:00
|
|
|
idx += len(p.Extension.Header) * 4
|
2018-11-17 09:22:57 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
// If there is payload, add to the buffer.
|
|
|
|
if p.Payload != nil {
|
|
|
|
copy(buf[idx:], p.Payload)
|
|
|
|
idx += len(p.Payload)
|
2018-11-17 09:22:57 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 08:18:34 +03:00
|
|
|
// Finally, if there is padding, add to the buffer.
|
|
|
|
if p.Padding != nil {
|
|
|
|
copy(buf[idx:], p.Padding)
|
2018-11-17 09:47:54 +03:00
|
|
|
}
|
2019-03-29 08:18:34 +03:00
|
|
|
|
2018-11-16 12:05:19 +03:00
|
|
|
return buf
|
|
|
|
}
|
2019-03-29 08:18:34 +03:00
|
|
|
|
|
|
|
func asByte(b bool) byte {
|
|
|
|
if b {
|
|
|
|
return 0x01
|
|
|
|
}
|
|
|
|
return 0x00
|
|
|
|
}
|