2014-06-12 18:35:42 +00:00
|
|
|
/* global $, config, connection, chrome, alert, getUserMediaWithConstraints, changeLocalVideo, getConferenceHandler */
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
|
|
|
* Indicates that desktop stream is currently in use(for toggle purpose).
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var isUsingScreenStream = false;
|
|
|
|
/**
|
|
|
|
* Indicates that switch stream operation is in progress and prevent from triggering new events.
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Flag used to cache desktop sharing enabled state. Do not use directly as it can be <tt>null</tt>.
|
|
|
|
* @type {null|boolean}
|
|
|
|
*/
|
|
|
|
var _desktopSharingEnabled = null;
|
|
|
|
|
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) {
|
|
|
|
getUserMediaWithConstraints(
|
|
|
|
['screen'],
|
|
|
|
streamCallback,
|
|
|
|
failCallback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs inline install URL for Chrome desktop streaming extension.
|
|
|
|
* The 'chromeExtensionId' must be defined in config.js.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getWebStoreInstallUrl()
|
|
|
|
{
|
|
|
|
return "https://chrome.google.com/webstore/detail/" + config.chromeExtensionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2014-08-14 15:29:28 +00:00
|
|
|
messageHandler.showError('Error',
|
|
|
|
'Error when trying to detect desktopsharing extension.');
|
2014-04-13 12:30:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function checkExtInstalled(isInstalledCallback) {
|
|
|
|
if (!chrome.runtime) {
|
|
|
|
// No API, so no extension for sure
|
|
|
|
isInstalledCallback(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
|
|
|
|
console.warn("Extension not installed?: " + chrome.runtime.lastError);
|
|
|
|
isInstalledCallback(false);
|
|
|
|
} else {
|
|
|
|
// Check installed extension version
|
|
|
|
var extVersion = response.version;
|
|
|
|
console.log('Extension version is: ' + extVersion);
|
|
|
|
var updateRequired = isUpdateRequired(config.minChromeExtVersion, extVersion);
|
|
|
|
if (updateRequired) {
|
|
|
|
alert(
|
|
|
|
'Jitsi Desktop Streamer requires update. ' +
|
|
|
|
'Changes will take effect after next Chrome restart.');
|
|
|
|
}
|
|
|
|
isInstalledCallback(!updateRequired);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doGetStreamFromExtension(streamCallback, failCallback) {
|
|
|
|
// Sends 'getStream' msg to the extension. Extension id must be defined in the config.
|
|
|
|
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) {
|
|
|
|
getUserMediaWithConstraints(
|
|
|
|
['desktop'],
|
|
|
|
function (stream) {
|
|
|
|
streamCallback(stream);
|
|
|
|
},
|
|
|
|
failCallback,
|
|
|
|
null, null, null,
|
|
|
|
response.streamId);
|
|
|
|
} else {
|
|
|
|
failCallback("Extension failed to get the stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
|
|
|
|
*/
|
|
|
|
function obtainScreenFromExtension(streamCallback, failCallback) {
|
|
|
|
checkExtInstalled(
|
|
|
|
function (isInstalled) {
|
|
|
|
if (isInstalled) {
|
|
|
|
doGetStreamFromExtension(streamCallback, failCallback);
|
|
|
|
} else {
|
|
|
|
chrome.webstore.install(
|
|
|
|
getWebStoreInstallUrl(),
|
|
|
|
function (arg) {
|
|
|
|
console.log("Extension installed successfully", arg);
|
|
|
|
// We need to reload the page in order to get the access to chrome.runtime
|
|
|
|
window.location.reload(false);
|
|
|
|
},
|
|
|
|
function (arg) {
|
|
|
|
console.log("Failed to install the extension", arg);
|
|
|
|
failCallback(arg);
|
2014-08-14 15:29:28 +00:00
|
|
|
messageHandler.showError('Error',
|
|
|
|
'Failed to install desktop sharing extension');
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
|
|
|
* @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
|
|
|
|
*/
|
|
|
|
function isDesktopSharingEnabled() {
|
2014-04-13 12:30:47 +00:00
|
|
|
if (_desktopSharingEnabled === null) {
|
|
|
|
if (obtainDesktopStream === obtainScreenFromExtension) {
|
2014-03-26 11:32:42 +00:00
|
|
|
// Parse chrome version
|
|
|
|
var userAgent = navigator.userAgent.toLowerCase();
|
|
|
|
// We can assume that user agent is chrome, because it's enforced when 'ext' streaming method is set
|
|
|
|
var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
|
|
|
|
console.log("Chrome version" + userAgent, ver);
|
2014-04-09 13:37:12 +00:00
|
|
|
_desktopSharingEnabled = ver >= 34;
|
2014-03-26 11:32:42 +00:00
|
|
|
} else {
|
|
|
|
_desktopSharingEnabled = obtainDesktopStream === obtainWebRTCScreen;
|
|
|
|
}
|
2014-03-17 13:00:47 +00:00
|
|
|
}
|
2014-03-26 11:32:42 +00:00
|
|
|
return _desktopSharingEnabled;
|
2014-03-17 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
function showDesktopSharingButton() {
|
|
|
|
if (isDesktopSharingEnabled()) {
|
|
|
|
$('#desktopsharing').css({display: "inline"});
|
|
|
|
} else {
|
|
|
|
$('#desktopsharing').css({display: "none"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 09:02:40 +00:00
|
|
|
/**
|
|
|
|
* Call this method to toggle desktop sharing feature.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
function setDesktopSharing(method) {
|
2014-03-26 11:32:42 +00:00
|
|
|
// Check if we are running chrome
|
2014-04-13 12:30:47 +00:00
|
|
|
if (!navigator.webkitGetUserMedia) {
|
2014-03-17 09:02:40 +00:00
|
|
|
obtainDesktopStream = null;
|
2014-03-17 15:43:06 +00:00
|
|
|
console.info("Desktop sharing disabled");
|
2014-04-13 12:30:47 +00:00
|
|
|
} else if (method == "ext") {
|
2014-03-26 11:32:42 +00:00
|
|
|
obtainDesktopStream = obtainScreenFromExtension;
|
|
|
|
console.info("Using Chrome extension for desktop sharing");
|
2014-04-13 12:30:47 +00:00
|
|
|
} else if (method == "webrtc") {
|
2014-03-26 11:32:42 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Reset enabled cache
|
|
|
|
_desktopSharingEnabled = null;
|
|
|
|
|
2014-03-17 09:02:40 +00:00
|
|
|
showDesktopSharingButton();
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:19:11 +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.
|
|
|
|
*/
|
|
|
|
function initInlineInstalls()
|
|
|
|
{
|
|
|
|
$("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
|
|
|
|
}
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
function getSwitchStreamFailed(error) {
|
|
|
|
console.error("Failed to obtain the stream to switch to", error);
|
|
|
|
switchInProgress = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function streamSwitchDone() {
|
|
|
|
//window.setTimeout(
|
|
|
|
// function () {
|
|
|
|
switchInProgress = false;
|
2014-09-24 12:47:26 +00:00
|
|
|
Toolbar.changeDesktopSharingButtonState(isUsingScreenStream);
|
2014-04-13 12:30:47 +00:00
|
|
|
// }, 100
|
|
|
|
//);
|
|
|
|
}
|
|
|
|
|
|
|
|
function newStreamCreated(stream) {
|
|
|
|
|
|
|
|
var oldStream = connection.jingle.localVideo;
|
|
|
|
|
2014-06-12 18:35:42 +00:00
|
|
|
connection.jingle.localVideo = stream;
|
|
|
|
|
|
|
|
VideoLayout.changeLocalVideo(stream, !isUsingScreenStream);
|
2014-04-13 12:30:47 +00:00
|
|
|
|
|
|
|
var conferenceHandler = getConferenceHandler();
|
|
|
|
if (conferenceHandler) {
|
|
|
|
// FIXME: will block switchInProgress on true value in case of exception
|
|
|
|
conferenceHandler.switchStreams(stream, oldStream, streamSwitchDone);
|
|
|
|
} else {
|
|
|
|
// We are done immediately
|
|
|
|
console.error("No conference handler");
|
2014-08-14 15:29:28 +00:00
|
|
|
messageHandler.showError('Error',
|
|
|
|
'Unable to switch video stream.');
|
2014-04-13 12:30:47 +00:00
|
|
|
streamSwitchDone();
|
|
|
|
}
|
2014-04-01 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 12:22:54 +00:00
|
|
|
/*
|
|
|
|
* Toggles screen sharing.
|
|
|
|
*/
|
|
|
|
function toggleScreenSharing() {
|
2014-03-17 15:43:06 +00:00
|
|
|
if (switchInProgress || !obtainDesktopStream) {
|
|
|
|
console.warn("Switch in progress or no method defined");
|
2014-03-13 12:22:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switchInProgress = true;
|
|
|
|
|
|
|
|
// Only the focus is able to set a shared key.
|
2014-04-13 12:30:47 +00:00
|
|
|
if (!isUsingScreenStream)
|
2014-03-13 12:22:54 +00:00
|
|
|
{
|
2014-03-17 09:02:40 +00:00
|
|
|
obtainDesktopStream(
|
2014-04-13 12:30:47 +00:00
|
|
|
function (stream) {
|
2014-03-17 09:02:40 +00:00
|
|
|
// We now use screen stream
|
2014-03-13 12:22:54 +00:00
|
|
|
isUsingScreenStream = true;
|
2014-03-17 09:02:40 +00:00
|
|
|
// Hook 'ended' event to restore camera when screen stream stops
|
|
|
|
stream.addEventListener('ended',
|
2014-04-13 12:30:47 +00:00
|
|
|
function (e) {
|
|
|
|
if (!switchInProgress && isUsingScreenStream) {
|
2014-03-17 09:02:40 +00:00
|
|
|
toggleScreenSharing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
newStreamCreated(stream);
|
2014-03-13 12:22:54 +00:00
|
|
|
},
|
2014-04-13 12:30:47 +00:00
|
|
|
getSwitchStreamFailed);
|
2014-03-13 12:22:54 +00:00
|
|
|
} else {
|
|
|
|
// Disable screen stream
|
|
|
|
getUserMediaWithConstraints(
|
|
|
|
['video'],
|
2014-04-13 12:30:47 +00:00
|
|
|
function (stream) {
|
2014-03-17 09:02:40 +00:00
|
|
|
// We are now using camera stream
|
2014-03-13 12:22:54 +00:00
|
|
|
isUsingScreenStream = false;
|
2014-03-17 09:02:40 +00:00
|
|
|
newStreamCreated(stream);
|
2014-03-13 12:22:54 +00:00
|
|
|
},
|
|
|
|
getSwitchStreamFailed, config.resolution || '360'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|