util.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package sdp
  4. import (
  5. "errors"
  6. "fmt"
  7. "io"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "github.com/pion/randutil"
  12. )
  13. const (
  14. attributeKey = "a="
  15. )
  16. var (
  17. errExtractCodecRtpmap = errors.New("could not extract codec from rtpmap")
  18. errExtractCodecFmtp = errors.New("could not extract codec from fmtp")
  19. errExtractCodecRtcpFb = errors.New("could not extract codec from rtcp-fb")
  20. errPayloadTypeNotFound = errors.New("payload type not found")
  21. errCodecNotFound = errors.New("codec not found")
  22. errSyntaxError = errors.New("SyntaxError")
  23. )
  24. // ConnectionRole indicates which of the end points should initiate the connection establishment
  25. type ConnectionRole int
  26. const (
  27. // ConnectionRoleActive indicates the endpoint will initiate an outgoing connection.
  28. ConnectionRoleActive ConnectionRole = iota + 1
  29. // ConnectionRolePassive indicates the endpoint will accept an incoming connection.
  30. ConnectionRolePassive
  31. // ConnectionRoleActpass indicates the endpoint is willing to accept an incoming connection or to initiate an outgoing connection.
  32. ConnectionRoleActpass
  33. // ConnectionRoleHoldconn indicates the endpoint does not want the connection to be established for the time being.
  34. ConnectionRoleHoldconn
  35. )
  36. func (t ConnectionRole) String() string {
  37. switch t {
  38. case ConnectionRoleActive:
  39. return "active"
  40. case ConnectionRolePassive:
  41. return "passive"
  42. case ConnectionRoleActpass:
  43. return "actpass"
  44. case ConnectionRoleHoldconn:
  45. return "holdconn"
  46. default:
  47. return "Unknown"
  48. }
  49. }
  50. func newSessionID() (uint64, error) {
  51. // https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-26#section-5.2.1
  52. // Session ID is recommended to be constructed by generating a 64-bit
  53. // quantity with the highest bit set to zero and the remaining 63-bits
  54. // being cryptographically random.
  55. id, err := randutil.CryptoUint64()
  56. return id & (^(uint64(1) << 63)), err
  57. }
  58. // Codec represents a codec
  59. type Codec struct {
  60. PayloadType uint8
  61. Name string
  62. ClockRate uint32
  63. EncodingParameters string
  64. Fmtp string
  65. RTCPFeedback []string
  66. }
  67. const (
  68. unknown = iota
  69. )
  70. func (c Codec) String() string {
  71. return fmt.Sprintf("%d %s/%d/%s (%s) [%s]", c.PayloadType, c.Name, c.ClockRate, c.EncodingParameters, c.Fmtp, strings.Join(c.RTCPFeedback, ", "))
  72. }
  73. func (c *Codec) appendRTCPFeedback(rtcpFeedback string) {
  74. for _, existingRTCPFeedback := range c.RTCPFeedback {
  75. if existingRTCPFeedback == rtcpFeedback {
  76. return
  77. }
  78. }
  79. c.RTCPFeedback = append(c.RTCPFeedback, rtcpFeedback)
  80. }
  81. func parseRtpmap(rtpmap string) (Codec, error) {
  82. var codec Codec
  83. parsingFailed := errExtractCodecRtpmap
  84. // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters>]
  85. split := strings.Split(rtpmap, " ")
  86. if len(split) != 2 {
  87. return codec, parsingFailed
  88. }
  89. ptSplit := strings.Split(split[0], ":")
  90. if len(ptSplit) != 2 {
  91. return codec, parsingFailed
  92. }
  93. ptInt, err := strconv.ParseUint(ptSplit[1], 10, 8)
  94. if err != nil {
  95. return codec, parsingFailed
  96. }
  97. codec.PayloadType = uint8(ptInt)
  98. split = strings.Split(split[1], "/")
  99. codec.Name = split[0]
  100. parts := len(split)
  101. if parts > 1 {
  102. rate, err := strconv.ParseUint(split[1], 10, 32)
  103. if err != nil {
  104. return codec, parsingFailed
  105. }
  106. codec.ClockRate = uint32(rate)
  107. }
  108. if parts > 2 {
  109. codec.EncodingParameters = split[2]
  110. }
  111. return codec, nil
  112. }
  113. func parseFmtp(fmtp string) (Codec, error) {
  114. var codec Codec
  115. parsingFailed := errExtractCodecFmtp
  116. // a=fmtp:<format> <format specific parameters>
  117. split := strings.Split(fmtp, " ")
  118. if len(split) != 2 {
  119. return codec, parsingFailed
  120. }
  121. formatParams := split[1]
  122. split = strings.Split(split[0], ":")
  123. if len(split) != 2 {
  124. return codec, parsingFailed
  125. }
  126. ptInt, err := strconv.ParseUint(split[1], 10, 8)
  127. if err != nil {
  128. return codec, parsingFailed
  129. }
  130. codec.PayloadType = uint8(ptInt)
  131. codec.Fmtp = formatParams
  132. return codec, nil
  133. }
  134. func parseRtcpFb(rtcpFb string) (codec Codec, isWildcard bool, err error) {
  135. var ptInt uint64
  136. err = errExtractCodecRtcpFb
  137. // a=ftcp-fb:<payload type> <RTCP feedback type> [<RTCP feedback parameter>]
  138. split := strings.SplitN(rtcpFb, " ", 2)
  139. if len(split) != 2 {
  140. return
  141. }
  142. ptSplit := strings.Split(split[0], ":")
  143. if len(ptSplit) != 2 {
  144. return
  145. }
  146. isWildcard = ptSplit[1] == "*"
  147. if !isWildcard {
  148. ptInt, err = strconv.ParseUint(ptSplit[1], 10, 8)
  149. if err != nil {
  150. return
  151. }
  152. codec.PayloadType = uint8(ptInt)
  153. }
  154. codec.RTCPFeedback = append(codec.RTCPFeedback, split[1])
  155. return codec, isWildcard, nil
  156. }
  157. func mergeCodecs(codec Codec, codecs map[uint8]Codec) {
  158. savedCodec := codecs[codec.PayloadType]
  159. if savedCodec.PayloadType == 0 {
  160. savedCodec.PayloadType = codec.PayloadType
  161. }
  162. if savedCodec.Name == "" {
  163. savedCodec.Name = codec.Name
  164. }
  165. if savedCodec.ClockRate == 0 {
  166. savedCodec.ClockRate = codec.ClockRate
  167. }
  168. if savedCodec.EncodingParameters == "" {
  169. savedCodec.EncodingParameters = codec.EncodingParameters
  170. }
  171. if savedCodec.Fmtp == "" {
  172. savedCodec.Fmtp = codec.Fmtp
  173. }
  174. savedCodec.RTCPFeedback = append(savedCodec.RTCPFeedback, codec.RTCPFeedback...)
  175. codecs[savedCodec.PayloadType] = savedCodec
  176. }
  177. func (s *SessionDescription) buildCodecMap() map[uint8]Codec {
  178. codecs := map[uint8]Codec{
  179. // static codecs that do not require a rtpmap
  180. 0: {
  181. PayloadType: 0,
  182. Name: "PCMU",
  183. ClockRate: 8000,
  184. },
  185. 8: {
  186. PayloadType: 8,
  187. Name: "PCMA",
  188. ClockRate: 8000,
  189. },
  190. }
  191. wildcardRTCPFeedback := []string{}
  192. for _, m := range s.MediaDescriptions {
  193. for _, a := range m.Attributes {
  194. attr := a.String()
  195. switch {
  196. case strings.HasPrefix(attr, "rtpmap:"):
  197. codec, err := parseRtpmap(attr)
  198. if err == nil {
  199. mergeCodecs(codec, codecs)
  200. }
  201. case strings.HasPrefix(attr, "fmtp:"):
  202. codec, err := parseFmtp(attr)
  203. if err == nil {
  204. mergeCodecs(codec, codecs)
  205. }
  206. case strings.HasPrefix(attr, "rtcp-fb:"):
  207. codec, isWildcard, err := parseRtcpFb(attr)
  208. switch {
  209. case err != nil:
  210. case isWildcard:
  211. wildcardRTCPFeedback = append(wildcardRTCPFeedback, codec.RTCPFeedback...)
  212. default:
  213. mergeCodecs(codec, codecs)
  214. }
  215. }
  216. }
  217. }
  218. for i, codec := range codecs {
  219. for _, newRTCPFeedback := range wildcardRTCPFeedback {
  220. codec.appendRTCPFeedback(newRTCPFeedback)
  221. }
  222. codecs[i] = codec
  223. }
  224. return codecs
  225. }
  226. func equivalentFmtp(want, got string) bool {
  227. wantSplit := strings.Split(want, ";")
  228. gotSplit := strings.Split(got, ";")
  229. if len(wantSplit) != len(gotSplit) {
  230. return false
  231. }
  232. sort.Strings(wantSplit)
  233. sort.Strings(gotSplit)
  234. for i, wantPart := range wantSplit {
  235. wantPart = strings.TrimSpace(wantPart)
  236. gotPart := strings.TrimSpace(gotSplit[i])
  237. if gotPart != wantPart {
  238. return false
  239. }
  240. }
  241. return true
  242. }
  243. func codecsMatch(wanted, got Codec) bool {
  244. if wanted.Name != "" && !strings.EqualFold(wanted.Name, got.Name) {
  245. return false
  246. }
  247. if wanted.ClockRate != 0 && wanted.ClockRate != got.ClockRate {
  248. return false
  249. }
  250. if wanted.EncodingParameters != "" && wanted.EncodingParameters != got.EncodingParameters {
  251. return false
  252. }
  253. if wanted.Fmtp != "" && !equivalentFmtp(wanted.Fmtp, got.Fmtp) {
  254. return false
  255. }
  256. return true
  257. }
  258. // GetCodecForPayloadType scans the SessionDescription for the given payload type and returns the codec
  259. func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, error) {
  260. codecs := s.buildCodecMap()
  261. codec, ok := codecs[payloadType]
  262. if ok {
  263. return codec, nil
  264. }
  265. return codec, errPayloadTypeNotFound
  266. }
  267. // GetPayloadTypeForCodec scans the SessionDescription for a codec that matches the provided codec
  268. // as closely as possible and returns its payload type
  269. func (s *SessionDescription) GetPayloadTypeForCodec(wanted Codec) (uint8, error) {
  270. codecs := s.buildCodecMap()
  271. for payloadType, codec := range codecs {
  272. if codecsMatch(wanted, codec) {
  273. return payloadType, nil
  274. }
  275. }
  276. return 0, errCodecNotFound
  277. }
  278. type stateFn func(*lexer) (stateFn, error)
  279. type lexer struct {
  280. desc *SessionDescription
  281. cache *unmarshalCache
  282. baseLexer
  283. }
  284. type keyToState func(key byte) stateFn
  285. func (l *lexer) handleType(fn keyToState) (stateFn, error) {
  286. key, err := l.readType()
  287. if errors.Is(err, io.EOF) && key == 0 {
  288. return nil, nil //nolint:nilnil
  289. } else if err != nil {
  290. return nil, err
  291. }
  292. if res := fn(key); res != nil {
  293. return res, nil
  294. }
  295. return nil, l.syntaxError()
  296. }