2017-06-14 04:18:21 +08:00
|
|
|
var isFirefox = typeof window.InstallTrigger !== 'undefined';
|
|
|
|
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
|
|
|
|
var isChrome = !!window.chrome && !isOpera;
|
|
|
|
var kurentoHandler = null;
|
|
|
|
|
|
|
|
Kurento = function (
|
|
|
|
tag,
|
|
|
|
voiceBridge,
|
|
|
|
conferenceUsername,
|
2017-07-04 03:13:00 +08:00
|
|
|
internalMeetingId,
|
2017-06-14 04:18:21 +08:00
|
|
|
onFail = null,
|
|
|
|
chromeExtension = null) {
|
|
|
|
|
|
|
|
this.ws = null;
|
|
|
|
this.video;
|
|
|
|
this.screen;
|
|
|
|
this.webRtcPeer;
|
|
|
|
this.extensionInstalled = false;
|
|
|
|
this.screenConstraints = {};
|
2017-07-04 03:13:00 +08:00
|
|
|
this.mediaCallback = null;
|
2017-06-14 04:18:21 +08:00
|
|
|
voiceBridge += "-DESKSHARE";
|
|
|
|
|
|
|
|
this.vid_width = window.screen.width;
|
|
|
|
this.vid_height = window.screen.height;
|
|
|
|
|
|
|
|
this.sessid = Math.random().toString();
|
|
|
|
|
|
|
|
this.renderTag = 'remote-media';
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
this.destination_number = internalMeetingId;
|
2017-06-14 04:18:21 +08:00
|
|
|
this.caller_id_name = conferenceUsername;
|
|
|
|
this.caller_id_number = conferenceUsername;
|
2017-07-04 03:13:00 +08:00
|
|
|
this.pingInterval;
|
2017-06-14 04:18:21 +08:00
|
|
|
|
|
|
|
this.kurentoPort = "kurento-screenshare";
|
|
|
|
this.hostName = window.location.hostname;
|
|
|
|
this.socketUrl = 'wss://' + this.hostName + '/' + this.kurentoPort;
|
|
|
|
|
|
|
|
this.iceServers = null;
|
|
|
|
|
|
|
|
if (chromeExtension != null) {
|
|
|
|
this.chromeExtension = chromeExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onFail != null) {
|
|
|
|
this.onFail = onFail;
|
|
|
|
} else {
|
|
|
|
var _this = this;
|
|
|
|
this.onFail = function () {
|
|
|
|
_this.logError('Default error handler');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.KurentoManager= function () {
|
|
|
|
this.kurentoVideo = null;
|
|
|
|
this.kurentoScreenShare = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
KurentoManager.prototype.exitScreenShare = function () {
|
|
|
|
console.log("exitScreenShare");
|
|
|
|
if (this.kurentoScreenShare != null) {
|
2017-07-04 03:13:00 +08:00
|
|
|
if(kurentoHandler.pingInterval) {
|
|
|
|
clearInterval(kurentoHandler.pingInterval);
|
|
|
|
}
|
|
|
|
if(kurentoHandler.ws !== null) {
|
|
|
|
kurentoHandler.ws.onclose = function(){};
|
|
|
|
kurentoHandler.ws.close();
|
|
|
|
}
|
2017-06-14 04:18:21 +08:00
|
|
|
console.log('Hanging up kurentoScreenShare');
|
|
|
|
kurentoHandler.disposeScreenShare();
|
|
|
|
this.kurentoScreenShare = null;
|
|
|
|
kurentoHandler = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
KurentoManager.prototype.shareScreen = function (tag) {
|
|
|
|
console.log("shareScreen");
|
|
|
|
this.exitScreenShare();
|
|
|
|
var obj = Object.create(Kurento.prototype);
|
|
|
|
Kurento.apply(obj, arguments);
|
|
|
|
console.log(JSON.stringify(obj, null, 2));
|
|
|
|
this.kurentoScreenShare = obj;
|
|
|
|
kurentoHandler = obj;
|
|
|
|
this.kurentoScreenShare.setScreenShare(tag);
|
|
|
|
};
|
|
|
|
|
2017-07-11 04:51:37 +08:00
|
|
|
// Still unused, part of the HTMl5 implementation
|
2017-07-04 03:13:00 +08:00
|
|
|
KurentoManager.prototype.joinWatchVideo = function (tag) {
|
|
|
|
this.exitVideo();
|
|
|
|
var obj = Object.create(Kurento.prototype);
|
|
|
|
Kurento.apply(obj, arguments);
|
|
|
|
this.kurentoVideo = obj;
|
|
|
|
kurentoHandler = obj;
|
|
|
|
this.kurentoVideo.setWatchVideo(tag);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-14 04:18:21 +08:00
|
|
|
Kurento.prototype.setScreenShare = function (tag) {
|
|
|
|
console.log('setScreenShare ' + tag);
|
2017-07-04 03:13:00 +08:00
|
|
|
this.mediaCallback = this.makeShare;
|
2017-06-14 04:18:21 +08:00
|
|
|
this.create(tag);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.create = function (tag) {
|
|
|
|
this.setRenderTag(tag);
|
|
|
|
this.iceServers = true;
|
|
|
|
this.init();
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.init = function () {
|
|
|
|
var self = this;
|
|
|
|
if("WebSocket" in window) {
|
|
|
|
console.log("this browser supports websockets");
|
|
|
|
this.ws = new WebSocket(this.socketUrl);
|
|
|
|
console.log(JSON.stringify(this, null, 2));
|
|
|
|
|
|
|
|
this.ws.onmessage = this.onWSMessage;
|
|
|
|
this.ws.onclose = function (close) {
|
2017-07-06 01:58:19 +08:00
|
|
|
kurentoManager.exitScreenShare();
|
2017-06-14 04:18:21 +08:00
|
|
|
console.log("TODO WS onclose");
|
|
|
|
};
|
|
|
|
this.ws.onerror = function (error) {
|
2017-07-06 01:58:19 +08:00
|
|
|
kurentoManager.exitScreenShare();
|
2017-06-14 04:18:21 +08:00
|
|
|
console.log("TODO WS error");
|
|
|
|
};
|
|
|
|
this.ws.onopen = function() {
|
2017-07-04 03:13:00 +08:00
|
|
|
self.pingInterval = setInterval(self.ping, 3000);
|
|
|
|
self.mediaCallback();
|
2017-06-14 04:18:21 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
console.log("this browser does not support websockets");
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Kurento.prototype.doShare = function (screenConstraints) {
|
|
|
|
console.log("doShare");
|
|
|
|
var _this = this;
|
|
|
|
this.webRtcPeer.generateOffer(this.onOfferPresenter);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.onWSMessage = function (message) {
|
|
|
|
var parsedMessage = JSON.parse(message.data);
|
|
|
|
switch (parsedMessage.id) {
|
|
|
|
|
|
|
|
case 'presenterResponse':
|
|
|
|
kurentoHandler.presenterResponse(parsedMessage);
|
|
|
|
break;
|
|
|
|
case 'stopSharing':
|
2017-07-11 04:51:37 +08:00
|
|
|
kurentoManager.exitScreenShare();
|
2017-06-14 04:18:21 +08:00
|
|
|
break;
|
|
|
|
case 'iceCandidate':
|
2017-07-04 03:13:00 +08:00
|
|
|
kurentoHandler.webRtcPeer.addIceCandidate(parsedMessage.candidate);
|
|
|
|
break;
|
|
|
|
case 'pong':
|
|
|
|
break;
|
2017-06-14 04:18:21 +08:00
|
|
|
default:
|
|
|
|
console.error('Unrecognized message', parsedMessage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.setRenderTag = function (tag) {
|
|
|
|
this.renderTag = tag;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Kurento.prototype.presenterResponse = function (message) {
|
|
|
|
if (message.response != 'accepted') {
|
|
|
|
var errorMsg = message.message ? message.message : 'Unknow error';
|
|
|
|
console.warn('Call not accepted for the following reason: ' + errorMsg);
|
2017-07-11 04:51:37 +08:00
|
|
|
kurentoManager.exitScreenShare();
|
2017-06-14 04:18:21 +08:00
|
|
|
} else {
|
|
|
|
console.log("Presenter call was accepted with SDP => " + message.sdpAnswer);
|
|
|
|
this.webRtcPeer.processAnswer(message.sdpAnswer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.serverResponse = function (message) {
|
|
|
|
if (message.response != 'accepted') {
|
|
|
|
var errorMsg = message.message ? message.message : 'Unknow error';
|
|
|
|
console.warn('Call not accepted for the following reason: ' + errorMsg);
|
2017-07-11 04:51:37 +08:00
|
|
|
kurentoHandler.dispose();
|
2017-06-14 04:18:21 +08:00
|
|
|
} else {
|
|
|
|
this.webRtcPeer.processAnswer(message.sdpAnswer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.makeShare = function() {
|
|
|
|
var self = this;
|
|
|
|
console.log("Kurento.prototype.makeShare " + JSON.stringify(this.webRtcPeer, null, 2));
|
|
|
|
if (!this.webRtcPeer) {
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
onicecandidate : this.onIceCandidate
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Peer options " + JSON.stringify(options, null, 2));
|
|
|
|
|
|
|
|
kurentoHandler.startScreenStreamFrom();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.onOfferPresenter = function (error, offerSdp) {
|
|
|
|
if(error) {
|
|
|
|
console.log("Kurento.prototype.onOfferPresenter Error " + error);
|
|
|
|
//return onError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var message = {
|
|
|
|
id : 'presenter',
|
|
|
|
presenterId : kurentoHandler.sessid,
|
|
|
|
callerName : kurentoHandler.caller_id_name,
|
|
|
|
voiceBridge : kurentoHandler.destination_number,
|
2017-07-04 03:13:00 +08:00
|
|
|
sdpOffer : offerSdp,
|
|
|
|
vh: kurentoHandler.vid_height,
|
|
|
|
vw: kurentoHandler.vid_width
|
2017-06-14 04:18:21 +08:00
|
|
|
};
|
|
|
|
console.log("onOfferPresenter sending to screenshare server => " + JSON.stringify(message, null, 2));
|
|
|
|
kurentoHandler.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.startScreenStreamFrom = function () {
|
2017-07-04 03:13:00 +08:00
|
|
|
console.log("Kurento.startScreenStreamFrom");
|
2017-06-14 04:18:21 +08:00
|
|
|
var screenInfo = null;
|
2017-07-04 03:13:00 +08:00
|
|
|
var _this = this;
|
|
|
|
if (!!window.chrome) {
|
2017-06-14 04:18:21 +08:00
|
|
|
if (!_this.chromeExtension) {
|
|
|
|
_this.logError({
|
|
|
|
status: 'failed',
|
|
|
|
message: 'Missing Chrome Extension key',
|
|
|
|
});
|
|
|
|
_this.onFail();
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 03:13:00 +08:00
|
|
|
}
|
|
|
|
// TODO it would be nice to check those constraints
|
|
|
|
_this.screenConstraints.video = {};
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
//localVideo: this.renderTag,
|
|
|
|
onicecandidate : _this.onIceCandidate,
|
|
|
|
mediaConstraints : _this.screenConstraints,
|
|
|
|
sendSource : 'desktop'
|
|
|
|
};
|
2017-06-14 04:18:21 +08:00
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
console.log(" Peer options => " + JSON.stringify(options, null, 2));
|
2017-06-14 04:18:21 +08:00
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
_this.webRtcPeer = kurentoUtils.WebRtcPeer.WebRtcPeerSendonly(options, function(error) {
|
|
|
|
if(error) {
|
|
|
|
console.log("WebRtcPeerSendonly constructor error " + JSON.stringify(error, null, 2));
|
|
|
|
//return onError(error);
|
|
|
|
return kurentoManager.exitScreenShare();
|
|
|
|
}
|
|
|
|
console.log("Generated peer offer w/ options " + JSON.stringify(options));
|
|
|
|
_this.doShare(constraints);
|
|
|
|
});
|
2017-06-14 04:18:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.onIceCandidate = function(candidate) {
|
|
|
|
console.log('Local candidate' + JSON.stringify(candidate));
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
id : 'onIceCandidate',
|
|
|
|
presenterId : kurentoHandler.sessid,
|
|
|
|
candidate : candidate
|
|
|
|
}
|
|
|
|
console.log("this object " + JSON.stringify(this, null, 2));
|
|
|
|
kurentoHandler.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
Kurento.prototype.setWatchVideo = function (tag) {
|
|
|
|
this.useVideo = true;
|
|
|
|
this.useCamera = 'none';
|
|
|
|
this.useMic = 'none';
|
|
|
|
this.mediaCallback = this.viewer;
|
|
|
|
this.create(tag);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.viewer = function () {
|
|
|
|
var self = this;
|
|
|
|
if (!this.webRtcPeer) {
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
remoteVideo: this.renderTag,
|
|
|
|
onicecandidate : onIceCandidate
|
|
|
|
}
|
|
|
|
|
|
|
|
webRtcPeer = kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly(options, function(error) {
|
|
|
|
if(error) return onError(error);
|
|
|
|
|
|
|
|
this.generateOffer(onOfferViewer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.onOfferViewer = function (error, offerSdp) {
|
|
|
|
if(error) {
|
|
|
|
console.log("Kurento.prototype.onOfferViewer Error " + error);
|
|
|
|
//return onError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var message = {
|
|
|
|
id : 'viewer',
|
|
|
|
presenterId : kurentoHandler.sessid,
|
|
|
|
callerName : kurentoHandler.caller_id_name,
|
|
|
|
voiceBridge : kurentoHandler.destination_number,
|
|
|
|
sdpOffer : offerSdp
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log("onOfferViewer sending to screenshare server => " + JSON.stringify(message, null, 2));
|
|
|
|
kurentoHandler.sendMessage(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.ping = function() {
|
|
|
|
var message = {
|
|
|
|
id : 'ping',
|
|
|
|
presenterId : kurentoHandler.sessid,
|
|
|
|
callerName : kurentoHandler.caller_id_name,
|
|
|
|
voiceBridge : kurentoHandler.destination_number,
|
|
|
|
};
|
|
|
|
|
|
|
|
kurentoHandler.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2017-06-14 04:18:21 +08:00
|
|
|
Kurento.prototype.stop = function() {
|
|
|
|
if (this.webRtcPeer) {
|
|
|
|
var message = {
|
|
|
|
id : 'stop',
|
|
|
|
presenterId : kurentoHandler.sessId,
|
|
|
|
}
|
|
|
|
kurentoHandler.sendMessage(message);
|
2017-07-11 04:51:37 +08:00
|
|
|
kurentoHandler.disposeScreenShare();
|
2017-06-14 04:18:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.dispose = function() {
|
|
|
|
if (this.webRtcPeer) {
|
|
|
|
this.webRtcPeer.dispose();
|
|
|
|
this.webRtcPeer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.disposeScreenShare = function() {
|
|
|
|
if (this.webRtcPeer) {
|
|
|
|
this.webRtcPeer.dispose();
|
|
|
|
this.webRtcPeer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.sendMessage = function(message) {
|
|
|
|
var jsonMessage = JSON.stringify(message);
|
|
|
|
console.log('Sending message: ' + jsonMessage);
|
|
|
|
this.ws.send(jsonMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
Kurento.prototype.logger = function (obj) {
|
|
|
|
console.log(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kurento.prototype.logError = function (obj) {
|
|
|
|
console.error(obj);
|
|
|
|
};
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
Kurento.prototype.getChromeScreenConstraints = function(callback, extensionId) {
|
2017-06-14 04:18:21 +08:00
|
|
|
chrome.runtime.sendMessage(extensionId, {
|
|
|
|
getStream: true,
|
|
|
|
sources: [
|
|
|
|
"window",
|
|
|
|
"screen",
|
|
|
|
"tab"
|
|
|
|
]},
|
|
|
|
function(response) {
|
|
|
|
console.log(response);
|
|
|
|
callback(response);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Global methods */
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
// this function explains how to use above methods/objects
|
2017-06-14 04:18:21 +08:00
|
|
|
window.getScreenConstraints = function(sendSource, callback) {
|
2017-07-04 03:13:00 +08:00
|
|
|
var _this = this;
|
|
|
|
var chromeMediaSourceId = sendSource;
|
|
|
|
if(isChrome) {
|
|
|
|
kurentoHandler.getChromeScreenConstraints (function (constraints) {
|
|
|
|
|
|
|
|
var sourceId = constraints.streamId;
|
|
|
|
|
|
|
|
// this statement sets gets 'sourceId" and sets "chromeMediaSourceId"
|
|
|
|
kurentoHandler.screenConstraints.video.chromeMediaSource = { exact: [sendSource]};
|
|
|
|
kurentoHandler.screenConstraints.video.chromeMediaSourceId= sourceId;
|
2017-07-06 01:58:19 +08:00
|
|
|
console.log("getScreenConstraints for Chrome returns => " +JSON.stringify(kurentoHandler.screenConstraints, null, 2));
|
2017-07-04 03:13:00 +08:00
|
|
|
// now invoking native getUserMedia API
|
|
|
|
callback(null, kurentoHandler.screenConstraints);
|
|
|
|
|
|
|
|
}, kurentoHandler.chromeExtension);
|
|
|
|
}
|
|
|
|
else if (isFirefox) {
|
|
|
|
kurentoHandler.screenConstraints.video.mediaSource= "screen";
|
|
|
|
kurentoHandler.screenConstraints.video.width= {max: kurentoHandler.vid_width};
|
|
|
|
kurentoHandler.screenConstraints.video.height = {max: kurentoHandler.vid_height};
|
|
|
|
|
2017-07-06 01:58:19 +08:00
|
|
|
console.log("getScreenConstraints for Firefox returns => " +JSON.stringify(kurentoHandler.screenConstraints, null, 2));
|
2017-07-04 03:13:00 +08:00
|
|
|
// now invoking native getUserMedia API
|
|
|
|
callback(null, kurentoHandler.screenConstraints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.kurentoInitialize = function () {
|
|
|
|
if (window.kurentoManager == null || window.KurentoManager == undefined) {
|
|
|
|
window.kurentoManager = new KurentoManager();
|
|
|
|
}
|
2017-06-14 04:18:21 +08:00
|
|
|
};
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
window.kurentoShareScreen = function() {
|
|
|
|
window.kurentoInitialize();
|
|
|
|
window.kurentoManager.shareScreen.apply(window.kurentoManager, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-14 04:18:21 +08:00
|
|
|
window.kurentoExitScreenShare = function () {
|
|
|
|
window.kurentoInitialize();
|
|
|
|
window.kurentoManager.exitScreenShare();
|
|
|
|
};
|
|
|
|
|
2017-07-04 03:13:00 +08:00
|
|
|
window.kurentoWatchVideo = function () {
|
|
|
|
window.kurentoInitialize();
|
|
|
|
window.kurentoManager.joinWatchVideo.apply(window.kurentoManager, arguments);
|
|
|
|
};
|