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.

strophe.jingle.session.js 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /* jshint -W117 */
  2. // Jingle stuff
  3. function JingleSession(me, sid, connection) {
  4. this.me = me;
  5. this.sid = sid;
  6. this.connection = connection;
  7. this.initiator = null;
  8. this.responder = null;
  9. this.isInitiator = null;
  10. this.peerjid = null;
  11. this.state = null;
  12. this.peerconnection = null;
  13. this.remoteStream = null;
  14. this.localSDP = null;
  15. this.remoteSDP = null;
  16. this.localStreams = [];
  17. this.relayedStreams = [];
  18. this.remoteStreams = [];
  19. this.startTime = null;
  20. this.stopTime = null;
  21. this.media_constraints = null;
  22. this.pc_constraints = null;
  23. this.ice_config = {};
  24. this.drip_container = [];
  25. this.usetrickle = true;
  26. this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
  27. this.usedrip = false; // dripping is sending trickle candidates not one-by-one
  28. this.hadstuncandidate = false;
  29. this.hadturncandidate = false;
  30. this.lasticecandidate = false;
  31. this.statsinterval = null;
  32. this.reason = null;
  33. this.addssrc = [];
  34. this.removessrc = [];
  35. this.pendingop = null;
  36. this.wait = true;
  37. }
  38. JingleSession.prototype.initiate = function (peerjid, isInitiator) {
  39. var self = this;
  40. if (this.state !== null) {
  41. console.error('attempt to initiate on session ' + this.sid +
  42. 'in state ' + this.state);
  43. return;
  44. }
  45. this.isInitiator = isInitiator;
  46. this.state = 'pending';
  47. this.initiator = isInitiator ? this.me : peerjid;
  48. this.responder = !isInitiator ? this.me : peerjid;
  49. this.peerjid = peerjid;
  50. //console.log('create PeerConnection ' + JSON.stringify(this.ice_config));
  51. try {
  52. this.peerconnection = new RTCPeerconnection(this.ice_config,
  53. this.pc_constraints);
  54. } catch (e) {
  55. console.error('Failed to create PeerConnection, exception: ',
  56. e.message);
  57. console.error(e);
  58. return;
  59. }
  60. this.hadstuncandidate = false;
  61. this.hadturncandidate = false;
  62. this.lasticecandidate = false;
  63. this.peerconnection.onicecandidate = function (event) {
  64. self.sendIceCandidate(event.candidate);
  65. };
  66. this.peerconnection.onaddstream = function (event) {
  67. self.remoteStream = event.stream;
  68. self.remoteStreams.push(event.stream);
  69. $(document).trigger('remotestreamadded.jingle', [event, self.sid]);
  70. };
  71. this.peerconnection.onremovestream = function (event) {
  72. self.remoteStream = null;
  73. // FIXME: remove from this.remoteStreams
  74. $(document).trigger('remotestreamremoved.jingle', [event, self.sid]);
  75. };
  76. this.peerconnection.onsignalingstatechange = function (event) {
  77. if (!(self && self.peerconnection)) return;
  78. };
  79. this.peerconnection.oniceconnectionstatechange = function (event) {
  80. if (!(self && self.peerconnection)) return;
  81. switch (self.peerconnection.iceConnectionState) {
  82. case 'connected':
  83. this.startTime = new Date();
  84. break;
  85. case 'disconnected':
  86. this.stopTime = new Date();
  87. break;
  88. }
  89. $(document).trigger('iceconnectionstatechange.jingle', [self.sid, self]);
  90. };
  91. // add any local and relayed stream
  92. this.localStreams.forEach(function(stream) {
  93. self.peerconnection.addStream(stream);
  94. });
  95. this.relayedStreams.forEach(function(stream) {
  96. self.peerconnection.addStream(stream);
  97. });
  98. };
  99. JingleSession.prototype.accept = function () {
  100. var self = this;
  101. this.state = 'active';
  102. var pranswer = this.peerconnection.localDescription;
  103. if (!pranswer || pranswer.type != 'pranswer') {
  104. return;
  105. }
  106. console.log('going from pranswer to answer');
  107. if (this.usetrickle) {
  108. // remove candidates already sent from session-accept
  109. var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
  110. for (var i = 0; i < lines.length; i++) {
  111. pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
  112. }
  113. }
  114. while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
  115. // FIXME: change any inactive to sendrecv or whatever they were originally
  116. pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
  117. }
  118. var prsdp = new SDP(pranswer.sdp);
  119. var accept = $iq({to: this.peerjid,
  120. type: 'set'})
  121. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  122. action: 'session-accept',
  123. initiator: this.initiator,
  124. responder: this.responder,
  125. sid: this.sid });
  126. prsdp.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
  127. this.connection.sendIQ(accept,
  128. function () {
  129. var ack = {};
  130. ack.source = 'answer';
  131. $(document).trigger('ack.jingle', [self.sid, ack]);
  132. },
  133. function (stanza) {
  134. var error = ($(stanza).find('error').length) ? {
  135. code: $(stanza).find('error').attr('code'),
  136. reason: $(stanza).find('error :first')[0].tagName,
  137. }:{};
  138. error.source = 'answer';
  139. $(document).trigger('error.jingle', [self.sid, error]);
  140. },
  141. 10000);
  142. var sdp = this.peerconnection.localDescription.sdp;
  143. while (SDPUtil.find_line(sdp, 'a=inactive')) {
  144. // FIXME: change any inactive to sendrecv or whatever they were originally
  145. sdp = sdp.replace('a=inactive', 'a=sendrecv');
  146. }
  147. this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
  148. function () {
  149. //console.log('setLocalDescription success');
  150. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  151. },
  152. function (e) {
  153. console.error('setLocalDescription failed', e);
  154. }
  155. );
  156. };
  157. JingleSession.prototype.terminate = function (reason) {
  158. this.state = 'ended';
  159. this.reason = reason;
  160. this.peerconnection.close();
  161. if (this.statsinterval !== null) {
  162. window.clearInterval(this.statsinterval);
  163. this.statsinterval = null;
  164. }
  165. };
  166. JingleSession.prototype.active = function () {
  167. return this.state == 'active';
  168. };
  169. JingleSession.prototype.sendIceCandidate = function (candidate) {
  170. var self = this;
  171. if (candidate && !this.lasticecandidate) {
  172. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  173. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  174. if (!(ice && jcand)) {
  175. console.error('failed to get ice && jcand');
  176. return;
  177. }
  178. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  179. if (jcand.type === 'srflx') {
  180. this.hadstuncandidate = true;
  181. } else if (jcand.type === 'relay') {
  182. this.hadturncandidate = true;
  183. }
  184. if (this.usetrickle) {
  185. if (this.usedrip) {
  186. if (this.drip_container.length === 0) {
  187. // start 20ms callout
  188. window.setTimeout(function () {
  189. if (self.drip_container.length === 0) return;
  190. self.sendIceCandidates(self.drip_container);
  191. self.drip_container = [];
  192. }, 20);
  193. }
  194. this.drip_container.push(event.candidate);
  195. return;
  196. } else {
  197. self.sendIceCandidate([event.candidate]);
  198. }
  199. }
  200. } else {
  201. //console.log('sendIceCandidate: last candidate.');
  202. if (!this.usetrickle) {
  203. //console.log('should send full offer now...');
  204. var init = $iq({to: this.peerjid,
  205. type: 'set'})
  206. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  207. action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
  208. initiator: this.initiator,
  209. sid: this.sid});
  210. this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
  211. this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
  212. this.connection.sendIQ(init,
  213. function () {
  214. //console.log('session initiate ack');
  215. var ack = {};
  216. ack.source = 'offer';
  217. $(document).trigger('ack.jingle', [self.sid, ack]);
  218. },
  219. function (stanza) {
  220. self.state = 'error';
  221. self.peerconnection.close();
  222. var error = ($(stanza).find('error').length) ? {
  223. code: $(stanza).find('error').attr('code'),
  224. reason: $(stanza).find('error :first')[0].tagName,
  225. }:{};
  226. error.source = 'offer';
  227. $(document).trigger('error.jingle', [self.sid, error]);
  228. },
  229. 10000);
  230. }
  231. this.lasticecandidate = true;
  232. console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  233. console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  234. if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
  235. $(document).trigger('nostuncandidates.jingle', [this.sid]);
  236. }
  237. }
  238. };
  239. JingleSession.prototype.sendIceCandidates = function (candidates) {
  240. console.log('sendIceCandidates', candidates);
  241. var cand = $iq({to: this.peerjid, type: 'set'})
  242. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  243. action: 'transport-info',
  244. initiator: this.initiator,
  245. sid: this.sid});
  246. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  247. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  248. if (cands.length > 0) {
  249. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  250. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  251. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  252. name: cands[0].sdpMid
  253. }).c('transport', ice);
  254. for (var i = 0; i < cands.length; i++) {
  255. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  256. }
  257. // add fingerprint
  258. if (SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session)) {
  259. var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session));
  260. tmp.required = true;
  261. cand.c('fingerprint').t(tmp.fingerprint);
  262. delete tmp.fingerprint;
  263. cand.attrs(tmp);
  264. cand.up();
  265. }
  266. cand.up(); // transport
  267. cand.up(); // content
  268. }
  269. }
  270. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  271. //console.log('was this the last candidate', this.lasticecandidate);
  272. this.connection.sendIQ(cand,
  273. function () {
  274. var ack = {};
  275. ack.source = 'transportinfo';
  276. $(document).trigger('ack.jingle', [this.sid, ack]);
  277. },
  278. function (stanza) {
  279. var error = ($(stanza).find('error').length) ? {
  280. code: $(stanza).find('error').attr('code'),
  281. reason: $(stanza).find('error :first')[0].tagName,
  282. }:{};
  283. error.source = 'transportinfo';
  284. $(document).trigger('error.jingle', [this.sid, error]);
  285. },
  286. 10000);
  287. };
  288. JingleSession.prototype.sendOffer = function () {
  289. //console.log('sendOffer...');
  290. var self = this;
  291. this.peerconnection.createOffer(function (sdp) {
  292. self.createdOffer(sdp);
  293. },
  294. function (e) {
  295. console.error('createOffer failed', e);
  296. },
  297. this.media_constraints
  298. );
  299. };
  300. JingleSession.prototype.createdOffer = function (sdp) {
  301. //console.log('createdOffer', sdp);
  302. var self = this;
  303. this.localSDP = new SDP(sdp.sdp);
  304. //this.localSDP.mangle();
  305. if (this.usetrickle) {
  306. var init = $iq({to: this.peerjid,
  307. type: 'set'})
  308. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  309. action: 'session-initiate',
  310. initiator: this.initiator,
  311. sid: this.sid});
  312. this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
  313. this.connection.sendIQ(init,
  314. function () {
  315. var ack = {};
  316. ack.source = 'offer';
  317. $(document).trigger('ack.jingle', [self.sid, ack]);
  318. },
  319. function (stanza) {
  320. self.state = 'error';
  321. self.peerconnection.close();
  322. var error = ($(stanza).find('error').length) ? {
  323. code: $(stanza).find('error').attr('code'),
  324. reason: $(stanza).find('error :first')[0].tagName,
  325. }:{};
  326. error.source = 'offer';
  327. $(document).trigger('error.jingle', [self.sid, error]);
  328. },
  329. 10000);
  330. }
  331. sdp.sdp = this.localSDP.raw;
  332. this.peerconnection.setLocalDescription(sdp,
  333. function () {
  334. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  335. //console.log('setLocalDescription success');
  336. },
  337. function (e) {
  338. console.error('setLocalDescription failed', e);
  339. }
  340. );
  341. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  342. for (var i = 0; i < cands.length; i++) {
  343. var cand = SDPUtil.parse_icecandidate(cands[i]);
  344. if (cand.type == 'srflx') {
  345. this.hadstuncandidate = true;
  346. } else if (cand.type == 'relay') {
  347. this.hadturncandidate = true;
  348. }
  349. }
  350. };
  351. JingleSession.prototype.setRemoteDescription = function (elem, desctype) {
  352. //console.log('setting remote description... ', desctype);
  353. this.remoteSDP = new SDP('');
  354. this.remoteSDP.fromJingle(elem);
  355. if (this.peerconnection.remoteDescription !== null) {
  356. console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
  357. if (this.peerconnection.remoteDescription.type == 'pranswer') {
  358. var pranswer = new SDP(this.peerconnection.remoteDescription.sdp);
  359. for (var i = 0; i < pranswer.media.length; i++) {
  360. // make sure we have ice ufrag and pwd
  361. if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
  362. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
  363. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
  364. } else {
  365. console.warn('no ice ufrag?');
  366. }
  367. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
  368. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
  369. } else {
  370. console.warn('no ice pwd?');
  371. }
  372. }
  373. // copy over candidates
  374. var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
  375. for (var j = 0; j < lines.length; j++) {
  376. this.remoteSDP.media[i] += lines[j] + '\r\n';
  377. }
  378. }
  379. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  380. }
  381. }
  382. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  383. this.peerconnection.setRemoteDescription(remotedesc,
  384. function () {
  385. //console.log('setRemoteDescription success');
  386. },
  387. function (e) {
  388. console.error('setRemoteDescription error', e);
  389. }
  390. );
  391. };
  392. JingleSession.prototype.addIceCandidate = function (elem) {
  393. var self = this;
  394. if (this.peerconnection.signalingState == 'closed') {
  395. return;
  396. }
  397. if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
  398. console.log('trickle ice candidate arriving before session accept...');
  399. // create a PRANSWER for setRemoteDescription
  400. if (!this.remoteSDP) {
  401. var cobbled = 'v=0\r\n' +
  402. 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
  403. 's=-\r\n' +
  404. 't=0 0\r\n';
  405. // first, take some things from the local description
  406. for (var i = 0; i < this.localSDP.media.length; i++) {
  407. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
  408. cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
  409. if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
  410. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
  411. }
  412. cobbled += 'a=inactive\r\n';
  413. }
  414. this.remoteSDP = new SDP(cobbled);
  415. }
  416. // then add things like ice and dtls from remote candidate
  417. elem.each(function () {
  418. for (var i = 0; i < self.remoteSDP.media.length; i++) {
  419. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  420. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  421. if (!SDPUtil.find_line(self.remoteSDP.media[i], 'a=ice-ufrag:')) {
  422. var tmp = $(this).find('transport');
  423. self.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  424. self.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  425. tmp = $(this).find('transport>fingerprint');
  426. if (tmp.length) {
  427. self.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  428. } else {
  429. console.log('no dtls fingerprint (webrtc issue #1718?)');
  430. self.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
  431. }
  432. break;
  433. }
  434. }
  435. }
  436. });
  437. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  438. // we need a complete SDP with ice-ufrag/ice-pwd in all parts
  439. // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
  440. // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
  441. var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
  442. return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
  443. }).length == this.remoteSDP.media.length;
  444. if (iscomplete) {
  445. console.log('setting pranswer');
  446. try {
  447. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }),
  448. function() {
  449. },
  450. function(e) {
  451. console.log('setRemoteDescription pranswer failed', e.toString());
  452. });
  453. } catch (e) {
  454. console.error('setting pranswer failed', e);
  455. }
  456. } else {
  457. //console.log('not yet setting pranswer');
  458. }
  459. }
  460. // operate on each content element
  461. elem.each(function () {
  462. // would love to deactivate this, but firefox still requires it
  463. var idx = -1;
  464. var i;
  465. for (i = 0; i < self.remoteSDP.media.length; i++) {
  466. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  467. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  468. idx = i;
  469. break;
  470. }
  471. }
  472. if (idx == -1) { // fall back to localdescription
  473. for (i = 0; i < self.localSDP.media.length; i++) {
  474. if (SDPUtil.find_line(self.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  475. self.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  476. idx = i;
  477. break;
  478. }
  479. }
  480. }
  481. var name = $(this).attr('name');
  482. // TODO: check ice-pwd and ice-ufrag?
  483. $(this).find('transport>candidate').each(function () {
  484. var line, candidate;
  485. line = SDPUtil.candidateFromJingle(this);
  486. candidate = new RTCIceCandidate({sdpMLineIndex: idx,
  487. sdpMid: name,
  488. candidate: line});
  489. try {
  490. self.peerconnection.addIceCandidate(candidate);
  491. } catch (e) {
  492. console.error('addIceCandidate failed', e.toString(), line);
  493. }
  494. });
  495. });
  496. };
  497. JingleSession.prototype.sendAnswer = function (provisional) {
  498. //console.log('createAnswer', provisional);
  499. var self = this;
  500. this.peerconnection.createAnswer(
  501. function (sdp) {
  502. self.createdAnswer(sdp, provisional);
  503. },
  504. function (e) {
  505. console.error('createAnswer failed', e);
  506. },
  507. this.media_constraints
  508. );
  509. };
  510. JingleSession.prototype.createdAnswer = function (sdp, provisional) {
  511. //console.log('createAnswer callback');
  512. var self = this;
  513. this.localSDP = new SDP(sdp.sdp);
  514. //this.localSDP.mangle();
  515. this.usepranswer = provisional === true;
  516. if (this.usetrickle) {
  517. if (!this.usepranswer) {
  518. var accept = $iq({to: this.peerjid,
  519. type: 'set'})
  520. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  521. action: 'session-accept',
  522. initiator: this.initiator,
  523. responder: this.responder,
  524. sid: this.sid });
  525. this.localSDP.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
  526. this.connection.sendIQ(accept,
  527. function () {
  528. var ack = {};
  529. ack.source = 'answer';
  530. $(document).trigger('ack.jingle', [self.sid, ack]);
  531. },
  532. function (stanza) {
  533. var error = ($(stanza).find('error').length) ? {
  534. code: $(stanza).find('error').attr('code'),
  535. reason: $(stanza).find('error :first')[0].tagName,
  536. }:{};
  537. error.source = 'answer';
  538. $(document).trigger('error.jingle', [self.sid, error]);
  539. },
  540. 10000);
  541. } else {
  542. sdp.type = 'pranswer';
  543. for (var i = 0; i < this.localSDP.media.length; i++) {
  544. this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
  545. }
  546. this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
  547. }
  548. }
  549. sdp.sdp = this.localSDP.raw;
  550. this.peerconnection.setLocalDescription(sdp,
  551. function () {
  552. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  553. //console.log('setLocalDescription success');
  554. },
  555. function (e) {
  556. console.error('setLocalDescription failed', e);
  557. }
  558. );
  559. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  560. for (var j = 0; j < cands.length; j++) {
  561. var cand = SDPUtil.parse_icecandidate(cands[j]);
  562. if (cand.type == 'srflx') {
  563. this.hadstuncandidate = true;
  564. } else if (cand.type == 'relay') {
  565. this.hadturncandidate = true;
  566. }
  567. }
  568. };
  569. JingleSession.prototype.sendTerminate = function (reason, text) {
  570. var self = this,
  571. term = $iq({to: this.peerjid,
  572. type: 'set'})
  573. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  574. action: 'session-terminate',
  575. initiator: this.initiator,
  576. sid: this.sid})
  577. .c('reason')
  578. .c(reason || 'success');
  579. if (text) {
  580. term.up().c('text').t(text);
  581. }
  582. this.connection.sendIQ(term,
  583. function () {
  584. self.peerconnection.close();
  585. self.peerconnection = null;
  586. self.terminate();
  587. var ack = {};
  588. ack.source = 'terminate';
  589. $(document).trigger('ack.jingle', [self.sid, ack]);
  590. },
  591. function (stanza) {
  592. var error = ($(stanza).find('error').length) ? {
  593. code: $(stanza).find('error').attr('code'),
  594. reason: $(stanza).find('error :first')[0].tagName,
  595. }:{};
  596. $(document).trigger('ack.jingle', [self.sid, error]);
  597. },
  598. 10000);
  599. if (this.statsinterval !== null) {
  600. window.clearInterval(this.statsinterval);
  601. this.statsinterval = null;
  602. }
  603. };
  604. JingleSession.prototype.addSource = function (elem) {
  605. console.log('addssrc', new Date().getTime());
  606. console.log('ice', this.peerconnection.iceConnectionState);
  607. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  608. var self = this;
  609. $(elem).each(function (idx, content) {
  610. var name = $(content).attr('name');
  611. var lines = '';
  612. tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  613. tmp.each(function () {
  614. var ssrc = $(this).attr('ssrc');
  615. $(this).find('>parameter').each(function () {
  616. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  617. if ($(this).attr('value') && $(this).attr('value').length)
  618. lines += ':' + $(this).attr('value');
  619. lines += '\r\n';
  620. });
  621. });
  622. sdp.media.forEach(function(media, idx) {
  623. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  624. return;
  625. sdp.media[idx] += lines;
  626. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  627. self.addssrc[idx] += lines;
  628. });
  629. sdp.raw = sdp.session + sdp.media.join('');
  630. });
  631. this.modifySources();
  632. };
  633. JingleSession.prototype.removeSource = function (elem) {
  634. console.log('removessrc', new Date().getTime());
  635. console.log('ice', this.peerconnection.iceConnectionState);
  636. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  637. var self = this;
  638. $(elem).each(function (idx, content) {
  639. var name = $(content).attr('name');
  640. var lines = '';
  641. tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  642. tmp.each(function () {
  643. var ssrc = $(this).attr('ssrc');
  644. $(this).find('>parameter').each(function () {
  645. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  646. if ($(this).attr('value') && $(this).attr('value').length)
  647. lines += ':' + $(this).attr('value');
  648. lines += '\r\n';
  649. });
  650. });
  651. sdp.media.forEach(function(media, idx) {
  652. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  653. return;
  654. sdp.media[idx] += lines;
  655. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  656. self.removessrc[idx] += lines;
  657. });
  658. sdp.raw = sdp.session + sdp.media.join('');
  659. });
  660. this.modifySources();
  661. };
  662. JingleSession.prototype.modifySources = function() {
  663. var self = this;
  664. if (this.peerconnection.signalingState == 'closed') return;
  665. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null)) return;
  666. if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
  667. console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
  668. this.wait = true;
  669. window.setTimeout(function() { self.modifySources(); }, 250);
  670. return;
  671. }
  672. if (this.wait) {
  673. window.setTimeout(function() { self.modifySources(); }, 2500);
  674. this.wait = false;
  675. return;
  676. }
  677. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  678. // add sources
  679. this.addssrc.forEach(function(lines, idx) {
  680. sdp.media[idx] += lines;
  681. });
  682. this.addssrc = [];
  683. // remove sources
  684. this.removessrc.forEach(function(lines, idx) {
  685. lines = lines.split('\r\n');
  686. lines.pop(); // remove empty last element;
  687. lines.forEach(function(line) {
  688. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  689. });
  690. });
  691. this.removessrc = [];
  692. sdp.raw = sdp.session + sdp.media.join('');
  693. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  694. function() {
  695. self.peerconnection.createAnswer(
  696. function(modifiedAnswer) {
  697. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  698. if (self.pendingop !== null) {
  699. var sdp = new SDP(modifiedAnswer.sdp);
  700. if (sdp.media.length > 1) {
  701. switch(self.pendingop) {
  702. case 'mute':
  703. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  704. break;
  705. case 'unmute':
  706. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  707. break;
  708. }
  709. sdp.raw = sdp.session + sdp.media.join('');
  710. modifiedAnswer.sdp = sdp.raw;
  711. }
  712. self.pendingop = null;
  713. }
  714. self.peerconnection.setLocalDescription(modifiedAnswer,
  715. function() {
  716. //console.log('modified setLocalDescription ok');
  717. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  718. },
  719. function(error) {
  720. console.log('modified setLocalDescription failed');
  721. }
  722. );
  723. },
  724. function(error) {
  725. console.log('modified answer failed');
  726. }
  727. );
  728. },
  729. function(error) {
  730. console.log('modify failed');
  731. }
  732. );
  733. };
  734. // SDP-based mute by going recvonly/sendrecv
  735. // FIXME: should probably black out the screen as well
  736. JingleSession.prototype.hardMuteVideo = function (muted) {
  737. this.pendingop = muted ? 'mute' : 'unmute';
  738. this.modifySources();
  739. this.connection.jingle.localStream.getVideoTracks().forEach(function (track) {
  740. track.enabled = !muted;
  741. });
  742. };
  743. JingleSession.prototype.sendMute = function (muted, content) {
  744. var info = $iq({to: this.peerjid,
  745. type: 'set'})
  746. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  747. action: 'session-info',
  748. initiator: this.initiator,
  749. sid: this.sid });
  750. info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  751. info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
  752. if (content) {
  753. info.attrs({'name': content});
  754. }
  755. this.connection.send(info);
  756. };
  757. JingleSession.prototype.sendRinging = function () {
  758. var info = $iq({to: this.peerjid,
  759. type: 'set'})
  760. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  761. action: 'session-info',
  762. initiator: this.initiator,
  763. sid: this.sid });
  764. info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  765. this.connection.send(info);
  766. };
  767. JingleSession.prototype.getStats = function (interval) {
  768. var self = this;
  769. var recv = {audio: 0, video: 0};
  770. var lost = {audio: 0, video: 0};
  771. var lastrecv = {audio: 0, video: 0};
  772. var lastlost = {audio: 0, video: 0};
  773. var loss = {audio: 0, video: 0};
  774. var delta = {audio: 0, video: 0};
  775. this.statsinterval = window.setInterval(function () {
  776. if (self && self.peerconnection && self.peerconnection.getStats) {
  777. self.peerconnection.getStats(function (stats) {
  778. var results = stats.result();
  779. // TODO: there are so much statistics you can get from this..
  780. for (var i = 0; i < results.length; ++i) {
  781. if (results[i].type == 'ssrc') {
  782. var packetsrecv = results[i].stat('packetsReceived');
  783. var packetslost = results[i].stat('packetsLost');
  784. if (packetsrecv && packetslost) {
  785. packetsrecv = parseInt(packetsrecv, 10);
  786. packetslost = parseInt(packetslost, 10);
  787. if (results[i].stat('googFrameRateReceived')) {
  788. lastlost.video = lost.video;
  789. lastrecv.video = recv.video;
  790. recv.video = packetsrecv;
  791. lost.video = packetslost;
  792. } else {
  793. lastlost.audio = lost.audio;
  794. lastrecv.audio = recv.audio;
  795. recv.audio = packetsrecv;
  796. lost.audio = packetslost;
  797. }
  798. }
  799. }
  800. }
  801. delta.audio = recv.audio - lastrecv.audio;
  802. delta.video = recv.video - lastrecv.video;
  803. loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
  804. loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
  805. $(document).trigger('packetloss.jingle', [self.sid, loss]);
  806. });
  807. }
  808. }, interval || 3000);
  809. return this.statsinterval;
  810. };