From 4e7e779de7b5cb29798010aca0e7e58947fafb71 Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 16 Nov 2018 19:35:19 +1030 Subject: [PATCH] rtp: created rtp packet structure, wrote byte function (interprets packet structure and creates equivalent byte slice and started writing test utilities --- stream/rtp/encoder.go | 28 ++++++++++++++ stream/rtp/rtp.go | 87 ++++++++++++++++++++++++++++++++++++++++++ stream/rtp/rtp_test.go | 43 +++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 stream/rtp/encoder.go create mode 100644 stream/rtp/rtp.go create mode 100644 stream/rtp/rtp_test.go diff --git a/stream/rtp/encoder.go b/stream/rtp/encoder.go new file mode 100644 index 00000000..a5270a13 --- /dev/null +++ b/stream/rtp/encoder.go @@ -0,0 +1,28 @@ +/* +NAME + encoder.go + +DESCRIPTION + See Readme.md + +AUTHOR + Saxon Nelson-Milton (saxon@ausocean.org) + +LICENSE + encoder.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 http://www.gnu.org/licenses. +*/ + +package rtp diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go new file mode 100644 index 00000000..1ee1422f --- /dev/null +++ b/stream/rtp/rtp.go @@ -0,0 +1,87 @@ +/* +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 + +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). +*/ + +// See https://tools.ietf.org/html/rfc6184 for rtp packet format + +package rtp + +const ( + rtpVer = 2 + defaultHeadSize = 3 * 4 // bytes +) + +type Pkt struct { + V byte + P byte + X byte + CC byte + M byte + PT byte + SN int16 + TS int32 + SSRC int32 + Payload []byte + Padding int +} + +func (p *Pkt) Bytes() []byte { + if p.V == 0 { + p.V = rtpVer + } + + if p.P != 0 && p.Padding == 0 { + panic("Padding bit set to something other than 1, but there is no padding size defined!") + } else if p.P == 0 && p.Padding != 0 { + panic("Padding bit is set to zero, but it's indicated that there is padding!") + } + + if p.CC != 0 { + panic("CC has been set to something other than 0 - this is not supported yet!") + } + + buf := make([]byte, defaultHeadSize, defaultHeadSize+len(p.Payload)+p.Padding) + // First 4 bytes + buf[0] |= p.V<<6 | p.P<<5 | p.CC + buf[1] |= p.M<<7 | p.PT + buf[2] |= byte(p.SN >> 8) + buf[3] |= byte(p.SN) + // Second lot of 4 bytes + buf[4] |= byte(p.TS >> 24) + buf[5] |= byte(p.TS >> 16) + buf[6] |= byte(p.TS >> 8) + buf[7] |= byte(p.TS) + // Third lot of 4 bytes + buf[8] |= byte(p.SSRC >> 24) + buf[9] |= byte(p.SSRC >> 16) + buf[10] |= byte(p.SSRC >> 8) + buf[11] |= byte(p.SSRC) + // Add payload and padding + buf = append(buf, p.Payload...) + buf = append(buf, append(make([]byte, p.Padding-1, p.Padding), byte(p.Padding))...) + return buf +} diff --git a/stream/rtp/rtp_test.go b/stream/rtp/rtp_test.go new file mode 100644 index 00000000..04213c75 --- /dev/null +++ b/stream/rtp/rtp_test.go @@ -0,0 +1,43 @@ +/* +NAME + rtp_test.go + +DESCRIPTION + See Readme.md + +AUTHOR + Saxon Nelson-Milton (saxon@ausocean.org) + +LICENSE + rtp_test.go is Copyright (C) 2017 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 http://www.gnu.org/licenses. +*/ + +package rtp + +var rtpTests = []struct { + pkt Pkt + byteOutput []byte +}{ + { + pkt: Pkt{}, + }, + { + name: "null short", + input: []byte{0x00, 0x00, 0x01, 0x0}, + delay: 0, + want: [][]byte{{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x0}}, + }, +}