rtpcodec.go 3.2 KB

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