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.

JingleSessionPC.js 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /* global $, $iq */
  2. import {getLogger} from "jitsi-meet-logger";
  3. const logger = getLogger(__filename);
  4. var JingleSession = require("./JingleSession");
  5. var TraceablePeerConnection = require("./TraceablePeerConnection");
  6. var SDPDiffer = require("./SDPDiffer");
  7. var SDPUtil = require("./SDPUtil");
  8. var SDP = require("./SDP");
  9. var async = require("async");
  10. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  11. var RTCBrowserType = require("../RTC/RTCBrowserType");
  12. import RTC from "../RTC/RTC";
  13. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  14. var Statistics = require("../statistics/statistics");
  15. import * as JingleSessionState from "./JingleSessionState";
  16. /**
  17. * Constant tells how long we're going to wait for IQ response, before timeout
  18. * error is triggered.
  19. * @type {number}
  20. */
  21. var IQ_TIMEOUT = 10000;
  22. // Jingle stuff
  23. function JingleSessionPC(me, sid, peerjid, connection,
  24. media_constraints, ice_config, service, eventEmitter) {
  25. JingleSession.call(this, me, sid, peerjid, connection,
  26. media_constraints, ice_config, service, eventEmitter);
  27. this.localSDP = null;
  28. this.lasticecandidate = false;
  29. this.closed = false;
  30. this.addssrc = [];
  31. this.removessrc = [];
  32. this.modifyingLocalStreams = false;
  33. this.modifiedSSRCs = {};
  34. /**
  35. * The local ICE username fragment for this session.
  36. */
  37. this.localUfrag = null;
  38. /**
  39. * The remote ICE username fragment for this session.
  40. */
  41. this.remoteUfrag = null;
  42. /**
  43. * A map that stores SSRCs of remote streams. And is used only locally
  44. * We store the mapping when jingle is received, and later is used
  45. * onaddstream webrtc event where we have only the ssrc
  46. * FIXME: This map got filled and never cleaned and can grow durring long
  47. * conference
  48. * @type {{}} maps SSRC number to jid
  49. */
  50. this.ssrcOwners = {};
  51. this.jingleOfferIq = null;
  52. this.webrtcIceUdpDisable = !!this.service.options.webrtcIceUdpDisable;
  53. this.webrtcIceTcpDisable = !!this.service.options.webrtcIceTcpDisable;
  54. /**
  55. * Flag used to enforce ICE failure through the URL parameter for
  56. * the automatic testing purpose.
  57. * @type {boolean}
  58. */
  59. this.failICE = !!this.service.options.failICE;
  60. this.modifySourcesQueue = async.queue(this._modifySources.bind(this), 1);
  61. }
  62. JingleSessionPC.prototype = Object.create(JingleSession.prototype);
  63. JingleSessionPC.prototype.constructor = JingleSessionPC;
  64. JingleSessionPC.prototype.doInitialize = function () {
  65. var self = this;
  66. this.lasticecandidate = false;
  67. // True if reconnect is in progress
  68. this.isreconnect = false;
  69. // Set to true if the connection was ever stable
  70. this.wasstable = false;
  71. this.peerconnection = new TraceablePeerConnection(
  72. this.connection.jingle.ice_config,
  73. RTC.getPCConstraints(),
  74. this);
  75. this.peerconnection.onicecandidate = function (ev) {
  76. if (!ev) {
  77. // There was an incomplete check for ev before which left the last
  78. // line of the function unprotected from a potential throw of an
  79. // exception. Consequently, it may be argued that the check is
  80. // unnecessary. Anyway, I'm leaving it and making the check
  81. // complete.
  82. return;
  83. }
  84. // XXX this is broken, candidate is not parsed.
  85. var candidate = ev.candidate;
  86. if (candidate) {
  87. // Discard candidates of disabled protocols.
  88. var protocol = candidate.protocol;
  89. if (typeof protocol === 'string') {
  90. protocol = protocol.toLowerCase();
  91. if (protocol === 'tcp' || protocol ==='ssltcp') {
  92. if (self.webrtcIceTcpDisable)
  93. return;
  94. } else if (protocol == 'udp') {
  95. if (self.webrtcIceUdpDisable)
  96. return;
  97. }
  98. }
  99. }
  100. self.sendIceCandidate(candidate);
  101. };
  102. this.peerconnection.onaddstream = function (event) {
  103. self.remoteStreamAdded(event.stream);
  104. };
  105. this.peerconnection.onremovestream = function (event) {
  106. self.remoteStreamRemoved(event.stream);
  107. };
  108. // Note there is a change in the spec about closed:
  109. // This value moved into the RTCPeerConnectionState enum in the May 13, 2016
  110. // draft of the specification, as it reflects the state of the
  111. // RTCPeerConnection, not the signaling connection. You now detect a
  112. // closed connection by checking for connectionState to be "closed" instead.
  113. // I suppose at some point this will be moved to onconnectionstatechange
  114. this.peerconnection.onsignalingstatechange = function () {
  115. if (!(self && self.peerconnection)) return;
  116. if (self.peerconnection.signalingState === 'stable') {
  117. self.wasstable = true;
  118. } else if (
  119. (self.peerconnection.signalingState === 'closed'
  120. || self.peerconnection.connectionState === 'closed')
  121. && !self.closed) {
  122. self.room.eventEmitter.emit(XMPPEvents.SUSPEND_DETECTED);
  123. }
  124. };
  125. /**
  126. * The oniceconnectionstatechange event handler contains the code to execute
  127. * when the iceconnectionstatechange event, of type Event, is received by
  128. * this RTCPeerConnection. Such an event is sent when the value of
  129. * RTCPeerConnection.iceConnectionState changes.
  130. */
  131. this.peerconnection.oniceconnectionstatechange = function () {
  132. if (!(self && self.peerconnection)) return;
  133. var now = window.performance.now();
  134. self.room.connectionTimes["ice.state." +
  135. self.peerconnection.iceConnectionState] = now;
  136. logger.log("(TIME) ICE " + self.peerconnection.iceConnectionState +
  137. ":\t", now);
  138. Statistics.analytics.sendEvent(
  139. 'ice.' + self.peerconnection.iceConnectionState, {value: now});
  140. self.room.eventEmitter.emit(
  141. XMPPEvents.ICE_CONNECTION_STATE_CHANGED,
  142. self.peerconnection.iceConnectionState);
  143. switch (self.peerconnection.iceConnectionState) {
  144. case 'connected':
  145. // Informs interested parties that the connection has been restored.
  146. if (self.peerconnection.signalingState === 'stable' && self.isreconnect)
  147. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_RESTORED);
  148. self.isreconnect = false;
  149. break;
  150. case 'disconnected':
  151. if(self.closed)
  152. break;
  153. self.isreconnect = true;
  154. // Informs interested parties that the connection has been interrupted.
  155. if (self.wasstable)
  156. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_INTERRUPTED);
  157. break;
  158. case 'failed':
  159. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_ICE_FAILED,
  160. self.peerconnection);
  161. break;
  162. }
  163. };
  164. this.peerconnection.onnegotiationneeded = function () {
  165. self.room.eventEmitter.emit(XMPPEvents.PEERCONNECTION_READY, self);
  166. };
  167. };
  168. JingleSessionPC.prototype.sendIceCandidate = function (candidate) {
  169. var self = this;
  170. if (candidate && !this.lasticecandidate) {
  171. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  172. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  173. if (!(ice && jcand)) {
  174. var errorMesssage = "failed to get ice && jcand";
  175. GlobalOnErrorHandler.callErrorHandler(new Error(errorMesssage));
  176. logger.error(errorMesssage);
  177. return;
  178. }
  179. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  180. if (this.usedrip) {
  181. if (this.drip_container.length === 0) {
  182. // start 20ms callout
  183. window.setTimeout(function () {
  184. if (self.drip_container.length === 0) return;
  185. self.sendIceCandidates(self.drip_container);
  186. self.drip_container = [];
  187. }, 20);
  188. }
  189. this.drip_container.push(candidate);
  190. } else {
  191. self.sendIceCandidates([candidate]);
  192. }
  193. } else {
  194. logger.log('sendIceCandidate: last candidate.');
  195. // FIXME: remember to re-think in ICE-restart
  196. this.lasticecandidate = true;
  197. }
  198. };
  199. JingleSessionPC.prototype.sendIceCandidates = function (candidates) {
  200. logger.log('sendIceCandidates', candidates);
  201. var cand = $iq({to: this.peerjid, type: 'set'})
  202. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  203. action: 'transport-info',
  204. initiator: this.initiator,
  205. sid: this.sid});
  206. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  207. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  208. var mline = SDPUtil.parse_mline(this.localSDP.media[mid].split('\r\n')[0]);
  209. if (cands.length > 0) {
  210. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  211. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  212. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  213. name: (cands[0].sdpMid? cands[0].sdpMid : mline.media)
  214. }).c('transport', ice);
  215. for (var i = 0; i < cands.length; i++) {
  216. var candidate = SDPUtil.candidateToJingle(cands[i].candidate);
  217. // Mangle ICE candidate if 'failICE' test option is enabled
  218. if (this.service.options.failICE) {
  219. candidate.ip = "1.1.1.1";
  220. }
  221. cand.c('candidate', candidate).up();
  222. }
  223. // add fingerprint
  224. var fingerprint_line = SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session);
  225. if (fingerprint_line) {
  226. var tmp = SDPUtil.parse_fingerprint(fingerprint_line);
  227. tmp.required = true;
  228. cand.c(
  229. 'fingerprint',
  230. {xmlns: 'urn:xmpp:jingle:apps:dtls:0'})
  231. .t(tmp.fingerprint);
  232. delete tmp.fingerprint;
  233. cand.attrs(tmp);
  234. cand.up();
  235. }
  236. cand.up(); // transport
  237. cand.up(); // content
  238. }
  239. }
  240. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  241. //logger.log('was this the last candidate', this.lasticecandidate);
  242. this.connection.sendIQ(
  243. cand, null, this.newJingleErrorHandler(cand, function (error) {
  244. GlobalOnErrorHandler.callErrorHandler(
  245. new Error("Jingle error: " + JSON.stringify(error)));
  246. }), IQ_TIMEOUT);
  247. };
  248. JingleSessionPC.prototype.readSsrcInfo = function (contents) {
  249. var self = this;
  250. $(contents).each(function (idx, content) {
  251. var ssrcs = $(content).find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  252. ssrcs.each(function () {
  253. var ssrc = this.getAttribute('ssrc');
  254. $(this).find('>ssrc-info[xmlns="http://jitsi.org/jitmeet"]').each(
  255. function () {
  256. var owner = this.getAttribute('owner');
  257. self.ssrcOwners[ssrc] = owner;
  258. }
  259. );
  260. });
  261. });
  262. };
  263. /**
  264. * Does accept incoming Jingle 'session-initiate' and should send
  265. * 'session-accept' in result.
  266. * @param jingleOffer jQuery selector pointing to the jingle element of
  267. * the offer IQ
  268. * @param success callback called when we accept incoming session successfully
  269. * and receive RESULT packet to 'session-accept' sent.
  270. * @param failure function(error) called if for any reason we fail to accept
  271. * the incoming offer. 'error' argument can be used to log some details
  272. * about the error.
  273. */
  274. JingleSessionPC.prototype.acceptOffer = function(jingleOffer,
  275. success, failure) {
  276. this.state = JingleSessionState.ACTIVE;
  277. this.setOfferCycle(jingleOffer,
  278. function() {
  279. // setOfferCycle succeeded, now we have self.localSDP up to date
  280. // Let's send an answer !
  281. // FIXME we may not care about RESULT packet for session-accept
  282. // then we should either call 'success' here immediately or
  283. // modify sendSessionAccept method to do that
  284. this.sendSessionAccept(this.localSDP, success, failure);
  285. }.bind(this),
  286. failure);
  287. };
  288. /**
  289. * This is a setRemoteDescription/setLocalDescription cycle which starts at
  290. * converting Strophe Jingle IQ into remote offer SDP. Once converted
  291. * setRemoteDescription, createAnswer and setLocalDescription calls follow.
  292. * @param jingleOfferIq jQuery selector pointing to the jingle element of
  293. * the offer IQ
  294. * @param success callback called when sRD/sLD cycle finishes successfully.
  295. * @param failure callback called with an error object as an argument if we fail
  296. * at any point during setRD, createAnswer, setLD.
  297. */
  298. JingleSessionPC.prototype.setOfferCycle = function (jingleOfferIq,
  299. success,
  300. failure) {
  301. this.jingleOfferIq = jingleOfferIq;
  302. this.modifySourcesQueue.push(success, function (error) {
  303. if(!error)
  304. return;
  305. if (failure)
  306. failure(error);
  307. JingleSessionPC.onJingleFatalError(this, error);
  308. }.bind(this));
  309. };
  310. /**
  311. * Modifies the values of the setup attributes (defined by
  312. * {@link http://tools.ietf.org/html/rfc4145#section-4}) of a specific SDP
  313. * answer in order to overcome a delay of 1 second in the connection
  314. * establishment between Chrome and Videobridge.
  315. *
  316. * @param {SDP} offer - the SDP offer to which the specified SDP answer is
  317. * being prepared to respond
  318. * @param {SDP} answer - the SDP to modify
  319. * @private
  320. */
  321. JingleSessionPC._fixAnswerRFC4145Setup = function (offer, answer) {
  322. if (!RTCBrowserType.isChrome()) {
  323. // It looks like Firefox doesn't agree with the fix (at least in its
  324. // current implementation) because it effectively remains active even
  325. // after we tell it to become passive. Apart from Firefox which I tested
  326. // after the fix was deployed, I tested Chrome only. In order to prevent
  327. // issues with other browsers, limit the fix to Chrome for the time
  328. // being.
  329. return;
  330. }
  331. // XXX Videobridge is the (SDP) offerer and WebRTC (e.g. Chrome) is the
  332. // answerer (as orchestrated by Jicofo). In accord with
  333. // http://tools.ietf.org/html/rfc5245#section-5.2 and because both peers
  334. // are ICE FULL agents, Videobridge will take on the controlling role and
  335. // WebRTC will take on the controlled role. In accord with
  336. // https://tools.ietf.org/html/rfc5763#section-5, Videobridge will use the
  337. // setup attribute value of setup:actpass and WebRTC will be allowed to
  338. // choose either the setup attribute value of setup:active or
  339. // setup:passive. Chrome will by default choose setup:active because it is
  340. // RECOMMENDED by the respective RFC since setup:passive adds additional
  341. // latency. The case of setup:active allows WebRTC to send a DTLS
  342. // ClientHello as soon as an ICE connectivity check of its succeeds.
  343. // Unfortunately, Videobridge will be unable to respond immediately because
  344. // may not have WebRTC's answer or may have not completed the ICE
  345. // connectivity establishment. Even more unfortunate is that in the
  346. // described scenario Chrome's DTLS implementation will insist on
  347. // retransmitting its ClientHello after a second (the time is in accord
  348. // with the respective RFC) and will thus cause the whole connection
  349. // establishment to exceed at least 1 second. To work around Chrome's
  350. // idiosyncracy, don't allow it to send a ClientHello i.e. change its
  351. // default choice of setup:active to setup:passive.
  352. if (offer && answer
  353. && offer.media && answer.media
  354. && offer.media.length == answer.media.length) {
  355. answer.media.forEach(function (a, i) {
  356. if (SDPUtil.find_line(
  357. offer.media[i],
  358. 'a=setup:actpass',
  359. offer.session)) {
  360. answer.media[i]
  361. = a.replace(/a=setup:active/g, 'a=setup:passive');
  362. }
  363. });
  364. answer.raw = answer.session + answer.media.join('');
  365. }
  366. };
  367. /**
  368. * Although it states "replace transport" it does accept full Jingle offer
  369. * which should contain new ICE transport details.
  370. * @param jingleOfferElem an element Jingle IQ that contains new offer and
  371. * transport info.
  372. * @param success callback called when we succeed to accept new offer.
  373. * @param failure function(error) called when we fail to accept new offer.
  374. */
  375. JingleSessionPC.prototype.replaceTransport = function (jingleOfferElem,
  376. success,
  377. failure) {
  378. // We need to first set an offer without the 'data' section to have the SCTP
  379. // stack cleaned up. After that the original offer is set to have the SCTP
  380. // connection established with the new bridge.
  381. this.room.eventEmitter.emit(XMPPEvents.ICE_RESTARTING);
  382. var originalOffer = jingleOfferElem.clone();
  383. jingleOfferElem.find(">content[name='data']").remove();
  384. var self = this;
  385. // First set an offer without the 'data' section
  386. this.setOfferCycle(
  387. jingleOfferElem,
  388. function() {
  389. // Now set the original offer(with the 'data' section)
  390. self.setOfferCycle(originalOffer,
  391. function () {
  392. // Set local description OK, now localSDP up to date
  393. self.sendTransportAccept(self.localSDP, success, failure);
  394. },
  395. failure);
  396. },
  397. failure
  398. );
  399. };
  400. /**
  401. * Sends Jingle 'session-accept' message.
  402. * @param localSDP the 'SDP' object with local session description
  403. * @param success callback called when we recive 'RESULT' packet for
  404. * 'session-accept'
  405. * @param failure function(error) called when we receive an error response or
  406. * when the request has timed out.
  407. */
  408. JingleSessionPC.prototype.sendSessionAccept = function (localSDP,
  409. success, failure) {
  410. var accept = $iq({to: this.peerjid,
  411. type: 'set'})
  412. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  413. action: 'session-accept',
  414. initiator: this.initiator,
  415. responder: this.responder,
  416. sid: this.sid });
  417. if (this.webrtcIceTcpDisable) {
  418. localSDP.removeTcpCandidates = true;
  419. }
  420. if (this.webrtcIceUdpDisable) {
  421. localSDP.removeUdpCandidates = true;
  422. }
  423. if (this.failICE) {
  424. localSDP.failICE = true;
  425. }
  426. localSDP.toJingle(
  427. accept,
  428. this.initiator == this.me ? 'initiator' : 'responder',
  429. null);
  430. this.fixJingle(accept);
  431. // Calling tree() to print something useful
  432. accept = accept.tree();
  433. logger.info("Sending session-accept", accept);
  434. var self = this;
  435. this.connection.sendIQ(accept,
  436. success,
  437. this.newJingleErrorHandler(accept, function (error) {
  438. failure(error);
  439. // 'session-accept' is a critical timeout and we'll have to restart
  440. self.room.eventEmitter.emit(XMPPEvents.SESSION_ACCEPT_TIMEOUT);
  441. }),
  442. IQ_TIMEOUT);
  443. // XXX Videobridge needs WebRTC's answer (ICE ufrag and pwd, DTLS
  444. // fingerprint and setup) ASAP in order to start the connection
  445. // establishment.
  446. //
  447. // FIXME Flushing the connection at this point triggers an issue with BOSH
  448. // request handling in Prosody on slow connections.
  449. //
  450. // The problem is that this request will be quite large and it may take time
  451. // before it reaches Prosody. In the meantime Strophe may decide to send
  452. // the next one. And it was observed that a small request with
  453. // 'transport-info' usually follows this one. It does reach Prosody before
  454. // the previous one was completely received. 'rid' on the server is
  455. // increased and Prosody ignores the request with 'session-accept'. It will
  456. // never reach Jicofo and everything in the request table is lost. Removing
  457. // the flush does not guarantee it will never happen, but makes it much less
  458. // likely('transport-info' is bundled with 'session-accept' and any
  459. // immediate requests).
  460. //
  461. // this.connection.flush();
  462. };
  463. /**
  464. * Sends Jingle 'transport-accept' message which is a response to
  465. * 'transport-replace'.
  466. * @param localSDP the 'SDP' object with local session description
  467. * @param success callback called when we receive 'RESULT' packet for
  468. * 'transport-replace'
  469. * @param failure function(error) called when we receive an error response or
  470. * when the request has timed out.
  471. */
  472. JingleSessionPC.prototype.sendTransportAccept = function(localSDP, success,
  473. failure) {
  474. var self = this;
  475. var tAccept = $iq({to: this.peerjid, type: 'set'})
  476. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  477. action: 'transport-accept',
  478. initiator: this.initiator,
  479. sid: this.sid});
  480. localSDP.media.forEach(function(medialines, idx){
  481. var mline = SDPUtil.parse_mline(medialines.split('\r\n')[0]);
  482. tAccept.c('content',
  483. { creator: self.initiator == self.me ? 'initiator' : 'responder',
  484. name: mline.media
  485. }
  486. );
  487. localSDP.transportToJingle(idx, tAccept);
  488. tAccept.up();
  489. });
  490. // Calling tree() to print something useful to the logger
  491. tAccept = tAccept.tree();
  492. console.info("Sending transport-accept: ", tAccept);
  493. self.connection.sendIQ(tAccept,
  494. success,
  495. self.newJingleErrorHandler(tAccept, failure),
  496. IQ_TIMEOUT);
  497. };
  498. /**
  499. * Sends Jingle 'transport-reject' message which is a response to
  500. * 'transport-replace'.
  501. * @param success callback called when we receive 'RESULT' packet for
  502. * 'transport-replace'
  503. * @param failure function(error) called when we receive an error response or
  504. * when the request has timed out.
  505. */
  506. JingleSessionPC.prototype.sendTransportReject = function(success, failure) {
  507. // Send 'transport-reject', so that the focus will
  508. // know that we've failed
  509. var tReject = $iq({to: this.peerjid, type: 'set'})
  510. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  511. action: 'transport-reject',
  512. initiator: this.initiator,
  513. sid: this.sid});
  514. tReject = tReject.tree();
  515. logger.info("Sending 'transport-reject", tReject);
  516. this.connection.sendIQ(tReject,
  517. success,
  518. this.newJingleErrorHandler(tReject, failure),
  519. IQ_TIMEOUT);
  520. };
  521. /**
  522. * @inheritDoc
  523. */
  524. JingleSessionPC.prototype.terminate = function (reason, text,
  525. success, failure) {
  526. this.state = JingleSessionState.ENDED;
  527. var term = $iq({to: this.peerjid,
  528. type: 'set'})
  529. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  530. action: 'session-terminate',
  531. initiator: this.initiator,
  532. sid: this.sid})
  533. .c('reason')
  534. .c(reason || 'success');
  535. if (text) {
  536. term.up().c('text').t(text);
  537. }
  538. // Calling tree() to print something useful
  539. term = term.tree();
  540. logger.info("Sending session-terminate", term);
  541. this.connection.sendIQ(
  542. term, success, this.newJingleErrorHandler(term, failure), IQ_TIMEOUT);
  543. // this should result in 'onTerminated' being called by strope.jingle.js
  544. this.connection.jingle.terminate(this.sid);
  545. };
  546. JingleSessionPC.prototype.onTerminated = function (reasonCondition,
  547. reasonText) {
  548. this.state = 'ended';
  549. // Do something with reason and reasonCondition when we start to care
  550. //this.reasonCondition = reasonCondition;
  551. //this.reasonText = reasonText;
  552. logger.info("Session terminated", this, reasonCondition, reasonText);
  553. this.close();
  554. };
  555. /**
  556. * Handles a Jingle source-add message for this Jingle session.
  557. * @param elem An array of Jingle "content" elements.
  558. */
  559. JingleSessionPC.prototype.addSource = function (elem) {
  560. var self = this;
  561. // FIXME: dirty waiting
  562. if (!this.peerconnection.localDescription)
  563. {
  564. logger.warn("addSource - localDescription not ready yet");
  565. setTimeout(function()
  566. {
  567. self.addSource(elem);
  568. },
  569. 200
  570. );
  571. return;
  572. }
  573. logger.log('addssrc', new Date().getTime());
  574. logger.log('ice', this.peerconnection.iceConnectionState);
  575. this.readSsrcInfo(elem);
  576. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  577. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  578. $(elem).each(function (idx, content) {
  579. var name = $(content).attr('name');
  580. var lines = '';
  581. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  582. var semantics = this.getAttribute('semantics');
  583. var ssrcs = $(this).find('>source').map(function () {
  584. return this.getAttribute('ssrc');
  585. }).get();
  586. if (ssrcs.length) {
  587. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  588. }
  589. });
  590. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  591. tmp.each(function () {
  592. var ssrc = $(this).attr('ssrc');
  593. if(mySdp.containsSSRC(ssrc)){
  594. /**
  595. * This happens when multiple participants change their streams at the same time and
  596. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  597. * addssrc are scheduled for update IQ. See
  598. */
  599. logger.warn("Got add stream request for my own ssrc: "+ssrc);
  600. return;
  601. }
  602. if (sdp.containsSSRC(ssrc)) {
  603. logger.warn("Source-add request for existing SSRC: " + ssrc);
  604. return;
  605. }
  606. $(this).find('>parameter').each(function () {
  607. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  608. if ($(this).attr('value') && $(this).attr('value').length)
  609. lines += ':' + $(this).attr('value');
  610. lines += '\r\n';
  611. });
  612. });
  613. sdp.media.forEach(function(media, idx) {
  614. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  615. return;
  616. sdp.media[idx] += lines;
  617. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  618. self.addssrc[idx] += lines;
  619. });
  620. sdp.raw = sdp.session + sdp.media.join('');
  621. });
  622. this.modifySourcesQueue.push(function() {
  623. // When a source is added and if this is FF, a new channel is allocated
  624. // for receiving the added source. We need to diffuse the SSRC of this
  625. // new recvonly channel to the rest of the peers.
  626. logger.log('modify sources done');
  627. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  628. logger.log("SDPs", mySdp, newSdp);
  629. self.notifyMySSRCUpdate(mySdp, newSdp);
  630. });
  631. };
  632. /**
  633. * Handles a Jingle source-remove message for this Jingle session.
  634. * @param elem An array of Jingle "content" elements.
  635. */
  636. JingleSessionPC.prototype.removeSource = function (elem) {
  637. var self = this;
  638. // FIXME: dirty waiting
  639. if (!this.peerconnection.localDescription) {
  640. logger.warn("removeSource - localDescription not ready yet");
  641. setTimeout(function() {
  642. self.removeSource(elem);
  643. },
  644. 200
  645. );
  646. return;
  647. }
  648. logger.log('removessrc', new Date().getTime());
  649. logger.log('ice', this.peerconnection.iceConnectionState);
  650. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  651. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  652. $(elem).each(function (idx, content) {
  653. var name = $(content).attr('name');
  654. var lines = '';
  655. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  656. var semantics = this.getAttribute('semantics');
  657. var ssrcs = $(this).find('>source').map(function () {
  658. return this.getAttribute('ssrc');
  659. }).get();
  660. if (ssrcs.length) {
  661. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  662. }
  663. });
  664. var ssrcs = [];
  665. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  666. tmp.each(function () {
  667. var ssrc = $(this).attr('ssrc');
  668. // This should never happen, but can be useful for bug detection
  669. if(mySdp.containsSSRC(ssrc)){
  670. var errmsg
  671. = "Got remove stream request for my own ssrc: " + ssrc;
  672. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  673. logger.error(errmsg);
  674. return;
  675. }
  676. ssrcs.push(ssrc);
  677. });
  678. sdp.media.forEach(function(media, idx) {
  679. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  680. return;
  681. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  682. ssrcs.forEach(function(ssrc) {
  683. var ssrcLines = SDPUtil.find_lines(media, 'a=ssrc:' + ssrc);
  684. if (ssrcLines.length)
  685. self.removessrc[idx] += ssrcLines.join("\r\n")+"\r\n";
  686. // Clear any pending 'source-add' for this SSRC
  687. if (self.addssrc[idx]) {
  688. self.addssrc[idx]
  689. = self.addssrc[idx].replace(
  690. new RegExp('^a=ssrc:'+ssrc+' .*\r\n', 'gm'), '');
  691. }
  692. });
  693. self.removessrc[idx] += lines;
  694. });
  695. sdp.raw = sdp.session + sdp.media.join('');
  696. });
  697. this.modifySourcesQueue.push(function() {
  698. // When a source is removed and if this is FF, the recvonly channel that
  699. // receives the remote stream is deactivated . We need to diffuse the
  700. // recvonly SSRC removal to the rest of the peers.
  701. logger.log('modify sources done');
  702. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  703. logger.log("SDPs", mySdp, newSdp);
  704. self.notifyMySSRCUpdate(mySdp, newSdp);
  705. });
  706. };
  707. JingleSessionPC.prototype._modifySources = function (successCallback, queueCallback) {
  708. var self = this, sdp = null, media_constraints;
  709. if (this.peerconnection.signalingState == 'closed') return;
  710. if (!(this.addssrc.length || this.removessrc.length
  711. || this.modifyingLocalStreams || this.jingleOfferIq !== null)){
  712. // There is nothing to do since scheduled job might have been
  713. // executed by another succeeding call
  714. if(successCallback){
  715. successCallback();
  716. }
  717. queueCallback();
  718. return;
  719. }
  720. if(this.jingleOfferIq) {
  721. sdp = new SDP('');
  722. if (this.webrtcIceTcpDisable) {
  723. sdp.removeTcpCandidates = true;
  724. }
  725. if (this.webrtcIceUdpDisable) {
  726. sdp.removeUdpCandidates = true;
  727. }
  728. if (this.failICE) {
  729. sdp.failICE = true;
  730. }
  731. sdp.fromJingle(this.jingleOfferIq);
  732. this.readSsrcInfo($(this.jingleOfferIq).find(">content"));
  733. this.jingleOfferIq = null;
  734. media_constraints = this.media_constraints;
  735. } else {
  736. // Reset switch streams flags
  737. this.modifyingLocalStreams = false;
  738. sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  739. }
  740. // add sources
  741. this.addssrc.forEach(function(lines, idx) {
  742. sdp.media[idx] += lines;
  743. });
  744. this.addssrc = [];
  745. // remove sources
  746. this.removessrc.forEach(function(lines, idx) {
  747. lines = lines.split('\r\n');
  748. lines.pop(); // remove empty last element;
  749. lines.forEach(function(line) {
  750. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  751. });
  752. });
  753. this.removessrc = [];
  754. sdp.raw = sdp.session + sdp.media.join('');
  755. /**
  756. * Implements a failure callback which reports an error message and an
  757. * optional error through (1) logger, (2) GlobalOnErrorHandler, and (3)
  758. * queueCallback.
  759. *
  760. * @param {string} errmsg the error message to report
  761. * @param {*} error an optional error to report in addition to errmsg
  762. */
  763. function reportError(errmsg, err) {
  764. if (err) {
  765. errmsg = errmsg + ': ' + err; // for logger and GlobalOnErrorHandler
  766. logger.error(errmsg, err);
  767. } else {
  768. logger.error(errmsg);
  769. err = new Error(errmsg); // for queueCallback
  770. }
  771. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  772. queueCallback(err);
  773. }
  774. var ufrag = getUfrag(sdp.raw);
  775. if (ufrag != self.remoteUfrag) {
  776. self.remoteUfrag = ufrag;
  777. self.room.eventEmitter.emit(
  778. XMPPEvents.REMOTE_UFRAG_CHANGED, ufrag);
  779. }
  780. this.peerconnection.setRemoteDescription(
  781. new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  782. function() {
  783. if(self.signalingState == 'closed') {
  784. reportError("createAnswer attempt on closed state");
  785. return;
  786. }
  787. self.peerconnection.createAnswer(
  788. function(answer) {
  789. // FIXME: pushing down an answer while ice connection state
  790. // is still checking is bad...
  791. //logger.log(self.peerconnection.iceConnectionState);
  792. var modifiedAnswer = new SDP(answer.sdp);
  793. JingleSessionPC._fixAnswerRFC4145Setup(
  794. /* offer */ sdp,
  795. /* answer */ modifiedAnswer);
  796. answer.sdp = modifiedAnswer.raw;
  797. self.localSDP = new SDP(answer.sdp);
  798. answer.sdp = self.localSDP.raw;
  799. var ufrag = getUfrag(answer.sdp);
  800. if (ufrag != self.localUfrag) {
  801. self.localUfrag = ufrag;
  802. self.room.eventEmitter.emit(
  803. XMPPEvents.LOCAL_UFRAG_CHANGED, ufrag);
  804. }
  805. self.peerconnection.setLocalDescription(answer,
  806. function() {
  807. successCallback && successCallback();
  808. queueCallback();
  809. },
  810. reportError.bind(
  811. undefined,
  812. "modified setLocalDescription failed")
  813. );
  814. }, reportError.bind(undefined, "modified answer failed"),
  815. media_constraints
  816. );
  817. },
  818. reportError.bind(undefined, 'modify failed')
  819. );
  820. };
  821. /**
  822. * Adds stream.
  823. * @param stream new stream that will be added.
  824. * @param callback callback executed after successful stream addition.
  825. * @param errorCallback callback executed if stream addition fail.
  826. * @param ssrcInfo object with information about the SSRCs associated with the
  827. * stream.
  828. * @param dontModifySources {boolean} if true _modifySources won't be called.
  829. * Used for streams added before the call start.
  830. */
  831. JingleSessionPC.prototype.addStream = function (stream, callback, errorCallback,
  832. ssrcInfo, dontModifySources) {
  833. // Remember SDP to figure out added/removed SSRCs
  834. var oldSdp = null;
  835. if(this.peerconnection && this.peerconnection.localDescription) {
  836. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  837. }
  838. // Conference is not active
  839. if(!oldSdp || !this.peerconnection || dontModifySources) {
  840. //when adding muted stream we have to pass the ssrcInfo but we don't
  841. //have a stream
  842. if(this.peerconnection && (stream || ssrcInfo))
  843. this.peerconnection.addStream(stream, ssrcInfo);
  844. if(ssrcInfo) {
  845. //available only on video unmute or when adding muted stream
  846. this.modifiedSSRCs[ssrcInfo.type] =
  847. this.modifiedSSRCs[ssrcInfo.type] || [];
  848. this.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  849. }
  850. callback();
  851. return;
  852. }
  853. if(stream || ssrcInfo)
  854. this.peerconnection.addStream(stream, ssrcInfo);
  855. this.modifyingLocalStreams = true;
  856. var self = this;
  857. this.modifySourcesQueue.push(function() {
  858. logger.log('modify sources done');
  859. if(ssrcInfo) {
  860. //available only on video unmute or when adding muted stream
  861. self.modifiedSSRCs[ssrcInfo.type] =
  862. self.modifiedSSRCs[ssrcInfo.type] || [];
  863. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  864. }
  865. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  866. logger.log("SDPs", oldSdp, newSdp);
  867. self.notifyMySSRCUpdate(oldSdp, newSdp);
  868. }, function (error) {
  869. if(!error) {
  870. callback();
  871. } else {
  872. errorCallback(error);
  873. }
  874. });
  875. };
  876. /**
  877. * Generate ssrc info object for a stream with the following properties:
  878. * - ssrcs - Array of the ssrcs associated with the stream.
  879. * - groups - Array of the groups associated with the stream.
  880. */
  881. JingleSessionPC.prototype.generateNewStreamSSRCInfo = function () {
  882. return this.peerconnection.generateNewStreamSSRCInfo();
  883. };
  884. /**
  885. * Remove streams.
  886. * @param stream stream that will be removed.
  887. * @param callback callback executed after successful stream addition.
  888. * @param errorCallback callback executed if stream addition fail.
  889. * @param ssrcInfo object with information about the SSRCs associated with the
  890. * stream.
  891. */
  892. JingleSessionPC.prototype.removeStream = function (stream, callback, errorCallback,
  893. ssrcInfo) {
  894. // Conference is not active
  895. if(!this.peerconnection) {
  896. callback();
  897. return;
  898. }
  899. // Remember SDP to figure out added/removed SSRCs
  900. var oldSdp = null;
  901. if(this.peerconnection.localDescription) {
  902. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  903. }
  904. if(!oldSdp) {
  905. callback();
  906. return;
  907. }
  908. if (RTCBrowserType.getBrowserType() ===
  909. RTCBrowserType.RTC_BROWSER_FIREFOX) {
  910. if(!stream) {//There is nothing to be changed
  911. callback();
  912. return;
  913. }
  914. var sender = null;
  915. // On Firefox we don't replace MediaStreams as this messes up the
  916. // m-lines (which can't be removed in Plan Unified) and brings a lot
  917. // of complications. Instead, we use the RTPSender and remove just
  918. // the track.
  919. var track = null;
  920. if(stream.getAudioTracks() && stream.getAudioTracks().length) {
  921. track = stream.getAudioTracks()[0];
  922. } else if(stream.getVideoTracks() && stream.getVideoTracks().length) {
  923. track = stream.getVideoTracks()[0];
  924. }
  925. if(!track) {
  926. var msg = "Cannot remove tracks: no tracks.";
  927. logger.log(msg);
  928. errorCallback(new Error(msg));
  929. return;
  930. }
  931. // Find the right sender (for audio or video)
  932. this.peerconnection.peerconnection.getSenders().some(function (s) {
  933. if (s.track === track) {
  934. sender = s;
  935. return true;
  936. }
  937. });
  938. if (sender) {
  939. this.peerconnection.peerconnection.removeTrack(sender);
  940. } else {
  941. logger.log("Cannot remove tracks: no RTPSender.");
  942. }
  943. } else if(stream)
  944. this.peerconnection.removeStream(stream, false, ssrcInfo);
  945. // else
  946. // NOTE: If there is no stream and the browser is not FF we still need to do
  947. // some transformation in order to send remove-source for the muted
  948. // streams. That's why we aren't calling return here.
  949. this.modifyingLocalStreams = true;
  950. var self = this;
  951. this.modifySourcesQueue.push(function() {
  952. logger.log('modify sources done');
  953. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  954. if(ssrcInfo) {
  955. self.modifiedSSRCs[ssrcInfo.type] =
  956. self.modifiedSSRCs[ssrcInfo.type] || [];
  957. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  958. }
  959. logger.log("SDPs", oldSdp, newSdp);
  960. self.notifyMySSRCUpdate(oldSdp, newSdp);
  961. }, function (error) {
  962. if(!error) {
  963. callback();
  964. } else {
  965. errorCallback(error);
  966. }
  967. });
  968. };
  969. /**
  970. * Figures out added/removed ssrcs and send update IQs.
  971. * @param old_sdp SDP object for old description.
  972. * @param new_sdp SDP object for new description.
  973. */
  974. JingleSessionPC.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  975. if (this.state !== JingleSessionState.ACTIVE){
  976. logger.warn(
  977. "Skipping SSRC update in \'" + this.state + " \' state.");
  978. return;
  979. }
  980. // send source-remove IQ.
  981. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  982. var remove = $iq({to: this.peerjid, type: 'set'})
  983. .c('jingle', {
  984. xmlns: 'urn:xmpp:jingle:1',
  985. action: 'source-remove',
  986. initiator: this.initiator,
  987. sid: this.sid
  988. }
  989. );
  990. sdpDiffer.toJingle(remove);
  991. var removed = this.fixJingle(remove);
  992. if (removed && remove) {
  993. logger.info("Sending source-remove", remove.tree());
  994. this.connection.sendIQ(
  995. remove, null, this.newJingleErrorHandler(remove, function (error) {
  996. GlobalOnErrorHandler.callErrorHandler(
  997. new Error("Jingle error: " + JSON.stringify(error)));
  998. }), IQ_TIMEOUT);
  999. } else {
  1000. logger.log('removal not necessary');
  1001. }
  1002. // send source-add IQ.
  1003. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  1004. var add = $iq({to: this.peerjid, type: 'set'})
  1005. .c('jingle', {
  1006. xmlns: 'urn:xmpp:jingle:1',
  1007. action: 'source-add',
  1008. initiator: this.initiator,
  1009. sid: this.sid
  1010. }
  1011. );
  1012. sdpDiffer.toJingle(add);
  1013. var added = this.fixJingle(add);
  1014. if (added && add) {
  1015. logger.info("Sending source-add", add.tree());
  1016. this.connection.sendIQ(
  1017. add, null, this.newJingleErrorHandler(add, function (error) {
  1018. GlobalOnErrorHandler.callErrorHandler(
  1019. new Error("Jingle error: " + JSON.stringify(error)));
  1020. }), IQ_TIMEOUT);
  1021. } else {
  1022. logger.log('addition not necessary');
  1023. }
  1024. };
  1025. /**
  1026. * Method returns function(errorResponse) which is a callback to be passed to
  1027. * Strophe connection.sendIQ method. An 'error' structure is created that is
  1028. * passed as 1st argument to given <tt>failureCb</tt>. The format of this
  1029. * structure is as follows:
  1030. * {
  1031. * code: {XMPP error response code}
  1032. * reason: {the name of XMPP error reason element or 'timeout' if the request
  1033. * has timed out within <tt>IQ_TIMEOUT</tt> milliseconds}
  1034. * source: {request.tree() that provides original request}
  1035. * session: {JingleSessionPC instance on which the error occurred}
  1036. * }
  1037. * @param request Strophe IQ instance which is the request to be dumped into
  1038. * the error structure
  1039. * @param failureCb function(error) called when error response was returned or
  1040. * when a timeout has occurred.
  1041. * @returns {function(this:JingleSessionPC)}
  1042. */
  1043. JingleSessionPC.prototype.newJingleErrorHandler = function(request, failureCb) {
  1044. return function (errResponse) {
  1045. var error = { };
  1046. // Get XMPP error code and condition(reason)
  1047. var errorElSel = $(errResponse).find('error');
  1048. if (errorElSel.length) {
  1049. error.code = errorElSel.attr('code');
  1050. var errorReasonSel = $(errResponse).find('error :first');
  1051. if (errorReasonSel.length)
  1052. error.reason = errorReasonSel[0].tagName;
  1053. }
  1054. if (!errResponse) {
  1055. error.reason = 'timeout';
  1056. }
  1057. error.source = null;
  1058. if (request && "function" == typeof request.tree) {
  1059. error.source = request.tree();
  1060. }
  1061. // Commented to fix JSON.stringify(error) exception for circular
  1062. // dependancies when we print that error.
  1063. // FIXME: Maybe we can include part of the session object
  1064. // error.session = this;
  1065. logger.error("Jingle error", error);
  1066. if (failureCb) {
  1067. failureCb(error);
  1068. }
  1069. }.bind(this);
  1070. };
  1071. JingleSessionPC.onJingleFatalError = function (session, error)
  1072. {
  1073. this.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED, error);
  1074. this.room.eventEmitter.emit(XMPPEvents.JINGLE_FATAL_ERROR, session, error);
  1075. };
  1076. /**
  1077. * Called when new remote MediaStream is added to the PeerConnection.
  1078. * @param stream the WebRTC MediaStream for remote participant
  1079. */
  1080. JingleSessionPC.prototype.remoteStreamAdded = function (stream) {
  1081. var self = this;
  1082. if (!RTC.isUserStream(stream)) {
  1083. logger.info(
  1084. "Ignored remote 'stream added' event for non-user stream", stream);
  1085. return;
  1086. }
  1087. // Bind 'addtrack'/'removetrack' event handlers
  1088. if (RTCBrowserType.isChrome() || RTCBrowserType.isNWJS()
  1089. || RTCBrowserType.isElectron()) {
  1090. stream.onaddtrack = function (event) {
  1091. self.remoteTrackAdded(event.target, event.track);
  1092. };
  1093. stream.onremovetrack = function (event) {
  1094. self.remoteTrackRemoved(event.target, event.track);
  1095. };
  1096. }
  1097. // Call remoteTrackAdded for each track in the stream
  1098. stream.getAudioTracks().forEach(function (track) {
  1099. self.remoteTrackAdded(stream, track);
  1100. });
  1101. stream.getVideoTracks().forEach(function (track) {
  1102. self.remoteTrackAdded(stream, track);
  1103. });
  1104. };
  1105. /**
  1106. * Called on "track added" and "stream added" PeerConnection events(cause we
  1107. * handle streams on per track basis). Does find the owner and the SSRC for
  1108. * the track and passes that to ChatRoom for further processing.
  1109. * @param stream WebRTC MediaStream instance which is the parent of the track
  1110. * @param track the WebRTC MediaStreamTrack added for remote participant
  1111. */
  1112. JingleSessionPC.prototype.remoteTrackAdded = function (stream, track) {
  1113. logger.info("Remote track added", stream, track);
  1114. var streamId = RTC.getStreamID(stream);
  1115. var mediaType = track.kind;
  1116. // This is our event structure which will be passed by the ChatRoom as
  1117. // XMPPEvents.REMOTE_TRACK_ADDED data
  1118. var jitsiTrackAddedEvent = {
  1119. stream: stream,
  1120. track: track,
  1121. mediaType: track.kind, /* 'audio' or 'video' */
  1122. owner: undefined, /* to be determined below */
  1123. muted: null /* will be set in the ChatRoom */
  1124. };
  1125. try{
  1126. // look up an associated JID for a stream id
  1127. if (!mediaType) {
  1128. logger.error("MediaType undefined", track);
  1129. throw new Error("MediaType undefined for remote track");
  1130. }
  1131. var remoteSDP = new SDP(this.peerconnection.remoteDescription.sdp);
  1132. var medialines = remoteSDP.media.filter(function (mediaLines){
  1133. return mediaLines.startsWith("m=" + mediaType);
  1134. });
  1135. if (!medialines.length) {
  1136. logger.error("No media for type " + mediaType + " found in remote SDP");
  1137. throw new Error("No media for type " + mediaType +
  1138. " found in remote SDP for remote track");
  1139. }
  1140. var ssrclines = SDPUtil.find_lines(medialines[0], 'a=ssrc:');
  1141. ssrclines = ssrclines.filter(function (line) {
  1142. var msid = RTCBrowserType.isTemasysPluginUsed() ? 'mslabel' : 'msid';
  1143. return line.indexOf(msid + ':' + streamId) !== -1;
  1144. });
  1145. var thessrc;
  1146. if (ssrclines.length) {
  1147. thessrc = ssrclines[0].substring(7).split(' ')[0];
  1148. if (!this.ssrcOwners[thessrc]) {
  1149. logger.error("No SSRC owner known for: " + thessrc);
  1150. throw new Error("No SSRC owner known for: " + thessrc +
  1151. " for remote track");
  1152. }
  1153. jitsiTrackAddedEvent.owner = this.ssrcOwners[thessrc];
  1154. logger.log('associated jid', this.ssrcOwners[thessrc], thessrc);
  1155. } else {
  1156. logger.error("No SSRC lines for ", streamId);
  1157. throw new Error("No SSRC lines for streamId " + streamId +
  1158. " for remote track");
  1159. }
  1160. jitsiTrackAddedEvent.ssrc = thessrc;
  1161. this.room.remoteTrackAdded(jitsiTrackAddedEvent);
  1162. } catch (error) {
  1163. GlobalOnErrorHandler.callErrorHandler(error);
  1164. }
  1165. };
  1166. /**
  1167. * Handles remote stream removal.
  1168. * @param stream the WebRTC MediaStream object which is being removed from the
  1169. * PeerConnection
  1170. */
  1171. JingleSessionPC.prototype.remoteStreamRemoved = function (stream) {
  1172. var self = this;
  1173. if (!RTC.isUserStream(stream)) {
  1174. logger.info(
  1175. "Ignored remote 'stream removed' event for non-user stream", stream);
  1176. return;
  1177. }
  1178. // Call remoteTrackRemoved for each track in the stream
  1179. stream.getVideoTracks().forEach(function(track){
  1180. self.remoteTrackRemoved(stream, track);
  1181. });
  1182. stream.getAudioTracks().forEach(function(track) {
  1183. self.remoteTrackRemoved(stream, track);
  1184. });
  1185. };
  1186. /**
  1187. * Handles remote media track removal.
  1188. * @param stream WebRTC MediaStream instance which is the parent of the track
  1189. * @param track the WebRTC MediaStreamTrack which has been removed from
  1190. * the PeerConnection.
  1191. */
  1192. JingleSessionPC.prototype.remoteTrackRemoved = function (stream, track) {
  1193. logger.info("Remote track removed", stream, track);
  1194. var streamId = RTC.getStreamID(stream);
  1195. var trackId = track && track.id;
  1196. try{
  1197. if (!streamId) {
  1198. logger.error("No stream ID for", stream);
  1199. throw new Error("Remote track removal failed - No stream ID");
  1200. }
  1201. if (!trackId) {
  1202. logger.error("No track ID for", track);
  1203. throw new Error("Remote track removal failed - No track ID");
  1204. }
  1205. this.room.eventEmitter.emit(
  1206. XMPPEvents.REMOTE_TRACK_REMOVED, streamId, trackId);
  1207. } catch (error) {
  1208. GlobalOnErrorHandler.callErrorHandler(error);
  1209. }
  1210. };
  1211. /**
  1212. * Returns the ice connection state for the peer connection.
  1213. * @returns the ice connection state for the peer connection.
  1214. */
  1215. JingleSessionPC.prototype.getIceConnectionState = function () {
  1216. return this.peerconnection.iceConnectionState;
  1217. };
  1218. /**
  1219. * Closes the peerconnection.
  1220. */
  1221. JingleSessionPC.prototype.close = function () {
  1222. this.closed = true;
  1223. // do not try to close if already closed.
  1224. this.peerconnection
  1225. && ((this.peerconnection.signalingState
  1226. && this.peerconnection.signalingState !== 'closed')
  1227. || (this.peerconnection.connectionState
  1228. && this.peerconnection.connectionState !== 'closed'))
  1229. && this.peerconnection.close();
  1230. };
  1231. /**
  1232. * Fixes the outgoing jingle packets by removing the nodes related to the
  1233. * muted/unmuted streams, handles removing of muted stream, etc.
  1234. * @param jingle the jingle packet that is going to be sent
  1235. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1236. */
  1237. JingleSessionPC.prototype.fixJingle = function(jingle) {
  1238. var action = $(jingle.nodeTree).find("jingle").attr("action");
  1239. switch (action) {
  1240. case "source-add":
  1241. case "session-accept":
  1242. this.fixSourceAddJingle(jingle);
  1243. break;
  1244. case "source-remove":
  1245. this.fixSourceRemoveJingle(jingle);
  1246. break;
  1247. default:
  1248. var errmsg = "Unknown jingle action!";
  1249. GlobalOnErrorHandler.callErrorHandler(errmsg);
  1250. logger.error(errmsg);
  1251. return false;
  1252. }
  1253. var sources = $(jingle.tree()).find(">jingle>content>description>source");
  1254. return sources && sources.length > 0;
  1255. };
  1256. /**
  1257. * Fixes the outgoing jingle packets with action source-add by removing the
  1258. * nodes related to the unmuted streams
  1259. * @param jingle the jingle packet that is going to be sent
  1260. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1261. */
  1262. JingleSessionPC.prototype.fixSourceAddJingle = function (jingle) {
  1263. var ssrcs = this.modifiedSSRCs["unmute"];
  1264. this.modifiedSSRCs["unmute"] = [];
  1265. if(ssrcs && ssrcs.length) {
  1266. ssrcs.forEach(function (ssrcObj) {
  1267. var desc = $(jingle.tree()).find(">jingle>content[name=\"" +
  1268. ssrcObj.mtype + "\"]>description");
  1269. if(!desc || !desc.length)
  1270. return;
  1271. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1272. var sourceNode = desc.find(">source[ssrc=\"" +
  1273. ssrc + "\"]");
  1274. sourceNode.remove();
  1275. });
  1276. ssrcObj.ssrc.groups.forEach(function (group) {
  1277. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1278. group.group.semantics + "\"]:has(source[ssrc=\"" +
  1279. group.primarySSRC +
  1280. "\"])");
  1281. groupNode.remove();
  1282. });
  1283. });
  1284. }
  1285. ssrcs = this.modifiedSSRCs["addMuted"];
  1286. this.modifiedSSRCs["addMuted"] = [];
  1287. if(ssrcs && ssrcs.length) {
  1288. ssrcs.forEach(function (ssrcObj) {
  1289. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1290. var cname = Math.random().toString(36).substring(2);
  1291. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1292. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1293. sourceNode.remove();
  1294. var sourceXML = "<source " +
  1295. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1296. ssrc + "\">" +
  1297. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1298. " value=\"" + ssrcObj.msid + "\" name=\"msid\"/>" +
  1299. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1300. " value=\"" + cname + "\" name=\"cname\" />" + "</source>";
  1301. desc.append(sourceXML);
  1302. });
  1303. ssrcObj.ssrc.groups.forEach(function (group) {
  1304. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1305. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1306. "\"])");
  1307. groupNode.remove();
  1308. desc.append("<ssrc-group semantics=\"" +
  1309. group.group.semantics +
  1310. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1311. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1312. "</ssrc-group>");
  1313. });
  1314. });
  1315. }
  1316. };
  1317. /**
  1318. * Fixes the outgoing jingle packets with action source-remove by removing the
  1319. * nodes related to the muted streams, handles removing of muted stream
  1320. * @param jingle the jingle packet that is going to be sent
  1321. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1322. */
  1323. JingleSessionPC.prototype.fixSourceRemoveJingle = function(jingle) {
  1324. var ssrcs = this.modifiedSSRCs["mute"];
  1325. this.modifiedSSRCs["mute"] = [];
  1326. if(ssrcs && ssrcs.length)
  1327. ssrcs.forEach(function (ssrcObj) {
  1328. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1329. var sourceNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1330. ssrcObj.mtype + "\"]>description>source[ssrc=\"" +
  1331. ssrc + "\"]");
  1332. sourceNode.remove();
  1333. });
  1334. ssrcObj.ssrc.groups.forEach(function (group) {
  1335. var groupNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1336. ssrcObj.mtype + "\"]>description>ssrc-group[semantics=\"" +
  1337. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1338. "\"])");
  1339. groupNode.remove();
  1340. });
  1341. });
  1342. ssrcs = this.modifiedSSRCs["remove"];
  1343. this.modifiedSSRCs["remove"] = [];
  1344. if(ssrcs && ssrcs.length)
  1345. ssrcs.forEach(function (ssrcObj) {
  1346. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1347. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1348. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1349. if(!sourceNode || !sourceNode.length) {
  1350. //Maybe we have to include cname, msid, etc here?
  1351. desc.append("<source " +
  1352. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1353. ssrc + "\"></source>");
  1354. }
  1355. });
  1356. ssrcObj.ssrc.groups.forEach(function (group) {
  1357. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1358. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1359. "\"])");
  1360. if(!groupNode || !groupNode.length) {
  1361. desc.append("<ssrc-group semantics=\"" +
  1362. group.group.semantics +
  1363. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1364. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1365. "</ssrc-group>");
  1366. }
  1367. });
  1368. });
  1369. };
  1370. /**
  1371. * Returns the description node related to the passed content type. If the node
  1372. * doesn't exists it will be created.
  1373. * @param jingle - the jingle packet
  1374. * @param mtype - the content type(audio, video, etc.)
  1375. */
  1376. function createDescriptionNode(jingle, mtype) {
  1377. var content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1378. mtype + "\"]");
  1379. if(!content || !content.length) {
  1380. $(jingle.tree()).find(">jingle").append(
  1381. "<content name=\"" + mtype + "\"></content>");
  1382. content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1383. mtype + "\"]");
  1384. }
  1385. var desc = content.find(">description");
  1386. if(!desc || !desc.length) {
  1387. content.append("<description " +
  1388. "xmlns=\"urn:xmpp:jingle:apps:rtp:1\" media=\"" +
  1389. mtype + "\"></description>");
  1390. desc = content.find(">description");
  1391. }
  1392. return desc;
  1393. }
  1394. /**
  1395. * Extracts the ice username fragment from an SDP string.
  1396. */
  1397. function getUfrag(sdp) {
  1398. var ufragLines = sdp.split('\n').filter(function(line) {
  1399. return line.startsWith("a=ice-ufrag:");});
  1400. if (ufragLines.length > 0) {
  1401. return ufragLines[0].substr("a=ice-ufrag:".length);
  1402. }
  1403. }
  1404. module.exports = JingleSessionPC;