Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

colibri.focus.js 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /* colibri.js -- a COLIBRI focus
  2. * The colibri spec has been submitted to the XMPP Standards Foundation
  3. * for publications as a XMPP extensions:
  4. * http://xmpp.org/extensions/inbox/colibri.html
  5. *
  6. * colibri.js is a participating focus, i.e. the focus participates
  7. * in the conference. The conference itself can be ad-hoc, through a
  8. * MUC, through PubSub, etc.
  9. *
  10. * colibri.js relies heavily on the strophe.jingle library available
  11. * from https://github.com/ESTOS/strophe.jingle
  12. * and interoperates with the Jitsi videobridge available from
  13. * https://jitsi.org/Projects/JitsiVideobridge
  14. */
  15. /*
  16. Copyright (c) 2013 ESTOS GmbH
  17. Permission is hereby granted, free of charge, to any person obtaining a copy
  18. of this software and associated documentation files (the "Software"), to deal
  19. in the Software without restriction, including without limitation the rights
  20. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. copies of the Software, and to permit persons to whom the Software is
  22. furnished to do so, subject to the following conditions:
  23. The above copyright notice and this permission notice shall be included in
  24. all copies or substantial portions of the Software.
  25. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31. THE SOFTWARE.
  32. */
  33. /* jshint -W117 */
  34. ColibriFocus.prototype = Object.create(SessionBase.prototype);
  35. function ColibriFocus(connection, bridgejid) {
  36. SessionBase.call(this, connection, Math.random().toString(36).substr(2, 12));
  37. this.bridgejid = bridgejid;
  38. this.peers = [];
  39. this.confid = null;
  40. /**
  41. * Local XMPP resource used to join the multi user chat.
  42. * @type {*}
  43. */
  44. this.myMucResource = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  45. /**
  46. * Default channel expire value in seconds.
  47. * @type {number}
  48. */
  49. this.channelExpire = 60;
  50. // media types of the conference
  51. if (config.openSctp)
  52. {
  53. this.media = ['audio', 'video', 'data'];
  54. }
  55. else
  56. {
  57. this.media = ['audio', 'video'];
  58. }
  59. this.connection.jingle.sessions[this.sid] = this;
  60. this.mychannel = [];
  61. this.channels = [];
  62. this.remotessrc = {};
  63. // container for candidates from the focus
  64. // gathered before confid is known
  65. this.drip_container = [];
  66. // silly wait flag
  67. this.wait = true;
  68. this.recordingEnabled = false;
  69. }
  70. // creates a conferences with an initial set of peers
  71. ColibriFocus.prototype.makeConference = function (peers) {
  72. var self = this;
  73. if (this.confid !== null) {
  74. console.error('makeConference called twice? Ignoring...');
  75. // FIXME: just invite peers?
  76. return;
  77. }
  78. this.confid = 0; // !null
  79. this.peers = [];
  80. peers.forEach(function (peer) {
  81. self.peers.push(peer);
  82. self.channels.push([]);
  83. });
  84. this.peerconnection
  85. = new TraceablePeerConnection(
  86. this.connection.jingle.ice_config,
  87. this.connection.jingle.pc_constraints );
  88. if(this.connection.jingle.localAudio) {
  89. this.peerconnection.addStream(this.connection.jingle.localAudio);
  90. }
  91. if(this.connection.jingle.localVideo) {
  92. this.peerconnection.addStream(this.connection.jingle.localVideo);
  93. }
  94. this.peerconnection.oniceconnectionstatechange = function (event) {
  95. console.warn('ice connection state changed to', self.peerconnection.iceConnectionState);
  96. /*
  97. if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
  98. console.log('adding new remote SSRCs from iceconnectionstatechange');
  99. window.setTimeout(function() { self.modifySources(); }, 1000);
  100. }
  101. */
  102. };
  103. this.peerconnection.onsignalingstatechange = function (event) {
  104. console.warn(self.peerconnection.signalingState);
  105. /*
  106. if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
  107. console.log('adding new remote SSRCs from signalingstatechange');
  108. window.setTimeout(function() { self.modifySources(); }, 1000);
  109. }
  110. */
  111. };
  112. this.peerconnection.onaddstream = function (event) {
  113. // search the jid associated with this stream
  114. Object.keys(self.remotessrc).forEach(function (jid) {
  115. if (self.remotessrc[jid].join('\r\n').indexOf('mslabel:' + event.stream.id) != -1) {
  116. event.peerjid = jid;
  117. }
  118. });
  119. $(document).trigger('remotestreamadded.jingle', [event, self.sid]);
  120. };
  121. this.peerconnection.onicecandidate = function (event) {
  122. //console.log('focus onicecandidate', self.confid, new Date().getTime(), event.candidate);
  123. if (!event.candidate) {
  124. console.log('end of candidates');
  125. return;
  126. }
  127. self.sendIceCandidate(event.candidate);
  128. };
  129. this._makeConference();
  130. /*
  131. this.peerconnection.createOffer(
  132. function (offer) {
  133. self.peerconnection.setLocalDescription(
  134. offer,
  135. function () {
  136. // success
  137. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  138. // FIXME: could call _makeConference here and trickle candidates later
  139. self._makeConference();
  140. },
  141. function (error) {
  142. console.log('setLocalDescription failed', error);
  143. }
  144. );
  145. },
  146. function (error) {
  147. console.warn(error);
  148. }
  149. );
  150. */
  151. };
  152. // Sends a COLIBRI message which enables or disables (according to 'state') the
  153. // recording on the bridge.
  154. ColibriFocus.prototype.setRecording = function(state, token, callback) {
  155. var self = this;
  156. var elem = $iq({to: this.bridgejid, type: 'get'});
  157. elem.c('conference', {
  158. xmlns: 'http://jitsi.org/protocol/colibri',
  159. id: this.confid
  160. });
  161. elem.c('recording', {state: state, token: token});
  162. elem.up();
  163. this.connection.sendIQ(elem,
  164. function (result) {
  165. console.log('Set recording "', state, '". Result:', result);
  166. var recordingElem = $(result).find('>conference>recording');
  167. var newState = recordingElem.attr('state');
  168. if (newState == null){
  169. newState = false;
  170. }
  171. self.recordingEnabled = newState;
  172. callback(newState);
  173. },
  174. function (error) {
  175. console.warn(error);
  176. }
  177. );
  178. };
  179. ColibriFocus.prototype._makeConference = function () {
  180. var self = this;
  181. var elem = $iq({to: this.bridgejid, type: 'get'});
  182. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri'});
  183. this.media.forEach(function (name) {
  184. var isData = name === 'data';
  185. var channel = isData ? 'sctpconnection' : 'channel';
  186. elem.c('content', {name: name});
  187. elem.c(channel, {
  188. initiator: 'true',
  189. expire: '15',
  190. endpoint: self.myMucResource
  191. });
  192. if (isData)
  193. elem.attrs({port: 5000});
  194. elem.up();// end of channel
  195. for (var j = 0; j < self.peers.length; j++) {
  196. elem.c(channel, {
  197. initiator: 'true',
  198. expire: '15',
  199. endpoint: self.peers[j].substr(1 + self.peers[j].lastIndexOf('/'))
  200. });
  201. if (isData)
  202. elem.attrs({port: 5000});
  203. elem.up(); // end of channel
  204. }
  205. elem.up(); // end of content
  206. });
  207. /*
  208. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  209. localSDP.media.forEach(function (media, channel) {
  210. var name = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  211. elem.c('content', {name: name});
  212. elem.c('channel', {initiator: 'false', expire: '15'});
  213. // FIXME: should reuse code from .toJingle
  214. var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
  215. for (var j = 0; j < mline.fmt.length; j++) {
  216. var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
  217. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  218. elem.up();
  219. }
  220. localSDP.TransportToJingle(channel, elem);
  221. elem.up(); // end of channel
  222. for (j = 0; j < self.peers.length; j++) {
  223. elem.c('channel', {initiator: 'true', expire:'15' }).up();
  224. }
  225. elem.up(); // end of content
  226. });
  227. */
  228. this.connection.sendIQ(elem,
  229. function (result) {
  230. self.createdConference(result);
  231. },
  232. function (error) {
  233. console.warn(error);
  234. }
  235. );
  236. };
  237. // callback when a conference was created
  238. ColibriFocus.prototype.createdConference = function (result) {
  239. console.log('created a conference on the bridge');
  240. var self = this;
  241. var tmp;
  242. this.confid = $(result).find('>conference').attr('id');
  243. var remotecontents = $(result).find('>conference>content').get();
  244. var numparticipants = 0;
  245. for (var i = 0; i < remotecontents.length; i++)
  246. {
  247. var contentName = $(remotecontents[i]).attr('name');
  248. var channelName
  249. = contentName !== 'data' ? '>channel' : '>sctpconnection';
  250. tmp = $(remotecontents[i]).find(channelName).get();
  251. this.mychannel.push($(tmp.shift()));
  252. numparticipants = tmp.length;
  253. for (j = 0; j < tmp.length; j++) {
  254. if (this.channels[j] === undefined) {
  255. this.channels[j] = [];
  256. }
  257. this.channels[j].push(tmp[j]);
  258. }
  259. }
  260. console.log('remote channels', this.channels);
  261. // Notify that the focus has created the conference on the bridge
  262. $(document).trigger('conferenceCreated.jingle', [self]);
  263. var bridgeSDP = new SDP(
  264. 'v=0\r\n' +
  265. 'o=- 5151055458874951233 2 IN IP4 127.0.0.1\r\n' +
  266. 's=-\r\n' +
  267. 't=0 0\r\n' +
  268. /* Audio */
  269. 'm=audio 1 RTP/SAVPF 111 103 104 0 8 106 105 13 126\r\n' +
  270. 'c=IN IP4 0.0.0.0\r\n' +
  271. 'a=rtcp:1 IN IP4 0.0.0.0\r\n' +
  272. 'a=mid:audio\r\n' +
  273. 'a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\n' +
  274. 'a=sendrecv\r\n' +
  275. 'a=rtpmap:111 opus/48000/2\r\n' +
  276. 'a=fmtp:111 minptime=10\r\n' +
  277. 'a=rtpmap:103 ISAC/16000\r\n' +
  278. 'a=rtpmap:104 ISAC/32000\r\n' +
  279. 'a=rtpmap:0 PCMU/8000\r\n' +
  280. 'a=rtpmap:8 PCMA/8000\r\n' +
  281. 'a=rtpmap:106 CN/32000\r\n' +
  282. 'a=rtpmap:105 CN/16000\r\n' +
  283. 'a=rtpmap:13 CN/8000\r\n' +
  284. 'a=rtpmap:126 telephone-event/8000\r\n' +
  285. 'a=maxptime:60\r\n' +
  286. /* Video */
  287. 'm=video 1 RTP/SAVPF 100 116 117\r\n' +
  288. 'c=IN IP4 0.0.0.0\r\n' +
  289. 'a=rtcp:1 IN IP4 0.0.0.0\r\n' +
  290. 'a=mid:video\r\n' +
  291. 'a=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\n' +
  292. 'a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\n' +
  293. 'a=sendrecv\r\n' +
  294. 'a=rtpmap:100 VP8/90000\r\n' +
  295. 'a=rtcp-fb:100 ccm fir\r\n' +
  296. 'a=rtcp-fb:100 nack\r\n' +
  297. 'a=rtcp-fb:100 goog-remb\r\n' +
  298. 'a=rtpmap:116 red/90000\r\n' +
  299. 'a=rtpmap:117 ulpfec/90000\r\n' +
  300. /* Data SCTP */
  301. (config.openSctp ?
  302. 'm=application 1 DTLS/SCTP 5000\r\n' +
  303. 'c=IN IP4 0.0.0.0\r\n' +
  304. 'a=sctpmap:5000 webrtc-datachannel\r\n' +
  305. 'a=mid:data\r\n'
  306. : '')
  307. );
  308. bridgeSDP.media.length = this.mychannel.length;
  309. var channel;
  310. /*
  311. for (channel = 0; channel < bridgeSDP.media.length; channel++) {
  312. bridgeSDP.media[channel] = '';
  313. // unchanged lines
  314. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'm=') + '\r\n';
  315. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'c=') + '\r\n';
  316. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:')) {
  317. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:') + '\r\n';
  318. }
  319. if (SDPUtil.find_line(localSDP.media[channel], 'a=mid:')) {
  320. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=mid:') + '\r\n';
  321. }
  322. if (SDPUtil.find_line(localSDP.media[channel], 'a=sendrecv')) {
  323. bridgeSDP.media[channel] += 'a=sendrecv\r\n';
  324. }
  325. if (SDPUtil.find_line(localSDP.media[channel], 'a=extmap:')) {
  326. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=extmap:').join('\r\n') + '\r\n';
  327. }
  328. // FIXME: should look at m-line and group the ids together
  329. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtpmap:')) {
  330. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtpmap:').join('\r\n') + '\r\n';
  331. }
  332. if (SDPUtil.find_line(localSDP.media[channel], 'a=fmtp:')) {
  333. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=fmtp:').join('\r\n') + '\r\n';
  334. }
  335. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp-fb:')) {
  336. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtcp-fb:').join('\r\n') + '\r\n';
  337. }
  338. // FIXME: changed lines -- a=sendrecv direction, a=setup direction
  339. }
  340. */
  341. for (channel = 0; channel < bridgeSDP.media.length; channel++) {
  342. // get the mixed ssrc
  343. tmp = $(this.mychannel[channel]).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  344. // FIXME: check rtp-level-relay-type
  345. var isData = bridgeSDP.media[channel].indexOf('application') !== -1;
  346. if (!isData && tmp.length)
  347. {
  348. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
  349. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
  350. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
  351. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  352. }
  353. else if (!isData)
  354. {
  355. // make chrome happy... '3735928559' == 0xDEADBEEF
  356. // FIXME: this currently appears as two streams, should be one
  357. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
  358. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
  359. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
  360. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  361. }
  362. // FIXME: should take code from .fromJingle
  363. tmp = $(this.mychannel[channel]).find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  364. if (tmp.length) {
  365. bridgeSDP.media[channel] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  366. bridgeSDP.media[channel] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  367. tmp.find('>candidate').each(function () {
  368. bridgeSDP.media[channel] += SDPUtil.candidateFromJingle(this);
  369. });
  370. tmp = tmp.find('>fingerprint');
  371. if (tmp.length) {
  372. bridgeSDP.media[channel] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  373. bridgeSDP.media[channel] += 'a=setup:actpass\r\n'; // offer so always actpass
  374. }
  375. }
  376. }
  377. bridgeSDP.raw = bridgeSDP.session + bridgeSDP.media.join('');
  378. this.peerconnection.setRemoteDescription(
  379. new RTCSessionDescription({type: 'offer', sdp: bridgeSDP.raw}),
  380. function () {
  381. console.log('setRemoteDescription success');
  382. self.peerconnection.createAnswer(
  383. function (answer) {
  384. self.peerconnection.setLocalDescription(answer,
  385. function () {
  386. console.log('setLocalDescription succeded.');
  387. // make sure our presence is updated
  388. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  389. var elem = $iq({to: self.bridgejid, type: 'get'});
  390. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: self.confid});
  391. var localSDP = new SDP(self.peerconnection.localDescription.sdp);
  392. localSDP.media.forEach(function (media, channel) {
  393. var name = SDPUtil.parse_mid(SDPUtil.find_line(media, 'a=mid:'));
  394. elem.c('content', {name: name});
  395. var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
  396. if (name !== 'data')
  397. {
  398. elem.c('channel', {
  399. initiator: 'true',
  400. expire: self.channelExpire,
  401. id: self.mychannel[channel].attr('id'),
  402. endpoint: self.myMucResource
  403. });
  404. // FIXME: should reuse code from .toJingle
  405. for (var j = 0; j < mline.fmt.length; j++)
  406. {
  407. var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
  408. if (rtpmap)
  409. {
  410. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  411. elem.up();
  412. }
  413. }
  414. }
  415. else
  416. {
  417. var sctpmap = SDPUtil.find_line(media, 'a=sctpmap:' + mline.fmt[0]);
  418. var sctpPort = SDPUtil.parse_sctpmap(sctpmap)[0];
  419. elem.c("sctpconnection",
  420. {
  421. initiator: 'true',
  422. expire: self.channelExpire,
  423. endpoint: self.myMucResource,
  424. port: sctpPort
  425. }
  426. );
  427. }
  428. localSDP.TransportToJingle(channel, elem);
  429. elem.up(); // end of channel
  430. elem.up(); // end of content
  431. });
  432. self.connection.sendIQ(elem,
  433. function (result) {
  434. // ...
  435. },
  436. function (error) {
  437. console.error(
  438. "ERROR setLocalDescription succeded",
  439. error, elem);
  440. }
  441. );
  442. // now initiate sessions
  443. for (var i = 0; i < numparticipants; i++) {
  444. self.initiate(self.peers[i], true);
  445. }
  446. // Notify we've created the conference
  447. $(document).trigger(
  448. 'conferenceCreated.jingle', self);
  449. },
  450. function (error) {
  451. console.warn('setLocalDescription failed.', error);
  452. }
  453. );
  454. },
  455. function (error) {
  456. console.warn('createAnswer failed.', error);
  457. }
  458. );
  459. /*
  460. for (var i = 0; i < numparticipants; i++) {
  461. self.initiate(self.peers[i], true);
  462. }
  463. */
  464. },
  465. function (error) {
  466. console.log('setRemoteDescription failed.', error);
  467. }
  468. );
  469. };
  470. // send a session-initiate to a new participant
  471. ColibriFocus.prototype.initiate = function (peer, isInitiator) {
  472. var participant = this.peers.indexOf(peer);
  473. console.log('tell', peer, participant);
  474. var sdp;
  475. if (this.peerconnection !== null && this.peerconnection.signalingState == 'stable') {
  476. sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  477. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  478. // throw away stuff we don't want
  479. // not needed with static offer
  480. sdp.removeSessionLines('a=group:');
  481. sdp.removeSessionLines('a=msid-semantic:'); // FIXME: not mapped over jingle anyway...
  482. for (var i = 0; i < sdp.media.length; i++) {
  483. sdp.removeMediaLines(i, 'a=rtcp-mux');
  484. sdp.removeMediaLines(i, 'a=ssrc:');
  485. sdp.removeMediaLines(i, 'a=crypto:');
  486. sdp.removeMediaLines(i, 'a=candidate:');
  487. sdp.removeMediaLines(i, 'a=ice-options:google-ice');
  488. sdp.removeMediaLines(i, 'a=ice-ufrag:');
  489. sdp.removeMediaLines(i, 'a=ice-pwd:');
  490. sdp.removeMediaLines(i, 'a=fingerprint:');
  491. sdp.removeMediaLines(i, 'a=setup:');
  492. if (1) { //i > 0) { // not for audio FIXME: does not work as intended
  493. // re-add all remote a=ssrcs
  494. for (var jid in this.remotessrc) {
  495. if (jid == peer) continue;
  496. sdp.media[i] += this.remotessrc[jid][i];
  497. }
  498. // and local a=ssrc lines
  499. sdp.media[i] += SDPUtil.find_lines(localSDP.media[i], 'a=ssrc').join('\r\n') + '\r\n';
  500. }
  501. }
  502. sdp.raw = sdp.session + sdp.media.join('');
  503. } else {
  504. console.error('can not initiate a new session without a stable peerconnection');
  505. return;
  506. }
  507. // add stuff we got from the bridge
  508. for (var j = 0; j < sdp.media.length; j++) {
  509. var chan = $(this.channels[participant][j]);
  510. console.log('channel id', chan.attr('id'));
  511. tmp = chan.find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  512. if (tmp.length) {
  513. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
  514. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
  515. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
  516. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  517. }
  518. // No SSRCs for 'data', comes when j == 2
  519. else if (j < 2)
  520. {
  521. // make chrome happy... '3735928559' == 0xDEADBEEF
  522. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
  523. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
  524. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
  525. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  526. }
  527. tmp = chan.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  528. if (tmp.length) {
  529. if (tmp.attr('ufrag'))
  530. sdp.media[j] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  531. if (tmp.attr('pwd'))
  532. sdp.media[j] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  533. // and the candidates...
  534. tmp.find('>candidate').each(function () {
  535. sdp.media[j] += SDPUtil.candidateFromJingle(this);
  536. });
  537. tmp = tmp.find('>fingerprint');
  538. if (tmp.length) {
  539. sdp.media[j] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  540. /*
  541. if (tmp.attr('direction')) {
  542. sdp.media[j] += 'a=setup:' + tmp.attr('direction') + '\r\n';
  543. }
  544. */
  545. sdp.media[j] += 'a=setup:actpass\r\n';
  546. }
  547. }
  548. }
  549. // make a new colibri session and configure it
  550. // FIXME: is it correct to use this.connection.jid when used in a MUC?
  551. var sess = new ColibriSession(this.connection.jid,
  552. Math.random().toString(36).substr(2, 12), // random string
  553. this.connection);
  554. sess.initiate(peer);
  555. sess.colibri = this;
  556. // We do not announce our audio per conference peer, so only video is set here
  557. sess.localVideo = this.connection.jingle.localVideo;
  558. sess.media_constraints = this.connection.jingle.media_constraints;
  559. sess.pc_constraints = this.connection.jingle.pc_constraints;
  560. sess.ice_config = this.connection.jingle.ice_config;
  561. this.connection.jingle.sessions[sess.sid] = sess;
  562. this.connection.jingle.jid2session[sess.peerjid] = sess;
  563. // send a session-initiate
  564. var init = $iq({to: peer, type: 'set'})
  565. .c('jingle',
  566. {xmlns: 'urn:xmpp:jingle:1',
  567. action: 'session-initiate',
  568. initiator: sess.me,
  569. sid: sess.sid
  570. }
  571. );
  572. sdp.toJingle(init, 'initiator');
  573. this.connection.sendIQ(init,
  574. function (res) {
  575. console.log('got result');
  576. },
  577. function (err) {
  578. console.log('got error');
  579. }
  580. );
  581. };
  582. // pull in a new participant into the conference
  583. ColibriFocus.prototype.addNewParticipant = function (peer) {
  584. var self = this;
  585. if (this.confid === 0 || !this.peerconnection.localDescription)
  586. {
  587. // bad state
  588. if (this.confid === 0)
  589. {
  590. console.error('confid does not exist yet, postponing', peer);
  591. }
  592. else
  593. {
  594. console.error('local description not ready yet, postponing', peer);
  595. }
  596. window.setTimeout(function () {
  597. self.addNewParticipant(peer);
  598. }, 250);
  599. return;
  600. }
  601. var index = this.channels.length;
  602. this.channels.push([]);
  603. this.peers.push(peer);
  604. var elem = $iq({to: this.bridgejid, type: 'get'});
  605. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  606. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  607. localSDP.media.forEach(function (media, channel) {
  608. var name = SDPUtil.parse_mid(SDPUtil.find_line(media, 'a=mid:'));
  609. elem.c('content', {name: name});
  610. if (name !== 'data')
  611. {
  612. elem.c('channel', {
  613. initiator: 'true',
  614. expire: self.channelExpire,
  615. endpoint: peer.substr(1 + peer.lastIndexOf('/'))
  616. });
  617. }
  618. else
  619. {
  620. elem.c('sctpconnection', {
  621. endpoint: peer.substr(1 + peer.lastIndexOf('/')),
  622. initiator: 'true',
  623. expire: self.channelExpire,
  624. port: 5000
  625. });
  626. }
  627. elem.up(); // end of channel/sctpconnection
  628. elem.up(); // end of content
  629. });
  630. this.connection.sendIQ(elem,
  631. function (result) {
  632. var contents = $(result).find('>conference>content').get();
  633. for (var i = 0; i < contents.length; i++) {
  634. var channelXml = $(contents[i]).find('>channel');
  635. if (channelXml.length)
  636. {
  637. tmp = channelXml.get();
  638. }
  639. else
  640. {
  641. tmp = $(contents[i]).find('>sctpconnection').get();
  642. }
  643. self.channels[index][i] = tmp[0];
  644. }
  645. self.initiate(peer, true);
  646. },
  647. function (error) {
  648. console.warn(error);
  649. }
  650. );
  651. };
  652. // update the channel description (payload-types + dtls fp) for a participant
  653. ColibriFocus.prototype.updateChannel = function (remoteSDP, participant) {
  654. console.log('change allocation for', this.confid);
  655. var self = this;
  656. var change = $iq({to: this.bridgejid, type: 'set'});
  657. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  658. for (channel = 0; channel < this.channels[participant].length; channel++)
  659. {
  660. var name = SDPUtil.parse_mid(SDPUtil.find_line(remoteSDP.media[channel], 'a=mid:'));
  661. change.c('content', {name: name});
  662. if (name !== 'data')
  663. {
  664. change.c('channel', {
  665. id: $(this.channels[participant][channel]).attr('id'),
  666. endpoint: $(this.channels[participant][channel]).attr('endpoint'),
  667. expire: self.channelExpire
  668. });
  669. var rtpmap = SDPUtil.find_lines(remoteSDP.media[channel], 'a=rtpmap:');
  670. rtpmap.forEach(function (val) {
  671. // TODO: too much copy-paste
  672. var rtpmap = SDPUtil.parse_rtpmap(val);
  673. change.c('payload-type', rtpmap);
  674. //
  675. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
  676. /*
  677. if (SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id)) {
  678. tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id));
  679. for (var k = 0; k < tmp.length; k++) {
  680. change.c('parameter', tmp[k]).up();
  681. }
  682. }
  683. */
  684. change.up();
  685. });
  686. }
  687. else
  688. {
  689. var sctpmap = SDPUtil.find_line(remoteSDP.media[channel], 'a=sctpmap:');
  690. change.c('sctpconnection', {
  691. endpoint: $(this.channels[participant][channel]).attr('endpoint'),
  692. expire: self.channelExpire,
  693. port: SDPUtil.parse_sctpmap(sctpmap)[0]
  694. });
  695. }
  696. // now add transport
  697. remoteSDP.TransportToJingle(channel, change);
  698. change.up(); // end of channel/sctpconnection
  699. change.up(); // end of content
  700. }
  701. this.connection.sendIQ(change,
  702. function (res) {
  703. console.log('got result');
  704. },
  705. function (err) {
  706. console.log('got error');
  707. }
  708. );
  709. };
  710. // tell everyone about a new participants a=ssrc lines (isadd is true)
  711. // or a leaving participants a=ssrc lines
  712. ColibriFocus.prototype.sendSSRCUpdate = function (sdpMediaSsrcs, fromJid, isadd) {
  713. var self = this;
  714. this.peers.forEach(function (peerjid) {
  715. if (peerjid == fromJid) return;
  716. console.log('tell', peerjid, 'about ' + (isadd ? 'new' : 'removed') + ' ssrcs from', fromJid);
  717. if (!self.remotessrc[peerjid]) {
  718. // FIXME: this should only send to participants that are stable, i.e. who have sent a session-accept
  719. // possibly, this.remoteSSRC[session.peerjid] does not exist yet
  720. console.warn('do we really want to bother', peerjid, 'with updates yet?');
  721. }
  722. var peersess = self.connection.jingle.jid2session[peerjid];
  723. if(!peersess){
  724. console.warn('no session with peer: '+peerjid+' yet...');
  725. return;
  726. }
  727. self.sendSSRCUpdateIq(sdpMediaSsrcs, peersess.sid, peersess.initiator, peerjid, isadd);
  728. });
  729. };
  730. /**
  731. * Overrides SessionBase.addSource.
  732. *
  733. * @param elem proprietary 'add source' Jingle request(XML node).
  734. * @param fromJid JID of the participant to whom new ssrcs belong.
  735. */
  736. ColibriFocus.prototype.addSource = function (elem, fromJid) {
  737. var self = this;
  738. // FIXME: dirty waiting
  739. if (!this.peerconnection.localDescription)
  740. {
  741. console.warn("addSource - localDescription not ready yet")
  742. setTimeout(function()
  743. {
  744. self.addSource(elem, fromJid);
  745. },
  746. 200
  747. );
  748. return;
  749. }
  750. this.peerconnection.addSource(elem);
  751. var peerSsrc = this.remotessrc[fromJid];
  752. //console.log("On ADD", self.addssrc, peerSsrc);
  753. this.peerconnection.addssrc.forEach(function(val, idx){
  754. if(!peerSsrc[idx]){
  755. // add ssrc
  756. peerSsrc[idx] = val;
  757. } else {
  758. if(peerSsrc[idx].indexOf(val) == -1){
  759. peerSsrc[idx] = peerSsrc[idx]+val;
  760. }
  761. }
  762. });
  763. var oldRemoteSdp = new SDP(this.peerconnection.remoteDescription.sdp);
  764. this.modifySources(function(){
  765. // Notify other participants about added ssrc
  766. var remoteSDP = new SDP(self.peerconnection.remoteDescription.sdp);
  767. var newSSRCs = oldRemoteSdp.getNewMedia(remoteSDP);
  768. self.sendSSRCUpdate(newSSRCs, fromJid, true);
  769. });
  770. };
  771. /**
  772. * Overrides SessionBase.removeSource.
  773. *
  774. * @param elem proprietary 'remove source' Jingle request(XML node).
  775. * @param fromJid JID of the participant to whom removed ssrcs belong.
  776. */
  777. ColibriFocus.prototype.removeSource = function (elem, fromJid) {
  778. var self = this;
  779. // FIXME: dirty waiting
  780. if (!self.peerconnection.localDescription)
  781. {
  782. console.warn("removeSource - localDescription not ready yet");
  783. setTimeout(function()
  784. {
  785. self.removeSource(elem, fromJid);
  786. },
  787. 200
  788. );
  789. return;
  790. }
  791. this.peerconnection.removeSource(elem);
  792. var peerSsrc = this.remotessrc[fromJid];
  793. //console.log("On REMOVE", self.removessrc, peerSsrc);
  794. this.peerconnection.removessrc.forEach(function(val, idx){
  795. if(peerSsrc[idx]){
  796. // Remove ssrc
  797. peerSsrc[idx] = peerSsrc[idx].replace(val, '');
  798. }
  799. });
  800. var oldSDP = new SDP(self.peerconnection.remoteDescription.sdp);
  801. this.modifySources(function(){
  802. // Notify other participants about removed ssrc
  803. var remoteSDP = new SDP(self.peerconnection.remoteDescription.sdp);
  804. var removedSSRCs = remoteSDP.getNewMedia(oldSDP);
  805. self.sendSSRCUpdate(removedSSRCs, fromJid, false);
  806. });
  807. };
  808. ColibriFocus.prototype.setRemoteDescription = function (session, elem, desctype) {
  809. var participant = this.peers.indexOf(session.peerjid);
  810. console.log('Colibri.setRemoteDescription from', session.peerjid, participant);
  811. var remoteSDP = new SDP('');
  812. var channel;
  813. remoteSDP.fromJingle(elem);
  814. // ACT 1: change allocation on bridge
  815. this.updateChannel(remoteSDP, participant);
  816. // ACT 2: tell anyone else about the new SSRCs
  817. this.sendSSRCUpdate(remoteSDP.getMediaSsrcMap(), session.peerjid, true);
  818. // ACT 3: note the SSRCs
  819. this.remotessrc[session.peerjid] = [];
  820. for (channel = 0; channel < this.channels[participant].length; channel++) {
  821. //if (channel == 0) continue; FIXME: does not work as intended
  822. if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length)
  823. {
  824. this.remotessrc[session.peerjid][channel] =
  825. SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:')
  826. .join('\r\n') + '\r\n';
  827. }
  828. }
  829. // ACT 4: add new a=ssrc lines to local remotedescription
  830. for (channel = 0; channel < this.channels[participant].length; channel++) {
  831. //if (channel == 0) continue; FIXME: does not work as intended
  832. if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length) {
  833. this.peerconnection.enqueueAddSsrc(
  834. channel,
  835. SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n'
  836. );
  837. }
  838. }
  839. this.modifySources();
  840. };
  841. // relay ice candidates to bridge using trickle
  842. ColibriFocus.prototype.addIceCandidate = function (session, elem) {
  843. var self = this;
  844. var participant = this.peers.indexOf(session.peerjid);
  845. //console.log('change transport allocation for', this.confid, session.peerjid, participant);
  846. var change = $iq({to: this.bridgejid, type: 'set'});
  847. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  848. $(elem).each(function () {
  849. var name = $(this).attr('name');
  850. var channel = name == 'audio' ? 0 : 1; // FIXME: search mlineindex in localdesc
  851. if (name != 'audio' && name != 'video')
  852. channel = 2; // name == 'data'
  853. change.c('content', {name: name});
  854. if (name !== 'data')
  855. {
  856. change.c('channel', {
  857. id: $(self.channels[participant][channel]).attr('id'),
  858. endpoint: $(self.channels[participant][channel]).attr('endpoint'),
  859. expire: self.channelExpire
  860. });
  861. }
  862. else
  863. {
  864. change.c('sctpconnection', {
  865. endpoint: $(self.channels[participant][channel]).attr('endpoint'),
  866. expire: self.channelExpire
  867. });
  868. }
  869. $(this).find('>transport').each(function () {
  870. change.c('transport', {
  871. ufrag: $(this).attr('ufrag'),
  872. pwd: $(this).attr('pwd'),
  873. xmlns: $(this).attr('xmlns')
  874. });
  875. $(this).find('>candidate').each(function () {
  876. /* not yet
  877. if (this.getAttribute('protocol') == 'tcp' && this.getAttribute('port') == 0) {
  878. // chrome generates TCP candidates with port 0
  879. return;
  880. }
  881. */
  882. var line = SDPUtil.candidateFromJingle(this);
  883. change.c('candidate', SDPUtil.candidateToJingle(line)).up();
  884. });
  885. change.up(); // end of transport
  886. });
  887. change.up(); // end of channel/sctpconnection
  888. change.up(); // end of content
  889. });
  890. // FIXME: need to check if there is at least one candidate when filtering TCP ones
  891. this.connection.sendIQ(change,
  892. function (res) {
  893. console.log('got result');
  894. },
  895. function (err) {
  896. console.error('got error', err);
  897. }
  898. );
  899. };
  900. // send our own candidate to the bridge
  901. ColibriFocus.prototype.sendIceCandidate = function (candidate) {
  902. var self = this;
  903. //console.log('candidate', candidate);
  904. if (!candidate) {
  905. console.log('end of candidates');
  906. return;
  907. }
  908. if (this.drip_container.length === 0) {
  909. // start 20ms callout
  910. window.setTimeout(function () {
  911. if (self.drip_container.length === 0) return;
  912. self.sendIceCandidates(self.drip_container);
  913. self.drip_container = [];
  914. }, 20);
  915. }
  916. this.drip_container.push(candidate);
  917. };
  918. // sort and send multiple candidates
  919. ColibriFocus.prototype.sendIceCandidates = function (candidates) {
  920. var self = this;
  921. var mycands = $iq({to: this.bridgejid, type: 'set'});
  922. mycands.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  923. // FIXME: multi-candidate logic is taken from strophe.jingle, should be refactored there
  924. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  925. for (var mid = 0; mid < localSDP.media.length; mid++)
  926. {
  927. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  928. if (cands.length > 0)
  929. {
  930. var name = cands[0].sdpMid;
  931. mycands.c('content', {name: name });
  932. if (name !== 'data')
  933. {
  934. mycands.c('channel', {
  935. id: $(this.mychannel[cands[0].sdpMLineIndex]).attr('id'),
  936. endpoint: $(this.mychannel[cands[0].sdpMLineIndex]).attr('endpoint'),
  937. expire: self.channelExpire
  938. });
  939. }
  940. else
  941. {
  942. mycands.c('sctpconnection', {
  943. endpoint: $(this.mychannel[cands[0].sdpMLineIndex]).attr('endpoint'),
  944. port: $(this.mychannel[cands[0].sdpMLineIndex]).attr('port'),
  945. expire: self.channelExpire
  946. });
  947. }
  948. mycands.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
  949. for (var i = 0; i < cands.length; i++) {
  950. mycands.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  951. }
  952. mycands.up(); // transport
  953. mycands.up(); // channel / sctpconnection
  954. mycands.up(); // content
  955. }
  956. }
  957. console.log('send cands', candidates);
  958. this.connection.sendIQ(mycands,
  959. function (res) {
  960. console.log('got result');
  961. },
  962. function (err) {
  963. console.error('got error', err);
  964. }
  965. );
  966. };
  967. ColibriFocus.prototype.terminate = function (session, reason) {
  968. console.log('remote session terminated from', session.peerjid);
  969. var participant = this.peers.indexOf(session.peerjid);
  970. if (!this.remotessrc[session.peerjid] || participant == -1) {
  971. return;
  972. }
  973. var ssrcs = this.remotessrc[session.peerjid];
  974. for (var i = 0; i < ssrcs.length; i++) {
  975. this.peerconnection.enqueueRemoveSsrc(i, ssrcs[i]);
  976. }
  977. // remove from this.peers
  978. this.peers.splice(participant, 1);
  979. // expire channel on bridge
  980. var change = $iq({to: this.bridgejid, type: 'set'});
  981. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  982. for (var channel = 0; channel < this.channels[participant].length; channel++) {
  983. var name = channel === 0 ? 'audio' : 'video';
  984. if (channel == 2)
  985. name = 'data';
  986. change.c('content', {name: name});
  987. if (name !== 'data')
  988. {
  989. change.c('channel', {
  990. id: $(this.channels[participant][channel]).attr('id'),
  991. endpoint: $(this.channels[participant][channel]).attr('endpoint'),
  992. expire: '0'
  993. });
  994. }
  995. else
  996. {
  997. change.c('sctpconnection', {
  998. endpoint: $(this.channels[participant][channel]).attr('endpoint'),
  999. expire: '0'
  1000. });
  1001. }
  1002. change.up(); // end of channel/sctpconnection
  1003. change.up(); // end of content
  1004. }
  1005. this.connection.sendIQ(change,
  1006. function (res) {
  1007. console.log('got result');
  1008. },
  1009. function (err) {
  1010. console.error('got error', err);
  1011. }
  1012. );
  1013. // and remove from channels
  1014. this.channels.splice(participant, 1);
  1015. // tell everyone about the ssrcs to be removed
  1016. var sdp = new SDP('');
  1017. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  1018. var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
  1019. for (var j = 0; j < ssrcs.length; j++) {
  1020. sdp.media[j] = 'a=mid:' + contents[j] + '\r\n';
  1021. sdp.media[j] += ssrcs[j];
  1022. this.peerconnection.enqueueRemoveSsrc(j, ssrcs[j]);
  1023. }
  1024. this.sendSSRCUpdate(sdp.getMediaSsrcMap(), session.peerjid, false);
  1025. delete this.remotessrc[session.peerjid];
  1026. this.modifySources();
  1027. };
  1028. ColibriFocus.prototype.sendTerminate = function (session, reason, text) {
  1029. var term = $iq({to: session.peerjid, type: 'set'})
  1030. .c('jingle',
  1031. {xmlns: 'urn:xmpp:jingle:1',
  1032. action: 'session-terminate',
  1033. initiator: session.me,
  1034. sid: session.sid})
  1035. .c('reason')
  1036. .c(reason || 'success');
  1037. if (text) {
  1038. term.up().c('text').t(text);
  1039. }
  1040. this.connection.sendIQ(term,
  1041. function () {
  1042. if (!session)
  1043. return;
  1044. if (session.peerconnection) {
  1045. session.peerconnection.close();
  1046. session.peerconnection = null;
  1047. }
  1048. session.terminate();
  1049. var ack = {};
  1050. ack.source = 'terminate';
  1051. $(document).trigger('ack.jingle', [session.sid, ack]);
  1052. },
  1053. function (stanza) {
  1054. var error = ($(stanza).find('error').length) ? {
  1055. code: $(stanza).find('error').attr('code'),
  1056. reason: $(stanza).find('error :first')[0].tagName,
  1057. }:{};
  1058. $(document).trigger('ack.jingle', [self.sid, error]);
  1059. },
  1060. 10000);
  1061. if (this.statsinterval !== null) {
  1062. window.clearInterval(this.statsinterval);
  1063. this.statsinterval = null;
  1064. }
  1065. };