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 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. tmp = $(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. 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. tmp = $(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. 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. self.peerconnection.addStream(new_stream);
  909. }
  910. RTC.switchVideoStreams(new_stream, oldStream);
  911. // Conference is not active
  912. if(!oldSdp || !self.peerconnection) {
  913. success_callback();
  914. return;
  915. }
  916. self.switchstreams = true;
  917. self.modifySources(function() {
  918. console.log('modify sources done');
  919. success_callback();
  920. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  921. console.log("SDPs", oldSdp, newSdp);
  922. self.notifyMySSRCUpdate(oldSdp, newSdp);
  923. });
  924. };
  925. /**
  926. * Figures out added/removed ssrcs and send update IQs.
  927. * @param old_sdp SDP object for old description.
  928. * @param new_sdp SDP object for new description.
  929. */
  930. JingleSession.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  931. if (!(this.peerconnection.signalingState == 'stable' &&
  932. this.peerconnection.iceConnectionState == 'connected')){
  933. console.log("Too early to send updates");
  934. return;
  935. }
  936. // send source-remove IQ.
  937. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  938. var remove = $iq({to: this.peerjid, type: 'set'})
  939. .c('jingle', {
  940. xmlns: 'urn:xmpp:jingle:1',
  941. action: 'source-remove',
  942. initiator: this.initiator,
  943. sid: this.sid
  944. }
  945. );
  946. var removed = sdpDiffer.toJingle(remove);
  947. if (removed) {
  948. this.connection.sendIQ(remove,
  949. function (res) {
  950. console.info('got remove result', res);
  951. },
  952. function (err) {
  953. console.error('got remove error', err);
  954. }
  955. );
  956. } else {
  957. console.log('removal not necessary');
  958. }
  959. // send source-add IQ.
  960. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  961. var add = $iq({to: this.peerjid, type: 'set'})
  962. .c('jingle', {
  963. xmlns: 'urn:xmpp:jingle:1',
  964. action: 'source-add',
  965. initiator: this.initiator,
  966. sid: this.sid
  967. }
  968. );
  969. var added = sdpDiffer.toJingle(add);
  970. if (added) {
  971. this.connection.sendIQ(add,
  972. function (res) {
  973. console.info('got add result', res);
  974. },
  975. function (err) {
  976. console.error('got add error', err);
  977. }
  978. );
  979. } else {
  980. console.log('addition not necessary');
  981. }
  982. };
  983. /**
  984. * Determines whether the (local) video is mute i.e. all video tracks are
  985. * disabled.
  986. *
  987. * @return <tt>true</tt> if the (local) video is mute i.e. all video tracks are
  988. * disabled; otherwise, <tt>false</tt>
  989. */
  990. JingleSession.prototype.isVideoMute = function () {
  991. var tracks = RTC.localVideo.getVideoTracks();
  992. var mute = true;
  993. for (var i = 0; i < tracks.length; ++i) {
  994. if (tracks[i].enabled) {
  995. mute = false;
  996. break;
  997. }
  998. }
  999. return mute;
  1000. };
  1001. /**
  1002. * Mutes/unmutes the (local) video i.e. enables/disables all video tracks.
  1003. *
  1004. * @param mute <tt>true</tt> to mute the (local) video i.e. to disable all video
  1005. * tracks; otherwise, <tt>false</tt>
  1006. * @param callback a function to be invoked with <tt>mute</tt> after all video
  1007. * tracks have been enabled/disabled. The function may, optionally, return
  1008. * another function which is to be invoked after the whole mute/unmute operation
  1009. * has completed successfully.
  1010. * @param options an object which specifies optional arguments such as the
  1011. * <tt>boolean</tt> key <tt>byUser</tt> with default value <tt>true</tt> which
  1012. * specifies whether the method was initiated in response to a user command (in
  1013. * contrast to an automatic decision made by the application logic)
  1014. */
  1015. JingleSession.prototype.setVideoMute = function (mute, callback, options) {
  1016. var byUser;
  1017. if (options) {
  1018. byUser = options.byUser;
  1019. if (typeof byUser === 'undefined') {
  1020. byUser = true;
  1021. }
  1022. } else {
  1023. byUser = true;
  1024. }
  1025. // The user's command to mute the (local) video takes precedence over any
  1026. // automatic decision made by the application logic.
  1027. if (byUser) {
  1028. this.videoMuteByUser = mute;
  1029. } else if (this.videoMuteByUser) {
  1030. return;
  1031. }
  1032. var self = this;
  1033. var localCallback = function (mute) {
  1034. self.connection.emuc.addVideoInfoToPresence(mute);
  1035. self.connection.emuc.sendPresence();
  1036. return callback(mute)
  1037. };
  1038. if (mute == RTC.localVideo.isMuted())
  1039. {
  1040. // Even if no change occurs, the specified callback is to be executed.
  1041. // The specified callback may, optionally, return a successCallback
  1042. // which is to be executed as well.
  1043. var successCallback = localCallback(mute);
  1044. if (successCallback) {
  1045. successCallback();
  1046. }
  1047. } else {
  1048. APP.RTC.localVideo.setMute(!mute);
  1049. this.hardMuteVideo(mute);
  1050. this.modifySources(localCallback(mute));
  1051. }
  1052. };
  1053. // SDP-based mute by going recvonly/sendrecv
  1054. // FIXME: should probably black out the screen as well
  1055. JingleSession.prototype.toggleVideoMute = function (callback) {
  1056. this.service.setVideoMute(APP.RTC.localVideo.isMuted(), callback);
  1057. };
  1058. JingleSession.prototype.hardMuteVideo = function (muted) {
  1059. this.pendingop = muted ? 'mute' : 'unmute';
  1060. };
  1061. JingleSession.prototype.sendMute = function (muted, content) {
  1062. var info = $iq({to: this.peerjid,
  1063. type: 'set'})
  1064. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1065. action: 'session-info',
  1066. initiator: this.initiator,
  1067. sid: this.sid });
  1068. info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1069. info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
  1070. if (content) {
  1071. info.attrs({'name': content});
  1072. }
  1073. this.connection.send(info);
  1074. };
  1075. JingleSession.prototype.sendRinging = function () {
  1076. var info = $iq({to: this.peerjid,
  1077. type: 'set'})
  1078. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1079. action: 'session-info',
  1080. initiator: this.initiator,
  1081. sid: this.sid });
  1082. info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1083. this.connection.send(info);
  1084. };
  1085. JingleSession.prototype.getStats = function (interval) {
  1086. var self = this;
  1087. var recv = {audio: 0, video: 0};
  1088. var lost = {audio: 0, video: 0};
  1089. var lastrecv = {audio: 0, video: 0};
  1090. var lastlost = {audio: 0, video: 0};
  1091. var loss = {audio: 0, video: 0};
  1092. var delta = {audio: 0, video: 0};
  1093. this.statsinterval = window.setInterval(function () {
  1094. if (self && self.peerconnection && self.peerconnection.getStats) {
  1095. self.peerconnection.getStats(function (stats) {
  1096. var results = stats.result();
  1097. // TODO: there are so much statistics you can get from this..
  1098. for (var i = 0; i < results.length; ++i) {
  1099. if (results[i].type == 'ssrc') {
  1100. var packetsrecv = results[i].stat('packetsReceived');
  1101. var packetslost = results[i].stat('packetsLost');
  1102. if (packetsrecv && packetslost) {
  1103. packetsrecv = parseInt(packetsrecv, 10);
  1104. packetslost = parseInt(packetslost, 10);
  1105. if (results[i].stat('googFrameRateReceived')) {
  1106. lastlost.video = lost.video;
  1107. lastrecv.video = recv.video;
  1108. recv.video = packetsrecv;
  1109. lost.video = packetslost;
  1110. } else {
  1111. lastlost.audio = lost.audio;
  1112. lastrecv.audio = recv.audio;
  1113. recv.audio = packetsrecv;
  1114. lost.audio = packetslost;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. delta.audio = recv.audio - lastrecv.audio;
  1120. delta.video = recv.video - lastrecv.video;
  1121. loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
  1122. loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
  1123. $(document).trigger('packetloss.jingle', [self.sid, loss]);
  1124. });
  1125. }
  1126. }, interval || 3000);
  1127. return this.statsinterval;
  1128. };
  1129. JingleSession.onJingleError = function (session, error)
  1130. {
  1131. console.error("Jingle error", error);
  1132. }
  1133. JingleSession.onJingleFatalError = function (session, error)
  1134. {
  1135. this.service.sessionTerminated = true;
  1136. this.connection.emuc.doLeave();
  1137. APP.UI.messageHandler.showError( "Sorry",
  1138. "Internal application error[setRemoteDescription]");
  1139. }
  1140. JingleSession.prototype.setLocalDescription = function () {
  1141. // put our ssrcs into presence so other clients can identify our stream
  1142. var newssrcs = [];
  1143. var media = APP.simulcast.parseMedia(this.peerconnection.localDescription);
  1144. media.forEach(function (media) {
  1145. if(Object.keys(media.sources).length > 0) {
  1146. // TODO(gp) maybe exclude FID streams?
  1147. Object.keys(media.sources).forEach(function (ssrc) {
  1148. newssrcs.push({
  1149. 'ssrc': ssrc,
  1150. 'type': media.type,
  1151. 'direction': media.direction
  1152. });
  1153. });
  1154. }
  1155. else if(this.localStreamsSSRC && this.localStreamsSSRC[media.type])
  1156. {
  1157. newssrcs.push({
  1158. 'ssrc': this.localStreamsSSRC[media.type],
  1159. 'type': media.type,
  1160. 'direction': media.direction
  1161. });
  1162. }
  1163. });
  1164. console.log('new ssrcs', newssrcs);
  1165. // Have to clear presence map to get rid of removed streams
  1166. this.connection.emuc.clearPresenceMedia();
  1167. if (newssrcs.length > 0) {
  1168. for (var i = 1; i <= newssrcs.length; i ++) {
  1169. // Change video type to screen
  1170. if (newssrcs[i-1].type === 'video' && APP.desktopsharing.isUsingScreenStream()) {
  1171. newssrcs[i-1].type = 'screen';
  1172. }
  1173. this.connection.emuc.addMediaToPresence(i,
  1174. newssrcs[i-1].type, newssrcs[i-1].ssrc, newssrcs[i-1].direction);
  1175. }
  1176. this.connection.emuc.sendPresence();
  1177. }
  1178. }
  1179. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1180. function sendKeyframe(pc) {
  1181. console.log('sendkeyframe', pc.iceConnectionState);
  1182. if (pc.iceConnectionState !== 'connected') return; // safe...
  1183. pc.setRemoteDescription(
  1184. pc.remoteDescription,
  1185. function () {
  1186. pc.createAnswer(
  1187. function (modifiedAnswer) {
  1188. pc.setLocalDescription(
  1189. modifiedAnswer,
  1190. function () {
  1191. // noop
  1192. },
  1193. function (error) {
  1194. console.log('triggerKeyframe setLocalDescription failed', error);
  1195. UI.messageHandler.showError();
  1196. }
  1197. );
  1198. },
  1199. function (error) {
  1200. console.log('triggerKeyframe createAnswer failed', error);
  1201. UI.messageHandler.showError();
  1202. }
  1203. );
  1204. },
  1205. function (error) {
  1206. console.log('triggerKeyframe setRemoteDescription failed', error);
  1207. UI.messageHandler.showError();
  1208. }
  1209. );
  1210. }
  1211. JingleSession.prototype.remoteStreamAdded = function (data) {
  1212. var self = this;
  1213. var thessrc;
  1214. var ssrc2jid = this.connection.emuc.ssrc2jid;
  1215. // look up an associated JID for a stream id
  1216. if (data.stream.id && data.stream.id.indexOf('mixedmslabel') === -1) {
  1217. // look only at a=ssrc: and _not_ at a=ssrc-group: lines
  1218. var ssrclines
  1219. = SDPUtil.find_lines(this.peerconnection.remoteDescription.sdp, 'a=ssrc:');
  1220. ssrclines = ssrclines.filter(function (line) {
  1221. // NOTE(gp) previously we filtered on the mslabel, but that property
  1222. // is not always present.
  1223. // return line.indexOf('mslabel:' + data.stream.label) !== -1;
  1224. return ((line.indexOf('msid:' + data.stream.id) !== -1));
  1225. });
  1226. if (ssrclines.length) {
  1227. thessrc = ssrclines[0].substring(7).split(' ')[0];
  1228. // We signal our streams (through Jingle to the focus) before we set
  1229. // our presence (through which peers associate remote streams to
  1230. // jids). So, it might arrive that a remote stream is added but
  1231. // ssrc2jid is not yet updated and thus data.peerjid cannot be
  1232. // successfully set. Here we wait for up to a second for the
  1233. // presence to arrive.
  1234. if (!ssrc2jid[thessrc]) {
  1235. // TODO(gp) limit wait duration to 1 sec.
  1236. setTimeout(function(d) {
  1237. return function() {
  1238. self.remoteStreamAdded(d);
  1239. }
  1240. }(data), 250);
  1241. return;
  1242. }
  1243. // ok to overwrite the one from focus? might save work in colibri.js
  1244. console.log('associated jid', ssrc2jid[thessrc], data.peerjid);
  1245. if (ssrc2jid[thessrc]) {
  1246. data.peerjid = ssrc2jid[thessrc];
  1247. }
  1248. }
  1249. }
  1250. //TODO: this code should be removed when firefox implement multistream support
  1251. if(APP.RTC.getBrowserType() == RTCBrowserType.RTC_BROWSER_FIREFOX)
  1252. {
  1253. if((JingleSession.notReceivedSSRCs.length == 0) ||
  1254. !ssrc2jid[JingleSession.notReceivedSSRCs[JingleSession.notReceivedSSRCs.length - 1]])
  1255. {
  1256. // TODO(gp) limit wait duration to 1 sec.
  1257. setTimeout(function(d) {
  1258. return function() {
  1259. self.remoteStreamAdded(d);
  1260. }
  1261. }(data), 250);
  1262. return;
  1263. }
  1264. thessrc = JingleSession.notReceivedSSRCs.pop();
  1265. if (ssrc2jid[thessrc]) {
  1266. data.peerjid = ssrc2jid[thessrc];
  1267. }
  1268. }
  1269. APP.RTC.createRemoteStream(data, this.sid, thessrc);
  1270. var isVideo = data.stream.getVideoTracks().length > 0;
  1271. // an attempt to work around https://github.com/jitsi/jitmeet/issues/32
  1272. if (isVideo &&
  1273. data.peerjid && this.peerjid === data.peerjid &&
  1274. data.stream.getVideoTracks().length === 0 &&
  1275. APP.RTC.localVideo.getTracks().length > 0) {
  1276. window.setTimeout(function () {
  1277. sendKeyframe(self.peerconnection);
  1278. }, 3000);
  1279. }
  1280. }
  1281. module.exports = JingleSession;