You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TraceablePeerConnection.js 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /* global __filename, mozRTCPeerConnection, webkitRTCPeerConnection,
  2. RTCPeerConnection, RTCSessionDescription */
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import * as GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  5. import RTC from './RTC';
  6. import RTCBrowserType from './RTCBrowserType.js';
  7. import RTCEvents from '../../service/RTC/RTCEvents';
  8. import RtxModifier from '../xmpp/RtxModifier.js';
  9. // FIXME SDP tools should end up in some kind of util module
  10. import SDP from '../xmpp/SDP';
  11. import SdpConsistency from '../xmpp/SdpConsistency.js';
  12. import SDPUtil from '../xmpp/SDPUtil';
  13. import transform from 'sdp-transform';
  14. const logger = getLogger(__filename);
  15. const SIMULCAST_LAYERS = 3;
  16. /* eslint-disable max-params */
  17. /**
  18. * Creates new instance of 'TraceablePeerConnection'.
  19. *
  20. * @param {RTC} rtc the instance of <tt>RTC</tt> service
  21. * @param {number} id the peer connection id assigned by the parent RTC module.
  22. * @param {SignalingLayer} signalingLayer the signaling layer instance
  23. * @param {object} iceConfig WebRTC 'PeerConnection' ICE config
  24. * @param {object} constraints WebRTC 'PeerConnection' constraints
  25. * @param {object} options <tt>TracablePeerConnection</tt> config options.
  26. * @param {boolean} options.disableSimulcast if set to 'true' will disable
  27. * the simulcast
  28. * @param {boolean} options.disableRtx if set to 'true' will disable the RTX
  29. * @param {boolean} options.preferH264 if set to 'true' H264 will be preferred
  30. * over other video codecs.
  31. *
  32. * FIXME: initially the purpose of TraceablePeerConnection was to be able to
  33. * debug the peer connection. Since many other responsibilities have been added
  34. * it would make sense to extract a separate class from it and come up with
  35. * a more suitable name.
  36. *
  37. * @constructor
  38. */
  39. function TraceablePeerConnection(
  40. rtc,
  41. id,
  42. signalingLayer,
  43. iceConfig,
  44. constraints,
  45. options) {
  46. /**
  47. * The parent instance of RTC service which created this
  48. * <tt>TracablePeerConnection</tt>.
  49. * @type {RTC}
  50. */
  51. this.rtc = rtc;
  52. /**
  53. * The peer connection identifier assigned by the RTC module.
  54. * @type {number}
  55. */
  56. this.id = id;
  57. /**
  58. * The signaling layer which operates this peer connection.
  59. * @type {SignalingLayer}
  60. */
  61. this.signalingLayer = signalingLayer;
  62. this.options = options;
  63. let RTCPeerConnectionType = null;
  64. if (RTCBrowserType.isFirefox()) {
  65. RTCPeerConnectionType = mozRTCPeerConnection;
  66. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  67. RTCPeerConnectionType = RTCPeerConnection;
  68. } else {
  69. RTCPeerConnectionType = webkitRTCPeerConnection;
  70. }
  71. this.peerconnection = new RTCPeerConnectionType(iceConfig, constraints);
  72. this.updateLog = [];
  73. this.stats = {};
  74. this.statsinterval = null;
  75. /**
  76. * @type {number}
  77. */
  78. this.maxstats = 0;
  79. const Interop = require('sdp-interop').Interop;
  80. this.interop = new Interop();
  81. const Simulcast = require('sdp-simulcast');
  82. this.simulcast = new Simulcast({ numOfLayers: SIMULCAST_LAYERS,
  83. explodeRemoteSimulcast: false });
  84. this.sdpConsistency = new SdpConsistency();
  85. /**
  86. * TracablePeerConnection uses RTC's eventEmitter
  87. * @type {EventEmitter}
  88. */
  89. this.eventEmitter = rtc.eventEmitter;
  90. this.rtxModifier = new RtxModifier();
  91. // override as desired
  92. this.trace = (what, info) => {
  93. /* logger.warn('WTRACE', what, info);
  94. if (info && RTCBrowserType.isIExplorer()) {
  95. if (info.length > 1024) {
  96. logger.warn('WTRACE', what, info.substr(1024));
  97. }
  98. if (info.length > 2048) {
  99. logger.warn('WTRACE', what, info.substr(2048));
  100. }
  101. }*/
  102. this.updateLog.push({
  103. time: new Date(),
  104. type: what,
  105. value: info || ''
  106. });
  107. };
  108. this.onicecandidate = null;
  109. this.peerconnection.onicecandidate = event => {
  110. // FIXME: this causes stack overflow with Temasys Plugin
  111. if (!RTCBrowserType.isTemasysPluginUsed()) {
  112. this.trace(
  113. 'onicecandidate',
  114. JSON.stringify(event.candidate, null, ' '));
  115. }
  116. if (this.onicecandidate !== null) {
  117. this.onicecandidate(event);
  118. }
  119. };
  120. this.onaddstream = null;
  121. this.peerconnection.onaddstream = event => {
  122. this.trace('onaddstream', event.stream.id);
  123. if (this.onaddstream !== null) {
  124. this.onaddstream(event);
  125. }
  126. };
  127. this.onremovestream = null;
  128. this.peerconnection.onremovestream = event => {
  129. this.trace('onremovestream', event.stream.id);
  130. if (this.onremovestream !== null) {
  131. this.onremovestream(event);
  132. }
  133. };
  134. this.peerconnection.onaddstream
  135. = event => this._remoteStreamAdded(event.stream);
  136. this.peerconnection.onremovestream
  137. = event => this._remoteStreamRemoved(event.stream);
  138. this.onsignalingstatechange = null;
  139. this.peerconnection.onsignalingstatechange = event => {
  140. this.trace('onsignalingstatechange', this.signalingState);
  141. if (this.onsignalingstatechange !== null) {
  142. this.onsignalingstatechange(event);
  143. }
  144. };
  145. this.oniceconnectionstatechange = null;
  146. this.peerconnection.oniceconnectionstatechange = event => {
  147. this.trace('oniceconnectionstatechange', this.iceConnectionState);
  148. if (this.oniceconnectionstatechange !== null) {
  149. this.oniceconnectionstatechange(event);
  150. }
  151. };
  152. this.onnegotiationneeded = null;
  153. this.peerconnection.onnegotiationneeded = event => {
  154. this.trace('onnegotiationneeded');
  155. if (this.onnegotiationneeded !== null) {
  156. this.onnegotiationneeded(event);
  157. }
  158. };
  159. this.ondatachannel = null;
  160. this.peerconnection.ondatachannel = event => {
  161. this.trace('ondatachannel', event);
  162. if (this.ondatachannel !== null) {
  163. this.ondatachannel(event);
  164. }
  165. };
  166. // XXX: do all non-firefox browsers which we support also support this?
  167. if (!RTCBrowserType.isFirefox() && this.maxstats) {
  168. this.statsinterval = window.setInterval(() => {
  169. this.peerconnection.getStats(stats => {
  170. const results = stats.result();
  171. const now = new Date();
  172. for (let i = 0; i < results.length; ++i) {
  173. results[i].names().forEach(name => {
  174. // eslint-disable-next-line no-shadow
  175. const id = `${results[i].id}-${name}`;
  176. let s = this.stats[id];
  177. if (!s) {
  178. this.stats[id] = s = {
  179. startTime: now,
  180. endTime: now,
  181. values: [],
  182. times: []
  183. };
  184. }
  185. s.values.push(results[i].stat(name));
  186. s.times.push(now.getTime());
  187. if (s.values.length > this.maxstats) {
  188. s.values.shift();
  189. s.times.shift();
  190. }
  191. s.endTime = now;
  192. });
  193. }
  194. });
  195. }, 1000);
  196. }
  197. }
  198. /* eslint-enable max-params */
  199. /**
  200. * Returns a string representation of a SessionDescription object.
  201. */
  202. const dumpSDP = function(description) {
  203. if (typeof description === 'undefined' || description === null) {
  204. return '';
  205. }
  206. return `type: ${description.type}\r\n${description.sdp}`;
  207. };
  208. /**
  209. * Called when new remote MediaStream is added to the PeerConnection.
  210. * @param {MediaStream} stream the WebRTC MediaStream for remote participant
  211. */
  212. TraceablePeerConnection.prototype._remoteStreamAdded = function(stream) {
  213. if (!RTC.isUserStream(stream)) {
  214. logger.info(
  215. 'Ignored remote \'stream added\' event for non-user stream',
  216. stream);
  217. return;
  218. }
  219. // Bind 'addtrack'/'removetrack' event handlers
  220. if (RTCBrowserType.isChrome() || RTCBrowserType.isNWJS()
  221. || RTCBrowserType.isElectron()) {
  222. stream.onaddtrack = event => {
  223. this._remoteTrackAdded(event.target, event.track);
  224. };
  225. stream.onremovetrack = event => {
  226. this._remoteTrackRemoved(event.target, event.track);
  227. };
  228. }
  229. // Call remoteTrackAdded for each track in the stream
  230. const streamAudioTracks = stream.getAudioTracks();
  231. for (const audioTrack of streamAudioTracks) {
  232. this._remoteTrackAdded(stream, audioTrack);
  233. }
  234. const streamVideoTracks = stream.getVideoTracks();
  235. for (const videoTrack of streamVideoTracks) {
  236. this._remoteTrackAdded(stream, videoTrack);
  237. }
  238. };
  239. /**
  240. * Called on "track added" and "stream added" PeerConnection events (because we
  241. * handle streams on per track basis). Finds the owner and the SSRC for
  242. * the track and passes that to ChatRoom for further processing.
  243. * @param {MediaStream} stream the WebRTC MediaStream instance which is
  244. * the parent of the track
  245. * @param {MediaStreamTrack} track the WebRTC MediaStreamTrack added for remote
  246. * participant
  247. */
  248. TraceablePeerConnection.prototype._remoteTrackAdded = function(stream, track) {
  249. const streamId = RTC.getStreamID(stream);
  250. const mediaType = track.kind;
  251. logger.info('Remote track added', streamId, mediaType);
  252. // look up an associated JID for a stream id
  253. if (!mediaType) {
  254. GlobalOnErrorHandler.callErrorHandler(
  255. new Error(
  256. `MediaType undefined for remote track, stream id: ${streamId}`
  257. ));
  258. // Abort
  259. return;
  260. }
  261. const remoteSDP = new SDP(this.remoteDescription.sdp);
  262. const mediaLines
  263. = remoteSDP.media.filter(mls => mls.startsWith(`m=${mediaType}`));
  264. if (!mediaLines.length) {
  265. GlobalOnErrorHandler.callErrorHandler(
  266. new Error(
  267. `No media lines for type ${mediaType
  268. } found in remote SDP for remote track: ${streamId}`));
  269. // Abort
  270. return;
  271. }
  272. let ssrcLines = SDPUtil.findLines(mediaLines[0], 'a=ssrc:');
  273. ssrcLines = ssrcLines.filter(
  274. line => {
  275. const msid
  276. = RTCBrowserType.isTemasysPluginUsed() ? 'mslabel' : 'msid';
  277. return line.indexOf(`${msid}:${streamId}`) !== -1;
  278. });
  279. if (!ssrcLines.length) {
  280. GlobalOnErrorHandler.callErrorHandler(
  281. new Error(
  282. `No SSRC lines for streamId ${streamId
  283. } for remote track, media type: ${mediaType}`));
  284. // Abort
  285. return;
  286. }
  287. // FIXME the length of ssrcLines[0] not verified, but it will fail
  288. // with global error handler anyway
  289. const trackSsrc = ssrcLines[0].substring(7).split(' ')[0];
  290. const ownerEndpointId = this.signalingLayer.getSSRCOwner(trackSsrc);
  291. if (!ownerEndpointId) {
  292. GlobalOnErrorHandler.callErrorHandler(
  293. new Error(
  294. `No SSRC owner known for: ${trackSsrc
  295. } for remote track, msid: ${streamId
  296. } media type: ${mediaType}`));
  297. // Abort
  298. return;
  299. }
  300. logger.log('associated ssrc', ownerEndpointId, trackSsrc);
  301. const peerMediaInfo
  302. = this.signalingLayer.getPeerMediaInfo(ownerEndpointId, mediaType);
  303. if (!peerMediaInfo) {
  304. GlobalOnErrorHandler.callErrorHandler(
  305. new Error(`No peer media info available for: ${ownerEndpointId}`));
  306. // Abort
  307. return;
  308. }
  309. const muted = peerMediaInfo.muted;
  310. const videoType = peerMediaInfo.videoType; // can be undefined
  311. this.rtc._createRemoteTrack(
  312. ownerEndpointId, stream, track, mediaType, videoType, trackSsrc, muted);
  313. };
  314. /**
  315. * Handles remote stream removal.
  316. * @param stream the WebRTC MediaStream object which is being removed from the
  317. * PeerConnection
  318. */
  319. TraceablePeerConnection.prototype._remoteStreamRemoved = function(stream) {
  320. if (!RTC.isUserStream(stream)) {
  321. const id = RTC.getStreamID(stream);
  322. logger.info(
  323. `Ignored remote 'stream removed' event for non-user stream ${id}`);
  324. return;
  325. }
  326. // Call remoteTrackRemoved for each track in the stream
  327. const streamVideoTracks = stream.getVideoTracks();
  328. for (const videoTrack of streamVideoTracks) {
  329. this._remoteTrackRemoved(stream, videoTrack);
  330. }
  331. const streamAudioTracks = stream.getAudioTracks();
  332. for (const audioTrack of streamAudioTracks) {
  333. this._remoteTrackRemoved(stream, audioTrack);
  334. }
  335. };
  336. /**
  337. * Handles remote media track removal.
  338. * @param {MediaStream} stream WebRTC MediaStream instance which is the parent
  339. * of the track.
  340. * @param {MediaStreamTrack} track the WebRTC MediaStreamTrack which has been
  341. * removed from the PeerConnection.
  342. */
  343. TraceablePeerConnection.prototype._remoteTrackRemoved
  344. = function(stream, track) {
  345. const streamId = RTC.getStreamID(stream);
  346. const trackId = track && track.id;
  347. logger.info('Remote track removed', streamId, trackId);
  348. if (!streamId) {
  349. GlobalOnErrorHandler.callErrorHandler(
  350. new Error('Remote track removal failed - no stream ID'));
  351. // Abort
  352. return;
  353. }
  354. if (!trackId) {
  355. GlobalOnErrorHandler.callErrorHandler(
  356. new Error('Remote track removal failed - no track ID'));
  357. // Abort
  358. return;
  359. }
  360. if (!this.rtc._removeRemoteTrack(streamId, trackId)) {
  361. // NOTE this warning is always printed when user leaves the room,
  362. // because we remove remote tracks manually on MUC member left event,
  363. // before the SSRCs are removed by Jicofo. In most cases it is fine to
  364. // ignore this warning, but still it's better to keep it printed for
  365. // debugging purposes.
  366. //
  367. // We could change the behaviour to emit track removed only from here,
  368. // but the order of the events will change and consuming apps could
  369. // behave unexpectedly (the "user left" event would come before "track
  370. // removed" events).
  371. logger.warn(
  372. `Removed track not found for msid: ${streamId},
  373. track id: ${trackId}`);
  374. }
  375. };
  376. /**
  377. * @typedef {Object} SSRCGroupInfo
  378. * @property {Array<number>} ssrcs group's SSRCs
  379. * @property {string} semantics
  380. */
  381. /**
  382. * @typedef {Object} TrackSSRCInfo
  383. * @property {Array<number>} ssrcs track's SSRCs
  384. * @property {Array<SSRCGroupInfo>} groups track's SSRC groups
  385. */
  386. /**
  387. * Returns map with keys msid and <tt>TrackSSRCInfo</tt> values.
  388. * @param {Object} desc the WebRTC SDP instance.
  389. * @return {Map<string,TrackSSRCInfo>}
  390. */
  391. function extractSSRCMap(desc) {
  392. /**
  393. * Track SSRC infos mapped by stream ID (msid)
  394. * @type {Map<string,TrackSSRCInfo>}
  395. */
  396. const ssrcMap = new Map();
  397. /**
  398. * Groups mapped by primary SSRC number
  399. * @type {Map<number,Array<SSRCGroupInfo>>}
  400. */
  401. const groupsMap = new Map();
  402. if (typeof desc !== 'object' || desc === null
  403. || typeof desc.sdp !== 'string') {
  404. logger.warn('An empty description was passed as an argument.');
  405. return ssrcMap;
  406. }
  407. const session = transform.parse(desc.sdp);
  408. if (!Array.isArray(session.media)) {
  409. return ssrcMap;
  410. }
  411. for (const mLine of session.media) {
  412. if (!Array.isArray(mLine.ssrcs)) {
  413. continue; // eslint-disable-line no-continue
  414. }
  415. if (Array.isArray(mLine.ssrcGroups)) {
  416. for (const group of mLine.ssrcGroups) {
  417. if (typeof group.semantics !== 'undefined'
  418. && typeof group.ssrcs !== 'undefined') {
  419. // Parse SSRCs and store as numbers
  420. const groupSSRCs
  421. = group.ssrcs.split(' ')
  422. .map(ssrcStr => parseInt(ssrcStr, 10));
  423. const primarySSRC = groupSSRCs[0];
  424. // Note that group.semantics is already present
  425. group.ssrcs = groupSSRCs;
  426. // eslint-disable-next-line max-depth
  427. if (!groupsMap.has(primarySSRC)) {
  428. groupsMap.set(primarySSRC, []);
  429. }
  430. groupsMap.get(primarySSRC).push(group);
  431. }
  432. }
  433. }
  434. for (const ssrc of mLine.ssrcs) {
  435. if (ssrc.attribute !== 'msid') {
  436. continue; // eslint-disable-line no-continue
  437. }
  438. const msid = ssrc.value;
  439. let ssrcInfo = ssrcMap.get(msid);
  440. if (!ssrcInfo) {
  441. ssrcInfo = {
  442. ssrcs: [],
  443. groups: []
  444. };
  445. ssrcMap.set(msid, ssrcInfo);
  446. }
  447. const ssrcNumber = ssrc.id;
  448. ssrcInfo.ssrcs.push(ssrcNumber);
  449. if (groupsMap.has(ssrcNumber)) {
  450. const ssrcGroups = groupsMap.get(ssrcNumber);
  451. for (const group of ssrcGroups) {
  452. ssrcInfo.groups.push(group);
  453. }
  454. }
  455. }
  456. }
  457. return ssrcMap;
  458. }
  459. /**
  460. * Takes a SessionDescription object and returns a "normalized" version.
  461. * Currently it only takes care of ordering the a=ssrc lines.
  462. */
  463. const normalizePlanB = function(desc) {
  464. if (typeof desc !== 'object' || desc === null
  465. || typeof desc.sdp !== 'string') {
  466. logger.warn('An empty description was passed as an argument.');
  467. return desc;
  468. }
  469. // eslint-disable-next-line no-shadow
  470. const transform = require('sdp-transform');
  471. const session = transform.parse(desc.sdp);
  472. if (typeof session !== 'undefined'
  473. && typeof session.media !== 'undefined'
  474. && Array.isArray(session.media)) {
  475. session.media.forEach(mLine => {
  476. // Chrome appears to be picky about the order in which a=ssrc lines
  477. // are listed in an m-line when rtx is enabled (and thus there are
  478. // a=ssrc-group lines with FID semantics). Specifically if we have
  479. // "a=ssrc-group:FID S1 S2" and the "a=ssrc:S2" lines appear before
  480. // the "a=ssrc:S1" lines, SRD fails.
  481. // So, put SSRC which appear as the first SSRC in an FID ssrc-group
  482. // first.
  483. const firstSsrcs = [];
  484. const newSsrcLines = [];
  485. if (typeof mLine.ssrcGroups !== 'undefined'
  486. && Array.isArray(mLine.ssrcGroups)) {
  487. mLine.ssrcGroups.forEach(group => {
  488. if (typeof group.semantics !== 'undefined'
  489. && group.semantics === 'FID') {
  490. if (typeof group.ssrcs !== 'undefined') {
  491. firstSsrcs.push(Number(group.ssrcs.split(' ')[0]));
  492. }
  493. }
  494. });
  495. }
  496. if (Array.isArray(mLine.ssrcs)) {
  497. let i;
  498. for (i = 0; i < mLine.ssrcs.length; i++) {
  499. if (typeof mLine.ssrcs[i] === 'object'
  500. && typeof mLine.ssrcs[i].id !== 'undefined'
  501. && firstSsrcs.indexOf(mLine.ssrcs[i].id) >= 0) {
  502. newSsrcLines.push(mLine.ssrcs[i]);
  503. delete mLine.ssrcs[i];
  504. }
  505. }
  506. for (i = 0; i < mLine.ssrcs.length; i++) {
  507. if (typeof mLine.ssrcs[i] !== 'undefined') {
  508. newSsrcLines.push(mLine.ssrcs[i]);
  509. }
  510. }
  511. mLine.ssrcs = newSsrcLines;
  512. }
  513. });
  514. }
  515. const resStr = transform.write(session);
  516. return new RTCSessionDescription({
  517. type: desc.type,
  518. sdp: resStr
  519. });
  520. };
  521. const getters = {
  522. signalingState() {
  523. return this.peerconnection.signalingState;
  524. },
  525. iceConnectionState() {
  526. return this.peerconnection.iceConnectionState;
  527. },
  528. localDescription() {
  529. let desc = this.peerconnection.localDescription;
  530. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  531. // if we're running on FF, transform to Plan B first.
  532. if (RTCBrowserType.usesUnifiedPlan()) {
  533. desc = this.interop.toPlanB(desc);
  534. this.trace('getLocalDescription::postTransform (Plan B)',
  535. dumpSDP(desc));
  536. }
  537. return desc;
  538. },
  539. remoteDescription() {
  540. let desc = this.peerconnection.remoteDescription;
  541. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  542. // if we're running on FF, transform to Plan B first.
  543. if (RTCBrowserType.usesUnifiedPlan()) {
  544. desc = this.interop.toPlanB(desc);
  545. this.trace(
  546. 'getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  547. }
  548. return desc;
  549. }
  550. };
  551. Object.keys(getters).forEach(prop => {
  552. Object.defineProperty(
  553. TraceablePeerConnection.prototype,
  554. prop, {
  555. get: getters[prop]
  556. }
  557. );
  558. });
  559. TraceablePeerConnection.prototype.addStream = function(stream, ssrcInfo) {
  560. this.trace('addStream', stream ? stream.id : 'null');
  561. if (stream) {
  562. this.peerconnection.addStream(stream);
  563. }
  564. if (ssrcInfo && ssrcInfo.type === 'addMuted') {
  565. this.sdpConsistency.setPrimarySsrc(ssrcInfo.ssrcs[0]);
  566. const simGroup
  567. = ssrcInfo.groups.find(groupInfo => groupInfo.semantics === 'SIM');
  568. if (simGroup) {
  569. this.simulcast.setSsrcCache(simGroup.ssrcs);
  570. }
  571. const fidGroups
  572. = ssrcInfo.groups.filter(
  573. groupInfo => groupInfo.semantics === 'FID');
  574. if (fidGroups) {
  575. const rtxSsrcMapping = new Map();
  576. fidGroups.forEach(fidGroup => {
  577. const primarySsrc = fidGroup.ssrcs[0];
  578. const rtxSsrc = fidGroup.ssrcs[1];
  579. rtxSsrcMapping.set(primarySsrc, rtxSsrc);
  580. });
  581. this.rtxModifier.setSsrcCache(rtxSsrcMapping);
  582. }
  583. }
  584. };
  585. TraceablePeerConnection.prototype.removeStream = function(stream) {
  586. this.trace('removeStream', stream.id);
  587. // FF doesn't support this yet.
  588. if (this.peerconnection.removeStream) {
  589. this.peerconnection.removeStream(stream);
  590. }
  591. };
  592. TraceablePeerConnection.prototype.createDataChannel = function(label, opts) {
  593. this.trace('createDataChannel', label, opts);
  594. return this.peerconnection.createDataChannel(label, opts);
  595. };
  596. TraceablePeerConnection.prototype.setLocalDescription
  597. = function(description, successCallback, failureCallback) {
  598. let d = description;
  599. this.trace('setLocalDescription::preTransform', dumpSDP(d));
  600. // if we're running on FF, transform to Plan A first.
  601. if (RTCBrowserType.usesUnifiedPlan()) {
  602. d = this.interop.toUnifiedPlan(d);
  603. this.trace(
  604. 'setLocalDescription::postTransform (Plan A)',
  605. dumpSDP(d));
  606. }
  607. const self = this;
  608. this.peerconnection.setLocalDescription(
  609. d,
  610. () => {
  611. self.trace('setLocalDescriptionOnSuccess');
  612. successCallback();
  613. },
  614. err => {
  615. self.trace('setLocalDescriptionOnFailure', err);
  616. self.eventEmitter.emit(
  617. RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  618. err, self.peerconnection);
  619. failureCallback(err);
  620. }
  621. );
  622. };
  623. TraceablePeerConnection.prototype.setRemoteDescription
  624. = function(description, successCallback, failureCallback) {
  625. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  626. // TODO the focus should squeze or explode the remote simulcast
  627. // eslint-disable-next-line no-param-reassign
  628. description = this.simulcast.mungeRemoteDescription(description);
  629. this.trace(
  630. 'setRemoteDescription::postTransform (simulcast)',
  631. dumpSDP(description));
  632. if (this.options.preferH264) {
  633. const parsedSdp = transform.parse(description.sdp);
  634. const videoMLine = parsedSdp.media.find(m => m.type === 'video');
  635. SDPUtil.preferVideoCodec(videoMLine, 'h264');
  636. description.sdp = transform.write(parsedSdp);
  637. }
  638. // if we're running on FF, transform to Plan A first.
  639. if (RTCBrowserType.usesUnifiedPlan()) {
  640. description.sdp = this.rtxModifier.stripRtx(description.sdp);
  641. this.trace(
  642. 'setRemoteDescription::postTransform (stripRtx)',
  643. dumpSDP(description));
  644. // eslint-disable-next-line no-param-reassign
  645. description = this.interop.toUnifiedPlan(description);
  646. this.trace(
  647. 'setRemoteDescription::postTransform (Plan A)',
  648. dumpSDP(description));
  649. }
  650. if (RTCBrowserType.usesPlanB()) {
  651. // eslint-disable-next-line no-param-reassign
  652. description = normalizePlanB(description);
  653. }
  654. this.peerconnection.setRemoteDescription(
  655. description,
  656. () => {
  657. this.trace('setRemoteDescriptionOnSuccess');
  658. successCallback();
  659. },
  660. err => {
  661. this.trace('setRemoteDescriptionOnFailure', err);
  662. this.eventEmitter.emit(
  663. RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  664. err,
  665. this.peerconnection);
  666. failureCallback(err);
  667. });
  668. };
  669. /**
  670. * Makes the underlying TraceablePeerConnection generate new SSRC for
  671. * the recvonly video stream.
  672. * @deprecated
  673. */
  674. TraceablePeerConnection.prototype.generateRecvonlySsrc = function() {
  675. // FIXME replace with SDPUtil.generateSsrc (when it's added)
  676. const newSSRC = this.generateNewStreamSSRCInfo().ssrcs[0];
  677. logger.info(`Generated new recvonly SSRC: ${newSSRC}`);
  678. this.sdpConsistency.setPrimarySsrc(newSSRC);
  679. };
  680. /**
  681. * Makes the underlying TraceablePeerConnection forget the current primary video
  682. * SSRC.
  683. * @deprecated
  684. */
  685. TraceablePeerConnection.prototype.clearRecvonlySsrc = function() {
  686. logger.info('Clearing primary video SSRC!');
  687. this.sdpConsistency.clearSsrcCache();
  688. };
  689. TraceablePeerConnection.prototype.close = function() {
  690. this.trace('stop');
  691. if (!this.rtc._removePeerConnection(this)) {
  692. logger.error('RTC._removePeerConnection returned false');
  693. }
  694. if (this.statsinterval !== null) {
  695. window.clearInterval(this.statsinterval);
  696. this.statsinterval = null;
  697. }
  698. this.peerconnection.close();
  699. };
  700. /**
  701. * Modifies the values of the setup attributes (defined by
  702. * {@link http://tools.ietf.org/html/rfc4145#section-4}) of a specific SDP
  703. * answer in order to overcome a delay of 1 second in the connection
  704. * establishment between Chrome and Videobridge.
  705. *
  706. * @param {SDP} offer - the SDP offer to which the specified SDP answer is
  707. * being prepared to respond
  708. * @param {SDP} answer - the SDP to modify
  709. * @private
  710. */
  711. const _fixAnswerRFC4145Setup = function(offer, answer) {
  712. if (!RTCBrowserType.isChrome()) {
  713. // It looks like Firefox doesn't agree with the fix (at least in its
  714. // current implementation) because it effectively remains active even
  715. // after we tell it to become passive. Apart from Firefox which I tested
  716. // after the fix was deployed, I tested Chrome only. In order to prevent
  717. // issues with other browsers, limit the fix to Chrome for the time
  718. // being.
  719. return;
  720. }
  721. // XXX Videobridge is the (SDP) offerer and WebRTC (e.g. Chrome) is the
  722. // answerer (as orchestrated by Jicofo). In accord with
  723. // http://tools.ietf.org/html/rfc5245#section-5.2 and because both peers
  724. // are ICE FULL agents, Videobridge will take on the controlling role and
  725. // WebRTC will take on the controlled role. In accord with
  726. // https://tools.ietf.org/html/rfc5763#section-5, Videobridge will use the
  727. // setup attribute value of setup:actpass and WebRTC will be allowed to
  728. // choose either the setup attribute value of setup:active or
  729. // setup:passive. Chrome will by default choose setup:active because it is
  730. // RECOMMENDED by the respective RFC since setup:passive adds additional
  731. // latency. The case of setup:active allows WebRTC to send a DTLS
  732. // ClientHello as soon as an ICE connectivity check of its succeeds.
  733. // Unfortunately, Videobridge will be unable to respond immediately because
  734. // may not have WebRTC's answer or may have not completed the ICE
  735. // connectivity establishment. Even more unfortunate is that in the
  736. // described scenario Chrome's DTLS implementation will insist on
  737. // retransmitting its ClientHello after a second (the time is in accord
  738. // with the respective RFC) and will thus cause the whole connection
  739. // establishment to exceed at least 1 second. To work around Chrome's
  740. // idiosyncracy, don't allow it to send a ClientHello i.e. change its
  741. // default choice of setup:active to setup:passive.
  742. if (offer && answer
  743. && offer.media && answer.media
  744. && offer.media.length === answer.media.length) {
  745. answer.media.forEach((a, i) => {
  746. if (SDPUtil.findLine(
  747. offer.media[i],
  748. 'a=setup:actpass',
  749. offer.session)) {
  750. answer.media[i]
  751. = a.replace(/a=setup:active/g, 'a=setup:passive');
  752. }
  753. });
  754. answer.raw = answer.session + answer.media.join('');
  755. }
  756. };
  757. TraceablePeerConnection.prototype.createAnswer
  758. = function(successCallback, failureCallback, constraints) {
  759. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  760. this.peerconnection.createAnswer(
  761. answer => {
  762. try {
  763. this.trace(
  764. 'createAnswerOnSuccess::preTransform', dumpSDP(answer));
  765. // if we're running on FF, transform to Plan A first.
  766. if (RTCBrowserType.usesUnifiedPlan()) {
  767. // eslint-disable-next-line no-param-reassign
  768. answer = this.interop.toPlanB(answer);
  769. this.trace('createAnswerOnSuccess::postTransform (Plan B)',
  770. dumpSDP(answer));
  771. }
  772. /**
  773. * We don't keep ssrcs consitent for Firefox because rewriting
  774. * the ssrcs between createAnswer and setLocalDescription
  775. * breaks the caching in sdp-interop (sdp-interop must
  776. * know about all ssrcs, and it updates its cache in
  777. * toPlanB so if we rewrite them after that, when we
  778. * try and go back to unified plan it will complain
  779. * about unmapped ssrcs)
  780. */
  781. if (!RTCBrowserType.isFirefox()) {
  782. answer.sdp
  783. = this.sdpConsistency.makeVideoPrimarySsrcsConsistent(
  784. answer.sdp);
  785. this.trace(
  786. 'createAnswerOnSuccess::postTransform (make primary'
  787. + ' video ssrcs consistent)',
  788. dumpSDP(answer));
  789. }
  790. // Add simulcast streams if simulcast is enabled
  791. if (!this.options.disableSimulcast
  792. && this.simulcast.isSupported()) {
  793. // eslint-disable-next-line no-param-reassign
  794. answer = this.simulcast.mungeLocalDescription(answer);
  795. this.trace(
  796. 'createAnswerOnSuccess::postTransform (simulcast)',
  797. dumpSDP(answer));
  798. }
  799. if (!this.options.disableRtx && !RTCBrowserType.isFirefox()) {
  800. answer.sdp = this.rtxModifier.modifyRtxSsrcs(answer.sdp);
  801. this.trace(
  802. 'createAnswerOnSuccess::postTransform (rtx modifier)',
  803. dumpSDP(answer));
  804. }
  805. // Fix the setup attribute (see _fixAnswerRFC4145Setup for
  806. // details)
  807. const remoteDescription = new SDP(this.remoteDescription.sdp);
  808. const localDescription = new SDP(answer.sdp);
  809. _fixAnswerRFC4145Setup(remoteDescription, localDescription);
  810. answer.sdp = localDescription.raw;
  811. this.eventEmitter.emit(RTCEvents.SENDRECV_STREAMS_CHANGED,
  812. extractSSRCMap(answer));
  813. successCallback(answer);
  814. } catch (e) {
  815. this.trace('createAnswerOnError', e);
  816. this.trace('createAnswerOnError', dumpSDP(answer));
  817. logger.error('createAnswerOnError', e, dumpSDP(answer));
  818. failureCallback(e);
  819. }
  820. },
  821. err => {
  822. this.trace('createAnswerOnFailure', err);
  823. this.eventEmitter.emit(RTCEvents.CREATE_ANSWER_FAILED, err,
  824. this.peerconnection);
  825. failureCallback(err);
  826. },
  827. constraints);
  828. };
  829. TraceablePeerConnection.prototype.addIceCandidate
  830. // eslint-disable-next-line no-unused-vars
  831. = function(candidate, successCallback, failureCallback) {
  832. // var self = this;
  833. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  834. this.peerconnection.addIceCandidate(candidate);
  835. /* maybe later
  836. this.peerconnection.addIceCandidate(candidate,
  837. function () {
  838. self.trace('addIceCandidateOnSuccess');
  839. successCallback();
  840. },
  841. function (err) {
  842. self.trace('addIceCandidateOnFailure', err);
  843. failureCallback(err);
  844. }
  845. );
  846. */
  847. };
  848. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  849. // TODO: Is this the correct way to handle Opera, Temasys?
  850. if (RTCBrowserType.isFirefox()
  851. || RTCBrowserType.isTemasysPluginUsed()
  852. || RTCBrowserType.isReactNative()) {
  853. this.peerconnection.getStats(
  854. null,
  855. callback,
  856. errback || (() => {
  857. // Making sure that getStats won't fail if error callback is
  858. // not passed.
  859. }));
  860. } else {
  861. this.peerconnection.getStats(callback);
  862. }
  863. };
  864. /**
  865. * Generate ssrc info object for a stream with the following properties:
  866. * - ssrcs - Array of the ssrcs associated with the stream.
  867. * - groups - Array of the groups associated with the stream.
  868. */
  869. TraceablePeerConnection.prototype.generateNewStreamSSRCInfo = function() {
  870. let ssrcInfo = { ssrcs: [],
  871. groups: [] };
  872. if (!this.options.disableSimulcast
  873. && this.simulcast.isSupported()) {
  874. for (let i = 0; i < SIMULCAST_LAYERS; i++) {
  875. ssrcInfo.ssrcs.push(SDPUtil.generateSsrc());
  876. }
  877. ssrcInfo.groups.push({
  878. ssrcs: ssrcInfo.ssrcs.slice(),
  879. semantics: 'SIM'
  880. });
  881. } else {
  882. ssrcInfo = {
  883. ssrcs: [ SDPUtil.generateSsrc() ],
  884. groups: []
  885. };
  886. }
  887. if (!this.options.disableRtx && !RTCBrowserType.isFirefox()) {
  888. // Specifically use a for loop here because we'll
  889. // be adding to the list we're iterating over, so we
  890. // only want to iterate through the items originally
  891. // on the list
  892. const currNumSsrcs = ssrcInfo.ssrcs.length;
  893. for (let i = 0; i < currNumSsrcs; ++i) {
  894. const primarySsrc = ssrcInfo.ssrcs[i];
  895. const rtxSsrc = SDPUtil.generateSsrc();
  896. ssrcInfo.ssrcs.push(rtxSsrc);
  897. ssrcInfo.groups.push({
  898. ssrcs: [ primarySsrc, rtxSsrc ],
  899. semantics: 'FID'
  900. });
  901. }
  902. }
  903. return ssrcInfo;
  904. };
  905. module.exports = TraceablePeerConnection;