1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
package rtp
import (
"encoding/binary"
"fmt"
"io"
)
const (
headerExtensionProfileOneByte = 0xBEDE
headerExtensionProfileTwoByte = 0x1000
headerExtensionIDReserved = 0xF
)
// HeaderExtension represents an RTP extension header.
type HeaderExtension interface {
Set(id uint8, payload []byte) error
GetIDs() []uint8
Get(id uint8) []byte
Del(id uint8) error
Unmarshal(buf []byte) (int, error)
Marshal() ([]byte, error)
MarshalTo(buf []byte) (int, error)
MarshalSize() int
}
// OneByteHeaderExtension is an RFC8285 one-byte header extension.
type OneByteHeaderExtension struct {
payload []byte
}
// Set sets the extension payload for the specified ID.
func (e *OneByteHeaderExtension) Set(id uint8, buf []byte) error {
if id < 1 || id > 14 {
return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderIDRange, id)
}
if len(buf) > 16 {
return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderSize, len(buf))
}
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n] >> 4
len := int(e.payload[n]&^0xF0 + 1)
n++
if extid == id {
e.payload = append(e.payload[:n+1], append(buf, e.payload[n+1+len:]...)...)
return nil
}
n += len
}
e.payload = append(e.payload, (id<<4 | uint8(len(buf)-1)))
e.payload = append(e.payload, buf...)
binary.BigEndian.PutUint16(e.payload[2:4], binary.BigEndian.Uint16(e.payload[2:4])+1)
return nil
}
// GetIDs returns the available IDs.
func (e *OneByteHeaderExtension) GetIDs() []uint8 {
ids := make([]uint8, 0, binary.BigEndian.Uint16(e.payload[2:4]))
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n] >> 4
len := int(e.payload[n]&^0xF0 + 1)
n++
if extid == headerExtensionIDReserved {
break
}
ids = append(ids, extid)
n += len
}
return ids
}
// Get returns the payload of the extension with the given ID.
func (e *OneByteHeaderExtension) Get(id uint8) []byte {
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n] >> 4
len := int(e.payload[n]&^0xF0 + 1)
n++
if extid == id {
return e.payload[n : n+len]
}
n += len
}
return nil
}
// Del deletes the extension with the specified ID.
func (e *OneByteHeaderExtension) Del(id uint8) error {
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n] >> 4
len := int(e.payload[n]&^0xF0 + 1)
if extid == id {
e.payload = append(e.payload[:n], e.payload[n+1+len:]...)
return nil
}
n += len + 1
}
return errHeaderExtensionNotFound
}
// Unmarshal parses the extension payload.
func (e *OneByteHeaderExtension) Unmarshal(buf []byte) (int, error) {
profile := binary.BigEndian.Uint16(buf[0:2])
if profile != headerExtensionProfileOneByte {
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
}
e.payload = buf
return len(buf), nil
}
// Marshal returns the extension payload.
func (e OneByteHeaderExtension) Marshal() ([]byte, error) {
return e.payload, nil
}
// MarshalTo writes the extension payload to the given buffer.
func (e OneByteHeaderExtension) MarshalTo(buf []byte) (int, error) {
size := e.MarshalSize()
if size > len(buf) {
return 0, io.ErrShortBuffer
}
return copy(buf, e.payload), nil
}
// MarshalSize returns the size of the extension payload.
func (e OneByteHeaderExtension) MarshalSize() int {
return len(e.payload)
}
// TwoByteHeaderExtension is an RFC8285 two-byte header extension.
type TwoByteHeaderExtension struct {
payload []byte
}
// Set sets the extension payload for the specified ID.
func (e *TwoByteHeaderExtension) Set(id uint8, buf []byte) error {
if id < 1 || id > 255 {
return fmt.Errorf("%w actual(%d)", errRFC8285TwoByteHeaderIDRange, id)
}
if len(buf) > 255 {
return fmt.Errorf("%w actual(%d)", errRFC8285TwoByteHeaderSize, len(buf))
}
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n]
n++
len := int(e.payload[n])
n++
if extid == id {
e.payload = append(e.payload[:n+2], append(buf, e.payload[n+2+len:]...)...)
return nil
}
n += len
}
e.payload = append(e.payload, id, uint8(len(buf)))
e.payload = append(e.payload, buf...)
binary.BigEndian.PutUint16(e.payload[2:4], binary.BigEndian.Uint16(e.payload[2:4])+1)
return nil
}
// GetIDs returns the available IDs.
func (e *TwoByteHeaderExtension) GetIDs() []uint8 {
ids := make([]uint8, 0, binary.BigEndian.Uint16(e.payload[2:4]))
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n]
n++
len := int(e.payload[n])
n++
ids = append(ids, extid)
n += len
}
return ids
}
// Get returns the payload of the extension with the given ID.
func (e *TwoByteHeaderExtension) Get(id uint8) []byte {
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n]
n++
len := int(e.payload[n])
n++
if extid == id {
return e.payload[n : n+len]
}
n += len
}
return nil
}
// Del deletes the extension with the specified ID.
func (e *TwoByteHeaderExtension) Del(id uint8) error {
for n := 4; n < len(e.payload); {
if e.payload[n] == 0x00 { // padding
n++
continue
}
extid := e.payload[n]
len := int(e.payload[n+1])
if extid == id {
e.payload = append(e.payload[:n], e.payload[n+2+len:]...)
return nil
}
n += len + 2
}
return errHeaderExtensionNotFound
}
// Unmarshal parses the extension payload.
func (e *TwoByteHeaderExtension) Unmarshal(buf []byte) (int, error) {
profile := binary.BigEndian.Uint16(buf[0:2])
if profile != headerExtensionProfileTwoByte {
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
}
e.payload = buf
return len(buf), nil
}
// Marshal returns the extension payload.
func (e TwoByteHeaderExtension) Marshal() ([]byte, error) {
return e.payload, nil
}
// MarshalTo marshals the extension to the given buffer.
func (e TwoByteHeaderExtension) MarshalTo(buf []byte) (int, error) {
size := e.MarshalSize()
if size > len(buf) {
return 0, io.ErrShortBuffer
}
return copy(buf, e.payload), nil
}
// MarshalSize returns the size of the extension payload.
func (e TwoByteHeaderExtension) MarshalSize() int {
return len(e.payload)
}
// RawExtension represents an RFC3550 header extension.
type RawExtension struct {
payload []byte
}
// Set sets the extension payload for the specified ID.
func (e *RawExtension) Set(id uint8, payload []byte) error {
if id != 0 {
return fmt.Errorf("%w actual(%d)", errRFC3550HeaderIDRange, id)
}
e.payload = payload
return nil
}
// GetIDs returns the available IDs.
func (e *RawExtension) GetIDs() []uint8 {
return []uint8{0}
}
// Get returns the payload of the extension with the given ID.
func (e *RawExtension) Get(id uint8) []byte {
if id == 0 {
return e.payload
}
return nil
}
// Del deletes the extension with the specified ID.
func (e *RawExtension) Del(id uint8) error {
if id == 0 {
e.payload = nil
return nil
}
return fmt.Errorf("%w actual(%d)", errRFC3550HeaderIDRange, id)
}
// Unmarshal parses the extension from the given buffer.
func (e *RawExtension) Unmarshal(buf []byte) (int, error) {
profile := binary.BigEndian.Uint16(buf[0:2])
if profile == headerExtensionProfileOneByte || profile == headerExtensionProfileTwoByte {
return 0, fmt.Errorf("%w actual(%x)", errHeaderExtensionNotFound, buf[0:2])
}
e.payload = buf
return len(buf), nil
}
// Marshal returns the raw extension payload.
func (e RawExtension) Marshal() ([]byte, error) {
return e.payload, nil
}
// MarshalTo marshals the extension to the given buffer.
func (e RawExtension) MarshalTo(buf []byte) (int, error) {
size := e.MarshalSize()
if size > len(buf) {
return 0, io.ErrShortBuffer
}
return copy(buf, e.payload), nil
}
// MarshalSize returns the size of the extension when marshaled.
func (e RawExtension) MarshalSize() int {
return len(e.payload)
}
|