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

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