2015-03-18 19:27:04 +00:00
|
|
|
/* global $, alert, APP, changeLocalVideo, chrome, config, getConferenceHandler,
|
|
|
|
getUserMediaWithConstraints */
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
|
|
|
* Indicates that desktop stream is currently in use(for toggle purpose).
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var isUsingScreenStream = false;
|
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* Indicates that switch stream operation is in progress and prevent from
|
|
|
|
* triggering new events.
|
2014-03-13 12:22:54 +00:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var switchInProgress = false;
|
|
|
|
|
2014-03-17 09:02:40 +00:00
|
|
|
/**
|
|
|
|
* Method used to get screen sharing stream.
|
|
|
|
*
|
2014-04-13 12:30:47 +00:00
|
|
|
* @type {function (stream_callback, failure_callback}
|
2014-03-17 09:02:40 +00:00
|
|
|
*/
|
2014-03-17 13:00:47 +00:00
|
|
|
var obtainDesktopStream = null;
|
2014-03-17 09:02:40 +00:00
|
|
|
|
2014-03-26 11:32:42 +00:00
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* Indicates whether desktop sharing extension is installed.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var extInstalled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether update of desktop sharing extension is required.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var extUpdateRequired = false;
|
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
var AdapterJS = require("../RTC/adapter.screenshare");
|
2014-03-26 11:32:42 +00:00
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
var EventEmitter = require("events");
|
|
|
|
|
|
|
|
var eventEmitter = new EventEmitter();
|
|
|
|
|
2015-03-18 19:27:04 +00:00
|
|
|
var DesktopSharingEventTypes
|
|
|
|
= require("../../service/desktopsharing/DesktopSharingEventTypes");
|
2015-01-28 14:35:22 +00:00
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
var RTCBrowserType = require("../RTC/RTCBrowserType");
|
|
|
|
|
|
|
|
var RTCEvents = require("../../service/RTC/RTCEvents");
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
/**
|
|
|
|
* Method obtains desktop stream from WebRTC 'screen' source.
|
|
|
|
* Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
|
|
|
|
*/
|
|
|
|
function obtainWebRTCScreen(streamCallback, failCallback) {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.getUserMediaWithConstraints(
|
2014-04-13 12:30:47 +00:00
|
|
|
['screen'],
|
|
|
|
streamCallback,
|
|
|
|
failCallback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs inline install URL for Chrome desktop streaming extension.
|
|
|
|
* The 'chromeExtensionId' must be defined in config.js.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getWebStoreInstallUrl()
|
|
|
|
{
|
2015-03-18 19:27:04 +00:00
|
|
|
return "https://chrome.google.com/webstore/detail/" +
|
|
|
|
config.chromeExtensionId;
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether extension update is required.
|
|
|
|
* @param minVersion minimal required version
|
|
|
|
* @param extVersion current extension version
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isUpdateRequired(minVersion, extVersion)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var s1 = minVersion.split('.');
|
|
|
|
var s2 = extVersion.split('.');
|
|
|
|
|
|
|
|
var len = Math.max(s1.length, s2.length);
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var n1 = 0,
|
|
|
|
n2 = 0;
|
|
|
|
|
|
|
|
if (i < s1.length)
|
|
|
|
n1 = parseInt(s1[i]);
|
|
|
|
if (i < s2.length)
|
|
|
|
n2 = parseInt(s2[i]);
|
|
|
|
|
|
|
|
if (isNaN(n1) || isNaN(n2))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (n1 !== n2)
|
|
|
|
{
|
|
|
|
return n1 > n2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// will happen if boths version has identical numbers in
|
|
|
|
// their components (even if one of them is longer, has more components)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
console.error("Failed to parse extension version", e);
|
2015-02-26 15:35:35 +00:00
|
|
|
APP.UI.messageHandler.showError("dialog.error",
|
|
|
|
"dialog.detectext");
|
2014-04-13 12:30:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
function checkChromeExtInstalled(callback) {
|
2014-04-13 12:30:47 +00:00
|
|
|
if (!chrome.runtime) {
|
|
|
|
// No API, so no extension for sure
|
2015-03-18 19:27:04 +00:00
|
|
|
callback(false, false);
|
2014-05-13 10:09:39 +00:00
|
|
|
return;
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
chrome.runtime.sendMessage(
|
|
|
|
config.chromeExtensionId,
|
|
|
|
{ getVersion: true },
|
|
|
|
function (response) {
|
|
|
|
if (!response || !response.version) {
|
|
|
|
// Communication failure - assume that no endpoint exists
|
2015-03-18 19:27:04 +00:00
|
|
|
console.warn(
|
|
|
|
"Extension not installed?: ", chrome.runtime.lastError);
|
|
|
|
callback(false, false);
|
|
|
|
return;
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
2015-03-18 19:27:04 +00:00
|
|
|
// Check installed extension version
|
|
|
|
var extVersion = response.version;
|
|
|
|
console.log('Extension version is: ' + extVersion);
|
|
|
|
var updateRequired
|
|
|
|
= isUpdateRequired(config.minChromeExtVersion, extVersion);
|
|
|
|
callback(!updateRequired, updateRequired);
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doGetStreamFromExtension(streamCallback, failCallback) {
|
2015-03-18 19:27:04 +00:00
|
|
|
// Sends 'getStream' msg to the extension.
|
|
|
|
// Extension id must be defined in the config.
|
2014-04-13 12:30:47 +00:00
|
|
|
chrome.runtime.sendMessage(
|
|
|
|
config.chromeExtensionId,
|
2014-09-09 07:05:42 +00:00
|
|
|
{ getStream: true, sources: config.desktopSharingSources },
|
2014-04-13 12:30:47 +00:00
|
|
|
function (response) {
|
|
|
|
if (!response) {
|
|
|
|
failCallback(chrome.runtime.lastError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Response from extension: " + response);
|
|
|
|
if (response.streamId) {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.getUserMediaWithConstraints(
|
2014-04-13 12:30:47 +00:00
|
|
|
['desktop'],
|
|
|
|
function (stream) {
|
|
|
|
streamCallback(stream);
|
|
|
|
},
|
|
|
|
failCallback,
|
|
|
|
null, null, null,
|
|
|
|
response.streamId);
|
|
|
|
} else {
|
|
|
|
failCallback("Extension failed to get the stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop'
|
|
|
|
* stream for returned stream token.
|
2014-04-13 12:30:47 +00:00
|
|
|
*/
|
|
|
|
function obtainScreenFromExtension(streamCallback, failCallback) {
|
2015-03-18 19:27:04 +00:00
|
|
|
if (extInstalled) {
|
|
|
|
doGetStreamFromExtension(streamCallback, failCallback);
|
|
|
|
} else {
|
|
|
|
if (extUpdateRequired) {
|
|
|
|
alert(
|
|
|
|
'Jitsi Desktop Streamer requires update. ' +
|
|
|
|
'Changes will take effect after next Chrome restart.');
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
2015-03-18 19:27:04 +00:00
|
|
|
|
|
|
|
chrome.webstore.install(
|
|
|
|
getWebStoreInstallUrl(),
|
|
|
|
function (arg) {
|
|
|
|
console.log("Extension installed successfully", arg);
|
2015-06-17 08:11:30 +00:00
|
|
|
extInstalled = true;
|
|
|
|
// We need to give a moment for the endpoint to become available
|
|
|
|
window.setTimeout(function () {
|
|
|
|
doGetStreamFromExtension(streamCallback, failCallback);
|
|
|
|
}, 500);
|
2015-03-18 19:27:04 +00:00
|
|
|
},
|
|
|
|
function (arg) {
|
|
|
|
console.log("Failed to install the extension", arg);
|
|
|
|
failCallback(arg);
|
|
|
|
APP.UI.messageHandler.showError("dialog.error",
|
|
|
|
"dialog.failtoinstall");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 09:02:40 +00:00
|
|
|
/**
|
|
|
|
* Call this method to toggle desktop sharing feature.
|
2015-03-18 19:27:04 +00:00
|
|
|
* @param method pass "ext" to use chrome extension for desktop capture(chrome
|
|
|
|
* extension required), pass "webrtc" to use WebRTC "screen" desktop
|
|
|
|
* source('chrome://flags/#enable-usermedia-screen-capture' must be
|
|
|
|
* enabled), pass any other string or nothing in order to disable this
|
|
|
|
* feature completely.
|
2014-03-17 09:02:40 +00:00
|
|
|
*/
|
|
|
|
function setDesktopSharing(method) {
|
2015-07-10 09:57:20 +00:00
|
|
|
|
|
|
|
obtainDesktopStream = null;
|
|
|
|
|
|
|
|
// When TemasysWebRTC plugin is used we always use getUserMedia, so we don't
|
|
|
|
// care about 'method' parameter
|
|
|
|
if (RTCBrowserType.isTemasysPluginUsed()) {
|
|
|
|
if (!AdapterJS.WebRTCPlugin.plugin.HasScreensharingFeature) {
|
|
|
|
console.info("Screensharing not supported by this plugin version");
|
|
|
|
} else if (!AdapterJS.WebRTCPlugin.plugin.isScreensharingAvailable) {
|
|
|
|
console.info(
|
|
|
|
"Screensharing not available with Temasys plugin on this site");
|
|
|
|
} else {
|
|
|
|
obtainDesktopStream = obtainWebRTCScreen;
|
|
|
|
console.info("Using Temasys plugin for desktop sharing");
|
|
|
|
}
|
|
|
|
} else if (RTCBrowserType.isChrome()) {
|
|
|
|
if (method == "ext") {
|
|
|
|
if (RTCBrowserType.getChromeVersion() >= 34) {
|
|
|
|
obtainDesktopStream = obtainScreenFromExtension;
|
|
|
|
console.info("Using Chrome extension for desktop sharing");
|
|
|
|
initChromeExtension();
|
|
|
|
} else {
|
|
|
|
console.info("Chrome extension not supported until ver 34");
|
|
|
|
}
|
|
|
|
} else if (method == "webrtc") {
|
|
|
|
obtainDesktopStream = obtainWebRTCScreen;
|
|
|
|
console.info("Using Chrome WebRTC for desktop sharing");
|
|
|
|
}
|
2014-03-17 09:02:40 +00:00
|
|
|
}
|
2014-03-26 11:32:42 +00:00
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
if (!obtainDesktopStream) {
|
|
|
|
console.info("Desktop sharing disabled");
|
|
|
|
}
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:19:11 +00:00
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* Initializes <link rel=chrome-webstore-item /> with extension id set in
|
|
|
|
* config.js to support inline installs. Host site must be selected as main
|
|
|
|
* website of published extension.
|
2014-04-01 11:19:11 +00:00
|
|
|
*/
|
|
|
|
function initInlineInstalls()
|
|
|
|
{
|
|
|
|
$("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
function initChromeExtension() {
|
|
|
|
// Initialize Chrome extension inline installs
|
|
|
|
initInlineInstalls();
|
|
|
|
// Check if extension is installed
|
|
|
|
checkChromeExtInstalled(function (installed, updateRequired) {
|
|
|
|
extInstalled = installed;
|
|
|
|
extUpdateRequired = updateRequired;
|
|
|
|
console.info(
|
|
|
|
"Chrome extension installed: " + extInstalled +
|
|
|
|
" updateRequired: " + extUpdateRequired);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-27 14:23:48 +00:00
|
|
|
function getVideoStreamFailed(error) {
|
2014-04-13 12:30:47 +00:00
|
|
|
console.error("Failed to obtain the stream to switch to", error);
|
|
|
|
switchInProgress = false;
|
2015-03-24 15:43:33 +00:00
|
|
|
isUsingScreenStream = false;
|
|
|
|
newStreamCreated(null);
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 14:23:48 +00:00
|
|
|
function getDesktopStreamFailed(error) {
|
|
|
|
console.error("Failed to obtain the stream to switch to", error);
|
|
|
|
switchInProgress = false;
|
|
|
|
}
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
function streamSwitchDone() {
|
|
|
|
switchInProgress = false;
|
2015-01-13 13:11:05 +00:00
|
|
|
eventEmitter.emit(
|
|
|
|
DesktopSharingEventTypes.SWITCHING_DONE,
|
|
|
|
isUsingScreenStream);
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
function newStreamCreated(stream)
|
|
|
|
{
|
|
|
|
eventEmitter.emit(DesktopSharingEventTypes.NEW_STREAM_CREATED,
|
|
|
|
stream, isUsingScreenStream, streamSwitchDone);
|
|
|
|
}
|
2014-04-13 12:30:47 +00:00
|
|
|
|
2015-07-10 09:57:20 +00:00
|
|
|
function onEndedHandler(stream) {
|
|
|
|
if (!switchInProgress && isUsingScreenStream) {
|
|
|
|
APP.desktopsharing.toggleScreenSharing();
|
|
|
|
}
|
|
|
|
//FIXME: to be verified
|
|
|
|
if (stream.removeEventListener) {
|
|
|
|
stream.removeEventListener('ended', onEndedHandler);
|
|
|
|
} else {
|
|
|
|
stream.detachEvent('ended', onEndedHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when RTC finishes initialization
|
|
|
|
function onWebRtcReady() {
|
|
|
|
|
|
|
|
setDesktopSharing(config.desktopSharing);
|
|
|
|
|
|
|
|
eventEmitter.emit(DesktopSharingEventTypes.INIT);
|
|
|
|
}
|
2014-04-13 12:30:47 +00:00
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
module.exports = {
|
|
|
|
isUsingScreenStream: function () {
|
|
|
|
return isUsingScreenStream;
|
|
|
|
},
|
2014-06-12 18:35:42 +00:00
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* @returns {boolean} <tt>true</tt> if desktop sharing feature is available
|
|
|
|
* and enabled.
|
2015-01-13 13:11:05 +00:00
|
|
|
*/
|
2015-07-10 09:57:20 +00:00
|
|
|
isDesktopSharingEnabled: function () { return !!obtainDesktopStream; },
|
2015-01-13 13:11:05 +00:00
|
|
|
|
|
|
|
init: function () {
|
2015-07-10 09:57:20 +00:00
|
|
|
APP.RTC.addListener(RTCEvents.RTC_READY, onWebRtcReady);
|
2015-01-13 13:11:05 +00:00
|
|
|
},
|
2014-03-13 12:22:54 +00:00
|
|
|
|
2015-03-18 19:27:04 +00:00
|
|
|
addListener: function (listener, type)
|
2014-03-13 12:22:54 +00:00
|
|
|
{
|
2015-01-13 13:11:05 +00:00
|
|
|
eventEmitter.on(type, listener);
|
|
|
|
},
|
|
|
|
|
2015-03-18 19:27:04 +00:00
|
|
|
removeListener: function (listener, type) {
|
2015-01-13 13:11:05 +00:00
|
|
|
eventEmitter.removeListener(type, listener);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Toggles screen sharing.
|
|
|
|
*/
|
|
|
|
toggleScreenSharing: function () {
|
|
|
|
if (switchInProgress || !obtainDesktopStream) {
|
|
|
|
console.warn("Switch in progress or no method defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switchInProgress = true;
|
|
|
|
|
|
|
|
if (!isUsingScreenStream)
|
|
|
|
{
|
|
|
|
// Switch to desktop stream
|
|
|
|
obtainDesktopStream(
|
|
|
|
function (stream) {
|
|
|
|
// We now use screen stream
|
|
|
|
isUsingScreenStream = true;
|
2015-03-18 19:27:04 +00:00
|
|
|
// Hook 'ended' event to restore camera
|
|
|
|
// when screen stream stops
|
2015-07-10 09:57:20 +00:00
|
|
|
//FIXME: to be verified
|
|
|
|
if (stream.addEventListener) {
|
|
|
|
stream.addEventListener('ended', function () {
|
|
|
|
onEndedHandler(stream);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
stream.attachEvent('ended', function () {
|
|
|
|
onEndedHandler(stream);
|
|
|
|
});
|
|
|
|
}
|
2015-01-13 13:11:05 +00:00
|
|
|
newStreamCreated(stream);
|
|
|
|
},
|
2015-03-27 14:23:48 +00:00
|
|
|
getDesktopStreamFailed);
|
2015-01-13 13:11:05 +00:00
|
|
|
} else {
|
|
|
|
// Disable screen stream
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.getUserMediaWithConstraints(
|
2015-01-13 13:11:05 +00:00
|
|
|
['video'],
|
|
|
|
function (stream) {
|
|
|
|
// We are now using camera stream
|
|
|
|
isUsingScreenStream = false;
|
|
|
|
newStreamCreated(stream);
|
|
|
|
},
|
2015-03-27 14:23:48 +00:00
|
|
|
getVideoStreamFailed, config.resolution || '360'
|
2015-01-13 13:11:05 +00:00
|
|
|
);
|
|
|
|
}
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
2015-01-13 13:11:05 +00:00
|
|
|
};
|
2014-03-13 12:22:54 +00:00
|
|
|
|