您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

colibri.js 33KB

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