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

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