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

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