您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TraceablePeerConnection.js 34KB

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