2014-02-08 08:56:23 +08:00
|
|
|
|
2015-11-05 08:44:15 +08:00
|
|
|
var userID, callerIdName=null, conferenceVoiceBridge, userAgent=null, userMicMedia, userWebcamMedia, currentSession=null, callTimeout, callActive, callICEConnected, iceConnectedTimeout, callFailCounter, callPurposefullyEnded, uaConnected, transferTimeout, iceGatheringTimeout;
|
2015-02-13 06:25:41 +08:00
|
|
|
var inEchoTest = true;
|
|
|
|
|
|
|
|
function webRTCCallback(message) {
|
|
|
|
switch (message.status) {
|
2015-05-27 04:03:33 +08:00
|
|
|
case 'succeded':
|
|
|
|
BBB.webRTCCallSucceeded();
|
|
|
|
break;
|
2015-02-13 06:25:41 +08:00
|
|
|
case 'failed':
|
2015-02-18 08:11:42 +08:00
|
|
|
if (message.errorcode !== 1004) {
|
|
|
|
message.cause = null;
|
|
|
|
}
|
|
|
|
BBB.webRTCCallFailed(inEchoTest, message.errorcode, message.cause);
|
2015-02-13 06:25:41 +08:00
|
|
|
break;
|
|
|
|
case 'ended':
|
|
|
|
BBB.webRTCCallEnded(inEchoTest);
|
|
|
|
break;
|
|
|
|
case 'started':
|
|
|
|
BBB.webRTCCallStarted(inEchoTest);
|
|
|
|
break;
|
|
|
|
case 'connecting':
|
|
|
|
BBB.webRTCCallConnecting(inEchoTest);
|
|
|
|
break;
|
|
|
|
case 'waitingforice':
|
|
|
|
BBB.webRTCCallWaitingForICE(inEchoTest);
|
|
|
|
break;
|
|
|
|
case 'transferring':
|
|
|
|
BBB.webRTCCallTransferring(inEchoTest);
|
|
|
|
break;
|
|
|
|
case 'mediarequest':
|
|
|
|
BBB.webRTCMediaRequest();
|
|
|
|
break;
|
|
|
|
case 'mediasuccess':
|
|
|
|
BBB.webRTCMediaSuccess();
|
|
|
|
break;
|
|
|
|
case 'mediafail':
|
|
|
|
BBB.webRTCMediaFail();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2015-04-01 03:35:09 +08:00
|
|
|
function callIntoConference(voiceBridge, callback, isListenOnly) {
|
2015-04-06 22:29:25 +08:00
|
|
|
// root of the call initiation process from the html5 client
|
|
|
|
// Flash will not pass in the listen only field. For html5 it is optional. Assume NOT listen only if no state passed
|
2015-04-01 03:35:09 +08:00
|
|
|
if (isListenOnly == null) {
|
|
|
|
isListenOnly = false;
|
|
|
|
}
|
2015-10-07 23:09:08 +08:00
|
|
|
// reset callerIdName
|
|
|
|
callerIdName = null;
|
2014-08-09 04:19:38 +08:00
|
|
|
if (!callerIdName) {
|
|
|
|
BBB.getMyUserInfo(function(userInfo) {
|
2015-10-07 01:57:39 +08:00
|
|
|
console.log("User info callback [myUserID=" + userInfo.myUserID
|
2014-08-09 04:19:38 +08:00
|
|
|
+ ",myUsername=" + userInfo.myUsername + ",myAvatarURL=" + userInfo.myAvatarURL
|
|
|
|
+ ",myRole=" + userInfo.myRole + ",amIPresenter=" + userInfo.amIPresenter
|
2015-04-01 03:35:09 +08:00
|
|
|
+ ",dialNumber=" + userInfo.dialNumber + ",voiceBridge=" + userInfo.voiceBridge
|
|
|
|
+ ",isListenOnly=" + isListenOnly + "].");
|
2015-10-07 01:57:39 +08:00
|
|
|
userID = userInfo.myUserID;
|
|
|
|
callerIdName = userInfo.myUserID + "-bbbID-" + userInfo.myUsername;
|
2015-04-11 02:51:46 +08:00
|
|
|
if (isListenOnly) {
|
|
|
|
//prepend the callerIdName so it is recognized as a global audio user
|
|
|
|
callerIdName = "GLOBAL_AUDIO_" + callerIdName;
|
|
|
|
}
|
2014-08-09 04:19:38 +08:00
|
|
|
conferenceVoiceBridge = userInfo.voiceBridge
|
2015-02-13 06:25:41 +08:00
|
|
|
if (voiceBridge === "9196") {
|
|
|
|
voiceBridge = voiceBridge + conferenceVoiceBridge;
|
|
|
|
} else {
|
|
|
|
voiceBridge = conferenceVoiceBridge;
|
|
|
|
}
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log(callerIdName);
|
2015-04-01 03:35:09 +08:00
|
|
|
webrtc_call(callerIdName, voiceBridge, callback, isListenOnly);
|
2014-08-09 04:19:38 +08:00
|
|
|
});
|
|
|
|
} else {
|
2015-02-13 06:25:41 +08:00
|
|
|
if (voiceBridge === "9196") {
|
|
|
|
voiceBridge = voiceBridge + conferenceVoiceBridge;
|
|
|
|
} else {
|
|
|
|
voiceBridge = conferenceVoiceBridge;
|
|
|
|
}
|
2015-04-01 03:35:09 +08:00
|
|
|
webrtc_call(callerIdName, voiceBridge, callback, isListenOnly);
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
function joinWebRTCVoiceConference() {
|
2015-02-13 06:25:41 +08:00
|
|
|
console.log("Joining to the voice conference directly");
|
|
|
|
inEchoTest = false;
|
2015-06-02 09:43:10 +08:00
|
|
|
// set proper callbacks to previously created user agent
|
2015-03-24 23:51:31 +08:00
|
|
|
if(userAgent) {
|
2015-06-02 09:43:10 +08:00
|
|
|
setUserAgentListeners(webRTCCallback);
|
2015-03-24 23:51:31 +08:00
|
|
|
}
|
2015-02-13 06:25:41 +08:00
|
|
|
callIntoConference(conferenceVoiceBridge, webRTCCallback);
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
function leaveWebRTCVoiceConference() {
|
|
|
|
console.log("Leaving the voice conference");
|
|
|
|
|
2014-08-10 03:39:37 +08:00
|
|
|
webrtc_hangup();
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-08-09 05:17:28 +08:00
|
|
|
function startWebRTCAudioTest(){
|
2015-02-13 06:25:41 +08:00
|
|
|
console.log("Joining the audio test first");
|
|
|
|
inEchoTest = true;
|
|
|
|
callIntoConference("9196", webRTCCallback);
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-08-09 05:17:28 +08:00
|
|
|
function stopWebRTCAudioTest(){
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log("Stopping webrtc audio test");
|
|
|
|
|
2014-08-10 03:39:37 +08:00
|
|
|
webrtc_hangup();
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-08-10 00:38:48 +08:00
|
|
|
function stopWebRTCAudioTestJoinConference(){
|
2015-02-13 06:25:41 +08:00
|
|
|
console.log("Transferring from audio test to conference");
|
2014-08-10 00:38:48 +08:00
|
|
|
|
2015-02-13 06:25:41 +08:00
|
|
|
webRTCCallback({'status': 'transferring'});
|
2014-09-02 04:10:50 +08:00
|
|
|
|
2015-02-13 06:25:41 +08:00
|
|
|
transferTimeout = setTimeout( function() {
|
|
|
|
console.log("Call transfer failed. No response after 3 seconds");
|
|
|
|
webRTCCallback({'status': 'failed', 'errorcode': 1008});
|
|
|
|
currentSession = null;
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2015-03-26 05:05:26 +08:00
|
|
|
}, 5000);
|
2014-09-02 04:10:50 +08:00
|
|
|
|
2015-02-13 06:25:41 +08:00
|
|
|
BBB.listen("UserJoinedVoiceEvent", userJoinedVoiceHandler);
|
2014-09-02 04:10:50 +08:00
|
|
|
|
2015-02-13 06:25:41 +08:00
|
|
|
currentSession.dtmf(1);
|
|
|
|
inEchoTest = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userJoinedVoiceHandler(event) {
|
|
|
|
console.log("UserJoinedVoiceHandler - " + event);
|
|
|
|
if (inEchoTest === false && userID === event.userID) {
|
|
|
|
BBB.unlisten("UserJoinedVoiceEvent", userJoinedVoiceHandler);
|
|
|
|
clearTimeout(transferTimeout);
|
|
|
|
webRTCCallback({'status': 'started'});
|
|
|
|
}
|
2014-09-02 04:10:50 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 03:36:14 +08:00
|
|
|
function createUA(username, server, callback, makeCallFunc) {
|
2014-08-09 04:19:38 +08:00
|
|
|
if (userAgent) {
|
|
|
|
console.log("User agent already created");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-18 01:00:04 +08:00
|
|
|
|
|
|
|
console.log("Fetching STUN/TURN server info for user agent");
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
dataType: 'json',
|
|
|
|
url: '/bigbluebutton/api/stuns'
|
|
|
|
}).done(function(data) {
|
|
|
|
var stunsConfig = {};
|
2015-02-25 03:36:14 +08:00
|
|
|
stunsConfig['stunServers'] = ( data['stunServers'] ? data['stunServers'].map(function(data) {
|
2015-02-18 01:00:04 +08:00
|
|
|
return data['url'];
|
2015-03-06 05:10:27 +08:00
|
|
|
}) : [] );
|
2015-02-25 03:36:14 +08:00
|
|
|
stunsConfig['turnServers'] = ( data['turnServers'] ? data['turnServers'].map(function(data) {
|
2015-02-18 01:00:04 +08:00
|
|
|
return {
|
|
|
|
'urls': data['url'],
|
|
|
|
'username': data['username'],
|
|
|
|
'password': data['password']
|
|
|
|
};
|
2015-03-06 05:10:27 +08:00
|
|
|
}) : [] );
|
2015-02-25 03:36:14 +08:00
|
|
|
createUAWithStuns(username, server, callback, stunsConfig, makeCallFunc);
|
2015-02-18 01:00:04 +08:00
|
|
|
}).fail(function(data, textStatus, errorThrown) {
|
2015-02-25 04:15:01 +08:00
|
|
|
BBBLog.error("Could not fetch stun/turn servers", {error: textStatus, user: callerIdName, voiceBridge: conferenceVoiceBridge});
|
2015-02-18 01:00:04 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1009});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-25 03:36:14 +08:00
|
|
|
function createUAWithStuns(username, server, callback, stunsConfig, makeCallFunc) {
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log("Creating new user agent");
|
2015-11-30 11:06:38 +08:00
|
|
|
|
2015-09-13 01:13:18 +08:00
|
|
|
var wsServer;
|
|
|
|
if (window.location.protocol == 'https:') {
|
|
|
|
wsServer = 'wss://' + server + '/wss';
|
|
|
|
} else {
|
|
|
|
wsServer = 'ws://' + server + '/ws';
|
|
|
|
}
|
2014-09-15 04:49:12 +08:00
|
|
|
/* VERY IMPORTANT
|
|
|
|
* - You must escape the username because spaces will cause the connection to fail
|
|
|
|
* - We are connecting to the websocket through an nginx redirect instead of directly to 5066
|
|
|
|
*/
|
2014-08-09 04:19:38 +08:00
|
|
|
var configuration = {
|
2014-09-10 05:27:03 +08:00
|
|
|
uri: 'sip:' + encodeURIComponent(username) + '@' + server,
|
2015-09-13 01:13:18 +08:00
|
|
|
wsServers: wsServer,
|
2014-08-09 05:17:28 +08:00
|
|
|
displayName: username,
|
2014-08-09 04:19:38 +08:00
|
|
|
register: false,
|
2014-10-07 05:14:34 +08:00
|
|
|
traceSip: true,
|
|
|
|
autostart: false,
|
2014-09-15 04:49:12 +08:00
|
|
|
userAgentString: "BigBlueButton",
|
2015-02-18 01:00:04 +08:00
|
|
|
stunServers: stunsConfig['stunServers'],
|
|
|
|
turnServers: stunsConfig['turnServers']
|
2014-08-09 04:19:38 +08:00
|
|
|
};
|
|
|
|
|
2014-10-07 05:14:34 +08:00
|
|
|
uaConnected = false;
|
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
userAgent = new SIP.UA(configuration);
|
2015-06-02 09:43:10 +08:00
|
|
|
setUserAgentListeners(callback, makeCallFunc);
|
2015-03-24 23:51:31 +08:00
|
|
|
userAgent.start();
|
|
|
|
};
|
|
|
|
|
2015-06-02 09:43:10 +08:00
|
|
|
function setUserAgentListeners(callback, makeCallFunc) {
|
|
|
|
console.log("reseting UA callbacks");
|
2015-03-24 23:51:31 +08:00
|
|
|
userAgent.off('connected');
|
2014-10-07 05:14:34 +08:00
|
|
|
userAgent.on('connected', function() {
|
|
|
|
uaConnected = true;
|
2015-05-27 04:03:33 +08:00
|
|
|
callback({'status':'succeded'});
|
2015-02-25 03:36:14 +08:00
|
|
|
makeCallFunc();
|
2014-10-07 05:14:34 +08:00
|
|
|
});
|
2015-03-24 23:51:31 +08:00
|
|
|
userAgent.off('disconnected');
|
2014-09-15 04:49:12 +08:00
|
|
|
userAgent.on('disconnected', function() {
|
|
|
|
if (userAgent) {
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2014-10-07 05:14:34 +08:00
|
|
|
|
|
|
|
if (uaConnected) {
|
2015-02-13 06:25:41 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1001}); // WebSocket disconnected
|
2014-10-07 05:14:34 +08:00
|
|
|
} else {
|
2015-02-13 06:25:41 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1002}); // Could not make a WebSocket connection
|
2014-10-07 05:14:34 +08:00
|
|
|
}
|
2014-09-15 04:49:12 +08:00
|
|
|
}
|
|
|
|
});
|
2014-08-09 04:19:38 +08:00
|
|
|
};
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-09-02 04:10:50 +08:00
|
|
|
function getUserMicMedia(getUserMicMediaSuccess, getUserMicMediaFailure) {
|
|
|
|
if (userMicMedia == undefined) {
|
2014-08-09 04:19:38 +08:00
|
|
|
if (SIP.WebRTC.isSupported()) {
|
2014-09-02 04:10:50 +08:00
|
|
|
SIP.WebRTC.getUserMedia({audio:true, video:false}, getUserMicMediaSuccess, getUserMicMediaFailure);
|
2014-08-09 04:19:38 +08:00
|
|
|
} else {
|
2014-09-02 04:10:50 +08:00
|
|
|
console.log("getUserMicMedia: webrtc not supported");
|
|
|
|
getUserMicMediaFailure("WebRTC is not supported");
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-02 04:10:50 +08:00
|
|
|
console.log("getUserMicMedia: mic already set");
|
|
|
|
getUserMicMediaSuccess(userMicMedia);
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
|
|
|
};
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2015-04-01 03:35:09 +08:00
|
|
|
function webrtc_call(username, voiceBridge, callback, isListenOnly) {
|
2014-08-09 04:19:38 +08:00
|
|
|
if (!isWebRTCAvailable()) {
|
2014-10-16 07:17:58 +08:00
|
|
|
callback({'status': 'failed', 'errorcode': 1003}); // Browser version not supported
|
2014-08-09 04:19:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-06 22:29:25 +08:00
|
|
|
if (isListenOnly == null) { // assume NOT listen only unless otherwise stated
|
2015-04-01 03:35:09 +08:00
|
|
|
isListenOnly = false;
|
|
|
|
}
|
|
|
|
|
2014-11-12 02:42:40 +08:00
|
|
|
var server = window.document.location.hostname;
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log("user " + username + " calling to " + voiceBridge);
|
2015-04-01 03:35:09 +08:00
|
|
|
|
2015-02-25 03:36:14 +08:00
|
|
|
var makeCallFunc = function() {
|
2015-04-06 22:29:25 +08:00
|
|
|
// only make the call when both microphone and useragent have been created
|
|
|
|
// for listen only, stating listen only is a viable substitute for acquiring user media control
|
|
|
|
if ((isListenOnly||userMicMedia) && userAgent)
|
2015-04-01 03:35:09 +08:00
|
|
|
make_call(username, voiceBridge, server, callback, false, isListenOnly);
|
2015-02-25 03:36:14 +08:00
|
|
|
};
|
2015-10-07 23:09:08 +08:00
|
|
|
|
|
|
|
// Reset userAgent so we can successfully switch between listenOnly and listen+speak modes
|
|
|
|
userAgent = null;
|
2014-08-09 04:19:38 +08:00
|
|
|
if (!userAgent) {
|
2015-02-25 03:36:14 +08:00
|
|
|
createUA(username, server, callback, makeCallFunc);
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2015-04-06 22:29:25 +08:00
|
|
|
// if the user requests to proceed as listen only (does not require media) or media is already acquired,
|
|
|
|
// proceed with making the call
|
2015-04-01 03:35:09 +08:00
|
|
|
if (isListenOnly || userMicMedia !== undefined) {
|
2015-02-25 03:36:14 +08:00
|
|
|
makeCallFunc();
|
2014-08-09 04:19:38 +08:00
|
|
|
} else {
|
|
|
|
callback({'status':'mediarequest'});
|
2014-09-02 04:10:50 +08:00
|
|
|
getUserMicMedia(function(stream) {
|
|
|
|
console.log("getUserMicMedia: success");
|
|
|
|
userMicMedia = stream;
|
2014-08-09 04:19:38 +08:00
|
|
|
callback({'status':'mediasuccess'});
|
2015-02-25 03:36:14 +08:00
|
|
|
makeCallFunc();
|
2014-08-09 04:19:38 +08:00
|
|
|
}, function(e) {
|
2014-09-02 04:10:50 +08:00
|
|
|
console.error("getUserMicMedia: failure - " + e);
|
2014-08-09 05:17:28 +08:00
|
|
|
callback({'status':'mediafail', 'cause': e});
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2015-04-01 03:35:09 +08:00
|
|
|
function make_call(username, voiceBridge, server, callback, recall, isListenOnly) {
|
|
|
|
if (isListenOnly == null) {
|
|
|
|
isListenOnly = false;
|
|
|
|
}
|
|
|
|
|
2015-02-18 08:11:42 +08:00
|
|
|
if (userAgent == null) {
|
|
|
|
console.log("userAgent is still null. Delaying call");
|
|
|
|
var callDelayTimeout = setTimeout( function() {
|
2015-04-01 03:35:09 +08:00
|
|
|
make_call(username, voiceBridge, server, callback, recall, isListenOnly);
|
2015-02-18 08:11:42 +08:00
|
|
|
}, 100);
|
2015-02-25 03:36:14 +08:00
|
|
|
return;
|
2015-02-18 08:11:42 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:14:34 +08:00
|
|
|
if (!userAgent.isConnected()) {
|
|
|
|
console.log("Trying to make call, but UserAgent hasn't connected yet. Delaying call");
|
|
|
|
userAgent.once('connected', function() {
|
|
|
|
console.log("UserAgent has now connected, retrying the call");
|
2015-04-01 03:35:09 +08:00
|
|
|
make_call(username, voiceBridge, server, callback, recall, isListenOnly);
|
2014-10-07 05:14:34 +08:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSession) {
|
|
|
|
console.log('Active call detected ignoring second make_call');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
// Make an audio/video call:
|
|
|
|
console.log("Setting options.. ");
|
2015-04-02 23:37:51 +08:00
|
|
|
|
2015-04-01 03:35:09 +08:00
|
|
|
var options = {};
|
|
|
|
if (isListenOnly) {
|
2015-04-06 22:29:25 +08:00
|
|
|
// create necessary options for a listen only stream
|
2015-04-01 03:35:09 +08:00
|
|
|
var stream = null;
|
2015-04-06 22:29:25 +08:00
|
|
|
// handle the web browser
|
|
|
|
// create a stream object through the browser separated from user media
|
2015-04-01 03:35:09 +08:00
|
|
|
if (typeof webkitMediaStream !== 'undefined') {
|
|
|
|
// Google Chrome
|
|
|
|
stream = new webkitMediaStream;
|
|
|
|
} else {
|
|
|
|
// Firefox
|
|
|
|
audioContext = new window.AudioContext;
|
|
|
|
stream = audioContext.createMediaStreamDestination().stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
media: {
|
2015-04-06 22:29:25 +08:00
|
|
|
stream: stream, // use the stream created above
|
2015-04-01 03:35:09 +08:00
|
|
|
render: {
|
|
|
|
remote: {
|
2015-04-06 22:29:25 +08:00
|
|
|
// select an element to render the incoming stream data
|
2015-04-01 03:35:09 +08:00
|
|
|
audio: document.getElementById('remote-media')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-04-06 22:29:25 +08:00
|
|
|
// a list of our RTC Connection constraints
|
2015-04-01 03:35:09 +08:00
|
|
|
RTCConstraints: {
|
2015-04-06 22:29:25 +08:00
|
|
|
// our constraints are mandatory. We must received audio and must not receive audio
|
2015-04-01 03:35:09 +08:00
|
|
|
mandatory: {
|
|
|
|
OfferToReceiveAudio: true,
|
|
|
|
OfferToReceiveVideo: false
|
2014-08-10 00:38:48 +08:00
|
|
|
}
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2015-04-01 03:35:09 +08:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
options = {
|
|
|
|
media: {
|
|
|
|
stream: userMicMedia,
|
|
|
|
render: {
|
|
|
|
remote: {
|
|
|
|
audio: document.getElementById('remote-media')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-08-09 04:19:38 +08:00
|
|
|
|
2014-10-07 05:14:34 +08:00
|
|
|
callTimeout = setTimeout(function() {
|
2014-10-30 00:30:03 +08:00
|
|
|
console.log('Ten seconds without updates sending timeout code');
|
|
|
|
callback({'status':'failed', 'errorcode': 1006}); // Failure on call
|
|
|
|
currentSession = null;
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2014-10-07 05:14:34 +08:00
|
|
|
}, 10000);
|
|
|
|
|
|
|
|
callActive = false;
|
2014-11-07 02:35:03 +08:00
|
|
|
callICEConnected = false;
|
2014-10-07 05:14:34 +08:00
|
|
|
callPurposefullyEnded = false;
|
|
|
|
callFailCounter = 0;
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log("Calling to " + voiceBridge + "....");
|
|
|
|
currentSession = userAgent.invite('sip:' + voiceBridge + '@' + server, options);
|
|
|
|
|
2014-10-07 05:14:34 +08:00
|
|
|
// Only send the callback if it's the first try
|
|
|
|
if (recall === false) {
|
|
|
|
console.log('call connecting');
|
|
|
|
callback({'status':'connecting'});
|
|
|
|
} else {
|
|
|
|
console.log('call connecting again');
|
|
|
|
}
|
|
|
|
|
2015-11-05 08:44:15 +08:00
|
|
|
iceGatheringTimeout = setTimeout(function() {
|
|
|
|
console.log('Thirty seconds without ICE gathering finishing');
|
|
|
|
callback({'status':'failed', 'errorcode': 1011}); // ICE Gathering Failed
|
|
|
|
currentSession = null;
|
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
|
|
|
}, 30000);
|
|
|
|
|
|
|
|
currentSession.mediaHandler.on('iceComplete', function() {
|
|
|
|
clearTimeout(iceGatheringTimeout);
|
|
|
|
});
|
|
|
|
|
2014-08-10 03:39:37 +08:00
|
|
|
// The connecting event fires before the listener can be added
|
|
|
|
currentSession.on('connecting', function(){
|
2014-10-31 04:54:35 +08:00
|
|
|
clearTimeout(callTimeout);
|
2014-08-10 03:39:37 +08:00
|
|
|
});
|
2014-10-07 05:14:34 +08:00
|
|
|
currentSession.on('progress', function(response){
|
|
|
|
console.log('call progress: ' + response);
|
|
|
|
clearTimeout(callTimeout);
|
|
|
|
});
|
2014-08-09 04:19:38 +08:00
|
|
|
currentSession.on('failed', function(response, cause){
|
|
|
|
console.log('call failed with cause: '+ cause);
|
2014-10-07 05:14:34 +08:00
|
|
|
|
2014-10-16 07:17:58 +08:00
|
|
|
if (currentSession) {
|
|
|
|
if (callActive === false) {
|
2015-02-18 08:11:42 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1004, 'cause': cause}); // Failure on call
|
2014-10-16 07:17:58 +08:00
|
|
|
currentSession = null;
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2014-10-16 07:17:58 +08:00
|
|
|
} else {
|
|
|
|
callActive = false;
|
|
|
|
//currentSession.bye();
|
|
|
|
currentSession = null;
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
userAgent.stop();
|
|
|
|
}
|
2014-10-16 07:17:58 +08:00
|
|
|
}
|
2014-10-07 05:14:34 +08:00
|
|
|
}
|
|
|
|
clearTimeout(callTimeout);
|
2014-08-09 04:19:38 +08:00
|
|
|
});
|
|
|
|
currentSession.on('bye', function(request){
|
2014-10-07 05:14:34 +08:00
|
|
|
callActive = false;
|
|
|
|
|
|
|
|
if (currentSession) {
|
|
|
|
console.log('call ended ' + currentSession.endTime);
|
|
|
|
|
|
|
|
if (callPurposefullyEnded === true) {
|
|
|
|
callback({'status':'ended'});
|
|
|
|
} else {
|
2014-10-16 07:17:58 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1005}); // Call ended unexpectedly
|
2014-10-07 05:14:34 +08:00
|
|
|
}
|
|
|
|
clearTimeout(callTimeout);
|
|
|
|
currentSession = null;
|
|
|
|
} else {
|
|
|
|
console.log('bye event already received');
|
|
|
|
}
|
2014-08-09 04:19:38 +08:00
|
|
|
});
|
2015-04-17 01:01:23 +08:00
|
|
|
currentSession.on('cancel', function(request){
|
|
|
|
callActive = false;
|
|
|
|
|
|
|
|
if (currentSession) {
|
2015-04-17 23:24:12 +08:00
|
|
|
console.log('call canceled');
|
2015-04-17 01:01:23 +08:00
|
|
|
clearTimeout(callTimeout);
|
|
|
|
currentSession = null;
|
|
|
|
} else {
|
|
|
|
console.log('cancel event already received');
|
|
|
|
}
|
|
|
|
});
|
2014-08-09 04:19:38 +08:00
|
|
|
currentSession.on('accepted', function(data){
|
2014-10-07 05:14:34 +08:00
|
|
|
callActive = true;
|
2014-11-07 02:35:03 +08:00
|
|
|
console.log('BigBlueButton call accepted');
|
2014-10-07 05:14:34 +08:00
|
|
|
|
2014-11-07 02:35:03 +08:00
|
|
|
if (callICEConnected === true) {
|
|
|
|
callback({'status':'started'});
|
|
|
|
} else {
|
|
|
|
callback({'status':'waitingforice'});
|
2015-08-27 04:56:52 +08:00
|
|
|
console.log('Waiting for ICE negotiation');
|
|
|
|
iceConnectedTimeout = setTimeout(function() {
|
2015-11-30 11:06:38 +08:00
|
|
|
console.log('5 seconds without ICE finishing');
|
2015-11-05 08:44:15 +08:00
|
|
|
callback({'status':'failed', 'errorcode': 1010}); // ICE negotiation timeout
|
2015-08-27 04:56:52 +08:00
|
|
|
currentSession = null;
|
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2015-11-30 11:06:38 +08:00
|
|
|
}, 5000);
|
2014-11-07 02:35:03 +08:00
|
|
|
}
|
2014-10-07 05:14:34 +08:00
|
|
|
clearTimeout(callTimeout);
|
2014-08-09 04:19:38 +08:00
|
|
|
});
|
2014-10-30 00:30:03 +08:00
|
|
|
currentSession.mediaHandler.on('iceFailed', function() {
|
|
|
|
console.log('received ice negotiation failed');
|
|
|
|
callback({'status':'failed', 'errorcode': 1007}); // Failure on call
|
|
|
|
currentSession = null;
|
2015-08-27 04:56:52 +08:00
|
|
|
clearTimeout(iceConnectedTimeout);
|
2015-03-19 06:36:29 +08:00
|
|
|
if (userAgent != null) {
|
|
|
|
var userAgentTemp = userAgent;
|
|
|
|
userAgent = null;
|
|
|
|
userAgentTemp.stop();
|
|
|
|
}
|
2014-10-30 00:30:03 +08:00
|
|
|
|
|
|
|
clearTimeout(callTimeout);
|
|
|
|
});
|
2014-11-07 02:35:03 +08:00
|
|
|
|
|
|
|
// Some browsers use status of 'connected', others use 'completed', and a couple use both
|
|
|
|
|
|
|
|
currentSession.mediaHandler.on('iceConnected', function() {
|
|
|
|
console.log('Received ICE status changed to connected');
|
|
|
|
if (callICEConnected === false) {
|
|
|
|
callICEConnected = true;
|
2015-08-27 04:56:52 +08:00
|
|
|
clearTimeout(iceConnectedTimeout);
|
2014-11-07 02:35:03 +08:00
|
|
|
if (callActive === true) {
|
|
|
|
callback({'status':'started'});
|
|
|
|
}
|
|
|
|
clearTimeout(callTimeout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
currentSession.mediaHandler.on('iceCompleted', function() {
|
|
|
|
console.log('Received ICE status changed to completed');
|
|
|
|
if (callICEConnected === false) {
|
|
|
|
callICEConnected = true;
|
2015-08-27 04:56:52 +08:00
|
|
|
clearTimeout(iceConnectedTimeout);
|
2014-11-07 02:35:03 +08:00
|
|
|
if (callActive === true) {
|
|
|
|
callback({'status':'started'});
|
|
|
|
}
|
|
|
|
clearTimeout(callTimeout);
|
|
|
|
}
|
|
|
|
});
|
2014-08-09 04:19:38 +08:00
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
function webrtc_hangup(callback) {
|
2014-10-07 05:14:34 +08:00
|
|
|
callPurposefullyEnded = true;
|
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
console.log("Hanging up current session");
|
|
|
|
if (callback) {
|
|
|
|
currentSession.on('bye', callback);
|
|
|
|
}
|
2015-04-17 01:01:23 +08:00
|
|
|
try {
|
|
|
|
currentSession.bye();
|
|
|
|
} catch (err) {
|
|
|
|
console.log("Forcing to cancel current session");
|
|
|
|
currentSession.cancel();
|
|
|
|
}
|
2014-02-08 08:56:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 04:19:38 +08:00
|
|
|
function isWebRTCAvailable() {
|
|
|
|
return SIP.WebRTC.isSupported();
|
|
|
|
}
|
2015-04-16 23:56:56 +08:00
|
|
|
|
|
|
|
function getCallStatus() {
|
|
|
|
return currentSession;
|
|
|
|
}
|