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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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);
  37. this.bridgejid = bridgejid;
  38. this.peers = [];
  39. this.confid = null;
  40. // media types of the conference
  41. this.media = ['audio', 'video'];
  42. this.sid = Math.random().toString(36).substr(2, 12);
  43. this.connection.jingle.sessions[this.sid] = this;
  44. this.mychannel = [];
  45. this.channels = [];
  46. this.remotessrc = {};
  47. // container for candidates from the focus
  48. // gathered before confid is known
  49. this.drip_container = [];
  50. // silly wait flag
  51. this.wait = true;
  52. }
  53. // creates a conferences with an initial set of peers
  54. ColibriFocus.prototype.makeConference = function (peers) {
  55. var self = this;
  56. if (this.confid !== null) {
  57. console.error('makeConference called twice? Ignoring...');
  58. // FIXME: just invite peers?
  59. return;
  60. }
  61. this.confid = 0; // !null
  62. this.peers = [];
  63. peers.forEach(function (peer) {
  64. self.peers.push(peer);
  65. self.channels.push([]);
  66. });
  67. if(connection.jingle.localAudio) {
  68. this.peerconnection.addStream(connection.jingle.localAudio);
  69. }
  70. if(connection.jingle.localVideo) {
  71. this.peerconnection.addStream(connection.jingle.localVideo);
  72. }
  73. this.peerconnection.oniceconnectionstatechange = function (event) {
  74. console.warn('ice connection state changed to', self.peerconnection.iceConnectionState);
  75. /*
  76. if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
  77. console.log('adding new remote SSRCs from iceconnectionstatechange');
  78. window.setTimeout(function() { self.modifySources(); }, 1000);
  79. }
  80. */
  81. };
  82. this.peerconnection.onsignalingstatechange = function (event) {
  83. console.warn(self.peerconnection.signalingState);
  84. /*
  85. if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
  86. console.log('adding new remote SSRCs from signalingstatechange');
  87. window.setTimeout(function() { self.modifySources(); }, 1000);
  88. }
  89. */
  90. };
  91. this.peerconnection.onaddstream = function (event) {
  92. // search the jid associated with this stream
  93. Object.keys(self.remotessrc).forEach(function (jid) {
  94. if (self.remotessrc[jid].join('\r\n').indexOf('mslabel:' + event.stream.id) != -1) {
  95. event.peerjid = jid;
  96. }
  97. });
  98. $(document).trigger('remotestreamadded.jingle', [event, self.sid]);
  99. };
  100. this.peerconnection.onicecandidate = function (event) {
  101. //console.log('focus onicecandidate', self.confid, new Date().getTime(), event.candidate);
  102. if (!event.candidate) {
  103. console.log('end of candidates');
  104. return;
  105. }
  106. self.sendIceCandidate(event.candidate);
  107. };
  108. this._makeConference();
  109. /*
  110. this.peerconnection.createOffer(
  111. function (offer) {
  112. self.peerconnection.setLocalDescription(
  113. offer,
  114. function () {
  115. // success
  116. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  117. // FIXME: could call _makeConference here and trickle candidates later
  118. self._makeConference();
  119. },
  120. function (error) {
  121. console.log('setLocalDescription failed', error);
  122. }
  123. );
  124. },
  125. function (error) {
  126. console.warn(error);
  127. }
  128. );
  129. */
  130. };
  131. ColibriFocus.prototype._makeConference = function () {
  132. var self = this;
  133. var elem = $iq({to: this.bridgejid, type: 'get'});
  134. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri'});
  135. this.media.forEach(function (name) {
  136. elem.c('content', {name: name});
  137. elem.c('channel', {initiator: 'true', expire: '15'}).up();
  138. for (var j = 0; j < self.peers.length; j++) {
  139. elem.c('channel', {initiator: 'true', expire:'15' }).up();
  140. }
  141. elem.up(); // end of content
  142. });
  143. /*
  144. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  145. localSDP.media.forEach(function (media, channel) {
  146. var name = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  147. elem.c('content', {name: name});
  148. elem.c('channel', {initiator: 'false', expire: '15'});
  149. // FIXME: should reuse code from .toJingle
  150. var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
  151. for (var j = 0; j < mline.fmt.length; j++) {
  152. var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
  153. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  154. elem.up();
  155. }
  156. localSDP.TransportToJingle(channel, elem);
  157. elem.up(); // end of channel
  158. for (j = 0; j < self.peers.length; j++) {
  159. elem.c('channel', {initiator: 'true', expire:'15' }).up();
  160. }
  161. elem.up(); // end of content
  162. });
  163. */
  164. this.connection.sendIQ(elem,
  165. function (result) {
  166. self.createdConference(result);
  167. },
  168. function (error) {
  169. console.warn(error);
  170. }
  171. );
  172. };
  173. // callback when a conference was created
  174. ColibriFocus.prototype.createdConference = function (result) {
  175. console.log('created a conference on the bridge');
  176. var self = this;
  177. var tmp;
  178. this.confid = $(result).find('>conference').attr('id');
  179. var remotecontents = $(result).find('>conference>content').get();
  180. var numparticipants = 0;
  181. for (var i = 0; i < remotecontents.length; i++) {
  182. tmp = $(remotecontents[i]).find('>channel').get();
  183. this.mychannel.push($(tmp.shift()));
  184. numparticipants = tmp.length;
  185. for (j = 0; j < tmp.length; j++) {
  186. if (this.channels[j] === undefined) {
  187. this.channels[j] = [];
  188. }
  189. this.channels[j].push(tmp[j]);
  190. }
  191. }
  192. console.log('remote channels', this.channels);
  193. var bridgeSDP = new SDP('v=0\r\no=- 5151055458874951233 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\nm=audio 1 RTP/SAVPF 111 103 104 0 8 106 105 13 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=mid:audio\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=sendrecv\r\na=rtpmap:111 opus/48000/2\r\na=fmtp:111 minptime=10\r\na=rtpmap:103 ISAC/16000\r\na=rtpmap:104 ISAC/32000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:106 CN/32000\r\na=rtpmap:105 CN/16000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:126 telephone-event/8000\r\na=maxptime:60\r\nm=video 1 RTP/SAVPF 100 116 117\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=mid:video\r\na=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=sendrecv\r\na=rtpmap:100 VP8/90000\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 goog-remb\r\na=rtpmap:116 red/90000\r\na=rtpmap:117 ulpfec/90000\r\n');
  194. bridgeSDP.media.length = this.mychannel.length;
  195. var channel;
  196. /*
  197. for (channel = 0; channel < bridgeSDP.media.length; channel++) {
  198. bridgeSDP.media[channel] = '';
  199. // unchanged lines
  200. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'm=') + '\r\n';
  201. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'c=') + '\r\n';
  202. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:')) {
  203. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:') + '\r\n';
  204. }
  205. if (SDPUtil.find_line(localSDP.media[channel], 'a=mid:')) {
  206. bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=mid:') + '\r\n';
  207. }
  208. if (SDPUtil.find_line(localSDP.media[channel], 'a=sendrecv')) {
  209. bridgeSDP.media[channel] += 'a=sendrecv\r\n';
  210. }
  211. if (SDPUtil.find_line(localSDP.media[channel], 'a=extmap:')) {
  212. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=extmap:').join('\r\n') + '\r\n';
  213. }
  214. // FIXME: should look at m-line and group the ids together
  215. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtpmap:')) {
  216. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtpmap:').join('\r\n') + '\r\n';
  217. }
  218. if (SDPUtil.find_line(localSDP.media[channel], 'a=fmtp:')) {
  219. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=fmtp:').join('\r\n') + '\r\n';
  220. }
  221. if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp-fb:')) {
  222. bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtcp-fb:').join('\r\n') + '\r\n';
  223. }
  224. // FIXME: changed lines -- a=sendrecv direction, a=setup direction
  225. }
  226. */
  227. for (channel = 0; channel < bridgeSDP.media.length; channel++) {
  228. // get the mixed ssrc
  229. tmp = $(this.mychannel[channel]).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  230. // FIXME: check rtp-level-relay-type
  231. if (tmp.length) {
  232. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
  233. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
  234. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
  235. bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  236. } else {
  237. // make chrome happy... '3735928559' == 0xDEADBEEF
  238. // FIXME: this currently appears as two streams, should be one
  239. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
  240. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
  241. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
  242. bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  243. }
  244. // FIXME: should take code from .fromJingle
  245. tmp = $(this.mychannel[channel]).find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  246. if (tmp.length) {
  247. bridgeSDP.media[channel] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  248. bridgeSDP.media[channel] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  249. tmp.find('>candidate').each(function () {
  250. bridgeSDP.media[channel] += SDPUtil.candidateFromJingle(this);
  251. });
  252. tmp = tmp.find('>fingerprint');
  253. if (tmp.length) {
  254. bridgeSDP.media[channel] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  255. bridgeSDP.media[channel] += 'a=setup:actpass\r\n'; // offer so always actpass
  256. }
  257. }
  258. }
  259. bridgeSDP.raw = bridgeSDP.session + bridgeSDP.media.join('');
  260. this.peerconnection.setRemoteDescription(
  261. new RTCSessionDescription({type: 'offer', sdp: bridgeSDP.raw}),
  262. function () {
  263. console.log('setRemoteDescription success');
  264. self.peerconnection.createAnswer(
  265. function (answer) {
  266. self.peerconnection.setLocalDescription(answer,
  267. function () {
  268. console.log('setLocalDescription succeded.');
  269. // make sure our presence is updated
  270. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  271. var elem = $iq({to: self.bridgejid, type: 'get'});
  272. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: self.confid});
  273. var localSDP = new SDP(self.peerconnection.localDescription.sdp);
  274. localSDP.media.forEach(function (media, channel) {
  275. var name = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  276. elem.c('content', {name: name});
  277. elem.c('channel', {
  278. initiator: 'true',
  279. expire: '15',
  280. id: self.mychannel[channel].attr('id')
  281. });
  282. // FIXME: should reuse code from .toJingle
  283. var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
  284. for (var j = 0; j < mline.fmt.length; j++) {
  285. var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
  286. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  287. elem.up();
  288. }
  289. localSDP.TransportToJingle(channel, elem);
  290. elem.up(); // end of channel
  291. elem.up(); // end of content
  292. });
  293. self.connection.sendIQ(elem,
  294. function (result) {
  295. // ...
  296. },
  297. function (error) {
  298. console.warn(error);
  299. }
  300. );
  301. // now initiate sessions
  302. for (var i = 0; i < numparticipants; i++) {
  303. self.initiate(self.peers[i], true);
  304. }
  305. },
  306. function (error) {
  307. console.warn('setLocalDescription failed.', error);
  308. }
  309. );
  310. },
  311. function (error) {
  312. console.warn('createAnswer failed.', error);
  313. }
  314. );
  315. /*
  316. for (var i = 0; i < numparticipants; i++) {
  317. self.initiate(self.peers[i], true);
  318. }
  319. */
  320. },
  321. function (error) {
  322. console.log('setRemoteDescription failed.', error);
  323. }
  324. );
  325. };
  326. // send a session-initiate to a new participant
  327. ColibriFocus.prototype.initiate = function (peer, isInitiator) {
  328. var participant = this.peers.indexOf(peer);
  329. console.log('tell', peer, participant);
  330. var sdp;
  331. if (this.peerconnection !== null && this.peerconnection.signalingState == 'stable') {
  332. sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  333. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  334. // throw away stuff we don't want
  335. // not needed with static offer
  336. sdp.removeSessionLines('a=group:');
  337. sdp.removeSessionLines('a=msid-semantic:'); // FIXME: not mapped over jingle anyway...
  338. for (var i = 0; i < sdp.media.length; i++) {
  339. sdp.removeMediaLines(i, 'a=rtcp-mux');
  340. sdp.removeMediaLines(i, 'a=ssrc:');
  341. sdp.removeMediaLines(i, 'a=crypto:');
  342. sdp.removeMediaLines(i, 'a=candidate:');
  343. sdp.removeMediaLines(i, 'a=ice-options:google-ice');
  344. sdp.removeMediaLines(i, 'a=ice-ufrag:');
  345. sdp.removeMediaLines(i, 'a=ice-pwd:');
  346. sdp.removeMediaLines(i, 'a=fingerprint:');
  347. sdp.removeMediaLines(i, 'a=setup:');
  348. if (1) { //i > 0) { // not for audio FIXME: does not work as intended
  349. // re-add all remote a=ssrcs
  350. for (var jid in this.remotessrc) {
  351. if (jid == peer) continue;
  352. sdp.media[i] += this.remotessrc[jid][i];
  353. }
  354. // and local a=ssrc lines
  355. sdp.media[i] += SDPUtil.find_lines(localSDP.media[i], 'a=ssrc').join('\r\n') + '\r\n';
  356. }
  357. }
  358. sdp.raw = sdp.session + sdp.media.join('');
  359. } else {
  360. console.error('can not initiate a new session without a stable peerconnection');
  361. return;
  362. }
  363. // add stuff we got from the bridge
  364. for (var j = 0; j < sdp.media.length; j++) {
  365. var chan = $(this.channels[participant][j]);
  366. console.log('channel id', chan.attr('id'));
  367. tmp = chan.find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  368. if (tmp.length) {
  369. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
  370. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
  371. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
  372. sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  373. } else {
  374. // make chrome happy... '3735928559' == 0xDEADBEEF
  375. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
  376. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
  377. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
  378. sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
  379. }
  380. tmp = chan.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  381. if (tmp.length) {
  382. if (tmp.attr('ufrag'))
  383. sdp.media[j] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  384. if (tmp.attr('pwd'))
  385. sdp.media[j] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  386. // and the candidates...
  387. tmp.find('>candidate').each(function () {
  388. sdp.media[j] += SDPUtil.candidateFromJingle(this);
  389. });
  390. tmp = tmp.find('>fingerprint');
  391. if (tmp.length) {
  392. sdp.media[j] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  393. /*
  394. if (tmp.attr('direction')) {
  395. sdp.media[j] += 'a=setup:' + tmp.attr('direction') + '\r\n';
  396. }
  397. */
  398. sdp.media[j] += 'a=setup:actpass\r\n';
  399. }
  400. }
  401. }
  402. // make a new colibri session and configure it
  403. // FIXME: is it correct to use this.connection.jid when used in a MUC?
  404. var sess = new ColibriSession(this.connection.jid,
  405. Math.random().toString(36).substr(2, 12), // random string
  406. this.connection);
  407. sess.initiate(peer);
  408. sess.colibri = this;
  409. // We do not announce our audio per conference peer, so only video is set here
  410. sess.localVideo = this.connection.jingle.localVideo;
  411. sess.media_constraints = this.connection.jingle.media_constraints;
  412. sess.pc_constraints = this.connection.jingle.pc_constraints;
  413. sess.ice_config = this.connection.jingle.ice_config;
  414. this.connection.jingle.sessions[sess.sid] = sess;
  415. this.connection.jingle.jid2session[sess.peerjid] = sess;
  416. // send a session-initiate
  417. var init = $iq({to: peer, type: 'set'})
  418. .c('jingle',
  419. {xmlns: 'urn:xmpp:jingle:1',
  420. action: 'session-initiate',
  421. initiator: sess.me,
  422. sid: sess.sid
  423. }
  424. );
  425. sdp.toJingle(init, 'initiator');
  426. this.connection.sendIQ(init,
  427. function (res) {
  428. console.log('got result');
  429. },
  430. function (err) {
  431. console.log('got error');
  432. }
  433. );
  434. };
  435. // pull in a new participant into the conference
  436. ColibriFocus.prototype.addNewParticipant = function (peer) {
  437. var self = this;
  438. if (this.confid === 0) {
  439. // bad state
  440. console.log('confid does not exist yet, postponing', peer);
  441. window.setTimeout(function () {
  442. self.addNewParticipant(peer);
  443. }, 250);
  444. return;
  445. }
  446. var index = this.channels.length;
  447. this.channels.push([]);
  448. this.peers.push(peer);
  449. var elem = $iq({to: this.bridgejid, type: 'get'});
  450. elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  451. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  452. localSDP.media.forEach(function (media, channel) {
  453. var name = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  454. elem.c('content', {name: name});
  455. elem.c('channel', {initiator: 'true', expire:'15'});
  456. elem.up(); // end of channel
  457. elem.up(); // end of content
  458. });
  459. this.connection.sendIQ(elem,
  460. function (result) {
  461. var contents = $(result).find('>conference>content').get();
  462. for (var i = 0; i < contents.length; i++) {
  463. tmp = $(contents[i]).find('>channel').get();
  464. self.channels[index][i] = tmp[0];
  465. }
  466. self.initiate(peer, true);
  467. },
  468. function (error) {
  469. console.warn(error);
  470. }
  471. );
  472. };
  473. // update the channel description (payload-types + dtls fp) for a participant
  474. ColibriFocus.prototype.updateChannel = function (remoteSDP, participant) {
  475. console.log('change allocation for', this.confid);
  476. var change = $iq({to: this.bridgejid, type: 'set'});
  477. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  478. for (channel = 0; channel < this.channels[participant].length; channel++) {
  479. change.c('content', {name: channel === 0 ? 'audio' : 'video'});
  480. change.c('channel', {id: $(this.channels[participant][channel]).attr('id')});
  481. var rtpmap = SDPUtil.find_lines(remoteSDP.media[channel], 'a=rtpmap:');
  482. rtpmap.forEach(function (val) {
  483. // TODO: too much copy-paste
  484. var rtpmap = SDPUtil.parse_rtpmap(val);
  485. change.c('payload-type', rtpmap);
  486. //
  487. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
  488. /*
  489. if (SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id)) {
  490. tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id));
  491. for (var k = 0; k < tmp.length; k++) {
  492. change.c('parameter', tmp[k]).up();
  493. }
  494. }
  495. */
  496. change.up();
  497. });
  498. // now add transport
  499. remoteSDP.TransportToJingle(channel, change);
  500. change.up(); // end of channel
  501. change.up(); // end of content
  502. }
  503. this.connection.sendIQ(change,
  504. function (res) {
  505. console.log('got result');
  506. },
  507. function (err) {
  508. console.log('got error');
  509. }
  510. );
  511. };
  512. // tell everyone about a new participants a=ssrc lines (isadd is true)
  513. // or a leaving participants a=ssrc lines
  514. // FIXME: should not take an SDP, but rather the a=ssrc lines and probably a=mid
  515. ColibriFocus.prototype.sendSSRCUpdate = function (sdp, jid, isadd) {
  516. var self = this;
  517. this.peers.forEach(function (peerjid) {
  518. if (peerjid == jid) return;
  519. console.log('tell', peerjid, 'about ' + (isadd ? 'new' : 'removed') + ' ssrcs from', jid);
  520. if (!self.remotessrc[peerjid]) {
  521. // FIXME: this should only send to participants that are stable, i.e. who have sent a session-accept
  522. // possibly, this.remoteSSRC[session.peerjid] does not exist yet
  523. console.warn('do we really want to bother', peerjid, 'with updates yet?');
  524. }
  525. var channel;
  526. var peersess = self.connection.jingle.jid2session[peerjid];
  527. var modify = $iq({to: peerjid, type: 'set'})
  528. .c('jingle', {
  529. xmlns: 'urn:xmpp:jingle:1',
  530. action: isadd ? 'addsource' : 'removesource',
  531. initiator: peersess.initiator,
  532. sid: peersess.sid
  533. }
  534. );
  535. // FIXME: only announce video ssrcs since we mix audio and dont need
  536. // the audio ssrcs therefore
  537. var modified = false;
  538. for (channel = 0; channel < sdp.media.length; channel++) {
  539. modified = true;
  540. tmp = SDPUtil.find_lines(sdp.media[channel], 'a=ssrc:');
  541. modify.c('content', {name: SDPUtil.parse_mid(SDPUtil.find_line(sdp.media[channel], 'a=mid:'))});
  542. modify.c('source', { xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  543. // FIXME: not completly sure this operates on blocks and / or handles different ssrcs correctly
  544. tmp.forEach(function (line) {
  545. var idx = line.indexOf(' ');
  546. var linessrc = line.substr(0, idx).substr(7);
  547. modify.attrs({ssrc: linessrc});
  548. var kv = line.substr(idx + 1);
  549. modify.c('parameter');
  550. if (kv.indexOf(':') == -1) {
  551. modify.attrs({ name: kv });
  552. } else {
  553. modify.attrs({ name: kv.split(':', 2)[0] });
  554. modify.attrs({ value: kv.split(':', 2)[1] });
  555. }
  556. modify.up();
  557. });
  558. modify.up(); // end of source
  559. modify.up(); // end of content
  560. }
  561. if (modified) {
  562. self.connection.sendIQ(modify,
  563. function (res) {
  564. console.warn('got modify result');
  565. },
  566. function (err) {
  567. console.warn('got modify error', err);
  568. }
  569. );
  570. } else {
  571. console.log('modification not necessary');
  572. }
  573. });
  574. };
  575. ColibriFocus.prototype.setRemoteDescription = function (session, elem, desctype) {
  576. var participant = this.peers.indexOf(session.peerjid);
  577. console.log('Colibri.setRemoteDescription from', session.peerjid, participant);
  578. var remoteSDP = new SDP('');
  579. var channel;
  580. remoteSDP.fromJingle(elem);
  581. // ACT 1: change allocation on bridge
  582. this.updateChannel(remoteSDP, participant);
  583. // ACT 2: tell anyone else about the new SSRCs
  584. this.sendSSRCUpdate(remoteSDP, session.peerjid, true);
  585. // ACT 3: note the SSRCs
  586. this.remotessrc[session.peerjid] = [];
  587. for (channel = 0; channel < this.channels[participant].length; channel++) {
  588. //if (channel == 0) continue; FIXME: does not work as intended
  589. if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length) {
  590. this.remotessrc[session.peerjid][channel] = SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n';
  591. }
  592. }
  593. // ACT 4: add new a=ssrc lines to local remotedescription
  594. for (channel = 0; channel < this.channels[participant].length; channel++) {
  595. //if (channel == 0) continue; FIXME: does not work as intended
  596. if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length) {
  597. this.peerconnection.enqueueAddSsrc(
  598. channel,
  599. SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n'
  600. );
  601. }
  602. }
  603. this.modifySources();
  604. };
  605. // relay ice candidates to bridge using trickle
  606. ColibriFocus.prototype.addIceCandidate = function (session, elem) {
  607. var self = this;
  608. var participant = this.peers.indexOf(session.peerjid);
  609. //console.log('change transport allocation for', this.confid, session.peerjid, participant);
  610. var change = $iq({to: this.bridgejid, type: 'set'});
  611. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  612. $(elem).each(function () {
  613. var name = $(this).attr('name');
  614. var channel = name == 'audio' ? 0 : 1; // FIXME: search mlineindex in localdesc
  615. change.c('content', {name: name});
  616. change.c('channel', {id: $(self.channels[participant][channel]).attr('id')});
  617. $(this).find('>transport').each(function () {
  618. change.c('transport', {
  619. ufrag: $(this).attr('ufrag'),
  620. pwd: $(this).attr('pwd'),
  621. xmlns: $(this).attr('xmlns')
  622. });
  623. $(this).find('>candidate').each(function () {
  624. /* not yet
  625. if (this.getAttribute('protocol') == 'tcp' && this.getAttribute('port') == 0) {
  626. // chrome generates TCP candidates with port 0
  627. return;
  628. }
  629. */
  630. var line = SDPUtil.candidateFromJingle(this);
  631. change.c('candidate', SDPUtil.candidateToJingle(line)).up();
  632. });
  633. change.up(); // end of transport
  634. });
  635. change.up(); // end of channel
  636. change.up(); // end of content
  637. });
  638. // FIXME: need to check if there is at least one candidate when filtering TCP ones
  639. this.connection.sendIQ(change,
  640. function (res) {
  641. console.log('got result');
  642. },
  643. function (err) {
  644. console.error('got error', err);
  645. }
  646. );
  647. };
  648. // send our own candidate to the bridge
  649. ColibriFocus.prototype.sendIceCandidate = function (candidate) {
  650. var self = this;
  651. //console.log('candidate', candidate);
  652. if (!candidate) {
  653. console.log('end of candidates');
  654. return;
  655. }
  656. if (this.drip_container.length === 0) {
  657. // start 20ms callout
  658. window.setTimeout(function () {
  659. if (self.drip_container.length === 0) return;
  660. self.sendIceCandidates(self.drip_container);
  661. self.drip_container = [];
  662. }, 20);
  663. }
  664. this.drip_container.push(candidate);
  665. };
  666. // sort and send multiple candidates
  667. ColibriFocus.prototype.sendIceCandidates = function (candidates) {
  668. var self = this;
  669. var mycands = $iq({to: this.bridgejid, type: 'set'});
  670. mycands.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  671. // FIXME: multi-candidate logic is taken from strophe.jingle, should be refactored there
  672. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  673. for (var mid = 0; mid < localSDP.media.length; mid++) {
  674. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  675. if (cands.length > 0) {
  676. mycands.c('content', {name: cands[0].sdpMid });
  677. mycands.c('channel', {id: $(this.mychannel[cands[0].sdpMLineIndex]).attr('id')});
  678. mycands.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
  679. for (var i = 0; i < cands.length; i++) {
  680. mycands.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  681. }
  682. mycands.up(); // transport
  683. mycands.up(); // channel
  684. mycands.up(); // content
  685. }
  686. }
  687. console.log('send cands', candidates);
  688. this.connection.sendIQ(mycands,
  689. function (res) {
  690. console.log('got result');
  691. },
  692. function (err) {
  693. console.error('got error', err);
  694. }
  695. );
  696. };
  697. ColibriFocus.prototype.terminate = function (session, reason) {
  698. console.log('remote session terminated from', session.peerjid);
  699. var participant = this.peers.indexOf(session.peerjid);
  700. if (!this.remotessrc[session.peerjid] || participant == -1) {
  701. return;
  702. }
  703. var ssrcs = this.remotessrc[session.peerjid];
  704. for (var i = 0; i < ssrcs.length; i++) {
  705. this.peerconnection.enqueueRemoveSsrc(i, ssrcs[i]);
  706. }
  707. // remove from this.peers
  708. this.peers.splice(participant, 1);
  709. // expire channel on bridge
  710. var change = $iq({to: this.bridgejid, type: 'set'});
  711. change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
  712. for (var channel = 0; channel < this.channels[participant].length; channel++) {
  713. change.c('content', {name: channel === 0 ? 'audio' : 'video'});
  714. change.c('channel', {id: $(this.channels[participant][channel]).attr('id'), expire: '0'});
  715. change.up(); // end of channel
  716. change.up(); // end of content
  717. }
  718. this.connection.sendIQ(change,
  719. function (res) {
  720. console.log('got result');
  721. },
  722. function (err) {
  723. console.error('got error', err);
  724. }
  725. );
  726. // and remove from channels
  727. this.channels.splice(participant, 1);
  728. // tell everyone about the ssrcs to be removed
  729. var sdp = new SDP('');
  730. var localSDP = new SDP(this.peerconnection.localDescription.sdp);
  731. var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
  732. for (var j = 0; j < ssrcs.length; j++) {
  733. sdp.media[j] = 'a=mid:' + contents[j] + '\r\n';
  734. sdp.media[j] += ssrcs[j];
  735. this.peerconnection.enqueueRemoveSsrc(j, ssrcs[j]);
  736. }
  737. this.sendSSRCUpdate(sdp, session.peerjid, false);
  738. delete this.remotessrc[session.peerjid];
  739. this.modifySources();
  740. };