123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155 |
- /* colibri.js -- a COLIBRI focus
- * The colibri spec has been submitted to the XMPP Standards Foundation
- * for publications as a XMPP extensions:
- * http://xmpp.org/extensions/inbox/colibri.html
- *
- * colibri.js is a participating focus, i.e. the focus participates
- * in the conference. The conference itself can be ad-hoc, through a
- * MUC, through PubSub, etc.
- *
- * colibri.js relies heavily on the strophe.jingle library available
- * from https://github.com/ESTOS/strophe.jingle
- * and interoperates with the Jitsi videobridge available from
- * https://jitsi.org/Projects/JitsiVideobridge
- */
- /*
- Copyright (c) 2013 ESTOS GmbH
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- /* jshint -W117 */
-
- ColibriFocus.prototype = Object.create(SessionBase.prototype);
- function ColibriFocus(connection, bridgejid) {
-
- SessionBase.call(this, connection, Math.random().toString(36).substr(2, 12));
-
- this.bridgejid = bridgejid;
- this.peers = [];
- this.confid = null;
-
- /**
- * Local XMPP resource used to join the multi user chat.
- * @type {*}
- */
- this.myMucResource = Strophe.getResourceFromJid(connection.emuc.myroomjid);
-
- /**
- * Default channel expire value in seconds.
- * @type {number}
- */
- this.channelExpire = 60;
-
- // media types of the conference
- if (config.openSctp)
- {
- this.media = ['audio', 'video', 'data'];
- }
- else
- {
- this.media = ['audio', 'video'];
- }
-
- this.connection.jingle.sessions[this.sid] = this;
- this.mychannel = [];
- this.channels = [];
- this.remotessrc = {};
-
- // container for candidates from the focus
- // gathered before confid is known
- this.drip_container = [];
-
- // silly wait flag
- this.wait = true;
-
- this.recordingEnabled = false;
- }
-
- // creates a conferences with an initial set of peers
- ColibriFocus.prototype.makeConference = function (peers) {
- var self = this;
- if (this.confid !== null) {
- console.error('makeConference called twice? Ignoring...');
- // FIXME: just invite peers?
- return;
- }
- this.confid = 0; // !null
- this.peers = [];
- peers.forEach(function (peer) {
- self.peers.push(peer);
- self.channels.push([]);
- });
-
- this.peerconnection
- = new TraceablePeerConnection(
- this.connection.jingle.ice_config,
- this.connection.jingle.pc_constraints );
-
- if(this.connection.jingle.localAudio) {
- this.peerconnection.addStream(this.connection.jingle.localAudio);
- }
- if(this.connection.jingle.localVideo) {
- this.peerconnection.addStream(this.connection.jingle.localVideo);
- }
- this.peerconnection.oniceconnectionstatechange = function (event) {
- console.warn('ice connection state changed to', self.peerconnection.iceConnectionState);
- /*
- if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
- console.log('adding new remote SSRCs from iceconnectionstatechange');
- window.setTimeout(function() { self.modifySources(); }, 1000);
- }
- */
- };
- this.peerconnection.onsignalingstatechange = function (event) {
- console.warn(self.peerconnection.signalingState);
- /*
- if (self.peerconnection.signalingState == 'stable' && self.peerconnection.iceConnectionState == 'connected') {
- console.log('adding new remote SSRCs from signalingstatechange');
- window.setTimeout(function() { self.modifySources(); }, 1000);
- }
- */
- };
- this.peerconnection.onaddstream = function (event) {
- // search the jid associated with this stream
- Object.keys(self.remotessrc).forEach(function (jid) {
- if (self.remotessrc[jid].join('\r\n').indexOf('mslabel:' + event.stream.id) != -1) {
- event.peerjid = jid;
- }
- });
- $(document).trigger('remotestreamadded.jingle', [event, self.sid]);
- };
- this.peerconnection.onicecandidate = function (event) {
- //console.log('focus onicecandidate', self.confid, new Date().getTime(), event.candidate);
- if (!event.candidate) {
- console.log('end of candidates');
- return;
- }
- self.sendIceCandidate(event.candidate);
- };
- this._makeConference();
- /*
- this.peerconnection.createOffer(
- function (offer) {
- self.peerconnection.setLocalDescription(
- offer,
- function () {
- // success
- $(document).trigger('setLocalDescription.jingle', [self.sid]);
- // FIXME: could call _makeConference here and trickle candidates later
- self._makeConference();
- },
- function (error) {
- console.log('setLocalDescription failed', error);
- }
- );
- },
- function (error) {
- console.warn(error);
- }
- );
- */
- };
-
- // Sends a COLIBRI message which enables or disables (according to 'state') the
- // recording on the bridge.
- ColibriFocus.prototype.setRecording = function(state, token, callback) {
- var self = this;
- var elem = $iq({to: this.bridgejid, type: 'get'});
- elem.c('conference', {
- xmlns: 'http://jitsi.org/protocol/colibri',
- id: this.confid
- });
- elem.c('recording', {state: state, token: token});
- elem.up();
-
- this.connection.sendIQ(elem,
- function (result) {
- console.log('Set recording "', state, '". Result:', result);
- var recordingElem = $(result).find('>conference>recording');
- var newState = recordingElem.attr('state');
- if (newState == null){
- newState = false;
- }
-
- self.recordingEnabled = newState;
- callback(newState);
- },
- function (error) {
- console.warn(error);
- }
- );
- };
-
- ColibriFocus.prototype._makeConference = function () {
- var self = this;
- var elem = $iq({to: this.bridgejid, type: 'get'});
- elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri'});
-
- this.media.forEach(function (name) {
- var isData = name === 'data';
- var channel = isData ? 'sctpconnection' : 'channel';
-
- elem.c('content', {name: name});
-
- elem.c(channel, {
- initiator: 'true',
- expire: '15',
- endpoint: self.myMucResource
- });
- if (isData)
- elem.attrs({port: 5000});
- elem.up();// end of channel
-
- for (var j = 0; j < self.peers.length; j++) {
- elem.c(channel, {
- initiator: 'true',
- expire: '15',
- endpoint: self.peers[j].substr(1 + self.peers[j].lastIndexOf('/'))
- });
- if (isData)
- elem.attrs({port: 5000});
- elem.up(); // end of channel
- }
- elem.up(); // end of content
- });
- /*
- var localSDP = new SDP(this.peerconnection.localDescription.sdp);
- localSDP.media.forEach(function (media, channel) {
- var name = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
- elem.c('content', {name: name});
- elem.c('channel', {initiator: 'false', expire: '15'});
-
- // FIXME: should reuse code from .toJingle
- var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
- for (var j = 0; j < mline.fmt.length; j++) {
- var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
- elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
- elem.up();
- }
-
- localSDP.TransportToJingle(channel, elem);
-
- elem.up(); // end of channel
- for (j = 0; j < self.peers.length; j++) {
- elem.c('channel', {initiator: 'true', expire:'15' }).up();
- }
- elem.up(); // end of content
- });
- */
-
- this.connection.sendIQ(elem,
- function (result) {
- self.createdConference(result);
- },
- function (error) {
- console.warn(error);
- }
- );
- };
-
- // callback when a conference was created
- ColibriFocus.prototype.createdConference = function (result) {
- console.log('created a conference on the bridge');
- var self = this;
- var tmp;
-
- this.confid = $(result).find('>conference').attr('id');
- var remotecontents = $(result).find('>conference>content').get();
- var numparticipants = 0;
- for (var i = 0; i < remotecontents.length; i++)
- {
- var contentName = $(remotecontents[i]).attr('name');
- var channelName
- = contentName !== 'data' ? '>channel' : '>sctpconnection';
-
- tmp = $(remotecontents[i]).find(channelName).get();
- this.mychannel.push($(tmp.shift()));
- numparticipants = tmp.length;
- for (j = 0; j < tmp.length; j++) {
- if (this.channels[j] === undefined) {
- this.channels[j] = [];
- }
- this.channels[j].push(tmp[j]);
- }
- }
-
- console.log('remote channels', this.channels);
-
- // Notify that the focus has created the conference on the bridge
- $(document).trigger('conferenceCreated.jingle', [self]);
-
- var bridgeSDP = new SDP(
- 'v=0\r\n' +
- 'o=- 5151055458874951233 2 IN IP4 127.0.0.1\r\n' +
- 's=-\r\n' +
- 't=0 0\r\n' +
- /* Audio */
- 'm=audio 1 RTP/SAVPF 111 103 104 0 8 106 105 13 126\r\n' +
- 'c=IN IP4 0.0.0.0\r\n' +
- 'a=rtcp:1 IN IP4 0.0.0.0\r\n' +
- 'a=mid:audio\r\n' +
- 'a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\n' +
- 'a=sendrecv\r\n' +
- 'a=rtpmap:111 opus/48000/2\r\n' +
- 'a=fmtp:111 minptime=10\r\n' +
- 'a=rtpmap:103 ISAC/16000\r\n' +
- 'a=rtpmap:104 ISAC/32000\r\n' +
- 'a=rtpmap:0 PCMU/8000\r\n' +
- 'a=rtpmap:8 PCMA/8000\r\n' +
- 'a=rtpmap:106 CN/32000\r\n' +
- 'a=rtpmap:105 CN/16000\r\n' +
- 'a=rtpmap:13 CN/8000\r\n' +
- 'a=rtpmap:126 telephone-event/8000\r\n' +
- 'a=maxptime:60\r\n' +
- /* Video */
- 'm=video 1 RTP/SAVPF 100 116 117\r\n' +
- 'c=IN IP4 0.0.0.0\r\n' +
- 'a=rtcp:1 IN IP4 0.0.0.0\r\n' +
- 'a=mid:video\r\n' +
- 'a=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\n' +
- 'a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\n' +
- 'a=sendrecv\r\n' +
- 'a=rtpmap:100 VP8/90000\r\n' +
- 'a=rtcp-fb:100 ccm fir\r\n' +
- 'a=rtcp-fb:100 nack\r\n' +
- 'a=rtcp-fb:100 goog-remb\r\n' +
- 'a=rtpmap:116 red/90000\r\n' +
- 'a=rtpmap:117 ulpfec/90000\r\n' +
- /* Data SCTP */
- (config.openSctp ?
- 'm=application 1 DTLS/SCTP 5000\r\n' +
- 'c=IN IP4 0.0.0.0\r\n' +
- 'a=sctpmap:5000 webrtc-datachannel\r\n' +
- 'a=mid:data\r\n'
- : '')
- );
-
- bridgeSDP.media.length = this.mychannel.length;
- var channel;
- /*
- for (channel = 0; channel < bridgeSDP.media.length; channel++) {
- bridgeSDP.media[channel] = '';
- // unchanged lines
- bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'm=') + '\r\n';
- bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'c=') + '\r\n';
- if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:')) {
- bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=rtcp:') + '\r\n';
- }
- if (SDPUtil.find_line(localSDP.media[channel], 'a=mid:')) {
- bridgeSDP.media[channel] += SDPUtil.find_line(localSDP.media[channel], 'a=mid:') + '\r\n';
- }
- if (SDPUtil.find_line(localSDP.media[channel], 'a=sendrecv')) {
- bridgeSDP.media[channel] += 'a=sendrecv\r\n';
- }
- if (SDPUtil.find_line(localSDP.media[channel], 'a=extmap:')) {
- bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=extmap:').join('\r\n') + '\r\n';
- }
-
- // FIXME: should look at m-line and group the ids together
- if (SDPUtil.find_line(localSDP.media[channel], 'a=rtpmap:')) {
- bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtpmap:').join('\r\n') + '\r\n';
- }
- if (SDPUtil.find_line(localSDP.media[channel], 'a=fmtp:')) {
- bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=fmtp:').join('\r\n') + '\r\n';
- }
- if (SDPUtil.find_line(localSDP.media[channel], 'a=rtcp-fb:')) {
- bridgeSDP.media[channel] += SDPUtil.find_lines(localSDP.media[channel], 'a=rtcp-fb:').join('\r\n') + '\r\n';
- }
- // FIXME: changed lines -- a=sendrecv direction, a=setup direction
- }
- */
- for (channel = 0; channel < bridgeSDP.media.length; channel++) {
- // get the mixed ssrc
- tmp = $(this.mychannel[channel]).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
- // FIXME: check rtp-level-relay-type
-
- var isData = bridgeSDP.media[channel].indexOf('application') !== -1;
- if (!isData && tmp.length)
- {
- bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
- }
- else if (!isData)
- {
- // make chrome happy... '3735928559' == 0xDEADBEEF
- // FIXME: this currently appears as two streams, should be one
- bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
- bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
- }
-
- // FIXME: should take code from .fromJingle
- tmp = $(this.mychannel[channel]).find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
- if (tmp.length) {
- bridgeSDP.media[channel] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
- bridgeSDP.media[channel] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
- tmp.find('>candidate').each(function () {
- bridgeSDP.media[channel] += SDPUtil.candidateFromJingle(this);
- });
- tmp = tmp.find('>fingerprint');
- if (tmp.length) {
- bridgeSDP.media[channel] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
- bridgeSDP.media[channel] += 'a=setup:actpass\r\n'; // offer so always actpass
- }
- }
- }
- bridgeSDP.raw = bridgeSDP.session + bridgeSDP.media.join('');
-
- this.peerconnection.setRemoteDescription(
- new RTCSessionDescription({type: 'offer', sdp: bridgeSDP.raw}),
- function () {
- console.log('setRemoteDescription success');
- self.peerconnection.createAnswer(
- function (answer) {
- self.peerconnection.setLocalDescription(answer,
- function () {
- console.log('setLocalDescription succeded.');
- // make sure our presence is updated
- $(document).trigger('setLocalDescription.jingle', [self.sid]);
- var elem = $iq({to: self.bridgejid, type: 'get'});
- elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: self.confid});
- var localSDP = new SDP(self.peerconnection.localDescription.sdp);
- localSDP.media.forEach(function (media, channel) {
- var name = SDPUtil.parse_mid(SDPUtil.find_line(media, 'a=mid:'));
- elem.c('content', {name: name});
- var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
- if (name !== 'data')
- {
- elem.c('channel', {
- initiator: 'true',
- expire: self.channelExpire,
- id: self.mychannel[channel].attr('id'),
- endpoint: self.myMucResource
- });
-
- // FIXME: should reuse code from .toJingle
- for (var j = 0; j < mline.fmt.length; j++)
- {
- var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
- if (rtpmap)
- {
- elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
- elem.up();
- }
- }
- }
- else
- {
- var sctpmap = SDPUtil.find_line(media, 'a=sctpmap:' + mline.fmt[0]);
- var sctpPort = SDPUtil.parse_sctpmap(sctpmap)[0];
- elem.c("sctpconnection",
- {
- initiator: 'true',
- expire: self.channelExpire,
- endpoint: self.myMucResource,
- port: sctpPort
- }
- );
- }
-
- localSDP.TransportToJingle(channel, elem);
-
- elem.up(); // end of channel
- elem.up(); // end of content
- });
-
- self.connection.sendIQ(elem,
- function (result) {
- // ...
- },
- function (error) {
- console.error(
- "ERROR setLocalDescription succeded",
- error, elem);
- }
- );
-
- // now initiate sessions
- for (var i = 0; i < numparticipants; i++) {
- self.initiate(self.peers[i], true);
- }
-
- // Notify we've created the conference
- $(document).trigger(
- 'conferenceCreated.jingle', self);
- },
- function (error) {
- console.warn('setLocalDescription failed.', error);
- }
- );
- },
- function (error) {
- console.warn('createAnswer failed.', error);
- }
- );
- /*
- for (var i = 0; i < numparticipants; i++) {
- self.initiate(self.peers[i], true);
- }
- */
- },
- function (error) {
- console.log('setRemoteDescription failed.', error);
- }
- );
-
- };
-
- // send a session-initiate to a new participant
- ColibriFocus.prototype.initiate = function (peer, isInitiator) {
- var participant = this.peers.indexOf(peer);
- console.log('tell', peer, participant);
- var sdp;
- if (this.peerconnection !== null && this.peerconnection.signalingState == 'stable') {
- sdp = new SDP(this.peerconnection.remoteDescription.sdp);
- var localSDP = new SDP(this.peerconnection.localDescription.sdp);
- // throw away stuff we don't want
- // not needed with static offer
- sdp.removeSessionLines('a=group:');
- sdp.removeSessionLines('a=msid-semantic:'); // FIXME: not mapped over jingle anyway...
- for (var i = 0; i < sdp.media.length; i++) {
- sdp.removeMediaLines(i, 'a=rtcp-mux');
- sdp.removeMediaLines(i, 'a=ssrc:');
- sdp.removeMediaLines(i, 'a=crypto:');
- sdp.removeMediaLines(i, 'a=candidate:');
- sdp.removeMediaLines(i, 'a=ice-options:google-ice');
- sdp.removeMediaLines(i, 'a=ice-ufrag:');
- sdp.removeMediaLines(i, 'a=ice-pwd:');
- sdp.removeMediaLines(i, 'a=fingerprint:');
- sdp.removeMediaLines(i, 'a=setup:');
-
- if (1) { //i > 0) { // not for audio FIXME: does not work as intended
- // re-add all remote a=ssrcs
- for (var jid in this.remotessrc) {
- if (jid == peer) continue;
- sdp.media[i] += this.remotessrc[jid][i];
- }
- // and local a=ssrc lines
- sdp.media[i] += SDPUtil.find_lines(localSDP.media[i], 'a=ssrc').join('\r\n') + '\r\n';
- }
- }
- sdp.raw = sdp.session + sdp.media.join('');
- } else {
- console.error('can not initiate a new session without a stable peerconnection');
- return;
- }
-
- // add stuff we got from the bridge
- for (var j = 0; j < sdp.media.length; j++) {
- var chan = $(this.channels[participant][j]);
- console.log('channel id', chan.attr('id'));
-
- tmp = chan.find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
- if (tmp.length) {
- sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabel mixedlabela0' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabel' + '\r\n';
- }
- // No SSRCs for 'data', comes when j == 2
- else if (j < 2)
- {
- // make chrome happy... '3735928559' == 0xDEADBEEF
- sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabel mixedlabelv0' + '\r\n';
- sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabel' + '\r\n';
- }
-
- tmp = chan.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
- if (tmp.length) {
- if (tmp.attr('ufrag'))
- sdp.media[j] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
- if (tmp.attr('pwd'))
- sdp.media[j] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
- // and the candidates...
- tmp.find('>candidate').each(function () {
- sdp.media[j] += SDPUtil.candidateFromJingle(this);
- });
- tmp = tmp.find('>fingerprint');
- if (tmp.length) {
- sdp.media[j] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
- /*
- if (tmp.attr('direction')) {
- sdp.media[j] += 'a=setup:' + tmp.attr('direction') + '\r\n';
- }
- */
- sdp.media[j] += 'a=setup:actpass\r\n';
- }
- }
- }
- // make a new colibri session and configure it
- // FIXME: is it correct to use this.connection.jid when used in a MUC?
- var sess = new ColibriSession(this.connection.jid,
- Math.random().toString(36).substr(2, 12), // random string
- this.connection);
- sess.initiate(peer);
- sess.colibri = this;
- // We do not announce our audio per conference peer, so only video is set here
- sess.localVideo = this.connection.jingle.localVideo;
- sess.media_constraints = this.connection.jingle.media_constraints;
- sess.pc_constraints = this.connection.jingle.pc_constraints;
- sess.ice_config = this.connection.jingle.ice_config;
-
- this.connection.jingle.sessions[sess.sid] = sess;
- this.connection.jingle.jid2session[sess.peerjid] = sess;
-
- // send a session-initiate
- var init = $iq({to: peer, type: 'set'})
- .c('jingle',
- {xmlns: 'urn:xmpp:jingle:1',
- action: 'session-initiate',
- initiator: sess.me,
- sid: sess.sid
- }
- );
- sdp.toJingle(init, 'initiator');
- this.connection.sendIQ(init,
- function (res) {
- console.log('got result');
- },
- function (err) {
- console.log('got error');
- }
- );
- };
-
- // pull in a new participant into the conference
- ColibriFocus.prototype.addNewParticipant = function (peer) {
- var self = this;
- if (this.confid === 0 || !this.peerconnection.localDescription)
- {
- // bad state
- if (this.confid === 0)
- {
- console.error('confid does not exist yet, postponing', peer);
- }
- else
- {
- console.error('local description not ready yet, postponing', peer);
- }
- window.setTimeout(function () {
- self.addNewParticipant(peer);
- }, 250);
- return;
- }
- var index = this.channels.length;
- this.channels.push([]);
- this.peers.push(peer);
-
- var elem = $iq({to: this.bridgejid, type: 'get'});
- elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
- var localSDP = new SDP(this.peerconnection.localDescription.sdp);
- localSDP.media.forEach(function (media, channel) {
- var name = SDPUtil.parse_mid(SDPUtil.find_line(media, 'a=mid:'));
- elem.c('content', {name: name});
- if (name !== 'data')
- {
- elem.c('channel', {
- initiator: 'true',
- expire: self.channelExpire,
- endpoint: peer.substr(1 + peer.lastIndexOf('/'))
- });
- }
- else
- {
- elem.c('sctpconnection', {
- endpoint: peer.substr(1 + peer.lastIndexOf('/')),
- initiator: 'true',
- expire: self.channelExpire,
- port: 5000
- });
- }
- elem.up(); // end of channel/sctpconnection
- elem.up(); // end of content
- });
-
- this.connection.sendIQ(elem,
- function (result) {
- var contents = $(result).find('>conference>content').get();
- for (var i = 0; i < contents.length; i++) {
- var channelXml = $(contents[i]).find('>channel');
- if (channelXml.length)
- {
- tmp = channelXml.get();
- }
- else
- {
- tmp = $(contents[i]).find('>sctpconnection').get();
- }
- self.channels[index][i] = tmp[0];
- }
- self.initiate(peer, true);
- },
- function (error) {
- console.warn(error);
- }
- );
- };
-
- // update the channel description (payload-types + dtls fp) for a participant
- ColibriFocus.prototype.updateChannel = function (remoteSDP, participant) {
- console.log('change allocation for', this.confid);
- var self = this;
- var change = $iq({to: this.bridgejid, type: 'set'});
- change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
- for (channel = 0; channel < this.channels[participant].length; channel++)
- {
- var name = SDPUtil.parse_mid(SDPUtil.find_line(remoteSDP.media[channel], 'a=mid:'));
- change.c('content', {name: name});
- if (name !== 'data')
- {
- change.c('channel', {
- id: $(this.channels[participant][channel]).attr('id'),
- endpoint: $(this.channels[participant][channel]).attr('endpoint'),
- expire: self.channelExpire
- });
-
- var rtpmap = SDPUtil.find_lines(remoteSDP.media[channel], 'a=rtpmap:');
- rtpmap.forEach(function (val) {
- // TODO: too much copy-paste
- var rtpmap = SDPUtil.parse_rtpmap(val);
- change.c('payload-type', rtpmap);
- //
- // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
- /*
- if (SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id)) {
- tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id));
- for (var k = 0; k < tmp.length; k++) {
- change.c('parameter', tmp[k]).up();
- }
- }
- */
- change.up();
- });
- }
- else
- {
- var sctpmap = SDPUtil.find_line(remoteSDP.media[channel], 'a=sctpmap:');
- change.c('sctpconnection', {
- endpoint: $(this.channels[participant][channel]).attr('endpoint'),
- expire: self.channelExpire,
- port: SDPUtil.parse_sctpmap(sctpmap)[0]
- });
- }
- // now add transport
- remoteSDP.TransportToJingle(channel, change);
-
- change.up(); // end of channel/sctpconnection
- change.up(); // end of content
- }
- this.connection.sendIQ(change,
- function (res) {
- console.log('got result');
- },
- function (err) {
- console.log('got error');
- }
- );
- };
-
- // tell everyone about a new participants a=ssrc lines (isadd is true)
- // or a leaving participants a=ssrc lines
- ColibriFocus.prototype.sendSSRCUpdate = function (sdpMediaSsrcs, fromJid, isadd) {
- var self = this;
- this.peers.forEach(function (peerjid) {
- if (peerjid == fromJid) return;
- console.log('tell', peerjid, 'about ' + (isadd ? 'new' : 'removed') + ' ssrcs from', fromJid);
- if (!self.remotessrc[peerjid]) {
- // FIXME: this should only send to participants that are stable, i.e. who have sent a session-accept
- // possibly, this.remoteSSRC[session.peerjid] does not exist yet
- console.warn('do we really want to bother', peerjid, 'with updates yet?');
- }
- var peersess = self.connection.jingle.jid2session[peerjid];
- if(!peersess){
- console.warn('no session with peer: '+peerjid+' yet...');
- return;
- }
-
- self.sendSSRCUpdateIq(sdpMediaSsrcs, peersess.sid, peersess.initiator, peerjid, isadd);
- });
- };
-
- /**
- * Overrides SessionBase.addSource.
- *
- * @param elem proprietary 'add source' Jingle request(XML node).
- * @param fromJid JID of the participant to whom new ssrcs belong.
- */
- ColibriFocus.prototype.addSource = function (elem, fromJid) {
-
- var self = this;
- // FIXME: dirty waiting
- if (!this.peerconnection.localDescription)
- {
- console.warn("addSource - localDescription not ready yet")
- setTimeout(function()
- {
- self.addSource(elem, fromJid);
- },
- 200
- );
- return;
- }
-
- this.peerconnection.addSource(elem);
-
- var peerSsrc = this.remotessrc[fromJid];
- //console.log("On ADD", self.addssrc, peerSsrc);
- this.peerconnection.addssrc.forEach(function(val, idx){
- if(!peerSsrc[idx]){
- // add ssrc
- peerSsrc[idx] = val;
- } else {
- if(peerSsrc[idx].indexOf(val) == -1){
- peerSsrc[idx] = peerSsrc[idx]+val;
- }
- }
- });
-
- var oldRemoteSdp = new SDP(this.peerconnection.remoteDescription.sdp);
- this.modifySources(function(){
- // Notify other participants about added ssrc
- var remoteSDP = new SDP(self.peerconnection.remoteDescription.sdp);
- var newSSRCs = oldRemoteSdp.getNewMedia(remoteSDP);
- self.sendSSRCUpdate(newSSRCs, fromJid, true);
- });
- };
-
- /**
- * Overrides SessionBase.removeSource.
- *
- * @param elem proprietary 'remove source' Jingle request(XML node).
- * @param fromJid JID of the participant to whom removed ssrcs belong.
- */
- ColibriFocus.prototype.removeSource = function (elem, fromJid) {
-
- var self = this;
- // FIXME: dirty waiting
- if (!self.peerconnection.localDescription)
- {
- console.warn("removeSource - localDescription not ready yet");
- setTimeout(function()
- {
- self.removeSource(elem, fromJid);
- },
- 200
- );
- return;
- }
-
- this.peerconnection.removeSource(elem);
-
- var peerSsrc = this.remotessrc[fromJid];
- //console.log("On REMOVE", self.removessrc, peerSsrc);
- this.peerconnection.removessrc.forEach(function(val, idx){
- if(peerSsrc[idx]){
- // Remove ssrc
- peerSsrc[idx] = peerSsrc[idx].replace(val, '');
- }
- });
-
- var oldSDP = new SDP(self.peerconnection.remoteDescription.sdp);
- this.modifySources(function(){
- // Notify other participants about removed ssrc
- var remoteSDP = new SDP(self.peerconnection.remoteDescription.sdp);
- var removedSSRCs = remoteSDP.getNewMedia(oldSDP);
- self.sendSSRCUpdate(removedSSRCs, fromJid, false);
- });
- };
-
- ColibriFocus.prototype.setRemoteDescription = function (session, elem, desctype) {
- var participant = this.peers.indexOf(session.peerjid);
- console.log('Colibri.setRemoteDescription from', session.peerjid, participant);
- var remoteSDP = new SDP('');
- var channel;
- remoteSDP.fromJingle(elem);
-
- // ACT 1: change allocation on bridge
- this.updateChannel(remoteSDP, participant);
-
- // ACT 2: tell anyone else about the new SSRCs
- this.sendSSRCUpdate(remoteSDP.getMediaSsrcMap(), session.peerjid, true);
-
- // ACT 3: note the SSRCs
- this.remotessrc[session.peerjid] = [];
- for (channel = 0; channel < this.channels[participant].length; channel++) {
- //if (channel == 0) continue; FIXME: does not work as intended
- if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length)
- {
- this.remotessrc[session.peerjid][channel] =
- SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:')
- .join('\r\n') + '\r\n';
- }
- }
-
- // ACT 4: add new a=ssrc lines to local remotedescription
- for (channel = 0; channel < this.channels[participant].length; channel++) {
- //if (channel == 0) continue; FIXME: does not work as intended
- if (SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').length) {
- this.peerconnection.enqueueAddSsrc(
- channel,
- SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n'
- );
- }
- }
- this.modifySources();
- };
-
- // relay ice candidates to bridge using trickle
- ColibriFocus.prototype.addIceCandidate = function (session, elem) {
- var self = this;
- var participant = this.peers.indexOf(session.peerjid);
- //console.log('change transport allocation for', this.confid, session.peerjid, participant);
- var change = $iq({to: this.bridgejid, type: 'set'});
- change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
- $(elem).each(function () {
- var name = $(this).attr('name');
-
- var channel = name == 'audio' ? 0 : 1; // FIXME: search mlineindex in localdesc
- if (name != 'audio' && name != 'video')
- channel = 2; // name == 'data'
-
- change.c('content', {name: name});
- if (name !== 'data')
- {
- change.c('channel', {
- id: $(self.channels[participant][channel]).attr('id'),
- endpoint: $(self.channels[participant][channel]).attr('endpoint'),
- expire: self.channelExpire
- });
- }
- else
- {
- change.c('sctpconnection', {
- endpoint: $(self.channels[participant][channel]).attr('endpoint'),
- expire: self.channelExpire
- });
- }
- $(this).find('>transport').each(function () {
- change.c('transport', {
- ufrag: $(this).attr('ufrag'),
- pwd: $(this).attr('pwd'),
- xmlns: $(this).attr('xmlns')
- });
-
- $(this).find('>candidate').each(function () {
- /* not yet
- if (this.getAttribute('protocol') == 'tcp' && this.getAttribute('port') == 0) {
- // chrome generates TCP candidates with port 0
- return;
- }
- */
- var line = SDPUtil.candidateFromJingle(this);
- change.c('candidate', SDPUtil.candidateToJingle(line)).up();
- });
- change.up(); // end of transport
- });
- change.up(); // end of channel/sctpconnection
- change.up(); // end of content
- });
- // FIXME: need to check if there is at least one candidate when filtering TCP ones
- this.connection.sendIQ(change,
- function (res) {
- console.log('got result');
- },
- function (err) {
- console.error('got error', err);
- }
- );
- };
-
- // send our own candidate to the bridge
- ColibriFocus.prototype.sendIceCandidate = function (candidate) {
- var self = this;
- //console.log('candidate', candidate);
- if (!candidate) {
- console.log('end of candidates');
- return;
- }
- if (this.drip_container.length === 0) {
- // start 20ms callout
- window.setTimeout(function () {
- if (self.drip_container.length === 0) return;
- self.sendIceCandidates(self.drip_container);
- self.drip_container = [];
- }, 20);
- }
- this.drip_container.push(candidate);
- };
-
- // sort and send multiple candidates
- ColibriFocus.prototype.sendIceCandidates = function (candidates) {
- var self = this;
- var mycands = $iq({to: this.bridgejid, type: 'set'});
- mycands.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
- // FIXME: multi-candidate logic is taken from strophe.jingle, should be refactored there
- var localSDP = new SDP(this.peerconnection.localDescription.sdp);
- for (var mid = 0; mid < localSDP.media.length; mid++)
- {
- var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
- if (cands.length > 0)
- {
- var name = cands[0].sdpMid;
- mycands.c('content', {name: name });
- if (name !== 'data')
- {
- mycands.c('channel', {
- id: $(this.mychannel[cands[0].sdpMLineIndex]).attr('id'),
- endpoint: $(this.mychannel[cands[0].sdpMLineIndex]).attr('endpoint'),
- expire: self.channelExpire
- });
- }
- else
- {
- mycands.c('sctpconnection', {
- endpoint: $(this.mychannel[cands[0].sdpMLineIndex]).attr('endpoint'),
- port: $(this.mychannel[cands[0].sdpMLineIndex]).attr('port'),
- expire: self.channelExpire
- });
- }
- mycands.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
- for (var i = 0; i < cands.length; i++) {
- mycands.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
- }
- mycands.up(); // transport
- mycands.up(); // channel / sctpconnection
- mycands.up(); // content
- }
- }
- console.log('send cands', candidates);
- this.connection.sendIQ(mycands,
- function (res) {
- console.log('got result');
- },
- function (err) {
- console.error('got error', err);
- }
- );
- };
-
- ColibriFocus.prototype.terminate = function (session, reason) {
- console.log('remote session terminated from', session.peerjid);
- var participant = this.peers.indexOf(session.peerjid);
- if (!this.remotessrc[session.peerjid] || participant == -1) {
- return;
- }
- var ssrcs = this.remotessrc[session.peerjid];
- for (var i = 0; i < ssrcs.length; i++) {
- this.peerconnection.enqueueRemoveSsrc(i, ssrcs[i]);
- }
- // remove from this.peers
- this.peers.splice(participant, 1);
- // expire channel on bridge
- var change = $iq({to: this.bridgejid, type: 'set'});
- change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
- for (var channel = 0; channel < this.channels[participant].length; channel++) {
- var name = channel === 0 ? 'audio' : 'video';
- if (channel == 2)
- name = 'data';
- change.c('content', {name: name});
- if (name !== 'data')
- {
- change.c('channel', {
- id: $(this.channels[participant][channel]).attr('id'),
- endpoint: $(this.channels[participant][channel]).attr('endpoint'),
- expire: '0'
- });
- }
- else
- {
- change.c('sctpconnection', {
- endpoint: $(this.channels[participant][channel]).attr('endpoint'),
- expire: '0'
- });
- }
- change.up(); // end of channel/sctpconnection
- change.up(); // end of content
- }
- this.connection.sendIQ(change,
- function (res) {
- console.log('got result');
- },
- function (err) {
- console.error('got error', err);
- }
- );
- // and remove from channels
- this.channels.splice(participant, 1);
-
- // tell everyone about the ssrcs to be removed
- var sdp = new SDP('');
- var localSDP = new SDP(this.peerconnection.localDescription.sdp);
- var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
- for (var j = 0; j < ssrcs.length; j++) {
- sdp.media[j] = 'a=mid:' + contents[j] + '\r\n';
- sdp.media[j] += ssrcs[j];
- this.peerconnection.enqueueRemoveSsrc(j, ssrcs[j]);
- }
- this.sendSSRCUpdate(sdp.getMediaSsrcMap(), session.peerjid, false);
-
- delete this.remotessrc[session.peerjid];
- this.modifySources();
- };
-
- ColibriFocus.prototype.sendTerminate = function (session, reason, text) {
- var term = $iq({to: session.peerjid, type: 'set'})
- .c('jingle',
- {xmlns: 'urn:xmpp:jingle:1',
- action: 'session-terminate',
- initiator: session.me,
- sid: session.sid})
- .c('reason')
- .c(reason || 'success');
-
- if (text) {
- term.up().c('text').t(text);
- }
-
- this.connection.sendIQ(term,
- function () {
- if (!session)
- return;
-
- if (session.peerconnection) {
- session.peerconnection.close();
- session.peerconnection = null;
- }
-
- session.terminate();
- var ack = {};
- ack.source = 'terminate';
- $(document).trigger('ack.jingle', [session.sid, ack]);
- },
- function (stanza) {
- var error = ($(stanza).find('error').length) ? {
- code: $(stanza).find('error').attr('code'),
- reason: $(stanza).find('error :first')[0].tagName,
- }:{};
- $(document).trigger('ack.jingle', [self.sid, error]);
- },
- 10000);
- if (this.statsinterval !== null) {
- window.clearInterval(this.statsinterval);
- this.statsinterval = null;
- }
- };
|