MCPcopy Create free account
hub / github.com/pion/rtp / SetExtension

Method SetExtension

packet.go:356–404  ·  view source on GitHub ↗

SetExtension sets an RTP header extension

(id uint8, payload []byte)

Source from the content-addressed store, hash-verified

354
355// SetExtension sets an RTP header extension
356func (h *Header) SetExtension(id uint8, payload []byte) error { //nolint:gocognit
357 if h.Extension {
358 switch h.ExtensionProfile {
359 // RFC 8285 RTP One Byte Header Extension
360 case extensionProfileOneByte:
361 if id < 1 || id > 14 {
362 return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderIDRange, id)
363 }
364 if len(payload) > 16 {
365 return fmt.Errorf("%w actual(%d)", errRFC8285OneByteHeaderSize, len(payload))
366 }
367 // RFC 8285 RTP Two Byte Header Extension
368 case extensionProfileTwoByte:
369 if id < 1 || id > 255 {
370 return fmt.Errorf("%w actual(%d)", errRFC8285TwoByteHeaderIDRange, id)
371 }
372 if len(payload) > 255 {
373 return fmt.Errorf("%w actual(%d)", errRFC8285TwoByteHeaderSize, len(payload))
374 }
375 default: // RFC3550 Extension
376 if id != 0 {
377 return fmt.Errorf("%w actual(%d)", errRFC3550HeaderIDRange, id)
378 }
379 }
380
381 // Update existing if it exists else add new extension
382 for i, extension := range h.Extensions {
383 if extension.id == id {
384 h.Extensions[i].payload = payload
385 return nil
386 }
387 }
388 h.Extensions = append(h.Extensions, Extension{id: id, payload: payload})
389 return nil
390 }
391
392 // No existing header extensions
393 h.Extension = true
394
395 switch len := len(payload); {
396 case len <= 16:
397 h.ExtensionProfile = extensionProfileOneByte
398 case len > 16 && len < 256:
399 h.ExtensionProfile = extensionProfileTwoByte
400 }
401
402 h.Extensions = append(h.Extensions, Extension{id: id, payload: payload})
403 return nil
404}
405
406// GetExtensionIDs returns an extension id array
407func (h *Header) GetExtensionIDs() []uint8 {

Calls

no outgoing calls