rtpcodec.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "strings"
  6. "github.com/pion/webrtc/v3/internal/fmtp"
  7. )
  8. // RTPCodecType determines the type of a codec
  9. type RTPCodecType int
  10. const (
  11. // RTPCodecTypeAudio indicates this is an audio codec
  12. RTPCodecTypeAudio RTPCodecType = iota + 1
  13. // RTPCodecTypeVideo indicates this is a video codec
  14. RTPCodecTypeVideo
  15. )
  16. func (t RTPCodecType) String() string {
  17. switch t {
  18. case RTPCodecTypeAudio:
  19. return "audio"
  20. case RTPCodecTypeVideo:
  21. return "video" //nolint: goconst
  22. default:
  23. return ErrUnknownType.Error()
  24. }
  25. }
  26. // NewRTPCodecType creates a RTPCodecType from a string
  27. func NewRTPCodecType(r string) RTPCodecType {
  28. switch {
  29. case strings.EqualFold(r, RTPCodecTypeAudio.String()):
  30. return RTPCodecTypeAudio
  31. case strings.EqualFold(r, RTPCodecTypeVideo.String()):
  32. return RTPCodecTypeVideo
  33. default:
  34. return RTPCodecType(0)
  35. }
  36. }
  37. // RTPCodecCapability provides information about codec capabilities.
  38. //
  39. // https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpcodeccapability-members
  40. type RTPCodecCapability struct {
  41. MimeType string
  42. ClockRate uint32
  43. Channels uint16
  44. SDPFmtpLine string
  45. RTCPFeedback []RTCPFeedback
  46. }
  47. // RTPHeaderExtensionCapability is used to define a RFC5285 RTP header extension supported by the codec.
  48. //
  49. // https://w3c.github.io/webrtc-pc/#dom-rtcrtpcapabilities-headerextensions
  50. type RTPHeaderExtensionCapability struct {
  51. URI string
  52. }
  53. // RTPHeaderExtensionParameter represents a negotiated RFC5285 RTP header extension.
  54. //
  55. // https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpheaderextensionparameters-members
  56. type RTPHeaderExtensionParameter struct {
  57. URI string
  58. ID int
  59. }
  60. // RTPCodecParameters is a sequence containing the media codecs that an RtpSender
  61. // will choose from, as well as entries for RTX, RED and FEC mechanisms. This also
  62. // includes the PayloadType that has been negotiated
  63. //
  64. // https://w3c.github.io/webrtc-pc/#rtcrtpcodecparameters
  65. type RTPCodecParameters struct {
  66. RTPCodecCapability
  67. PayloadType PayloadType
  68. statsID string
  69. }
  70. // RTPParameters is a list of negotiated codecs and header extensions
  71. //
  72. // https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpparameters-members
  73. type RTPParameters struct {
  74. HeaderExtensions []RTPHeaderExtensionParameter
  75. Codecs []RTPCodecParameters
  76. }
  77. type codecMatchType int
  78. const (
  79. codecMatchNone codecMatchType = 0
  80. codecMatchPartial codecMatchType = 1
  81. codecMatchExact codecMatchType = 2
  82. )
  83. // Do a fuzzy find for a codec in the list of codecs
  84. // Used for lookup up a codec in an existing list to find a match
  85. // Returns codecMatchExact, codecMatchPartial, or codecMatchNone
  86. func codecParametersFuzzySearch(needle RTPCodecParameters, haystack []RTPCodecParameters) (RTPCodecParameters, codecMatchType) {
  87. needleFmtp := fmtp.Parse(needle.RTPCodecCapability.MimeType, needle.RTPCodecCapability.SDPFmtpLine)
  88. // First attempt to match on MimeType + SDPFmtpLine
  89. for _, c := range haystack {
  90. cfmtp := fmtp.Parse(c.RTPCodecCapability.MimeType, c.RTPCodecCapability.SDPFmtpLine)
  91. if needleFmtp.Match(cfmtp) {
  92. return c, codecMatchExact
  93. }
  94. }
  95. // Fallback to just MimeType
  96. for _, c := range haystack {
  97. if strings.EqualFold(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) {
  98. return c, codecMatchPartial
  99. }
  100. }
  101. return RTPCodecParameters{}, codecMatchNone
  102. }