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.

colibri.focus.js 47KB

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