compound_packet.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package rtcp
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // A CompoundPacket is a collection of RTCP packets transmitted as a single packet with
  9. // the underlying protocol (for example UDP).
  10. //
  11. // To maximize the resolution of receiption statistics, the first Packet in a CompoundPacket
  12. // must always be either a SenderReport or a ReceiverReport. This is true even if no data
  13. // has been sent or received, in which case an empty ReceiverReport must be sent, and even
  14. // if the only other RTCP packet in the compound packet is a Goodbye.
  15. //
  16. // Next, a SourceDescription containing a CNAME item must be included in each CompoundPacket
  17. // to identify the source and to begin associating media for purposes such as lip-sync.
  18. //
  19. // Other RTCP packet types may follow in any order. Packet types may appear more than once.
  20. type CompoundPacket []Packet
  21. // Validate returns an error if this is not an RFC-compliant CompoundPacket.
  22. func (c CompoundPacket) Validate() error {
  23. if len(c) == 0 {
  24. return errEmptyCompound
  25. }
  26. // SenderReport and ReceiverReport are the only types that
  27. // are allowed to be the first packet in a compound datagram
  28. switch c[0].(type) {
  29. case *SenderReport, *ReceiverReport:
  30. // ok
  31. default:
  32. return errBadFirstPacket
  33. }
  34. for _, pkt := range c[1:] {
  35. switch p := pkt.(type) {
  36. // If the number of RecetpionReports exceeds 31 additional ReceiverReports
  37. // can be included here.
  38. case *ReceiverReport:
  39. continue
  40. // A SourceDescription containing a CNAME must be included in every
  41. // CompoundPacket.
  42. case *SourceDescription:
  43. var hasCNAME bool
  44. for _, c := range p.Chunks {
  45. for _, it := range c.Items {
  46. if it.Type == SDESCNAME {
  47. hasCNAME = true
  48. }
  49. }
  50. }
  51. if !hasCNAME {
  52. return errMissingCNAME
  53. }
  54. return nil
  55. // Other packets are not permitted before the CNAME
  56. default:
  57. return errPacketBeforeCNAME
  58. }
  59. }
  60. // CNAME never reached
  61. return errMissingCNAME
  62. }
  63. // CNAME returns the CNAME that *must* be present in every CompoundPacket
  64. func (c CompoundPacket) CNAME() (string, error) {
  65. var err error
  66. if len(c) < 1 {
  67. return "", errEmptyCompound
  68. }
  69. for _, pkt := range c[1:] {
  70. sdes, ok := pkt.(*SourceDescription)
  71. if ok {
  72. for _, c := range sdes.Chunks {
  73. for _, it := range c.Items {
  74. if it.Type == SDESCNAME {
  75. return it.Text, err
  76. }
  77. }
  78. }
  79. } else {
  80. _, ok := pkt.(*ReceiverReport)
  81. if !ok {
  82. err = errPacketBeforeCNAME
  83. }
  84. }
  85. }
  86. return "", errMissingCNAME
  87. }
  88. // Marshal encodes the CompoundPacket as binary.
  89. func (c CompoundPacket) Marshal() ([]byte, error) {
  90. if err := c.Validate(); err != nil {
  91. return nil, err
  92. }
  93. p := []Packet(c)
  94. return Marshal(p)
  95. }
  96. // Unmarshal decodes a CompoundPacket from binary.
  97. func (c *CompoundPacket) Unmarshal(rawData []byte) error {
  98. out := make(CompoundPacket, 0)
  99. for len(rawData) != 0 {
  100. p, processed, err := unmarshal(rawData)
  101. if err != nil {
  102. return err
  103. }
  104. out = append(out, p)
  105. rawData = rawData[processed:]
  106. }
  107. *c = out
  108. return c.Validate()
  109. }
  110. // DestinationSSRC returns the synchronization sources associated with this
  111. // CompoundPacket's reception report.
  112. func (c CompoundPacket) DestinationSSRC() []uint32 {
  113. if len(c) == 0 {
  114. return nil
  115. }
  116. return c[0].DestinationSSRC()
  117. }
  118. func (c CompoundPacket) String() string {
  119. out := "CompoundPacket\n"
  120. for _, p := range c {
  121. stringer, canString := p.(fmt.Stringer)
  122. if canString {
  123. out += stringer.String()
  124. } else {
  125. out += stringify(p)
  126. }
  127. }
  128. out = strings.TrimSuffix(strings.ReplaceAll(out, "\n", "\n\t"), "\t")
  129. return out
  130. }