Created
March 24, 2017 06:41
-
-
Save aylarov/edae05d3d61da3d63d39d683dcacab84 to your computer and use it in GitHub Desktop.
Babelfish conference scenario
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Require Conference module to get conferencing functionality | |
*/ | |
require(Modules.Conference); | |
var videoconf, | |
calls = [], | |
clientType; | |
// Add event handler for session start event | |
VoxEngine.addEventListener(AppEvents.Started, handleConferenceStarted); | |
function handleConferenceStarted(e) { | |
// Create conference | |
videoconf = VoxEngine.createConference(); | |
} | |
/** | |
* Handle inbound call | |
*/ | |
VoxEngine.addEventListener(AppEvents.CallAlerting, function (e) { | |
if (calls.length == 2) e.call.reject(603, { | |
"X-Reason": "ROOM_IS_FULL" | |
}); | |
// get caller's client type | |
clientType = e.headers["X-ClientType"]; | |
// Add event handlers depending on the client type | |
if (clientType == "web") { | |
e.call.addEventListener(CallEvents.Connected, handleParticipantConnected); | |
e.call.addEventListener(CallEvents.Disconnected, handleParticipantDisconnected); | |
} | |
e.call.addEventListener(CallEvents.Failed, handleConnectionFailed); | |
e.call.addEventListener(CallEvents.MessageReceived, handleMessageReceived); | |
// Answer the call | |
e.call.answer(); | |
}); | |
/** | |
* Message handler | |
*/ | |
function handleMessageReceived(e) { | |
Logger.write("Message Recevied: " + e.text); | |
var msg; | |
try { | |
msg = JSON.parse(e.text); | |
} catch (err) { | |
Logger.write(err); | |
} | |
if (msg.type == "ICE_FAILED") { | |
// P2P call failed because of ICE problems - sending notification to retry | |
var caller = msg.caller.substr(0, msg.caller.indexOf('@')); | |
caller = caller.replace("sip:", ""); | |
Logger.write("Sending notification to " + caller); | |
var call = getCallById(caller); | |
if (call != null) call.sendMessage(JSON.stringify({ | |
type: "ICE_FAILED", | |
callee: msg.callee, | |
displayName: msg.displayName | |
})); | |
} else { | |
// Just proxy all messages to other conference participants | |
for (var i = 0; i < calls.length; i++) { | |
if (calls[i] != e.call) calls[i].sendMessage(e.text); | |
} | |
} | |
} | |
function handleConnectionFailed(e) { | |
Logger.write("Participant couldn't join the conference"); | |
} | |
/** | |
* Check if participant is already connected to the conference | |
*/ | |
function participantExists(callerid) { | |
for (var i = 0; i < calls.length; i++) { | |
if (calls[i].callerid() == callerid) return true; | |
} | |
return false; | |
} | |
function getCallById(callerid) { | |
for (var i = 0; i < calls.length; i++) { | |
if (calls[i].callerid() == callerid) return calls[i]; | |
} | |
return null; | |
} | |
/** | |
* Web client connected | |
*/ | |
function handleParticipantConnected(e) { | |
if (!participantExists(e.call.callerid())) calls.push(e.call); | |
e.call.say("You have joined the conference.", Language.UK_ENGLISH_FEMALE); | |
e.call.addEventListener(CallEvents.PlaybackFinished, function (e) { | |
videoconf.sendMediaTo(e.call); | |
sendCallsInfo(); | |
}); | |
} | |
/** | |
* Send info about participants to all participants | |
*/ | |
function sendCallsInfo() { | |
var info = { | |
peers: [] | |
}; | |
for (var k = 0; k < calls.length; k++) { | |
info.peers.push({ | |
callerid: calls[k].callerid(), | |
displayName: calls[k].displayName() | |
}); | |
} | |
for (var k = 0; k < calls.length; k++) { | |
calls[k].sendMessage(JSON.stringify(info)); | |
} | |
} | |
/** | |
* Web client disconnected | |
*/ | |
function handleParticipantDisconnected(e) { | |
for (var i = 0; i < calls.length; i++) { | |
if (calls[i].callerid() == e.call.callerid()) { | |
Logger.write("Caller with id " + e.call.callerid() + " disconnected"); | |
calls.splice(i, 1); | |
} | |
} | |
// No need for conference to be alive w/o any participants | |
if (calls.length == 0) VoxEngine.terminate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment