You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JingleSessionPC.js 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /* jshint -W117 */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JingleSession = require("./JingleSession");
  4. var TraceablePeerConnection = require("./TraceablePeerConnection");
  5. var SDPDiffer = require("./SDPDiffer");
  6. var SDPUtil = require("./SDPUtil");
  7. var SDP = require("./SDP");
  8. var async = require("async");
  9. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  10. var RTCBrowserType = require("../RTC/RTCBrowserType");
  11. var RTC = require("../RTC/RTC");
  12. // Jingle stuff
  13. function JingleSessionPC(me, sid, connection, service) {
  14. JingleSession.call(this, me, sid, connection, service);
  15. this.initiator = null;
  16. this.responder = null;
  17. this.peerjid = null;
  18. this.state = null;
  19. this.localSDP = null;
  20. this.remoteSDP = null;
  21. this.usetrickle = true;
  22. this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
  23. this.hadstuncandidate = false;
  24. this.hadturncandidate = false;
  25. this.lasticecandidate = false;
  26. this.reason = null;
  27. this.addssrc = [];
  28. this.removessrc = [];
  29. this.pendingop = null;
  30. this.modifyingLocalStreams = false;
  31. this.modifiedSSRCs = {};
  32. /**
  33. * A map that stores SSRCs of remote streams. And is used only locally
  34. * We store the mapping when jingle is received, and later is used
  35. * onaddstream webrtc event where we have only the ssrc
  36. * FIXME: This map got filled and never cleaned and can grow durring long
  37. * conference
  38. * @type {{}} maps SSRC number to jid
  39. */
  40. this.ssrcOwners = {};
  41. this.webrtcIceUdpDisable = !!this.service.options.webrtcIceUdpDisable;
  42. this.webrtcIceTcpDisable = !!this.service.options.webrtcIceTcpDisable;
  43. this.modifySourcesQueue = async.queue(this._modifySources.bind(this), 1);
  44. // We start with the queue paused. We resume it when the signaling state is
  45. // stable and the ice connection state is connected.
  46. this.modifySourcesQueue.pause();
  47. }
  48. //XXX this is badly broken...
  49. JingleSessionPC.prototype = JingleSession.prototype;
  50. JingleSessionPC.prototype.constructor = JingleSessionPC;
  51. JingleSessionPC.prototype.setOffer = function(offer) {
  52. this.setRemoteDescription(offer, 'offer');
  53. };
  54. JingleSessionPC.prototype.setAnswer = function(answer) {
  55. this.setRemoteDescription(answer, 'answer');
  56. };
  57. JingleSessionPC.prototype.updateModifySourcesQueue = function() {
  58. var signalingState = this.peerconnection.signalingState;
  59. var iceConnectionState = this.peerconnection.iceConnectionState;
  60. if (signalingState === 'stable' && iceConnectionState === 'connected') {
  61. this.modifySourcesQueue.resume();
  62. } else {
  63. this.modifySourcesQueue.pause();
  64. }
  65. };
  66. JingleSessionPC.prototype.doInitialize = function () {
  67. var self = this;
  68. this.hadstuncandidate = false;
  69. this.hadturncandidate = false;
  70. this.lasticecandidate = false;
  71. // True if reconnect is in progress
  72. this.isreconnect = false;
  73. // Set to true if the connection was ever stable
  74. this.wasstable = false;
  75. this.peerconnection = new TraceablePeerConnection(
  76. this.connection.jingle.ice_config,
  77. RTC.getPCConstraints(),
  78. this);
  79. this.peerconnection.onicecandidate = function (ev) {
  80. if (!ev) {
  81. // There was an incomplete check for ev before which left the last
  82. // line of the function unprotected from a potential throw of an
  83. // exception. Consequently, it may be argued that the check is
  84. // unnecessary. Anyway, I'm leaving it and making the check
  85. // complete.
  86. return;
  87. }
  88. var candidate = ev.candidate;
  89. if (candidate) {
  90. // Discard candidates of disabled protocols.
  91. var protocol = candidate.protocol;
  92. if (typeof protocol === 'string') {
  93. protocol = protocol.toLowerCase();
  94. if (protocol == 'tcp') {
  95. if (self.webrtcIceTcpDisable)
  96. return;
  97. } else if (protocol == 'udp') {
  98. if (self.webrtcIceUdpDisable)
  99. return;
  100. }
  101. }
  102. }
  103. self.sendIceCandidate(candidate);
  104. };
  105. this.peerconnection.onaddstream = function (event) {
  106. if (event.stream.id !== 'default') {
  107. logger.log("REMOTE STREAM ADDED: ", event.stream , event.stream.id);
  108. self.remoteStreamAdded(event);
  109. } else {
  110. // This is a recvonly stream. Clients that implement Unified Plan,
  111. // such as Firefox use recvonly "streams/channels/tracks" for
  112. // receiving remote stream/tracks, as opposed to Plan B where there
  113. // are only 3 channels: audio, video and data.
  114. logger.log("RECVONLY REMOTE STREAM IGNORED: " + event.stream + " - " + event.stream.id);
  115. }
  116. };
  117. this.peerconnection.onremovestream = function (event) {
  118. // Remove the stream from remoteStreams
  119. if (event.stream.id !== 'default') {
  120. logger.log("REMOTE STREAM REMOVED: ", event.stream , event.stream.id);
  121. self.remoteStreamRemoved(event);
  122. } else {
  123. // This is a recvonly stream. Clients that implement Unified Plan,
  124. // such as Firefox use recvonly "streams/channels/tracks" for
  125. // receiving remote stream/tracks, as opposed to Plan B where there
  126. // are only 3 channels: audio, video and data.
  127. logger.log("RECVONLY REMOTE STREAM IGNORED: " + event.stream + " - " + event.stream.id);
  128. }
  129. };
  130. this.peerconnection.onsignalingstatechange = function (event) {
  131. if (!(self && self.peerconnection)) return;
  132. if (self.peerconnection.signalingState === 'stable') {
  133. self.wasstable = true;
  134. }
  135. self.updateModifySourcesQueue();
  136. };
  137. /**
  138. * The oniceconnectionstatechange event handler contains the code to execute when the iceconnectionstatechange event,
  139. * of type Event, is received by this RTCPeerConnection. Such an event is sent when the value of
  140. * RTCPeerConnection.iceConnectionState changes.
  141. *
  142. * @param event the event containing information about the change
  143. */
  144. this.peerconnection.oniceconnectionstatechange = function (event) {
  145. if (!(self && self.peerconnection)) return;
  146. logger.log("(TIME) ICE " + self.peerconnection.iceConnectionState +
  147. ":\t", window.performance.now());
  148. self.updateModifySourcesQueue();
  149. switch (self.peerconnection.iceConnectionState) {
  150. case 'connected':
  151. // Informs interested parties that the connection has been restored.
  152. if (self.peerconnection.signalingState === 'stable' && self.isreconnect)
  153. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_RESTORED);
  154. self.isreconnect = false;
  155. break;
  156. case 'disconnected':
  157. self.isreconnect = true;
  158. // Informs interested parties that the connection has been interrupted.
  159. if (self.wasstable)
  160. self.room.eventEmitter.emit(XMPPEvents.CONNECTION_INTERRUPTED);
  161. break;
  162. case 'failed':
  163. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  164. break;
  165. }
  166. };
  167. this.peerconnection.onnegotiationneeded = function (event) {
  168. self.room.eventEmitter.emit(XMPPEvents.PEERCONNECTION_READY, self);
  169. };
  170. };
  171. JingleSessionPC.prototype.accept = function () {
  172. this.state = 'active';
  173. var pranswer = this.peerconnection.localDescription;
  174. if (!pranswer || pranswer.type != 'pranswer') {
  175. return;
  176. }
  177. logger.log('going from pranswer to answer');
  178. if (this.usetrickle) {
  179. // remove candidates already sent from session-accept
  180. var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
  181. for (var i = 0; i < lines.length; i++) {
  182. pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
  183. }
  184. }
  185. while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
  186. // FIXME: change any inactive to sendrecv or whatever they were originally
  187. pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
  188. }
  189. var prsdp = new SDP(pranswer.sdp);
  190. if (this.webrtcIceTcpDisable) {
  191. prsdp.removeTcpCandidates = true;
  192. }
  193. if (this.webrtcIceUdpDisable) {
  194. prsdp.removeUdpCandidates = true;
  195. }
  196. var accept = $iq({to: this.peerjid,
  197. type: 'set'})
  198. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  199. action: 'session-accept',
  200. initiator: this.initiator,
  201. responder: this.responder,
  202. sid: this.sid });
  203. // FIXME why do we generate session-accept in 3 different places ?
  204. prsdp.toJingle(
  205. accept,
  206. this.initiator == this.me ? 'initiator' : 'responder');
  207. var sdp = this.peerconnection.localDescription.sdp;
  208. while (SDPUtil.find_line(sdp, 'a=inactive')) {
  209. // FIXME: change any inactive to sendrecv or whatever they were originally
  210. sdp = sdp.replace('a=inactive', 'a=sendrecv');
  211. }
  212. var self = this;
  213. this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
  214. function () {
  215. self.connection.sendIQ(accept,
  216. function () {
  217. var ack = {};
  218. ack.source = 'answer';
  219. $(document).trigger('ack.jingle', [self.sid, ack]);
  220. },
  221. function (stanza) {
  222. var error = ($(stanza).find('error').length) ? {
  223. code: $(stanza).find('error').attr('code'),
  224. reason: $(stanza).find('error :first')[0].tagName
  225. }:{};
  226. error.source = 'answer';
  227. JingleSessionPC.onJingleError(self.sid, error);
  228. },
  229. 10000);
  230. },
  231. function (e) {
  232. logger.error('setLocalDescription failed', e);
  233. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  234. }
  235. );
  236. };
  237. JingleSessionPC.prototype.terminate = function (reason) {
  238. this.state = 'ended';
  239. this.reason = reason;
  240. this.peerconnection.close();
  241. };
  242. JingleSessionPC.prototype.active = function () {
  243. return this.state == 'active';
  244. };
  245. JingleSessionPC.prototype.sendIceCandidate = function (candidate) {
  246. var self = this;
  247. if (candidate && !this.lasticecandidate) {
  248. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  249. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  250. if (!(ice && jcand)) {
  251. logger.error('failed to get ice && jcand');
  252. return;
  253. }
  254. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  255. if (jcand.type === 'srflx') {
  256. this.hadstuncandidate = true;
  257. } else if (jcand.type === 'relay') {
  258. this.hadturncandidate = true;
  259. }
  260. if (this.usetrickle) {
  261. if (this.usedrip) {
  262. if (this.drip_container.length === 0) {
  263. // start 20ms callout
  264. window.setTimeout(function () {
  265. if (self.drip_container.length === 0) return;
  266. self.sendIceCandidates(self.drip_container);
  267. self.drip_container = [];
  268. }, 20);
  269. }
  270. this.drip_container.push(candidate);
  271. return;
  272. } else {
  273. self.sendIceCandidates([candidate]);
  274. // FIXME this.lasticecandidate is going to be set to true
  275. // bellow and that seems wrong. The execution doesn't come here
  276. // with the default values at the time of this writing.
  277. }
  278. }
  279. } else {
  280. //logger.log('sendIceCandidate: last candidate.');
  281. if (!this.usetrickle) {
  282. //logger.log('should send full offer now...');
  283. //FIXME why do we generate session-accept in 3 different places ?
  284. var init = $iq({to: this.peerjid,
  285. type: 'set'})
  286. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  287. action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
  288. initiator: this.initiator,
  289. sid: this.sid});
  290. this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
  291. if (self.webrtcIceTcpDisable) {
  292. this.localSDP.removeTcpCandidates = true;
  293. }
  294. if (self.webrtcIceUdpDisable) {
  295. this.localSDP.removeUdpCandidates = true;
  296. }
  297. var sendJingle = function (ssrc) {
  298. if(!ssrc)
  299. ssrc = {};
  300. self.localSDP.toJingle(
  301. init,
  302. self.initiator == self.me ? 'initiator' : 'responder',
  303. ssrc);
  304. self.connection.sendIQ(init,
  305. function () {
  306. //logger.log('session initiate ack');
  307. var ack = {};
  308. ack.source = 'offer';
  309. $(document).trigger('ack.jingle', [self.sid, ack]);
  310. },
  311. function (stanza) {
  312. self.state = 'error';
  313. self.peerconnection.close();
  314. var error = ($(stanza).find('error').length) ? {
  315. code: $(stanza).find('error').attr('code'),
  316. reason: $(stanza).find('error :first')[0].tagName,
  317. }:{};
  318. error.source = 'offer';
  319. JingleSessionPC.onJingleError(self.sid, error);
  320. },
  321. 10000);
  322. };
  323. sendJingle();
  324. }
  325. this.lasticecandidate = true;
  326. logger.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  327. logger.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  328. if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
  329. $(document).trigger('nostuncandidates.jingle', [this.sid]);
  330. }
  331. }
  332. };
  333. JingleSessionPC.prototype.sendIceCandidates = function (candidates) {
  334. logger.log('sendIceCandidates', candidates);
  335. var cand = $iq({to: this.peerjid, type: 'set'})
  336. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  337. action: 'transport-info',
  338. initiator: this.initiator,
  339. sid: this.sid});
  340. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  341. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  342. var mline = SDPUtil.parse_mline(this.localSDP.media[mid].split('\r\n')[0]);
  343. if (cands.length > 0) {
  344. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  345. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  346. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  347. name: (cands[0].sdpMid? cands[0].sdpMid : mline.media)
  348. }).c('transport', ice);
  349. for (var i = 0; i < cands.length; i++) {
  350. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  351. }
  352. // add fingerprint
  353. var fingerprint_line = SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session);
  354. if (fingerprint_line) {
  355. var tmp = SDPUtil.parse_fingerprint(fingerprint_line);
  356. tmp.required = true;
  357. cand.c(
  358. 'fingerprint',
  359. {xmlns: 'urn:xmpp:jingle:apps:dtls:0'})
  360. .t(tmp.fingerprint);
  361. delete tmp.fingerprint;
  362. cand.attrs(tmp);
  363. cand.up();
  364. }
  365. cand.up(); // transport
  366. cand.up(); // content
  367. }
  368. }
  369. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  370. //logger.log('was this the last candidate', this.lasticecandidate);
  371. this.connection.sendIQ(cand,
  372. function () {
  373. var ack = {};
  374. ack.source = 'transportinfo';
  375. $(document).trigger('ack.jingle', [this.sid, ack]);
  376. },
  377. function (stanza) {
  378. var error = ($(stanza).find('error').length) ? {
  379. code: $(stanza).find('error').attr('code'),
  380. reason: $(stanza).find('error :first')[0].tagName,
  381. }:{};
  382. error.source = 'transportinfo';
  383. JingleSessionPC.onJingleError(this.sid, error);
  384. },
  385. 10000);
  386. };
  387. JingleSessionPC.prototype.readSsrcInfo = function (contents) {
  388. var self = this;
  389. $(contents).each(function (idx, content) {
  390. var name = $(content).attr('name');
  391. var mediaType = this.getAttribute('name');
  392. var ssrcs = $(content).find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  393. ssrcs.each(function () {
  394. var ssrc = this.getAttribute('ssrc');
  395. $(this).find('>ssrc-info[xmlns="http://jitsi.org/jitmeet"]').each(
  396. function () {
  397. var owner = this.getAttribute('owner');
  398. self.ssrcOwners[ssrc] = owner;
  399. }
  400. );
  401. });
  402. });
  403. };
  404. JingleSessionPC.prototype.setRemoteDescription = function (elem, desctype) {
  405. //logger.log('setting remote description... ', desctype);
  406. this.remoteSDP = new SDP('');
  407. if (this.webrtcIceTcpDisable) {
  408. this.remoteSDP.removeTcpCandidates = true;
  409. }
  410. if (this.webrtcIceUdpDisable) {
  411. this.remoteSDP.removeUdpCandidates = true;
  412. }
  413. this.remoteSDP.fromJingle(elem);
  414. this.readSsrcInfo($(elem).find(">content"));
  415. var pcremotedesc = this.peerconnection.remoteDescription;
  416. if (pcremotedesc) {
  417. logger.log('setRemoteDescription when remote description is not null, should be pranswer', pcremotedesc);
  418. if (pcremotedesc.type == 'pranswer') {
  419. var pranswer = new SDP(pcremotedesc.sdp);
  420. for (var i = 0; i < pranswer.media.length; i++) {
  421. // make sure we have ice ufrag and pwd
  422. if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
  423. var ice_ufrag_line = SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session);
  424. if (ice_ufrag_line) {
  425. this.remoteSDP.media[i] += ice_ufrag_line + '\r\n';
  426. } else {
  427. logger.warn('no ice ufrag?');
  428. }
  429. var ice_pwd_line = SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session);
  430. if (ice_pwd_line) {
  431. this.remoteSDP.media[i] += ice_pwd_line + '\r\n';
  432. } else {
  433. logger.warn('no ice pwd?');
  434. }
  435. }
  436. // copy over candidates
  437. var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
  438. for (var j = 0; j < lines.length; j++) {
  439. this.remoteSDP.media[i] += lines[j] + '\r\n';
  440. }
  441. }
  442. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  443. }
  444. }
  445. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  446. this.peerconnection.setRemoteDescription(remotedesc,
  447. function () {
  448. //logger.log('setRemoteDescription success');
  449. },
  450. function (e) {
  451. logger.error('setRemoteDescription error', e);
  452. JingleSessionPC.onJingleFatalError(self, e);
  453. }
  454. );
  455. };
  456. /**
  457. * Adds remote ICE candidates to this Jingle session.
  458. * @param elem An array of Jingle "content" elements?
  459. */
  460. JingleSessionPC.prototype.addIceCandidate = function (elem) {
  461. var self = this;
  462. if (this.peerconnection.signalingState == 'closed') {
  463. return;
  464. }
  465. if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
  466. logger.log('trickle ice candidate arriving before session accept...');
  467. // create a PRANSWER for setRemoteDescription
  468. if (!this.remoteSDP) {
  469. var cobbled = 'v=0\r\n' +
  470. 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
  471. 's=-\r\n' +
  472. 't=0 0\r\n';
  473. // first, take some things from the local description
  474. for (var i = 0; i < this.localSDP.media.length; i++) {
  475. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
  476. cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
  477. var mid_line = SDPUtil.find_line(this.localSDP.media[i], 'a=mid:');
  478. if (mid_line) {
  479. cobbled += mid_line + '\r\n';
  480. }
  481. cobbled += 'a=inactive\r\n';
  482. }
  483. this.remoteSDP = new SDP(cobbled);
  484. }
  485. // then add things like ice and dtls from remote candidate
  486. elem.each(function () {
  487. for (var i = 0; i < self.remoteSDP.media.length; i++) {
  488. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  489. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  490. if (!SDPUtil.find_line(self.remoteSDP.media[i], 'a=ice-ufrag:')) {
  491. var tmp = $(this).find('transport');
  492. self.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  493. self.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  494. tmp = $(this).find('transport>fingerprint');
  495. if (tmp.length) {
  496. self.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  497. } else {
  498. logger.log('no dtls fingerprint (webrtc issue #1718?)');
  499. self.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
  500. }
  501. break;
  502. }
  503. }
  504. }
  505. });
  506. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  507. // we need a complete SDP with ice-ufrag/ice-pwd in all parts
  508. // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
  509. // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
  510. var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
  511. return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
  512. }).length == this.remoteSDP.media.length;
  513. if (iscomplete) {
  514. logger.log('setting pranswer');
  515. try {
  516. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }),
  517. function() {
  518. },
  519. function(e) {
  520. logger.log('setRemoteDescription pranswer failed', e.toString());
  521. });
  522. } catch (e) {
  523. logger.error('setting pranswer failed', e);
  524. }
  525. } else {
  526. //logger.log('not yet setting pranswer');
  527. }
  528. }
  529. // operate on each content element
  530. elem.each(function () {
  531. // would love to deactivate this, but firefox still requires it
  532. var idx = -1;
  533. var i;
  534. for (i = 0; i < self.remoteSDP.media.length; i++) {
  535. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  536. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  537. idx = i;
  538. break;
  539. }
  540. }
  541. if (idx == -1) { // fall back to localdescription
  542. for (i = 0; i < self.localSDP.media.length; i++) {
  543. if (SDPUtil.find_line(self.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  544. self.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  545. idx = i;
  546. break;
  547. }
  548. }
  549. }
  550. var name = $(this).attr('name');
  551. // TODO: check ice-pwd and ice-ufrag?
  552. $(this).find('transport>candidate').each(function () {
  553. var line, candidate;
  554. var protocol = this.getAttribute('protocol');
  555. protocol =
  556. (typeof protocol === 'string') ? protocol.toLowerCase() : '';
  557. if ((self.webrtcIceTcpDisable && protocol == 'tcp') ||
  558. (self.webrtcIceUdpDisable && protocol == 'udp')) {
  559. return;
  560. }
  561. line = SDPUtil.candidateFromJingle(this);
  562. candidate = new RTCIceCandidate({sdpMLineIndex: idx,
  563. sdpMid: name,
  564. candidate: line});
  565. try {
  566. self.peerconnection.addIceCandidate(candidate);
  567. } catch (e) {
  568. logger.error('addIceCandidate failed', e.toString(), line);
  569. self.room.eventEmitter.emit(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  570. err, self.peerconnection);
  571. }
  572. });
  573. });
  574. };
  575. JingleSessionPC.prototype.sendAnswer = function (provisional) {
  576. //logger.log('createAnswer', provisional);
  577. var self = this;
  578. this.peerconnection.createAnswer(
  579. function (sdp) {
  580. self.createdAnswer(sdp, provisional);
  581. },
  582. function (e) {
  583. logger.error('createAnswer failed', e);
  584. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  585. },
  586. this.media_constraints
  587. );
  588. };
  589. JingleSessionPC.prototype.createdAnswer = function (sdp, provisional) {
  590. //logger.log('createAnswer callback');
  591. var self = this;
  592. this.localSDP = new SDP(sdp.sdp);
  593. //this.localSDP.mangle();
  594. this.usepranswer = provisional === true;
  595. if (this.usetrickle) {
  596. if (this.usepranswer) {
  597. sdp.type = 'pranswer';
  598. for (var i = 0; i < this.localSDP.media.length; i++) {
  599. this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
  600. }
  601. this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
  602. }
  603. }
  604. var self = this;
  605. var sendJingle = function (ssrcs) {
  606. // FIXME why do we generate session-accept in 3 different places ?
  607. var accept = $iq({to: self.peerjid,
  608. type: 'set'})
  609. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  610. action: 'session-accept',
  611. initiator: self.initiator,
  612. responder: self.responder,
  613. sid: self.sid });
  614. if (self.webrtcIceTcpDisable) {
  615. self.localSDP.removeTcpCandidates = true;
  616. }
  617. if (self.webrtcIceUdpDisable) {
  618. self.localSDP.removeUdpCandidates = true;
  619. }
  620. self.localSDP.toJingle(
  621. accept,
  622. self.initiator == self.me ? 'initiator' : 'responder',
  623. ssrcs);
  624. self.fixJingle(accept);
  625. self.connection.sendIQ(accept,
  626. function () {
  627. var ack = {};
  628. ack.source = 'answer';
  629. $(document).trigger('ack.jingle', [self.sid, ack]);
  630. },
  631. function (stanza) {
  632. var error = ($(stanza).find('error').length) ? {
  633. code: $(stanza).find('error').attr('code'),
  634. reason: $(stanza).find('error :first')[0].tagName,
  635. }:{};
  636. error.source = 'answer';
  637. JingleSessionPC.onJingleError(self.sid, error);
  638. },
  639. 10000);
  640. }
  641. sdp.sdp = this.localSDP.raw;
  642. this.peerconnection.setLocalDescription(sdp,
  643. function () {
  644. //logger.log('setLocalDescription success');
  645. if (self.usetrickle && !self.usepranswer) {
  646. sendJingle();
  647. }
  648. },
  649. function (e) {
  650. logger.error('setLocalDescription failed', e);
  651. self.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  652. }
  653. );
  654. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  655. for (var j = 0; j < cands.length; j++) {
  656. var cand = SDPUtil.parse_icecandidate(cands[j]);
  657. if (cand.type == 'srflx') {
  658. this.hadstuncandidate = true;
  659. } else if (cand.type == 'relay') {
  660. this.hadturncandidate = true;
  661. }
  662. }
  663. };
  664. JingleSessionPC.prototype.sendTerminate = function (reason, text) {
  665. var self = this,
  666. term = $iq({to: this.peerjid,
  667. type: 'set'})
  668. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  669. action: 'session-terminate',
  670. initiator: this.initiator,
  671. sid: this.sid})
  672. .c('reason')
  673. .c(reason || 'success');
  674. if (text) {
  675. term.up().c('text').t(text);
  676. }
  677. this.connection.sendIQ(term,
  678. function () {
  679. self.peerconnection.close();
  680. self.peerconnection = null;
  681. self.terminate();
  682. var ack = {};
  683. ack.source = 'terminate';
  684. $(document).trigger('ack.jingle', [self.sid, ack]);
  685. },
  686. function (stanza) {
  687. var error = ($(stanza).find('error').length) ? {
  688. code: $(stanza).find('error').attr('code'),
  689. reason: $(stanza).find('error :first')[0].tagName,
  690. }:{};
  691. $(document).trigger('ack.jingle', [self.sid, error]);
  692. },
  693. 10000);
  694. };
  695. /**
  696. * Handles a Jingle source-add message for this Jingle session.
  697. * @param elem An array of Jingle "content" elements.
  698. */
  699. JingleSessionPC.prototype.addSource = function (elem) {
  700. var self = this;
  701. // FIXME: dirty waiting
  702. if (!this.peerconnection.localDescription)
  703. {
  704. logger.warn("addSource - localDescription not ready yet")
  705. setTimeout(function()
  706. {
  707. self.addSource(elem);
  708. },
  709. 200
  710. );
  711. return;
  712. }
  713. logger.log('addssrc', new Date().getTime());
  714. logger.log('ice', this.peerconnection.iceConnectionState);
  715. this.readSsrcInfo(elem);
  716. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  717. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  718. $(elem).each(function (idx, content) {
  719. var name = $(content).attr('name');
  720. var lines = '';
  721. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  722. var semantics = this.getAttribute('semantics');
  723. var ssrcs = $(this).find('>source').map(function () {
  724. return this.getAttribute('ssrc');
  725. }).get();
  726. if (ssrcs.length) {
  727. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  728. }
  729. });
  730. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  731. tmp.each(function () {
  732. var ssrc = $(this).attr('ssrc');
  733. if(mySdp.containsSSRC(ssrc)){
  734. /**
  735. * This happens when multiple participants change their streams at the same time and
  736. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  737. * addssrc are scheduled for update IQ. See
  738. */
  739. logger.warn("Got add stream request for my own ssrc: "+ssrc);
  740. return;
  741. }
  742. if (sdp.containsSSRC(ssrc)) {
  743. logger.warn("Source-add request for existing SSRC: " + ssrc);
  744. return;
  745. }
  746. $(this).find('>parameter').each(function () {
  747. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  748. if ($(this).attr('value') && $(this).attr('value').length)
  749. lines += ':' + $(this).attr('value');
  750. lines += '\r\n';
  751. });
  752. });
  753. sdp.media.forEach(function(media, idx) {
  754. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  755. return;
  756. sdp.media[idx] += lines;
  757. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  758. self.addssrc[idx] += lines;
  759. });
  760. sdp.raw = sdp.session + sdp.media.join('');
  761. });
  762. this.modifySourcesQueue.push(function() {
  763. // When a source is added and if this is FF, a new channel is allocated
  764. // for receiving the added source. We need to diffuse the SSRC of this
  765. // new recvonly channel to the rest of the peers.
  766. logger.log('modify sources done');
  767. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  768. logger.log("SDPs", mySdp, newSdp);
  769. self.notifyMySSRCUpdate(mySdp, newSdp);
  770. });
  771. };
  772. /**
  773. * Handles a Jingle source-remove message for this Jingle session.
  774. * @param elem An array of Jingle "content" elements.
  775. */
  776. JingleSessionPC.prototype.removeSource = function (elem) {
  777. var self = this;
  778. // FIXME: dirty waiting
  779. if (!this.peerconnection.localDescription) {
  780. logger.warn("removeSource - localDescription not ready yet");
  781. setTimeout(function() {
  782. self.removeSource(elem);
  783. },
  784. 200
  785. );
  786. return;
  787. }
  788. logger.log('removessrc', new Date().getTime());
  789. logger.log('ice', this.peerconnection.iceConnectionState);
  790. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  791. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  792. $(elem).each(function (idx, content) {
  793. var name = $(content).attr('name');
  794. var lines = '';
  795. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  796. var semantics = this.getAttribute('semantics');
  797. var ssrcs = $(this).find('>source').map(function () {
  798. return this.getAttribute('ssrc');
  799. }).get();
  800. if (ssrcs.length) {
  801. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  802. }
  803. });
  804. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  805. tmp.each(function () {
  806. var ssrc = $(this).attr('ssrc');
  807. // This should never happen, but can be useful for bug detection
  808. if(mySdp.containsSSRC(ssrc)){
  809. logger.error("Got remove stream request for my own ssrc: "+ssrc);
  810. return;
  811. }
  812. $(this).find('>parameter').each(function () {
  813. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  814. if ($(this).attr('value') && $(this).attr('value').length)
  815. lines += ':' + $(this).attr('value');
  816. lines += '\r\n';
  817. });
  818. });
  819. sdp.media.forEach(function(media, idx) {
  820. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  821. return;
  822. sdp.media[idx] += lines;
  823. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  824. self.removessrc[idx] += lines;
  825. });
  826. sdp.raw = sdp.session + sdp.media.join('');
  827. });
  828. this.modifySourcesQueue.push(function() {
  829. // When a source is removed and if this is FF, the recvonly channel that
  830. // receives the remote stream is deactivated . We need to diffuse the
  831. // recvonly SSRC removal to the rest of the peers.
  832. logger.log('modify sources done');
  833. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  834. logger.log("SDPs", mySdp, newSdp);
  835. self.notifyMySSRCUpdate(mySdp, newSdp);
  836. });
  837. };
  838. JingleSessionPC.prototype._modifySources = function (successCallback, queueCallback) {
  839. var self = this;
  840. if (this.peerconnection.signalingState == 'closed') return;
  841. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null
  842. || this.modifyingLocalStreams)){
  843. // There is nothing to do since scheduled job might have been
  844. // executed by another succeeding call
  845. if(successCallback){
  846. successCallback();
  847. }
  848. queueCallback();
  849. return;
  850. }
  851. // Reset switch streams flags
  852. this.modifyingLocalStreams = false;
  853. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  854. // add sources
  855. this.addssrc.forEach(function(lines, idx) {
  856. sdp.media[idx] += lines;
  857. });
  858. this.addssrc = [];
  859. // remove sources
  860. this.removessrc.forEach(function(lines, idx) {
  861. lines = lines.split('\r\n');
  862. lines.pop(); // remove empty last element;
  863. lines.forEach(function(line) {
  864. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  865. });
  866. });
  867. this.removessrc = [];
  868. sdp.raw = sdp.session + sdp.media.join('');
  869. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  870. function() {
  871. if(self.signalingState == 'closed') {
  872. logger.error("createAnswer attempt on closed state");
  873. queueCallback("createAnswer attempt on closed state");
  874. return;
  875. }
  876. self.peerconnection.createAnswer(
  877. function(modifiedAnswer) {
  878. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  879. if (self.pendingop !== null) {
  880. var sdp = new SDP(modifiedAnswer.sdp);
  881. if (sdp.media.length > 1) {
  882. switch(self.pendingop) {
  883. case 'mute':
  884. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  885. break;
  886. case 'unmute':
  887. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  888. break;
  889. }
  890. sdp.raw = sdp.session + sdp.media.join('');
  891. modifiedAnswer.sdp = sdp.raw;
  892. }
  893. self.pendingop = null;
  894. }
  895. // FIXME: pushing down an answer while ice connection state
  896. // is still checking is bad...
  897. //logger.log(self.peerconnection.iceConnectionState);
  898. // trying to work around another chrome bug
  899. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  900. self.peerconnection.setLocalDescription(modifiedAnswer,
  901. function() {
  902. if(successCallback){
  903. successCallback();
  904. }
  905. queueCallback();
  906. },
  907. function(error) {
  908. logger.error('modified setLocalDescription failed', error);
  909. queueCallback(error);
  910. }
  911. );
  912. },
  913. function(error) {
  914. logger.error('modified answer failed', error);
  915. queueCallback(error);
  916. }
  917. );
  918. },
  919. function(error) {
  920. logger.error('modify failed', error);
  921. queueCallback(error);
  922. }
  923. );
  924. };
  925. /**
  926. * Adds stream.
  927. * @param stream new stream that will be added.
  928. * @param success_callback callback executed after successful stream addition.
  929. * @param ssrcInfo object with information about the SSRCs associated with the
  930. * stream.
  931. * @param dontModifySources {boolean} if true _modifySources won't be called.
  932. * Used for streams added before the call start.
  933. */
  934. JingleSessionPC.prototype.addStream = function (stream, callback, ssrcInfo,
  935. dontModifySources) {
  936. // Remember SDP to figure out added/removed SSRCs
  937. var oldSdp = null;
  938. if(this.peerconnection) {
  939. if(this.peerconnection.localDescription) {
  940. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  941. }
  942. //when adding muted stream we have to pass the ssrcInfo but we don't
  943. //have a stream
  944. if(stream || ssrcInfo)
  945. this.peerconnection.addStream(stream, ssrcInfo);
  946. }
  947. // Conference is not active
  948. if(!oldSdp || !this.peerconnection || dontModifySources) {
  949. if(ssrcInfo) {
  950. //available only on video unmute or when adding muted stream
  951. this.modifiedSSRCs[ssrcInfo.type] =
  952. this.modifiedSSRCs[ssrcInfo.type] || [];
  953. this.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  954. }
  955. callback();
  956. return;
  957. }
  958. this.modifyingLocalStreams = true;
  959. var self = this;
  960. this.modifySourcesQueue.push(function() {
  961. logger.log('modify sources done');
  962. if(ssrcInfo) {
  963. //available only on video unmute or when adding muted stream
  964. self.modifiedSSRCs[ssrcInfo.type] =
  965. self.modifiedSSRCs[ssrcInfo.type] || [];
  966. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  967. }
  968. callback();
  969. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  970. logger.log("SDPs", oldSdp, newSdp);
  971. self.notifyMySSRCUpdate(oldSdp, newSdp);
  972. });
  973. }
  974. /**
  975. * Generate ssrc info object for a stream with the following properties:
  976. * - ssrcs - Array of the ssrcs associated with the stream.
  977. * - groups - Array of the groups associated with the stream.
  978. */
  979. JingleSessionPC.prototype.generateNewStreamSSRCInfo = function () {
  980. return this.peerconnection.generateNewStreamSSRCInfo();
  981. };
  982. /**
  983. * Remove streams.
  984. * @param stream stream that will be removed.
  985. * @param success_callback callback executed after successful stream addition.
  986. * @param ssrcInfo object with information about the SSRCs associated with the
  987. * stream.
  988. */
  989. JingleSessionPC.prototype.removeStream = function (stream, callback, ssrcInfo) {
  990. // Remember SDP to figure out added/removed SSRCs
  991. var oldSdp = null;
  992. if(this.peerconnection) {
  993. if(this.peerconnection.localDescription) {
  994. oldSdp = new SDP(this.peerconnection.localDescription.sdp);
  995. }
  996. if (RTCBrowserType.getBrowserType() ===
  997. RTCBrowserType.RTC_BROWSER_FIREFOX) {
  998. if(!stream)//There is nothing to be changed
  999. return;
  1000. var sender = null;
  1001. // On Firefox we don't replace MediaStreams as this messes up the
  1002. // m-lines (which can't be removed in Plan Unified) and brings a lot
  1003. // of complications. Instead, we use the RTPSender and remove just
  1004. // the track.
  1005. var track = null;
  1006. if(stream.getAudioTracks() && stream.getAudioTracks().length) {
  1007. track = stream.getAudioTracks()[0];
  1008. } else if(stream.getVideoTracks() && stream.getVideoTracks().length)
  1009. {
  1010. track = stream.getVideoTracks()[0];
  1011. }
  1012. if(!track) {
  1013. logger.log("Cannot remove tracks: no tracks.");
  1014. return;
  1015. }
  1016. // Find the right sender (for audio or video)
  1017. this.peerconnection.peerconnection.getSenders().some(function (s) {
  1018. if (s.track === track) {
  1019. sender = s;
  1020. return true;
  1021. }
  1022. });
  1023. if (sender) {
  1024. this.peerconnection.peerconnection.removeTrack(sender);
  1025. } else {
  1026. logger.log("Cannot remove tracks: no RTPSender.");
  1027. }
  1028. } else if(stream)
  1029. this.peerconnection.removeStream(stream, false, ssrcInfo);
  1030. // else
  1031. // NOTE: If there is no stream and the browser is not FF we still need to do
  1032. // some transformation in order to send remove-source for the muted
  1033. // streams. That's why we aren't calling return here.
  1034. }
  1035. // Conference is not active
  1036. if(!oldSdp || !this.peerconnection) {
  1037. callback();
  1038. return;
  1039. }
  1040. this.modifyingLocalStreams = true;
  1041. var self = this;
  1042. this.modifySourcesQueue.push(function() {
  1043. logger.log('modify sources done');
  1044. callback();
  1045. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  1046. if(ssrcInfo) {
  1047. self.modifiedSSRCs[ssrcInfo.type] =
  1048. self.modifiedSSRCs[ssrcInfo.type] || [];
  1049. self.modifiedSSRCs[ssrcInfo.type].push(ssrcInfo);
  1050. }
  1051. logger.log("SDPs", oldSdp, newSdp);
  1052. self.notifyMySSRCUpdate(oldSdp, newSdp);
  1053. });
  1054. }
  1055. /**
  1056. * Figures out added/removed ssrcs and send update IQs.
  1057. * @param old_sdp SDP object for old description.
  1058. * @param new_sdp SDP object for new description.
  1059. */
  1060. JingleSessionPC.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  1061. if (!(this.peerconnection.signalingState == 'stable' &&
  1062. this.peerconnection.iceConnectionState == 'connected')){
  1063. logger.log("Too early to send updates");
  1064. return;
  1065. }
  1066. // send source-remove IQ.
  1067. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  1068. var remove = $iq({to: this.peerjid, type: 'set'})
  1069. .c('jingle', {
  1070. xmlns: 'urn:xmpp:jingle:1',
  1071. action: 'source-remove',
  1072. initiator: this.initiator,
  1073. sid: this.sid
  1074. }
  1075. );
  1076. sdpDiffer.toJingle(remove);
  1077. var removed = this.fixJingle(remove);
  1078. if (removed && remove) {
  1079. logger.info("Sending source-remove", remove.tree());
  1080. this.connection.sendIQ(remove,
  1081. function (res) {
  1082. logger.info('got remove result', res);
  1083. },
  1084. function (err) {
  1085. logger.error('got remove error', err);
  1086. }
  1087. );
  1088. } else {
  1089. logger.log('removal not necessary');
  1090. }
  1091. // send source-add IQ.
  1092. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  1093. var add = $iq({to: this.peerjid, type: 'set'})
  1094. .c('jingle', {
  1095. xmlns: 'urn:xmpp:jingle:1',
  1096. action: 'source-add',
  1097. initiator: this.initiator,
  1098. sid: this.sid
  1099. }
  1100. );
  1101. sdpDiffer.toJingle(add);
  1102. var added = this.fixJingle(add);
  1103. if (added && add) {
  1104. logger.info("Sending source-add", add.tree());
  1105. this.connection.sendIQ(add,
  1106. function (res) {
  1107. logger.info('got add result', res);
  1108. },
  1109. function (err) {
  1110. logger.error('got add error', err);
  1111. }
  1112. );
  1113. } else {
  1114. logger.log('addition not necessary');
  1115. }
  1116. };
  1117. JingleSessionPC.onJingleError = function (session, error)
  1118. {
  1119. logger.error("Jingle error", error);
  1120. }
  1121. JingleSessionPC.onJingleFatalError = function (session, error)
  1122. {
  1123. this.room.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  1124. this.room.eventEmitter.emit(XMPPEvents.JINGLE_FATAL_ERROR, session, error);
  1125. }
  1126. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1127. JingleSessionPC.prototype.sendKeyframe = function () {
  1128. var pc = this.peerconnection;
  1129. logger.log('sendkeyframe', pc.iceConnectionState);
  1130. if (pc.iceConnectionState !== 'connected') return; // safe...
  1131. var self = this;
  1132. pc.setRemoteDescription(
  1133. pc.remoteDescription,
  1134. function () {
  1135. pc.createAnswer(
  1136. function (modifiedAnswer) {
  1137. pc.setLocalDescription(
  1138. modifiedAnswer,
  1139. function () {
  1140. // noop
  1141. },
  1142. function (error) {
  1143. logger.log('triggerKeyframe setLocalDescription failed', error);
  1144. self.room.eventEmitter.emit(XMPPEvents.SET_LOCAL_DESCRIPTION_ERROR);
  1145. }
  1146. );
  1147. },
  1148. function (error) {
  1149. logger.log('triggerKeyframe createAnswer failed', error);
  1150. self.room.eventEmitter.emit(XMPPEvents.CREATE_ANSWER_ERROR);
  1151. }
  1152. );
  1153. },
  1154. function (error) {
  1155. logger.log('triggerKeyframe setRemoteDescription failed', error);
  1156. eventEmitter.emit(XMPPEvents.SET_REMOTE_DESCRIPTION_ERROR);
  1157. }
  1158. );
  1159. }
  1160. JingleSessionPC.prototype.remoteStreamAdded = function (data, times) {
  1161. var self = this;
  1162. var thessrc;
  1163. var streamId = RTC.getStreamID(data.stream);
  1164. // look up an associated JID for a stream id
  1165. if (!streamId) {
  1166. logger.error("No stream ID for", data.stream);
  1167. } else if (streamId && streamId.indexOf('mixedmslabel') === -1) {
  1168. // look only at a=ssrc: and _not_ at a=ssrc-group: lines
  1169. var ssrclines = this.peerconnection.remoteDescription?
  1170. SDPUtil.find_lines(this.peerconnection.remoteDescription.sdp, 'a=ssrc:') : [];
  1171. ssrclines = ssrclines.filter(function (line) {
  1172. // NOTE(gp) previously we filtered on the mslabel, but that property
  1173. // is not always present.
  1174. // return line.indexOf('mslabel:' + data.stream.label) !== -1;
  1175. if (RTCBrowserType.isTemasysPluginUsed()) {
  1176. return ((line.indexOf('mslabel:' + streamId) !== -1));
  1177. } else {
  1178. return ((line.indexOf('msid:' + streamId) !== -1));
  1179. }
  1180. });
  1181. if (ssrclines.length) {
  1182. thessrc = ssrclines[0].substring(7).split(' ')[0];
  1183. if (!self.ssrcOwners[thessrc]) {
  1184. logger.error("No SSRC owner known for: " + thessrc);
  1185. return;
  1186. }
  1187. data.peerjid = self.ssrcOwners[thessrc];
  1188. logger.log('associated jid', self.ssrcOwners[thessrc]);
  1189. } else {
  1190. logger.error("No SSRC lines for ", streamId);
  1191. }
  1192. }
  1193. this.room.remoteStreamAdded(data, this.sid, thessrc);
  1194. var isVideo = data.stream.getVideoTracks().length > 0;
  1195. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1196. if (isVideo &&
  1197. data.peerjid && this.peerjid === data.peerjid &&
  1198. data.stream.getVideoTracks().length === 0 &&
  1199. RTC.localVideo.getTracks().length > 0) {
  1200. window.setTimeout(function () {
  1201. self.sendKeyframe();
  1202. }, 3000);
  1203. }
  1204. }
  1205. /**
  1206. * Handles remote stream removal.
  1207. * @param event The event object associated with the removal.
  1208. */
  1209. JingleSessionPC.prototype.remoteStreamRemoved = function (event) {
  1210. var thessrc;
  1211. var streamId = RTC.getStreamID(event.stream);
  1212. if (!streamId) {
  1213. logger.error("No stream ID for", event.stream);
  1214. } else if (streamId && streamId.indexOf('mixedmslabel') === -1) {
  1215. this.room.eventEmitter.emit(XMPPEvents.REMOTE_STREAM_REMOVED, streamId);
  1216. }
  1217. }
  1218. /**
  1219. * Returns the ice connection state for the peer connection.
  1220. * @returns the ice connection state for the peer connection.
  1221. */
  1222. JingleSessionPC.prototype.getIceConnectionState = function () {
  1223. return this.peerconnection.iceConnectionState;
  1224. }
  1225. /**
  1226. * Fixes the outgoing jingle packets by removing the nodes related to the
  1227. * muted/unmuted streams, handles removing of muted stream, etc.
  1228. * @param jingle the jingle packet that is going to be sent
  1229. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1230. */
  1231. JingleSessionPC.prototype.fixJingle = function(jingle) {
  1232. var action = $(jingle.nodeTree).find("jingle").attr("action");
  1233. switch (action) {
  1234. case "source-add":
  1235. case "session-accept":
  1236. this.fixSourceAddJingle(jingle);
  1237. break;
  1238. case "source-remove":
  1239. this.fixSourceRemoveJingle(jingle);
  1240. break;
  1241. default:
  1242. logger.error("Unknown jingle action!");
  1243. return false;
  1244. }
  1245. var sources = $(jingle.tree()).find(">jingle>content>description>source");
  1246. return sources && sources.length > 0;
  1247. }
  1248. /**
  1249. * Fixes the outgoing jingle packets with action source-add by removing the
  1250. * nodes related to the unmuted streams
  1251. * @param jingle the jingle packet that is going to be sent
  1252. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1253. */
  1254. JingleSessionPC.prototype.fixSourceAddJingle = function (jingle) {
  1255. var ssrcs = this.modifiedSSRCs["unmute"];
  1256. this.modifiedSSRCs["unmute"] = [];
  1257. if(ssrcs && ssrcs.length) {
  1258. ssrcs.forEach(function (ssrcObj) {
  1259. var desc = $(jingle.tree()).find(">jingle>content[name=\"" +
  1260. ssrcObj.mtype + "\"]>description");
  1261. if(!desc || !desc.length)
  1262. return;
  1263. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1264. var sourceNode = desc.find(">source[ssrc=\"" +
  1265. ssrc + "\"]");
  1266. sourceNode.remove();
  1267. });
  1268. ssrcObj.ssrc.groups.forEach(function (group) {
  1269. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1270. group.group.semantics + "\"]:has(source[ssrc=\"" +
  1271. group.primarySSRC +
  1272. "\"])");
  1273. groupNode.remove();
  1274. });
  1275. });
  1276. }
  1277. ssrcs = this.modifiedSSRCs["addMuted"];
  1278. this.modifiedSSRCs["addMuted"] = [];
  1279. if(ssrcs && ssrcs.length) {
  1280. ssrcs.forEach(function (ssrcObj) {
  1281. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1282. var cname = Math.random().toString(36).substring(2);
  1283. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1284. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1285. sourceNode.remove();
  1286. var sourceXML = "<source " +
  1287. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1288. ssrc + "\">" +
  1289. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1290. " value=\"" + ssrcObj.msid + "\" name=\"msid\"/>" +
  1291. "<parameter xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"" +
  1292. " value=\"" + cname + "\" name=\"cname\" />" + "</source>";
  1293. desc.append(sourceXML);
  1294. });
  1295. ssrcObj.ssrc.groups.forEach(function (group) {
  1296. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1297. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1298. "\"])");
  1299. groupNode.remove();
  1300. desc.append("<ssrc-group semantics=\"" +
  1301. group.group.semantics +
  1302. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1303. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1304. "</ssrc-group>");
  1305. });
  1306. });
  1307. }
  1308. }
  1309. /**
  1310. * Fixes the outgoing jingle packets with action source-remove by removing the
  1311. * nodes related to the muted streams, handles removing of muted stream
  1312. * @param jingle the jingle packet that is going to be sent
  1313. * @returns {boolean} true if the jingle has to be sent and false otherwise.
  1314. */
  1315. JingleSessionPC.prototype.fixSourceRemoveJingle = function(jingle) {
  1316. var ssrcs = this.modifiedSSRCs["mute"];
  1317. this.modifiedSSRCs["mute"] = [];
  1318. if(ssrcs && ssrcs.length)
  1319. ssrcs.forEach(function (ssrcObj) {
  1320. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1321. var sourceNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1322. ssrcObj.mtype + "\"]>description>source[ssrc=\"" +
  1323. ssrc + "\"]");
  1324. sourceNode.remove();
  1325. });
  1326. ssrcObj.ssrc.groups.forEach(function (group) {
  1327. var groupNode = $(jingle.tree()).find(">jingle>content[name=\"" +
  1328. ssrcObj.mtype + "\"]>description>ssrc-group[semantics=\"" +
  1329. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1330. "\"])");
  1331. groupNode.remove();
  1332. });
  1333. });
  1334. ssrcs = this.modifiedSSRCs["remove"];
  1335. this.modifiedSSRCs["remove"] = [];
  1336. if(ssrcs && ssrcs.length)
  1337. ssrcs.forEach(function (ssrcObj) {
  1338. var desc = createDescriptionNode(jingle, ssrcObj.mtype);
  1339. ssrcObj.ssrc.ssrcs.forEach(function (ssrc) {
  1340. var sourceNode = desc.find(">source[ssrc=\"" +ssrc + "\"]");
  1341. if(!sourceNode || !sourceNode.length) {
  1342. //Maybe we have to include cname, msid, etc here?
  1343. desc.append("<source " +
  1344. "xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\" ssrc=\"" +
  1345. ssrc + "\"></source>");
  1346. }
  1347. });
  1348. ssrcObj.ssrc.groups.forEach(function (group) {
  1349. var groupNode = desc.find(">ssrc-group[semantics=\"" +
  1350. group.group.semantics + "\"]:has(source[ssrc=\"" + group.primarySSRC +
  1351. "\"])");
  1352. if(!groupNode || !groupNode.length) {
  1353. desc.append("<ssrc-group semantics=\"" +
  1354. group.group.semantics +
  1355. "\" xmlns=\"urn:xmpp:jingle:apps:rtp:ssma:0\"><source ssrc=\"" +
  1356. group.group.ssrcs.split(" ").join("\"/><source ssrc=\"") + "\"/>" +
  1357. "</ssrc-group>");
  1358. }
  1359. });
  1360. });
  1361. }
  1362. /**
  1363. * Returns the description node related to the passed content type. If the node
  1364. * doesn't exists it will be created.
  1365. * @param jingle - the jingle packet
  1366. * @param mtype - the content type(audio, video, etc.)
  1367. */
  1368. function createDescriptionNode(jingle, mtype) {
  1369. var content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1370. mtype + "\"]");
  1371. if(!content || !content.length) {
  1372. $(jingle.tree()).find(">jingle").append(
  1373. "<content name=\"" + mtype + "\"></content>");
  1374. content = $(jingle.tree()).find(">jingle>content[name=\"" +
  1375. mtype + "\"]");
  1376. }
  1377. var desc = content.find(">description");
  1378. if(!desc || !desc.length) {
  1379. content.append("<description " +
  1380. "xmlns=\"urn:xmpp:jingle:apps:rtp:1\" media=\"" +
  1381. mtype + "\"></description>");
  1382. desc = content.find(">description");
  1383. }
  1384. return desc;
  1385. }
  1386. module.exports = JingleSessionPC;