Skip to content

Instantly share code, notes, and snippets.

@gtk2k
Created April 17, 2016 06:06
Show Gist options
  • Select an option

  • Save gtk2k/20dfb8a91653d1cdb692f00a8b844d72 to your computer and use it in GitHub Desktop.

Select an option

Save gtk2k/20dfb8a91653d1cdb692f00a8b844d72 to your computer and use it in GitHub Desktop.
OpenTok probrem demo code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="//static.opentok.com/v2/js/opentok.min.js"></script>
<style>
.preview {
width: 640px;
height: 360px;
margin: 5px;
border: 1px solid;
}
</style>
</head>
<body>
<button id='swapVideoSource'>Swap video source (screen <=> camera)</button>
<div id="msg"></div>
<div id="localPreview" class="preview"></div>
<div id="remotePreview" class="preview"></div>
<script>
if(location.search){
var params = new URLSearchParams(location.search.substr(1));
var sessionId = params.get('sid');
var token = params.get('tkn');
} else{
var sessionId = '<?php echo $sessionId ?>';
var token = '<?php echo $token ?>';
var params = new URLSearchParams();
params.append('sid', sessionId);
params.append('tkn', token);
var newTabLink = document.createElement('a');
newTabLink.textContent = 'peer tab';
newTabLink.target = '_blank';
newTabLink.href = location.href + '?' + params.toString();
document.body.appendChild(newTabLink);
}
var isCanScreenShare = false;
var apiKey = '<?php echo $apiKey ?>';
var extensionId = '<?php echo $extensionId ?>';
var publisher;
var videoSource = '';
OT.registerScreenSharingExtension('chrome', extensionId, 2);
OT.checkScreenSharingCapability(function(res){
isCanScreenShare = res.supported && res.extensionRegistered && res.extensionInstalled;
if(!isCanScreenShare) msg.textContent = 'Can not use screen share';
swapVideoSource.disabled = !isCanScreenShare;
console.log('isCanScreenShare = ' + isCanScreenShare);
});
var session = OT.initSession(apiKey, sessionId)
.on('streamCreated', function(evt){
session.subscribe(evt.stream, 'remotePreview');
console.log('streamCreated');
})
.connect(token, function(err) {
if (err) {
session = null;
console.log(err.message);
return;
}
swap();
console.log('connected');
});
var swap = function (){
videoSource = videoSource === undefined ? 'screen' : undefined;
if(publisher){
if(session){
session.unpublish(publisher);
publisher.destroy();
console.log('unpublished');
}
}
publisher = OT.initPublisher('localPreview', {
publishAudio: false,
publishVideo: true,
videoSource: videoSource,
audioSource: null,
width: '640px',
height: '360px',
style:{buttonDisplayMode: 'off'},
insertMode: 'append'
},
function (error) {
if (error) {
console.log(error);
return;
}
console.log('Publisher initialized');
});
session.publish(publisher, function(error){
if(error){
console.log(error.message);
return;
}
console.log('published');
});
};
swapVideoSource.onclick = swap;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment