peerconnection.go 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. package webrtc
  6. import (
  7. "crypto/ecdsa"
  8. "crypto/elliptic"
  9. "crypto/rand"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "sync/atomic"
  17. "time"
  18. "github.com/pion/ice/v2"
  19. "github.com/pion/interceptor"
  20. "github.com/pion/logging"
  21. "github.com/pion/rtcp"
  22. "github.com/pion/sdp/v3"
  23. "github.com/pion/srtp/v2"
  24. "github.com/pion/webrtc/v3/internal/util"
  25. "github.com/pion/webrtc/v3/pkg/rtcerr"
  26. )
  27. // PeerConnection represents a WebRTC connection that establishes a
  28. // peer-to-peer communications with another PeerConnection instance in a
  29. // browser, or to another endpoint implementing the required protocols.
  30. type PeerConnection struct {
  31. statsID string
  32. mu sync.RWMutex
  33. sdpOrigin sdp.Origin
  34. // ops is an operations queue which will ensure the enqueued actions are
  35. // executed in order. It is used for asynchronously, but serially processing
  36. // remote and local descriptions
  37. ops *operations
  38. configuration Configuration
  39. currentLocalDescription *SessionDescription
  40. pendingLocalDescription *SessionDescription
  41. currentRemoteDescription *SessionDescription
  42. pendingRemoteDescription *SessionDescription
  43. signalingState SignalingState
  44. iceConnectionState atomic.Value // ICEConnectionState
  45. connectionState atomic.Value // PeerConnectionState
  46. idpLoginURL *string
  47. isClosed *atomicBool
  48. isNegotiationNeeded *atomicBool
  49. negotiationNeededState negotiationNeededState
  50. lastOffer string
  51. lastAnswer string
  52. // a value containing the last known greater mid value
  53. // we internally generate mids as numbers. Needed since JSEP
  54. // requires that when reusing a media section a new unique mid
  55. // should be defined (see JSEP 3.4.1).
  56. greaterMid int
  57. rtpTransceivers []*RTPTransceiver
  58. onSignalingStateChangeHandler func(SignalingState)
  59. onICEConnectionStateChangeHandler atomic.Value // func(ICEConnectionState)
  60. onConnectionStateChangeHandler atomic.Value // func(PeerConnectionState)
  61. onTrackHandler func(*TrackRemote, *RTPReceiver)
  62. onDataChannelHandler func(*DataChannel)
  63. onNegotiationNeededHandler atomic.Value // func()
  64. iceGatherer *ICEGatherer
  65. iceTransport *ICETransport
  66. dtlsTransport *DTLSTransport
  67. sctpTransport *SCTPTransport
  68. // A reference to the associated API state used by this connection
  69. api *API
  70. log logging.LeveledLogger
  71. interceptorRTCPWriter interceptor.RTCPWriter
  72. }
  73. // NewPeerConnection creates a PeerConnection with the default codecs and
  74. // interceptors. See RegisterDefaultCodecs and RegisterDefaultInterceptors.
  75. //
  76. // If you wish to customize the set of available codecs or the set of
  77. // active interceptors, create a MediaEngine and call api.NewPeerConnection
  78. // instead of this function.
  79. func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
  80. m := &MediaEngine{}
  81. if err := m.RegisterDefaultCodecs(); err != nil {
  82. return nil, err
  83. }
  84. i := &interceptor.Registry{}
  85. if err := RegisterDefaultInterceptors(m, i); err != nil {
  86. return nil, err
  87. }
  88. api := NewAPI(WithMediaEngine(m), WithInterceptorRegistry(i))
  89. return api.NewPeerConnection(configuration)
  90. }
  91. // NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object
  92. func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
  93. // https://w3c.github.io/webrtc-pc/#constructor (Step #2)
  94. // Some variables defined explicitly despite their implicit zero values to
  95. // allow better readability to understand what is happening.
  96. pc := &PeerConnection{
  97. statsID: fmt.Sprintf("PeerConnection-%d", time.Now().UnixNano()),
  98. configuration: Configuration{
  99. ICEServers: []ICEServer{},
  100. ICETransportPolicy: ICETransportPolicyAll,
  101. BundlePolicy: BundlePolicyBalanced,
  102. RTCPMuxPolicy: RTCPMuxPolicyRequire,
  103. Certificates: []Certificate{},
  104. ICECandidatePoolSize: 0,
  105. },
  106. ops: newOperations(),
  107. isClosed: &atomicBool{},
  108. isNegotiationNeeded: &atomicBool{},
  109. negotiationNeededState: negotiationNeededStateEmpty,
  110. lastOffer: "",
  111. lastAnswer: "",
  112. greaterMid: -1,
  113. signalingState: SignalingStateStable,
  114. api: api,
  115. log: api.settingEngine.LoggerFactory.NewLogger("pc"),
  116. }
  117. pc.iceConnectionState.Store(ICEConnectionStateNew)
  118. pc.connectionState.Store(PeerConnectionStateNew)
  119. i, err := api.interceptorRegistry.Build("")
  120. if err != nil {
  121. return nil, err
  122. }
  123. pc.api = &API{
  124. settingEngine: api.settingEngine,
  125. interceptor: i,
  126. }
  127. if api.settingEngine.disableMediaEngineCopy {
  128. pc.api.mediaEngine = api.mediaEngine
  129. } else {
  130. pc.api.mediaEngine = api.mediaEngine.copy()
  131. }
  132. if err = pc.initConfiguration(configuration); err != nil {
  133. return nil, err
  134. }
  135. pc.iceGatherer, err = pc.createICEGatherer()
  136. if err != nil {
  137. return nil, err
  138. }
  139. // Create the ice transport
  140. iceTransport := pc.createICETransport()
  141. pc.iceTransport = iceTransport
  142. // Create the DTLS transport
  143. dtlsTransport, err := pc.api.NewDTLSTransport(pc.iceTransport, pc.configuration.Certificates)
  144. if err != nil {
  145. return nil, err
  146. }
  147. pc.dtlsTransport = dtlsTransport
  148. // Create the SCTP transport
  149. pc.sctpTransport = pc.api.NewSCTPTransport(pc.dtlsTransport)
  150. // Wire up the on datachannel handler
  151. pc.sctpTransport.OnDataChannel(func(d *DataChannel) {
  152. pc.mu.RLock()
  153. handler := pc.onDataChannelHandler
  154. pc.mu.RUnlock()
  155. if handler != nil {
  156. handler(d)
  157. }
  158. })
  159. pc.interceptorRTCPWriter = pc.api.interceptor.BindRTCPWriter(interceptor.RTCPWriterFunc(pc.writeRTCP))
  160. return pc, nil
  161. }
  162. // initConfiguration defines validation of the specified Configuration and
  163. // its assignment to the internal configuration variable. This function differs
  164. // from its SetConfiguration counterpart because most of the checks do not
  165. // include verification statements related to the existing state. Thus the
  166. // function describes only minor verification of some the struct variables.
  167. func (pc *PeerConnection) initConfiguration(configuration Configuration) error {
  168. if configuration.PeerIdentity != "" {
  169. pc.configuration.PeerIdentity = configuration.PeerIdentity
  170. }
  171. // https://www.w3.org/TR/webrtc/#constructor (step #3)
  172. if len(configuration.Certificates) > 0 {
  173. now := time.Now()
  174. for _, x509Cert := range configuration.Certificates {
  175. if !x509Cert.Expires().IsZero() && now.After(x509Cert.Expires()) {
  176. return &rtcerr.InvalidAccessError{Err: ErrCertificateExpired}
  177. }
  178. pc.configuration.Certificates = append(pc.configuration.Certificates, x509Cert)
  179. }
  180. } else {
  181. sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  182. if err != nil {
  183. return &rtcerr.UnknownError{Err: err}
  184. }
  185. certificate, err := GenerateCertificate(sk)
  186. if err != nil {
  187. return err
  188. }
  189. pc.configuration.Certificates = []Certificate{*certificate}
  190. }
  191. if configuration.BundlePolicy != BundlePolicy(Unknown) {
  192. pc.configuration.BundlePolicy = configuration.BundlePolicy
  193. }
  194. if configuration.RTCPMuxPolicy != RTCPMuxPolicy(Unknown) {
  195. pc.configuration.RTCPMuxPolicy = configuration.RTCPMuxPolicy
  196. }
  197. if configuration.ICECandidatePoolSize != 0 {
  198. pc.configuration.ICECandidatePoolSize = configuration.ICECandidatePoolSize
  199. }
  200. if configuration.ICETransportPolicy != ICETransportPolicy(Unknown) {
  201. pc.configuration.ICETransportPolicy = configuration.ICETransportPolicy
  202. }
  203. if configuration.SDPSemantics != SDPSemantics(Unknown) {
  204. pc.configuration.SDPSemantics = configuration.SDPSemantics
  205. }
  206. sanitizedICEServers := configuration.getICEServers()
  207. if len(sanitizedICEServers) > 0 {
  208. for _, server := range sanitizedICEServers {
  209. if err := server.validate(); err != nil {
  210. return err
  211. }
  212. }
  213. pc.configuration.ICEServers = sanitizedICEServers
  214. }
  215. return nil
  216. }
  217. // OnSignalingStateChange sets an event handler which is invoked when the
  218. // peer connection's signaling state changes
  219. func (pc *PeerConnection) OnSignalingStateChange(f func(SignalingState)) {
  220. pc.mu.Lock()
  221. defer pc.mu.Unlock()
  222. pc.onSignalingStateChangeHandler = f
  223. }
  224. func (pc *PeerConnection) onSignalingStateChange(newState SignalingState) {
  225. pc.mu.RLock()
  226. handler := pc.onSignalingStateChangeHandler
  227. pc.mu.RUnlock()
  228. pc.log.Infof("signaling state changed to %s", newState)
  229. if handler != nil {
  230. go handler(newState)
  231. }
  232. }
  233. // OnDataChannel sets an event handler which is invoked when a data
  234. // channel message arrives from a remote peer.
  235. func (pc *PeerConnection) OnDataChannel(f func(*DataChannel)) {
  236. pc.mu.Lock()
  237. defer pc.mu.Unlock()
  238. pc.onDataChannelHandler = f
  239. }
  240. // OnNegotiationNeeded sets an event handler which is invoked when
  241. // a change has occurred which requires session negotiation
  242. func (pc *PeerConnection) OnNegotiationNeeded(f func()) {
  243. pc.onNegotiationNeededHandler.Store(f)
  244. }
  245. // onNegotiationNeeded enqueues negotiationNeededOp if necessary
  246. // caller of this method should hold `pc.mu` lock
  247. func (pc *PeerConnection) onNegotiationNeeded() {
  248. // https://w3c.github.io/webrtc-pc/#updating-the-negotiation-needed-flag
  249. // non-canon step 1
  250. if pc.negotiationNeededState == negotiationNeededStateRun {
  251. pc.negotiationNeededState = negotiationNeededStateQueue
  252. return
  253. } else if pc.negotiationNeededState == negotiationNeededStateQueue {
  254. return
  255. }
  256. pc.negotiationNeededState = negotiationNeededStateRun
  257. pc.ops.Enqueue(pc.negotiationNeededOp)
  258. }
  259. func (pc *PeerConnection) negotiationNeededOp() {
  260. // Don't run NegotiatedNeeded checks if OnNegotiationNeeded is not set
  261. if handler, ok := pc.onNegotiationNeededHandler.Load().(func()); !ok || handler == nil {
  262. return
  263. }
  264. // https://www.w3.org/TR/webrtc/#updating-the-negotiation-needed-flag
  265. // Step 2.1
  266. if pc.isClosed.get() {
  267. return
  268. }
  269. // non-canon step 2.2
  270. if !pc.ops.IsEmpty() {
  271. pc.ops.Enqueue(pc.negotiationNeededOp)
  272. return
  273. }
  274. // non-canon, run again if there was a request
  275. defer func() {
  276. pc.mu.Lock()
  277. defer pc.mu.Unlock()
  278. if pc.negotiationNeededState == negotiationNeededStateQueue {
  279. defer pc.onNegotiationNeeded()
  280. }
  281. pc.negotiationNeededState = negotiationNeededStateEmpty
  282. }()
  283. // Step 2.3
  284. if pc.SignalingState() != SignalingStateStable {
  285. return
  286. }
  287. // Step 2.4
  288. if !pc.checkNegotiationNeeded() {
  289. pc.isNegotiationNeeded.set(false)
  290. return
  291. }
  292. // Step 2.5
  293. if pc.isNegotiationNeeded.get() {
  294. return
  295. }
  296. // Step 2.6
  297. pc.isNegotiationNeeded.set(true)
  298. // Step 2.7
  299. if handler, ok := pc.onNegotiationNeededHandler.Load().(func()); ok && handler != nil {
  300. handler()
  301. }
  302. }
  303. func (pc *PeerConnection) checkNegotiationNeeded() bool { //nolint:gocognit
  304. // To check if negotiation is needed for connection, perform the following checks:
  305. // Skip 1, 2 steps
  306. // Step 3
  307. pc.mu.Lock()
  308. defer pc.mu.Unlock()
  309. localDesc := pc.currentLocalDescription
  310. remoteDesc := pc.currentRemoteDescription
  311. if localDesc == nil {
  312. return true
  313. }
  314. pc.sctpTransport.lock.Lock()
  315. lenDataChannel := len(pc.sctpTransport.dataChannels)
  316. pc.sctpTransport.lock.Unlock()
  317. if lenDataChannel != 0 && haveDataChannel(localDesc) == nil {
  318. return true
  319. }
  320. for _, t := range pc.rtpTransceivers {
  321. // https://www.w3.org/TR/webrtc/#dfn-update-the-negotiation-needed-flag
  322. // Step 5.1
  323. // if t.stopping && !t.stopped {
  324. // return true
  325. // }
  326. m := getByMid(t.Mid(), localDesc)
  327. // Step 5.2
  328. if !t.stopped && m == nil {
  329. return true
  330. }
  331. if !t.stopped && m != nil {
  332. // Step 5.3.1
  333. if t.Direction() == RTPTransceiverDirectionSendrecv || t.Direction() == RTPTransceiverDirectionSendonly {
  334. descMsid, okMsid := m.Attribute(sdp.AttrKeyMsid)
  335. sender := t.Sender()
  336. if sender == nil {
  337. return true
  338. }
  339. track := sender.Track()
  340. if !okMsid || descMsid != track.StreamID()+" "+track.ID() {
  341. return true
  342. }
  343. }
  344. switch localDesc.Type {
  345. case SDPTypeOffer:
  346. // Step 5.3.2
  347. rm := getByMid(t.Mid(), remoteDesc)
  348. if rm == nil {
  349. return true
  350. }
  351. if getPeerDirection(m) != t.Direction() && getPeerDirection(rm) != t.Direction().Revers() {
  352. return true
  353. }
  354. case SDPTypeAnswer:
  355. // Step 5.3.3
  356. if _, ok := m.Attribute(t.Direction().String()); !ok {
  357. return true
  358. }
  359. default:
  360. }
  361. }
  362. // Step 5.4
  363. if t.stopped && t.Mid() != "" {
  364. if getByMid(t.Mid(), localDesc) != nil || getByMid(t.Mid(), remoteDesc) != nil {
  365. return true
  366. }
  367. }
  368. }
  369. // Step 6
  370. return false
  371. }
  372. // OnICECandidate sets an event handler which is invoked when a new ICE
  373. // candidate is found.
  374. // ICE candidate gathering only begins when SetLocalDescription or
  375. // SetRemoteDescription is called.
  376. // Take note that the handler will be called with a nil pointer when
  377. // gathering is finished.
  378. func (pc *PeerConnection) OnICECandidate(f func(*ICECandidate)) {
  379. pc.iceGatherer.OnLocalCandidate(f)
  380. }
  381. // OnICEGatheringStateChange sets an event handler which is invoked when the
  382. // ICE candidate gathering state has changed.
  383. func (pc *PeerConnection) OnICEGatheringStateChange(f func(ICEGathererState)) {
  384. pc.iceGatherer.OnStateChange(f)
  385. }
  386. // OnTrack sets an event handler which is called when remote track
  387. // arrives from a remote peer.
  388. func (pc *PeerConnection) OnTrack(f func(*TrackRemote, *RTPReceiver)) {
  389. pc.mu.Lock()
  390. defer pc.mu.Unlock()
  391. pc.onTrackHandler = f
  392. }
  393. func (pc *PeerConnection) onTrack(t *TrackRemote, r *RTPReceiver) {
  394. pc.mu.RLock()
  395. handler := pc.onTrackHandler
  396. pc.mu.RUnlock()
  397. pc.log.Debugf("got new track: %+v", t)
  398. if t != nil {
  399. if handler != nil {
  400. go handler(t, r)
  401. } else {
  402. pc.log.Warnf("OnTrack unset, unable to handle incoming media streams")
  403. }
  404. }
  405. }
  406. // OnICEConnectionStateChange sets an event handler which is called
  407. // when an ICE connection state is changed.
  408. func (pc *PeerConnection) OnICEConnectionStateChange(f func(ICEConnectionState)) {
  409. pc.onICEConnectionStateChangeHandler.Store(f)
  410. }
  411. func (pc *PeerConnection) onICEConnectionStateChange(cs ICEConnectionState) {
  412. pc.iceConnectionState.Store(cs)
  413. pc.log.Infof("ICE connection state changed: %s", cs)
  414. if handler, ok := pc.onICEConnectionStateChangeHandler.Load().(func(ICEConnectionState)); ok && handler != nil {
  415. handler(cs)
  416. }
  417. }
  418. // OnConnectionStateChange sets an event handler which is called
  419. // when the PeerConnectionState has changed
  420. func (pc *PeerConnection) OnConnectionStateChange(f func(PeerConnectionState)) {
  421. pc.onConnectionStateChangeHandler.Store(f)
  422. }
  423. func (pc *PeerConnection) onConnectionStateChange(cs PeerConnectionState) {
  424. pc.connectionState.Store(cs)
  425. pc.log.Infof("peer connection state changed: %s", cs)
  426. if handler, ok := pc.onConnectionStateChangeHandler.Load().(func(PeerConnectionState)); ok && handler != nil {
  427. go handler(cs)
  428. }
  429. }
  430. // SetConfiguration updates the configuration of this PeerConnection object.
  431. func (pc *PeerConnection) SetConfiguration(configuration Configuration) error { //nolint:gocognit
  432. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-setconfiguration (step #2)
  433. if pc.isClosed.get() {
  434. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  435. }
  436. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #3)
  437. if configuration.PeerIdentity != "" {
  438. if configuration.PeerIdentity != pc.configuration.PeerIdentity {
  439. return &rtcerr.InvalidModificationError{Err: ErrModifyingPeerIdentity}
  440. }
  441. pc.configuration.PeerIdentity = configuration.PeerIdentity
  442. }
  443. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #4)
  444. if len(configuration.Certificates) > 0 {
  445. if len(configuration.Certificates) != len(pc.configuration.Certificates) {
  446. return &rtcerr.InvalidModificationError{Err: ErrModifyingCertificates}
  447. }
  448. for i, certificate := range configuration.Certificates {
  449. if !pc.configuration.Certificates[i].Equals(certificate) {
  450. return &rtcerr.InvalidModificationError{Err: ErrModifyingCertificates}
  451. }
  452. }
  453. pc.configuration.Certificates = configuration.Certificates
  454. }
  455. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #5)
  456. if configuration.BundlePolicy != BundlePolicy(Unknown) {
  457. if configuration.BundlePolicy != pc.configuration.BundlePolicy {
  458. return &rtcerr.InvalidModificationError{Err: ErrModifyingBundlePolicy}
  459. }
  460. pc.configuration.BundlePolicy = configuration.BundlePolicy
  461. }
  462. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #6)
  463. if configuration.RTCPMuxPolicy != RTCPMuxPolicy(Unknown) {
  464. if configuration.RTCPMuxPolicy != pc.configuration.RTCPMuxPolicy {
  465. return &rtcerr.InvalidModificationError{Err: ErrModifyingRTCPMuxPolicy}
  466. }
  467. pc.configuration.RTCPMuxPolicy = configuration.RTCPMuxPolicy
  468. }
  469. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #7)
  470. if configuration.ICECandidatePoolSize != 0 {
  471. if pc.configuration.ICECandidatePoolSize != configuration.ICECandidatePoolSize &&
  472. pc.LocalDescription() != nil {
  473. return &rtcerr.InvalidModificationError{Err: ErrModifyingICECandidatePoolSize}
  474. }
  475. pc.configuration.ICECandidatePoolSize = configuration.ICECandidatePoolSize
  476. }
  477. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #8)
  478. if configuration.ICETransportPolicy != ICETransportPolicy(Unknown) {
  479. pc.configuration.ICETransportPolicy = configuration.ICETransportPolicy
  480. }
  481. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11)
  482. if len(configuration.ICEServers) > 0 {
  483. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3)
  484. for _, server := range configuration.ICEServers {
  485. if err := server.validate(); err != nil {
  486. return err
  487. }
  488. }
  489. pc.configuration.ICEServers = configuration.ICEServers
  490. }
  491. return nil
  492. }
  493. // GetConfiguration returns a Configuration object representing the current
  494. // configuration of this PeerConnection object. The returned object is a
  495. // copy and direct mutation on it will not take affect until SetConfiguration
  496. // has been called with Configuration passed as its only argument.
  497. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-getconfiguration
  498. func (pc *PeerConnection) GetConfiguration() Configuration {
  499. return pc.configuration
  500. }
  501. func (pc *PeerConnection) getStatsID() string {
  502. pc.mu.RLock()
  503. defer pc.mu.RUnlock()
  504. return pc.statsID
  505. }
  506. // hasLocalDescriptionChanged returns whether local media (rtpTransceivers) has changed
  507. // caller of this method should hold `pc.mu` lock
  508. func (pc *PeerConnection) hasLocalDescriptionChanged(desc *SessionDescription) bool {
  509. for _, t := range pc.rtpTransceivers {
  510. m := getByMid(t.Mid(), desc)
  511. if m == nil {
  512. return true
  513. }
  514. if getPeerDirection(m) != t.Direction() {
  515. return true
  516. }
  517. }
  518. return false
  519. }
  520. // CreateOffer starts the PeerConnection and generates the localDescription
  521. // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer
  522. func (pc *PeerConnection) CreateOffer(options *OfferOptions) (SessionDescription, error) { //nolint:gocognit
  523. useIdentity := pc.idpLoginURL != nil
  524. switch {
  525. case useIdentity:
  526. return SessionDescription{}, errIdentityProviderNotImplemented
  527. case pc.isClosed.get():
  528. return SessionDescription{}, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  529. }
  530. if options != nil && options.ICERestart {
  531. if err := pc.iceTransport.restart(); err != nil {
  532. return SessionDescription{}, err
  533. }
  534. }
  535. var (
  536. d *sdp.SessionDescription
  537. offer SessionDescription
  538. err error
  539. )
  540. // This may be necessary to recompute if, for example, createOffer was called when only an
  541. // audio RTCRtpTransceiver was added to connection, but while performing the in-parallel
  542. // steps to create an offer, a video RTCRtpTransceiver was added, requiring additional
  543. // inspection of video system resources.
  544. count := 0
  545. pc.mu.Lock()
  546. defer pc.mu.Unlock()
  547. for {
  548. // We cache current transceivers to ensure they aren't
  549. // mutated during offer generation. We later check if they have
  550. // been mutated and recompute the offer if necessary.
  551. currentTransceivers := pc.rtpTransceivers
  552. // in-parallel steps to create an offer
  553. // https://w3c.github.io/webrtc-pc/#dfn-in-parallel-steps-to-create-an-offer
  554. isPlanB := pc.configuration.SDPSemantics == SDPSemanticsPlanB
  555. if pc.currentRemoteDescription != nil && isPlanB {
  556. isPlanB = descriptionPossiblyPlanB(pc.currentRemoteDescription)
  557. }
  558. // include unmatched local transceivers
  559. if !isPlanB {
  560. // update the greater mid if the remote description provides a greater one
  561. if pc.currentRemoteDescription != nil {
  562. var numericMid int
  563. for _, media := range pc.currentRemoteDescription.parsed.MediaDescriptions {
  564. mid := getMidValue(media)
  565. if mid == "" {
  566. continue
  567. }
  568. numericMid, err = strconv.Atoi(mid)
  569. if err != nil {
  570. continue
  571. }
  572. if numericMid > pc.greaterMid {
  573. pc.greaterMid = numericMid
  574. }
  575. }
  576. }
  577. for _, t := range currentTransceivers {
  578. if mid := t.Mid(); mid != "" {
  579. numericMid, errMid := strconv.Atoi(mid)
  580. if errMid == nil {
  581. if numericMid > pc.greaterMid {
  582. pc.greaterMid = numericMid
  583. }
  584. }
  585. continue
  586. }
  587. pc.greaterMid++
  588. err = t.SetMid(strconv.Itoa(pc.greaterMid))
  589. if err != nil {
  590. return SessionDescription{}, err
  591. }
  592. }
  593. }
  594. if pc.currentRemoteDescription == nil {
  595. d, err = pc.generateUnmatchedSDP(currentTransceivers, useIdentity)
  596. } else {
  597. d, err = pc.generateMatchedSDP(currentTransceivers, useIdentity, true /*includeUnmatched */, connectionRoleFromDtlsRole(defaultDtlsRoleOffer))
  598. }
  599. if err != nil {
  600. return SessionDescription{}, err
  601. }
  602. updateSDPOrigin(&pc.sdpOrigin, d)
  603. sdpBytes, err := d.Marshal()
  604. if err != nil {
  605. return SessionDescription{}, err
  606. }
  607. offer = SessionDescription{
  608. Type: SDPTypeOffer,
  609. SDP: string(sdpBytes),
  610. parsed: d,
  611. }
  612. // Verify local media hasn't changed during offer
  613. // generation. Recompute if necessary
  614. if isPlanB || !pc.hasLocalDescriptionChanged(&offer) {
  615. break
  616. }
  617. count++
  618. if count >= 128 {
  619. return SessionDescription{}, errExcessiveRetries
  620. }
  621. }
  622. pc.lastOffer = offer.SDP
  623. return offer, nil
  624. }
  625. func (pc *PeerConnection) createICEGatherer() (*ICEGatherer, error) {
  626. g, err := pc.api.NewICEGatherer(ICEGatherOptions{
  627. ICEServers: pc.configuration.getICEServers(),
  628. ICEGatherPolicy: pc.configuration.ICETransportPolicy,
  629. })
  630. if err != nil {
  631. return nil, err
  632. }
  633. return g, nil
  634. }
  635. // Update the PeerConnectionState given the state of relevant transports
  636. // https://www.w3.org/TR/webrtc/#rtcpeerconnectionstate-enum
  637. func (pc *PeerConnection) updateConnectionState(iceConnectionState ICEConnectionState, dtlsTransportState DTLSTransportState) {
  638. connectionState := PeerConnectionStateNew
  639. switch {
  640. // The RTCPeerConnection object's [[IsClosed]] slot is true.
  641. case pc.isClosed.get():
  642. connectionState = PeerConnectionStateClosed
  643. // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
  644. case iceConnectionState == ICEConnectionStateFailed || dtlsTransportState == DTLSTransportStateFailed:
  645. connectionState = PeerConnectionStateFailed
  646. // Any of the RTCIceTransports or RTCDtlsTransports are in the "disconnected"
  647. // state and none of them are in the "failed" or "connecting" or "checking" state. */
  648. case iceConnectionState == ICEConnectionStateDisconnected:
  649. connectionState = PeerConnectionStateDisconnected
  650. // None of the previous states apply and all RTCIceTransports are in the "new" or "closed" state,
  651. // and all RTCDtlsTransports are in the "new" or "closed" state, or there are no transports.
  652. case (iceConnectionState == ICEConnectionStateNew || iceConnectionState == ICEConnectionStateClosed) &&
  653. (dtlsTransportState == DTLSTransportStateNew || dtlsTransportState == DTLSTransportStateClosed):
  654. connectionState = PeerConnectionStateNew
  655. // None of the previous states apply and any RTCIceTransport is in the "new" or "checking" state or
  656. // any RTCDtlsTransport is in the "new" or "connecting" state.
  657. case (iceConnectionState == ICEConnectionStateNew || iceConnectionState == ICEConnectionStateChecking) ||
  658. (dtlsTransportState == DTLSTransportStateNew || dtlsTransportState == DTLSTransportStateConnecting):
  659. connectionState = PeerConnectionStateConnecting
  660. // All RTCIceTransports and RTCDtlsTransports are in the "connected", "completed" or "closed"
  661. // state and all RTCDtlsTransports are in the "connected" or "closed" state.
  662. case (iceConnectionState == ICEConnectionStateConnected || iceConnectionState == ICEConnectionStateCompleted || iceConnectionState == ICEConnectionStateClosed) &&
  663. (dtlsTransportState == DTLSTransportStateConnected || dtlsTransportState == DTLSTransportStateClosed):
  664. connectionState = PeerConnectionStateConnected
  665. }
  666. if pc.connectionState.Load() == connectionState {
  667. return
  668. }
  669. pc.onConnectionStateChange(connectionState)
  670. }
  671. func (pc *PeerConnection) createICETransport() *ICETransport {
  672. t := pc.api.NewICETransport(pc.iceGatherer)
  673. t.internalOnConnectionStateChangeHandler.Store(func(state ICETransportState) {
  674. var cs ICEConnectionState
  675. switch state {
  676. case ICETransportStateNew:
  677. cs = ICEConnectionStateNew
  678. case ICETransportStateChecking:
  679. cs = ICEConnectionStateChecking
  680. case ICETransportStateConnected:
  681. cs = ICEConnectionStateConnected
  682. case ICETransportStateCompleted:
  683. cs = ICEConnectionStateCompleted
  684. case ICETransportStateFailed:
  685. cs = ICEConnectionStateFailed
  686. case ICETransportStateDisconnected:
  687. cs = ICEConnectionStateDisconnected
  688. case ICETransportStateClosed:
  689. cs = ICEConnectionStateClosed
  690. default:
  691. pc.log.Warnf("OnConnectionStateChange: unhandled ICE state: %s", state)
  692. return
  693. }
  694. pc.onICEConnectionStateChange(cs)
  695. pc.updateConnectionState(cs, pc.dtlsTransport.State())
  696. })
  697. return t
  698. }
  699. // CreateAnswer starts the PeerConnection and generates the localDescription
  700. func (pc *PeerConnection) CreateAnswer(*AnswerOptions) (SessionDescription, error) {
  701. useIdentity := pc.idpLoginURL != nil
  702. remoteDesc := pc.RemoteDescription()
  703. switch {
  704. case remoteDesc == nil:
  705. return SessionDescription{}, &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
  706. case useIdentity:
  707. return SessionDescription{}, errIdentityProviderNotImplemented
  708. case pc.isClosed.get():
  709. return SessionDescription{}, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  710. case pc.signalingState.Get() != SignalingStateHaveRemoteOffer && pc.signalingState.Get() != SignalingStateHaveLocalPranswer:
  711. return SessionDescription{}, &rtcerr.InvalidStateError{Err: ErrIncorrectSignalingState}
  712. }
  713. connectionRole := connectionRoleFromDtlsRole(pc.api.settingEngine.answeringDTLSRole)
  714. if connectionRole == sdp.ConnectionRole(0) {
  715. connectionRole = connectionRoleFromDtlsRole(defaultDtlsRoleAnswer)
  716. // If one of the agents is lite and the other one is not, the lite agent must be the controlled agent.
  717. // If both or neither agents are lite the offering agent is controlling.
  718. // RFC 8445 S6.1.1
  719. if isIceLiteSet(remoteDesc.parsed) && !pc.api.settingEngine.candidates.ICELite {
  720. connectionRole = connectionRoleFromDtlsRole(DTLSRoleServer)
  721. }
  722. }
  723. pc.mu.Lock()
  724. defer pc.mu.Unlock()
  725. d, err := pc.generateMatchedSDP(pc.rtpTransceivers, useIdentity, false /*includeUnmatched */, connectionRole)
  726. if err != nil {
  727. return SessionDescription{}, err
  728. }
  729. updateSDPOrigin(&pc.sdpOrigin, d)
  730. sdpBytes, err := d.Marshal()
  731. if err != nil {
  732. return SessionDescription{}, err
  733. }
  734. desc := SessionDescription{
  735. Type: SDPTypeAnswer,
  736. SDP: string(sdpBytes),
  737. parsed: d,
  738. }
  739. pc.lastAnswer = desc.SDP
  740. return desc, nil
  741. }
  742. // 4.4.1.6 Set the SessionDescription
  743. func (pc *PeerConnection) setDescription(sd *SessionDescription, op stateChangeOp) error { //nolint:gocognit
  744. switch {
  745. case pc.isClosed.get():
  746. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  747. case NewSDPType(sd.Type.String()) == SDPType(Unknown):
  748. return &rtcerr.TypeError{Err: fmt.Errorf("%w: '%d' is not a valid enum value of type SDPType", errPeerConnSDPTypeInvalidValue, sd.Type)}
  749. }
  750. nextState, err := func() (SignalingState, error) {
  751. pc.mu.Lock()
  752. defer pc.mu.Unlock()
  753. cur := pc.SignalingState()
  754. setLocal := stateChangeOpSetLocal
  755. setRemote := stateChangeOpSetRemote
  756. newSDPDoesNotMatchOffer := &rtcerr.InvalidModificationError{Err: errSDPDoesNotMatchOffer}
  757. newSDPDoesNotMatchAnswer := &rtcerr.InvalidModificationError{Err: errSDPDoesNotMatchAnswer}
  758. var nextState SignalingState
  759. var err error
  760. switch op {
  761. case setLocal:
  762. switch sd.Type {
  763. // stable->SetLocal(offer)->have-local-offer
  764. case SDPTypeOffer:
  765. if sd.SDP != pc.lastOffer {
  766. return nextState, newSDPDoesNotMatchOffer
  767. }
  768. nextState, err = checkNextSignalingState(cur, SignalingStateHaveLocalOffer, setLocal, sd.Type)
  769. if err == nil {
  770. pc.pendingLocalDescription = sd
  771. }
  772. // have-remote-offer->SetLocal(answer)->stable
  773. // have-local-pranswer->SetLocal(answer)->stable
  774. case SDPTypeAnswer:
  775. if sd.SDP != pc.lastAnswer {
  776. return nextState, newSDPDoesNotMatchAnswer
  777. }
  778. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setLocal, sd.Type)
  779. if err == nil {
  780. pc.currentLocalDescription = sd
  781. pc.currentRemoteDescription = pc.pendingRemoteDescription
  782. pc.pendingRemoteDescription = nil
  783. pc.pendingLocalDescription = nil
  784. }
  785. case SDPTypeRollback:
  786. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setLocal, sd.Type)
  787. if err == nil {
  788. pc.pendingLocalDescription = nil
  789. }
  790. // have-remote-offer->SetLocal(pranswer)->have-local-pranswer
  791. case SDPTypePranswer:
  792. if sd.SDP != pc.lastAnswer {
  793. return nextState, newSDPDoesNotMatchAnswer
  794. }
  795. nextState, err = checkNextSignalingState(cur, SignalingStateHaveLocalPranswer, setLocal, sd.Type)
  796. if err == nil {
  797. pc.pendingLocalDescription = sd
  798. }
  799. default:
  800. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %s(%s)", errPeerConnStateChangeInvalid, op, sd.Type)}
  801. }
  802. case setRemote:
  803. switch sd.Type {
  804. // stable->SetRemote(offer)->have-remote-offer
  805. case SDPTypeOffer:
  806. nextState, err = checkNextSignalingState(cur, SignalingStateHaveRemoteOffer, setRemote, sd.Type)
  807. if err == nil {
  808. pc.pendingRemoteDescription = sd
  809. }
  810. // have-local-offer->SetRemote(answer)->stable
  811. // have-remote-pranswer->SetRemote(answer)->stable
  812. case SDPTypeAnswer:
  813. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setRemote, sd.Type)
  814. if err == nil {
  815. pc.currentRemoteDescription = sd
  816. pc.currentLocalDescription = pc.pendingLocalDescription
  817. pc.pendingRemoteDescription = nil
  818. pc.pendingLocalDescription = nil
  819. }
  820. case SDPTypeRollback:
  821. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setRemote, sd.Type)
  822. if err == nil {
  823. pc.pendingRemoteDescription = nil
  824. }
  825. // have-local-offer->SetRemote(pranswer)->have-remote-pranswer
  826. case SDPTypePranswer:
  827. nextState, err = checkNextSignalingState(cur, SignalingStateHaveRemotePranswer, setRemote, sd.Type)
  828. if err == nil {
  829. pc.pendingRemoteDescription = sd
  830. }
  831. default:
  832. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %s(%s)", errPeerConnStateChangeInvalid, op, sd.Type)}
  833. }
  834. default:
  835. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %q", errPeerConnStateChangeUnhandled, op)}
  836. }
  837. return nextState, err
  838. }()
  839. if err == nil {
  840. pc.signalingState.Set(nextState)
  841. if pc.signalingState.Get() == SignalingStateStable {
  842. pc.isNegotiationNeeded.set(false)
  843. pc.mu.Lock()
  844. pc.onNegotiationNeeded()
  845. pc.mu.Unlock()
  846. }
  847. pc.onSignalingStateChange(nextState)
  848. }
  849. return err
  850. }
  851. // SetLocalDescription sets the SessionDescription of the local peer
  852. func (pc *PeerConnection) SetLocalDescription(desc SessionDescription) error {
  853. if pc.isClosed.get() {
  854. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  855. }
  856. haveLocalDescription := pc.currentLocalDescription != nil
  857. // JSEP 5.4
  858. if desc.SDP == "" {
  859. switch desc.Type {
  860. case SDPTypeAnswer, SDPTypePranswer:
  861. desc.SDP = pc.lastAnswer
  862. case SDPTypeOffer:
  863. desc.SDP = pc.lastOffer
  864. default:
  865. return &rtcerr.InvalidModificationError{
  866. Err: fmt.Errorf("%w: %s", errPeerConnSDPTypeInvalidValueSetLocalDescription, desc.Type),
  867. }
  868. }
  869. }
  870. desc.parsed = &sdp.SessionDescription{}
  871. if err := desc.parsed.Unmarshal([]byte(desc.SDP)); err != nil {
  872. return err
  873. }
  874. if err := pc.setDescription(&desc, stateChangeOpSetLocal); err != nil {
  875. return err
  876. }
  877. currentTransceivers := append([]*RTPTransceiver{}, pc.GetTransceivers()...)
  878. weAnswer := desc.Type == SDPTypeAnswer
  879. remoteDesc := pc.RemoteDescription()
  880. if weAnswer && remoteDesc != nil {
  881. _ = setRTPTransceiverCurrentDirection(&desc, currentTransceivers, false)
  882. if err := pc.startRTPSenders(currentTransceivers); err != nil {
  883. return err
  884. }
  885. pc.configureRTPReceivers(haveLocalDescription, remoteDesc, currentTransceivers)
  886. pc.ops.Enqueue(func() {
  887. pc.startRTP(haveLocalDescription, remoteDesc, currentTransceivers)
  888. })
  889. }
  890. if pc.iceGatherer.State() == ICEGathererStateNew {
  891. return pc.iceGatherer.Gather()
  892. }
  893. return nil
  894. }
  895. // LocalDescription returns PendingLocalDescription if it is not null and
  896. // otherwise it returns CurrentLocalDescription. This property is used to
  897. // determine if SetLocalDescription has already been called.
  898. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-localdescription
  899. func (pc *PeerConnection) LocalDescription() *SessionDescription {
  900. if pendingLocalDescription := pc.PendingLocalDescription(); pendingLocalDescription != nil {
  901. return pendingLocalDescription
  902. }
  903. return pc.CurrentLocalDescription()
  904. }
  905. // SetRemoteDescription sets the SessionDescription of the remote peer
  906. func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error { //nolint:gocognit,gocyclo
  907. if pc.isClosed.get() {
  908. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  909. }
  910. isRenegotiation := pc.currentRemoteDescription != nil
  911. if _, err := desc.Unmarshal(); err != nil {
  912. return err
  913. }
  914. if err := pc.setDescription(&desc, stateChangeOpSetRemote); err != nil {
  915. return err
  916. }
  917. if err := pc.api.mediaEngine.updateFromRemoteDescription(*desc.parsed); err != nil {
  918. return err
  919. }
  920. var t *RTPTransceiver
  921. localTransceivers := append([]*RTPTransceiver{}, pc.GetTransceivers()...)
  922. detectedPlanB := descriptionIsPlanB(pc.RemoteDescription(), pc.log)
  923. if pc.configuration.SDPSemantics != SDPSemanticsUnifiedPlan {
  924. detectedPlanB = descriptionPossiblyPlanB(pc.RemoteDescription())
  925. }
  926. weOffer := desc.Type == SDPTypeAnswer
  927. if !weOffer && !detectedPlanB {
  928. for _, media := range pc.RemoteDescription().parsed.MediaDescriptions {
  929. midValue := getMidValue(media)
  930. if midValue == "" {
  931. return errPeerConnRemoteDescriptionWithoutMidValue
  932. }
  933. if media.MediaName.Media == mediaSectionApplication {
  934. continue
  935. }
  936. kind := NewRTPCodecType(media.MediaName.Media)
  937. direction := getPeerDirection(media)
  938. if kind == 0 || direction == RTPTransceiverDirection(Unknown) {
  939. continue
  940. }
  941. t, localTransceivers = findByMid(midValue, localTransceivers)
  942. if t == nil {
  943. t, localTransceivers = satisfyTypeAndDirection(kind, direction, localTransceivers)
  944. } else if direction == RTPTransceiverDirectionInactive {
  945. if err := t.Stop(); err != nil {
  946. return err
  947. }
  948. }
  949. switch {
  950. case t == nil:
  951. receiver, err := pc.api.NewRTPReceiver(kind, pc.dtlsTransport)
  952. if err != nil {
  953. return err
  954. }
  955. localDirection := RTPTransceiverDirectionRecvonly
  956. if direction == RTPTransceiverDirectionRecvonly {
  957. localDirection = RTPTransceiverDirectionSendonly
  958. } else if direction == RTPTransceiverDirectionInactive {
  959. localDirection = RTPTransceiverDirectionInactive
  960. }
  961. t = newRTPTransceiver(receiver, nil, localDirection, kind, pc.api)
  962. pc.mu.Lock()
  963. pc.addRTPTransceiver(t)
  964. pc.mu.Unlock()
  965. // if transceiver is create by remote sdp, set prefer codec same as remote peer
  966. if codecs, err := codecsFromMediaDescription(media); err == nil {
  967. filteredCodecs := []RTPCodecParameters{}
  968. for _, codec := range codecs {
  969. if c, matchType := codecParametersFuzzySearch(codec, pc.api.mediaEngine.getCodecsByKind(kind)); matchType == codecMatchExact {
  970. // if codec match exact, use payloadtype register to mediaengine
  971. codec.PayloadType = c.PayloadType
  972. filteredCodecs = append(filteredCodecs, codec)
  973. }
  974. }
  975. _ = t.SetCodecPreferences(filteredCodecs)
  976. }
  977. case direction == RTPTransceiverDirectionRecvonly:
  978. if t.Direction() == RTPTransceiverDirectionSendrecv {
  979. t.setDirection(RTPTransceiverDirectionSendonly)
  980. }
  981. case direction == RTPTransceiverDirectionSendrecv:
  982. if t.Direction() == RTPTransceiverDirectionSendonly {
  983. t.setDirection(RTPTransceiverDirectionSendrecv)
  984. }
  985. }
  986. if t.Mid() == "" {
  987. if err := t.SetMid(midValue); err != nil {
  988. return err
  989. }
  990. }
  991. }
  992. }
  993. remoteUfrag, remotePwd, candidates, err := extractICEDetails(desc.parsed, pc.log)
  994. if err != nil {
  995. return err
  996. }
  997. if isRenegotiation && pc.iceTransport.haveRemoteCredentialsChange(remoteUfrag, remotePwd) {
  998. // An ICE Restart only happens implicitly for a SetRemoteDescription of type offer
  999. if !weOffer {
  1000. if err = pc.iceTransport.restart(); err != nil {
  1001. return err
  1002. }
  1003. }
  1004. if err = pc.iceTransport.setRemoteCredentials(remoteUfrag, remotePwd); err != nil {
  1005. return err
  1006. }
  1007. }
  1008. for i := range candidates {
  1009. if err = pc.iceTransport.AddRemoteCandidate(&candidates[i]); err != nil {
  1010. return err
  1011. }
  1012. }
  1013. currentTransceivers := append([]*RTPTransceiver{}, pc.GetTransceivers()...)
  1014. if isRenegotiation {
  1015. if weOffer {
  1016. _ = setRTPTransceiverCurrentDirection(&desc, currentTransceivers, true)
  1017. if err = pc.startRTPSenders(currentTransceivers); err != nil {
  1018. return err
  1019. }
  1020. pc.configureRTPReceivers(true, &desc, currentTransceivers)
  1021. pc.ops.Enqueue(func() {
  1022. pc.startRTP(true, &desc, currentTransceivers)
  1023. })
  1024. }
  1025. return nil
  1026. }
  1027. remoteIsLite := isIceLiteSet(desc.parsed)
  1028. fingerprint, fingerprintHash, err := extractFingerprint(desc.parsed)
  1029. if err != nil {
  1030. return err
  1031. }
  1032. iceRole := ICERoleControlled
  1033. // If one of the agents is lite and the other one is not, the lite agent must be the controlled agent.
  1034. // If both or neither agents are lite the offering agent is controlling.
  1035. // RFC 8445 S6.1.1
  1036. if (weOffer && remoteIsLite == pc.api.settingEngine.candidates.ICELite) || (remoteIsLite && !pc.api.settingEngine.candidates.ICELite) {
  1037. iceRole = ICERoleControlling
  1038. }
  1039. // Start the networking in a new routine since it will block until
  1040. // the connection is actually established.
  1041. if weOffer {
  1042. _ = setRTPTransceiverCurrentDirection(&desc, currentTransceivers, true)
  1043. if err := pc.startRTPSenders(currentTransceivers); err != nil {
  1044. return err
  1045. }
  1046. pc.configureRTPReceivers(false, &desc, currentTransceivers)
  1047. }
  1048. pc.ops.Enqueue(func() {
  1049. pc.startTransports(iceRole, dtlsRoleFromRemoteSDP(desc.parsed), remoteUfrag, remotePwd, fingerprint, fingerprintHash)
  1050. if weOffer {
  1051. pc.startRTP(false, &desc, currentTransceivers)
  1052. }
  1053. })
  1054. return nil
  1055. }
  1056. func (pc *PeerConnection) configureReceiver(incoming trackDetails, receiver *RTPReceiver) {
  1057. receiver.configureReceive(trackDetailsToRTPReceiveParameters(&incoming))
  1058. // set track id and label early so they can be set as new track information
  1059. // is received from the SDP.
  1060. for i := range receiver.tracks {
  1061. receiver.tracks[i].track.mu.Lock()
  1062. receiver.tracks[i].track.id = incoming.id
  1063. receiver.tracks[i].track.streamID = incoming.streamID
  1064. receiver.tracks[i].track.mu.Unlock()
  1065. }
  1066. }
  1067. func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPReceiver) {
  1068. if err := receiver.startReceive(trackDetailsToRTPReceiveParameters(&incoming)); err != nil {
  1069. pc.log.Warnf("RTPReceiver Receive failed %s", err)
  1070. return
  1071. }
  1072. for _, t := range receiver.Tracks() {
  1073. if t.SSRC() == 0 || t.RID() != "" {
  1074. return
  1075. }
  1076. go func(track *TrackRemote) {
  1077. b := make([]byte, pc.api.settingEngine.getReceiveMTU())
  1078. n, _, err := track.peek(b)
  1079. if err != nil {
  1080. pc.log.Warnf("Could not determine PayloadType for SSRC %d (%s)", track.SSRC(), err)
  1081. return
  1082. }
  1083. if err = track.checkAndUpdateTrack(b[:n]); err != nil {
  1084. pc.log.Warnf("Failed to set codec settings for track SSRC %d (%s)", track.SSRC(), err)
  1085. return
  1086. }
  1087. pc.onTrack(track, receiver)
  1088. }(t)
  1089. }
  1090. }
  1091. func setRTPTransceiverCurrentDirection(answer *SessionDescription, currentTransceivers []*RTPTransceiver, weOffer bool) error {
  1092. currentTransceivers = append([]*RTPTransceiver{}, currentTransceivers...)
  1093. for _, media := range answer.parsed.MediaDescriptions {
  1094. midValue := getMidValue(media)
  1095. if midValue == "" {
  1096. return errPeerConnRemoteDescriptionWithoutMidValue
  1097. }
  1098. if media.MediaName.Media == mediaSectionApplication {
  1099. continue
  1100. }
  1101. var t *RTPTransceiver
  1102. t, currentTransceivers = findByMid(midValue, currentTransceivers)
  1103. if t == nil {
  1104. return fmt.Errorf("%w: %q", errPeerConnTranscieverMidNil, midValue)
  1105. }
  1106. direction := getPeerDirection(media)
  1107. if direction == RTPTransceiverDirection(Unknown) {
  1108. continue
  1109. }
  1110. // reverse direction if it was a remote answer
  1111. if weOffer {
  1112. switch direction {
  1113. case RTPTransceiverDirectionSendonly:
  1114. direction = RTPTransceiverDirectionRecvonly
  1115. case RTPTransceiverDirectionRecvonly:
  1116. direction = RTPTransceiverDirectionSendonly
  1117. default:
  1118. }
  1119. }
  1120. // If a transceiver is created by applying a remote description that has recvonly transceiver,
  1121. // it will have no sender. In this case, the transceiver's current direction is set to inactive so
  1122. // that the transceiver can be reused by next AddTrack.
  1123. if direction == RTPTransceiverDirectionSendonly && t.Sender() == nil {
  1124. direction = RTPTransceiverDirectionInactive
  1125. }
  1126. t.setCurrentDirection(direction)
  1127. }
  1128. return nil
  1129. }
  1130. func runIfNewReceiver(
  1131. incomingTrack trackDetails,
  1132. transceivers []*RTPTransceiver,
  1133. f func(incomingTrack trackDetails, receiver *RTPReceiver),
  1134. ) bool {
  1135. for _, t := range transceivers {
  1136. if t.Mid() != incomingTrack.mid {
  1137. continue
  1138. }
  1139. receiver := t.Receiver()
  1140. if (incomingTrack.kind != t.Kind()) ||
  1141. (t.Direction() != RTPTransceiverDirectionRecvonly && t.Direction() != RTPTransceiverDirectionSendrecv) ||
  1142. receiver == nil ||
  1143. (receiver.haveReceived()) {
  1144. continue
  1145. }
  1146. f(incomingTrack, receiver)
  1147. return true
  1148. }
  1149. return false
  1150. }
  1151. // configurepRTPReceivers opens knows inbound SRTP streams from the RemoteDescription
  1152. func (pc *PeerConnection) configureRTPReceivers(isRenegotiation bool, remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) { //nolint:gocognit
  1153. incomingTracks := trackDetailsFromSDP(pc.log, remoteDesc.parsed)
  1154. if isRenegotiation {
  1155. for _, t := range currentTransceivers {
  1156. receiver := t.Receiver()
  1157. if receiver == nil {
  1158. continue
  1159. }
  1160. tracks := t.Receiver().Tracks()
  1161. if len(tracks) == 0 {
  1162. continue
  1163. }
  1164. mid := t.Mid()
  1165. receiverNeedsStopped := false
  1166. func() {
  1167. for _, t := range tracks {
  1168. t.mu.Lock()
  1169. defer t.mu.Unlock()
  1170. if t.rid != "" {
  1171. if details := trackDetailsForRID(incomingTracks, mid, t.rid); details != nil {
  1172. t.id = details.id
  1173. t.streamID = details.streamID
  1174. continue
  1175. }
  1176. } else if t.ssrc != 0 {
  1177. if details := trackDetailsForSSRC(incomingTracks, t.ssrc); details != nil {
  1178. t.id = details.id
  1179. t.streamID = details.streamID
  1180. continue
  1181. }
  1182. }
  1183. receiverNeedsStopped = true
  1184. }
  1185. }()
  1186. if !receiverNeedsStopped {
  1187. continue
  1188. }
  1189. if err := receiver.Stop(); err != nil {
  1190. pc.log.Warnf("Failed to stop RtpReceiver: %s", err)
  1191. continue
  1192. }
  1193. receiver, err := pc.api.NewRTPReceiver(receiver.kind, pc.dtlsTransport)
  1194. if err != nil {
  1195. pc.log.Warnf("Failed to create new RtpReceiver: %s", err)
  1196. continue
  1197. }
  1198. t.setReceiver(receiver)
  1199. }
  1200. }
  1201. localTransceivers := append([]*RTPTransceiver{}, currentTransceivers...)
  1202. // Ensure we haven't already started a transceiver for this ssrc
  1203. filteredTracks := append([]trackDetails{}, incomingTracks...)
  1204. for _, incomingTrack := range incomingTracks {
  1205. // If we already have a TrackRemote for a given SSRC don't handle it again
  1206. for _, t := range localTransceivers {
  1207. if receiver := t.Receiver(); receiver != nil {
  1208. for _, track := range receiver.Tracks() {
  1209. for _, ssrc := range incomingTrack.ssrcs {
  1210. if ssrc == track.SSRC() {
  1211. filteredTracks = filterTrackWithSSRC(filteredTracks, track.SSRC())
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. for _, incomingTrack := range filteredTracks {
  1219. _ = runIfNewReceiver(incomingTrack, localTransceivers, pc.configureReceiver)
  1220. }
  1221. }
  1222. // startRTPReceivers opens knows inbound SRTP streams from the RemoteDescription
  1223. func (pc *PeerConnection) startRTPReceivers(remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) {
  1224. incomingTracks := trackDetailsFromSDP(pc.log, remoteDesc.parsed)
  1225. if len(incomingTracks) == 0 {
  1226. return
  1227. }
  1228. localTransceivers := append([]*RTPTransceiver{}, currentTransceivers...)
  1229. unhandledTracks := incomingTracks[:0]
  1230. for _, incomingTrack := range incomingTracks {
  1231. trackHandled := runIfNewReceiver(incomingTrack, localTransceivers, pc.startReceiver)
  1232. if !trackHandled {
  1233. unhandledTracks = append(unhandledTracks, incomingTrack)
  1234. }
  1235. }
  1236. remoteIsPlanB := false
  1237. switch pc.configuration.SDPSemantics {
  1238. case SDPSemanticsPlanB:
  1239. remoteIsPlanB = true
  1240. case SDPSemanticsUnifiedPlanWithFallback:
  1241. remoteIsPlanB = descriptionPossiblyPlanB(pc.RemoteDescription())
  1242. default:
  1243. // none
  1244. }
  1245. if remoteIsPlanB {
  1246. for _, incomingTrack := range unhandledTracks {
  1247. t, err := pc.AddTransceiverFromKind(incomingTrack.kind, RTPTransceiverInit{
  1248. Direction: RTPTransceiverDirectionSendrecv,
  1249. })
  1250. if err != nil {
  1251. pc.log.Warnf("Could not add transceiver for remote SSRC %d: %s", incomingTrack.ssrcs[0], err)
  1252. continue
  1253. }
  1254. pc.configureReceiver(incomingTrack, t.Receiver())
  1255. pc.startReceiver(incomingTrack, t.Receiver())
  1256. }
  1257. }
  1258. }
  1259. // startRTPSenders starts all outbound RTP streams
  1260. func (pc *PeerConnection) startRTPSenders(currentTransceivers []*RTPTransceiver) error {
  1261. for _, transceiver := range currentTransceivers {
  1262. if sender := transceiver.Sender(); sender != nil && sender.isNegotiated() && !sender.hasSent() {
  1263. err := sender.Send(sender.GetParameters())
  1264. if err != nil {
  1265. return err
  1266. }
  1267. }
  1268. }
  1269. return nil
  1270. }
  1271. // Start SCTP subsystem
  1272. func (pc *PeerConnection) startSCTP() {
  1273. // Start sctp
  1274. if err := pc.sctpTransport.Start(SCTPCapabilities{
  1275. MaxMessageSize: 0,
  1276. }); err != nil {
  1277. pc.log.Warnf("Failed to start SCTP: %s", err)
  1278. if err = pc.sctpTransport.Stop(); err != nil {
  1279. pc.log.Warnf("Failed to stop SCTPTransport: %s", err)
  1280. }
  1281. return
  1282. }
  1283. }
  1284. func (pc *PeerConnection) handleUndeclaredSSRC(ssrc SSRC, remoteDescription *SessionDescription) (handled bool, err error) {
  1285. if len(remoteDescription.parsed.MediaDescriptions) != 1 {
  1286. return false, nil
  1287. }
  1288. onlyMediaSection := remoteDescription.parsed.MediaDescriptions[0]
  1289. streamID := ""
  1290. id := ""
  1291. hasRidAttribute := false
  1292. hasSSRCAttribute := false
  1293. for _, a := range onlyMediaSection.Attributes {
  1294. switch a.Key {
  1295. case sdp.AttrKeyMsid:
  1296. if split := strings.Split(a.Value, " "); len(split) == 2 {
  1297. streamID = split[0]
  1298. id = split[1]
  1299. }
  1300. case sdp.AttrKeySSRC:
  1301. hasSSRCAttribute = true
  1302. case sdpAttributeRid:
  1303. hasRidAttribute = true
  1304. }
  1305. }
  1306. if hasRidAttribute {
  1307. return false, nil
  1308. } else if hasSSRCAttribute {
  1309. return false, errPeerConnSingleMediaSectionHasExplicitSSRC
  1310. }
  1311. incoming := trackDetails{
  1312. ssrcs: []SSRC{ssrc},
  1313. kind: RTPCodecTypeVideo,
  1314. streamID: streamID,
  1315. id: id,
  1316. }
  1317. if onlyMediaSection.MediaName.Media == RTPCodecTypeAudio.String() {
  1318. incoming.kind = RTPCodecTypeAudio
  1319. }
  1320. t, err := pc.AddTransceiverFromKind(incoming.kind, RTPTransceiverInit{
  1321. Direction: RTPTransceiverDirectionSendrecv,
  1322. })
  1323. if err != nil {
  1324. // nolint
  1325. return false, fmt.Errorf("%w: %d: %s", errPeerConnRemoteSSRCAddTransceiver, ssrc, err)
  1326. }
  1327. pc.configureReceiver(incoming, t.Receiver())
  1328. pc.startReceiver(incoming, t.Receiver())
  1329. return true, nil
  1330. }
  1331. func (pc *PeerConnection) handleIncomingSSRC(rtpStream io.Reader, ssrc SSRC) error { //nolint:gocognit
  1332. remoteDescription := pc.RemoteDescription()
  1333. if remoteDescription == nil {
  1334. return errPeerConnRemoteDescriptionNil
  1335. }
  1336. // If a SSRC already exists in the RemoteDescription don't perform heuristics upon it
  1337. for _, track := range trackDetailsFromSDP(pc.log, remoteDescription.parsed) {
  1338. if track.repairSsrc != nil && ssrc == *track.repairSsrc {
  1339. return nil
  1340. }
  1341. for _, trackSsrc := range track.ssrcs {
  1342. if ssrc == trackSsrc {
  1343. return nil
  1344. }
  1345. }
  1346. }
  1347. // If the remote SDP was only one media section the ssrc doesn't have to be explicitly declared
  1348. if handled, err := pc.handleUndeclaredSSRC(ssrc, remoteDescription); handled || err != nil {
  1349. return err
  1350. }
  1351. midExtensionID, audioSupported, videoSupported := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdp.SDESMidURI})
  1352. if !audioSupported && !videoSupported {
  1353. return errPeerConnSimulcastMidRTPExtensionRequired
  1354. }
  1355. streamIDExtensionID, audioSupported, videoSupported := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdp.SDESRTPStreamIDURI})
  1356. if !audioSupported && !videoSupported {
  1357. return errPeerConnSimulcastStreamIDRTPExtensionRequired
  1358. }
  1359. repairStreamIDExtensionID, _, _ := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdesRepairRTPStreamIDURI})
  1360. b := make([]byte, pc.api.settingEngine.getReceiveMTU())
  1361. i, err := rtpStream.Read(b)
  1362. if err != nil {
  1363. return err
  1364. }
  1365. var mid, rid, rsid string
  1366. payloadType, err := handleUnknownRTPPacket(b[:i], uint8(midExtensionID), uint8(streamIDExtensionID), uint8(repairStreamIDExtensionID), &mid, &rid, &rsid)
  1367. if err != nil {
  1368. return err
  1369. }
  1370. params, err := pc.api.mediaEngine.getRTPParametersByPayloadType(payloadType)
  1371. if err != nil {
  1372. return err
  1373. }
  1374. streamInfo := createStreamInfo("", ssrc, params.Codecs[0].PayloadType, params.Codecs[0].RTPCodecCapability, params.HeaderExtensions)
  1375. readStream, interceptor, rtcpReadStream, rtcpInterceptor, err := pc.dtlsTransport.streamsForSSRC(ssrc, *streamInfo)
  1376. if err != nil {
  1377. return err
  1378. }
  1379. for readCount := 0; readCount <= simulcastProbeCount; readCount++ {
  1380. if mid == "" || (rid == "" && rsid == "") {
  1381. i, _, err := interceptor.Read(b, nil)
  1382. if err != nil {
  1383. return err
  1384. }
  1385. if _, err = handleUnknownRTPPacket(b[:i], uint8(midExtensionID), uint8(streamIDExtensionID), uint8(repairStreamIDExtensionID), &mid, &rid, &rsid); err != nil {
  1386. return err
  1387. }
  1388. continue
  1389. }
  1390. for _, t := range pc.GetTransceivers() {
  1391. receiver := t.Receiver()
  1392. if t.Mid() != mid || receiver == nil {
  1393. continue
  1394. }
  1395. if rsid != "" {
  1396. receiver.mu.Lock()
  1397. defer receiver.mu.Unlock()
  1398. return receiver.receiveForRtx(SSRC(0), rsid, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1399. }
  1400. track, err := receiver.receiveForRid(rid, params, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1401. if err != nil {
  1402. return err
  1403. }
  1404. pc.onTrack(track, receiver)
  1405. return nil
  1406. }
  1407. }
  1408. pc.api.interceptor.UnbindRemoteStream(streamInfo)
  1409. return errPeerConnSimulcastIncomingSSRCFailed
  1410. }
  1411. // undeclaredMediaProcessor handles RTP/RTCP packets that don't match any a:ssrc lines
  1412. func (pc *PeerConnection) undeclaredMediaProcessor() {
  1413. go pc.undeclaredRTPMediaProcessor()
  1414. go pc.undeclaredRTCPMediaProcessor()
  1415. }
  1416. func (pc *PeerConnection) undeclaredRTPMediaProcessor() {
  1417. var simulcastRoutineCount uint64
  1418. for {
  1419. srtpSession, err := pc.dtlsTransport.getSRTPSession()
  1420. if err != nil {
  1421. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtpSession: %v", err)
  1422. return
  1423. }
  1424. stream, ssrc, err := srtpSession.AcceptStream()
  1425. if err != nil {
  1426. pc.log.Warnf("Failed to accept RTP %v", err)
  1427. return
  1428. }
  1429. if pc.isClosed.get() {
  1430. if err = stream.Close(); err != nil {
  1431. pc.log.Warnf("Failed to close RTP stream %v", err)
  1432. }
  1433. continue
  1434. }
  1435. pc.dtlsTransport.storeSimulcastStream(stream)
  1436. if atomic.AddUint64(&simulcastRoutineCount, 1) >= simulcastMaxProbeRoutines {
  1437. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1438. pc.log.Warn(ErrSimulcastProbeOverflow.Error())
  1439. continue
  1440. }
  1441. go func(rtpStream io.Reader, ssrc SSRC) {
  1442. if err := pc.handleIncomingSSRC(rtpStream, ssrc); err != nil {
  1443. pc.log.Errorf(incomingUnhandledRTPSsrc, ssrc, err)
  1444. }
  1445. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1446. }(stream, SSRC(ssrc))
  1447. }
  1448. }
  1449. func (pc *PeerConnection) undeclaredRTCPMediaProcessor() {
  1450. var unhandledStreams []*srtp.ReadStreamSRTCP
  1451. defer func() {
  1452. for _, s := range unhandledStreams {
  1453. _ = s.Close()
  1454. }
  1455. }()
  1456. for {
  1457. srtcpSession, err := pc.dtlsTransport.getSRTCPSession()
  1458. if err != nil {
  1459. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtcpSession: %v", err)
  1460. return
  1461. }
  1462. stream, ssrc, err := srtcpSession.AcceptStream()
  1463. if err != nil {
  1464. pc.log.Warnf("Failed to accept RTCP %v", err)
  1465. return
  1466. }
  1467. pc.log.Warnf("Incoming unhandled RTCP ssrc(%d), OnTrack will not be fired", ssrc)
  1468. unhandledStreams = append(unhandledStreams, stream)
  1469. }
  1470. }
  1471. // RemoteDescription returns pendingRemoteDescription if it is not null and
  1472. // otherwise it returns currentRemoteDescription. This property is used to
  1473. // determine if setRemoteDescription has already been called.
  1474. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-remotedescription
  1475. func (pc *PeerConnection) RemoteDescription() *SessionDescription {
  1476. pc.mu.RLock()
  1477. defer pc.mu.RUnlock()
  1478. if pc.pendingRemoteDescription != nil {
  1479. return pc.pendingRemoteDescription
  1480. }
  1481. return pc.currentRemoteDescription
  1482. }
  1483. // AddICECandidate accepts an ICE candidate string and adds it
  1484. // to the existing set of candidates.
  1485. func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
  1486. if pc.RemoteDescription() == nil {
  1487. return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
  1488. }
  1489. candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
  1490. var iceCandidate *ICECandidate
  1491. if candidateValue != "" {
  1492. candidate, err := ice.UnmarshalCandidate(candidateValue)
  1493. if err != nil {
  1494. if errors.Is(err, ice.ErrUnknownCandidateTyp) || errors.Is(err, ice.ErrDetermineNetworkType) {
  1495. pc.log.Warnf("Discarding remote candidate: %s", err)
  1496. return nil
  1497. }
  1498. return err
  1499. }
  1500. c, err := newICECandidateFromICE(candidate)
  1501. if err != nil {
  1502. return err
  1503. }
  1504. iceCandidate = &c
  1505. }
  1506. return pc.iceTransport.AddRemoteCandidate(iceCandidate)
  1507. }
  1508. // ICEConnectionState returns the ICE connection state of the
  1509. // PeerConnection instance.
  1510. func (pc *PeerConnection) ICEConnectionState() ICEConnectionState {
  1511. if state, ok := pc.iceConnectionState.Load().(ICEConnectionState); ok {
  1512. return state
  1513. }
  1514. return ICEConnectionState(0)
  1515. }
  1516. // GetSenders returns the RTPSender that are currently attached to this PeerConnection
  1517. func (pc *PeerConnection) GetSenders() (result []*RTPSender) {
  1518. pc.mu.Lock()
  1519. defer pc.mu.Unlock()
  1520. for _, transceiver := range pc.rtpTransceivers {
  1521. if sender := transceiver.Sender(); sender != nil {
  1522. result = append(result, sender)
  1523. }
  1524. }
  1525. return result
  1526. }
  1527. // GetReceivers returns the RTPReceivers that are currently attached to this PeerConnection
  1528. func (pc *PeerConnection) GetReceivers() (receivers []*RTPReceiver) {
  1529. pc.mu.Lock()
  1530. defer pc.mu.Unlock()
  1531. for _, transceiver := range pc.rtpTransceivers {
  1532. if receiver := transceiver.Receiver(); receiver != nil {
  1533. receivers = append(receivers, receiver)
  1534. }
  1535. }
  1536. return
  1537. }
  1538. // GetTransceivers returns the RtpTransceiver that are currently attached to this PeerConnection
  1539. func (pc *PeerConnection) GetTransceivers() []*RTPTransceiver {
  1540. pc.mu.Lock()
  1541. defer pc.mu.Unlock()
  1542. return pc.rtpTransceivers
  1543. }
  1544. // AddTrack adds a Track to the PeerConnection
  1545. func (pc *PeerConnection) AddTrack(track TrackLocal) (*RTPSender, error) {
  1546. if pc.isClosed.get() {
  1547. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1548. }
  1549. pc.mu.Lock()
  1550. defer pc.mu.Unlock()
  1551. for _, t := range pc.rtpTransceivers {
  1552. currentDirection := t.getCurrentDirection()
  1553. // According to https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-addtrack, if the
  1554. // transceiver can be reused only if it's currentDirection never be sendrecv or sendonly.
  1555. // But that will cause sdp inflate. So we only check currentDirection's current value,
  1556. // that's worked for all browsers.
  1557. if !t.stopped && t.kind == track.Kind() && t.Sender() == nil &&
  1558. !(currentDirection == RTPTransceiverDirectionSendrecv || currentDirection == RTPTransceiverDirectionSendonly) {
  1559. sender, err := pc.api.NewRTPSender(track, pc.dtlsTransport)
  1560. if err == nil {
  1561. err = t.SetSender(sender, track)
  1562. if err != nil {
  1563. _ = sender.Stop()
  1564. t.setSender(nil)
  1565. }
  1566. }
  1567. if err != nil {
  1568. return nil, err
  1569. }
  1570. pc.onNegotiationNeeded()
  1571. return sender, nil
  1572. }
  1573. }
  1574. transceiver, err := pc.newTransceiverFromTrack(RTPTransceiverDirectionSendrecv, track)
  1575. if err != nil {
  1576. return nil, err
  1577. }
  1578. pc.addRTPTransceiver(transceiver)
  1579. return transceiver.Sender(), nil
  1580. }
  1581. // RemoveTrack removes a Track from the PeerConnection
  1582. func (pc *PeerConnection) RemoveTrack(sender *RTPSender) (err error) {
  1583. if pc.isClosed.get() {
  1584. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1585. }
  1586. var transceiver *RTPTransceiver
  1587. pc.mu.Lock()
  1588. defer pc.mu.Unlock()
  1589. for _, t := range pc.rtpTransceivers {
  1590. if t.Sender() == sender {
  1591. transceiver = t
  1592. break
  1593. }
  1594. }
  1595. if transceiver == nil {
  1596. return &rtcerr.InvalidAccessError{Err: ErrSenderNotCreatedByConnection}
  1597. } else if err = sender.Stop(); err == nil {
  1598. err = transceiver.setSendingTrack(nil)
  1599. if err == nil {
  1600. pc.onNegotiationNeeded()
  1601. }
  1602. }
  1603. return
  1604. }
  1605. func (pc *PeerConnection) newTransceiverFromTrack(direction RTPTransceiverDirection, track TrackLocal) (t *RTPTransceiver, err error) {
  1606. var (
  1607. r *RTPReceiver
  1608. s *RTPSender
  1609. )
  1610. switch direction {
  1611. case RTPTransceiverDirectionSendrecv:
  1612. r, err = pc.api.NewRTPReceiver(track.Kind(), pc.dtlsTransport)
  1613. if err != nil {
  1614. return
  1615. }
  1616. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1617. case RTPTransceiverDirectionSendonly:
  1618. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1619. default:
  1620. err = errPeerConnAddTransceiverFromTrackSupport
  1621. }
  1622. if err != nil {
  1623. return
  1624. }
  1625. return newRTPTransceiver(r, s, direction, track.Kind(), pc.api), nil
  1626. }
  1627. // AddTransceiverFromKind Create a new RtpTransceiver and adds it to the set of transceivers.
  1628. func (pc *PeerConnection) AddTransceiverFromKind(kind RTPCodecType, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1629. if pc.isClosed.get() {
  1630. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1631. }
  1632. direction := RTPTransceiverDirectionSendrecv
  1633. if len(init) > 1 {
  1634. return nil, errPeerConnAddTransceiverFromKindOnlyAcceptsOne
  1635. } else if len(init) == 1 {
  1636. direction = init[0].Direction
  1637. }
  1638. switch direction {
  1639. case RTPTransceiverDirectionSendonly, RTPTransceiverDirectionSendrecv:
  1640. codecs := pc.api.mediaEngine.getCodecsByKind(kind)
  1641. if len(codecs) == 0 {
  1642. return nil, ErrNoCodecsAvailable
  1643. }
  1644. track, err := NewTrackLocalStaticSample(codecs[0].RTPCodecCapability, util.MathRandAlpha(16), util.MathRandAlpha(16))
  1645. if err != nil {
  1646. return nil, err
  1647. }
  1648. t, err = pc.newTransceiverFromTrack(direction, track)
  1649. if err != nil {
  1650. return nil, err
  1651. }
  1652. case RTPTransceiverDirectionRecvonly:
  1653. receiver, err := pc.api.NewRTPReceiver(kind, pc.dtlsTransport)
  1654. if err != nil {
  1655. return nil, err
  1656. }
  1657. t = newRTPTransceiver(receiver, nil, RTPTransceiverDirectionRecvonly, kind, pc.api)
  1658. default:
  1659. return nil, errPeerConnAddTransceiverFromKindSupport
  1660. }
  1661. pc.mu.Lock()
  1662. pc.addRTPTransceiver(t)
  1663. pc.mu.Unlock()
  1664. return t, nil
  1665. }
  1666. // AddTransceiverFromTrack Create a new RtpTransceiver(SendRecv or SendOnly) and add it to the set of transceivers.
  1667. func (pc *PeerConnection) AddTransceiverFromTrack(track TrackLocal, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1668. if pc.isClosed.get() {
  1669. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1670. }
  1671. direction := RTPTransceiverDirectionSendrecv
  1672. if len(init) > 1 {
  1673. return nil, errPeerConnAddTransceiverFromTrackOnlyAcceptsOne
  1674. } else if len(init) == 1 {
  1675. direction = init[0].Direction
  1676. }
  1677. t, err = pc.newTransceiverFromTrack(direction, track)
  1678. if err == nil {
  1679. pc.mu.Lock()
  1680. pc.addRTPTransceiver(t)
  1681. pc.mu.Unlock()
  1682. }
  1683. return
  1684. }
  1685. // CreateDataChannel creates a new DataChannel object with the given label
  1686. // and optional DataChannelInit used to configure properties of the
  1687. // underlying channel such as data reliability.
  1688. func (pc *PeerConnection) CreateDataChannel(label string, options *DataChannelInit) (*DataChannel, error) {
  1689. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #2)
  1690. if pc.isClosed.get() {
  1691. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1692. }
  1693. params := &DataChannelParameters{
  1694. Label: label,
  1695. Ordered: true,
  1696. }
  1697. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
  1698. if options != nil {
  1699. params.ID = options.ID
  1700. }
  1701. if options != nil {
  1702. // Ordered indicates if data is allowed to be delivered out of order. The
  1703. // default value of true, guarantees that data will be delivered in order.
  1704. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
  1705. if options.Ordered != nil {
  1706. params.Ordered = *options.Ordered
  1707. }
  1708. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #7)
  1709. if options.MaxPacketLifeTime != nil {
  1710. params.MaxPacketLifeTime = options.MaxPacketLifeTime
  1711. }
  1712. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #8)
  1713. if options.MaxRetransmits != nil {
  1714. params.MaxRetransmits = options.MaxRetransmits
  1715. }
  1716. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #10)
  1717. if options.Protocol != nil {
  1718. params.Protocol = *options.Protocol
  1719. }
  1720. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #11)
  1721. if len(params.Protocol) > 65535 {
  1722. return nil, &rtcerr.TypeError{Err: ErrProtocolTooLarge}
  1723. }
  1724. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
  1725. if options.Negotiated != nil {
  1726. params.Negotiated = *options.Negotiated
  1727. }
  1728. }
  1729. d, err := pc.api.newDataChannel(params, nil, pc.log)
  1730. if err != nil {
  1731. return nil, err
  1732. }
  1733. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #16)
  1734. if d.maxPacketLifeTime != nil && d.maxRetransmits != nil {
  1735. return nil, &rtcerr.TypeError{Err: ErrRetransmitsOrPacketLifeTime}
  1736. }
  1737. pc.sctpTransport.lock.Lock()
  1738. pc.sctpTransport.dataChannels = append(pc.sctpTransport.dataChannels, d)
  1739. pc.sctpTransport.dataChannelsRequested++
  1740. pc.sctpTransport.lock.Unlock()
  1741. // If SCTP already connected open all the channels
  1742. if pc.sctpTransport.State() == SCTPTransportStateConnected {
  1743. if err = d.open(pc.sctpTransport); err != nil {
  1744. return nil, err
  1745. }
  1746. }
  1747. pc.mu.Lock()
  1748. pc.onNegotiationNeeded()
  1749. pc.mu.Unlock()
  1750. return d, nil
  1751. }
  1752. // SetIdentityProvider is used to configure an identity provider to generate identity assertions
  1753. func (pc *PeerConnection) SetIdentityProvider(string) error {
  1754. return errPeerConnSetIdentityProviderNotImplemented
  1755. }
  1756. // WriteRTCP sends a user provided RTCP packet to the connected peer. If no peer is connected the
  1757. // packet is discarded. It also runs any configured interceptors.
  1758. func (pc *PeerConnection) WriteRTCP(pkts []rtcp.Packet) error {
  1759. _, err := pc.interceptorRTCPWriter.Write(pkts, make(interceptor.Attributes))
  1760. return err
  1761. }
  1762. func (pc *PeerConnection) writeRTCP(pkts []rtcp.Packet, _ interceptor.Attributes) (int, error) {
  1763. return pc.dtlsTransport.WriteRTCP(pkts)
  1764. }
  1765. // Close ends the PeerConnection
  1766. func (pc *PeerConnection) Close() error {
  1767. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #1)
  1768. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #2)
  1769. if pc.isClosed.swap(true) {
  1770. return nil
  1771. }
  1772. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #3)
  1773. pc.signalingState.Set(SignalingStateClosed)
  1774. // Try closing everything and collect the errors
  1775. // Shutdown strategy:
  1776. // 1. All Conn close by closing their underlying Conn.
  1777. // 2. A Mux stops this chain. It won't close the underlying
  1778. // Conn if one of the endpoints is closed down. To
  1779. // continue the chain the Mux has to be closed.
  1780. closeErrs := make([]error, 4)
  1781. closeErrs = append(closeErrs, pc.api.interceptor.Close())
  1782. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4)
  1783. pc.mu.Lock()
  1784. for _, t := range pc.rtpTransceivers {
  1785. if !t.stopped {
  1786. closeErrs = append(closeErrs, t.Stop())
  1787. }
  1788. }
  1789. pc.mu.Unlock()
  1790. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #5)
  1791. pc.sctpTransport.lock.Lock()
  1792. for _, d := range pc.sctpTransport.dataChannels {
  1793. d.setReadyState(DataChannelStateClosed)
  1794. }
  1795. pc.sctpTransport.lock.Unlock()
  1796. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #6)
  1797. if pc.sctpTransport != nil {
  1798. closeErrs = append(closeErrs, pc.sctpTransport.Stop())
  1799. }
  1800. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #7)
  1801. closeErrs = append(closeErrs, pc.dtlsTransport.Stop())
  1802. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #8, #9, #10)
  1803. if pc.iceTransport != nil {
  1804. closeErrs = append(closeErrs, pc.iceTransport.Stop())
  1805. }
  1806. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #11)
  1807. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1808. return util.FlattenErrs(closeErrs)
  1809. }
  1810. // addRTPTransceiver appends t into rtpTransceivers
  1811. // and fires onNegotiationNeeded;
  1812. // caller of this method should hold `pc.mu` lock
  1813. func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
  1814. pc.rtpTransceivers = append(pc.rtpTransceivers, t)
  1815. pc.onNegotiationNeeded()
  1816. }
  1817. // CurrentLocalDescription represents the local description that was
  1818. // successfully negotiated the last time the PeerConnection transitioned
  1819. // into the stable state plus any local candidates that have been generated
  1820. // by the ICEAgent since the offer or answer was created.
  1821. func (pc *PeerConnection) CurrentLocalDescription() *SessionDescription {
  1822. pc.mu.Lock()
  1823. localDescription := pc.currentLocalDescription
  1824. iceGather := pc.iceGatherer
  1825. iceGatheringState := pc.ICEGatheringState()
  1826. pc.mu.Unlock()
  1827. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1828. }
  1829. // PendingLocalDescription represents a local description that is in the
  1830. // process of being negotiated plus any local candidates that have been
  1831. // generated by the ICEAgent since the offer or answer was created. If the
  1832. // PeerConnection is in the stable state, the value is null.
  1833. func (pc *PeerConnection) PendingLocalDescription() *SessionDescription {
  1834. pc.mu.Lock()
  1835. localDescription := pc.pendingLocalDescription
  1836. iceGather := pc.iceGatherer
  1837. iceGatheringState := pc.ICEGatheringState()
  1838. pc.mu.Unlock()
  1839. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1840. }
  1841. // CurrentRemoteDescription represents the last remote description that was
  1842. // successfully negotiated the last time the PeerConnection transitioned
  1843. // into the stable state plus any remote candidates that have been supplied
  1844. // via AddICECandidate() since the offer or answer was created.
  1845. func (pc *PeerConnection) CurrentRemoteDescription() *SessionDescription {
  1846. pc.mu.RLock()
  1847. defer pc.mu.RUnlock()
  1848. return pc.currentRemoteDescription
  1849. }
  1850. // PendingRemoteDescription represents a remote description that is in the
  1851. // process of being negotiated, complete with any remote candidates that
  1852. // have been supplied via AddICECandidate() since the offer or answer was
  1853. // created. If the PeerConnection is in the stable state, the value is
  1854. // null.
  1855. func (pc *PeerConnection) PendingRemoteDescription() *SessionDescription {
  1856. pc.mu.RLock()
  1857. defer pc.mu.RUnlock()
  1858. return pc.pendingRemoteDescription
  1859. }
  1860. // SignalingState attribute returns the signaling state of the
  1861. // PeerConnection instance.
  1862. func (pc *PeerConnection) SignalingState() SignalingState {
  1863. return pc.signalingState.Get()
  1864. }
  1865. // ICEGatheringState attribute returns the ICE gathering state of the
  1866. // PeerConnection instance.
  1867. func (pc *PeerConnection) ICEGatheringState() ICEGatheringState {
  1868. if pc.iceGatherer == nil {
  1869. return ICEGatheringStateNew
  1870. }
  1871. switch pc.iceGatherer.State() {
  1872. case ICEGathererStateNew:
  1873. return ICEGatheringStateNew
  1874. case ICEGathererStateGathering:
  1875. return ICEGatheringStateGathering
  1876. default:
  1877. return ICEGatheringStateComplete
  1878. }
  1879. }
  1880. // ConnectionState attribute returns the connection state of the
  1881. // PeerConnection instance.
  1882. func (pc *PeerConnection) ConnectionState() PeerConnectionState {
  1883. if state, ok := pc.connectionState.Load().(PeerConnectionState); ok {
  1884. return state
  1885. }
  1886. return PeerConnectionState(0)
  1887. }
  1888. // GetStats return data providing statistics about the overall connection
  1889. func (pc *PeerConnection) GetStats() StatsReport {
  1890. var (
  1891. dataChannelsAccepted uint32
  1892. dataChannelsClosed uint32
  1893. dataChannelsOpened uint32
  1894. dataChannelsRequested uint32
  1895. )
  1896. statsCollector := newStatsReportCollector()
  1897. statsCollector.Collecting()
  1898. pc.mu.Lock()
  1899. if pc.iceGatherer != nil {
  1900. pc.iceGatherer.collectStats(statsCollector)
  1901. }
  1902. if pc.iceTransport != nil {
  1903. pc.iceTransport.collectStats(statsCollector)
  1904. }
  1905. pc.sctpTransport.lock.Lock()
  1906. dataChannels := append([]*DataChannel{}, pc.sctpTransport.dataChannels...)
  1907. dataChannelsAccepted = pc.sctpTransport.dataChannelsAccepted
  1908. dataChannelsOpened = pc.sctpTransport.dataChannelsOpened
  1909. dataChannelsRequested = pc.sctpTransport.dataChannelsRequested
  1910. pc.sctpTransport.lock.Unlock()
  1911. for _, d := range dataChannels {
  1912. state := d.ReadyState()
  1913. if state != DataChannelStateConnecting && state != DataChannelStateOpen {
  1914. dataChannelsClosed++
  1915. }
  1916. d.collectStats(statsCollector)
  1917. }
  1918. pc.sctpTransport.collectStats(statsCollector)
  1919. stats := PeerConnectionStats{
  1920. Timestamp: statsTimestampNow(),
  1921. Type: StatsTypePeerConnection,
  1922. ID: pc.statsID,
  1923. DataChannelsAccepted: dataChannelsAccepted,
  1924. DataChannelsClosed: dataChannelsClosed,
  1925. DataChannelsOpened: dataChannelsOpened,
  1926. DataChannelsRequested: dataChannelsRequested,
  1927. }
  1928. statsCollector.Collect(stats.ID, stats)
  1929. certificates := pc.configuration.Certificates
  1930. for _, certificate := range certificates {
  1931. if err := certificate.collectStats(statsCollector); err != nil {
  1932. continue
  1933. }
  1934. }
  1935. pc.mu.Unlock()
  1936. pc.api.mediaEngine.collectStats(statsCollector)
  1937. return statsCollector.Ready()
  1938. }
  1939. // Start all transports. PeerConnection now has enough state
  1940. func (pc *PeerConnection) startTransports(iceRole ICERole, dtlsRole DTLSRole, remoteUfrag, remotePwd, fingerprint, fingerprintHash string) {
  1941. // Start the ice transport
  1942. err := pc.iceTransport.Start(
  1943. pc.iceGatherer,
  1944. ICEParameters{
  1945. UsernameFragment: remoteUfrag,
  1946. Password: remotePwd,
  1947. ICELite: false,
  1948. },
  1949. &iceRole,
  1950. )
  1951. if err != nil {
  1952. pc.log.Warnf("Failed to start manager: %s", err)
  1953. return
  1954. }
  1955. // Start the dtls transport
  1956. err = pc.dtlsTransport.Start(DTLSParameters{
  1957. Role: dtlsRole,
  1958. Fingerprints: []DTLSFingerprint{{Algorithm: fingerprintHash, Value: fingerprint}},
  1959. })
  1960. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1961. if err != nil {
  1962. pc.log.Warnf("Failed to start manager: %s", err)
  1963. return
  1964. }
  1965. }
  1966. // nolint: gocognit
  1967. func (pc *PeerConnection) startRTP(isRenegotiation bool, remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) {
  1968. if !isRenegotiation {
  1969. pc.undeclaredMediaProcessor()
  1970. }
  1971. pc.startRTPReceivers(remoteDesc, currentTransceivers)
  1972. if haveApplicationMediaSection(remoteDesc.parsed) {
  1973. pc.startSCTP()
  1974. }
  1975. }
  1976. // generateUnmatchedSDP generates an SDP that doesn't take remote state into account
  1977. // This is used for the initial call for CreateOffer
  1978. func (pc *PeerConnection) generateUnmatchedSDP(transceivers []*RTPTransceiver, useIdentity bool) (*sdp.SessionDescription, error) {
  1979. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  1980. if err != nil {
  1981. return nil, err
  1982. }
  1983. iceParams, err := pc.iceGatherer.GetLocalParameters()
  1984. if err != nil {
  1985. return nil, err
  1986. }
  1987. candidates, err := pc.iceGatherer.GetLocalCandidates()
  1988. if err != nil {
  1989. return nil, err
  1990. }
  1991. isPlanB := pc.configuration.SDPSemantics == SDPSemanticsPlanB
  1992. mediaSections := []mediaSection{}
  1993. // Needed for pc.sctpTransport.dataChannelsRequested
  1994. pc.sctpTransport.lock.Lock()
  1995. defer pc.sctpTransport.lock.Unlock()
  1996. if isPlanB {
  1997. video := make([]*RTPTransceiver, 0)
  1998. audio := make([]*RTPTransceiver, 0)
  1999. for _, t := range transceivers {
  2000. if t.kind == RTPCodecTypeVideo {
  2001. video = append(video, t)
  2002. } else if t.kind == RTPCodecTypeAudio {
  2003. audio = append(audio, t)
  2004. }
  2005. if sender := t.Sender(); sender != nil {
  2006. sender.setNegotiated()
  2007. }
  2008. }
  2009. if len(video) > 0 {
  2010. mediaSections = append(mediaSections, mediaSection{id: "video", transceivers: video})
  2011. }
  2012. if len(audio) > 0 {
  2013. mediaSections = append(mediaSections, mediaSection{id: "audio", transceivers: audio})
  2014. }
  2015. if pc.sctpTransport.dataChannelsRequested != 0 {
  2016. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2017. }
  2018. } else {
  2019. for _, t := range transceivers {
  2020. if sender := t.Sender(); sender != nil {
  2021. sender.setNegotiated()
  2022. }
  2023. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2024. }
  2025. if pc.sctpTransport.dataChannelsRequested != 0 {
  2026. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2027. }
  2028. }
  2029. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2030. if err != nil {
  2031. return nil, err
  2032. }
  2033. return populateSDP(d, isPlanB, dtlsFingerprints, pc.api.settingEngine.sdpMediaLevelFingerprints, pc.api.settingEngine.candidates.ICELite, true, pc.api.mediaEngine, connectionRoleFromDtlsRole(defaultDtlsRoleOffer), candidates, iceParams, mediaSections, pc.ICEGatheringState(), nil)
  2034. }
  2035. // generateMatchedSDP generates a SDP and takes the remote state into account
  2036. // this is used everytime we have a RemoteDescription
  2037. // nolint: gocyclo
  2038. func (pc *PeerConnection) generateMatchedSDP(transceivers []*RTPTransceiver, useIdentity bool, includeUnmatched bool, connectionRole sdp.ConnectionRole) (*sdp.SessionDescription, error) { //nolint:gocognit
  2039. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  2040. if err != nil {
  2041. return nil, err
  2042. }
  2043. iceParams, err := pc.iceGatherer.GetLocalParameters()
  2044. if err != nil {
  2045. return nil, err
  2046. }
  2047. candidates, err := pc.iceGatherer.GetLocalCandidates()
  2048. if err != nil {
  2049. return nil, err
  2050. }
  2051. var t *RTPTransceiver
  2052. remoteDescription := pc.currentRemoteDescription
  2053. if pc.pendingRemoteDescription != nil {
  2054. remoteDescription = pc.pendingRemoteDescription
  2055. }
  2056. isExtmapAllowMixed := isExtMapAllowMixedSet(remoteDescription.parsed)
  2057. localTransceivers := append([]*RTPTransceiver{}, transceivers...)
  2058. detectedPlanB := descriptionIsPlanB(remoteDescription, pc.log)
  2059. if pc.configuration.SDPSemantics != SDPSemanticsUnifiedPlan {
  2060. detectedPlanB = descriptionPossiblyPlanB(remoteDescription)
  2061. }
  2062. mediaSections := []mediaSection{}
  2063. alreadyHaveApplicationMediaSection := false
  2064. for _, media := range remoteDescription.parsed.MediaDescriptions {
  2065. midValue := getMidValue(media)
  2066. if midValue == "" {
  2067. return nil, errPeerConnRemoteDescriptionWithoutMidValue
  2068. }
  2069. if media.MediaName.Media == mediaSectionApplication {
  2070. mediaSections = append(mediaSections, mediaSection{id: midValue, data: true})
  2071. alreadyHaveApplicationMediaSection = true
  2072. continue
  2073. }
  2074. kind := NewRTPCodecType(media.MediaName.Media)
  2075. direction := getPeerDirection(media)
  2076. if kind == 0 || direction == RTPTransceiverDirection(Unknown) {
  2077. continue
  2078. }
  2079. sdpSemantics := pc.configuration.SDPSemantics
  2080. switch {
  2081. case sdpSemantics == SDPSemanticsPlanB || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB:
  2082. if !detectedPlanB {
  2083. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected PlanB, but RemoteDescription is UnifiedPlan", ErrIncorrectSDPSemantics)}
  2084. }
  2085. // If we're responding to a plan-b offer, then we should try to fill up this
  2086. // media entry with all matching local transceivers
  2087. mediaTransceivers := []*RTPTransceiver{}
  2088. for {
  2089. // keep going until we can't get any more
  2090. t, localTransceivers = satisfyTypeAndDirection(kind, direction, localTransceivers)
  2091. if t == nil {
  2092. if len(mediaTransceivers) == 0 {
  2093. t = &RTPTransceiver{kind: kind, api: pc.api, codecs: pc.api.mediaEngine.getCodecsByKind(kind)}
  2094. t.setDirection(RTPTransceiverDirectionInactive)
  2095. mediaTransceivers = append(mediaTransceivers, t)
  2096. }
  2097. break
  2098. }
  2099. if sender := t.Sender(); sender != nil {
  2100. sender.setNegotiated()
  2101. }
  2102. mediaTransceivers = append(mediaTransceivers, t)
  2103. }
  2104. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers})
  2105. case sdpSemantics == SDPSemanticsUnifiedPlan || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback:
  2106. if detectedPlanB {
  2107. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected UnifiedPlan, but RemoteDescription is PlanB", ErrIncorrectSDPSemantics)}
  2108. }
  2109. t, localTransceivers = findByMid(midValue, localTransceivers)
  2110. if t == nil {
  2111. return nil, fmt.Errorf("%w: %q", errPeerConnTranscieverMidNil, midValue)
  2112. }
  2113. if sender := t.Sender(); sender != nil {
  2114. sender.setNegotiated()
  2115. }
  2116. mediaTransceivers := []*RTPTransceiver{t}
  2117. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, ridMap: getRids(media)})
  2118. }
  2119. }
  2120. var bundleGroup *string
  2121. // If we are offering also include unmatched local transceivers
  2122. if includeUnmatched {
  2123. if !detectedPlanB {
  2124. for _, t := range localTransceivers {
  2125. if sender := t.Sender(); sender != nil {
  2126. sender.setNegotiated()
  2127. }
  2128. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2129. }
  2130. }
  2131. if pc.sctpTransport.dataChannelsRequested != 0 && !alreadyHaveApplicationMediaSection {
  2132. if detectedPlanB {
  2133. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2134. } else {
  2135. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2136. }
  2137. }
  2138. } else if remoteDescription != nil {
  2139. groupValue, _ := remoteDescription.parsed.Attribute(sdp.AttrKeyGroup)
  2140. groupValue = strings.TrimLeft(groupValue, "BUNDLE")
  2141. bundleGroup = &groupValue
  2142. }
  2143. if pc.configuration.SDPSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB {
  2144. pc.log.Info("Plan-B Offer detected; responding with Plan-B Answer")
  2145. }
  2146. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2147. if err != nil {
  2148. return nil, err
  2149. }
  2150. return populateSDP(d, detectedPlanB, dtlsFingerprints, pc.api.settingEngine.sdpMediaLevelFingerprints, pc.api.settingEngine.candidates.ICELite, isExtmapAllowMixed, pc.api.mediaEngine, connectionRole, candidates, iceParams, mediaSections, pc.ICEGatheringState(), bundleGroup)
  2151. }
  2152. func (pc *PeerConnection) setGatherCompleteHandler(handler func()) {
  2153. pc.iceGatherer.onGatheringCompleteHandler.Store(handler)
  2154. }
  2155. // SCTP returns the SCTPTransport for this PeerConnection
  2156. //
  2157. // The SCTP transport over which SCTP data is sent and received. If SCTP has not been negotiated, the value is nil.
  2158. // https://www.w3.org/TR/webrtc/#attributes-15
  2159. func (pc *PeerConnection) SCTP() *SCTPTransport {
  2160. return pc.sctpTransport
  2161. }