rapid_resynchronization_request.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package rtcp
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. // The RapidResynchronizationRequest packet informs the encoder about the loss of an undefined amount of coded video data belonging to one or more pictures
  7. type RapidResynchronizationRequest struct {
  8. // SSRC of sender
  9. SenderSSRC uint32
  10. // SSRC of the media source
  11. MediaSSRC uint32
  12. }
  13. const (
  14. rrrLength = 2
  15. rrrHeaderLength = ssrcLength * 2
  16. rrrMediaOffset = 4
  17. )
  18. // Marshal encodes the RapidResynchronizationRequest in binary
  19. func (p RapidResynchronizationRequest) Marshal() ([]byte, error) {
  20. /*
  21. * RRR does not require parameters. Therefore, the length field MUST be
  22. * 2, and there MUST NOT be any Feedback Control Information.
  23. *
  24. * The semantics of this FB message is independent of the payload type.
  25. */
  26. rawPacket := make([]byte, p.len())
  27. packetBody := rawPacket[headerLength:]
  28. binary.BigEndian.PutUint32(packetBody, p.SenderSSRC)
  29. binary.BigEndian.PutUint32(packetBody[rrrMediaOffset:], p.MediaSSRC)
  30. hData, err := p.Header().Marshal()
  31. if err != nil {
  32. return nil, err
  33. }
  34. copy(rawPacket, hData)
  35. return rawPacket, nil
  36. }
  37. // Unmarshal decodes the RapidResynchronizationRequest from binary
  38. func (p *RapidResynchronizationRequest) Unmarshal(rawPacket []byte) error {
  39. if len(rawPacket) < (headerLength + (ssrcLength * 2)) {
  40. return errPacketTooShort
  41. }
  42. var h Header
  43. if err := h.Unmarshal(rawPacket); err != nil {
  44. return err
  45. }
  46. if h.Type != TypeTransportSpecificFeedback || h.Count != FormatRRR {
  47. return errWrongType
  48. }
  49. p.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:])
  50. p.MediaSSRC = binary.BigEndian.Uint32(rawPacket[headerLength+ssrcLength:])
  51. return nil
  52. }
  53. func (p *RapidResynchronizationRequest) len() int {
  54. return headerLength + rrrHeaderLength
  55. }
  56. // Header returns the Header associated with this packet.
  57. func (p *RapidResynchronizationRequest) Header() Header {
  58. return Header{
  59. Count: FormatRRR,
  60. Type: TypeTransportSpecificFeedback,
  61. Length: rrrLength,
  62. }
  63. }
  64. // DestinationSSRC returns an array of SSRC values that this packet refers to.
  65. func (p *RapidResynchronizationRequest) DestinationSSRC() []uint32 {
  66. return []uint32{p.MediaSSRC}
  67. }
  68. func (p *RapidResynchronizationRequest) String() string {
  69. return fmt.Sprintf("RapidResynchronizationRequest %x %x", p.SenderSSRC, p.MediaSSRC)
  70. }