Skip to content

Instantly share code, notes, and snippets.

@jeanlescure
Last active September 7, 2015 22:45
Show Gist options
  • Save jeanlescure/6f9e20fe2a2c51bf0041 to your computer and use it in GitHub Desktop.
Save jeanlescure/6f9e20fe2a2c51bf0041 to your computer and use it in GitHub Desktop.
OpenTok call module harnessing Pre-Call bandwidth test
// Override global variable callback
onTestComplete = function(){
// Run the following only if there's enough bandwidth
// to stream at least audio
if (audioSupported){
setTimeout(function(){
setText(statusTitleEl, 'Session Started');
setText(statusMessageEl, '');
statusContainerEl.removeChild(statusIconEl);
initCall(); // Warm up the engine
}, 3000);
}
}
// Again we call the NodeJS server to get
// a Session Id and a Token
function initCall(){
getJSON('/session.id', function(data) {
SESSION_ID = data.sessionId;
getJSON('/token', function(data) {
TOKEN = data.token;
// We have both a Session Id and a Token
startCall(); // Let's start the call
}, function(status) {
alert("TOKEN ERROR:\nSomething went wrong.");
});
}, function(status) {
alert("SESSION ID ERROR:\nSomething went wrong.");
});
}
// We initiate a session and a publisher just like with any other
// OpenTok implementation except...
function startCall(){
var peersContainer = statusContainerEl.querySelector('div');
session = OT.initSession(API_KEY, SESSION_ID);
session.connect(TOKEN, function(error) {
if (!error) {
publisher = OT.initPublisher(statusMessageEl, {
insertMode: 'append',
width: '320px',
height: '240px'
});
// ************************************
publisher.publishVideo(videoSupported); // ... We can turn off video if not supported!
// ************************************
session.publish(publisher, function(error){
if (error) {
console.log(error);
}
});
session.on('streamCreated', function(event) {
subscriber = session.subscribe(event.stream,
peersContainer,
{
insertMode: 'append',
width: '320px',
height: '240px'
},
function (error) {
if (error) {
console.log(error);
}
}
);
});
} else {
console.log('There was an error connecting to the session:', error.code, error.message);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment