peerconnection.go 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496
  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 controlling agent.
  717. // If both or neither agents are lite the offering agent is controlling.
  718. // RFC 8445 S6.1.1
  719. if isIceLiteSet(remoteDesc.parsed) && !pc.api.settingEngine.candidates.ICELite {
  720. connectionRole = connectionRoleFromDtlsRole(DTLSRoleServer)
  721. }
  722. }
  723. pc.mu.Lock()
  724. defer pc.mu.Unlock()
  725. d, err := pc.generateMatchedSDP(pc.rtpTransceivers, useIdentity, false /*includeUnmatched */, connectionRole)
  726. if err != nil {
  727. return SessionDescription{}, err
  728. }
  729. updateSDPOrigin(&pc.sdpOrigin, d)
  730. sdpBytes, err := d.Marshal()
  731. if err != nil {
  732. return SessionDescription{}, err
  733. }
  734. desc := SessionDescription{
  735. Type: SDPTypeAnswer,
  736. SDP: string(sdpBytes),
  737. parsed: d,
  738. }
  739. pc.lastAnswer = desc.SDP
  740. return desc, nil
  741. }
  742. // 4.4.1.6 Set the SessionDescription
  743. func (pc *PeerConnection) setDescription(sd *SessionDescription, op stateChangeOp) error { //nolint:gocognit
  744. switch {
  745. case pc.isClosed.get():
  746. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  747. case NewSDPType(sd.Type.String()) == SDPType(Unknown):
  748. return &rtcerr.TypeError{Err: fmt.Errorf("%w: '%d' is not a valid enum value of type SDPType", errPeerConnSDPTypeInvalidValue, sd.Type)}
  749. }
  750. nextState, err := func() (SignalingState, error) {
  751. pc.mu.Lock()
  752. defer pc.mu.Unlock()
  753. cur := pc.SignalingState()
  754. setLocal := stateChangeOpSetLocal
  755. setRemote := stateChangeOpSetRemote
  756. newSDPDoesNotMatchOffer := &rtcerr.InvalidModificationError{Err: errSDPDoesNotMatchOffer}
  757. newSDPDoesNotMatchAnswer := &rtcerr.InvalidModificationError{Err: errSDPDoesNotMatchAnswer}
  758. var nextState SignalingState
  759. var err error
  760. switch op {
  761. case setLocal:
  762. switch sd.Type {
  763. // stable->SetLocal(offer)->have-local-offer
  764. case SDPTypeOffer:
  765. if sd.SDP != pc.lastOffer {
  766. return nextState, newSDPDoesNotMatchOffer
  767. }
  768. nextState, err = checkNextSignalingState(cur, SignalingStateHaveLocalOffer, setLocal, sd.Type)
  769. if err == nil {
  770. pc.pendingLocalDescription = sd
  771. }
  772. // have-remote-offer->SetLocal(answer)->stable
  773. // have-local-pranswer->SetLocal(answer)->stable
  774. case SDPTypeAnswer:
  775. if sd.SDP != pc.lastAnswer {
  776. return nextState, newSDPDoesNotMatchAnswer
  777. }
  778. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setLocal, sd.Type)
  779. if err == nil {
  780. pc.currentLocalDescription = sd
  781. pc.currentRemoteDescription = pc.pendingRemoteDescription
  782. pc.pendingRemoteDescription = nil
  783. pc.pendingLocalDescription = nil
  784. }
  785. case SDPTypeRollback:
  786. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setLocal, sd.Type)
  787. if err == nil {
  788. pc.pendingLocalDescription = nil
  789. }
  790. // have-remote-offer->SetLocal(pranswer)->have-local-pranswer
  791. case SDPTypePranswer:
  792. if sd.SDP != pc.lastAnswer {
  793. return nextState, newSDPDoesNotMatchAnswer
  794. }
  795. nextState, err = checkNextSignalingState(cur, SignalingStateHaveLocalPranswer, setLocal, sd.Type)
  796. if err == nil {
  797. pc.pendingLocalDescription = sd
  798. }
  799. default:
  800. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %s(%s)", errPeerConnStateChangeInvalid, op, sd.Type)}
  801. }
  802. case setRemote:
  803. switch sd.Type {
  804. // stable->SetRemote(offer)->have-remote-offer
  805. case SDPTypeOffer:
  806. nextState, err = checkNextSignalingState(cur, SignalingStateHaveRemoteOffer, setRemote, sd.Type)
  807. if err == nil {
  808. pc.pendingRemoteDescription = sd
  809. }
  810. // have-local-offer->SetRemote(answer)->stable
  811. // have-remote-pranswer->SetRemote(answer)->stable
  812. case SDPTypeAnswer:
  813. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setRemote, sd.Type)
  814. if err == nil {
  815. pc.currentRemoteDescription = sd
  816. pc.currentLocalDescription = pc.pendingLocalDescription
  817. pc.pendingRemoteDescription = nil
  818. pc.pendingLocalDescription = nil
  819. }
  820. case SDPTypeRollback:
  821. nextState, err = checkNextSignalingState(cur, SignalingStateStable, setRemote, sd.Type)
  822. if err == nil {
  823. pc.pendingRemoteDescription = nil
  824. }
  825. // have-local-offer->SetRemote(pranswer)->have-remote-pranswer
  826. case SDPTypePranswer:
  827. nextState, err = checkNextSignalingState(cur, SignalingStateHaveRemotePranswer, setRemote, sd.Type)
  828. if err == nil {
  829. pc.pendingRemoteDescription = sd
  830. }
  831. default:
  832. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %s(%s)", errPeerConnStateChangeInvalid, op, sd.Type)}
  833. }
  834. default:
  835. return nextState, &rtcerr.OperationError{Err: fmt.Errorf("%w: %q", errPeerConnStateChangeUnhandled, op)}
  836. }
  837. return nextState, err
  838. }()
  839. if err == nil {
  840. pc.signalingState.Set(nextState)
  841. if pc.signalingState.Get() == SignalingStateStable {
  842. pc.isNegotiationNeeded.set(false)
  843. pc.mu.Lock()
  844. pc.onNegotiationNeeded()
  845. pc.mu.Unlock()
  846. }
  847. pc.onSignalingStateChange(nextState)
  848. }
  849. return err
  850. }
  851. // SetLocalDescription sets the SessionDescription of the local peer
  852. func (pc *PeerConnection) SetLocalDescription(desc SessionDescription) error {
  853. if pc.isClosed.get() {
  854. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  855. }
  856. haveLocalDescription := pc.currentLocalDescription != nil
  857. // JSEP 5.4
  858. if desc.SDP == "" {
  859. switch desc.Type {
  860. case SDPTypeAnswer, SDPTypePranswer:
  861. desc.SDP = pc.lastAnswer
  862. case SDPTypeOffer:
  863. desc.SDP = pc.lastOffer
  864. default:
  865. return &rtcerr.InvalidModificationError{
  866. Err: fmt.Errorf("%w: %s", errPeerConnSDPTypeInvalidValueSetLocalDescription, desc.Type),
  867. }
  868. }
  869. }
  870. desc.parsed = &sdp.SessionDescription{}
  871. if err := desc.parsed.Unmarshal([]byte(desc.SDP)); err != nil {
  872. return err
  873. }
  874. if err := pc.setDescription(&desc, stateChangeOpSetLocal); err != nil {
  875. return err
  876. }
  877. currentTransceivers := append([]*RTPTransceiver{}, pc.GetTransceivers()...)
  878. weAnswer := desc.Type == SDPTypeAnswer
  879. remoteDesc := pc.RemoteDescription()
  880. if weAnswer && remoteDesc != nil {
  881. _ = setRTPTransceiverCurrentDirection(&desc, currentTransceivers, false)
  882. if err := pc.startRTPSenders(currentTransceivers); err != nil {
  883. return err
  884. }
  885. pc.configureRTPReceivers(haveLocalDescription, remoteDesc, currentTransceivers)
  886. pc.ops.Enqueue(func() {
  887. pc.startRTP(haveLocalDescription, remoteDesc, currentTransceivers)
  888. })
  889. }
  890. if pc.iceGatherer.State() == ICEGathererStateNew {
  891. return pc.iceGatherer.Gather()
  892. }
  893. return nil
  894. }
  895. // LocalDescription returns PendingLocalDescription if it is not null and
  896. // otherwise it returns CurrentLocalDescription. This property is used to
  897. // determine if SetLocalDescription has already been called.
  898. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-localdescription
  899. func (pc *PeerConnection) LocalDescription() *SessionDescription {
  900. if pendingLocalDescription := pc.PendingLocalDescription(); pendingLocalDescription != nil {
  901. return pendingLocalDescription
  902. }
  903. return pc.CurrentLocalDescription()
  904. }
  905. // SetRemoteDescription sets the SessionDescription of the remote peer
  906. func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error { //nolint:gocognit,gocyclo
  907. if pc.isClosed.get() {
  908. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  909. }
  910. isRenegotation := 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 isRenegotation && 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 isRenegotation {
  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 controlling agent.
  1034. // If both or neither agents are lite the offering agent is controlling.
  1035. // RFC 8445 S6.1.1
  1036. if (weOffer && remoteIsLite == pc.api.settingEngine.candidates.ICELite) || (remoteIsLite && !pc.api.settingEngine.candidates.ICELite) {
  1037. iceRole = ICERoleControlling
  1038. }
  1039. // Start the networking in a new routine since it will block until
  1040. // the connection is actually established.
  1041. if weOffer {
  1042. _ = setRTPTransceiverCurrentDirection(&desc, currentTransceivers, true)
  1043. if err := pc.startRTPSenders(currentTransceivers); err != nil {
  1044. return err
  1045. }
  1046. pc.configureRTPReceivers(false, &desc, currentTransceivers)
  1047. }
  1048. pc.ops.Enqueue(func() {
  1049. pc.startTransports(iceRole, dtlsRoleFromRemoteSDP(desc.parsed), remoteUfrag, remotePwd, fingerprint, fingerprintHash)
  1050. if weOffer {
  1051. pc.startRTP(false, &desc, currentTransceivers)
  1052. }
  1053. })
  1054. return nil
  1055. }
  1056. func (pc *PeerConnection) configureReceiver(incoming trackDetails, receiver *RTPReceiver) {
  1057. receiver.configureReceive(trackDetailsToRTPReceiveParameters(&incoming))
  1058. // set track id and label early so they can be set as new track information
  1059. // is received from the SDP.
  1060. for i := range receiver.tracks {
  1061. receiver.tracks[i].track.mu.Lock()
  1062. receiver.tracks[i].track.id = incoming.id
  1063. receiver.tracks[i].track.streamID = incoming.streamID
  1064. receiver.tracks[i].track.mu.Unlock()
  1065. }
  1066. }
  1067. func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPReceiver) {
  1068. if err := receiver.startReceive(trackDetailsToRTPReceiveParameters(&incoming)); err != nil {
  1069. pc.log.Warnf("RTPReceiver Receive failed %s", err)
  1070. return
  1071. }
  1072. for _, t := range receiver.Tracks() {
  1073. if t.SSRC() == 0 || t.RID() != "" {
  1074. return
  1075. }
  1076. go func(track *TrackRemote) {
  1077. b := make([]byte, pc.api.settingEngine.getReceiveMTU())
  1078. n, _, err := track.peek(b)
  1079. if err != nil {
  1080. pc.log.Warnf("Could not determine PayloadType for SSRC %d (%s)", track.SSRC(), err)
  1081. return
  1082. }
  1083. if err = track.checkAndUpdateTrack(b[:n]); err != nil {
  1084. pc.log.Warnf("Failed to set codec settings for track SSRC %d (%s)", track.SSRC(), err)
  1085. return
  1086. }
  1087. pc.onTrack(track, receiver)
  1088. }(t)
  1089. }
  1090. }
  1091. func setRTPTransceiverCurrentDirection(answer *SessionDescription, currentTransceivers []*RTPTransceiver, weOffer bool) error {
  1092. currentTransceivers = append([]*RTPTransceiver{}, currentTransceivers...)
  1093. for _, media := range answer.parsed.MediaDescriptions {
  1094. midValue := getMidValue(media)
  1095. if midValue == "" {
  1096. return errPeerConnRemoteDescriptionWithoutMidValue
  1097. }
  1098. if media.MediaName.Media == mediaSectionApplication {
  1099. continue
  1100. }
  1101. var t *RTPTransceiver
  1102. t, currentTransceivers = findByMid(midValue, currentTransceivers)
  1103. if t == nil {
  1104. return fmt.Errorf("%w: %q", errPeerConnTranscieverMidNil, midValue)
  1105. }
  1106. direction := getPeerDirection(media)
  1107. if direction == RTPTransceiverDirection(Unknown) {
  1108. continue
  1109. }
  1110. // reverse direction if it was a remote answer
  1111. if weOffer {
  1112. switch direction {
  1113. case RTPTransceiverDirectionSendonly:
  1114. direction = RTPTransceiverDirectionRecvonly
  1115. case RTPTransceiverDirectionRecvonly:
  1116. direction = RTPTransceiverDirectionSendonly
  1117. default:
  1118. }
  1119. }
  1120. // If a transceiver is created by applying a remote description that has recvonly transceiver,
  1121. // it will have no sender. In this case, the transceiver's current direction is set to inactive so
  1122. // that the transceiver can be reused by next AddTrack.
  1123. if direction == RTPTransceiverDirectionSendonly && t.Sender() == nil {
  1124. direction = RTPTransceiverDirectionInactive
  1125. }
  1126. t.setCurrentDirection(direction)
  1127. }
  1128. return nil
  1129. }
  1130. func runIfNewReceiver(
  1131. incomingTrack trackDetails,
  1132. transceivers []*RTPTransceiver,
  1133. f func(incomingTrack trackDetails, receiver *RTPReceiver),
  1134. ) bool {
  1135. for _, t := range transceivers {
  1136. if t.Mid() != incomingTrack.mid {
  1137. continue
  1138. }
  1139. receiver := t.Receiver()
  1140. if (incomingTrack.kind != t.Kind()) ||
  1141. (t.Direction() != RTPTransceiverDirectionRecvonly && t.Direction() != RTPTransceiverDirectionSendrecv) ||
  1142. receiver == nil ||
  1143. (receiver.haveReceived()) {
  1144. continue
  1145. }
  1146. f(incomingTrack, receiver)
  1147. return true
  1148. }
  1149. return false
  1150. }
  1151. // configurepRTPReceivers opens knows inbound SRTP streams from the RemoteDescription
  1152. func (pc *PeerConnection) configureRTPReceivers(isRenegotiation bool, remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) { //nolint:gocognit
  1153. incomingTracks := trackDetailsFromSDP(pc.log, remoteDesc.parsed)
  1154. if isRenegotiation {
  1155. for _, t := range currentTransceivers {
  1156. receiver := t.Receiver()
  1157. if receiver == nil {
  1158. continue
  1159. }
  1160. tracks := t.Receiver().Tracks()
  1161. if len(tracks) == 0 {
  1162. continue
  1163. }
  1164. mid := t.Mid()
  1165. receiverNeedsStopped := false
  1166. func() {
  1167. for _, t := range tracks {
  1168. t.mu.Lock()
  1169. defer t.mu.Unlock()
  1170. if t.rid != "" {
  1171. if details := trackDetailsForRID(incomingTracks, mid, t.rid); details != nil {
  1172. t.id = details.id
  1173. t.streamID = details.streamID
  1174. continue
  1175. }
  1176. } else if t.ssrc != 0 {
  1177. if details := trackDetailsForSSRC(incomingTracks, t.ssrc); details != nil {
  1178. t.id = details.id
  1179. t.streamID = details.streamID
  1180. continue
  1181. }
  1182. }
  1183. receiverNeedsStopped = true
  1184. }
  1185. }()
  1186. if !receiverNeedsStopped {
  1187. continue
  1188. }
  1189. if err := receiver.Stop(); err != nil {
  1190. pc.log.Warnf("Failed to stop RtpReceiver: %s", err)
  1191. continue
  1192. }
  1193. receiver, err := pc.api.NewRTPReceiver(receiver.kind, pc.dtlsTransport)
  1194. if err != nil {
  1195. pc.log.Warnf("Failed to create new RtpReceiver: %s", err)
  1196. continue
  1197. }
  1198. t.setReceiver(receiver)
  1199. }
  1200. }
  1201. localTransceivers := append([]*RTPTransceiver{}, currentTransceivers...)
  1202. // Ensure we haven't already started a transceiver for this ssrc
  1203. filteredTracks := append([]trackDetails{}, incomingTracks...)
  1204. for _, incomingTrack := range incomingTracks {
  1205. // If we already have a TrackRemote for a given SSRC don't handle it again
  1206. for _, t := range localTransceivers {
  1207. if receiver := t.Receiver(); receiver != nil {
  1208. for _, track := range receiver.Tracks() {
  1209. for _, ssrc := range incomingTrack.ssrcs {
  1210. if ssrc == track.SSRC() {
  1211. filteredTracks = filterTrackWithSSRC(filteredTracks, track.SSRC())
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. for _, incomingTrack := range filteredTracks {
  1219. _ = runIfNewReceiver(incomingTrack, localTransceivers, pc.configureReceiver)
  1220. }
  1221. }
  1222. // startRTPReceivers opens knows inbound SRTP streams from the RemoteDescription
  1223. func (pc *PeerConnection) startRTPReceivers(remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) {
  1224. incomingTracks := trackDetailsFromSDP(pc.log, remoteDesc.parsed)
  1225. if len(incomingTracks) == 0 {
  1226. return
  1227. }
  1228. localTransceivers := append([]*RTPTransceiver{}, currentTransceivers...)
  1229. unhandledTracks := incomingTracks[:0]
  1230. for _, incomingTrack := range incomingTracks {
  1231. trackHandled := runIfNewReceiver(incomingTrack, localTransceivers, pc.startReceiver)
  1232. if !trackHandled {
  1233. unhandledTracks = append(unhandledTracks, incomingTrack)
  1234. }
  1235. }
  1236. remoteIsPlanB := false
  1237. switch pc.configuration.SDPSemantics {
  1238. case SDPSemanticsPlanB:
  1239. remoteIsPlanB = true
  1240. case SDPSemanticsUnifiedPlanWithFallback:
  1241. remoteIsPlanB = descriptionPossiblyPlanB(pc.RemoteDescription())
  1242. default:
  1243. // none
  1244. }
  1245. if remoteIsPlanB {
  1246. for _, incomingTrack := range unhandledTracks {
  1247. t, err := pc.AddTransceiverFromKind(incomingTrack.kind, RTPTransceiverInit{
  1248. Direction: RTPTransceiverDirectionSendrecv,
  1249. })
  1250. if err != nil {
  1251. pc.log.Warnf("Could not add transceiver for remote SSRC %d: %s", incomingTrack.ssrcs[0], err)
  1252. continue
  1253. }
  1254. pc.configureReceiver(incomingTrack, t.Receiver())
  1255. pc.startReceiver(incomingTrack, t.Receiver())
  1256. }
  1257. }
  1258. }
  1259. // startRTPSenders starts all outbound RTP streams
  1260. func (pc *PeerConnection) startRTPSenders(currentTransceivers []*RTPTransceiver) error {
  1261. for _, transceiver := range currentTransceivers {
  1262. if sender := transceiver.Sender(); sender != nil && sender.isNegotiated() && !sender.hasSent() {
  1263. err := sender.Send(sender.GetParameters())
  1264. if err != nil {
  1265. return err
  1266. }
  1267. }
  1268. }
  1269. return nil
  1270. }
  1271. // Start SCTP subsystem
  1272. func (pc *PeerConnection) startSCTP() {
  1273. // Start sctp
  1274. if err := pc.sctpTransport.Start(SCTPCapabilities{
  1275. MaxMessageSize: 0,
  1276. }); err != nil {
  1277. pc.log.Warnf("Failed to start SCTP: %s", err)
  1278. if err = pc.sctpTransport.Stop(); err != nil {
  1279. pc.log.Warnf("Failed to stop SCTPTransport: %s", err)
  1280. }
  1281. return
  1282. }
  1283. }
  1284. func (pc *PeerConnection) handleUndeclaredSSRC(ssrc SSRC, remoteDescription *SessionDescription) (handled bool, err error) {
  1285. if len(remoteDescription.parsed.MediaDescriptions) != 1 {
  1286. return false, nil
  1287. }
  1288. onlyMediaSection := remoteDescription.parsed.MediaDescriptions[0]
  1289. streamID := ""
  1290. id := ""
  1291. hasRidAttribute := false
  1292. hasSSRCAttribute := false
  1293. for _, a := range onlyMediaSection.Attributes {
  1294. switch a.Key {
  1295. case sdp.AttrKeyMsid:
  1296. if split := strings.Split(a.Value, " "); len(split) == 2 {
  1297. streamID = split[0]
  1298. id = split[1]
  1299. }
  1300. case sdp.AttrKeySSRC:
  1301. hasSSRCAttribute = true
  1302. case sdpAttributeRid:
  1303. hasRidAttribute = true
  1304. }
  1305. }
  1306. if hasRidAttribute {
  1307. return false, nil
  1308. } else if hasSSRCAttribute {
  1309. return false, errPeerConnSingleMediaSectionHasExplicitSSRC
  1310. }
  1311. incoming := trackDetails{
  1312. ssrcs: []SSRC{ssrc},
  1313. kind: RTPCodecTypeVideo,
  1314. streamID: streamID,
  1315. id: id,
  1316. }
  1317. if onlyMediaSection.MediaName.Media == RTPCodecTypeAudio.String() {
  1318. incoming.kind = RTPCodecTypeAudio
  1319. }
  1320. t, err := pc.AddTransceiverFromKind(incoming.kind, RTPTransceiverInit{
  1321. Direction: RTPTransceiverDirectionSendrecv,
  1322. })
  1323. if err != nil {
  1324. // nolint
  1325. return false, fmt.Errorf("%w: %d: %s", errPeerConnRemoteSSRCAddTransceiver, ssrc, err)
  1326. }
  1327. pc.configureReceiver(incoming, t.Receiver())
  1328. pc.startReceiver(incoming, t.Receiver())
  1329. return true, nil
  1330. }
  1331. func (pc *PeerConnection) handleIncomingSSRC(rtpStream io.Reader, ssrc SSRC) error { //nolint:gocognit
  1332. remoteDescription := pc.RemoteDescription()
  1333. if remoteDescription == nil {
  1334. return errPeerConnRemoteDescriptionNil
  1335. }
  1336. // If a SSRC already exists in the RemoteDescription don't perform heuristics upon it
  1337. for _, track := range trackDetailsFromSDP(pc.log, remoteDescription.parsed) {
  1338. if track.repairSsrc != nil && ssrc == *track.repairSsrc {
  1339. return nil
  1340. }
  1341. for _, trackSsrc := range track.ssrcs {
  1342. if ssrc == trackSsrc {
  1343. return nil
  1344. }
  1345. }
  1346. }
  1347. // If the remote SDP was only one media section the ssrc doesn't have to be explicitly declared
  1348. if handled, err := pc.handleUndeclaredSSRC(ssrc, remoteDescription); handled || err != nil {
  1349. return err
  1350. }
  1351. midExtensionID, audioSupported, videoSupported := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdp.SDESMidURI})
  1352. if !audioSupported && !videoSupported {
  1353. return errPeerConnSimulcastMidRTPExtensionRequired
  1354. }
  1355. streamIDExtensionID, audioSupported, videoSupported := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdp.SDESRTPStreamIDURI})
  1356. if !audioSupported && !videoSupported {
  1357. return errPeerConnSimulcastStreamIDRTPExtensionRequired
  1358. }
  1359. repairStreamIDExtensionID, _, _ := pc.api.mediaEngine.getHeaderExtensionID(RTPHeaderExtensionCapability{sdesRepairRTPStreamIDURI})
  1360. b := make([]byte, pc.api.settingEngine.getReceiveMTU())
  1361. i, err := rtpStream.Read(b)
  1362. if err != nil {
  1363. return err
  1364. }
  1365. var mid, rid, rsid string
  1366. payloadType, err := handleUnknownRTPPacket(b[:i], uint8(midExtensionID), uint8(streamIDExtensionID), uint8(repairStreamIDExtensionID), &mid, &rid, &rsid)
  1367. if err != nil {
  1368. return err
  1369. }
  1370. params, err := pc.api.mediaEngine.getRTPParametersByPayloadType(payloadType)
  1371. if err != nil {
  1372. return err
  1373. }
  1374. streamInfo := createStreamInfo("", ssrc, params.Codecs[0].PayloadType, params.Codecs[0].RTPCodecCapability, params.HeaderExtensions)
  1375. readStream, interceptor, rtcpReadStream, rtcpInterceptor, err := pc.dtlsTransport.streamsForSSRC(ssrc, *streamInfo)
  1376. if err != nil {
  1377. return err
  1378. }
  1379. for readCount := 0; readCount <= simulcastProbeCount; readCount++ {
  1380. if mid == "" || (rid == "" && rsid == "") {
  1381. i, _, err := interceptor.Read(b, nil)
  1382. if err != nil {
  1383. return err
  1384. }
  1385. if _, err = handleUnknownRTPPacket(b[:i], uint8(midExtensionID), uint8(streamIDExtensionID), uint8(repairStreamIDExtensionID), &mid, &rid, &rsid); err != nil {
  1386. return err
  1387. }
  1388. continue
  1389. }
  1390. for _, t := range pc.GetTransceivers() {
  1391. receiver := t.Receiver()
  1392. if t.Mid() != mid || receiver == nil {
  1393. continue
  1394. }
  1395. if rsid != "" {
  1396. receiver.mu.Lock()
  1397. defer receiver.mu.Unlock()
  1398. return receiver.receiveForRtx(SSRC(0), rsid, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1399. }
  1400. track, err := receiver.receiveForRid(rid, params, streamInfo, readStream, interceptor, rtcpReadStream, rtcpInterceptor)
  1401. if err != nil {
  1402. return err
  1403. }
  1404. pc.onTrack(track, receiver)
  1405. return nil
  1406. }
  1407. }
  1408. pc.api.interceptor.UnbindRemoteStream(streamInfo)
  1409. return errPeerConnSimulcastIncomingSSRCFailed
  1410. }
  1411. // undeclaredMediaProcessor handles RTP/RTCP packets that don't match any a:ssrc lines
  1412. func (pc *PeerConnection) undeclaredMediaProcessor() {
  1413. go pc.undeclaredRTPMediaProcessor()
  1414. go pc.undeclaredRTCPMediaProcessor()
  1415. }
  1416. func (pc *PeerConnection) undeclaredRTPMediaProcessor() {
  1417. var simulcastRoutineCount uint64
  1418. for {
  1419. srtpSession, err := pc.dtlsTransport.getSRTPSession()
  1420. if err != nil {
  1421. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtpSession: %v", err)
  1422. return
  1423. }
  1424. stream, ssrc, err := srtpSession.AcceptStream()
  1425. if err != nil {
  1426. pc.log.Warnf("Failed to accept RTP %v", err)
  1427. return
  1428. }
  1429. if pc.isClosed.get() {
  1430. if err = stream.Close(); err != nil {
  1431. pc.log.Warnf("Failed to close RTP stream %v", err)
  1432. }
  1433. continue
  1434. }
  1435. if atomic.AddUint64(&simulcastRoutineCount, 1) >= simulcastMaxProbeRoutines {
  1436. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1437. pc.log.Warn(ErrSimulcastProbeOverflow.Error())
  1438. pc.dtlsTransport.storeSimulcastStream(stream)
  1439. continue
  1440. }
  1441. go func(rtpStream io.Reader, ssrc SSRC) {
  1442. if err := pc.handleIncomingSSRC(rtpStream, ssrc); err != nil {
  1443. pc.log.Errorf(incomingUnhandledRTPSsrc, ssrc, err)
  1444. pc.dtlsTransport.storeSimulcastStream(stream)
  1445. }
  1446. atomic.AddUint64(&simulcastRoutineCount, ^uint64(0))
  1447. }(stream, SSRC(ssrc))
  1448. }
  1449. }
  1450. func (pc *PeerConnection) undeclaredRTCPMediaProcessor() {
  1451. var unhandledStreams []*srtp.ReadStreamSRTCP
  1452. defer func() {
  1453. for _, s := range unhandledStreams {
  1454. _ = s.Close()
  1455. }
  1456. }()
  1457. for {
  1458. srtcpSession, err := pc.dtlsTransport.getSRTCPSession()
  1459. if err != nil {
  1460. pc.log.Warnf("undeclaredMediaProcessor failed to open SrtcpSession: %v", err)
  1461. return
  1462. }
  1463. stream, ssrc, err := srtcpSession.AcceptStream()
  1464. if err != nil {
  1465. pc.log.Warnf("Failed to accept RTCP %v", err)
  1466. return
  1467. }
  1468. pc.log.Warnf("Incoming unhandled RTCP ssrc(%d), OnTrack will not be fired", ssrc)
  1469. unhandledStreams = append(unhandledStreams, stream)
  1470. }
  1471. }
  1472. // RemoteDescription returns pendingRemoteDescription if it is not null and
  1473. // otherwise it returns currentRemoteDescription. This property is used to
  1474. // determine if setRemoteDescription has already been called.
  1475. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-remotedescription
  1476. func (pc *PeerConnection) RemoteDescription() *SessionDescription {
  1477. pc.mu.RLock()
  1478. defer pc.mu.RUnlock()
  1479. if pc.pendingRemoteDescription != nil {
  1480. return pc.pendingRemoteDescription
  1481. }
  1482. return pc.currentRemoteDescription
  1483. }
  1484. // AddICECandidate accepts an ICE candidate string and adds it
  1485. // to the existing set of candidates.
  1486. func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
  1487. if pc.RemoteDescription() == nil {
  1488. return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
  1489. }
  1490. candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
  1491. var iceCandidate *ICECandidate
  1492. if candidateValue != "" {
  1493. candidate, err := ice.UnmarshalCandidate(candidateValue)
  1494. if err != nil {
  1495. if errors.Is(err, ice.ErrUnknownCandidateTyp) || errors.Is(err, ice.ErrDetermineNetworkType) {
  1496. pc.log.Warnf("Discarding remote candidate: %s", err)
  1497. return nil
  1498. }
  1499. return err
  1500. }
  1501. c, err := newICECandidateFromICE(candidate)
  1502. if err != nil {
  1503. return err
  1504. }
  1505. iceCandidate = &c
  1506. }
  1507. return pc.iceTransport.AddRemoteCandidate(iceCandidate)
  1508. }
  1509. // ICEConnectionState returns the ICE connection state of the
  1510. // PeerConnection instance.
  1511. func (pc *PeerConnection) ICEConnectionState() ICEConnectionState {
  1512. if state, ok := pc.iceConnectionState.Load().(ICEConnectionState); ok {
  1513. return state
  1514. }
  1515. return ICEConnectionState(0)
  1516. }
  1517. // GetSenders returns the RTPSender that are currently attached to this PeerConnection
  1518. func (pc *PeerConnection) GetSenders() (result []*RTPSender) {
  1519. pc.mu.Lock()
  1520. defer pc.mu.Unlock()
  1521. for _, transceiver := range pc.rtpTransceivers {
  1522. if sender := transceiver.Sender(); sender != nil {
  1523. result = append(result, sender)
  1524. }
  1525. }
  1526. return result
  1527. }
  1528. // GetReceivers returns the RTPReceivers that are currently attached to this PeerConnection
  1529. func (pc *PeerConnection) GetReceivers() (receivers []*RTPReceiver) {
  1530. pc.mu.Lock()
  1531. defer pc.mu.Unlock()
  1532. for _, transceiver := range pc.rtpTransceivers {
  1533. if receiver := transceiver.Receiver(); receiver != nil {
  1534. receivers = append(receivers, receiver)
  1535. }
  1536. }
  1537. return
  1538. }
  1539. // GetTransceivers returns the RtpTransceiver that are currently attached to this PeerConnection
  1540. func (pc *PeerConnection) GetTransceivers() []*RTPTransceiver {
  1541. pc.mu.Lock()
  1542. defer pc.mu.Unlock()
  1543. return pc.rtpTransceivers
  1544. }
  1545. // AddTrack adds a Track to the PeerConnection
  1546. func (pc *PeerConnection) AddTrack(track TrackLocal) (*RTPSender, error) {
  1547. if pc.isClosed.get() {
  1548. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1549. }
  1550. pc.mu.Lock()
  1551. defer pc.mu.Unlock()
  1552. for _, t := range pc.rtpTransceivers {
  1553. currentDirection := t.getCurrentDirection()
  1554. // According to https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-addtrack, if the
  1555. // transceiver can be reused only if it's currentDirection never be sendrecv or sendonly.
  1556. // But that will cause sdp inflate. So we only check currentDirection's current value,
  1557. // that's worked for all browsers.
  1558. if !t.stopped && t.kind == track.Kind() && t.Sender() == nil &&
  1559. !(currentDirection == RTPTransceiverDirectionSendrecv || currentDirection == RTPTransceiverDirectionSendonly) {
  1560. sender, err := pc.api.NewRTPSender(track, pc.dtlsTransport)
  1561. if err == nil {
  1562. err = t.SetSender(sender, track)
  1563. if err != nil {
  1564. _ = sender.Stop()
  1565. t.setSender(nil)
  1566. }
  1567. }
  1568. if err != nil {
  1569. return nil, err
  1570. }
  1571. pc.onNegotiationNeeded()
  1572. return sender, nil
  1573. }
  1574. }
  1575. transceiver, err := pc.newTransceiverFromTrack(RTPTransceiverDirectionSendrecv, track)
  1576. if err != nil {
  1577. return nil, err
  1578. }
  1579. pc.addRTPTransceiver(transceiver)
  1580. return transceiver.Sender(), nil
  1581. }
  1582. // RemoveTrack removes a Track from the PeerConnection
  1583. func (pc *PeerConnection) RemoveTrack(sender *RTPSender) (err error) {
  1584. if pc.isClosed.get() {
  1585. return &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1586. }
  1587. var transceiver *RTPTransceiver
  1588. pc.mu.Lock()
  1589. defer pc.mu.Unlock()
  1590. for _, t := range pc.rtpTransceivers {
  1591. if t.Sender() == sender {
  1592. transceiver = t
  1593. break
  1594. }
  1595. }
  1596. if transceiver == nil {
  1597. return &rtcerr.InvalidAccessError{Err: ErrSenderNotCreatedByConnection}
  1598. } else if err = sender.Stop(); err == nil {
  1599. err = transceiver.setSendingTrack(nil)
  1600. if err == nil {
  1601. pc.onNegotiationNeeded()
  1602. }
  1603. }
  1604. return
  1605. }
  1606. func (pc *PeerConnection) newTransceiverFromTrack(direction RTPTransceiverDirection, track TrackLocal) (t *RTPTransceiver, err error) {
  1607. var (
  1608. r *RTPReceiver
  1609. s *RTPSender
  1610. )
  1611. switch direction {
  1612. case RTPTransceiverDirectionSendrecv:
  1613. r, err = pc.api.NewRTPReceiver(track.Kind(), pc.dtlsTransport)
  1614. if err != nil {
  1615. return
  1616. }
  1617. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1618. case RTPTransceiverDirectionSendonly:
  1619. s, err = pc.api.NewRTPSender(track, pc.dtlsTransport)
  1620. default:
  1621. err = errPeerConnAddTransceiverFromTrackSupport
  1622. }
  1623. if err != nil {
  1624. return
  1625. }
  1626. return newRTPTransceiver(r, s, direction, track.Kind(), pc.api), nil
  1627. }
  1628. // AddTransceiverFromKind Create a new RtpTransceiver and adds it to the set of transceivers.
  1629. func (pc *PeerConnection) AddTransceiverFromKind(kind RTPCodecType, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1630. if pc.isClosed.get() {
  1631. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1632. }
  1633. direction := RTPTransceiverDirectionSendrecv
  1634. if len(init) > 1 {
  1635. return nil, errPeerConnAddTransceiverFromKindOnlyAcceptsOne
  1636. } else if len(init) == 1 {
  1637. direction = init[0].Direction
  1638. }
  1639. switch direction {
  1640. case RTPTransceiverDirectionSendonly, RTPTransceiverDirectionSendrecv:
  1641. codecs := pc.api.mediaEngine.getCodecsByKind(kind)
  1642. if len(codecs) == 0 {
  1643. return nil, ErrNoCodecsAvailable
  1644. }
  1645. track, err := NewTrackLocalStaticSample(codecs[0].RTPCodecCapability, util.MathRandAlpha(16), util.MathRandAlpha(16))
  1646. if err != nil {
  1647. return nil, err
  1648. }
  1649. t, err = pc.newTransceiverFromTrack(direction, track)
  1650. if err != nil {
  1651. return nil, err
  1652. }
  1653. case RTPTransceiverDirectionRecvonly:
  1654. receiver, err := pc.api.NewRTPReceiver(kind, pc.dtlsTransport)
  1655. if err != nil {
  1656. return nil, err
  1657. }
  1658. t = newRTPTransceiver(receiver, nil, RTPTransceiverDirectionRecvonly, kind, pc.api)
  1659. default:
  1660. return nil, errPeerConnAddTransceiverFromKindSupport
  1661. }
  1662. pc.mu.Lock()
  1663. pc.addRTPTransceiver(t)
  1664. pc.mu.Unlock()
  1665. return t, nil
  1666. }
  1667. // AddTransceiverFromTrack Create a new RtpTransceiver(SendRecv or SendOnly) and add it to the set of transceivers.
  1668. func (pc *PeerConnection) AddTransceiverFromTrack(track TrackLocal, init ...RTPTransceiverInit) (t *RTPTransceiver, err error) {
  1669. if pc.isClosed.get() {
  1670. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1671. }
  1672. direction := RTPTransceiverDirectionSendrecv
  1673. if len(init) > 1 {
  1674. return nil, errPeerConnAddTransceiverFromTrackOnlyAcceptsOne
  1675. } else if len(init) == 1 {
  1676. direction = init[0].Direction
  1677. }
  1678. t, err = pc.newTransceiverFromTrack(direction, track)
  1679. if err == nil {
  1680. pc.mu.Lock()
  1681. pc.addRTPTransceiver(t)
  1682. pc.mu.Unlock()
  1683. }
  1684. return
  1685. }
  1686. // CreateDataChannel creates a new DataChannel object with the given label
  1687. // and optional DataChannelInit used to configure properties of the
  1688. // underlying channel such as data reliability.
  1689. func (pc *PeerConnection) CreateDataChannel(label string, options *DataChannelInit) (*DataChannel, error) {
  1690. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #2)
  1691. if pc.isClosed.get() {
  1692. return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
  1693. }
  1694. params := &DataChannelParameters{
  1695. Label: label,
  1696. Ordered: true,
  1697. }
  1698. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
  1699. if options != nil {
  1700. params.ID = options.ID
  1701. }
  1702. if options != nil {
  1703. // Ordered indicates if data is allowed to be delivered out of order. The
  1704. // default value of true, guarantees that data will be delivered in order.
  1705. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
  1706. if options.Ordered != nil {
  1707. params.Ordered = *options.Ordered
  1708. }
  1709. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #7)
  1710. if options.MaxPacketLifeTime != nil {
  1711. params.MaxPacketLifeTime = options.MaxPacketLifeTime
  1712. }
  1713. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #8)
  1714. if options.MaxRetransmits != nil {
  1715. params.MaxRetransmits = options.MaxRetransmits
  1716. }
  1717. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #10)
  1718. if options.Protocol != nil {
  1719. params.Protocol = *options.Protocol
  1720. }
  1721. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #11)
  1722. if len(params.Protocol) > 65535 {
  1723. return nil, &rtcerr.TypeError{Err: ErrProtocolTooLarge}
  1724. }
  1725. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
  1726. if options.Negotiated != nil {
  1727. params.Negotiated = *options.Negotiated
  1728. }
  1729. }
  1730. d, err := pc.api.newDataChannel(params, nil, pc.log)
  1731. if err != nil {
  1732. return nil, err
  1733. }
  1734. // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #16)
  1735. if d.maxPacketLifeTime != nil && d.maxRetransmits != nil {
  1736. return nil, &rtcerr.TypeError{Err: ErrRetransmitsOrPacketLifeTime}
  1737. }
  1738. pc.sctpTransport.lock.Lock()
  1739. pc.sctpTransport.dataChannels = append(pc.sctpTransport.dataChannels, d)
  1740. pc.sctpTransport.dataChannelsRequested++
  1741. pc.sctpTransport.lock.Unlock()
  1742. // If SCTP already connected open all the channels
  1743. if pc.sctpTransport.State() == SCTPTransportStateConnected {
  1744. if err = d.open(pc.sctpTransport); err != nil {
  1745. return nil, err
  1746. }
  1747. }
  1748. pc.mu.Lock()
  1749. pc.onNegotiationNeeded()
  1750. pc.mu.Unlock()
  1751. return d, nil
  1752. }
  1753. // SetIdentityProvider is used to configure an identity provider to generate identity assertions
  1754. func (pc *PeerConnection) SetIdentityProvider(string) error {
  1755. return errPeerConnSetIdentityProviderNotImplemented
  1756. }
  1757. // WriteRTCP sends a user provided RTCP packet to the connected peer. If no peer is connected the
  1758. // packet is discarded. It also runs any configured interceptors.
  1759. func (pc *PeerConnection) WriteRTCP(pkts []rtcp.Packet) error {
  1760. _, err := pc.interceptorRTCPWriter.Write(pkts, make(interceptor.Attributes))
  1761. return err
  1762. }
  1763. func (pc *PeerConnection) writeRTCP(pkts []rtcp.Packet, _ interceptor.Attributes) (int, error) {
  1764. return pc.dtlsTransport.WriteRTCP(pkts)
  1765. }
  1766. // Close ends the PeerConnection
  1767. func (pc *PeerConnection) Close() error {
  1768. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #1)
  1769. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #2)
  1770. if pc.isClosed.swap(true) {
  1771. return nil
  1772. }
  1773. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #3)
  1774. pc.signalingState.Set(SignalingStateClosed)
  1775. // Try closing everything and collect the errors
  1776. // Shutdown strategy:
  1777. // 1. All Conn close by closing their underlying Conn.
  1778. // 2. A Mux stops this chain. It won't close the underlying
  1779. // Conn if one of the endpoints is closed down. To
  1780. // continue the chain the Mux has to be closed.
  1781. closeErrs := make([]error, 4)
  1782. closeErrs = append(closeErrs, pc.api.interceptor.Close())
  1783. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4)
  1784. pc.mu.Lock()
  1785. for _, t := range pc.rtpTransceivers {
  1786. if !t.stopped {
  1787. closeErrs = append(closeErrs, t.Stop())
  1788. }
  1789. }
  1790. pc.mu.Unlock()
  1791. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #5)
  1792. pc.sctpTransport.lock.Lock()
  1793. for _, d := range pc.sctpTransport.dataChannels {
  1794. d.setReadyState(DataChannelStateClosed)
  1795. }
  1796. pc.sctpTransport.lock.Unlock()
  1797. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #6)
  1798. if pc.sctpTransport != nil {
  1799. closeErrs = append(closeErrs, pc.sctpTransport.Stop())
  1800. }
  1801. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #7)
  1802. closeErrs = append(closeErrs, pc.dtlsTransport.Stop())
  1803. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #8, #9, #10)
  1804. if pc.iceTransport != nil {
  1805. closeErrs = append(closeErrs, pc.iceTransport.Stop())
  1806. }
  1807. // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #11)
  1808. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1809. return util.FlattenErrs(closeErrs)
  1810. }
  1811. // addRTPTransceiver appends t into rtpTransceivers
  1812. // and fires onNegotiationNeeded;
  1813. // caller of this method should hold `pc.mu` lock
  1814. func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
  1815. pc.rtpTransceivers = append(pc.rtpTransceivers, t)
  1816. pc.onNegotiationNeeded()
  1817. }
  1818. // CurrentLocalDescription represents the local description that was
  1819. // successfully negotiated the last time the PeerConnection transitioned
  1820. // into the stable state plus any local candidates that have been generated
  1821. // by the ICEAgent since the offer or answer was created.
  1822. func (pc *PeerConnection) CurrentLocalDescription() *SessionDescription {
  1823. pc.mu.Lock()
  1824. localDescription := pc.currentLocalDescription
  1825. iceGather := pc.iceGatherer
  1826. iceGatheringState := pc.ICEGatheringState()
  1827. pc.mu.Unlock()
  1828. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1829. }
  1830. // PendingLocalDescription represents a local description that is in the
  1831. // process of being negotiated plus any local candidates that have been
  1832. // generated by the ICEAgent since the offer or answer was created. If the
  1833. // PeerConnection is in the stable state, the value is null.
  1834. func (pc *PeerConnection) PendingLocalDescription() *SessionDescription {
  1835. pc.mu.Lock()
  1836. localDescription := pc.pendingLocalDescription
  1837. iceGather := pc.iceGatherer
  1838. iceGatheringState := pc.ICEGatheringState()
  1839. pc.mu.Unlock()
  1840. return populateLocalCandidates(localDescription, iceGather, iceGatheringState)
  1841. }
  1842. // CurrentRemoteDescription represents the last remote description that was
  1843. // successfully negotiated the last time the PeerConnection transitioned
  1844. // into the stable state plus any remote candidates that have been supplied
  1845. // via AddICECandidate() since the offer or answer was created.
  1846. func (pc *PeerConnection) CurrentRemoteDescription() *SessionDescription {
  1847. pc.mu.RLock()
  1848. defer pc.mu.RUnlock()
  1849. return pc.currentRemoteDescription
  1850. }
  1851. // PendingRemoteDescription represents a remote description that is in the
  1852. // process of being negotiated, complete with any remote candidates that
  1853. // have been supplied via AddICECandidate() since the offer or answer was
  1854. // created. If the PeerConnection is in the stable state, the value is
  1855. // null.
  1856. func (pc *PeerConnection) PendingRemoteDescription() *SessionDescription {
  1857. pc.mu.RLock()
  1858. defer pc.mu.RUnlock()
  1859. return pc.pendingRemoteDescription
  1860. }
  1861. // SignalingState attribute returns the signaling state of the
  1862. // PeerConnection instance.
  1863. func (pc *PeerConnection) SignalingState() SignalingState {
  1864. return pc.signalingState.Get()
  1865. }
  1866. // ICEGatheringState attribute returns the ICE gathering state of the
  1867. // PeerConnection instance.
  1868. func (pc *PeerConnection) ICEGatheringState() ICEGatheringState {
  1869. if pc.iceGatherer == nil {
  1870. return ICEGatheringStateNew
  1871. }
  1872. switch pc.iceGatherer.State() {
  1873. case ICEGathererStateNew:
  1874. return ICEGatheringStateNew
  1875. case ICEGathererStateGathering:
  1876. return ICEGatheringStateGathering
  1877. default:
  1878. return ICEGatheringStateComplete
  1879. }
  1880. }
  1881. // ConnectionState attribute returns the connection state of the
  1882. // PeerConnection instance.
  1883. func (pc *PeerConnection) ConnectionState() PeerConnectionState {
  1884. if state, ok := pc.connectionState.Load().(PeerConnectionState); ok {
  1885. return state
  1886. }
  1887. return PeerConnectionState(0)
  1888. }
  1889. // GetStats return data providing statistics about the overall connection
  1890. func (pc *PeerConnection) GetStats() StatsReport {
  1891. var (
  1892. dataChannelsAccepted uint32
  1893. dataChannelsClosed uint32
  1894. dataChannelsOpened uint32
  1895. dataChannelsRequested uint32
  1896. )
  1897. statsCollector := newStatsReportCollector()
  1898. statsCollector.Collecting()
  1899. pc.mu.Lock()
  1900. if pc.iceGatherer != nil {
  1901. pc.iceGatherer.collectStats(statsCollector)
  1902. }
  1903. if pc.iceTransport != nil {
  1904. pc.iceTransport.collectStats(statsCollector)
  1905. }
  1906. pc.sctpTransport.lock.Lock()
  1907. dataChannels := append([]*DataChannel{}, pc.sctpTransport.dataChannels...)
  1908. dataChannelsAccepted = pc.sctpTransport.dataChannelsAccepted
  1909. dataChannelsOpened = pc.sctpTransport.dataChannelsOpened
  1910. dataChannelsRequested = pc.sctpTransport.dataChannelsRequested
  1911. pc.sctpTransport.lock.Unlock()
  1912. for _, d := range dataChannels {
  1913. state := d.ReadyState()
  1914. if state != DataChannelStateConnecting && state != DataChannelStateOpen {
  1915. dataChannelsClosed++
  1916. }
  1917. d.collectStats(statsCollector)
  1918. }
  1919. pc.sctpTransport.collectStats(statsCollector)
  1920. stats := PeerConnectionStats{
  1921. Timestamp: statsTimestampNow(),
  1922. Type: StatsTypePeerConnection,
  1923. ID: pc.statsID,
  1924. DataChannelsAccepted: dataChannelsAccepted,
  1925. DataChannelsClosed: dataChannelsClosed,
  1926. DataChannelsOpened: dataChannelsOpened,
  1927. DataChannelsRequested: dataChannelsRequested,
  1928. }
  1929. statsCollector.Collect(stats.ID, stats)
  1930. certificates := pc.configuration.Certificates
  1931. for _, certificate := range certificates {
  1932. if err := certificate.collectStats(statsCollector); err != nil {
  1933. continue
  1934. }
  1935. }
  1936. pc.mu.Unlock()
  1937. pc.api.mediaEngine.collectStats(statsCollector)
  1938. return statsCollector.Ready()
  1939. }
  1940. // Start all transports. PeerConnection now has enough state
  1941. func (pc *PeerConnection) startTransports(iceRole ICERole, dtlsRole DTLSRole, remoteUfrag, remotePwd, fingerprint, fingerprintHash string) {
  1942. // Start the ice transport
  1943. err := pc.iceTransport.Start(
  1944. pc.iceGatherer,
  1945. ICEParameters{
  1946. UsernameFragment: remoteUfrag,
  1947. Password: remotePwd,
  1948. ICELite: false,
  1949. },
  1950. &iceRole,
  1951. )
  1952. if err != nil {
  1953. pc.log.Warnf("Failed to start manager: %s", err)
  1954. return
  1955. }
  1956. // Start the dtls transport
  1957. err = pc.dtlsTransport.Start(DTLSParameters{
  1958. Role: dtlsRole,
  1959. Fingerprints: []DTLSFingerprint{{Algorithm: fingerprintHash, Value: fingerprint}},
  1960. })
  1961. pc.updateConnectionState(pc.ICEConnectionState(), pc.dtlsTransport.State())
  1962. if err != nil {
  1963. pc.log.Warnf("Failed to start manager: %s", err)
  1964. return
  1965. }
  1966. }
  1967. // nolint: gocognit
  1968. func (pc *PeerConnection) startRTP(isRenegotiation bool, remoteDesc *SessionDescription, currentTransceivers []*RTPTransceiver) {
  1969. if !isRenegotiation {
  1970. pc.undeclaredMediaProcessor()
  1971. }
  1972. pc.startRTPReceivers(remoteDesc, currentTransceivers)
  1973. if haveApplicationMediaSection(remoteDesc.parsed) {
  1974. pc.startSCTP()
  1975. }
  1976. }
  1977. // generateUnmatchedSDP generates an SDP that doesn't take remote state into account
  1978. // This is used for the initial call for CreateOffer
  1979. func (pc *PeerConnection) generateUnmatchedSDP(transceivers []*RTPTransceiver, useIdentity bool) (*sdp.SessionDescription, error) {
  1980. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  1981. if err != nil {
  1982. return nil, err
  1983. }
  1984. iceParams, err := pc.iceGatherer.GetLocalParameters()
  1985. if err != nil {
  1986. return nil, err
  1987. }
  1988. candidates, err := pc.iceGatherer.GetLocalCandidates()
  1989. if err != nil {
  1990. return nil, err
  1991. }
  1992. isPlanB := pc.configuration.SDPSemantics == SDPSemanticsPlanB
  1993. mediaSections := []mediaSection{}
  1994. // Needed for pc.sctpTransport.dataChannelsRequested
  1995. pc.sctpTransport.lock.Lock()
  1996. defer pc.sctpTransport.lock.Unlock()
  1997. if isPlanB {
  1998. video := make([]*RTPTransceiver, 0)
  1999. audio := make([]*RTPTransceiver, 0)
  2000. for _, t := range transceivers {
  2001. if t.kind == RTPCodecTypeVideo {
  2002. video = append(video, t)
  2003. } else if t.kind == RTPCodecTypeAudio {
  2004. audio = append(audio, t)
  2005. }
  2006. if sender := t.Sender(); sender != nil {
  2007. sender.setNegotiated()
  2008. }
  2009. }
  2010. if len(video) > 0 {
  2011. mediaSections = append(mediaSections, mediaSection{id: "video", transceivers: video})
  2012. }
  2013. if len(audio) > 0 {
  2014. mediaSections = append(mediaSections, mediaSection{id: "audio", transceivers: audio})
  2015. }
  2016. if pc.sctpTransport.dataChannelsRequested != 0 {
  2017. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2018. }
  2019. } else {
  2020. for _, t := range transceivers {
  2021. if sender := t.Sender(); sender != nil {
  2022. sender.setNegotiated()
  2023. }
  2024. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2025. }
  2026. if pc.sctpTransport.dataChannelsRequested != 0 {
  2027. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2028. }
  2029. }
  2030. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2031. if err != nil {
  2032. return nil, err
  2033. }
  2034. 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())
  2035. }
  2036. // generateMatchedSDP generates a SDP and takes the remote state into account
  2037. // this is used everytime we have a RemoteDescription
  2038. // nolint: gocyclo
  2039. func (pc *PeerConnection) generateMatchedSDP(transceivers []*RTPTransceiver, useIdentity bool, includeUnmatched bool, connectionRole sdp.ConnectionRole) (*sdp.SessionDescription, error) { //nolint:gocognit
  2040. d, err := sdp.NewJSEPSessionDescription(useIdentity)
  2041. if err != nil {
  2042. return nil, err
  2043. }
  2044. iceParams, err := pc.iceGatherer.GetLocalParameters()
  2045. if err != nil {
  2046. return nil, err
  2047. }
  2048. candidates, err := pc.iceGatherer.GetLocalCandidates()
  2049. if err != nil {
  2050. return nil, err
  2051. }
  2052. var t *RTPTransceiver
  2053. remoteDescription := pc.currentRemoteDescription
  2054. if pc.pendingRemoteDescription != nil {
  2055. remoteDescription = pc.pendingRemoteDescription
  2056. }
  2057. isExtmapAllowMixed := isExtMapAllowMixedSet(remoteDescription.parsed)
  2058. localTransceivers := append([]*RTPTransceiver{}, transceivers...)
  2059. detectedPlanB := descriptionIsPlanB(remoteDescription, pc.log)
  2060. if pc.configuration.SDPSemantics != SDPSemanticsUnifiedPlan {
  2061. detectedPlanB = descriptionPossiblyPlanB(remoteDescription)
  2062. }
  2063. mediaSections := []mediaSection{}
  2064. alreadyHaveApplicationMediaSection := false
  2065. for _, media := range remoteDescription.parsed.MediaDescriptions {
  2066. midValue := getMidValue(media)
  2067. if midValue == "" {
  2068. return nil, errPeerConnRemoteDescriptionWithoutMidValue
  2069. }
  2070. if media.MediaName.Media == mediaSectionApplication {
  2071. mediaSections = append(mediaSections, mediaSection{id: midValue, data: true})
  2072. alreadyHaveApplicationMediaSection = true
  2073. continue
  2074. }
  2075. kind := NewRTPCodecType(media.MediaName.Media)
  2076. direction := getPeerDirection(media)
  2077. if kind == 0 || direction == RTPTransceiverDirection(Unknown) {
  2078. continue
  2079. }
  2080. sdpSemantics := pc.configuration.SDPSemantics
  2081. switch {
  2082. case sdpSemantics == SDPSemanticsPlanB || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB:
  2083. if !detectedPlanB {
  2084. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected PlanB, but RemoteDescription is UnifiedPlan", ErrIncorrectSDPSemantics)}
  2085. }
  2086. // If we're responding to a plan-b offer, then we should try to fill up this
  2087. // media entry with all matching local transceivers
  2088. mediaTransceivers := []*RTPTransceiver{}
  2089. for {
  2090. // keep going until we can't get any more
  2091. t, localTransceivers = satisfyTypeAndDirection(kind, direction, localTransceivers)
  2092. if t == nil {
  2093. if len(mediaTransceivers) == 0 {
  2094. t = &RTPTransceiver{kind: kind, api: pc.api, codecs: pc.api.mediaEngine.getCodecsByKind(kind)}
  2095. t.setDirection(RTPTransceiverDirectionInactive)
  2096. mediaTransceivers = append(mediaTransceivers, t)
  2097. }
  2098. break
  2099. }
  2100. if sender := t.Sender(); sender != nil {
  2101. sender.setNegotiated()
  2102. }
  2103. mediaTransceivers = append(mediaTransceivers, t)
  2104. }
  2105. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers})
  2106. case sdpSemantics == SDPSemanticsUnifiedPlan || sdpSemantics == SDPSemanticsUnifiedPlanWithFallback:
  2107. if detectedPlanB {
  2108. return nil, &rtcerr.TypeError{Err: fmt.Errorf("%w: Expected UnifiedPlan, but RemoteDescription is PlanB", ErrIncorrectSDPSemantics)}
  2109. }
  2110. t, localTransceivers = findByMid(midValue, localTransceivers)
  2111. if t == nil {
  2112. return nil, fmt.Errorf("%w: %q", errPeerConnTranscieverMidNil, midValue)
  2113. }
  2114. if sender := t.Sender(); sender != nil {
  2115. sender.setNegotiated()
  2116. }
  2117. mediaTransceivers := []*RTPTransceiver{t}
  2118. mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, ridMap: getRids(media)})
  2119. }
  2120. }
  2121. // If we are offering also include unmatched local transceivers
  2122. if includeUnmatched {
  2123. if !detectedPlanB {
  2124. for _, t := range localTransceivers {
  2125. if sender := t.Sender(); sender != nil {
  2126. sender.setNegotiated()
  2127. }
  2128. mediaSections = append(mediaSections, mediaSection{id: t.Mid(), transceivers: []*RTPTransceiver{t}})
  2129. }
  2130. }
  2131. if pc.sctpTransport.dataChannelsRequested != 0 && !alreadyHaveApplicationMediaSection {
  2132. if detectedPlanB {
  2133. mediaSections = append(mediaSections, mediaSection{id: "data", data: true})
  2134. } else {
  2135. mediaSections = append(mediaSections, mediaSection{id: strconv.Itoa(len(mediaSections)), data: true})
  2136. }
  2137. }
  2138. }
  2139. if pc.configuration.SDPSemantics == SDPSemanticsUnifiedPlanWithFallback && detectedPlanB {
  2140. pc.log.Info("Plan-B Offer detected; responding with Plan-B Answer")
  2141. }
  2142. dtlsFingerprints, err := pc.configuration.Certificates[0].GetFingerprints()
  2143. if err != nil {
  2144. return nil, err
  2145. }
  2146. return populateSDP(d, detectedPlanB, dtlsFingerprints, pc.api.settingEngine.sdpMediaLevelFingerprints, pc.api.settingEngine.candidates.ICELite, isExtmapAllowMixed, pc.api.mediaEngine, connectionRole, candidates, iceParams, mediaSections, pc.ICEGatheringState())
  2147. }
  2148. func (pc *PeerConnection) setGatherCompleteHandler(handler func()) {
  2149. pc.iceGatherer.onGatheringCompleteHandler.Store(handler)
  2150. }
  2151. // SCTP returns the SCTPTransport for this PeerConnection
  2152. //
  2153. // The SCTP transport over which SCTP data is sent and received. If SCTP has not been negotiated, the value is nil.
  2154. // https://www.w3.org/TR/webrtc/#attributes-15
  2155. func (pc *PeerConnection) SCTP() *SCTPTransport {
  2156. return pc.sctpTransport
  2157. }