compound_packet.go 3.5 KB

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