settingengine.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //go:build !js
  2. // +build !js
  3. package webrtc
  4. import (
  5. "io"
  6. "time"
  7. "github.com/pion/dtls/v2"
  8. "github.com/pion/ice/v2"
  9. "github.com/pion/logging"
  10. "github.com/pion/transport/packetio"
  11. "github.com/pion/transport/vnet"
  12. "golang.org/x/net/proxy"
  13. )
  14. // SettingEngine allows influencing behavior in ways that are not
  15. // supported by the WebRTC API. This allows us to support additional
  16. // use-cases without deviating from the WebRTC API elsewhere.
  17. type SettingEngine struct {
  18. ephemeralUDP struct {
  19. PortMin uint16
  20. PortMax uint16
  21. }
  22. detach struct {
  23. DataChannels bool
  24. }
  25. timeout struct {
  26. ICEDisconnectedTimeout *time.Duration
  27. ICEFailedTimeout *time.Duration
  28. ICEKeepaliveInterval *time.Duration
  29. ICEHostAcceptanceMinWait *time.Duration
  30. ICESrflxAcceptanceMinWait *time.Duration
  31. ICEPrflxAcceptanceMinWait *time.Duration
  32. ICERelayAcceptanceMinWait *time.Duration
  33. }
  34. candidates struct {
  35. ICELite bool
  36. ICENetworkTypes []NetworkType
  37. InterfaceFilter func(string) bool
  38. NAT1To1IPs []string
  39. NAT1To1IPCandidateType ICECandidateType
  40. MulticastDNSMode ice.MulticastDNSMode
  41. MulticastDNSHostName string
  42. UsernameFragment string
  43. Password string
  44. }
  45. replayProtection struct {
  46. DTLS *uint
  47. SRTP *uint
  48. SRTCP *uint
  49. }
  50. dtls struct {
  51. retransmissionInterval time.Duration
  52. }
  53. sctp struct {
  54. maxReceiveBufferSize uint32
  55. }
  56. sdpMediaLevelFingerprints bool
  57. answeringDTLSRole DTLSRole
  58. disableCertificateFingerprintVerification bool
  59. disableSRTPReplayProtection bool
  60. disableSRTCPReplayProtection bool
  61. vnet *vnet.Net
  62. BufferFactory func(packetType packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser
  63. LoggerFactory logging.LoggerFactory
  64. iceTCPMux ice.TCPMux
  65. iceUDPMux ice.UDPMux
  66. iceProxyDialer proxy.Dialer
  67. disableMediaEngineCopy bool
  68. srtpProtectionProfiles []dtls.SRTPProtectionProfile
  69. receiveMTU uint
  70. }
  71. // getReceiveMTU returns the configured MTU. If SettingEngine's MTU is configured to 0 it returns the default
  72. func (e *SettingEngine) getReceiveMTU() uint {
  73. if e.receiveMTU != 0 {
  74. return e.receiveMTU
  75. }
  76. return receiveMTU
  77. }
  78. // DetachDataChannels enables detaching data channels. When enabled
  79. // data channels have to be detached in the OnOpen callback using the
  80. // DataChannel.Detach method.
  81. func (e *SettingEngine) DetachDataChannels() {
  82. e.detach.DataChannels = true
  83. }
  84. // SetSRTPProtectionProfiles allows the user to override the default SRTP Protection Profiles
  85. // The default srtp protection profiles are provided by the function `defaultSrtpProtectionProfiles`
  86. func (e *SettingEngine) SetSRTPProtectionProfiles(profiles ...dtls.SRTPProtectionProfile) {
  87. e.srtpProtectionProfiles = profiles
  88. }
  89. // SetICETimeouts sets the behavior around ICE Timeouts
  90. // * disconnectedTimeout is the duration without network activity before a Agent is considered disconnected. Default is 5 Seconds
  91. // * failedTimeout is the duration without network activity before a Agent is considered failed after disconnected. Default is 25 Seconds
  92. // * keepAliveInterval is how often the ICE Agent sends extra traffic if there is no activity, if media is flowing no traffic will be sent. Default is 2 seconds
  93. func (e *SettingEngine) SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval time.Duration) {
  94. e.timeout.ICEDisconnectedTimeout = &disconnectedTimeout
  95. e.timeout.ICEFailedTimeout = &failedTimeout
  96. e.timeout.ICEKeepaliveInterval = &keepAliveInterval
  97. }
  98. // SetHostAcceptanceMinWait sets the ICEHostAcceptanceMinWait
  99. func (e *SettingEngine) SetHostAcceptanceMinWait(t time.Duration) {
  100. e.timeout.ICEHostAcceptanceMinWait = &t
  101. }
  102. // SetSrflxAcceptanceMinWait sets the ICESrflxAcceptanceMinWait
  103. func (e *SettingEngine) SetSrflxAcceptanceMinWait(t time.Duration) {
  104. e.timeout.ICESrflxAcceptanceMinWait = &t
  105. }
  106. // SetPrflxAcceptanceMinWait sets the ICEPrflxAcceptanceMinWait
  107. func (e *SettingEngine) SetPrflxAcceptanceMinWait(t time.Duration) {
  108. e.timeout.ICEPrflxAcceptanceMinWait = &t
  109. }
  110. // SetRelayAcceptanceMinWait sets the ICERelayAcceptanceMinWait
  111. func (e *SettingEngine) SetRelayAcceptanceMinWait(t time.Duration) {
  112. e.timeout.ICERelayAcceptanceMinWait = &t
  113. }
  114. // SetEphemeralUDPPortRange limits the pool of ephemeral ports that
  115. // ICE UDP connections can allocate from. This affects both host candidates,
  116. // and the local address of server reflexive candidates.
  117. func (e *SettingEngine) SetEphemeralUDPPortRange(portMin, portMax uint16) error {
  118. if portMax < portMin {
  119. return ice.ErrPort
  120. }
  121. e.ephemeralUDP.PortMin = portMin
  122. e.ephemeralUDP.PortMax = portMax
  123. return nil
  124. }
  125. // SetLite configures whether or not the ice agent should be a lite agent
  126. func (e *SettingEngine) SetLite(lite bool) {
  127. e.candidates.ICELite = lite
  128. }
  129. // SetNetworkTypes configures what types of candidate networks are supported
  130. // during local and server reflexive gathering.
  131. func (e *SettingEngine) SetNetworkTypes(candidateTypes []NetworkType) {
  132. e.candidates.ICENetworkTypes = candidateTypes
  133. }
  134. // SetInterfaceFilter sets the filtering functions when gathering ICE candidates
  135. // This can be used to exclude certain network interfaces from ICE. Which may be
  136. // useful if you know a certain interface will never succeed, or if you wish to reduce
  137. // the amount of information you wish to expose to the remote peer
  138. func (e *SettingEngine) SetInterfaceFilter(filter func(string) bool) {
  139. e.candidates.InterfaceFilter = filter
  140. }
  141. // SetNAT1To1IPs sets a list of external IP addresses of 1:1 (D)NAT
  142. // and a candidate type for which the external IP address is used.
  143. // This is useful when you are host a server using Pion on an AWS EC2 instance
  144. // which has a private address, behind a 1:1 DNAT with a public IP (e.g.
  145. // Elastic IP). In this case, you can give the public IP address so that
  146. // Pion will use the public IP address in its candidate instead of the private
  147. // IP address. The second argument, candidateType, is used to tell Pion which
  148. // type of candidate should use the given public IP address.
  149. // Two types of candidates are supported:
  150. //
  151. // ICECandidateTypeHost:
  152. // The public IP address will be used for the host candidate in the SDP.
  153. // ICECandidateTypeSrflx:
  154. // A server reflexive candidate with the given public IP address will be added
  155. // to the SDP.
  156. //
  157. // Please note that if you choose ICECandidateTypeHost, then the private IP address
  158. // won't be advertised with the peer. Also, this option cannot be used along with mDNS.
  159. //
  160. // If you choose ICECandidateTypeSrflx, it simply adds a server reflexive candidate
  161. // with the public IP. The host candidate is still available along with mDNS
  162. // capabilities unaffected. Also, you cannot give STUN server URL at the same time.
  163. // It will result in an error otherwise.
  164. func (e *SettingEngine) SetNAT1To1IPs(ips []string, candidateType ICECandidateType) {
  165. e.candidates.NAT1To1IPs = ips
  166. e.candidates.NAT1To1IPCandidateType = candidateType
  167. }
  168. // SetAnsweringDTLSRole sets the DTLS role that is selected when offering
  169. // The DTLS role controls if the WebRTC Client as a client or server. This
  170. // may be useful when interacting with non-compliant clients or debugging issues.
  171. //
  172. // DTLSRoleActive:
  173. // Act as DTLS Client, send the ClientHello and starts the handshake
  174. // DTLSRolePassive:
  175. // Act as DTLS Server, wait for ClientHello
  176. func (e *SettingEngine) SetAnsweringDTLSRole(role DTLSRole) error {
  177. if role != DTLSRoleClient && role != DTLSRoleServer {
  178. return errSettingEngineSetAnsweringDTLSRole
  179. }
  180. e.answeringDTLSRole = role
  181. return nil
  182. }
  183. // SetVNet sets the VNet instance that is passed to pion/ice
  184. //
  185. // VNet is a virtual network layer for Pion, allowing users to simulate
  186. // different topologies, latency, loss and jitter. This can be useful for
  187. // learning WebRTC concepts or testing your application in a lab environment
  188. func (e *SettingEngine) SetVNet(vnet *vnet.Net) {
  189. e.vnet = vnet
  190. }
  191. // SetICEMulticastDNSMode controls if pion/ice queries and generates mDNS ICE Candidates
  192. func (e *SettingEngine) SetICEMulticastDNSMode(multicastDNSMode ice.MulticastDNSMode) {
  193. e.candidates.MulticastDNSMode = multicastDNSMode
  194. }
  195. // SetMulticastDNSHostName sets a static HostName to be used by pion/ice instead of generating one on startup
  196. //
  197. // This should only be used for a single PeerConnection. Having multiple PeerConnections with the same HostName will cause
  198. // undefined behavior
  199. func (e *SettingEngine) SetMulticastDNSHostName(hostName string) {
  200. e.candidates.MulticastDNSHostName = hostName
  201. }
  202. // SetICECredentials sets a staic uFrag/uPwd to be used by pion/ice
  203. //
  204. // This is useful if you want to do signalless WebRTC session, or having a reproducible environment with static credentials
  205. func (e *SettingEngine) SetICECredentials(usernameFragment, password string) {
  206. e.candidates.UsernameFragment = usernameFragment
  207. e.candidates.Password = password
  208. }
  209. // DisableCertificateFingerprintVerification disables fingerprint verification after DTLS Handshake has finished
  210. func (e *SettingEngine) DisableCertificateFingerprintVerification(isDisabled bool) {
  211. e.disableCertificateFingerprintVerification = isDisabled
  212. }
  213. // SetDTLSReplayProtectionWindow sets a replay attack protection window size of DTLS connection.
  214. func (e *SettingEngine) SetDTLSReplayProtectionWindow(n uint) {
  215. e.replayProtection.DTLS = &n
  216. }
  217. // SetSRTPReplayProtectionWindow sets a replay attack protection window size of SRTP session.
  218. func (e *SettingEngine) SetSRTPReplayProtectionWindow(n uint) {
  219. e.disableSRTPReplayProtection = false
  220. e.replayProtection.SRTP = &n
  221. }
  222. // SetSRTCPReplayProtectionWindow sets a replay attack protection window size of SRTCP session.
  223. func (e *SettingEngine) SetSRTCPReplayProtectionWindow(n uint) {
  224. e.disableSRTCPReplayProtection = false
  225. e.replayProtection.SRTCP = &n
  226. }
  227. // DisableSRTPReplayProtection disables SRTP replay protection.
  228. func (e *SettingEngine) DisableSRTPReplayProtection(isDisabled bool) {
  229. e.disableSRTPReplayProtection = isDisabled
  230. }
  231. // DisableSRTCPReplayProtection disables SRTCP replay protection.
  232. func (e *SettingEngine) DisableSRTCPReplayProtection(isDisabled bool) {
  233. e.disableSRTCPReplayProtection = isDisabled
  234. }
  235. // SetSDPMediaLevelFingerprints configures the logic for DTLS Fingerprint insertion
  236. // If true, fingerprints will be inserted in the sdp at the fingerprint
  237. // level, instead of the session level. This helps with compatibility with
  238. // some webrtc implementations.
  239. func (e *SettingEngine) SetSDPMediaLevelFingerprints(sdpMediaLevelFingerprints bool) {
  240. e.sdpMediaLevelFingerprints = sdpMediaLevelFingerprints
  241. }
  242. // SetICETCPMux enables ICE-TCP when set to a non-nil value. Make sure that
  243. // NetworkTypeTCP4 or NetworkTypeTCP6 is enabled as well.
  244. func (e *SettingEngine) SetICETCPMux(tcpMux ice.TCPMux) {
  245. e.iceTCPMux = tcpMux
  246. }
  247. // SetICEUDPMux allows ICE traffic to come through a single UDP port, drastically
  248. // simplifying deployments where ports will need to be opened/forwarded.
  249. // UDPMux should be started prior to creating PeerConnections.
  250. func (e *SettingEngine) SetICEUDPMux(udpMux ice.UDPMux) {
  251. e.iceUDPMux = udpMux
  252. }
  253. // SetICEProxyDialer sets the proxy dialer interface based on golang.org/x/net/proxy.
  254. func (e *SettingEngine) SetICEProxyDialer(d proxy.Dialer) {
  255. e.iceProxyDialer = d
  256. }
  257. // DisableMediaEngineCopy stops the MediaEngine from being copied. This allows a user to modify
  258. // the MediaEngine after the PeerConnection has been constructed. This is useful if you wish to
  259. // modify codecs after signaling. Make sure not to share MediaEngines between PeerConnections.
  260. func (e *SettingEngine) DisableMediaEngineCopy(isDisabled bool) {
  261. e.disableMediaEngineCopy = isDisabled
  262. }
  263. // SetReceiveMTU sets the size of read buffer that copies incoming packets. This is optional.
  264. // Leave this 0 for the default receiveMTU
  265. func (e *SettingEngine) SetReceiveMTU(receiveMTU uint) {
  266. e.receiveMTU = receiveMTU
  267. }
  268. // SetDTLSRetransmissionInterval sets the retranmission interval for DTLS.
  269. func (e *SettingEngine) SetDTLSRetransmissionInterval(interval time.Duration) {
  270. e.dtls.retransmissionInterval = interval
  271. }
  272. // SetSCTPMaxReceiveBufferSize sets the maximum receive buffer size.
  273. // Leave this 0 for the default maxReceiveBufferSize.
  274. func (e *SettingEngine) SetSCTPMaxReceiveBufferSize(maxReceiveBufferSize uint32) {
  275. e.sctp.maxReceiveBufferSize = maxReceiveBufferSize
  276. }