Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JingleSessionPC.js 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /* jshint -W117 */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JingleSession = require("./JingleSession");
  4. var TraceablePeerConnection = require("./TraceablePeerConnection");
  5. var SDPDiffer = require("./SDPDiffer");
  6. var SDPUtil = require("./SDPUtil");
  7. var SDP = require("./SDP");
  8. var async = require("async");
  9. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  10. var RTCBrowserType = require("../RTC/RTCBrowserType");
  11. var RTC = require("../RTC/RTC");
  12. /**
  13. * Constant tells how long we're going to wait for IQ response, before timeout
  14. * error is triggered.
  15. * @type {number}
  16. */
  17. const IQ_TIMEOUT = 10000;
  18. // Jingle stuff
  19. function JingleSessionPC(me, sid, peerjid, connection,
  20. media_constraints, ice_config, service, eventEmitter) {
  21. JingleSession.call(this, me, sid, peerjid, connection,
  22. media_constraints, ice_config, service, eventEmitter);
  23. this.localSDP = null;
  24. this.remoteSDP = null;
  25. this.hadstuncandidate = false;
  26. this.hadturncandidate = false;
  27. this.lasticecandidate = false;
  28. this.addssrc = [];
  29. this.removessrc = [];
  30. this.pendingop = null;
  31. this.modifyingLocalStreams = false;
  32. this.modifiedSSRCs = {};
  33. /**
  34. * A map that stores SSRCs of remote streams. And is used only locally
  35. * We store the mapping when jingle is received, and later is used
  36. * onaddstream webrtc event where we have only the ssrc
  37. * FIXME: This map got filled and never cleaned and can grow durring long
  38. * conference
  39. * @type {{}} maps SSRC number to jid
  40. */
  41. this.ssrcOwners = {};
  42. this.webrtcIceUdpDisable = !!this.service.options.webrtcIceUdpDisable;
  43. this.webrtcIceTcpDisable = !!this.service.options.webrtcIceTcpDisable;
  44. this.modifySourcesQueue = async.queue(this._modifySources.bind(this), 1);
  45. // We start with the queue paused. We resume it when the signaling state is
  46. // stable and the ice connection state is connected.
  47. this.modifySourcesQueue.pause();
  48. }
  49. //XXX this is badly broken...
  50. JingleSessionPC.prototype = JingleSession.prototype;
  51. JingleSessionPC.prototype.constructor = JingleSessionPC;
  52. JingleSessionPC.prototype.updateModifySourcesQueue = function() {
  53. var signalingState = this.peerconnection.signalingState;
  54. var iceConnectionState = this.peerconnection.iceConnectionState;
  55. if (signalingState === 'stable' && iceConnectionState === 'connected') {
  56. this.modifySourcesQueue.resume();
  57. } else {
  58. this.modifySourcesQueue.pause();
  59. }
  60. };
  61. JingleSessionPC.prototype.doInitialize = function () {
  62. var self = this;
  63. this.hadstuncandidate = false;
  64. this.hadturncandidate = false;
  65. this.lasticecandidate = false;
  66. // True if reconnect is in progress
  67. this.isreconnect = false;
  68. // Set to true if the connection was ever stable
  69. this.wasstable = false;
  70. this.peerconnection = new TraceablePeerConnection(
  71. this.connection.jingle.ice_config,
  72. RTC.getPCConstraints(),
  73. this);
  74. this.peerconnection.onicecandidate = function (ev) {
  75. if (!ev) {
  76. // There was an incomplete check for ev before which left the last
  77. // line of the function unprotected from a potential throw of an
  78. // exception. Consequently, it may be argued that the check is
  79. // unnecessary. Anyway, I'm leaving it and making the check
  80. // complete.
  81. return;
  82. }
  83. var candidate = ev.candidate;
  84. if (candidate) {
  85. // Discard candidates of disabled protocols.
  86. var protocol = candidate.protocol;
  87. if (typeof protocol === 'string') {
  88. protocol = protocol.toLowerCase();
  89. if (protocol == 'tcp') {
  90. if (self.webrtcIceTcpDisable)
  91. return;
  92. } else if (protocol == 'udp') {
  93. if (self.webrtcIceUdpDisable)
  94. return;
  95. }
  96. }
  97. }
  98. self.sendIceCandidate(candidate);
  99. };
  100. this.peerconnection.onaddstream = function (event) {
  101. if (event.stream.id !== 'default') {
  102. logger.log("REMOTE STREAM ADDED: ", event.stream , event.stream.id);
  103. self.remoteStreamAdded(event);
  104. } else {
  105. // This is a recvonly stream. Clients that implement Unified Plan,
  106. // such as Firefox use recvonly "streams/channels/tracks" for
  107. // receiving remote stream/tracks, as opposed to Plan B where there
  108. // are only 3 channels: audio, video and data.
  109. logger.log("RECVONLY REMOTE STREAM IGNORED: " + event.stream + " - " + event.stream.id);
  110. }
  111. };
  112. this.peerconnection.onremovestream = function (event) {
  113. // Remove the stream from remoteStreams
  114. if (event.stream.id !== 'default') {
  115. logger.log("REMOTE STREAM REMOVED: ", event.stream , event.stream.id);
  116. self.remoteStreamRemoved(event);
  117. } else {
  118. // This is a recvonly stream. Clients that implement Unified Plan,
  119. // such as Firefox use recvonly "streams/channels/tracks" for
  120. // receiving remote stream/tracks, as opposed to Plan B where there
  121. // are only 3 channels: audio, video and data.
  122. logger.log("RECVONLY REMOTE STREAM IGNORED: " + event.stream + " - " + event.stream.id);
  123. }
  124. };
  125. this.peerconnection.onsignalingstatechange = function (event) {
  126. if (!(self && self.peerconnection)) return;
  127. if (self.peerconnection.signalingState === 'stable') {
  128. self.wasstable = true;
  129. }
  130. self.updateModifySourcesQueue();
  131. };
  132. /**
  133. * The oniceconnectionstatechange event handler contains the code to execute when the iceconnectionstatechange event,
  134. * of type Event, is received by this RTCPeerConnection. Such an event is sent when the value of
  135. * RTCPeerConnection.iceConnectionState changes.
  136. *
  137. * @param event the event containing information about the change
  138. */
  139. this.peerconnection.oniceconnectionstatechange = function (event) {
  140. if (!(self && self.peerconnection)) return;
  141. logger.log("(TIME) ICE " + self.peerconnection.iceConnectionState +
  142. ":\t", window.performance.now());
  143. self.updateModifySourcesQueue();
  144. switch (self.peerconnection.iceConnectionState) {
  145. case 'connected':
  146. // Informs interested parties that the connection has been restored.
  147. if (self.peerconnection.signalingState === 'stable' && self.isreconnect)
  148. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_RESTORED);
  149. self.isreconnect = false;
  150. break;
  151. case 'disconnected':
  152. self.isreconnect = true;
  153. // Informs interested parties that the connection has been interrupted.
  154. if (self.wasstable)
  155. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_INTERRUPTED);
  156. break;
  157. case 'failed':
  158. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  159. break;
  160. }
  161. };
  162. this.peerconnection.onnegotiationneeded = function (event) {
  163. self.room.eventEmitter.emit(XMPPEvents.PEERCONNECTION_READY, self);
  164. };
  165. };
  166. JingleSessionPC.prototype.sendIceCandidate = function (candidate) {
  167. var self = this;
  168. if (candidate && !this.lasticecandidate) {
  169. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  170. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  171. if (!(ice && jcand)) {
  172. logger.error('failed to get ice && jcand');
  173. return;
  174. }
  175. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  176. if (jcand.type === 'srflx') {
  177. this.hadstuncandidate = true;
  178. } else if (jcand.type === 'relay') {
  179. this.hadturncandidate = true;
  180. }
  181. if (this.usedrip) {
  182. if (this.drip_container.length === 0) {
  183. // start 20ms callout
  184. window.setTimeout(function () {
  185. if (self.drip_container.length === 0) return;
  186. self.sendIceCandidates(self.drip_container);
  187. self.drip_container = [];
  188. }, 20);
  189. }
  190. this.drip_container.push(candidate);
  191. } else {
  192. self.sendIceCandidates([candidate]);
  193. }
  194. } else {
  195. logger.log('sendIceCandidate: last candidate.');
  196. // FIXME: remember to re-think in ICE-restart
  197. this.lasticecandidate = true;
  198. logger.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  199. logger.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  200. }
  201. };
  202. JingleSessionPC.prototype.sendIceCandidates = function (candidates) {
  203. logger.log('sendIceCandidates', candidates);
  204. var cand = $iq({to: this.peerjid, type: 'set'})
  205. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  206. action: 'transport-info',
  207. initiator: this.initiator,
  208. sid: this.sid});
  209. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  210. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  211. var mline = SDPUtil.parse_mline(this.localSDP.media[mid].split('\r\n')[0]);
  212. if (cands.length > 0) {
  213. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  214. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  215. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  216. name: (cands[0].sdpMid? cands[0].sdpMid : mline.media)
  217. }).c('transport', ice);
  218. for (var i = 0; i < cands.length; i++) {
  219. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  220. }
  221. // add fingerprint
  222. var fingerprint_line = SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session);
  223. if (fingerprint_line) {
  224. var tmp = SDPUtil.parse_fingerprint(fingerprint_line);
  225. tmp.required = true;
  226. cand.c(
  227. 'fingerprint',
  228. {xmlns: 'urn:xmpp:jingle:apps:dtls:0'})
  229. .t(tmp.fingerprint);
  230. delete tmp.fingerprint;
  231. cand.attrs(tmp);
  232. cand.up();
  233. }
  234. cand.up(); // transport
  235. cand.up(); // content
  236. }
  237. }
  238. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  239. //logger.log('was this the last candidate', this.lasticecandidate);
  240. this.connection.sendIQ(
  241. cand, null, this.newJingleErrorHandler(cand), IQ_TIMEOUT);
  242. };
  243. JingleSessionPC.prototype.readSsrcInfo = function (contents) {
  244. var self = this;
  245. $(contents).each(function (idx, content) {
  246. var name = $(content).attr('name');
  247. var mediaType = this.getAttribute('name');
  248. var ssrcs = $(content).find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  249. ssrcs.each(function () {
  250. var ssrc = this.getAttribute('ssrc');
  251. $(this).find('>ssrc-info[xmlns="http://jitsi.org/jitmeet"]').each(
  252. function () {
  253. var owner = this.getAttribute('owner');
  254. self.ssrcOwners[ssrc] = owner;
  255. }
  256. );
  257. });
  258. });
  259. };
  260. JingleSessionPC.prototype.acceptOffer = function(jingleOffer,
  261. success, failure) {
  262. this.state = 'active';
  263. this.setRemoteDescription(jingleOffer, 'offer',
  264. function() {
  265. this.sendAnswer(success, failure);
  266. }.bind(this),
  267. failure);
  268. };
  269. JingleSessionPC.prototype.setRemoteDescription = function (elem, desctype,
  270. success, failure) {
  271. //logger.log('setting remote description... ', desctype);
  272. this.remoteSDP = new SDP('');
  273. if (this.webrtcIceTcpDisable) {
  274. this.remoteSDP.removeTcpCandidates = true;
  275. }
  276. if (this.webrtcIceUdpDisable) {
  277. this.remoteSDP.removeUdpCandidates = true;
  278. }
  279. this.remoteSDP.fromJingle(elem);
  280. this.readSsrcInfo($(elem).find(">content"));
  281. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  282. this.peerconnection.setRemoteDescription(remotedesc,
  283. function () {
  284. //logger.log('setRemoteDescription success');
  285. if (success) {
  286. success();
  287. }
  288. },
  289. function (e) {
  290. logger.error('setRemoteDescription error', e);
  291. if (failure)
  292. failure(e);
  293. JingleSessionPC.onJingleFatalError(this, e);
  294. }.bind(this)
  295. );
  296. };
  297. JingleSessionPC.prototype.sendAnswer = function (success, failure) {
  298. //logger.log('createAnswer');
  299. this.peerconnection.createAnswer(
  300. function (sdp) {
  301. this.createdAnswer(sdp, success, failure);
  302. }.bind(this),
  303. function (error) {
  304. logger.error("createAnswer failed", error);
  305. if (failure)
  306. failure(error);
  307. this.room.eventEmitter.emit(
  308. XMPPEvents.CONFERENCE_SETUP_FAILED, error);
  309. }.bind(this),
  310. this.media_constraints
  311. );
  312. };
  313. JingleSessionPC.prototype.createdAnswer = function (sdp, success, failure) {
  314. //logger.log('createAnswer callback');
  315. const self = this;
  316. this.localSDP = new SDP(sdp.sdp);
  317. var sendJingle = function (ssrcs) {
  318. var accept = $iq({to: self.peerjid,
  319. type: 'set'})
  320. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  321. action: 'session-accept',
  322. initiator: self.initiator,
  323. responder: self.responder,
  324. sid: self.sid });
  325. if (self.webrtcIceTcpDisable) {
  326. self.localSDP.removeTcpCandidates = true;
  327. }
  328. if (self.webrtcIceUdpDisable) {
  329. self.localSDP.removeUdpCandidates = true;
  330. }
  331. self.localSDP.toJingle(
  332. accept,
  333. self.initiator == self.me ? 'initiator' : 'responder',
  334. ssrcs);
  335. self.fixJingle(accept);
  336. self.connection.sendIQ(accept,
  337. success,
  338. self.newJingleErrorHandler(accept, failure),
  339. IQ_TIMEOUT);
  340. };
  341. sdp.sdp = this.localSDP.raw;
  342. this.peerconnection.setLocalDescription(sdp,
  343. function () {
  344. //logger.log('setLocalDescription success');
  345. sendJingle(success, failure);
  346. },
  347. function (error) {
  348. logger.error('setLocalDescription failed', error);
  349. if (failure)
  350. failure(error);
  351. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  352. }
  353. );
  354. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  355. for (var j = 0; j < cands.length; j++) {
  356. var cand = SDPUtil.parse_icecandidate(cands[j]);
  357. if (cand.type == 'srflx') {
  358. this.hadstuncandidate = true;
  359. } else if (cand.type == 'relay') {
  360. this.hadturncandidate = true;
  361. }
  362. }
  363. };
  364. JingleSessionPC.prototype.terminate = function (reason, text,
  365. success, failure) {
  366. var term = $iq({to: this.peerjid,
  367. type: 'set'})
  368. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  369. action: 'session-terminate',
  370. initiator: this.initiator,
  371. sid: this.sid})
  372. .c('reason')
  373. .c(reason || 'success');
  374. if (text) {
  375. term.up().c('text').t(text);
  376. }
  377. this.connection.sendIQ(
  378. term, success, this.newJingleErrorHandler(term, failure), IQ_TIMEOUT);
  379. // this should result in 'onTerminated' being called by strope.jingle.js
  380. this.connection.jingle.terminate(this.sid);
  381. };
  382. JingleSessionPC.prototype.onTerminated = function (reasonCondition,
  383. reasonText) {
  384. this.state = 'ended';
  385. // Do something with reason and reasonCondition when we start to care
  386. //this.reasonCondition = reasonCondition;
  387. //this.reasonText = reasonText;
  388. logger.info("Session terminated", this, reasonCondition, reasonText);
  389. if (this.peerconnection)
  390. this.peerconnection.close();
  391. };
  392. /**
  393. * Handles a Jingle source-add message for this Jingle session.
  394. * @param elem An array of Jingle "content" elements.
  395. */
  396. JingleSessionPC.prototype.addSource = function (elem) {
  397. var self = this;
  398. // FIXME: dirty waiting
  399. if (!this.peerconnection.localDescription)
  400. {
  401. logger.warn("addSource - localDescription not ready yet")
  402. setTimeout(function()
  403. {
  404. self.addSource(elem);
  405. },
  406. 200
  407. );
  408. return;
  409. }
  410. logger.log('addssrc', new Date().getTime());
  411. logger.log('ice', this.peerconnection.iceConnectionState);
  412. this.readSsrcInfo(elem);
  413. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  414. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  415. $(elem).each(function (idx, content) {
  416. var name = $(content).attr('name');
  417. var lines = '';
  418. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  419. var semantics = this.getAttribute('semantics');
  420. var ssrcs = $(this).find('>source').map(function () {
  421. return this.getAttribute('ssrc');
  422. }).get();
  423. if (ssrcs.length) {
  424. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  425. }
  426. });
  427. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  428. tmp.each(function () {
  429. var ssrc = $(this).attr('ssrc');
  430. if(mySdp.containsSSRC(ssrc)){
  431. /**
  432. * This happens when multiple participants change their streams at the same time and
  433. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  434. * addssrc are scheduled for update IQ. See
  435. */
  436. logger.warn("Got add stream request for my own ssrc: "+ssrc);
  437. return;
  438. }
  439. if (sdp.containsSSRC(ssrc)) {
  440. logger.warn("Source-add request for existing SSRC: " + ssrc);
  441. return;
  442. }
  443. $(this).find('>parameter').each(function () {
  444. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  445. if ($(this).attr('value') && $(this).attr('value').length)
  446. lines += ':' + $(this).attr('value');
  447. lines += '\r\n';
  448. });
  449. });
  450. sdp.media.forEach(function(media, idx) {
  451. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  452. return;
  453. sdp.media[idx] += lines;
  454. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  455. self.addssrc[idx] += lines;
  456. });
  457. sdp.raw = sdp.session + sdp.media.join('');
  458. });
  459. this.modifySourcesQueue.push(function() {
  460. // When a source is added and if this is FF, a new channel is allocated
  461. // for receiving the added source. We need to diffuse the SSRC of this
  462. // new recvonly channel to the rest of the peers.
  463. logger.log('modify sources done');
  464. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  465. logger.log("SDPs", mySdp, newSdp);
  466. self.notifyMySSRCUpdate(mySdp, newSdp);
  467. });
  468. };
  469. /**
  470. * Handles a Jingle source-remove message for this Jingle session.
  471. * @param elem An array of Jingle "content" elements.
  472. */
  473. JingleSessionPC.prototype.removeSource = function (elem) {
  474. var self = this;
  475. // FIXME: dirty waiting
  476. if (!this.peerconnection.localDescription) {
  477. logger.warn("removeSource - localDescription not ready yet");
  478. setTimeout(function() {
  479. self.removeSource(elem);
  480. },
  481. 200
  482. );
  483. return;
  484. }
  485. logger.log('removessrc', new Date().getTime());
  486. logger.log('ice', this.peerconnection.iceConnectionState);
  487. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  488. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  489. $(elem).each(function (idx, content) {
  490. var name = $(content).attr('name');
  491. var lines = '';
  492. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  493. var semantics = this.getAttribute('semantics');
  494. var ssrcs = $(this).find('>source').map(function () {
  495. return this.getAttribute('ssrc');
  496. }).get();
  497. if (ssrcs.length) {
  498. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  499. }
  500. });
  501. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  502. tmp.each(function () {
  503. var ssrc = $(this).attr('ssrc');
  504. // This should never happen, but can be useful for bug detection
  505. if(mySdp.containsSSRC(ssrc)){
  506. logger.error("Got remove stream request for my own ssrc: "+ssrc);
  507. return;
  508. }
  509. $(this).find('>parameter').each(function () {
  510. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  511. if ($(this).attr('value') && $(this).attr('value').length)
  512. lines += ':' + $(this).attr('value');
  513. lines += '\r\n';
  514. });
  515. });
  516. sdp.media.forEach(function(media, idx) {
  517. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  518. return;
  519. sdp.media[idx] += lines;
  520. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  521. self.removessrc[idx] += lines;
  522. });
  523. sdp.raw = sdp.session + sdp.media.join('');
  524. });
  525. this.modifySourcesQueue.push(function() {
  526. // When a source is removed and if this is FF, the recvonly channel that
  527. // receives the remote stream is deactivated . We need to diffuse the
  528. // recvonly SSRC removal to the rest of the peers.
  529. logger.log('modify sources done');
  530. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  531. logger.log("SDPs", mySdp, newSdp);
  532. self.notifyMySSRCUpdate(mySdp, newSdp);
  533. });
  534. };
  535. JingleSessionPC.prototype._modifySources = function (successCallback, queueCallback) {
  536. var self = this;
  537. if (this.peerconnection.signalingState == 'closed') return;
  538. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null
  539. || this.modifyingLocalStreams)){
  540. // There is nothing to do since scheduled job might have been
  541. // executed by another succeeding call
  542. if(successCallback){
  543. successCallback();
  544. }
  545. queueCallback();
  546. return;
  547. }
  548. // Reset switch streams flags
  549. this.modifyingLocalStreams = false;
  550. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  551. // add sources
  552. this.addssrc.forEach(function(lines, idx) {
  553. sdp.media[idx] += lines;
  554. });
  555. this.addssrc = [];
  556. // remove sources
  557. this.removessrc.forEach(function(lines, idx) {
  558. lines = lines.split('\r\n');
  559. lines.pop(); // remove empty last element;
  560. lines.forEach(function(line) {
  561. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  562. });
  563. });
  564. this.removessrc = [];
  565. sdp.raw = sdp.session + sdp.media.join('');
  566. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  567. function() {
  568. if(self.signalingState == 'closed') {
  569. logger.error("createAnswer attempt on closed state");
  570. queueCallback("createAnswer attempt on closed state");
  571. return;
  572. }
  573. self.peerconnection.createAnswer(
  574. function(modifiedAnswer) {
  575. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  576. if (self.pendingop !== null) {
  577. var sdp = new SDP(modifiedAnswer.sdp);
  578. if (sdp.media.length > 1) {
  579. switch(self.pendingop) {
  580. case 'mute':
  581. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  582. break;
  583. case 'unmute':
  584. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  585. break;
  586. }
  587. sdp.raw = sdp.session + sdp.media.join('');
  588. modifiedAnswer.sdp = sdp.raw;
  589. }
  590. self.pendingop = null;
  591. }
  592. // FIXME: pushing down an answer while ice connection state
  593. // is still checking is bad...
  594. //logger.log(self.peerconnection.iceConnectionState);
  595. // trying to work around another chrome bug
  596. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  597. self.peerconnection.setLocalDescription(modifiedAnswer,
  598. function() {
  599. if(successCallback){
  600. successCallback();
  601. }
  602. queueCallback();
  603. },
  604. function(error) {
  605. logger.error('modified setLocalDescription failed', error);
  606. queueCallback(error);
  607. }
  608. );
  609. },
  610. function(error) {
  611. logger.error('modified answer failed', error);
  612. queueCallback(error);
  613. }
  614. );
  615. },
  616. function(error) {
  617. logger.error('modify failed', error);
  618. queueCallback(error);
  619. }
  620. );
  621. };
  622. /**
  623. * Adds stream.
  624. * @param stream new stream that will be added.
  625. * @param success_callback callback executed after successful stream addition.
  626. * @param ssrcInfo object with information about the SSRCs associated with the
  627. * stream.
  628. * @param dontModifySources {boolean} if true _modifySources won't be called.
  629. * Used for streams added before the call start.
  630. */
  631. JingleSessionPC.prototype.addStream = function (stream, callback, ssrcInfo,
  632. dontModifySources) {
  633. // Remember SDP to figure out added/removed SSRCs
  634. var oldSdp = null;
  635. if(this.peerconnection) {
  636. if(this.peerconnection.localDescription) {
  637. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  638. }
  639. //when adding muted stream we have to pass the ssrcInfo but we don't
  640. //have a stream
  641. if(stream || ssrcInfo)
  642. this.peerconnection.addStream(stream, ssrcInfo);
  643. }
  644. // Conference is not active
  645. if(!oldSdp || !this.peerconnection || dontModifySources) {
  646. if(ssrcInfo) {
  647. //available only on video unmute or when adding muted stream
  648. this.modifiedSSRCs[ssrcInfo.type] =
  649. this.modifiedSSRCs[ssrcInfo.type] || [];
  650. this.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  651. }
  652. callback();
  653. return;
  654. }
  655. this.modifyingLocalStreams = true;
  656. var self = this;
  657. this.modifySourcesQueue.push(function() {
  658. logger.log('modify sources done');
  659. if(ssrcInfo) {
  660. //available only on video unmute or when adding muted stream
  661. self.modifiedSSRCs[ssrcInfo.type] =
  662. self.modifiedSSRCs[ssrcInfo.type] || [];
  663. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  664. }
  665. callback();
  666. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  667. logger.log("SDPs", oldSdp, newSdp);
  668. self.notifyMySSRCUpdate(oldSdp, newSdp);
  669. });
  670. }
  671. /**
  672. * Generate ssrc info object for a stream with the following properties:
  673. * - ssrcs - Array of the ssrcs associated with the stream.
  674. * - groups - Array of the groups associated with the stream.
  675. */
  676. JingleSessionPC.prototype.generateNewStreamSSRCInfo = function () {
  677. return this.peerconnection.generateNewStreamSSRCInfo();
  678. };
  679. /**
  680. * Remove streams.
  681. * @param stream stream that will be removed.
  682. * @param success_callback callback executed after successful stream addition.
  683. * @param ssrcInfo object with information about the SSRCs associated with the
  684. * stream.
  685. */
  686. JingleSessionPC.prototype.removeStream = function (stream, callback, ssrcInfo) {
  687. // Remember SDP to figure out added/removed SSRCs
  688. var oldSdp = null;
  689. if(this.peerconnection) {
  690. if(this.peerconnection.localDescription) {
  691. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  692. }
  693. if (RTCBrowserType.getBrowserType() ===
  694. RTCBrowserType.RTC_BROWSER_FIREFOX) {
  695. if(!stream)//There is nothing to be changed
  696. return;
  697. var sender = null;
  698. // On Firefox we don't replace MediaStreams as this messes up the
  699. // m-lines (which can't be removed in Plan Unified) and brings a lot
  700. // of complications. Instead, we use the RTPSender and remove just
  701. // the track.
  702. var track = null;
  703. if(stream.getAudioTracks() && stream.getAudioTracks().length) {
  704. track = stream.getAudioTracks()[0];
  705. } else if(stream.getVideoTracks() && stream.getVideoTracks().length)
  706. {
  707. track = stream.getVideoTracks()[0];
  708. }
  709. if(!track) {
  710. logger.log("Cannot remove tracks: no tracks.");
  711. return;
  712. }
  713. // Find the right sender (for audio or video)
  714. this.peerconnection.peerconnection.getSenders().some(function (s) {
  715. if (s.track === track) {
  716. sender = s;
  717. return true;
  718. }
  719. });
  720. if (sender) {
  721. this.peerconnection.peerconnection.removeTrack(sender);
  722. } else {
  723. logger.log("Cannot remove tracks: no RTPSender.");
  724. }
  725. } else if(stream)
  726. this.peerconnection.removeStream(stream, false, ssrcInfo);
  727. // else
  728. // NOTE: If there is no stream and the browser is not FF we still need to do
  729. // some transformation in order to send remove-source for the muted
  730. // streams. That's why we aren't calling return here.
  731. }
  732. // Conference is not active
  733. if(!oldSdp || !this.peerconnection) {
  734. callback();
  735. return;
  736. }
  737. this.modifyingLocalStreams = true;
  738. var self = this;
  739. this.modifySourcesQueue.push(function() {
  740. logger.log('modify sources done');
  741. callback();
  742. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  743. if(ssrcInfo) {
  744. self.modifiedSSRCs[ssrcInfo.type] =
  745. self.modifiedSSRCs[ssrcInfo.type] || [];
  746. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  747. }
  748. logger.log("SDPs", oldSdp, newSdp);
  749. self.notifyMySSRCUpdate(oldSdp, newSdp);
  750. });
  751. }
  752. /**
  753. * Figures out added/removed ssrcs and send update IQs.
  754. * @param old_sdp SDP object for old description.
  755. * @param new_sdp SDP object for new description.
  756. */
  757. JingleSessionPC.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  758. if (!(this.peerconnection.signalingState == 'stable' &&
  759. this.peerconnection.iceConnectionState == 'connected')){
  760. logger.log("Too early to send updates");
  761. return;
  762. }
  763. // send source-remove IQ.
  764. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  765. var remove = $iq({to: this.peerjid, type: 'set'})
  766. .c('jingle', {
  767. xmlns: 'urn:xmpp:jingle:1',
  768. action: 'source-remove',
  769. initiator: this.initiator,
  770. sid: this.sid
  771. }
  772. );
  773. sdpDiffer.toJingle(remove);
  774. var removed = this.fixJingle(remove);
  775. if (removed && remove) {
  776. logger.info("Sending source-remove", remove.tree());
  777. this.connection.sendIQ(
  778. remove, null, this.newJingleErrorHandler(remove), IQ_TIMEOUT);
  779. } else {
  780. logger.log('removal not necessary');
  781. }
  782. // send source-add IQ.
  783. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  784. var add = $iq({to: this.peerjid, type: 'set'})
  785. .c('jingle', {
  786. xmlns: 'urn:xmpp:jingle:1',
  787. action: 'source-add',
  788. initiator: this.initiator,
  789. sid: this.sid
  790. }
  791. );
  792. sdpDiffer.toJingle(add);
  793. var added = this.fixJingle(add);
  794. if (added && add) {
  795. logger.info("Sending source-add", add.tree());
  796. this.connection.sendIQ(
  797. add, null, this.newJingleErrorHandler(add), IQ_TIMEOUT);
  798. } else {
  799. logger.log('addition not necessary');
  800. }
  801. };
  802. JingleSessionPC.prototype.newJingleErrorHandler = function(request, failureCb) {
  803. return function (errResponse) {
  804. var error = { };
  805. // Get XMPP error code and condition(reason)
  806. const errorElSel = $(errResponse).find('error');
  807. if (errorElSel.length) {
  808. error.code = errorElSel.attr('code');
  809. const errorReasonSel = $(errResponse).find('error :first');
  810. if (errorReasonSel.length)
  811. error.reason = errorReasonSel[0].tagName;
  812. }
  813. error.source = request ? request.tree() : null;
  814. error.session = this;
  815. logger.error("Jingle error", error);
  816. if (failureCb) {
  817. failureCb(error);
  818. }
  819. this.room.eventEmitter.emit(XMPPEvents.JINGLE_ERROR, error);
  820. }.bind(this);
  821. };
  822. JingleSessionPC.onJingleFatalError = function (session, error)
  823. {
  824. this.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  825. this.room.eventEmitter.emit(XMPPEvents.JINGLE_FATAL_ERROR, session, error);
  826. };
  827. JingleSessionPC.prototype.remoteStreamAdded = function (data, times) {
  828. var self = this;
  829. var thessrc;
  830. var streamId = RTC.getStreamID(data.stream);
  831. // look up an associated JID for a stream id
  832. if (!streamId) {
  833. logger.error("No stream ID for", data.stream);
  834. } else if (streamId && streamId.indexOf('mixedmslabel') === -1) {
  835. // look only at a=ssrc: and _not_ at a=ssrc-group: lines
  836. var ssrclines = this.peerconnection.remoteDescription?
  837. SDPUtil.find_lines(this.peerconnection.remoteDescription.sdp, 'a=ssrc:') : [];
  838. ssrclines = ssrclines.filter(function (line) {
  839. // NOTE(gp) previously we filtered on the mslabel, but that property
  840. // is not always present.
  841. // return line.indexOf('mslabel:' + data.stream.label) !== -1;
  842. if (RTCBrowserType.isTemasysPluginUsed()) {
  843. return ((line.indexOf('mslabel:' + streamId) !== -1));
  844. } else {
  845. return ((line.indexOf('msid:' + streamId) !== -1));
  846. }
  847. });
  848. if (ssrclines.length) {
  849. thessrc = ssrclines[0].substring(7).split(' ')[0];
  850. if (!self.ssrcOwners[thessrc]) {
  851. logger.error("No SSRC owner known for: " + thessrc);
  852. return;
  853. }
  854. data.peerjid = self.ssrcOwners[thessrc];
  855. logger.log('associated jid', self.ssrcOwners[thessrc]);
  856. } else {
  857. logger.error("No SSRC lines for ", streamId);
  858. }
  859. }
  860. this.room.remoteStreamAdded(data, this.sid, thessrc);
  861. };
  862. /**
  863. * Handles remote stream removal.
  864. * @param event The event object associated with the removal.
  865. */
  866. JingleSessionPC.prototype.remoteStreamRemoved = function (event) {
  867. var thessrc;
  868. var streamId = RTC.getStreamID(event.stream);
  869. if (!streamId) {
  870. logger.error("No stream ID for", event.stream);
  871. } else if (streamId && streamId.indexOf('mixedmslabel') === -1) {
  872. this.room.eventEmitter.emit(XMPPEvents.REMOTE_STREAM_REMOVED, streamId);
  873. }
  874. };
  875. /**
  876. * Returns the ice connection state for the peer connection.
  877. * @returns the ice connection state for the peer connection.
  878. */
  879. JingleSessionPC.prototype.getIceConnectionState = function () {
  880. return this.peerconnection.iceConnectionState;
  881. };
  882. /**
  883. * Fixes the outgoing jingle packets by removing the nodes related to the
  884. * muted/unmuted streams, handles removing of muted stream, etc.
  885. * @param jingle the jingle packet that is going to be sent
  886. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  887. */
  888. JingleSessionPC.prototype.fixJingle = function(jingle) {
  889. var action = $(jingle.nodeTree).find("jingle").attr("action");
  890. switch (action) {
  891. case "source-add":
  892. case "session-accept":
  893. this.fixSourceAddJingle(jingle);
  894. break;
  895. case "source-remove":
  896. this.fixSourceRemoveJingle(jingle);
  897. break;
  898. default:
  899. logger.error("Unknown jingle action!");
  900. return false;
  901. }
  902. var sources = $(jingle.tree()).find(">jingle>content>description>source");
  903. return sources && sources.length > 0;
  904. };
  905. /**
  906. * Fixes the outgoing jingle packets with action source-add by removing the
  907. * nodes related to the unmuted streams
  908. * @param jingle the jingle packet that is going to be sent
  909. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  910. */
  911. JingleSessionPC.prototype.fixSourceAddJingle = function (jingle) {
  912. var ssrcs = this.modifiedSSRCs["unmute"];
  913. this.modifiedSSRCs["unmute"] = [];
  914. if(ssrcs && ssrcs.length) {
  915. ssrcs.forEach(function (ssrcObj) {
  916. var desc = $(jingle.tree()).find(">jingle>content[name=\"" +
  917. ssrcObj.mtype + "\"]>description");
  918. if(!desc || !desc.length)
  919. return;
  920. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  921. var sourceNode = desc.find(">source[ssrc=\"" +
  922. ssrc + "\"]");
  923. sourceNode.remove();
  924. });
  925. ssrcObj.ssrc.groups.forEach(function (group) {
  926. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  927. group.group.semantics + "\"]:has(source[ssrc=\"" +
  928. group.primarySSRC +
  929. "\"])");
  930. groupNode.remove();
  931. });
  932. });
  933. }
  934. ssrcs = this.modifiedSSRCs["addMuted"];
  935. this.modifiedSSRCs["addMuted"] = [];
  936. if(ssrcs && ssrcs.length) {
  937. ssrcs.forEach(function (ssrcObj) {
  938. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  939. var cname = Math.random().toString(36).substring(2);
  940. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  941. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  942. sourceNode.remove();
  943. var sourceXML = "<source " +
  944. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  945. ssrc + "\">" +
  946. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  947. " value=\"" + ssrcObj.msid + "\" name=\"msid\"/>" +
  948. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  949. " value=\"" + cname + "\" name=\"cname\" />" + "</source>";
  950. desc.append(sourceXML);
  951. });
  952. ssrcObj.ssrc.groups.forEach(function (group) {
  953. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  954. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  955. "\"])");
  956. groupNode.remove();
  957. desc.append("<ssrc-group semantics=\"" +
  958. group.group.semantics +
  959. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  960. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  961. "</ssrc-group>");
  962. });
  963. });
  964. }
  965. };
  966. /**
  967. * Fixes the outgoing jingle packets with action source-remove by removing the
  968. * nodes related to the muted streams, handles removing of muted stream
  969. * @param jingle the jingle packet that is going to be sent
  970. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  971. */
  972. JingleSessionPC.prototype.fixSourceRemoveJingle = function(jingle) {
  973. var ssrcs = this.modifiedSSRCs["mute"];
  974. this.modifiedSSRCs["mute"] = [];
  975. if(ssrcs && ssrcs.length)
  976. ssrcs.forEach(function (ssrcObj) {
  977. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  978. var sourceNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  979. ssrcObj.mtype + "\"]>description>source[ssrc=\"" +
  980. ssrc + "\"]");
  981. sourceNode.remove();
  982. });
  983. ssrcObj.ssrc.groups.forEach(function (group) {
  984. var groupNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  985. ssrcObj.mtype + "\"]>description>ssrc-group[semantics=\"" +
  986. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  987. "\"])");
  988. groupNode.remove();
  989. });
  990. });
  991. ssrcs = this.modifiedSSRCs["remove"];
  992. this.modifiedSSRCs["remove"] = [];
  993. if(ssrcs && ssrcs.length)
  994. ssrcs.forEach(function (ssrcObj) {
  995. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  996. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  997. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  998. if(!sourceNode || !sourceNode.length) {
  999. //Maybe we have to include cname, msid, etc here?
  1000. desc.append("<source " +
  1001. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1002. ssrc + "\"></source>");
  1003. }
  1004. });
  1005. ssrcObj.ssrc.groups.forEach(function (group) {
  1006. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1007. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1008. "\"])");
  1009. if(!groupNode || !groupNode.length) {
  1010. desc.append("<ssrc-group semantics=\"" +
  1011. group.group.semantics +
  1012. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1013. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1014. "</ssrc-group>");
  1015. }
  1016. });
  1017. });
  1018. };
  1019. /**
  1020. * Returns the description node related to the passed content type. If the node
  1021. * doesn't exists it will be created.
  1022. * @param jingle - the jingle packet
  1023. * @param mtype - the content type(audio, video, etc.)
  1024. */
  1025. function createDescriptionNode(jingle, mtype) {
  1026. var content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1027. mtype + "\"]");
  1028. if(!content || !content.length) {
  1029. $(jingle.tree()).find(">jingle").append(
  1030. "<content name=\"" + mtype + "\"></content>");
  1031. content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1032. mtype + "\"]");
  1033. }
  1034. var desc = content.find(">description");
  1035. if(!desc || !desc.length) {
  1036. content.append("<description " +
  1037. "xmlns=\"urn:xmpp:jingle:apps:rtp:1\" media=\"" +
  1038. mtype + "\"></description>");
  1039. desc = content.find(">description");
  1040. }
  1041. return desc;
  1042. }
  1043. module.exports = JingleSessionPC;