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

JingleSessionPC.js 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. stream.onaddtrack = function (event) {
  1090. self.remoteTrackAdded(event.target, event.track);
  1091. };
  1092. stream.onremovetrack = function (event) {
  1093. self.remoteTrackRemoved(event.target, event.track);
  1094. };
  1095. }
  1096. // Call remoteTrackAdded for each track in the stream
  1097. stream.getAudioTracks().forEach(function (track) {
  1098. self.remoteTrackAdded(stream, track);
  1099. });
  1100. stream.getVideoTracks().forEach(function (track) {
  1101. self.remoteTrackAdded(stream, track);
  1102. });
  1103. };
  1104. /**
  1105. * Called on "track added" and "stream added" PeerConnection events(cause we
  1106. * handle streams on per track basis). Does find the owner and the SSRC for
  1107. * the track and passes that to ChatRoom for further processing.
  1108. * @param stream WebRTC MediaStream instance which is the parent of the track
  1109. * @param track the WebRTC MediaStreamTrack added for remote participant
  1110. */
  1111. JingleSessionPC.prototype.remoteTrackAdded = function (stream, track) {
  1112. logger.info("Remote track added", stream, track);
  1113. var streamId = RTC.getStreamID(stream);
  1114. var mediaType = track.kind;
  1115. // This is our event structure which will be passed by the ChatRoom as
  1116. // XMPPEvents.REMOTE_TRACK_ADDED data
  1117. var jitsiTrackAddedEvent = {
  1118. stream: stream,
  1119. track: track,
  1120. mediaType: track.kind, /* 'audio' or 'video' */
  1121. owner: undefined, /* to be determined below */
  1122. muted: null /* will be set in the ChatRoom */
  1123. };
  1124. try{
  1125. // look up an associated JID for a stream id
  1126. if (!mediaType) {
  1127. logger.error("MediaType undefined", track);
  1128. throw new Error("MediaType undefined for remote track");
  1129. }
  1130. var remoteSDP = new SDP(this.peerconnection.remoteDescription.sdp);
  1131. var medialines = remoteSDP.media.filter(function (mediaLines){
  1132. return mediaLines.startsWith("m=" + mediaType);
  1133. });
  1134. if (!medialines.length) {
  1135. logger.error("No media for type " + mediaType + " found in remote SDP");
  1136. throw new Error("No media for type " + mediaType +
  1137. " found in remote SDP for remote track");
  1138. }
  1139. var ssrclines = SDPUtil.find_lines(medialines[0], 'a=ssrc:');
  1140. ssrclines = ssrclines.filter(function (line) {
  1141. var msid = RTCBrowserType.isTemasysPluginUsed() ? 'mslabel' : 'msid';
  1142. return line.indexOf(msid + ':' + streamId) !== -1;
  1143. });
  1144. var thessrc;
  1145. if (ssrclines.length) {
  1146. thessrc = ssrclines[0].substring(7).split(' ')[0];
  1147. if (!this.ssrcOwners[thessrc]) {
  1148. logger.error("No SSRC owner known for: " + thessrc);
  1149. throw new Error("No SSRC owner known for: " + thessrc +
  1150. " for remote track");
  1151. }
  1152. jitsiTrackAddedEvent.owner = this.ssrcOwners[thessrc];
  1153. logger.log('associated jid', this.ssrcOwners[thessrc], thessrc);
  1154. } else {
  1155. logger.error("No SSRC lines for ", streamId);
  1156. throw new Error("No SSRC lines for streamId " + streamId +
  1157. " for remote track");
  1158. }
  1159. jitsiTrackAddedEvent.ssrc = thessrc;
  1160. this.room.remoteTrackAdded(jitsiTrackAddedEvent);
  1161. } catch (error) {
  1162. GlobalOnErrorHandler.callErrorHandler(error);
  1163. }
  1164. };
  1165. /**
  1166. * Handles remote stream removal.
  1167. * @param stream the WebRTC MediaStream object which is being removed from the
  1168. * PeerConnection
  1169. */
  1170. JingleSessionPC.prototype.remoteStreamRemoved = function (stream) {
  1171. var self = this;
  1172. if (!RTC.isUserStream(stream)) {
  1173. logger.info(
  1174. "Ignored remote 'stream removed' event for non-user stream", stream);
  1175. return;
  1176. }
  1177. // Call remoteTrackRemoved for each track in the stream
  1178. stream.getVideoTracks().forEach(function(track){
  1179. self.remoteTrackRemoved(stream, track);
  1180. });
  1181. stream.getAudioTracks().forEach(function(track) {
  1182. self.remoteTrackRemoved(stream, track);
  1183. });
  1184. };
  1185. /**
  1186. * Handles remote media track removal.
  1187. * @param stream WebRTC MediaStream instance which is the parent of the track
  1188. * @param track the WebRTC MediaStreamTrack which has been removed from
  1189. * the PeerConnection.
  1190. */
  1191. JingleSessionPC.prototype.remoteTrackRemoved = function (stream, track) {
  1192. logger.info("Remote track removed", stream, track);
  1193. var streamId = RTC.getStreamID(stream);
  1194. var trackId = track && track.id;
  1195. try{
  1196. if (!streamId) {
  1197. logger.error("No stream ID for", stream);
  1198. throw new Error("Remote track removal failed - No stream ID");
  1199. }
  1200. if (!trackId) {
  1201. logger.error("No track ID for", track);
  1202. throw new Error("Remote track removal failed - No track ID");
  1203. }
  1204. this.room.eventEmitter.emit(
  1205. XMPPEvents.REMOTE_TRACK_REMOVED, streamId, trackId);
  1206. } catch (error) {
  1207. GlobalOnErrorHandler.callErrorHandler(error);
  1208. }
  1209. };
  1210. /**
  1211. * Returns the ice connection state for the peer connection.
  1212. * @returns the ice connection state for the peer connection.
  1213. */
  1214. JingleSessionPC.prototype.getIceConnectionState = function () {
  1215. return this.peerconnection.iceConnectionState;
  1216. };
  1217. /**
  1218. * Closes the peerconnection.
  1219. */
  1220. JingleSessionPC.prototype.close = function () {
  1221. this.closed = true;
  1222. // do not try to close if already closed.
  1223. this.peerconnection
  1224. && ((this.peerconnection.signalingState
  1225. && this.peerconnection.signalingState !== 'closed')
  1226. || (this.peerconnection.connectionState
  1227. && this.peerconnection.connectionState !== 'closed'))
  1228. && this.peerconnection.close();
  1229. };
  1230. /**
  1231. * Fixes the outgoing jingle packets by removing the nodes related to the
  1232. * muted/unmuted streams, handles removing of muted stream, etc.
  1233. * @param jingle the jingle packet that is going to be sent
  1234. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1235. */
  1236. JingleSessionPC.prototype.fixJingle = function(jingle) {
  1237. var action = $(jingle.nodeTree).find("jingle").attr("action");
  1238. switch (action) {
  1239. case "source-add":
  1240. case "session-accept":
  1241. this.fixSourceAddJingle(jingle);
  1242. break;
  1243. case "source-remove":
  1244. this.fixSourceRemoveJingle(jingle);
  1245. break;
  1246. default:
  1247. var errmsg = "Unknown jingle action!";
  1248. GlobalOnErrorHandler.callErrorHandler(errmsg);
  1249. logger.error(errmsg);
  1250. return false;
  1251. }
  1252. var sources = $(jingle.tree()).find(">jingle>content>description>source");
  1253. return sources && sources.length > 0;
  1254. };
  1255. /**
  1256. * Fixes the outgoing jingle packets with action source-add by removing the
  1257. * nodes related to the unmuted streams
  1258. * @param jingle the jingle packet that is going to be sent
  1259. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1260. */
  1261. JingleSessionPC.prototype.fixSourceAddJingle = function (jingle) {
  1262. var ssrcs = this.modifiedSSRCs["unmute"];
  1263. this.modifiedSSRCs["unmute"] = [];
  1264. if(ssrcs && ssrcs.length) {
  1265. ssrcs.forEach(function (ssrcObj) {
  1266. var desc = $(jingle.tree()).find(">jingle>content[name=\"" +
  1267. ssrcObj.mtype + "\"]>description");
  1268. if(!desc || !desc.length)
  1269. return;
  1270. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1271. var sourceNode = desc.find(">source[ssrc=\"" +
  1272. ssrc + "\"]");
  1273. sourceNode.remove();
  1274. });
  1275. ssrcObj.ssrc.groups.forEach(function (group) {
  1276. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1277. group.group.semantics + "\"]:has(source[ssrc=\"" +
  1278. group.primarySSRC +
  1279. "\"])");
  1280. groupNode.remove();
  1281. });
  1282. });
  1283. }
  1284. ssrcs = this.modifiedSSRCs["addMuted"];
  1285. this.modifiedSSRCs["addMuted"] = [];
  1286. if(ssrcs && ssrcs.length) {
  1287. ssrcs.forEach(function (ssrcObj) {
  1288. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1289. var cname = Math.random().toString(36).substring(2);
  1290. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1291. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1292. sourceNode.remove();
  1293. var sourceXML = "<source " +
  1294. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1295. ssrc + "\">" +
  1296. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1297. " value=\"" + ssrcObj.msid + "\" name=\"msid\"/>" +
  1298. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1299. " value=\"" + cname + "\" name=\"cname\" />" + "</source>";
  1300. desc.append(sourceXML);
  1301. });
  1302. ssrcObj.ssrc.groups.forEach(function (group) {
  1303. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1304. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1305. "\"])");
  1306. groupNode.remove();
  1307. desc.append("<ssrc-group semantics=\"" +
  1308. group.group.semantics +
  1309. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1310. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1311. "</ssrc-group>");
  1312. });
  1313. });
  1314. }
  1315. };
  1316. /**
  1317. * Fixes the outgoing jingle packets with action source-remove by removing the
  1318. * nodes related to the muted streams, handles removing of muted stream
  1319. * @param jingle the jingle packet that is going to be sent
  1320. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1321. */
  1322. JingleSessionPC.prototype.fixSourceRemoveJingle = function(jingle) {
  1323. var ssrcs = this.modifiedSSRCs["mute"];
  1324. this.modifiedSSRCs["mute"] = [];
  1325. if(ssrcs && ssrcs.length)
  1326. ssrcs.forEach(function (ssrcObj) {
  1327. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1328. var sourceNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1329. ssrcObj.mtype + "\"]>description>source[ssrc=\"" +
  1330. ssrc + "\"]");
  1331. sourceNode.remove();
  1332. });
  1333. ssrcObj.ssrc.groups.forEach(function (group) {
  1334. var groupNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1335. ssrcObj.mtype + "\"]>description>ssrc-group[semantics=\"" +
  1336. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1337. "\"])");
  1338. groupNode.remove();
  1339. });
  1340. });
  1341. ssrcs = this.modifiedSSRCs["remove"];
  1342. this.modifiedSSRCs["remove"] = [];
  1343. if(ssrcs && ssrcs.length)
  1344. ssrcs.forEach(function (ssrcObj) {
  1345. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1346. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1347. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1348. if(!sourceNode || !sourceNode.length) {
  1349. //Maybe we have to include cname, msid, etc here?
  1350. desc.append("<source " +
  1351. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1352. ssrc + "\"></source>");
  1353. }
  1354. });
  1355. ssrcObj.ssrc.groups.forEach(function (group) {
  1356. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1357. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1358. "\"])");
  1359. if(!groupNode || !groupNode.length) {
  1360. desc.append("<ssrc-group semantics=\"" +
  1361. group.group.semantics +
  1362. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1363. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1364. "</ssrc-group>");
  1365. }
  1366. });
  1367. });
  1368. };
  1369. /**
  1370. * Returns the description node related to the passed content type. If the node
  1371. * doesn't exists it will be created.
  1372. * @param jingle - the jingle packet
  1373. * @param mtype - the content type(audio, video, etc.)
  1374. */
  1375. function createDescriptionNode(jingle, mtype) {
  1376. var content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1377. mtype + "\"]");
  1378. if(!content || !content.length) {
  1379. $(jingle.tree()).find(">jingle").append(
  1380. "<content name=\"" + mtype + "\"></content>");
  1381. content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1382. mtype + "\"]");
  1383. }
  1384. var desc = content.find(">description");
  1385. if(!desc || !desc.length) {
  1386. content.append("<description " +
  1387. "xmlns=\"urn:xmpp:jingle:apps:rtp:1\" media=\"" +
  1388. mtype + "\"></description>");
  1389. desc = content.find(">description");
  1390. }
  1391. return desc;
  1392. }
  1393. /**
  1394. * Extracts the ice username fragment from an SDP string.
  1395. */
  1396. function getUfrag(sdp) {
  1397. var ufragLines = sdp.split('\n').filter(function(line) {
  1398. return line.startsWith("a=ice-ufrag:");});
  1399. if (ufragLines.length > 0) {
  1400. return ufragLines[0].substr("a=ice-ufrag:".length);
  1401. }
  1402. }
  1403. module.exports = JingleSessionPC;