peerconnection.go 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509
  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.UnmarshalString(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. if i < 4 {
  1366. return errRTPTooShort
  1367. }
  1368. payloadType := PayloadType(b[1] & 0x7f)
  1369. params, err := pc.api.mediaEngine.getRTPParametersByPayloadType(payloadType)
  1370. if err != nil {
  1371. return err
  1372. }
  1373. streamInfo := createStreamInfo("", ssrc, params.Codecs[0].PayloadType, params.Codecs[0].RTPCodecCapability, params.HeaderExtensions)
  1374. readStream, interceptor, rtcpReadStream, rtcpInterceptor, err := pc.dtlsTransport.streamsForSSRC(ssrc, *streamInfo)
  1375. if err != nil {
  1376. return err
  1377. }
  1378. var mid, rid, rsid string
  1379. var paddingOnly bool
  1380. for readCount := 0; readCount <= simulcastProbeCount; readCount++ {
  1381. if mid == "" || (rid == "" && rsid == "") {
  1382. // skip padding only packets for probing
  1383. if paddingOnly {
  1384. readCount--
  1385. }
  1386. i, _, err := interceptor.Read(b, nil)
  1387. if err != nil {
  1388. return err
  1389. }
  1390. if _, paddingOnly, err = handleUnknownRTPPacket(b[:i], uint8(midExtensionID), uint8(streamIDExtensionID), uint8(repairStreamIDExtensionID), &mid, &rid, &rsid); err != nil {
  1391. return err
  1392. }
  1393. continue
  1394. }
  1395. for _, t := range pc.GetTransceivers() {
  1396. receiver := t.Receiver()
  1397. if t.Mid() != mid || receiver == nil {
  1398. continue
  1399. }
  1400. if rsid != "" {
  1401. receiver.mu.Lock()
  1402. defer receiver.mu.Unlock()
  1403. return receiver.receiveForRtx(SSRC(0), rsid, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1404. }
  1405. track, err := receiver.receiveForRid(rid, params, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1406. if err != nil {
  1407. return err
  1408. }
  1409. pc.onTrack(track, receiver)
  1410. return nil
  1411. }
  1412. }
  1413. pc.api.interceptor.UnbindRemoteStream(streamInfo)
  1414. return errPeerConnSimulcastIncomingSSRCFailed
  1415. }
  1416. // undeclaredMediaProcessor handles RTP/RTCP packets that don't match any a:ssrc lines
  1417. func (pc *PeerConnection) undeclaredMediaProcessor() {
  1418. go pc.undeclaredRTPMediaProcessor()
  1419. go pc.undeclaredRTCPMediaProcessor()
  1420. }
  1421. func (pc *PeerConnection) undeclaredRTPMediaProcessor() {
  1422. var simulcastRoutineCount uint64
  1423. for {
  1424. srtpSession, err := pc.dtlsTransport.getSRTPSession()
  1425. if err != nil {
  1426. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtpSession: %v", err)
  1427. return
  1428. }
  1429. stream, ssrc, err := srtpSession.AcceptStream()
  1430. if err != nil {
  1431. pc.log.Warnf("Failed to accept RTP %v", err)
  1432. return
  1433. }
  1434. if pc.isClosed.get() {
  1435. if err = stream.Close(); err != nil {
  1436. pc.log.Warnf("Failed to close RTP stream %v", err)
  1437. }
  1438. continue
  1439. }
  1440. pc.dtlsTransport.storeSimulcastStream(stream)
  1441. if atomic.AddUint64(&simulcastRoutineCount, 1) >= simulcastMaxProbeRoutines {
  1442. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1443. pc.log.Warn(ErrSimulcastProbeOverflow.Error())
  1444. continue
  1445. }
  1446. go func(rtpStream io.Reader, ssrc SSRC) {
  1447. if err := pc.handleIncomingSSRC(rtpStream, ssrc); err != nil {
  1448. pc.log.Errorf(incomingUnhandledRTPSsrc, ssrc, err)
  1449. }
  1450. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1451. }(stream, SSRC(ssrc))
  1452. }
  1453. }
  1454. func (pc *PeerConnection) undeclaredRTCPMediaProcessor() {
  1455. var unhandledStreams []*srtp.ReadStreamSRTCP
  1456. defer func() {
  1457. for _, s := range unhandledStreams {
  1458. _ = s.Close()
  1459. }
  1460. }()
  1461. for {
  1462. srtcpSession, err := pc.dtlsTransport.getSRTCPSession()
  1463. if err != nil {
  1464. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtcpSession: %v", err)
  1465. return
  1466. }
  1467. stream, ssrc, err := srtcpSession.AcceptStream()
  1468. if err != nil {
  1469. pc.log.Warnf("Failed to accept RTCP %v", err)
  1470. return
  1471. }
  1472. pc.log.Warnf("Incoming unhandled RTCP ssrc(%d), OnTrack will not be fired", ssrc)
  1473. unhandledStreams = append(unhandledStreams, stream)
  1474. }
  1475. }
  1476. // RemoteDescription returns pendingRemoteDescription if it is not null and
  1477. // otherwise it returns currentRemoteDescription. This property is used to
  1478. // determine if setRemoteDescription has already been called.
  1479. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-remotedescription
  1480. func (pc *PeerConnection) RemoteDescription() *SessionDescription {
  1481. pc.mu.RLock()
  1482. defer pc.mu.RUnlock()
  1483. if pc.pendingRemoteDescription != nil {
  1484. return pc.pendingRemoteDescription
  1485. }
  1486. return pc.currentRemoteDescription
  1487. }
  1488. // AddICECandidate accepts an ICE candidate string and adds it
  1489. // to the existing set of candidates.
  1490. func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
  1491. if pc.RemoteDescription() == nil {
  1492. return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
  1493. }
  1494. candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
  1495. var iceCandidate *ICECandidate
  1496. if candidateValue != "" {
  1497. candidate, err := ice.UnmarshalCandidate(candidateValue)
  1498. if err != nil {
  1499. if errors.Is(err, ice.ErrUnknownCandidateTyp) || errors.Is(err, ice.ErrDetermineNetworkType) {
  1500. pc.log.Warnf("Discarding remote candidate: %s", err)
  1501. return nil
  1502. }
  1503. return err
  1504. }
  1505. c, err := newICECandidateFromICE(candidate)
  1506. if err != nil {
  1507. return err
  1508. }
  1509. iceCandidate = &c
  1510. }
  1511. return pc.iceTransport.AddRemoteCandidate(iceCandidate)
  1512. }
  1513. // ICEConnectionState returns the ICE connection state of the
  1514. // PeerConnection instance.
  1515. func (pc *PeerConnection) ICEConnectionState() ICEConnectionState {
  1516. if state, ok := pc.iceConnectionState.Load().(ICEConnectionState); ok {
  1517. return state
  1518. }
  1519. return ICEConnectionState(0)
  1520. }
  1521. // GetSenders returns the RTPSender that are currently attached to this PeerConnection
  1522. func (pc *PeerConnection) GetSenders() (result []*RTPSender) {
  1523. pc.mu.Lock()
  1524. defer pc.mu.Unlock()
  1525. for _, transceiver := range pc.rtpTransceivers {
  1526. if sender := transceiver.Sender(); sender != nil {
  1527. result = append(result, sender)
  1528. }
  1529. }
  1530. return result
  1531. }
  1532. // GetReceivers returns the RTPReceivers that are currently attached to this PeerConnection
  1533. func (pc *PeerConnection) GetReceivers() (receivers []*RTPReceiver) {
  1534. pc.mu.Lock()
  1535. defer pc.mu.Unlock()
  1536. for _, transceiver := range pc.rtpTransceivers {
  1537. if receiver := transceiver.Receiver(); receiver != nil {
  1538. receivers = append(receivers, receiver)
  1539. }
  1540. }
  1541. return
  1542. }
  1543. // GetTransceivers returns the RtpTransceiver that are currently attached to this PeerConnection
  1544. func (pc *PeerConnection) GetTransceivers() []*RTPTransceiver {
  1545. pc.mu.Lock()
  1546. defer pc.mu.Unlock()
  1547. return pc.rtpTransceivers
  1548. }
  1549. // AddTrack adds a Track to the PeerConnection
  1550. func (pc *PeerConnection) AddTrack(track TrackLocal) (*RTPSender, error) {
  1551. if pc.isClosed.get() {
  1552. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1553. }
  1554. pc.mu.Lock()
  1555. defer pc.mu.Unlock()
  1556. for _, t := range pc.rtpTransceivers {
  1557. currentDirection := t.getCurrentDirection()
  1558. // According to https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-addtrack, if the
  1559. // transceiver can be reused only if it's currentDirection never be sendrecv or sendonly.
  1560. // But that will cause sdp inflate. So we only check currentDirection's current value,
  1561. // that's worked for all browsers.
  1562. if !t.stopped && t.kind == track.Kind() && t.Sender() == nil &&
  1563. !(currentDirection == RTPTransceiverDirectionSendrecv || currentDirection == RTPTransceiverDirectionSendonly) {
  1564. sender, err := pc.api.NewRTPSender(track, pc.dtlsTransport)
  1565. if err == nil {
  1566. err = t.SetSender(sender, track)
  1567. if err != nil {
  1568. _ = sender.Stop()
  1569. t.setSender(nil)
  1570. }
  1571. }
  1572. if err != nil {
  1573. return nil, err
  1574. }
  1575. pc.onNegotiationNeeded()
  1576. return sender, nil
  1577. }
  1578. }
  1579. transceiver, err := pc.newTransceiverFromTrack(RTPTransceiverDirectionSendrecv, track)
  1580. if err != nil {
  1581. return nil, err
  1582. }
  1583. pc.addRTPTransceiver(transceiver)
  1584. return transceiver.Sender(), nil
  1585. }
  1586. // RemoveTrack removes a Track from the PeerConnection
  1587. func (pc *PeerConnection) RemoveTrack(sender *RTPSender) (err error) {
  1588. if pc.isClosed.get() {
  1589. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1590. }
  1591. var transceiver *RTPTransceiver
  1592. pc.mu.Lock()
  1593. defer pc.mu.Unlock()
  1594. for _, t := range pc.rtpTransceivers {
  1595. if t.Sender() == sender {
  1596. transceiver = t
  1597. break
  1598. }
  1599. }
  1600. if transceiver == nil {
  1601. return &rtcerr.InvalidAccessError{Err: ErrSenderNotCreatedByConnection}
  1602. } else if err = sender.Stop(); err == nil {
  1603. err = transceiver.setSendingTrack(nil)
  1604. if err == nil {
  1605. pc.onNegotiationNeeded()
  1606. }
  1607. }
  1608. return
  1609. }
  1610. func (pc *PeerConnection) newTransceiverFromTrack(direction RTPTransceiverDirection, track TrackLocal) (t *RTPTransceiver, err error) {
  1611. var (
  1612. r *RTPReceiver
  1613. s *RTPSender
  1614. )
  1615. switch direction {
  1616. case RTPTransceiverDirectionSendrecv:
  1617. r, err = pc.api.NewRTPReceiver(track.Kind(), pc.dtlsTransport)
  1618. if err != nil {
  1619. return
  1620. }
  1621. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1622. case RTPTransceiverDirectionSendonly:
  1623. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1624. default:
  1625. err = errPeerConnAddTransceiverFromTrackSupport
  1626. }
  1627. if err != nil {
  1628. return
  1629. }
  1630. return newRTPTransceiver(r, s, direction, track.Kind(), pc.api), nil
  1631. }
  1632. // AddTransceiverFromKind Create a new RtpTransceiver and adds it to the set of transceivers.
  1633. func (pc *PeerConnection) AddTransceiverFromKind(kind RTPCodecType, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1634. if pc.isClosed.get() {
  1635. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1636. }
  1637. direction := RTPTransceiverDirectionSendrecv
  1638. if len(init) > 1 {
  1639. return nil, errPeerConnAddTransceiverFromKindOnlyAcceptsOne
  1640. } else if len(init) == 1 {
  1641. direction = init[0].Direction
  1642. }
  1643. switch direction {
  1644. case RTPTransceiverDirectionSendonly, RTPTransceiverDirectionSendrecv:
  1645. codecs := pc.api.mediaEngine.getCodecsByKind(kind)
  1646. if len(codecs) == 0 {
  1647. return nil, ErrNoCodecsAvailable
  1648. }
  1649. track, err := NewTrackLocalStaticSample(codecs[0].RTPCodecCapability, util.MathRandAlpha(16), util.MathRandAlpha(16))
  1650. if err != nil {
  1651. return nil, err
  1652. }
  1653. t, err = pc.newTransceiverFromTrack(direction, track)
  1654. if err != nil {
  1655. return nil, err
  1656. }
  1657. case RTPTransceiverDirectionRecvonly:
  1658. receiver, err := pc.api.NewRTPReceiver(kind, pc.dtlsTransport)
  1659. if err != nil {
  1660. return nil, err
  1661. }
  1662. t = newRTPTransceiver(receiver, nil, RTPTransceiverDirectionRecvonly, kind, pc.api)
  1663. default:
  1664. return nil, errPeerConnAddTransceiverFromKindSupport
  1665. }
  1666. pc.mu.Lock()
  1667. pc.addRTPTransceiver(t)
  1668. pc.mu.Unlock()
  1669. return t, nil
  1670. }
  1671. // AddTransceiverFromTrack Create a new RtpTransceiver(SendRecv or SendOnly) and add it to the set of transceivers.
  1672. func (pc *PeerConnection) AddTransceiverFromTrack(track TrackLocal, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1673. if pc.isClosed.get() {
  1674. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1675. }
  1676. direction := RTPTransceiverDirectionSendrecv
  1677. if len(init) > 1 {
  1678. return nil, errPeerConnAddTransceiverFromTrackOnlyAcceptsOne
  1679. } else if len(init) == 1 {
  1680. direction = init[0].Direction
  1681. }
  1682. t, err = pc.newTransceiverFromTrack(direction, track)
  1683. if err == nil {
  1684. pc.mu.Lock()
  1685. pc.addRTPTransceiver(t)
  1686. pc.mu.Unlock()
  1687. }
  1688. return
  1689. }
  1690. // CreateDataChannel creates a new DataChannel object with the given label
  1691. // and optional DataChannelInit used to configure properties of the
  1692. // underlying channel such as data reliability.
  1693. func (pc *PeerConnection) CreateDataChannel(label string, options *DataChannelInit) (*DataChannel, error) {
  1694. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #2)
  1695. if pc.isClosed.get() {
  1696. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1697. }
  1698. params := &DataChannelParameters{
  1699. Label: label,
  1700. Ordered: true,
  1701. }
  1702. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
  1703. if options != nil {
  1704. params.ID = options.ID
  1705. }
  1706. if options != nil {
  1707. // Ordered indicates if data is allowed to be delivered out of order. The
  1708. // default value of true, guarantees that data will be delivered in order.
  1709. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
  1710. if options.Ordered != nil {
  1711. params.Ordered = *options.Ordered
  1712. }
  1713. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #7)
  1714. if options.MaxPacketLifeTime != nil {
  1715. params.MaxPacketLifeTime = options.MaxPacketLifeTime
  1716. }
  1717. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #8)
  1718. if options.MaxRetransmits != nil {
  1719. params.MaxRetransmits = options.MaxRetransmits
  1720. }
  1721. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #10)
  1722. if options.Protocol != nil {
  1723. params.Protocol = *options.Protocol
  1724. }
  1725. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #11)
  1726. if len(params.Protocol) > 65535 {
  1727. return nil, &rtcerr.TypeError{Err: ErrProtocolTooLarge}
  1728. }
  1729. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
  1730. if options.Negotiated != nil {
  1731. params.Negotiated = *options.Negotiated
  1732. }
  1733. }
  1734. d, err := pc.api.newDataChannel(params, nil, pc.log)
  1735. if err != nil {
  1736. return nil, err
  1737. }
  1738. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #16)
  1739. if d.maxPacketLifeTime != nil && d.maxRetransmits != nil {
  1740. return nil, &rtcerr.TypeError{Err: ErrRetransmitsOrPacketLifeTime}
  1741. }
  1742. pc.sctpTransport.lock.Lock()
  1743. pc.sctpTransport.dataChannels = append(pc.sctpTransport.dataChannels, d)
  1744. pc.sctpTransport.dataChannelsRequested++
  1745. pc.sctpTransport.lock.Unlock()
  1746. // If SCTP already connected open all the channels
  1747. if pc.sctpTransport.State() == SCTPTransportStateConnected {
  1748. if err = d.open(pc.sctpTransport); err != nil {
  1749. return nil, err
  1750. }
  1751. }
  1752. pc.mu.Lock()
  1753. pc.onNegotiationNeeded()
  1754. pc.mu.Unlock()
  1755. return d, nil
  1756. }
  1757. // SetIdentityProvider is used to configure an identity provider to generate identity assertions
  1758. func (pc *PeerConnection) SetIdentityProvider(string) error {
  1759. return errPeerConnSetIdentityProviderNotImplemented
  1760. }
  1761. // WriteRTCP sends a user provided RTCP packet to the connected peer. If no peer is connected the
  1762. // packet is discarded. It also runs any configured interceptors.
  1763. func (pc *PeerConnection) WriteRTCP(pkts []rtcp.Packet) error {
  1764. _, err := pc.interceptorRTCPWriter.Write(pkts, make(interceptor.Attributes))
  1765. return err
  1766. }
  1767. func (pc *PeerConnection) writeRTCP(pkts []rtcp.Packet, _ interceptor.Attributes) (int, error) {
  1768. return pc.dtlsTransport.WriteRTCP(pkts)
  1769. }
  1770. // Close ends the PeerConnection
  1771. func (pc *PeerConnection) Close() error {
  1772. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #1)
  1773. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #2)
  1774. if pc.isClosed.swap(true) {
  1775. return nil
  1776. }
  1777. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #3)
  1778. pc.signalingState.Set(SignalingStateClosed)
  1779. // Try closing everything and collect the errors
  1780. // Shutdown strategy:
  1781. // 1. All Conn close by closing their underlying Conn.
  1782. // 2. A Mux stops this chain. It won't close the underlying
  1783. // Conn if one of the endpoints is closed down. To
  1784. // continue the chain the Mux has to be closed.
  1785. closeErrs := make([]error, 4)
  1786. closeErrs = append(closeErrs, pc.api.interceptor.Close())
  1787. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4)
  1788. pc.mu.Lock()
  1789. for _, t := range pc.rtpTransceivers {
  1790. if !t.stopped {
  1791. closeErrs = append(closeErrs, t.Stop())
  1792. }
  1793. }
  1794. pc.mu.Unlock()
  1795. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #5)
  1796. pc.sctpTransport.lock.Lock()
  1797. for _, d := range pc.sctpTransport.dataChannels {
  1798. d.setReadyState(DataChannelStateClosed)
  1799. }
  1800. pc.sctpTransport.lock.Unlock()
  1801. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #6)
  1802. if pc.sctpTransport != nil {
  1803. closeErrs = append(closeErrs, pc.sctpTransport.Stop())
  1804. }
  1805. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #7)
  1806. closeErrs = append(closeErrs, pc.dtlsTransport.Stop())
  1807. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #8, #9, #10)
  1808. if pc.iceTransport != nil {
  1809. closeErrs = append(closeErrs, pc.iceTransport.Stop())
  1810. }
  1811. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #11)
  1812. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1813. return util.FlattenErrs(closeErrs)
  1814. }
  1815. // addRTPTransceiver appends t into rtpTransceivers
  1816. // and fires onNegotiationNeeded;
  1817. // caller of this method should hold `pc.mu` lock
  1818. func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
  1819. pc.rtpTransceivers = append(pc.rtpTransceivers, t)
  1820. pc.onNegotiationNeeded()
  1821. }
  1822. // CurrentLocalDescription represents the local description that was
  1823. // successfully negotiated the last time the PeerConnection transitioned
  1824. // into the stable state plus any local candidates that have been generated
  1825. // by the ICEAgent since the offer or answer was created.
  1826. func (pc *PeerConnection) CurrentLocalDescription() *SessionDescription {
  1827. pc.mu.Lock()
  1828. localDescription := pc.currentLocalDescription
  1829. iceGather := pc.iceGatherer
  1830. iceGatheringState := pc.ICEGatheringState()
  1831. pc.mu.Unlock()
  1832. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1833. }
  1834. // PendingLocalDescription represents a local description that is in the
  1835. // process of being negotiated plus any local candidates that have been
  1836. // generated by the ICEAgent since the offer or answer was created. If the
  1837. // PeerConnection is in the stable state, the value is null.
  1838. func (pc *PeerConnection) PendingLocalDescription() *SessionDescription {
  1839. pc.mu.Lock()
  1840. localDescription := pc.pendingLocalDescription
  1841. iceGather := pc.iceGatherer
  1842. iceGatheringState := pc.ICEGatheringState()
  1843. pc.mu.Unlock()
  1844. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1845. }
  1846. // CurrentRemoteDescription represents the last remote description that was
  1847. // successfully negotiated the last time the PeerConnection transitioned
  1848. // into the stable state plus any remote candidates that have been supplied
  1849. // via AddICECandidate() since the offer or answer was created.
  1850. func (pc *PeerConnection) CurrentRemoteDescription() *SessionDescription {
  1851. pc.mu.RLock()
  1852. defer pc.mu.RUnlock()
  1853. return pc.currentRemoteDescription
  1854. }
  1855. // PendingRemoteDescription represents a remote description that is in the
  1856. // process of being negotiated, complete with any remote candidates that
  1857. // have been supplied via AddICECandidate() since the offer or answer was
  1858. // created. If the PeerConnection is in the stable state, the value is
  1859. // null.
  1860. func (pc *PeerConnection) PendingRemoteDescription() *SessionDescription {
  1861. pc.mu.RLock()
  1862. defer pc.mu.RUnlock()
  1863. return pc.pendingRemoteDescription
  1864. }
  1865. // SignalingState attribute returns the signaling state of the
  1866. // PeerConnection instance.
  1867. func (pc *PeerConnection) SignalingState() SignalingState {
  1868. return pc.signalingState.Get()
  1869. }
  1870. // ICEGatheringState attribute returns the ICE gathering state of the
  1871. // PeerConnection instance.
  1872. func (pc *PeerConnection) ICEGatheringState() ICEGatheringState {
  1873. if pc.iceGatherer == nil {
  1874. return ICEGatheringStateNew
  1875. }
  1876. switch pc.iceGatherer.State() {
  1877. case ICEGathererStateNew:
  1878. return ICEGatheringStateNew
  1879. case ICEGathererStateGathering:
  1880. return ICEGatheringStateGathering
  1881. default:
  1882. return ICEGatheringStateComplete
  1883. }
  1884. }
  1885. // ConnectionState attribute returns the connection state of the
  1886. // PeerConnection instance.
  1887. func (pc *PeerConnection) ConnectionState() PeerConnectionState {
  1888. if state, ok := pc.connectionState.Load().(PeerConnectionState); ok {
  1889. return state
  1890. }
  1891. return PeerConnectionState(0)
  1892. }
  1893. // GetStats return data providing statistics about the overall connection
  1894. func (pc *PeerConnection) GetStats() StatsReport {
  1895. var (
  1896. dataChannelsAccepted uint32
  1897. dataChannelsClosed uint32
  1898. dataChannelsOpened uint32
  1899. dataChannelsRequested uint32
  1900. )
  1901. statsCollector := newStatsReportCollector()
  1902. statsCollector.Collecting()
  1903. pc.mu.Lock()
  1904. if pc.iceGatherer != nil {
  1905. pc.iceGatherer.collectStats(statsCollector)
  1906. }
  1907. if pc.iceTransport != nil {
  1908. pc.iceTransport.collectStats(statsCollector)
  1909. }
  1910. pc.sctpTransport.lock.Lock()
  1911. dataChannels := append([]*DataChannel{}, pc.sctpTransport.dataChannels...)
  1912. dataChannelsAccepted = pc.sctpTransport.dataChannelsAccepted
  1913. dataChannelsOpened = pc.sctpTransport.dataChannelsOpened
  1914. dataChannelsRequested = pc.sctpTransport.dataChannelsRequested
  1915. pc.sctpTransport.lock.Unlock()
  1916. for _, d := range dataChannels {
  1917. state := d.ReadyState()
  1918. if state != DataChannelStateConnecting && state != DataChannelStateOpen {
  1919. dataChannelsClosed++
  1920. }
  1921. d.collectStats(statsCollector)
  1922. }
  1923. pc.sctpTransport.collectStats(statsCollector)
  1924. stats := PeerConnectionStats{
  1925. Timestamp: statsTimestampNow(),
  1926. Type: StatsTypePeerConnection,
  1927. ID: pc.statsID,
  1928. DataChannelsAccepted: dataChannelsAccepted,
  1929. DataChannelsClosed: dataChannelsClosed,
  1930. DataChannelsOpened: dataChannelsOpened,
  1931. DataChannelsRequested: dataChannelsRequested,
  1932. }
  1933. statsCollector.Collect(stats.ID, stats)
  1934. certificates := pc.configuration.Certificates
  1935. for _, certificate := range certificates {
  1936. if err := certificate.collectStats(statsCollector); err != nil {
  1937. continue
  1938. }
  1939. }
  1940. pc.mu.Unlock()
  1941. pc.api.mediaEngine.collectStats(statsCollector)
  1942. return statsCollector.Ready()
  1943. }
  1944. // Start all transports. PeerConnection now has enough state
  1945. func (pc *PeerConnection) startTransports(iceRole ICERole, dtlsRole DTLSRole, remoteUfrag, remotePwd, fingerprint, fingerprintHash string) {
  1946. // Start the ice transport
  1947. err := pc.iceTransport.Start(
  1948. pc.iceGatherer,
  1949. ICEParameters{
  1950. UsernameFragment: remoteUfrag,
  1951. Password: remotePwd,
  1952. ICELite: false,
  1953. },
  1954. &iceRole,
  1955. )
  1956. if err != nil {
  1957. pc.log.Warnf("Failed to start manager: %s", err)
  1958. return
  1959. }
  1960. // Start the dtls transport
  1961. err = pc.dtlsTransport.Start(DTLSParameters{
  1962. Role: dtlsRole,
  1963. Fingerprints: []DTLSFingerprint{{Algorithm: fingerprintHash, Value: fingerprint}},
  1964. })
  1965. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1966. if err != nil {
  1967. pc.log.Warnf("Failed to start manager: %s", err)
  1968. return
  1969. }
  1970. }
  1971. // nolint: gocognit
  1972. func (pc *PeerConnection) startRTP(isRenegotiation bool, remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) {
  1973. if !isRenegotiation {
  1974. pc.undeclaredMediaProcessor()
  1975. }
  1976. pc.startRTPReceivers(remoteDesc, currentTransceivers)
  1977. if haveApplicationMediaSection(remoteDesc.parsed) {
  1978. pc.startSCTP()
  1979. }
  1980. }
  1981. // generateUnmatchedSDP generates an SDP that doesn't take remote state into account
  1982. // This is used for the initial call for CreateOffer
  1983. func (pc *PeerConnection) generateUnmatchedSDP(transceivers []*RTPTransceiver, useIdentity bool) (*sdp.SessionDescription, error) {
  1984. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  1985. if err != nil {
  1986. return nil, err
  1987. }
  1988. d.Attributes = append(d.Attributes, sdp.Attribute{Key: sdp.AttrKeyMsidSemantic, Value: "WMS*"})
  1989. iceParams, err := pc.iceGatherer.GetLocalParameters()
  1990. if err != nil {
  1991. return nil, err
  1992. }
  1993. candidates, err := pc.iceGatherer.GetLocalCandidates()
  1994. if err != nil {
  1995. return nil, err
  1996. }
  1997. isPlanB := pc.configuration.SDPSemantics == SDPSemanticsPlanB
  1998. mediaSections := []mediaSection{}
  1999. // Needed for pc.sctpTransport.dataChannelsRequested
  2000. pc.sctpTransport.lock.Lock()
  2001. defer pc.sctpTransport.lock.Unlock()
  2002. if isPlanB {
  2003. video := make([]*RTPTransceiver, 0)
  2004. audio := make([]*RTPTransceiver, 0)
  2005. for _, t := range transceivers {
  2006. if t.kind == RTPCodecTypeVideo {
  2007. video = append(video, t)
  2008. } else if t.kind == RTPCodecTypeAudio {
  2009. audio = append(audio, t)
  2010. }
  2011. if sender := t.Sender(); sender != nil {
  2012. sender.setNegotiated()
  2013. }
  2014. }
  2015. if len(video) > 0 {
  2016. mediaSections = append(mediaSections, mediaSection{id: "video", transceivers: video})
  2017. }
  2018. if len(audio) > 0 {
  2019. mediaSections = append(mediaSections, mediaSection{id: "audio", transceivers: audio})
  2020. }
  2021. if pc.sctpTransport.dataChannelsRequested != 0 {
  2022. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2023. }
  2024. } else {
  2025. for _, t := range transceivers {
  2026. if sender := t.Sender(); sender != nil {
  2027. sender.setNegotiated()
  2028. }
  2029. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2030. }
  2031. if pc.sctpTransport.dataChannelsRequested != 0 {
  2032. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2033. }
  2034. }
  2035. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2036. if err != nil {
  2037. return nil, err
  2038. }
  2039. 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)
  2040. }
  2041. // generateMatchedSDP generates a SDP and takes the remote state into account
  2042. // this is used everytime we have a RemoteDescription
  2043. // nolint: gocyclo
  2044. func (pc *PeerConnection) generateMatchedSDP(transceivers []*RTPTransceiver, useIdentity bool, includeUnmatched bool, connectionRole sdp.ConnectionRole) (*sdp.SessionDescription, error) { //nolint:gocognit
  2045. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  2046. if err != nil {
  2047. return nil, err
  2048. }
  2049. d.Attributes = append(d.Attributes, sdp.Attribute{Key: sdp.AttrKeyMsidSemantic, Value: "WMS*"})
  2050. iceParams, err := pc.iceGatherer.GetLocalParameters()
  2051. if err != nil {
  2052. return nil, err
  2053. }
  2054. candidates, err := pc.iceGatherer.GetLocalCandidates()
  2055. if err != nil {
  2056. return nil, err
  2057. }
  2058. var t *RTPTransceiver
  2059. remoteDescription := pc.currentRemoteDescription
  2060. if pc.pendingRemoteDescription != nil {
  2061. remoteDescription = pc.pendingRemoteDescription
  2062. }
  2063. isExtmapAllowMixed := isExtMapAllowMixedSet(remoteDescription.parsed)
  2064. localTransceivers := append([]*RTPTransceiver{}, transceivers...)
  2065. detectedPlanB := descriptionIsPlanB(remoteDescription, pc.log)
  2066. if pc.configuration.SDPSemantics != SDPSemanticsUnifiedPlan {
  2067. detectedPlanB = descriptionPossiblyPlanB(remoteDescription)
  2068. }
  2069. mediaSections := []mediaSection{}
  2070. alreadyHaveApplicationMediaSection := false
  2071. for _, media := range remoteDescription.parsed.MediaDescriptions {
  2072. midValue := getMidValue(media)
  2073. if midValue == "" {
  2074. return nil, errPeerConnRemoteDescriptionWithoutMidValue
  2075. }
  2076. if media.MediaName.Media == mediaSectionApplication {
  2077. mediaSections = append(mediaSections, mediaSection{id: midValue, data: true})
  2078. alreadyHaveApplicationMediaSection = true
  2079. continue
  2080. }
  2081. kind := NewRTPCodecType(media.MediaName.Media)
  2082. direction := getPeerDirection(media)
  2083. if kind == 0 || direction == RTPTransceiverDirection(Unknown) {
  2084. continue
  2085. }
  2086. sdpSemantics := pc.configuration.SDPSemantics
  2087. switch {
  2088. case sdpSemantics == SDPSemanticsPlanB || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB:
  2089. if !detectedPlanB {
  2090. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected PlanB, but RemoteDescription is UnifiedPlan", ErrIncorrectSDPSemantics)}
  2091. }
  2092. // If we're responding to a plan-b offer, then we should try to fill up this
  2093. // media entry with all matching local transceivers
  2094. mediaTransceivers := []*RTPTransceiver{}
  2095. for {
  2096. // keep going until we can't get any more
  2097. t, localTransceivers = satisfyTypeAndDirection(kind, direction, localTransceivers)
  2098. if t == nil {
  2099. if len(mediaTransceivers) == 0 {
  2100. t = &RTPTransceiver{kind: kind, api: pc.api, codecs: pc.api.mediaEngine.getCodecsByKind(kind)}
  2101. t.setDirection(RTPTransceiverDirectionInactive)
  2102. mediaTransceivers = append(mediaTransceivers, t)
  2103. }
  2104. break
  2105. }
  2106. if sender := t.Sender(); sender != nil {
  2107. sender.setNegotiated()
  2108. }
  2109. mediaTransceivers = append(mediaTransceivers, t)
  2110. }
  2111. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers})
  2112. case sdpSemantics == SDPSemanticsUnifiedPlan || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback:
  2113. if detectedPlanB {
  2114. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected UnifiedPlan, but RemoteDescription is PlanB", ErrIncorrectSDPSemantics)}
  2115. }
  2116. t, localTransceivers = findByMid(midValue, localTransceivers)
  2117. if t == nil {
  2118. return nil, fmt.Errorf("%w: %q", errPeerConnTranscieverMidNil, midValue)
  2119. }
  2120. if sender := t.Sender(); sender != nil {
  2121. sender.setNegotiated()
  2122. }
  2123. mediaTransceivers := []*RTPTransceiver{t}
  2124. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, ridMap: getRids(media)})
  2125. }
  2126. }
  2127. var bundleGroup *string
  2128. // If we are offering also include unmatched local transceivers
  2129. if includeUnmatched {
  2130. if !detectedPlanB {
  2131. for _, t := range localTransceivers {
  2132. if sender := t.Sender(); sender != nil {
  2133. sender.setNegotiated()
  2134. }
  2135. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2136. }
  2137. }
  2138. if pc.sctpTransport.dataChannelsRequested != 0 && !alreadyHaveApplicationMediaSection {
  2139. if detectedPlanB {
  2140. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2141. } else {
  2142. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2143. }
  2144. }
  2145. } else if remoteDescription != nil {
  2146. groupValue, _ := remoteDescription.parsed.Attribute(sdp.AttrKeyGroup)
  2147. groupValue = strings.TrimLeft(groupValue, "BUNDLE")
  2148. bundleGroup = &groupValue
  2149. }
  2150. if pc.configuration.SDPSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB {
  2151. pc.log.Info("Plan-B Offer detected; responding with Plan-B Answer")
  2152. }
  2153. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2154. if err != nil {
  2155. return nil, err
  2156. }
  2157. 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)
  2158. }
  2159. func (pc *PeerConnection) setGatherCompleteHandler(handler func()) {
  2160. pc.iceGatherer.onGatheringCompleteHandler.Store(handler)
  2161. }
  2162. // SCTP returns the SCTPTransport for this PeerConnection
  2163. //
  2164. // The SCTP transport over which SCTP data is sent and received. If SCTP has not been negotiated, the value is nil.
  2165. // https://www.w3.org/TR/webrtc/#attributes-15
  2166. func (pc *PeerConnection) SCTP() *SCTPTransport {
  2167. return pc.sctpTransport
  2168. }