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

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