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.

JingleSession.js 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /* jshint -W117 */
  2. var TraceablePeerConnection = require("./TraceablePeerConnection");
  3. var SDPDiffer = require("./SDPDiffer");
  4. var SDPUtil = require("./SDPUtil");
  5. var SDP = require("./SDP");
  6. var RTCBrowserType = require("../../service/RTC/RTCBrowserType");
  7. // Jingle stuff
  8. function JingleSession(me, sid, connection, service) {
  9. this.me = me;
  10. this.sid = sid;
  11. this.connection = connection;
  12. this.initiator = null;
  13. this.responder = null;
  14. this.isInitiator = null;
  15. this.peerjid = null;
  16. this.state = null;
  17. this.localSDP = null;
  18. this.remoteSDP = null;
  19. this.relayedStreams = [];
  20. this.startTime = null;
  21. this.stopTime = null;
  22. this.media_constraints = null;
  23. this.pc_constraints = null;
  24. this.ice_config = {};
  25. this.drip_container = [];
  26. this.service = service;
  27. this.usetrickle = true;
  28. this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
  29. this.usedrip = false; // dripping is sending trickle candidates not one-by-one
  30. this.hadstuncandidate = false;
  31. this.hadturncandidate = false;
  32. this.lasticecandidate = false;
  33. this.statsinterval = null;
  34. this.reason = null;
  35. this.addssrc = [];
  36. this.removessrc = [];
  37. this.pendingop = null;
  38. this.switchstreams = false;
  39. this.wait = true;
  40. this.localStreamsSSRC = null;
  41. /**
  42. * The indicator which determines whether the (local) video has been muted
  43. * in response to a user command in contrast to an automatic decision made
  44. * by the application logic.
  45. */
  46. this.videoMuteByUser = false;
  47. }
  48. //TODO: this array must be removed when firefox implement multistream support
  49. JingleSession.notReceivedSSRCs = [];
  50. JingleSession.prototype.initiate = function (peerjid, isInitiator) {
  51. var self = this;
  52. if (this.state !== null) {
  53. console.error('attempt to initiate on session ' + this.sid +
  54. 'in state ' + this.state);
  55. return;
  56. }
  57. this.isInitiator = isInitiator;
  58. this.state = 'pending';
  59. this.initiator = isInitiator ? this.me : peerjid;
  60. this.responder = !isInitiator ? this.me : peerjid;
  61. this.peerjid = peerjid;
  62. this.hadstuncandidate = false;
  63. this.hadturncandidate = false;
  64. this.lasticecandidate = false;
  65. this.peerconnection
  66. = new TraceablePeerConnection(
  67. this.connection.jingle.ice_config,
  68. this.connection.jingle.pc_constraints );
  69. this.peerconnection.onicecandidate = function (event) {
  70. self.sendIceCandidate(event.candidate);
  71. };
  72. this.peerconnection.onaddstream = function (event) {
  73. console.log("REMOTE STREAM ADDED: " + event.stream + " - " + event.stream.id);
  74. self.remoteStreamAdded(event);
  75. };
  76. this.peerconnection.onremovestream = function (event) {
  77. // Remove the stream from remoteStreams
  78. // FIXME: remotestreamremoved.jingle not defined anywhere(unused)
  79. $(document).trigger('remotestreamremoved.jingle', [event, self.sid]);
  80. };
  81. this.peerconnection.onsignalingstatechange = function (event) {
  82. if (!(self && self.peerconnection)) return;
  83. };
  84. this.peerconnection.oniceconnectionstatechange = function (event) {
  85. if (!(self && self.peerconnection)) return;
  86. switch (self.peerconnection.iceConnectionState) {
  87. case 'connected':
  88. this.startTime = new Date();
  89. break;
  90. case 'disconnected':
  91. this.stopTime = new Date();
  92. break;
  93. }
  94. onIceConnectionStateChange(self.sid, self);
  95. };
  96. // add any local and relayed stream
  97. APP.RTC.localStreams.forEach(function(stream) {
  98. self.peerconnection.addStream(stream.getOriginalStream());
  99. });
  100. this.relayedStreams.forEach(function(stream) {
  101. self.peerconnection.addStream(stream);
  102. });
  103. };
  104. function onIceConnectionStateChange(sid, session) {
  105. switch (session.peerconnection.iceConnectionState) {
  106. case 'checking':
  107. session.timeChecking = (new Date()).getTime();
  108. session.firstconnect = true;
  109. break;
  110. case 'completed': // on caller side
  111. case 'connected':
  112. if (session.firstconnect) {
  113. session.firstconnect = false;
  114. var metadata = {};
  115. metadata.setupTime
  116. = (new Date()).getTime() - session.timeChecking;
  117. session.peerconnection.getStats(function (res) {
  118. if(res && res.result) {
  119. res.result().forEach(function (report) {
  120. if (report.type == 'googCandidatePair' &&
  121. report.stat('googActiveConnection') == 'true') {
  122. metadata.localCandidateType
  123. = report.stat('googLocalCandidateType');
  124. metadata.remoteCandidateType
  125. = report.stat('googRemoteCandidateType');
  126. // log pair as well so we can get nice pie
  127. // charts
  128. metadata.candidatePair
  129. = report.stat('googLocalCandidateType') +
  130. ';' +
  131. report.stat('googRemoteCandidateType');
  132. if (report.stat('googRemoteAddress').indexOf('[') === 0)
  133. {
  134. metadata.ipv6 = true;
  135. }
  136. }
  137. });
  138. }
  139. });
  140. }
  141. break;
  142. }
  143. }
  144. JingleSession.prototype.accept = function () {
  145. var self = this;
  146. this.state = 'active';
  147. var pranswer = this.peerconnection.localDescription;
  148. if (!pranswer || pranswer.type != 'pranswer') {
  149. return;
  150. }
  151. console.log('going from pranswer to answer');
  152. if (this.usetrickle) {
  153. // remove candidates already sent from session-accept
  154. var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
  155. for (var i = 0; i < lines.length; i++) {
  156. pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
  157. }
  158. }
  159. while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
  160. // FIXME: change any inactive to sendrecv or whatever they were originally
  161. pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
  162. }
  163. pranswer = APP.simulcast.reverseTransformLocalDescription(pranswer);
  164. var prsdp = new SDP(pranswer.sdp);
  165. var accept = $iq({to: this.peerjid,
  166. type: 'set'})
  167. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  168. action: 'session-accept',
  169. initiator: this.initiator,
  170. responder: this.responder,
  171. sid: this.sid });
  172. prsdp.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder', this.localStreamsSSRC);
  173. var sdp = this.peerconnection.localDescription.sdp;
  174. while (SDPUtil.find_line(sdp, 'a=inactive')) {
  175. // FIXME: change any inactive to sendrecv or whatever they were originally
  176. sdp = sdp.replace('a=inactive', 'a=sendrecv');
  177. }
  178. var self = this;
  179. this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
  180. function () {
  181. //console.log('setLocalDescription success');
  182. self.setLocalDescription();
  183. self.connection.sendIQ(accept,
  184. function () {
  185. var ack = {};
  186. ack.source = 'answer';
  187. $(document).trigger('ack.jingle', [self.sid, ack]);
  188. },
  189. function (stanza) {
  190. var error = ($(stanza).find('error').length) ? {
  191. code: $(stanza).find('error').attr('code'),
  192. reason: $(stanza).find('error :first')[0].tagName
  193. }:{};
  194. error.source = 'answer';
  195. JingleSession.onJingleError(self.sid, error);
  196. },
  197. 10000);
  198. },
  199. function (e) {
  200. console.error('setLocalDescription failed', e);
  201. }
  202. );
  203. };
  204. JingleSession.prototype.terminate = function (reason) {
  205. this.state = 'ended';
  206. this.reason = reason;
  207. this.peerconnection.close();
  208. if (this.statsinterval !== null) {
  209. window.clearInterval(this.statsinterval);
  210. this.statsinterval = null;
  211. }
  212. };
  213. JingleSession.prototype.active = function () {
  214. return this.state == 'active';
  215. };
  216. JingleSession.prototype.sendIceCandidate = function (candidate) {
  217. var self = this;
  218. if (candidate && !this.lasticecandidate) {
  219. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  220. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  221. if (!(ice && jcand)) {
  222. console.error('failed to get ice && jcand');
  223. return;
  224. }
  225. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  226. if (jcand.type === 'srflx') {
  227. this.hadstuncandidate = true;
  228. } else if (jcand.type === 'relay') {
  229. this.hadturncandidate = true;
  230. }
  231. if (this.usetrickle) {
  232. if (this.usedrip) {
  233. if (this.drip_container.length === 0) {
  234. // start 20ms callout
  235. window.setTimeout(function () {
  236. if (self.drip_container.length === 0) return;
  237. self.sendIceCandidates(self.drip_container);
  238. self.drip_container = [];
  239. }, 20);
  240. }
  241. this.drip_container.push(candidate);
  242. return;
  243. } else {
  244. self.sendIceCandidate([candidate]);
  245. }
  246. }
  247. } else {
  248. //console.log('sendIceCandidate: last candidate.');
  249. if (!this.usetrickle) {
  250. //console.log('should send full offer now...');
  251. var init = $iq({to: this.peerjid,
  252. type: 'set'})
  253. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  254. action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
  255. initiator: this.initiator,
  256. sid: this.sid});
  257. this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
  258. var self = this;
  259. var sendJingle = function (ssrc) {
  260. if(!ssrc)
  261. ssrc = {};
  262. self.localSDP.toJingle(init, self.initiator == self.me ? 'initiator' : 'responder', ssrc);
  263. self.connection.sendIQ(init,
  264. function () {
  265. //console.log('session initiate ack');
  266. var ack = {};
  267. ack.source = 'offer';
  268. $(document).trigger('ack.jingle', [self.sid, ack]);
  269. },
  270. function (stanza) {
  271. self.state = 'error';
  272. self.peerconnection.close();
  273. var error = ($(stanza).find('error').length) ? {
  274. code: $(stanza).find('error').attr('code'),
  275. reason: $(stanza).find('error :first')[0].tagName,
  276. }:{};
  277. error.source = 'offer';
  278. JingleSession.onJingleError(self.sid, error);
  279. },
  280. 10000);
  281. }
  282. sendJingle();
  283. }
  284. this.lasticecandidate = true;
  285. console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  286. console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  287. if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
  288. $(document).trigger('nostuncandidates.jingle', [this.sid]);
  289. }
  290. }
  291. };
  292. JingleSession.prototype.sendIceCandidates = function (candidates) {
  293. console.log('sendIceCandidates', candidates);
  294. var cand = $iq({to: this.peerjid, type: 'set'})
  295. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  296. action: 'transport-info',
  297. initiator: this.initiator,
  298. sid: this.sid});
  299. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  300. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  301. var mline = SDPUtil.parse_mline(this.localSDP.media[mid].split('\r\n')[0]);
  302. if (cands.length > 0) {
  303. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  304. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  305. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  306. name: (cands[0].sdpMid? cands[0].sdpMid : mline.media)
  307. }).c('transport', ice);
  308. for (var i = 0; i < cands.length; i++) {
  309. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  310. }
  311. // add fingerprint
  312. if (SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session)) {
  313. var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session));
  314. tmp.required = true;
  315. cand.c(
  316. 'fingerprint',
  317. {xmlns: 'urn:xmpp:jingle:apps:dtls:0'})
  318. .t(tmp.fingerprint);
  319. delete tmp.fingerprint;
  320. cand.attrs(tmp);
  321. cand.up();
  322. }
  323. cand.up(); // transport
  324. cand.up(); // content
  325. }
  326. }
  327. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  328. //console.log('was this the last candidate', this.lasticecandidate);
  329. this.connection.sendIQ(cand,
  330. function () {
  331. var ack = {};
  332. ack.source = 'transportinfo';
  333. $(document).trigger('ack.jingle', [this.sid, ack]);
  334. },
  335. function (stanza) {
  336. var error = ($(stanza).find('error').length) ? {
  337. code: $(stanza).find('error').attr('code'),
  338. reason: $(stanza).find('error :first')[0].tagName,
  339. }:{};
  340. error.source = 'transportinfo';
  341. JingleSession.onJingleError(this.sid, error);
  342. },
  343. 10000);
  344. };
  345. JingleSession.prototype.sendOffer = function () {
  346. //console.log('sendOffer...');
  347. var self = this;
  348. this.peerconnection.createOffer(function (sdp) {
  349. self.createdOffer(sdp);
  350. },
  351. function (e) {
  352. console.error('createOffer failed', e);
  353. },
  354. this.media_constraints
  355. );
  356. };
  357. JingleSession.prototype.createdOffer = function (sdp) {
  358. //console.log('createdOffer', sdp);
  359. var self = this;
  360. this.localSDP = new SDP(sdp.sdp);
  361. //this.localSDP.mangle();
  362. var sendJingle = function () {
  363. var init = $iq({to: this.peerjid,
  364. type: 'set'})
  365. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  366. action: 'session-initiate',
  367. initiator: this.initiator,
  368. sid: this.sid});
  369. self.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder', this.localStreamsSSRC);
  370. self.connection.sendIQ(init,
  371. function () {
  372. var ack = {};
  373. ack.source = 'offer';
  374. $(document).trigger('ack.jingle', [self.sid, ack]);
  375. },
  376. function (stanza) {
  377. self.state = 'error';
  378. self.peerconnection.close();
  379. var error = ($(stanza).find('error').length) ? {
  380. code: $(stanza).find('error').attr('code'),
  381. reason: $(stanza).find('error :first')[0].tagName,
  382. }:{};
  383. error.source = 'offer';
  384. JingleSession.onJingleError(self.sid, error);
  385. },
  386. 10000);
  387. }
  388. sdp.sdp = this.localSDP.raw;
  389. this.peerconnection.setLocalDescription(sdp,
  390. function () {
  391. if(self.usetrickle)
  392. {
  393. sendJingle();
  394. }
  395. self.setLocalDescription();
  396. //console.log('setLocalDescription success');
  397. },
  398. function (e) {
  399. console.error('setLocalDescription failed', e);
  400. }
  401. );
  402. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  403. for (var i = 0; i < cands.length; i++) {
  404. var cand = SDPUtil.parse_icecandidate(cands[i]);
  405. if (cand.type == 'srflx') {
  406. this.hadstuncandidate = true;
  407. } else if (cand.type == 'relay') {
  408. this.hadturncandidate = true;
  409. }
  410. }
  411. };
  412. JingleSession.prototype.setRemoteDescription = function (elem, desctype) {
  413. //console.log('setting remote description... ', desctype);
  414. this.remoteSDP = new SDP('');
  415. this.remoteSDP.fromJingle(elem);
  416. if (this.peerconnection.remoteDescription !== null) {
  417. console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
  418. if (this.peerconnection.remoteDescription.type == 'pranswer') {
  419. var pranswer = new SDP(this.peerconnection.remoteDescription.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. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
  424. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
  425. } else {
  426. console.warn('no ice ufrag?');
  427. }
  428. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
  429. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
  430. } else {
  431. console.warn('no ice pwd?');
  432. }
  433. }
  434. // copy over candidates
  435. var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
  436. for (var j = 0; j < lines.length; j++) {
  437. this.remoteSDP.media[i] += lines[j] + '\r\n';
  438. }
  439. }
  440. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  441. }
  442. }
  443. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  444. this.peerconnection.setRemoteDescription(remotedesc,
  445. function () {
  446. //console.log('setRemoteDescription success');
  447. },
  448. function (e) {
  449. console.error('setRemoteDescription error', e);
  450. JingleSession.onJingleFatalError(self, e);
  451. }
  452. );
  453. };
  454. JingleSession.prototype.addIceCandidate = function (elem) {
  455. var self = this;
  456. if (this.peerconnection.signalingState == 'closed') {
  457. return;
  458. }
  459. if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
  460. console.log('trickle ice candidate arriving before session accept...');
  461. // create a PRANSWER for setRemoteDescription
  462. if (!this.remoteSDP) {
  463. var cobbled = 'v=0\r\n' +
  464. 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
  465. 's=-\r\n' +
  466. 't=0 0\r\n';
  467. // first, take some things from the local description
  468. for (var i = 0; i < this.localSDP.media.length; i++) {
  469. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
  470. cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
  471. if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
  472. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
  473. }
  474. cobbled += 'a=inactive\r\n';
  475. }
  476. this.remoteSDP = new SDP(cobbled);
  477. }
  478. // then add things like ice and dtls from remote candidate
  479. elem.each(function () {
  480. for (var i = 0; i < self.remoteSDP.media.length; i++) {
  481. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  482. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  483. if (!SDPUtil.find_line(self.remoteSDP.media[i], 'a=ice-ufrag:')) {
  484. var tmp = $(this).find('transport');
  485. self.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  486. self.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  487. tmp = $(this).find('transport>fingerprint');
  488. if (tmp.length) {
  489. self.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  490. } else {
  491. console.log('no dtls fingerprint (webrtc issue #1718?)');
  492. self.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
  493. }
  494. break;
  495. }
  496. }
  497. }
  498. });
  499. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  500. // we need a complete SDP with ice-ufrag/ice-pwd in all parts
  501. // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
  502. // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
  503. var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
  504. return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
  505. }).length == this.remoteSDP.media.length;
  506. if (iscomplete) {
  507. console.log('setting pranswer');
  508. try {
  509. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }),
  510. function() {
  511. },
  512. function(e) {
  513. console.log('setRemoteDescription pranswer failed', e.toString());
  514. });
  515. } catch (e) {
  516. console.error('setting pranswer failed', e);
  517. }
  518. } else {
  519. //console.log('not yet setting pranswer');
  520. }
  521. }
  522. // operate on each content element
  523. elem.each(function () {
  524. // would love to deactivate this, but firefox still requires it
  525. var idx = -1;
  526. var i;
  527. for (i = 0; i < self.remoteSDP.media.length; i++) {
  528. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  529. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  530. idx = i;
  531. break;
  532. }
  533. }
  534. if (idx == -1) { // fall back to localdescription
  535. for (i = 0; i < self.localSDP.media.length; i++) {
  536. if (SDPUtil.find_line(self.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  537. self.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  538. idx = i;
  539. break;
  540. }
  541. }
  542. }
  543. var name = $(this).attr('name');
  544. // TODO: check ice-pwd and ice-ufrag?
  545. $(this).find('transport>candidate').each(function () {
  546. var line, candidate;
  547. line = SDPUtil.candidateFromJingle(this);
  548. candidate = new RTCIceCandidate({sdpMLineIndex: idx,
  549. sdpMid: name,
  550. candidate: line});
  551. try {
  552. self.peerconnection.addIceCandidate(candidate);
  553. } catch (e) {
  554. console.error('addIceCandidate failed', e.toString(), line);
  555. }
  556. });
  557. });
  558. };
  559. JingleSession.prototype.sendAnswer = function (provisional) {
  560. //console.log('createAnswer', provisional);
  561. var self = this;
  562. this.peerconnection.createAnswer(
  563. function (sdp) {
  564. self.createdAnswer(sdp, provisional);
  565. },
  566. function (e) {
  567. console.error('createAnswer failed', e);
  568. },
  569. this.media_constraints
  570. );
  571. };
  572. JingleSession.prototype.createdAnswer = function (sdp, provisional) {
  573. //console.log('createAnswer callback');
  574. var self = this;
  575. this.localSDP = new SDP(sdp.sdp);
  576. //this.localSDP.mangle();
  577. this.usepranswer = provisional === true;
  578. if (this.usetrickle) {
  579. if (this.usepranswer) {
  580. sdp.type = 'pranswer';
  581. for (var i = 0; i < this.localSDP.media.length; i++) {
  582. this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
  583. }
  584. this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
  585. }
  586. }
  587. var self = this;
  588. var sendJingle = function (ssrcs) {
  589. var accept = $iq({to: self.peerjid,
  590. type: 'set'})
  591. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  592. action: 'session-accept',
  593. initiator: self.initiator,
  594. responder: self.responder,
  595. sid: self.sid });
  596. var publicLocalDesc = APP.simulcast.reverseTransformLocalDescription(sdp);
  597. var publicLocalSDP = new SDP(publicLocalDesc.sdp);
  598. publicLocalSDP.toJingle(accept, self.initiator == self.me ? 'initiator' : 'responder', ssrcs);
  599. self.connection.sendIQ(accept,
  600. function () {
  601. var ack = {};
  602. ack.source = 'answer';
  603. $(document).trigger('ack.jingle', [self.sid, ack]);
  604. },
  605. function (stanza) {
  606. var error = ($(stanza).find('error').length) ? {
  607. code: $(stanza).find('error').attr('code'),
  608. reason: $(stanza).find('error :first')[0].tagName,
  609. }:{};
  610. error.source = 'answer';
  611. JingleSession.onJingleError(self.sid, error);
  612. },
  613. 10000);
  614. }
  615. sdp.sdp = this.localSDP.raw;
  616. this.peerconnection.setLocalDescription(sdp,
  617. function () {
  618. //console.log('setLocalDescription success');
  619. if (self.usetrickle && !self.usepranswer) {
  620. sendJingle();
  621. }
  622. self.setLocalDescription();
  623. },
  624. function (e) {
  625. console.error('setLocalDescription failed', e);
  626. }
  627. );
  628. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  629. for (var j = 0; j < cands.length; j++) {
  630. var cand = SDPUtil.parse_icecandidate(cands[j]);
  631. if (cand.type == 'srflx') {
  632. this.hadstuncandidate = true;
  633. } else if (cand.type == 'relay') {
  634. this.hadturncandidate = true;
  635. }
  636. }
  637. };
  638. JingleSession.prototype.sendTerminate = function (reason, text) {
  639. var self = this,
  640. term = $iq({to: this.peerjid,
  641. type: 'set'})
  642. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  643. action: 'session-terminate',
  644. initiator: this.initiator,
  645. sid: this.sid})
  646. .c('reason')
  647. .c(reason || 'success');
  648. if (text) {
  649. term.up().c('text').t(text);
  650. }
  651. this.connection.sendIQ(term,
  652. function () {
  653. self.peerconnection.close();
  654. self.peerconnection = null;
  655. self.terminate();
  656. var ack = {};
  657. ack.source = 'terminate';
  658. $(document).trigger('ack.jingle', [self.sid, ack]);
  659. },
  660. function (stanza) {
  661. var error = ($(stanza).find('error').length) ? {
  662. code: $(stanza).find('error').attr('code'),
  663. reason: $(stanza).find('error :first')[0].tagName,
  664. }:{};
  665. $(document).trigger('ack.jingle', [self.sid, error]);
  666. },
  667. 10000);
  668. if (this.statsinterval !== null) {
  669. window.clearInterval(this.statsinterval);
  670. this.statsinterval = null;
  671. }
  672. };
  673. JingleSession.prototype.addSource = function (elem, fromJid) {
  674. var self = this;
  675. // FIXME: dirty waiting
  676. if (!this.peerconnection.localDescription)
  677. {
  678. console.warn("addSource - localDescription not ready yet")
  679. setTimeout(function()
  680. {
  681. self.addSource(elem, fromJid);
  682. },
  683. 200
  684. );
  685. return;
  686. }
  687. console.log('addssrc', new Date().getTime());
  688. console.log('ice', this.peerconnection.iceConnectionState);
  689. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  690. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  691. $(elem).each(function (idx, content) {
  692. var name = $(content).attr('name');
  693. var lines = '';
  694. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  695. var semantics = this.getAttribute('semantics');
  696. var ssrcs = $(this).find('>source').map(function () {
  697. return this.getAttribute('ssrc');
  698. }).get();
  699. if (ssrcs.length != 0) {
  700. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  701. }
  702. });
  703. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  704. tmp.each(function () {
  705. var ssrc = $(this).attr('ssrc');
  706. if(mySdp.containsSSRC(ssrc)){
  707. /**
  708. * This happens when multiple participants change their streams at the same time and
  709. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  710. * addssrc are scheduled for update IQ. See
  711. */
  712. console.warn("Got add stream request for my own ssrc: "+ssrc);
  713. return;
  714. }
  715. $(this).find('>parameter').each(function () {
  716. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  717. if ($(this).attr('value') && $(this).attr('value').length)
  718. lines += ':' + $(this).attr('value');
  719. lines += '\r\n';
  720. });
  721. });
  722. sdp.media.forEach(function(media, idx) {
  723. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  724. return;
  725. sdp.media[idx] += lines;
  726. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  727. self.addssrc[idx] += lines;
  728. });
  729. sdp.raw = sdp.session + sdp.media.join('');
  730. });
  731. this.modifySources();
  732. };
  733. JingleSession.prototype.removeSource = function (elem, fromJid) {
  734. var self = this;
  735. // FIXME: dirty waiting
  736. if (!this.peerconnection.localDescription)
  737. {
  738. console.warn("removeSource - localDescription not ready yet")
  739. setTimeout(function()
  740. {
  741. self.removeSource(elem, fromJid);
  742. },
  743. 200
  744. );
  745. return;
  746. }
  747. console.log('removessrc', new Date().getTime());
  748. console.log('ice', this.peerconnection.iceConnectionState);
  749. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  750. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  751. $(elem).each(function (idx, content) {
  752. var name = $(content).attr('name');
  753. var lines = '';
  754. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  755. var semantics = this.getAttribute('semantics');
  756. var ssrcs = $(this).find('>source').map(function () {
  757. return this.getAttribute('ssrc');
  758. }).get();
  759. if (ssrcs.length != 0) {
  760. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  761. }
  762. });
  763. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  764. tmp.each(function () {
  765. var ssrc = $(this).attr('ssrc');
  766. // This should never happen, but can be useful for bug detection
  767. if(mySdp.containsSSRC(ssrc)){
  768. console.error("Got remove stream request for my own ssrc: "+ssrc);
  769. return;
  770. }
  771. $(this).find('>parameter').each(function () {
  772. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  773. if ($(this).attr('value') && $(this).attr('value').length)
  774. lines += ':' + $(this).attr('value');
  775. lines += '\r\n';
  776. });
  777. });
  778. sdp.media.forEach(function(media, idx) {
  779. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  780. return;
  781. sdp.media[idx] += lines;
  782. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  783. self.removessrc[idx] += lines;
  784. });
  785. sdp.raw = sdp.session + sdp.media.join('');
  786. });
  787. this.modifySources();
  788. };
  789. JingleSession.prototype.modifySources = function (successCallback) {
  790. var self = this;
  791. if (this.peerconnection.signalingState == 'closed') return;
  792. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null || this.switchstreams)){
  793. // There is nothing to do since scheduled job might have been executed by another succeeding call
  794. this.setLocalDescription();
  795. if(successCallback){
  796. successCallback();
  797. }
  798. return;
  799. }
  800. // FIXME: this is a big hack
  801. // https://code.google.com/p/webrtc/issues/detail?id=2688
  802. // ^ has been fixed.
  803. if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
  804. console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
  805. this.wait = true;
  806. window.setTimeout(function() { self.modifySources(successCallback); }, 250);
  807. return;
  808. }
  809. if (this.wait) {
  810. window.setTimeout(function() { self.modifySources(successCallback); }, 2500);
  811. this.wait = false;
  812. return;
  813. }
  814. // Reset switch streams flag
  815. this.switchstreams = false;
  816. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  817. // add sources
  818. this.addssrc.forEach(function(lines, idx) {
  819. sdp.media[idx] += lines;
  820. });
  821. this.addssrc = [];
  822. // remove sources
  823. this.removessrc.forEach(function(lines, idx) {
  824. lines = lines.split('\r\n');
  825. lines.pop(); // remove empty last element;
  826. lines.forEach(function(line) {
  827. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  828. });
  829. });
  830. this.removessrc = [];
  831. // FIXME:
  832. // this was a hack for the situation when only one peer exists
  833. // in the conference.
  834. // check if still required and remove
  835. if (sdp.media[0])
  836. sdp.media[0] = sdp.media[0].replace('a=recvonly', 'a=sendrecv');
  837. if (sdp.media[1])
  838. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  839. sdp.raw = sdp.session + sdp.media.join('');
  840. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  841. function() {
  842. if(self.signalingState == 'closed') {
  843. console.error("createAnswer attempt on closed state");
  844. return;
  845. }
  846. self.peerconnection.createAnswer(
  847. function(modifiedAnswer) {
  848. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  849. if (self.pendingop !== null) {
  850. var sdp = new SDP(modifiedAnswer.sdp);
  851. if (sdp.media.length > 1) {
  852. switch(self.pendingop) {
  853. case 'mute':
  854. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  855. break;
  856. case 'unmute':
  857. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  858. break;
  859. }
  860. sdp.raw = sdp.session + sdp.media.join('');
  861. modifiedAnswer.sdp = sdp.raw;
  862. }
  863. self.pendingop = null;
  864. }
  865. // FIXME: pushing down an answer while ice connection state
  866. // is still checking is bad...
  867. //console.log(self.peerconnection.iceConnectionState);
  868. // trying to work around another chrome bug
  869. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  870. self.peerconnection.setLocalDescription(modifiedAnswer,
  871. function() {
  872. //console.log('modified setLocalDescription ok');
  873. self.setLocalDescription();
  874. if(successCallback){
  875. successCallback();
  876. }
  877. },
  878. function(error) {
  879. console.error('modified setLocalDescription failed', error);
  880. }
  881. );
  882. },
  883. function(error) {
  884. console.error('modified answer failed', error);
  885. }
  886. );
  887. },
  888. function(error) {
  889. console.error('modify failed', error);
  890. }
  891. );
  892. };
  893. /**
  894. * Switches video streams.
  895. * @param new_stream new stream that will be used as video of this session.
  896. * @param oldStream old video stream of this session.
  897. * @param success_callback callback executed after successful stream switch.
  898. */
  899. JingleSession.prototype.switchStreams = function (new_stream, oldStream, success_callback) {
  900. var self = this;
  901. // Remember SDP to figure out added/removed SSRCs
  902. var oldSdp = null;
  903. if(self.peerconnection) {
  904. if(self.peerconnection.localDescription) {
  905. oldSdp = new SDP(self.peerconnection.localDescription.sdp);
  906. }
  907. self.peerconnection.removeStream(oldStream, true);
  908. if(new_stream)
  909. self.peerconnection.addStream(new_stream);
  910. }
  911. APP.RTC.switchVideoStreams(new_stream, oldStream);
  912. // Conference is not active
  913. if(!oldSdp || !self.peerconnection) {
  914. success_callback();
  915. return;
  916. }
  917. self.switchstreams = true;
  918. self.modifySources(function() {
  919. console.log('modify sources done');
  920. success_callback();
  921. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  922. console.log("SDPs", oldSdp, newSdp);
  923. self.notifyMySSRCUpdate(oldSdp, newSdp);
  924. });
  925. };
  926. /**
  927. * Figures out added/removed ssrcs and send update IQs.
  928. * @param old_sdp SDP object for old description.
  929. * @param new_sdp SDP object for new description.
  930. */
  931. JingleSession.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  932. if (!(this.peerconnection.signalingState == 'stable' &&
  933. this.peerconnection.iceConnectionState == 'connected')){
  934. console.log("Too early to send updates");
  935. return;
  936. }
  937. // send source-remove IQ.
  938. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  939. var remove = $iq({to: this.peerjid, type: 'set'})
  940. .c('jingle', {
  941. xmlns: 'urn:xmpp:jingle:1',
  942. action: 'source-remove',
  943. initiator: this.initiator,
  944. sid: this.sid
  945. }
  946. );
  947. var removed = sdpDiffer.toJingle(remove);
  948. if (removed) {
  949. this.connection.sendIQ(remove,
  950. function (res) {
  951. console.info('got remove result', res);
  952. },
  953. function (err) {
  954. console.error('got remove error', err);
  955. }
  956. );
  957. } else {
  958. console.log('removal not necessary');
  959. }
  960. // send source-add IQ.
  961. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  962. var add = $iq({to: this.peerjid, type: 'set'})
  963. .c('jingle', {
  964. xmlns: 'urn:xmpp:jingle:1',
  965. action: 'source-add',
  966. initiator: this.initiator,
  967. sid: this.sid
  968. }
  969. );
  970. var added = sdpDiffer.toJingle(add);
  971. if (added) {
  972. this.connection.sendIQ(add,
  973. function (res) {
  974. console.info('got add result', res);
  975. },
  976. function (err) {
  977. console.error('got add error', err);
  978. }
  979. );
  980. } else {
  981. console.log('addition not necessary');
  982. }
  983. };
  984. /**
  985. * Mutes/unmutes the (local) video i.e. enables/disables all video tracks.
  986. *
  987. * @param mute <tt>true</tt> to mute the (local) video i.e. to disable all video
  988. * tracks; otherwise, <tt>false</tt>
  989. * @param callback a function to be invoked with <tt>mute</tt> after all video
  990. * tracks have been enabled/disabled. The function may, optionally, return
  991. * another function which is to be invoked after the whole mute/unmute operation
  992. * has completed successfully.
  993. * @param options an object which specifies optional arguments such as the
  994. * <tt>boolean</tt> key <tt>byUser</tt> with default value <tt>true</tt> which
  995. * specifies whether the method was initiated in response to a user command (in
  996. * contrast to an automatic decision made by the application logic)
  997. */
  998. JingleSession.prototype.setVideoMute = function (mute, callback, options) {
  999. var byUser;
  1000. if (options) {
  1001. byUser = options.byUser;
  1002. if (typeof byUser === 'undefined') {
  1003. byUser = true;
  1004. }
  1005. } else {
  1006. byUser = true;
  1007. }
  1008. // The user's command to mute the (local) video takes precedence over any
  1009. // automatic decision made by the application logic.
  1010. if (byUser) {
  1011. this.videoMuteByUser = mute;
  1012. } else if (this.videoMuteByUser) {
  1013. return;
  1014. }
  1015. this.hardMuteVideo(mute);
  1016. this.modifySources(callback(mute));
  1017. };
  1018. JingleSession.prototype.hardMuteVideo = function (muted) {
  1019. this.pendingop = muted ? 'mute' : 'unmute';
  1020. };
  1021. JingleSession.prototype.sendMute = function (muted, content) {
  1022. var info = $iq({to: this.peerjid,
  1023. type: 'set'})
  1024. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1025. action: 'session-info',
  1026. initiator: this.initiator,
  1027. sid: this.sid });
  1028. info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1029. info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
  1030. if (content) {
  1031. info.attrs({'name': content});
  1032. }
  1033. this.connection.send(info);
  1034. };
  1035. JingleSession.prototype.sendRinging = function () {
  1036. var info = $iq({to: this.peerjid,
  1037. type: 'set'})
  1038. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1039. action: 'session-info',
  1040. initiator: this.initiator,
  1041. sid: this.sid });
  1042. info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1043. this.connection.send(info);
  1044. };
  1045. JingleSession.prototype.getStats = function (interval) {
  1046. var self = this;
  1047. var recv = {audio: 0, video: 0};
  1048. var lost = {audio: 0, video: 0};
  1049. var lastrecv = {audio: 0, video: 0};
  1050. var lastlost = {audio: 0, video: 0};
  1051. var loss = {audio: 0, video: 0};
  1052. var delta = {audio: 0, video: 0};
  1053. this.statsinterval = window.setInterval(function () {
  1054. if (self && self.peerconnection && self.peerconnection.getStats) {
  1055. self.peerconnection.getStats(function (stats) {
  1056. var results = stats.result();
  1057. // TODO: there are so much statistics you can get from this..
  1058. for (var i = 0; i < results.length; ++i) {
  1059. if (results[i].type == 'ssrc') {
  1060. var packetsrecv = results[i].stat('packetsReceived');
  1061. var packetslost = results[i].stat('packetsLost');
  1062. if (packetsrecv && packetslost) {
  1063. packetsrecv = parseInt(packetsrecv, 10);
  1064. packetslost = parseInt(packetslost, 10);
  1065. if (results[i].stat('googFrameRateReceived')) {
  1066. lastlost.video = lost.video;
  1067. lastrecv.video = recv.video;
  1068. recv.video = packetsrecv;
  1069. lost.video = packetslost;
  1070. } else {
  1071. lastlost.audio = lost.audio;
  1072. lastrecv.audio = recv.audio;
  1073. recv.audio = packetsrecv;
  1074. lost.audio = packetslost;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. delta.audio = recv.audio - lastrecv.audio;
  1080. delta.video = recv.video - lastrecv.video;
  1081. loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
  1082. loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
  1083. $(document).trigger('packetloss.jingle', [self.sid, loss]);
  1084. });
  1085. }
  1086. }, interval || 3000);
  1087. return this.statsinterval;
  1088. };
  1089. JingleSession.onJingleError = function (session, error)
  1090. {
  1091. console.error("Jingle error", error);
  1092. }
  1093. JingleSession.onJingleFatalError = function (session, error)
  1094. {
  1095. this.service.sessionTerminated = true;
  1096. this.connection.emuc.doLeave();
  1097. APP.UI.messageHandler.showError("dialog.sorry",
  1098. "dialog.internalError");
  1099. }
  1100. JingleSession.prototype.setLocalDescription = function () {
  1101. // put our ssrcs into presence so other clients can identify our stream
  1102. var newssrcs = [];
  1103. var media = APP.simulcast.parseMedia(this.peerconnection.localDescription);
  1104. media.forEach(function (media) {
  1105. if(Object.keys(media.sources).length > 0) {
  1106. // TODO(gp) maybe exclude FID streams?
  1107. Object.keys(media.sources).forEach(function (ssrc) {
  1108. newssrcs.push({
  1109. 'ssrc': ssrc,
  1110. 'type': media.type,
  1111. 'direction': media.direction
  1112. });
  1113. });
  1114. }
  1115. else if(this.localStreamsSSRC && this.localStreamsSSRC[media.type])
  1116. {
  1117. newssrcs.push({
  1118. 'ssrc': this.localStreamsSSRC[media.type],
  1119. 'type': media.type,
  1120. 'direction': media.direction
  1121. });
  1122. }
  1123. });
  1124. console.log('new ssrcs', newssrcs);
  1125. // Have to clear presence map to get rid of removed streams
  1126. this.connection.emuc.clearPresenceMedia();
  1127. if (newssrcs.length > 0) {
  1128. for (var i = 1; i <= newssrcs.length; i ++) {
  1129. // Change video type to screen
  1130. if (newssrcs[i-1].type === 'video' && APP.desktopsharing.isUsingScreenStream()) {
  1131. newssrcs[i-1].type = 'screen';
  1132. }
  1133. this.connection.emuc.addMediaToPresence(i,
  1134. newssrcs[i-1].type, newssrcs[i-1].ssrc, newssrcs[i-1].direction);
  1135. }
  1136. this.connection.emuc.sendPresence();
  1137. }
  1138. }
  1139. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1140. function sendKeyframe(pc) {
  1141. console.log('sendkeyframe', pc.iceConnectionState);
  1142. if (pc.iceConnectionState !== 'connected') return; // safe...
  1143. pc.setRemoteDescription(
  1144. pc.remoteDescription,
  1145. function () {
  1146. pc.createAnswer(
  1147. function (modifiedAnswer) {
  1148. pc.setLocalDescription(
  1149. modifiedAnswer,
  1150. function () {
  1151. // noop
  1152. },
  1153. function (error) {
  1154. console.log('triggerKeyframe setLocalDescription failed', error);
  1155. APP.UI.messageHandler.showError();
  1156. }
  1157. );
  1158. },
  1159. function (error) {
  1160. console.log('triggerKeyframe createAnswer failed', error);
  1161. APP.UI.messageHandler.showError();
  1162. }
  1163. );
  1164. },
  1165. function (error) {
  1166. console.log('triggerKeyframe setRemoteDescription failed', error);
  1167. APP.UI.messageHandler.showError();
  1168. }
  1169. );
  1170. }
  1171. JingleSession.prototype.remoteStreamAdded = function (data, times) {
  1172. var self = this;
  1173. var thessrc;
  1174. var ssrc2jid = this.connection.emuc.ssrc2jid;
  1175. // look up an associated JID for a stream id
  1176. if (data.stream.id && data.stream.id.indexOf('mixedmslabel') === -1) {
  1177. // look only at a=ssrc: and _not_ at a=ssrc-group: lines
  1178. var ssrclines
  1179. = SDPUtil.find_lines(this.peerconnection.remoteDescription.sdp, 'a=ssrc:');
  1180. ssrclines = ssrclines.filter(function (line) {
  1181. // NOTE(gp) previously we filtered on the mslabel, but that property
  1182. // is not always present.
  1183. // return line.indexOf('mslabel:' + data.stream.label) !== -1;
  1184. return ((line.indexOf('msid:' + data.stream.id) !== -1));
  1185. });
  1186. if (ssrclines.length) {
  1187. thessrc = ssrclines[0].substring(7).split(' ')[0];
  1188. // We signal our streams (through Jingle to the focus) before we set
  1189. // our presence (through which peers associate remote streams to
  1190. // jids). So, it might arrive that a remote stream is added but
  1191. // ssrc2jid is not yet updated and thus data.peerjid cannot be
  1192. // successfully set. Here we wait for up to a second for the
  1193. // presence to arrive.
  1194. if (!ssrc2jid[thessrc]) {
  1195. if (typeof times === 'undefined')
  1196. {
  1197. times = 0;
  1198. }
  1199. if (times > 10)
  1200. {
  1201. console.warning('Waiting for jid timed out', thessrc);
  1202. }
  1203. else
  1204. {
  1205. setTimeout(function(d) {
  1206. return function() {
  1207. self.remoteStreamAdded(d, times++);
  1208. }
  1209. }(data), 250);
  1210. }
  1211. return;
  1212. }
  1213. // ok to overwrite the one from focus? might save work in colibri.js
  1214. console.log('associated jid', ssrc2jid[thessrc], data.peerjid);
  1215. if (ssrc2jid[thessrc]) {
  1216. data.peerjid = ssrc2jid[thessrc];
  1217. }
  1218. }
  1219. }
  1220. APP.RTC.createRemoteStream(data, this.sid, thessrc);
  1221. var isVideo = data.stream.getVideoTracks().length > 0;
  1222. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1223. if (isVideo &&
  1224. data.peerjid && this.peerjid === data.peerjid &&
  1225. data.stream.getVideoTracks().length === 0 &&
  1226. APP.RTC.localVideo.getTracks().length > 0) {
  1227. window.setTimeout(function () {
  1228. sendKeyframe(self.peerconnection);
  1229. }, 3000);
  1230. }
  1231. }
  1232. module.exports = JingleSession;