Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RTCPeerConnection.js 66KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. /* global __filename, RTCIceGatherer, RTCIceTransport, RTCDtlsTransport,
  2. RTCRtpSender, RTCRtpReceiver */
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import yaeti from 'yaeti';
  5. import RTCSessionDescription from './RTCSessionDescription';
  6. import * as utils from './utils';
  7. import { InvalidStateError } from './errors';
  8. import RandomUtil from '../../util/RandomUtil';
  9. import SDPUtil from '../../xmpp/SDPUtil';
  10. const logger = getLogger(__filename);
  11. const RTCSignalingState = {
  12. stable: 'stable',
  13. haveLocalOffer: 'have-local-offer',
  14. haveRemoteOffer: 'have-remote-offer',
  15. closed: 'closed'
  16. };
  17. const RTCIceGatheringState = {
  18. new: 'new',
  19. gathering: 'gathering',
  20. complete: 'complete'
  21. };
  22. const CNAME = `jitsi-ortc-cname-${RandomUtil.randomInt(10000, 99999)}`;
  23. /**
  24. * RTCPeerConnection shim for ORTC based endpoints (such as Edge).
  25. *
  26. * The interface is based on the W3C specification of 2015, which matches
  27. * the implementation of Chrome nowadays:
  28. *
  29. * https://www.w3.org/TR/2015/WD-webrtc-20150210/
  30. *
  31. * It also implements Plan-B for multi-stream, and assumes single BUNDLEd
  32. * transport and rtcp-mux.
  33. */
  34. export default class ortcRTCPeerConnection extends yaeti.EventTarget {
  35. /**
  36. */
  37. constructor(pcConfig) {
  38. super();
  39. logger.debug('constructor() pcConfig:', pcConfig);
  40. // Buffered local ICE candidates (in WebRTC format).
  41. // @type {sequence<RTCIceCandidate>}
  42. this._bufferedIceCandidates = [];
  43. // Closed flag.
  44. // @type {Boolean}
  45. this._closed = false;
  46. // RTCDtlsTransport.
  47. // @type {RTCDtlsTransport}
  48. this._dtlsTransport = null;
  49. // RTCIceGatherer.
  50. // @type {RTCIceGatherer}
  51. this._iceGatherer = null;
  52. // RTCPeerConnection iceGatheringState.
  53. // NOTE: This should not be needed, but Edge does not implement
  54. // iceGatherer.state.
  55. // @type {RTCIceGatheringState}
  56. this._iceGatheringState = RTCIceGatheringState.new;
  57. // Use a promise to signify when the local client has finished gathering
  58. // ice candidates. This is needed to prevent errors on offer creation.
  59. this._resolveLocalIceGatheringPromise = null;
  60. this._iceGatheringCompletePromise = new Promise(resolve => {
  61. this._resolveLocalIceGatheringPromise = resolve;
  62. });
  63. // RTCIceTransport.
  64. // @type {RTCIceTransport}
  65. this._iceTransport = null;
  66. // Local RTP capabilities (filtered with remote ones).
  67. // @type {RTCRtpCapabilities}
  68. this._localCapabilities = null;
  69. // Local RTCSessionDescription.
  70. // @type {RTCSessionDescription}
  71. this._localDescription = null;
  72. // Map with info regarding local media.
  73. // - index: MediaStreamTrack.id
  74. // - value: Object
  75. // - rtpSender: Associated RTCRtpSender instance
  76. // - stream: Associated MediaStream instance
  77. // - ssrc: Provisional or definitive SSRC
  78. // - rtxSsrc: Provisional or definitive SSRC for RTX
  79. // - sending: Boolean indicating whether rtpSender.send() was called.
  80. this._localTrackInfos = new Map();
  81. // Ordered Map with MID as key and kind as value.
  82. // @type {map<String, String>}
  83. this._mids = new Map();
  84. // Remote RTCSessionDescription.
  85. // @type {RTCSessionDescription}
  86. this._remoteDescription = null;
  87. // Map of remote streams.
  88. // - index: MediaStream.jitsiRemoteId (as signaled in remote SDP)
  89. // - value: MediaStream (locally generated so id does not match)
  90. // @type {map<Number, MediaStream>}
  91. this._remoteStreams = new Map();
  92. // Map with info about receiving media.
  93. // - index: Media SSRC
  94. // - value: Object
  95. // - kind: 'audio' / 'video'
  96. // - ssrc: Media SSRC
  97. // - rtxSsrc: RTX SSRC (may be unset)
  98. // - streamId: MediaStream.jitsiRemoteId
  99. // - trackId: MediaStreamTrack.jitsiRemoteId
  100. // - cname: CNAME
  101. // - stream: MediaStream
  102. // - track: MediaStreamTrack
  103. // - rtpReceiver: Associated RTCRtpReceiver instance
  104. // @type {map<Number, Object>}
  105. this._remoteTrackInfos = new Map();
  106. // Local SDP global fields.
  107. this._sdpGlobalFields = {
  108. id: SDPUtil.generateSsrc(),
  109. version: 0
  110. };
  111. // RTCPeerConnection signalingState.
  112. // @type {RTCSignalingState}
  113. this._signalingState = RTCSignalingState.stable;
  114. // Create the RTCIceGatherer.
  115. this._setIceGatherer(pcConfig);
  116. // Create the RTCIceTransport.
  117. this._setIceTransport(this._iceGatherer);
  118. // Create the RTCDtlsTransport.
  119. this._setDtlsTransport(this._iceTransport);
  120. }
  121. /**
  122. * Current ICE+DTLS connection state.
  123. * @return {RTCPeerConnectionState}
  124. */
  125. get connectionState() {
  126. return this._dtlsTransport.state;
  127. }
  128. /**
  129. * Current ICE connection state.
  130. * @return {RTCIceConnectionState}
  131. */
  132. get iceConnectionState() {
  133. return this._iceTransport.state;
  134. }
  135. /**
  136. * Current ICE gathering state.
  137. * @return {RTCIceGatheringState}
  138. */
  139. get iceGatheringState() {
  140. return this._iceGatheringState;
  141. }
  142. /**
  143. * Gets the local description.
  144. * @return {RTCSessionDescription}
  145. */
  146. get localDescription() {
  147. return this._localDescription;
  148. }
  149. /**
  150. * Gets the remote description.
  151. * @return {RTCSessionDescription}
  152. */
  153. get remoteDescription() {
  154. return this._remoteDescription;
  155. }
  156. /**
  157. * Current signaling state.
  158. * @return {RTCSignalingState}
  159. */
  160. get signalingState() {
  161. return this._signalingState;
  162. }
  163. /**
  164. * Adds a remote ICE candidate. Implements both the old callbacks based
  165. * signature and the new Promise based style.
  166. *
  167. * Arguments in Promise mode:
  168. * @param {RTCIceCandidate} candidate
  169. *
  170. * Arguments in callbacks mode:
  171. * @param {RTCIceCandidate} candidate
  172. */
  173. addIceCandidate(candidate) { // eslint-disable-line no-unused-vars
  174. if (!candidate) {
  175. throw new TypeError('candidate missing');
  176. }
  177. logger.debug('addIceCandidate() candidate:', candidate);
  178. if (this._closed) {
  179. return Promise.reject(
  180. new InvalidStateError('RTCPeerConnection closed'));
  181. }
  182. // NOTE: Edge does not support Trickle-ICE so just candidates in the
  183. // remote SDP are applied. Candidates given later would be just
  184. // ignored, so notify the called about that.
  185. return Promise.reject(new Error('addIceCandidate() not supported'));
  186. }
  187. /**
  188. * Adds a local MediaStream.
  189. * @param {MediaStream} stream.
  190. * NOTE: Deprecated API.
  191. */
  192. addStream(stream) {
  193. logger.debug('addStream()');
  194. if (this._closed) {
  195. throw new InvalidStateError('RTCPeerConnection closed');
  196. }
  197. // Create a RTCRtpSender for each track.
  198. for (const track of stream.getTracks()) {
  199. // Ignore if ended.
  200. if (track.readyState === 'ended') {
  201. logger.warn('ignoring ended MediaStreamTrack');
  202. continue; // eslint-disable-line no-continue
  203. }
  204. // Ignore if track is already present.
  205. if (this._localTrackInfos.has(track.id)) {
  206. logger.warn('ignoring already handled MediaStreamTrack');
  207. continue; // eslint-disable-line no-continue
  208. }
  209. const rtpSender = new RTCRtpSender(track, this._dtlsTransport);
  210. // Store it in the map.
  211. this._localTrackInfos.set(track.id, {
  212. rtpSender,
  213. stream
  214. });
  215. }
  216. // Check for local tracks removal.
  217. for (const [ trackId, info ] of this._localTrackInfos) {
  218. const track = info.rtpSender.track;
  219. // Check if any of the local tracks has been stopped.
  220. if (track.readyState === 'ended') {
  221. logger.warn(
  222. 'addStream() an already handled track was stopped, '
  223. + `track.id:${track.id}`);
  224. try {
  225. info.rtpSender.stop();
  226. } catch (error) {
  227. logger.warn(`rtpSender.stop() failed:${error}`);
  228. }
  229. // Remove from the map.
  230. this._localTrackInfos.delete(track.id);
  231. // Also, if the stream was already handled, check whether tracks
  232. // have been removed via stream.removeTrack() and, if so, stop
  233. // their RtpSenders.
  234. } else if (info.stream === stream
  235. && !stream.getTrackById(trackId)) {
  236. logger.warn(
  237. 'addStream() a track in this stream was removed, '
  238. + `track.id:${trackId}`);
  239. try {
  240. info.rtpSender.stop();
  241. } catch (error) {
  242. logger.warn(`rtpSender.stop() failed:${error}`);
  243. }
  244. // Remove from the map.
  245. this._localTrackInfos.delete(track.id);
  246. }
  247. }
  248. // It may need to renegotiate.
  249. this._emitNegotiationNeeded();
  250. }
  251. /**
  252. * Closes the RTCPeerConnection and all the underlying ORTC objects.
  253. */
  254. close() {
  255. if (this._closed) {
  256. return;
  257. }
  258. this._closed = true;
  259. logger.debug('close()');
  260. this._updateAndEmitSignalingStateChange(RTCSignalingState.closed);
  261. // Close RTCIceGatherer.
  262. // NOTE: Not yet implemented by Edge.
  263. try {
  264. this._iceGatherer.close();
  265. } catch (error) {
  266. logger.warn(`iceGatherer.close() failed:${error}`);
  267. }
  268. // Close RTCIceTransport.
  269. try {
  270. this._iceTransport.stop();
  271. } catch (error) {
  272. logger.warn(`iceTransport.stop() failed:${error}`);
  273. }
  274. // Close RTCDtlsTransport.
  275. try {
  276. this._dtlsTransport.stop();
  277. } catch (error) {
  278. logger.warn(`dtlsTransport.stop() failed:${error}`);
  279. }
  280. // Close and clear RTCRtpSenders.
  281. for (const info of this._localTrackInfos.values()) {
  282. const rtpSender = info.rtpSender;
  283. try {
  284. rtpSender.stop();
  285. } catch (error) {
  286. logger.warn(`rtpSender.stop() failed:${error}`);
  287. }
  288. }
  289. this._localTrackInfos.clear();
  290. // Close and clear RTCRtpReceivers.
  291. for (const info of this._remoteTrackInfos.values()) {
  292. const rtpReceiver = info.rtpReceiver;
  293. try {
  294. rtpReceiver.stop();
  295. } catch (error) {
  296. logger.warn(`rtpReceiver.stop() failed:${error}`);
  297. }
  298. }
  299. this._remoteTrackInfos.clear();
  300. // Clear remote streams.
  301. this._remoteStreams.clear();
  302. }
  303. /**
  304. * Creates a local answer.
  305. *
  306. * @param {RTCOfferOptions} [options]
  307. */
  308. createAnswer(options) {
  309. logger.debug('createAnswer() options:', options);
  310. if (this._closed) {
  311. return Promise.reject(
  312. new InvalidStateError('RTCPeerConnection closed'));
  313. }
  314. if (this.signalingState !== RTCSignalingState.haveRemoteOffer) {
  315. return Promise.reject(new InvalidStateError(
  316. `invalid signalingState "${this.signalingState}"`));
  317. }
  318. // Create an answer.
  319. const localDescription = this._createLocalDescription('answer');
  320. // Resolve with it.
  321. return Promise.resolve(localDescription);
  322. }
  323. /**
  324. * Creates a RTCDataChannel.
  325. */
  326. createDataChannel() {
  327. logger.debug('createDataChannel()');
  328. // NOTE: DataChannels not implemented in Edge.
  329. throw new Error('createDataChannel() not supported in Edge');
  330. }
  331. /**
  332. * Creates a local offer.
  333. *
  334. * @param {RTCOfferOptions} [options]
  335. */
  336. createOffer(options) {
  337. logger.debug('createOffer() options:', options);
  338. if (this._closed) {
  339. return Promise.reject(
  340. new InvalidStateError('RTCPeerConnection closed'));
  341. }
  342. if (this.signalingState !== RTCSignalingState.stable) {
  343. return Promise.reject(new InvalidStateError(
  344. `invalid signalingState "${this.signalingState}"`));
  345. }
  346. // Use ice gathering complete promise an invalid state error occurs if
  347. // creating a local description before it is complete.
  348. return this._iceGatheringCompletePromise
  349. .then(() => {
  350. // Stub out _mids and _localCapabilities if creating an offer
  351. // as they are normally set on answer and will cause errors in
  352. // create a local description if falsy.
  353. if (!this.hasAttemptedOffer) {
  354. if (!this._mids.size) {
  355. this._mids = new Map([
  356. [ 'audio', 'audio' ],
  357. [ 'video', 'video' ]
  358. ]);
  359. }
  360. if (!this._localCapabilities) {
  361. this._localCapabilities
  362. = utils.getLocalCapabilities(
  363. RTCRtpSender.getCapabilities());
  364. }
  365. this.hasAttemptedOffer = true;
  366. }
  367. return this._createLocalDescription('offer');
  368. });
  369. }
  370. /**
  371. * Gets a sequence of local MediaStreams.
  372. * @return {sequence<MediaStream>}
  373. */
  374. getLocalStreams() {
  375. return Array.from(this._localTrackInfos.values())
  376. .map(info => info.stream)
  377. .filter((elem, pos, arr) => arr.indexOf(elem) === pos);
  378. }
  379. /**
  380. * Gets a sequence of remote MediaStreams.
  381. * @return {sequence<MediaStream>}
  382. */
  383. getRemoteStreams() {
  384. return Array.from(this._remoteStreams.values());
  385. }
  386. /**
  387. * Get RTP statistics. Implements both the old callbacks based signature
  388. * and the new Promise based style.
  389. *
  390. * Arguments in Promise mode:
  391. * @param {MediaStreamTrack} [selector]
  392. *
  393. * Arguments in callbacks mode:
  394. * @param {MediaStreamTrack} [selector]
  395. * @param {function(desc)} callback
  396. * @param {function(error)} errback
  397. */
  398. getStats(...args) {
  399. let usePromise;
  400. let selector;
  401. let callback;
  402. let errback;
  403. if (typeof args[0] === 'function') {
  404. usePromise = false;
  405. callback = args[0];
  406. errback = args[1];
  407. } else if (typeof args[1] === 'function') {
  408. usePromise = false;
  409. selector = args[0];
  410. callback = args[1];
  411. errback = args[2];
  412. } else {
  413. usePromise = true;
  414. selector = args[0];
  415. }
  416. if (!usePromise && !errback) {
  417. errback = error => {
  418. logger.error(`getStats() failed: ${error}`);
  419. logger.error(error.stack);
  420. };
  421. }
  422. if (usePromise) {
  423. return this._getStats(selector);
  424. }
  425. this._getStats(selector)
  426. .then(stats => callback(stats))
  427. .catch(error => errback(error));
  428. }
  429. /**
  430. * Removes a local MediaStream.
  431. * @param {MediaStream} stream.
  432. * NOTE: Deprecated API.
  433. */
  434. removeStream(stream) {
  435. logger.debug('removeStream()');
  436. if (this._closed) {
  437. throw new InvalidStateError('RTCPeerConnection closed');
  438. }
  439. // Stop and remove the RTCRtpSender associated to each track.
  440. for (const track of stream.getTracks()) {
  441. // Ignore if track not present.
  442. if (!this._localTrackInfos.has(track.id)) {
  443. continue; // eslint-disable-line no-continue
  444. }
  445. const rtpSender = this._localTrackInfos.get(track.id).rtpSender;
  446. try {
  447. rtpSender.stop();
  448. } catch (error) {
  449. logger.warn(`rtpSender.stop() failed:${error}`);
  450. }
  451. // Remove from the map.
  452. this._localTrackInfos.delete(track.id);
  453. }
  454. // It may need to renegotiate.
  455. this._emitNegotiationNeeded();
  456. }
  457. /**
  458. * Applies a local description.
  459. *
  460. * Arguments in Promise mode:
  461. * @param {RTCSessionDescriptionInit} desc
  462. */
  463. setLocalDescription(desc) {
  464. if (!desc) {
  465. throw new TypeError('description missing');
  466. }
  467. logger.debug('setLocalDescription() desc:', desc);
  468. return this._setLocalDescription(desc);
  469. }
  470. /**
  471. * Applies a remote description.
  472. *
  473. * Arguments in Promise mode:
  474. * @param {RTCSessionDescriptionInit} desc
  475. */
  476. setRemoteDescription(desc) {
  477. if (!desc) {
  478. throw new TypeError('description missing');
  479. }
  480. logger.debug('setRemoteDescription() desc:', desc);
  481. return this._setRemoteDescription(desc);
  482. }
  483. /**
  484. * Creates the local RTCSessionDescription.
  485. * @param {String} type - 'offer' / 'answer'.
  486. * @return {RTCSessionDescription}
  487. */
  488. _createLocalDescription(type) {
  489. const sdpObject = {};
  490. const localIceParameters = this._iceGatherer.getLocalParameters();
  491. const localIceCandidates = this._iceGatherer.getLocalCandidates();
  492. const localDtlsParameters = this._dtlsTransport.getLocalParameters();
  493. // remoteDtlsParameters are not needed for offers.
  494. const remoteDtlsParameters = type === 'answer'
  495. ? this._dtlsTransport.getRemoteParameters() : null;
  496. const localCapabilities = this._localCapabilities;
  497. const localTrackInfos = this._localTrackInfos;
  498. // Increase SDP version if an offer.
  499. if (type === 'offer') {
  500. this._sdpGlobalFields.version++;
  501. }
  502. // SDP global fields.
  503. sdpObject.version = 0;
  504. sdpObject.origin = {
  505. address: '127.0.0.1',
  506. ipVer: 4,
  507. netType: 'IN',
  508. sessionId: this._sdpGlobalFields.id,
  509. sessionVersion: this._sdpGlobalFields.version,
  510. username: 'jitsi-ortc-webrtc-shim'
  511. };
  512. sdpObject.name = '-';
  513. sdpObject.timing = {
  514. start: 0,
  515. stop: 0
  516. };
  517. sdpObject.msidSemantic = {
  518. semantic: 'WMS',
  519. token: '*'
  520. };
  521. sdpObject.groups = [
  522. {
  523. mids: Array.from(this._mids.keys()).join(' '),
  524. type: 'BUNDLE'
  525. }
  526. ];
  527. sdpObject.media = [];
  528. // DTLS fingerprint.
  529. sdpObject.fingerprint = {
  530. hash: localDtlsParameters.fingerprints[0].value,
  531. type: localDtlsParameters.fingerprints[0].algorithm
  532. };
  533. // Let's check whether there is video RTX.
  534. let hasVideoRtx = false;
  535. for (const codec of localCapabilities.codecs) {
  536. if (codec.kind === 'video' && codec.name === 'rtx') {
  537. hasVideoRtx = true;
  538. break;
  539. }
  540. }
  541. // Add m= sections.
  542. for (const [ mid, kind ] of this._mids) {
  543. addMediaSection.call(this, mid, kind);
  544. }
  545. // Create a RTCSessionDescription.
  546. const localDescription = new RTCSessionDescription({
  547. type,
  548. _sdpObject: sdpObject
  549. });
  550. logger.debug('_createLocalDescription():', localDescription);
  551. return localDescription;
  552. /**
  553. * Add a m= section.
  554. */
  555. function addMediaSection(mid, kind) {
  556. const mediaObject = {};
  557. // m= line.
  558. mediaObject.type = kind;
  559. switch (kind) {
  560. case 'audio':
  561. case 'video':
  562. mediaObject.protocol = 'RTP/SAVPF';
  563. mediaObject.port = 9;
  564. mediaObject.direction = 'sendrecv';
  565. break;
  566. case 'application':
  567. mediaObject.protocol = 'DTLS/SCTP';
  568. mediaObject.port = 0; // Reject m section.
  569. mediaObject.payloads = '0'; // Just put something.
  570. mediaObject.direction = 'inactive';
  571. break;
  572. }
  573. // c= line.
  574. mediaObject.connection = {
  575. ip: '127.0.0.1',
  576. version: 4
  577. };
  578. // a=mid attribute.
  579. mediaObject.mid = mid;
  580. // ICE.
  581. mediaObject.iceUfrag = localIceParameters.usernameFragment;
  582. mediaObject.icePwd = localIceParameters.password;
  583. mediaObject.candidates = [];
  584. for (const candidate of localIceCandidates) {
  585. const candidateObject = {};
  586. // rtcp-mux is assumed, so component is always 1 (RTP).
  587. candidateObject.component = 1;
  588. candidateObject.foundation = candidate.foundation;
  589. candidateObject.ip = candidate.ip;
  590. candidateObject.port = candidate.port;
  591. candidateObject.priority = candidate.priority;
  592. candidateObject.transport
  593. = candidate.protocol.toLowerCase();
  594. candidateObject.type = candidate.type;
  595. if (candidateObject.transport === 'tcp') {
  596. candidateObject.tcptype = candidate.tcpType;
  597. }
  598. mediaObject.candidates.push(candidateObject);
  599. }
  600. mediaObject.endOfCandidates = 'end-of-candidates';
  601. // DTLS.
  602. // If 'offer' always use 'actpass'.
  603. if (type === 'offer') {
  604. mediaObject.setup = 'actpass';
  605. } else {
  606. mediaObject.setup = remoteDtlsParameters.role === 'server'
  607. ? 'active' : 'passive';
  608. }
  609. if (kind === 'audio' || kind === 'video') {
  610. mediaObject.rtp = [];
  611. mediaObject.rtcpFb = [];
  612. mediaObject.fmtp = [];
  613. // Array of payload types.
  614. const payloads = [];
  615. // Add codecs.
  616. for (const codec of localCapabilities.codecs) {
  617. if (codec.kind && codec.kind !== kind) {
  618. continue; // eslint-disable-line no-continue
  619. }
  620. payloads.push(codec.preferredPayloadType);
  621. const rtpObject = {
  622. codec: codec.name,
  623. payload: codec.preferredPayloadType,
  624. rate: codec.clockRate
  625. };
  626. if (codec.numChannels > 1) {
  627. rtpObject.encoding = codec.numChannels;
  628. }
  629. mediaObject.rtp.push(rtpObject);
  630. // If codec has parameters add them into a=fmtp attributes.
  631. if (codec.parameters) {
  632. const paramFmtp = {
  633. config: '',
  634. payload: codec.preferredPayloadType
  635. };
  636. for (const name of Object.keys(codec.parameters)) {
  637. /* eslint-disable max-depth */
  638. if (paramFmtp.config) {
  639. paramFmtp.config += ';';
  640. }
  641. /* eslint-enable max-depth */
  642. paramFmtp.config
  643. += `${name}=${codec.parameters[name]}`;
  644. }
  645. if (paramFmtp.config) {
  646. mediaObject.fmtp.push(paramFmtp);
  647. }
  648. }
  649. // Set RTCP feedback.
  650. for (const fb of codec.rtcpFeedback || []) {
  651. mediaObject.rtcpFb.push({
  652. payload: codec.preferredPayloadType,
  653. subtype: fb.parameter || undefined,
  654. type: fb.type
  655. });
  656. }
  657. }
  658. // If there are no codecs, set this m section as unavailable.
  659. if (payloads.length === 0) {
  660. mediaObject.payloads = '9'; // Just put something.
  661. mediaObject.port = 0;
  662. mediaObject.direction = 'inactive';
  663. } else {
  664. mediaObject.payloads = payloads.join(' ');
  665. }
  666. // SSRCs.
  667. mediaObject.ssrcs = [];
  668. mediaObject.ssrcGroups = [];
  669. // Add RTP sending stuff.
  670. for (const info of localTrackInfos.values()) {
  671. const rtpSender = info.rtpSender;
  672. const streamId = info.stream.id;
  673. const track = rtpSender.track;
  674. // Ignore if ended.
  675. if (track.readyState === 'ended') {
  676. continue; // eslint-disable-line no-continue
  677. }
  678. if (track.kind !== kind) {
  679. continue; // eslint-disable-line no-continue
  680. }
  681. // Set a random provisional SSRC if not set.
  682. if (!info.ssrc) {
  683. info.ssrc = SDPUtil.generateSsrc();
  684. }
  685. // Whether RTX should be enabled.
  686. const enableRtx = hasVideoRtx && track.kind === 'video';
  687. // Set a random provisional RTX SSRC if not set.
  688. if (enableRtx && !info.rtxSsrc) {
  689. info.rtxSsrc = info.ssrc + 1;
  690. }
  691. mediaObject.ssrcs.push({
  692. attribute: 'cname',
  693. id: info.ssrc,
  694. value: CNAME
  695. });
  696. mediaObject.ssrcs.push({
  697. attribute: 'msid',
  698. id: info.ssrc,
  699. value: `${streamId} ${track.id}`
  700. });
  701. mediaObject.ssrcs.push({
  702. attribute: 'mslabel',
  703. id: info.ssrc,
  704. value: streamId
  705. });
  706. mediaObject.ssrcs.push({
  707. attribute: 'label',
  708. id: info.ssrc,
  709. value: track.id
  710. });
  711. if (enableRtx) {
  712. mediaObject.ssrcs.push({
  713. attribute: 'cname',
  714. id: info.rtxSsrc,
  715. value: CNAME
  716. });
  717. mediaObject.ssrcs.push({
  718. attribute: 'msid',
  719. id: info.rtxSsrc,
  720. value: `${streamId} ${track.id}`
  721. });
  722. mediaObject.ssrcs.push({
  723. attribute: 'mslabel',
  724. id: info.rtxSsrc,
  725. value: streamId
  726. });
  727. mediaObject.ssrcs.push({
  728. attribute: 'label',
  729. id: info.rtxSsrc,
  730. value: track.id
  731. });
  732. mediaObject.ssrcGroups.push({
  733. semantics: 'FID',
  734. ssrcs: `${info.ssrc} ${info.rtxSsrc}`
  735. });
  736. }
  737. }
  738. // RTP header extensions.
  739. mediaObject.ext = [];
  740. for (const extension of localCapabilities.headerExtensions) {
  741. if (extension.kind && extension.kind !== kind) {
  742. continue; // eslint-disable-line no-continue
  743. }
  744. mediaObject.ext.push({
  745. value: extension.preferredId,
  746. uri: extension.uri
  747. });
  748. }
  749. // a=rtcp-mux attribute.
  750. mediaObject.rtcpMux = 'rtcp-mux';
  751. // a=rtcp-rsize.
  752. mediaObject.rtcpRsize = 'rtcp-rsize';
  753. }
  754. // Add the media section.
  755. sdpObject.media.push(mediaObject);
  756. }
  757. }
  758. /**
  759. * Emit 'addstream' event.
  760. * @private
  761. */
  762. _emitAddStream(stream) {
  763. if (this._closed) {
  764. return;
  765. }
  766. logger.debug('emitting "addstream"');
  767. const event = new yaeti.Event('addstream');
  768. event.stream = stream;
  769. this.dispatchEvent(event);
  770. }
  771. /**
  772. * May emit buffered ICE candidates.
  773. * @private
  774. */
  775. _emitBufferedIceCandidates() {
  776. if (this._closed) {
  777. return;
  778. }
  779. for (const sdpCandidate of this._bufferedIceCandidates) {
  780. if (!sdpCandidate) {
  781. continue; // eslint-disable-line no-continue
  782. }
  783. // Now we have set the MID values of the SDP O/A, so let's fill the
  784. // sdpMIndex of the candidate.
  785. sdpCandidate.sdpMIndex = this._mids.keys().next().value;
  786. logger.debug(
  787. 'emitting buffered "icecandidate", candidate:', sdpCandidate);
  788. const event = new yaeti.Event('icecandidate');
  789. event.candidate = sdpCandidate;
  790. this.dispatchEvent(event);
  791. }
  792. this._bufferedIceCandidates = [];
  793. }
  794. /**
  795. * May emit 'connectionstatechange' event.
  796. * @private
  797. */
  798. _emitConnectionStateChange() {
  799. if (this._closed && this.connectionState !== 'closed') {
  800. return;
  801. }
  802. logger.debug(
  803. 'emitting "connectionstatechange", connectionState:',
  804. this.connectionState);
  805. const event = new yaeti.Event('connectionstatechange');
  806. this.dispatchEvent(event);
  807. }
  808. /**
  809. * May emit 'icecandidate' event.
  810. * @private
  811. */
  812. _emitIceCandidate(candidate) {
  813. if (this._closed) {
  814. return;
  815. }
  816. let sdpCandidate = null;
  817. if (candidate) {
  818. // NOTE: We assume BUNDLE so let's just emit candidates for the
  819. // first m= section.
  820. const sdpMIndex = this._mids.keys().next().value;
  821. const sdpMLineIndex = 0;
  822. let sdpAttribute
  823. = `candidate:${candidate.foundation} 1 ${candidate.protocol}`
  824. + ` ${candidate.priority} ${candidate.ip} ${candidate.port}`
  825. + ` typ ${candidate.type}`;
  826. if (candidate.relatedAddress) {
  827. sdpAttribute += ` raddr ${candidate.relatedAddress}`;
  828. }
  829. if (candidate.relatedPort) {
  830. sdpAttribute += ` rport ${candidate.relatedPort}`;
  831. }
  832. if (candidate.protocol === 'tcp') {
  833. sdpAttribute += ` tcptype ${candidate.tcpType}`;
  834. }
  835. sdpCandidate = {
  836. candidate: sdpAttribute,
  837. component: 1, // rtcp-mux assumed, so always 1 (RTP).
  838. foundation: candidate.foundation,
  839. ip: candidate.ip,
  840. port: candidate.port,
  841. priority: candidate.priority,
  842. protocol: candidate.protocol,
  843. type: candidate.type,
  844. sdpMIndex,
  845. sdpMLineIndex
  846. };
  847. if (candidate.protocol === 'tcp') {
  848. sdpCandidate.tcptype = candidate.tcpType;
  849. }
  850. if (candidate.relatedAddress) {
  851. sdpCandidate.relatedAddress = candidate.relatedAddress;
  852. }
  853. if (candidate.relatedPort) {
  854. sdpCandidate.relatedPort = candidate.relatedPort;
  855. }
  856. }
  857. // If we don't have yet a local description, buffer the candidate.
  858. if (this._localDescription) {
  859. logger.debug(
  860. 'emitting "icecandidate", candidate:', sdpCandidate);
  861. const event = new yaeti.Event('icecandidate');
  862. event.candidate = sdpCandidate;
  863. this.dispatchEvent(event);
  864. } else {
  865. logger.debug(
  866. 'buffering gathered ICE candidate:', sdpCandidate);
  867. this._bufferedIceCandidates.push(sdpCandidate);
  868. }
  869. }
  870. /**
  871. * May emit 'iceconnectionstatechange' event.
  872. * @private
  873. */
  874. _emitIceConnectionStateChange() {
  875. if (this._closed && this.iceConnectionState !== 'closed') {
  876. return;
  877. }
  878. logger.debug(
  879. 'emitting "iceconnectionstatechange", iceConnectionState:',
  880. this.iceConnectionState);
  881. const event = new yaeti.Event('iceconnectionstatechange');
  882. this.dispatchEvent(event);
  883. }
  884. /**
  885. * May emit 'negotiationneeded' event.
  886. * @private
  887. */
  888. _emitNegotiationNeeded() {
  889. // Ignore if signalingState is not 'stable'.
  890. if (this.signalingState !== RTCSignalingState.stable) {
  891. return;
  892. }
  893. logger.debug('emitting "negotiationneeded"');
  894. const event = new yaeti.Event('negotiationneeded');
  895. this.dispatchEvent(event);
  896. }
  897. /**
  898. * Emit 'removestream' event.
  899. * @private
  900. */
  901. _emitRemoveStream(stream) {
  902. if (this._closed) {
  903. return;
  904. }
  905. logger.debug('emitting "removestream"');
  906. const event = new yaeti.Event('removestream');
  907. event.stream = stream;
  908. this.dispatchEvent(event);
  909. }
  910. /**
  911. * Get RTP parameters for a RTCRtpReceiver.
  912. * @private
  913. * @return {RTCRtpParameters}
  914. */
  915. _getParametersForRtpReceiver(kind, data) {
  916. const ssrc = data.ssrc;
  917. const rtxSsrc = data.rtxSsrc;
  918. const cname = data.cname;
  919. const localCapabilities = this._localCapabilities;
  920. const parameters = {
  921. codecs: [],
  922. degradationPreference: 'balanced',
  923. encodings: [],
  924. headerExtensions: [],
  925. muxId: '',
  926. rtcp: {
  927. cname,
  928. compound: true, // NOTE: Implemented in Edge.
  929. mux: true,
  930. reducedSize: true // NOTE: Not yet implemented in Edge.
  931. }
  932. };
  933. const codecs = [];
  934. let codecPayloadType;
  935. for (const codecCapability of localCapabilities.codecs) {
  936. if (codecCapability.kind !== kind
  937. || codecCapability.name === 'rtx') {
  938. continue; // eslint-disable-line no-continue
  939. }
  940. codecPayloadType = codecCapability.preferredPayloadType;
  941. codecs.push({
  942. clockRate: codecCapability.clockRate,
  943. maxptime: codecCapability.maxptime,
  944. mimeType: codecCapability.mimeType,
  945. name: codecCapability.name,
  946. numChannels: codecCapability.numChannels,
  947. parameters: codecCapability.parameters,
  948. payloadType: codecCapability.preferredPayloadType,
  949. ptime: codecCapability.ptime,
  950. rtcpFeedback: codecCapability.rtcpFeedback
  951. });
  952. break;
  953. }
  954. if (rtxSsrc) {
  955. for (const codecCapability of localCapabilities.codecs) {
  956. if (codecCapability.kind !== kind
  957. || codecCapability.name !== 'rtx') {
  958. continue; // eslint-disable-line no-continue
  959. }
  960. codecs.push({
  961. clockRate: codecCapability.clockRate,
  962. mimeType: codecCapability.mimeType,
  963. name: 'rtx',
  964. parameters: codecCapability.parameters,
  965. payloadType: codecCapability.preferredPayloadType,
  966. rtcpFeedback: codecCapability.rtcpFeedback
  967. });
  968. break;
  969. }
  970. }
  971. parameters.codecs = codecs;
  972. const encoding = {
  973. active: true,
  974. codecPayloadType,
  975. ssrc
  976. };
  977. if (rtxSsrc) {
  978. encoding.rtx = {
  979. ssrc: rtxSsrc
  980. };
  981. }
  982. parameters.encodings.push(encoding);
  983. for (const extension of localCapabilities.headerExtensions) {
  984. if (extension.kind !== kind) {
  985. continue; // eslint-disable-line no-continue
  986. }
  987. parameters.headerExtensions.push({
  988. encrypt: extension.preferredEncrypt,
  989. id: extension.preferredId,
  990. uri: extension.uri
  991. });
  992. }
  993. return parameters;
  994. }
  995. /**
  996. * Get RTP parameters for a RTCRtpSender.
  997. * @private
  998. * @return {RTCRtpParameters}
  999. */
  1000. _getParametersForRtpSender(kind, data) {
  1001. const ssrc = data.ssrc;
  1002. const rtxSsrc = data.rtxSsrc;
  1003. const cname = CNAME;
  1004. const localCapabilities = this._localCapabilities;
  1005. const parameters = {
  1006. codecs: [],
  1007. degradationPreference: 'balanced',
  1008. encodings: [],
  1009. headerExtensions: [],
  1010. muxId: '',
  1011. rtcp: {
  1012. cname,
  1013. compound: true, // NOTE: Implemented in Edge.
  1014. mux: true,
  1015. reducedSize: true // NOTE: Not yet implemented in Edge.
  1016. }
  1017. };
  1018. const codecs = [];
  1019. let codecPayloadType;
  1020. for (const codecCapability of localCapabilities.codecs) {
  1021. if (codecCapability.kind !== kind
  1022. || codecCapability.name === 'rtx') {
  1023. continue; // eslint-disable-line no-continue
  1024. }
  1025. codecPayloadType = codecCapability.preferredPayloadType;
  1026. codecs.push({
  1027. clockRate: codecCapability.clockRate,
  1028. maxptime: codecCapability.maxptime,
  1029. mimeType: codecCapability.mimeType,
  1030. name: codecCapability.name,
  1031. numChannels: codecCapability.numChannels,
  1032. parameters: codecCapability.parameters,
  1033. payloadType: codecCapability.preferredPayloadType,
  1034. ptime: codecCapability.ptime,
  1035. rtcpFeedback: codecCapability.rtcpFeedback
  1036. });
  1037. break;
  1038. }
  1039. if (rtxSsrc) {
  1040. for (const codecCapability of localCapabilities.codecs) {
  1041. if (codecCapability.kind !== kind
  1042. || codecCapability.name !== 'rtx') {
  1043. continue; // eslint-disable-line no-continue
  1044. }
  1045. codecs.push({
  1046. clockRate: codecCapability.clockRate,
  1047. mimeType: codecCapability.mimeType,
  1048. name: 'rtx',
  1049. parameters: codecCapability.parameters,
  1050. payloadType: codecCapability.preferredPayloadType,
  1051. rtcpFeedback: codecCapability.rtcpFeedback
  1052. });
  1053. break;
  1054. }
  1055. }
  1056. parameters.codecs = codecs;
  1057. const encoding = {
  1058. active: true,
  1059. codecPayloadType,
  1060. ssrc
  1061. };
  1062. if (rtxSsrc) {
  1063. encoding.rtx = {
  1064. ssrc: rtxSsrc
  1065. };
  1066. }
  1067. parameters.encodings.push(encoding);
  1068. for (const extension of localCapabilities.headerExtensions) {
  1069. if (extension.kind !== kind) {
  1070. continue; // eslint-disable-line no-continue
  1071. }
  1072. parameters.headerExtensions.push({
  1073. encrypt: extension.preferredEncrypt,
  1074. id: extension.preferredId,
  1075. uri: extension.uri
  1076. });
  1077. }
  1078. return parameters;
  1079. }
  1080. /**
  1081. * Promise based implementation for getStats().
  1082. * @return {Promise} RTCStats dictionary.
  1083. * @private
  1084. */
  1085. _getStats(selector) { // eslint-disable-line no-unused-vars
  1086. if (this._closed) {
  1087. return Promise.reject(
  1088. new InvalidStateError('RTCPeerConnection closed'));
  1089. }
  1090. const iceGatherer = this._iceGatherer;
  1091. const iceTransport = this._iceTransport;
  1092. const rtpSenders = [];
  1093. const rtpReceivers = [];
  1094. const promises = [];
  1095. // Get RtpSenders.
  1096. for (const info of this._localTrackInfos.values()) {
  1097. const { rtpSender, sending } = info;
  1098. if (sending) {
  1099. rtpSenders.push(rtpSender);
  1100. }
  1101. }
  1102. // Get RtpReceivers.
  1103. for (const info of this._remoteTrackInfos.values()) {
  1104. const { rtpReceiver } = info;
  1105. rtpReceivers.push(rtpReceiver);
  1106. }
  1107. // Collect all the stats.
  1108. if (iceGatherer) {
  1109. promises.push(
  1110. iceGatherer.getStats()
  1111. .catch(() => null));
  1112. }
  1113. if (iceTransport) {
  1114. promises.push(
  1115. iceTransport.getStats()
  1116. .catch(() => null));
  1117. // NOTE: Proprietary stuff in Edge.
  1118. if (typeof iceTransport.msGetStats === 'function') {
  1119. promises.push(
  1120. iceTransport.msGetStats()
  1121. .catch(() => null));
  1122. }
  1123. }
  1124. for (const rtpSender of rtpSenders) {
  1125. const isAudio = rtpSender.track.kind === 'audio';
  1126. promises.push(rtpSender.getStats()
  1127. .then(data => {
  1128. // Remove audioLevel from type="track" stats if this is
  1129. // not an audio sender.
  1130. if (!isAudio) {
  1131. for (const key of Object.keys(data)) {
  1132. const stat = data[key];
  1133. if (stat.type === 'track') {
  1134. delete stat.audioLevel;
  1135. }
  1136. }
  1137. }
  1138. return data;
  1139. })
  1140. .catch(() => null));
  1141. }
  1142. for (const rtpReceiver of rtpReceivers) {
  1143. const isAudio = rtpReceiver.track.kind === 'audio';
  1144. promises.push(rtpReceiver.getStats()
  1145. .then(data => {
  1146. // Remove audioLevel from type="track" stats if this is
  1147. // not an audio receiver.
  1148. if (!isAudio) {
  1149. for (const key of Object.keys(data)) {
  1150. const stat = data[key];
  1151. if (stat.type === 'track') {
  1152. delete stat.audioLevel;
  1153. }
  1154. }
  1155. }
  1156. return data;
  1157. })
  1158. .catch(() => null));
  1159. }
  1160. return Promise.all(promises)
  1161. .then(datas => {
  1162. const stats = {};
  1163. for (const data of datas) {
  1164. if (!data) {
  1165. continue; // eslint-disable-line no-continue
  1166. }
  1167. for (const key of Object.keys(data)) {
  1168. stats[key] = data[key];
  1169. }
  1170. }
  1171. return stats;
  1172. });
  1173. }
  1174. /**
  1175. * Handles the local initial answer.
  1176. * @return {Promise}
  1177. * @private
  1178. */
  1179. _handleLocalInitialAnswer(desc) {
  1180. logger.debug('_handleLocalInitialAnswer(), desc:', desc);
  1181. const sdpObject = desc.sdpObject;
  1182. // Update local capabilities as decided by the app.
  1183. this._localCapabilities = utils.extractCapabilities(sdpObject);
  1184. logger.debug('local capabilities:', this._localCapabilities);
  1185. // NOTE: We assume that the answer given by the app does not change
  1186. // SSRC or PT values. If so, things won't work as expected.
  1187. }
  1188. /**
  1189. * Handles a local re-answer.
  1190. * @return {Promise}
  1191. * @private
  1192. */
  1193. _handleLocalReAnswer(desc) {
  1194. logger.debug('_handleLocalReAnswer(), desc:', desc);
  1195. const sdpObject = desc.sdpObject;
  1196. // Update local capabilities as decided by the app.
  1197. this._localCapabilities = utils.extractCapabilities(sdpObject);
  1198. logger.debug('local capabilities:', this._localCapabilities);
  1199. // NOTE: We assume that the answer given by the app does not change
  1200. // SSRC or PT values. If so, things won't work as expected.
  1201. }
  1202. /**
  1203. * Handles the remote initial offer.
  1204. * @return {Promise}
  1205. * @private
  1206. */
  1207. _handleRemoteInitialOffer(desc) {
  1208. logger.debug('_handleRemoteInitialOffer(), desc:', desc);
  1209. const sdpObject = desc.sdpObject;
  1210. // Set MID values.
  1211. this._mids = utils.extractMids(sdpObject);
  1212. // Get remote RTP capabilities.
  1213. const remoteCapabilities = utils.extractCapabilities(sdpObject);
  1214. logger.debug('remote capabilities:', remoteCapabilities);
  1215. // Get local RTP capabilities (filter them with remote capabilities).
  1216. this._localCapabilities
  1217. = utils.getLocalCapabilities(remoteCapabilities);
  1218. // Start ICE and DTLS.
  1219. this._startIceAndDtls(desc);
  1220. }
  1221. /**
  1222. * Handles a remote re-offer.
  1223. * @return {Promise}
  1224. * @private
  1225. */
  1226. _handleRemoteReOffer(desc) {
  1227. logger.debug('_handleRemoteReOffer(), desc:', desc);
  1228. const sdpObject = desc.sdpObject;
  1229. // Update MID values (just in case).
  1230. this._mids = utils.extractMids(sdpObject);
  1231. // Get remote RTP capabilities (filter them with remote capabilities).
  1232. const remoteCapabilities = utils.extractCapabilities(sdpObject);
  1233. logger.debug('remote capabilities:', remoteCapabilities);
  1234. // Update local RTP capabilities (just in case).
  1235. this._localCapabilities
  1236. = utils.getLocalCapabilities(remoteCapabilities);
  1237. }
  1238. /**
  1239. * Start receiving remote media.
  1240. */
  1241. _receiveMedia() {
  1242. logger.debug('_receiveMedia()');
  1243. const currentRemoteSsrcs = new Set(this._remoteTrackInfos.keys());
  1244. const newRemoteTrackInfos
  1245. = utils.extractTrackInfos(this._remoteDescription.sdpObject);
  1246. // Map of new remote MediaStream indexed by MediaStream.jitsiRemoteId.
  1247. const addedRemoteStreams = new Map();
  1248. // Map of remote MediaStream indexed by added MediaStreamTrack.
  1249. // NOTE: Just filled for already existing streams.
  1250. const addedRemoteTracks = new Map();
  1251. // Map of remote MediaStream indexed by removed MediaStreamTrack.
  1252. const removedRemoteTracks = new Map();
  1253. logger.debug(
  1254. '_receiveMedia() remote track infos:', newRemoteTrackInfos);
  1255. // Check new tracks.
  1256. for (const [ ssrc, info ] of newRemoteTrackInfos) {
  1257. // If already handled, ignore it.
  1258. if (currentRemoteSsrcs.has(ssrc)) {
  1259. continue; // eslint-disable-line no-continue
  1260. }
  1261. logger.debug(`_receiveMedia() new remote track, ssrc:${ssrc}`);
  1262. // Otherwise append to the map.
  1263. this._remoteTrackInfos.set(ssrc, info);
  1264. const kind = info.kind;
  1265. const rtxSsrc = info.rtxSsrc;
  1266. const streamRemoteId = info.streamId;
  1267. const trackRemoteId = info.trackId;
  1268. const cname = info.cname;
  1269. const isNewStream = !this._remoteStreams.has(streamRemoteId);
  1270. let stream;
  1271. if (isNewStream) {
  1272. logger.debug(
  1273. `_receiveMedia() new remote stream, id:${streamRemoteId}`);
  1274. // Create a new MediaStream.
  1275. stream = new MediaStream();
  1276. // Set custom property with the remote id.
  1277. stream.jitsiRemoteId = streamRemoteId;
  1278. addedRemoteStreams.set(streamRemoteId, stream);
  1279. this._remoteStreams.set(streamRemoteId, stream);
  1280. } else {
  1281. stream = this._remoteStreams.get(streamRemoteId);
  1282. }
  1283. const rtpReceiver = new RTCRtpReceiver(this._dtlsTransport, kind);
  1284. const parameters = this._getParametersForRtpReceiver(kind, {
  1285. ssrc,
  1286. rtxSsrc,
  1287. cname
  1288. });
  1289. // Store the track into the info object.
  1290. // NOTE: This should not be needed, but Edge has a bug:
  1291. // https://developer.microsoft.com/en-us/microsoft-edge/platform/
  1292. // issues/12399497/
  1293. info.track = rtpReceiver.track;
  1294. // Set error handler.
  1295. rtpReceiver.onerror = ev => {
  1296. logger.error('rtpReceiver "error" event, event:');
  1297. logger.error(ev);
  1298. };
  1299. // Fill the info with the stream and rtpReceiver.
  1300. info.stream = stream;
  1301. info.rtpReceiver = rtpReceiver;
  1302. logger.debug(
  1303. 'calling rtpReceiver.receive(), parameters:', parameters);
  1304. // Start receiving media.
  1305. try {
  1306. rtpReceiver.receive(parameters);
  1307. // Get the associated MediaStreamTrack.
  1308. const track = info.track;
  1309. // Set custom property with the remote id.
  1310. track.jitsiRemoteId = trackRemoteId;
  1311. // Add the track to the stream.
  1312. stream.addTrack(track);
  1313. if (!addedRemoteStreams.has(streamRemoteId)) {
  1314. addedRemoteTracks.set(track, stream);
  1315. }
  1316. } catch (error) {
  1317. logger.error(`rtpReceiver.receive() failed:${error.message}`);
  1318. logger.error(error);
  1319. }
  1320. }
  1321. // Check track removal.
  1322. for (const ssrc of currentRemoteSsrcs) {
  1323. if (newRemoteTrackInfos.has(ssrc)) {
  1324. continue; // eslint-disable-line no-continue
  1325. }
  1326. logger.debug(`_receiveMedia() remote track removed, ssrc:${ssrc}`);
  1327. const info = this._remoteTrackInfos.get(ssrc);
  1328. const stream = info.stream;
  1329. const track = info.track;
  1330. const rtpReceiver = info.rtpReceiver;
  1331. try {
  1332. rtpReceiver.stop();
  1333. } catch (error) {
  1334. logger.warn(`rtpReceiver.stop() failed:${error}`);
  1335. }
  1336. removedRemoteTracks.set(track, stream);
  1337. stream.removeTrack(track);
  1338. this._remoteTrackInfos.delete(ssrc);
  1339. }
  1340. // Emit MediaStream 'addtrack' for new tracks in already existing
  1341. // streams.
  1342. for (const [ track, stream ] of addedRemoteTracks) {
  1343. const event = new Event('addtrack');
  1344. event.track = track;
  1345. stream.dispatchEvent(event);
  1346. }
  1347. // Emit MediaStream 'removetrack' for removed tracks.
  1348. for (const [ track, stream ] of removedRemoteTracks) {
  1349. const event = new Event('removetrack');
  1350. event.track = track;
  1351. stream.dispatchEvent(event);
  1352. }
  1353. // Emit RTCPeerConnection 'addstream' for new remote streams.
  1354. for (const stream of addedRemoteStreams.values()) {
  1355. // Check whether at least a track was added, otherwise ignore it.
  1356. if (stream.getTracks().length === 0) {
  1357. logger.warn(
  1358. 'ignoring new stream for which no track could be added');
  1359. addedRemoteStreams.delete(stream.jitsiRemoteId);
  1360. this._remoteStreams.delete(stream.jitsiRemoteId);
  1361. } else {
  1362. this._emitAddStream(stream);
  1363. }
  1364. }
  1365. // Emit RTCPeerConnection 'removestream' for removed remote streams.
  1366. for (const [ streamRemoteId, stream ] of this._remoteStreams) {
  1367. if (stream.getTracks().length > 0) {
  1368. continue; // eslint-disable-line no-continue
  1369. }
  1370. this._remoteStreams.delete(streamRemoteId);
  1371. this._emitRemoveStream(stream);
  1372. }
  1373. }
  1374. /**
  1375. * Start sending our media to the remote.
  1376. */
  1377. _sendMedia() {
  1378. logger.debug('_sendMedia()');
  1379. for (const info of this._localTrackInfos.values()) {
  1380. // Ignore if already sending.
  1381. if (info.sending) {
  1382. continue; // eslint-disable-line no-continue
  1383. }
  1384. const rtpSender = info.rtpSender;
  1385. const ssrc = info.ssrc;
  1386. const rtxSsrc = info.rtxSsrc;
  1387. const track = rtpSender.track;
  1388. const kind = track.kind;
  1389. const parameters = this._getParametersForRtpSender(kind, {
  1390. ssrc,
  1391. rtxSsrc
  1392. });
  1393. logger.debug(
  1394. 'calling rtpSender.send(), parameters:', parameters);
  1395. // Start sending media.
  1396. try {
  1397. rtpSender.send(parameters);
  1398. // Update sending field.
  1399. info.sending = true;
  1400. } catch (error) {
  1401. logger.error(`rtpSender.send() failed:${error.message}`);
  1402. logger.error(error);
  1403. }
  1404. }
  1405. }
  1406. /**
  1407. * Creates the RTCDtlsTransport.
  1408. * @private
  1409. */
  1410. _setDtlsTransport(iceTransport) {
  1411. const dtlsTransport = new RTCDtlsTransport(iceTransport);
  1412. // NOTE: Not yet implemented by Edge.
  1413. dtlsTransport.onstatechange = () => {
  1414. logger.debug(
  1415. 'dtlsTransport "statechange" event, '
  1416. + `state:${dtlsTransport.state}`);
  1417. this._emitConnectionStateChange();
  1418. };
  1419. // NOTE: Not standard, but implemented by Edge.
  1420. dtlsTransport.ondtlsstatechange = () => {
  1421. logger.debug(
  1422. 'dtlsTransport "dtlsstatechange" event, '
  1423. + `state:${dtlsTransport.state}`);
  1424. this._emitConnectionStateChange();
  1425. };
  1426. dtlsTransport.onerror = ev => {
  1427. let message;
  1428. if (ev.message) {
  1429. message = ev.message;
  1430. } else if (ev.error) {
  1431. message = ev.error.message;
  1432. }
  1433. logger.error(`dtlsTransport "error" event, message:${message}`);
  1434. // TODO: Edge does not set state to 'failed' on error. We may
  1435. // hack it.
  1436. this._emitConnectionStateChange();
  1437. };
  1438. this._dtlsTransport = dtlsTransport;
  1439. }
  1440. /**
  1441. * Creates the RTCIceGatherer.
  1442. * @private
  1443. */
  1444. _setIceGatherer(pcConfig) {
  1445. const iceGatherOptions = {
  1446. gatherPolicy: pcConfig.iceTransportPolicy || 'all',
  1447. iceServers: pcConfig.iceServers
  1448. };
  1449. const iceGatherer = new RTCIceGatherer(iceGatherOptions);
  1450. // NOTE: Not yet implemented by Edge.
  1451. iceGatherer.onstatechange = () => {
  1452. logger.debug(
  1453. `iceGatherer "statechange" event, state:${iceGatherer.state}`);
  1454. this._updateAndEmitIceGatheringStateChange(iceGatherer.state);
  1455. };
  1456. iceGatherer.onlocalcandidate = ev => {
  1457. let candidate = ev.candidate;
  1458. // NOTE: Not yet implemented by Edge.
  1459. const complete = ev.complete;
  1460. logger.debug(
  1461. 'iceGatherer "localcandidate" event, candidate:', candidate);
  1462. // NOTE: Instead of null candidate or complete:true, current Edge
  1463. // signals end of gathering with an empty candidate object.
  1464. if (complete
  1465. || !candidate
  1466. || Object.keys(candidate).length === 0) {
  1467. candidate = null;
  1468. this._updateAndEmitIceGatheringStateChange(
  1469. RTCIceGatheringState.complete);
  1470. this._resolveLocalIceGatheringPromise();
  1471. this._emitIceCandidate(null);
  1472. } else {
  1473. this._emitIceCandidate(candidate);
  1474. }
  1475. };
  1476. iceGatherer.onerror = ev => {
  1477. const errorCode = ev.errorCode;
  1478. const errorText = ev.errorText;
  1479. logger.error(
  1480. `iceGatherer "error" event, errorCode:${errorCode}, `
  1481. + `errorText:${errorText}`);
  1482. };
  1483. // NOTE: Not yet implemented by Edge, which starts gathering
  1484. // automatically.
  1485. try {
  1486. iceGatherer.gather();
  1487. } catch (error) {
  1488. logger.warn(`iceGatherer.gather() failed:${error}`);
  1489. }
  1490. this._iceGatherer = iceGatherer;
  1491. }
  1492. /**
  1493. * Creates the RTCIceTransport.
  1494. * @private
  1495. */
  1496. _setIceTransport(iceGatherer) {
  1497. const iceTransport = new RTCIceTransport(iceGatherer);
  1498. // NOTE: Not yet implemented by Edge.
  1499. iceTransport.onstatechange = () => {
  1500. logger.debug(
  1501. 'iceTransport "statechange" event, '
  1502. + `state:${iceTransport.state}`);
  1503. this._emitIceConnectionStateChange();
  1504. };
  1505. // NOTE: Not standard, but implemented by Edge.
  1506. iceTransport.onicestatechange = () => {
  1507. logger.debug(
  1508. 'iceTransport "icestatechange" event, '
  1509. + `state:${iceTransport.state}`);
  1510. if (iceTransport.state === 'completed') {
  1511. logger.debug(
  1512. 'nominated candidate pair:',
  1513. iceTransport.getNominatedCandidatePair());
  1514. }
  1515. this._emitIceConnectionStateChange();
  1516. };
  1517. iceTransport.oncandidatepairchange = ev => {
  1518. logger.debug(
  1519. 'iceTransport "candidatepairchange" event, '
  1520. + `pair:${ev.pair}`);
  1521. };
  1522. this._iceTransport = iceTransport;
  1523. }
  1524. /**
  1525. * Promise based implementation for setLocalDescription().
  1526. * @returns {Promise}
  1527. * @private
  1528. */
  1529. _setLocalDescription(desc) {
  1530. if (this._closed) {
  1531. return Promise.reject(
  1532. new InvalidStateError('RTCPeerConnection closed'));
  1533. }
  1534. let localDescription;
  1535. try {
  1536. localDescription = new RTCSessionDescription(desc);
  1537. } catch (error) {
  1538. return Promise.reject(new TypeError(
  1539. `invalid RTCSessionDescriptionInit: ${error}`));
  1540. }
  1541. switch (desc.type) {
  1542. case 'offer': {
  1543. if (this.signalingState !== RTCSignalingState.stable) {
  1544. return Promise.reject(new InvalidStateError(
  1545. `invalid signalingState "${this.signalingState}"`));
  1546. }
  1547. return Promise.resolve()
  1548. .then(() => {
  1549. const sdpObject = localDescription.sdpObject;
  1550. // Update local values? Needed?
  1551. this._localCapabilities
  1552. = utils.extractCapabilities(sdpObject);
  1553. this._localDescription = localDescription;
  1554. // Update signaling state.
  1555. this._updateAndEmitSignalingStateChange(
  1556. RTCSignalingState.haveLocalOffer);
  1557. })
  1558. .catch(error => {
  1559. logger.error(
  1560. `setLocalDescription() failed: ${error.message}`);
  1561. logger.error(error);
  1562. throw error;
  1563. });
  1564. }
  1565. case 'answer': {
  1566. if (this.signalingState !== RTCSignalingState.haveRemoteOffer) {
  1567. return Promise.reject(new InvalidStateError(
  1568. `invalid signalingState "${this.signalingState}"`));
  1569. }
  1570. const isLocalInitialAnswer = Boolean(!this._localDescription);
  1571. return Promise.resolve()
  1572. .then(() => {
  1573. // Different handling for initial answer and re-answer.
  1574. if (isLocalInitialAnswer) {
  1575. return this._handleLocalInitialAnswer(localDescription);
  1576. } else { // eslint-disable-line no-else-return
  1577. return this._handleLocalReAnswer(localDescription);
  1578. }
  1579. })
  1580. .then(() => {
  1581. logger.debug('setLocalDescription() succeed');
  1582. // Update local description.
  1583. this._localDescription = localDescription;
  1584. // Update signaling state.
  1585. this._updateAndEmitSignalingStateChange(
  1586. RTCSignalingState.stable);
  1587. // If initial answer, emit buffered ICE candidates.
  1588. if (isLocalInitialAnswer) {
  1589. this._emitBufferedIceCandidates();
  1590. }
  1591. // Send our RTP.
  1592. this._sendMedia();
  1593. // Receive remote RTP.
  1594. this._receiveMedia();
  1595. })
  1596. .catch(error => {
  1597. logger.error(
  1598. `setLocalDescription() failed: ${error.message}`);
  1599. logger.error(error);
  1600. throw error;
  1601. });
  1602. }
  1603. default:
  1604. return Promise.reject(new TypeError(
  1605. `unsupported description.type "${desc.type}"`));
  1606. }
  1607. }
  1608. /**
  1609. * Promise based implementation for setRemoteDescription().
  1610. * @returns {Promise}
  1611. * @private
  1612. */
  1613. _setRemoteDescription(desc) {
  1614. if (this._closed) {
  1615. return Promise.reject(
  1616. new InvalidStateError('RTCPeerConnection closed'));
  1617. }
  1618. let remoteDescription;
  1619. try {
  1620. remoteDescription = new RTCSessionDescription(desc);
  1621. } catch (error) {
  1622. return Promise.reject(new TypeError(
  1623. `invalid RTCSessionDescriptionInit: ${error}`));
  1624. }
  1625. switch (desc.type) {
  1626. case 'offer': {
  1627. if (this.signalingState !== RTCSignalingState.stable) {
  1628. return Promise.reject(new InvalidStateError(
  1629. `invalid signalingState "${this.signalingState}"`));
  1630. }
  1631. const isRemoteInitialOffer = Boolean(!this._remoteDescription);
  1632. return Promise.resolve()
  1633. .then(() => {
  1634. // Different handling for initial answer and re-answer.
  1635. if (isRemoteInitialOffer) {
  1636. return this._handleRemoteInitialOffer(
  1637. remoteDescription);
  1638. } else { // eslint-disable-line no-else-return
  1639. return this._handleRemoteReOffer(remoteDescription);
  1640. }
  1641. })
  1642. .then(() => {
  1643. logger.debug('setRemoteDescription() succeed');
  1644. this._remoteDescription = remoteDescription;
  1645. this._updateAndEmitSignalingStateChange(
  1646. RTCSignalingState.haveRemoteOffer);
  1647. })
  1648. .catch(error => {
  1649. logger.error(`setRemoteDescription() failed: ${error}`);
  1650. throw error;
  1651. });
  1652. }
  1653. case 'answer': {
  1654. if (this.signalingState !== RTCSignalingState.haveLocalOffer) {
  1655. return Promise.reject(new InvalidStateError(
  1656. `invalid signalingState "${this.signalingState}"`));
  1657. }
  1658. return Promise.resolve()
  1659. .then(() => {
  1660. // Update remote description.
  1661. this._remoteDescription = remoteDescription;
  1662. // Update signaling state.
  1663. this._updateAndEmitSignalingStateChange(
  1664. RTCSignalingState.stable);
  1665. })
  1666. .catch(error => {
  1667. logger.error(`setRemoteDescription() failed: ${error}`);
  1668. throw error;
  1669. });
  1670. }
  1671. default:
  1672. return Promise.reject(new TypeError(
  1673. `unsupported description.type "${desc.type}"`));
  1674. }
  1675. }
  1676. /**
  1677. * Start ICE and DTLS connection procedures.
  1678. * @param {RTCSessionDescription} desc - Remote description.
  1679. */
  1680. _startIceAndDtls(desc) {
  1681. const sdpObject = desc.sdpObject;
  1682. const remoteIceParameters
  1683. = utils.extractIceParameters(sdpObject);
  1684. const remoteIceCandidates
  1685. = utils.extractIceCandidates(sdpObject);
  1686. const remoteDtlsParameters
  1687. = utils.extractDtlsParameters(sdpObject);
  1688. // Start the RTCIceTransport.
  1689. switch (desc.type) {
  1690. case 'offer':
  1691. this._iceTransport.start(
  1692. this._iceGatherer, remoteIceParameters, 'controlled');
  1693. break;
  1694. case 'answer':
  1695. this._iceTransport.start(
  1696. this._iceGatherer, remoteIceParameters, 'controlling');
  1697. break;
  1698. }
  1699. // Add remote ICE candidates.
  1700. // NOTE: Remove candidates that Edge doesn't like.
  1701. for (const candidate of remoteIceCandidates) {
  1702. if (candidate.port === 0 || candidate.port === 9) {
  1703. continue; // eslint-disable-line no-continue
  1704. }
  1705. this._iceTransport.addRemoteCandidate(candidate);
  1706. }
  1707. // Also signal a 'complete' candidate as per spec.
  1708. // NOTE: It should be {complete: true} but Edge prefers {}.
  1709. // NOTE: We know that addCandidate() is never used so we need to signal
  1710. // end of candidates (otherwise the RTCIceTransport never enters the
  1711. // 'completed' state).
  1712. this._iceTransport.addRemoteCandidate({});
  1713. // Set desired remote DTLS role (as we receive the offer).
  1714. switch (desc.type) {
  1715. case 'offer':
  1716. remoteDtlsParameters.role = 'server';
  1717. break;
  1718. case 'answer':
  1719. remoteDtlsParameters.role = 'client';
  1720. break;
  1721. }
  1722. // Start RTCDtlsTransport.
  1723. this._dtlsTransport.start(remoteDtlsParameters);
  1724. }
  1725. /**
  1726. * May update iceGatheringState and emit 'icegatheringstatechange' event.
  1727. * @private
  1728. */
  1729. _updateAndEmitIceGatheringStateChange(state) {
  1730. if (this._closed || state === this.iceGatheringState) {
  1731. return;
  1732. }
  1733. this._iceGatheringState = state;
  1734. logger.debug(
  1735. 'emitting "icegatheringstatechange", iceGatheringState:',
  1736. this.iceGatheringState);
  1737. const event = new yaeti.Event('icegatheringstatechange');
  1738. this.dispatchEvent(event);
  1739. }
  1740. /**
  1741. * May update signalingState and emit 'signalingstatechange' event.
  1742. * @private
  1743. */
  1744. _updateAndEmitSignalingStateChange(state) {
  1745. if (state === this.signalingState) {
  1746. return;
  1747. }
  1748. this._signalingState = state;
  1749. logger.debug(
  1750. 'emitting "signalingstatechange", signalingState:',
  1751. this.signalingState);
  1752. const event = new yaeti.Event('signalingstatechange');
  1753. this.dispatchEvent(event);
  1754. }
  1755. }