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

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