Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TraceablePeerConnection.js 35KB

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