2018-11-16 12:05:19 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtp.go - provides a data structure intended to encapsulate the properties
|
|
|
|
of an rtp packet and also functions to allow manipulation of these packets.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
2018-11-21 05:37:01 +03:00
|
|
|
/*
|
|
|
|
See https://tools.ietf.org/html/rfc6184 and https://tools.ietf.org/html/rfc3550
|
|
|
|
for rtp-h264 and rtp standards.
|
|
|
|
*/
|
2018-11-16 12:05:19 +03:00
|
|
|
package rtp
|
|
|
|
|
|
|
|
const (
|
2018-12-27 07:14:30 +03:00
|
|
|
rtpVer = 2
|
|
|
|
headLen = 3 * 4
|
|
|
|
defPayloadLen = 7 * 188
|
|
|
|
defPktLen = headLen + defPayloadLen
|
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
|
2018-11-16 12:05:19 +03:00
|
|
|
type Pkt struct {
|
2018-11-17 09:22:57 +03:00
|
|
|
V byte // Version (currently 2)
|
2018-11-24 04:52:17 +03:00
|
|
|
p byte // Padding indicator (0 => padding, 1 => padding)
|
2018-11-17 09:22:57 +03:00
|
|
|
X byte // Extension header indicator
|
|
|
|
CC byte // CSRC count
|
|
|
|
M byte // Marker bit
|
|
|
|
PT byte // Packet type
|
2018-11-17 15:47:08 +03:00
|
|
|
SN uint16 // Synch number
|
|
|
|
TS uint32 // Timestamp
|
|
|
|
SSRC uint32 // Synchronisation source identifier
|
2018-11-17 09:22:57 +03:00
|
|
|
Payload []byte // H264 Payload data
|
2018-11-21 05:30:48 +03:00
|
|
|
Padding byte // No of bytes of padding
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:58:40 +03:00
|
|
|
// Bytes provides a byte slice of the packet
|
2018-12-27 07:14:30 +03:00
|
|
|
func (p *Pkt) Bytes(buf []byte) []byte {
|
|
|
|
if buf == nil || len(buf) != defPktLen {
|
|
|
|
buf = make([]byte, headLen, defPktLen)
|
|
|
|
}
|
|
|
|
buf = buf[:headLen]
|
2018-11-16 12:05:19 +03:00
|
|
|
if p.V == 0 {
|
|
|
|
p.V = rtpVer
|
|
|
|
}
|
|
|
|
|
2018-11-24 04:52:17 +03:00
|
|
|
if p.Padding > 0 {
|
|
|
|
p.p = 1
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.CC != 0 {
|
2018-11-22 12:02:31 +03:00
|
|
|
panic("CC has been set to something other than 0 - this is not supported yet.")
|
2018-11-16 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
2018-11-17 09:22:57 +03:00
|
|
|
if p.X != 0 {
|
2018-11-22 12:02:31 +03:00
|
|
|
panic("rtp: X (extension header indicator) not 0, but extensiion headers not currently supported.")
|
2018-11-17 09:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.CC != 0 {
|
2018-11-22 12:02:31 +03:00
|
|
|
panic("rtp: CC (CSRC count) not 0, but CSRC headers not yet supported.")
|
2018-11-17 09:22:57 +03:00
|
|
|
}
|
|
|
|
|
2018-11-24 04:52:17 +03:00
|
|
|
buf[0] = p.V<<6 | p.p<<5 | p.CC
|
2018-11-21 05:27:58 +03:00
|
|
|
buf[1] = p.M<<7 | p.PT
|
|
|
|
buf[2] = byte(p.SN >> 8)
|
|
|
|
buf[3] = byte(p.SN)
|
|
|
|
buf[4] = byte(p.TS >> 24)
|
|
|
|
buf[5] = byte(p.TS >> 16)
|
|
|
|
buf[6] = byte(p.TS >> 8)
|
|
|
|
buf[7] = byte(p.TS)
|
|
|
|
buf[8] = byte(p.SSRC >> 24)
|
|
|
|
buf[9] = byte(p.SSRC >> 16)
|
|
|
|
buf[10] = byte(p.SSRC >> 8)
|
|
|
|
buf[11] = byte(p.SSRC)
|
2018-11-17 09:47:54 +03:00
|
|
|
|
2018-11-16 12:05:19 +03:00
|
|
|
buf = append(buf, p.Payload...)
|
2018-11-21 05:37:01 +03:00
|
|
|
// see https://tools.ietf.org/html/rfc3550 section 5.1 (padding). At end of
|
|
|
|
// rtp packet, padding may exist, with the last octet being the length of the
|
|
|
|
// padding including itself.
|
2018-11-17 09:47:54 +03:00
|
|
|
if p.Padding != 0 {
|
2018-11-21 05:30:48 +03:00
|
|
|
buf = buf[:cap(buf)]
|
|
|
|
buf[len(buf)-1] = byte(p.Padding)
|
2018-11-17 09:47:54 +03:00
|
|
|
}
|
2018-11-16 12:05:19 +03:00
|
|
|
return buf
|
|
|
|
}
|