2015-01-19 09:20:00 +00:00
|
|
|
var Moderator = require("./moderator");
|
|
|
|
var EventEmitter = require("events");
|
|
|
|
var Recording = require("./recording");
|
|
|
|
var SDP = require("./SDP");
|
2015-01-21 15:13:13 +00:00
|
|
|
var Pako = require("pako");
|
2015-01-28 14:35:22 +00:00
|
|
|
var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
|
|
|
|
var UIEvents = require("../../service/UI/UIEvents");
|
|
|
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
2015-01-19 09:20:00 +00:00
|
|
|
|
|
|
|
var eventEmitter = new EventEmitter();
|
|
|
|
var connection = null;
|
|
|
|
var authenticatedUser = false;
|
|
|
|
|
|
|
|
function connect(jid, password, uiCredentials) {
|
|
|
|
var bosh
|
2015-01-29 09:43:40 +00:00
|
|
|
= (uiCredentials && uiCredentials.bosh? uiCredentials.bosh : null)
|
|
|
|
|| config.bosh || '/http-bind';
|
2015-01-19 09:20:00 +00:00
|
|
|
connection = new Strophe.Connection(bosh);
|
|
|
|
Moderator.setConnection(connection);
|
|
|
|
|
2015-01-29 09:43:40 +00:00
|
|
|
if(uiCredentials) {
|
|
|
|
var email = uiCredentials.email;
|
|
|
|
var displayName = uiCredentials.displayName;
|
|
|
|
if (email) {
|
|
|
|
connection.emuc.addEmailToPresence(email);
|
|
|
|
} else {
|
|
|
|
connection.emuc.addUserIdToPresence(uiCredentials.uid);
|
|
|
|
}
|
|
|
|
if (displayName) {
|
|
|
|
connection.emuc.addDisplayNameToPresence(displayName);
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connection.disco) {
|
|
|
|
// for chrome, add multistream cap
|
|
|
|
}
|
2015-01-28 14:35:22 +00:00
|
|
|
connection.jingle.pc_constraints = APP.RTC.getPCConstraints();
|
2015-01-19 09:20:00 +00:00
|
|
|
if (config.useIPv6) {
|
|
|
|
// https://code.google.com/p/webrtc/issues/detail?id=2828
|
|
|
|
if (!connection.jingle.pc_constraints.optional)
|
|
|
|
connection.jingle.pc_constraints.optional = [];
|
|
|
|
connection.jingle.pc_constraints.optional.push({googIPv6: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!password)
|
|
|
|
password = uiCredentials.password;
|
|
|
|
|
|
|
|
var anonymousConnectionFailed = false;
|
|
|
|
connection.connect(jid, password, function (status, msg) {
|
|
|
|
console.log('Strophe status changed to',
|
|
|
|
Strophe.getStatusString(status));
|
|
|
|
if (status === Strophe.Status.CONNECTED) {
|
|
|
|
if (config.useStunTurn) {
|
|
|
|
connection.jingle.getStunAndTurnCredentials();
|
|
|
|
}
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.disableConnect();
|
2015-01-19 09:20:00 +00:00
|
|
|
|
|
|
|
console.info("My Jabber ID: " + connection.jid);
|
|
|
|
|
|
|
|
if(password)
|
|
|
|
authenticatedUser = true;
|
|
|
|
maybeDoJoin();
|
|
|
|
} else if (status === Strophe.Status.CONNFAIL) {
|
|
|
|
if(msg === 'x-strophe-bad-non-anon-jid') {
|
|
|
|
anonymousConnectionFailed = true;
|
|
|
|
}
|
|
|
|
} else if (status === Strophe.Status.DISCONNECTED) {
|
|
|
|
if(anonymousConnectionFailed) {
|
|
|
|
// prompt user for username and password
|
|
|
|
XMPP.promptLogin();
|
|
|
|
}
|
|
|
|
} else if (status === Strophe.Status.AUTHFAIL) {
|
|
|
|
// wrong password or username, prompt user
|
|
|
|
XMPP.promptLogin();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function maybeDoJoin() {
|
|
|
|
if (connection && connection.connected &&
|
|
|
|
Strophe.getResourceFromJid(connection.jid)
|
2015-01-28 14:35:22 +00:00
|
|
|
&& (APP.RTC.localAudio || APP.RTC.localVideo)) {
|
2015-01-19 09:20:00 +00:00
|
|
|
// .connected is true while connecting?
|
|
|
|
doJoin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doJoin() {
|
2015-01-28 14:35:22 +00:00
|
|
|
var roomName = APP.UI.generateRoomName();
|
2015-01-19 09:20:00 +00:00
|
|
|
|
|
|
|
Moderator.allocateConferenceFocus(
|
2015-01-28 14:35:22 +00:00
|
|
|
roomName, APP.UI.checkForNicknameAndJoin);
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initStrophePlugins()
|
|
|
|
{
|
|
|
|
require("./strophe.emuc")(XMPP, eventEmitter);
|
2015-01-24 14:28:02 +00:00
|
|
|
require("./strophe.jingle")(XMPP, eventEmitter);
|
2015-01-19 09:20:00 +00:00
|
|
|
require("./strophe.moderate")(XMPP);
|
|
|
|
require("./strophe.util")();
|
|
|
|
require("./strophe.rayo")();
|
|
|
|
require("./strophe.logger")();
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerListeners() {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.addStreamListener(maybeDoJoin,
|
2015-01-19 09:20:00 +00:00
|
|
|
StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.addListener(UIEvents.NICKNAME_CHANGED, function (nickname) {
|
2015-01-22 16:02:37 +00:00
|
|
|
XMPP.addToPresence("displayName", nickname);
|
2015-01-24 14:28:02 +00:00
|
|
|
});
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupEvents() {
|
|
|
|
$(window).bind('beforeunload', function () {
|
|
|
|
if (connection && connection.connected) {
|
|
|
|
// ensure signout
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: config.bosh,
|
|
|
|
async: false,
|
|
|
|
cache: false,
|
|
|
|
contentType: 'application/xml',
|
|
|
|
data: "<body rid='" + (connection.rid || connection._proto.rid)
|
|
|
|
+ "' xmlns='http://jabber.org/protocol/httpbind' sid='"
|
|
|
|
+ (connection.sid || connection._proto.sid)
|
|
|
|
+ "' type='terminate'>" +
|
|
|
|
"<presence xmlns='jabber:client' type='unavailable'/>" +
|
|
|
|
"</body>",
|
|
|
|
success: function (data) {
|
|
|
|
console.log('signed out');
|
|
|
|
console.log(data);
|
|
|
|
},
|
|
|
|
error: function (XMLHttpRequest, textStatus, errorThrown) {
|
|
|
|
console.log('signout error',
|
|
|
|
textStatus + ' (' + errorThrown + ')');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
XMPP.disposeConference(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var XMPP = {
|
|
|
|
sessionTerminated: false,
|
|
|
|
/**
|
|
|
|
* Remembers if we were muted by the focus.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
forceMuted: false,
|
|
|
|
start: function (uiCredentials) {
|
|
|
|
setupEvents();
|
|
|
|
initStrophePlugins();
|
|
|
|
registerListeners();
|
2015-01-29 09:09:09 +00:00
|
|
|
Moderator.init(this, eventEmitter);
|
2015-01-19 10:00:30 +00:00
|
|
|
var configDomain = config.hosts.anonymousdomain || config.hosts.domain;
|
|
|
|
// Force authenticated domain if room is appended with '?login=true'
|
|
|
|
if (config.hosts.anonymousdomain &&
|
|
|
|
window.location.search.indexOf("login=true") !== -1) {
|
|
|
|
configDomain = config.hosts.domain;
|
|
|
|
}
|
|
|
|
var jid = uiCredentials.jid || configDomain || window.location.hostname;
|
2015-01-19 09:20:00 +00:00
|
|
|
connect(jid, null, uiCredentials);
|
|
|
|
},
|
|
|
|
promptLogin: function () {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.showLoginPopup(connect);
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2015-01-21 17:01:58 +00:00
|
|
|
joinRoom: function(roomName, useNicks, nick)
|
2015-01-19 09:20:00 +00:00
|
|
|
{
|
|
|
|
var roomjid;
|
|
|
|
roomjid = roomName;
|
|
|
|
|
|
|
|
if (useNicks) {
|
|
|
|
if (nick) {
|
|
|
|
roomjid += '/' + nick;
|
|
|
|
} else {
|
|
|
|
roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var tmpJid = Strophe.getNodeFromJid(connection.jid);
|
|
|
|
|
|
|
|
if(!authenticatedUser)
|
|
|
|
tmpJid = tmpJid.substr(0, 8);
|
|
|
|
|
|
|
|
roomjid += '/' + tmpJid;
|
|
|
|
}
|
|
|
|
connection.emuc.doJoin(roomjid);
|
|
|
|
},
|
|
|
|
myJid: function () {
|
|
|
|
if(!connection)
|
|
|
|
return null;
|
|
|
|
return connection.emuc.myroomjid;
|
|
|
|
},
|
|
|
|
myResource: function () {
|
|
|
|
if(!connection || ! connection.emuc.myroomjid)
|
|
|
|
return null;
|
|
|
|
return Strophe.getResourceFromJid(connection.emuc.myroomjid);
|
|
|
|
},
|
|
|
|
disposeConference: function (onUnload) {
|
|
|
|
eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE, onUnload);
|
2015-01-19 16:54:41 +00:00
|
|
|
var handler = connection.jingle.activecall;
|
2015-01-19 09:20:00 +00:00
|
|
|
if (handler && handler.peerconnection) {
|
|
|
|
// FIXME: probably removing streams is not required and close() should
|
|
|
|
// be enough
|
2015-01-28 14:35:22 +00:00
|
|
|
if (APP.RTC.localAudio) {
|
|
|
|
handler.peerconnection.removeStream(APP.RTC.localAudio.getOriginalStream(), onUnload);
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
2015-01-28 14:35:22 +00:00
|
|
|
if (APP.RTC.localVideo) {
|
|
|
|
handler.peerconnection.removeStream(APP.RTC.localVideo.getOriginalStream(), onUnload);
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
handler.peerconnection.close();
|
|
|
|
}
|
2015-01-19 16:54:41 +00:00
|
|
|
connection.jingle.activecall = null;
|
2015-01-19 09:20:00 +00:00
|
|
|
if(!onUnload)
|
|
|
|
{
|
|
|
|
this.sessionTerminated = true;
|
|
|
|
connection.emuc.doLeave();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addListener: function(type, listener)
|
|
|
|
{
|
|
|
|
eventEmitter.on(type, listener);
|
|
|
|
},
|
|
|
|
removeListener: function (type, listener) {
|
|
|
|
eventEmitter.removeListener(type, listener);
|
|
|
|
},
|
|
|
|
allocateConferenceFocus: function(roomName, callback) {
|
|
|
|
Moderator.allocateConferenceFocus(roomName, callback);
|
|
|
|
},
|
|
|
|
isModerator: function () {
|
|
|
|
return Moderator.isModerator();
|
|
|
|
},
|
|
|
|
isSipGatewayEnabled: function () {
|
|
|
|
return Moderator.isSipGatewayEnabled();
|
|
|
|
},
|
|
|
|
isExternalAuthEnabled: function () {
|
|
|
|
return Moderator.isExternalAuthEnabled();
|
|
|
|
},
|
|
|
|
switchStreams: function (stream, oldStream, callback) {
|
2015-01-19 16:54:41 +00:00
|
|
|
if (connection && connection.jingle.activecall) {
|
2015-01-19 09:20:00 +00:00
|
|
|
// FIXME: will block switchInProgress on true value in case of exception
|
2015-01-19 16:54:41 +00:00
|
|
|
connection.jingle.activecall.switchStreams(stream, oldStream, callback);
|
2015-01-19 09:20:00 +00:00
|
|
|
} else {
|
|
|
|
// We are done immediately
|
|
|
|
console.error("No conference handler");
|
2015-02-06 12:54:19 +00:00
|
|
|
APP.UI.messageHandler.showError('Error',
|
2015-01-19 09:20:00 +00:00
|
|
|
'Unable to switch video stream.');
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setVideoMute: function (mute, callback, options) {
|
2015-01-28 14:35:22 +00:00
|
|
|
if(connection && APP.RTC.localVideo && connection.jingle.activecall)
|
2015-01-19 09:20:00 +00:00
|
|
|
{
|
2015-01-19 16:54:41 +00:00
|
|
|
connection.jingle.activecall.setVideoMute(mute, callback, options);
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setAudioMute: function (mute, callback) {
|
2015-01-28 14:35:22 +00:00
|
|
|
if (!(connection && APP.RTC.localAudio)) {
|
2015-01-19 09:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.forceMuted && !mute) {
|
|
|
|
console.info("Asking focus for unmute");
|
|
|
|
connection.moderate.setMute(connection.emuc.myroomjid, mute);
|
|
|
|
// FIXME: wait for result before resetting muted status
|
|
|
|
this.forceMuted = false;
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:35:22 +00:00
|
|
|
if (mute == APP.RTC.localAudio.isMuted()) {
|
2015-01-19 09:20:00 +00:00
|
|
|
// Nothing to do
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is not clear what is the right way to handle multiple tracks.
|
|
|
|
// So at least make sure that they are all muted or all unmuted and
|
|
|
|
// that we send presence just once.
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.localAudio.mute();
|
2015-01-19 09:20:00 +00:00
|
|
|
// isMuted is the opposite of audioEnabled
|
|
|
|
connection.emuc.addAudioInfoToPresence(mute);
|
|
|
|
connection.emuc.sendPresence();
|
|
|
|
callback();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
// Really mute video, i.e. dont even send black frames
|
|
|
|
muteVideo: function (pc, unmute) {
|
|
|
|
// FIXME: this probably needs another of those lovely state safeguards...
|
|
|
|
// which checks for iceconn == connected and sigstate == stable
|
|
|
|
pc.setRemoteDescription(pc.remoteDescription,
|
|
|
|
function () {
|
|
|
|
pc.createAnswer(
|
|
|
|
function (answer) {
|
|
|
|
var sdp = new SDP(answer.sdp);
|
|
|
|
if (sdp.media.length > 1) {
|
|
|
|
if (unmute)
|
|
|
|
sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
|
|
|
|
else
|
|
|
|
sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
|
|
|
|
sdp.raw = sdp.session + sdp.media.join('');
|
|
|
|
answer.sdp = sdp.raw;
|
|
|
|
}
|
|
|
|
pc.setLocalDescription(answer,
|
|
|
|
function () {
|
|
|
|
console.log('mute SLD ok');
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
console.log('mute SLD error');
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.messageHandler.showError('Error',
|
2015-01-19 09:20:00 +00:00
|
|
|
'Oops! Something went wrong and we failed to ' +
|
|
|
|
'mute! (SLD Failure)');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
console.log(error);
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.messageHandler.showError();
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
console.log('muteVideo SRD error');
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.UI.messageHandler.showError('Error',
|
2015-01-19 09:20:00 +00:00
|
|
|
'Oops! Something went wrong and we failed to stop video!' +
|
|
|
|
'(SRD Failure)');
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
toggleRecording: function (tokenEmptyCallback,
|
|
|
|
startingCallback, startedCallback) {
|
|
|
|
Recording.toggleRecording(tokenEmptyCallback,
|
2015-01-20 15:56:00 +00:00
|
|
|
startingCallback, startedCallback, connection);
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
|
|
|
addToPresence: function (name, value, dontSend) {
|
|
|
|
switch (name)
|
|
|
|
{
|
|
|
|
case "displayName":
|
|
|
|
connection.emuc.addDisplayNameToPresence(value);
|
|
|
|
break;
|
|
|
|
case "etherpad":
|
|
|
|
connection.emuc.addEtherpadToPresence(value);
|
|
|
|
break;
|
|
|
|
case "prezi":
|
|
|
|
connection.emuc.addPreziToPresence(value, 0);
|
|
|
|
break;
|
|
|
|
case "preziSlide":
|
|
|
|
connection.emuc.addCurrentSlideToPresence(value);
|
|
|
|
break;
|
|
|
|
case "connectionQuality":
|
|
|
|
connection.emuc.addConnectionInfoToPresence(value);
|
|
|
|
break;
|
|
|
|
case "email":
|
|
|
|
connection.emuc.addEmailToPresence(value);
|
|
|
|
default :
|
|
|
|
console.log("Unknown tag for presence.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!dontSend)
|
|
|
|
connection.emuc.sendPresence();
|
|
|
|
},
|
|
|
|
sendLogs: function (data) {
|
2015-01-20 15:56:00 +00:00
|
|
|
if(!connection.emuc.focusMucJid)
|
2015-01-19 09:20:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var deflate = true;
|
|
|
|
|
2015-01-19 10:00:30 +00:00
|
|
|
var content = JSON.stringify(data);
|
2015-01-19 09:20:00 +00:00
|
|
|
if (deflate) {
|
2015-01-21 15:35:23 +00:00
|
|
|
content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
content = Base64.encode(content);
|
|
|
|
// XEP-0337-ish
|
2015-01-20 15:56:00 +00:00
|
|
|
var message = $msg({to: connection.emuc.focusMucJid, type: 'normal'});
|
2015-01-19 09:20:00 +00:00
|
|
|
message.c('log', { xmlns: 'urn:xmpp:eventlog',
|
|
|
|
id: 'PeerConnectionStats'});
|
|
|
|
message.c('message').t(content).up();
|
|
|
|
if (deflate) {
|
|
|
|
message.c('tag', {name: "deflated", value: "true"}).up();
|
|
|
|
}
|
|
|
|
message.up();
|
|
|
|
|
|
|
|
connection.send(message);
|
|
|
|
},
|
|
|
|
populateData: function () {
|
|
|
|
var data = {};
|
|
|
|
if (connection.jingle) {
|
|
|
|
data = connection.jingle.populateData();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
getLogger: function () {
|
|
|
|
if(connection.logger)
|
|
|
|
return connection.logger.log;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
getPrezi: function () {
|
|
|
|
return connection.emuc.getPrezi(this.myJid());
|
|
|
|
},
|
|
|
|
removePreziFromPresence: function () {
|
|
|
|
connection.emuc.removePreziFromPresence();
|
|
|
|
connection.emuc.sendPresence();
|
|
|
|
},
|
|
|
|
sendChatMessage: function (message, nickname) {
|
|
|
|
connection.emuc.sendMessage(message, nickname);
|
|
|
|
},
|
|
|
|
setSubject: function (topic) {
|
|
|
|
connection.emuc.setSubject(topic);
|
|
|
|
},
|
|
|
|
lockRoom: function (key, onSuccess, onError, onNotSupported) {
|
|
|
|
connection.emuc.lockRoom(key, onSuccess, onError, onNotSupported);
|
|
|
|
},
|
|
|
|
dial: function (to, from, roomName,roomPass) {
|
|
|
|
connection.rayo.dial(to, from, roomName,roomPass);
|
|
|
|
},
|
|
|
|
setMute: function (jid, mute) {
|
|
|
|
connection.moderate.setMute(jid, mute);
|
|
|
|
},
|
|
|
|
eject: function (jid) {
|
|
|
|
connection.moderate.eject(jid);
|
|
|
|
},
|
|
|
|
findJidFromResource: function (resource) {
|
2015-01-19 16:54:41 +00:00
|
|
|
return connection.emuc.findJidFromResource(resource);
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
|
|
|
getMembers: function () {
|
|
|
|
return connection.emuc.members;
|
2015-01-21 11:55:20 +00:00
|
|
|
},
|
|
|
|
getJidFromSSRC: function (ssrc) {
|
|
|
|
if(!connection)
|
|
|
|
return null;
|
|
|
|
return connection.emuc.ssrc2jid[ssrc];
|
2015-01-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-01-21 15:13:13 +00:00
|
|
|
module.exports = XMPP;
|