playoutdelayextension.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package rtp
  4. import (
  5. "encoding/binary"
  6. "errors"
  7. )
  8. const (
  9. playoutDelayExtensionSize = 3
  10. playoutDelayMaxValue = (1 << 12) - 1
  11. )
  12. var errPlayoutDelayInvalidValue = errors.New("invalid playout delay value")
  13. // PlayoutDelayExtension is a extension payload format in
  14. // http://www.webrtc.org/experiments/rtp-hdrext/playout-delay
  15. // 0 1 2 3
  16. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  17. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  18. // | ID | len=2 | MIN delay | MAX delay |
  19. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  20. type PlayoutDelayExtension struct {
  21. minDelay, maxDelay uint16
  22. }
  23. // Marshal serializes the members to buffer
  24. func (p PlayoutDelayExtension) Marshal() ([]byte, error) {
  25. if p.minDelay > playoutDelayMaxValue || p.maxDelay > playoutDelayMaxValue {
  26. return nil, errPlayoutDelayInvalidValue
  27. }
  28. return []byte{
  29. byte(p.minDelay >> 4),
  30. byte(p.minDelay<<4) | byte(p.maxDelay>>8),
  31. byte(p.maxDelay),
  32. }, nil
  33. }
  34. // Unmarshal parses the passed byte slice and stores the result in the members
  35. func (p *PlayoutDelayExtension) Unmarshal(rawData []byte) error {
  36. if len(rawData) < playoutDelayExtensionSize {
  37. return errTooSmall
  38. }
  39. p.minDelay = binary.BigEndian.Uint16(rawData[0:2]) >> 4
  40. p.maxDelay = binary.BigEndian.Uint16(rawData[1:3]) & 0x0FFF
  41. return nil
  42. }