Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import adapter from 'webrtc-adapter';
import Webcom from 'webcom/webcom';
/**
* The current browser's infos
* @typedef {Object} Browser
* @property {string} name - current browser's name
* @property {number} [version] - current browser's version
* @property {number} [minVersion] - current browser's minimum supported version
* @property {boolean} compatible - is current browser supported ?
*/
const browser = Object.assign({
compatible: !/\s/.test(adapter.browserDetails.browser)
&& (!adapter.browserDetails.minVersion
|| adapter.browserDetails.version >= adapter.browserDetails.minVersion)
}, adapter.browserDetails);
const version = `Reach v${SDK_VERSION} | Schema ${SCHEMA_VERSION} | Webcom v${Webcom.SDK_VERSION}`; // eslint-disable-line no-undef
let message = '';
if (browser.version === null) {
message = 'Unsupported browser: browser must support WebRTC Peer-to-peer connections to use Reach';
} else if (browser.minVersion && browser.minVersion > browser.version) {
message = `Unsupported browser: browser is outdated, update to latest version (${browser.minVersion}+)`;
} else if (!adapter.browserShim) {
message = 'No shim for your browser. There might a problem with your package.';
}
// Log browser status & SDKs versions on load
let args = [`${version} ${message}`];
if (/^(chrome|firefox)$/.test(browser.browser)) {
connection.setRemoteDescription(rtcSdp).then(function () {
//use in edge for ice
if (adapter.browserDetails.browser == "edge") {
// var sdpArray = sdp.split("\n");
// var i;
// for (i = 0; i < sdpArray.length; i++) {
// if (sdpArray[i].indexOf("m=video") == 0) {
// break;
// }
// if (sdpArray[i].indexOf("a=candidate:1 1") == 0 || sdpArray[i].indexOf("a=candidate:2 1") == 0) {
// var rtcIceCandidate = new RTCIceCandidate({
// candidate: sdpArray[i],
// sdpMid: "audio",
// sdpMLineIndex: 0
// });
// connection.addIceCandidate(rtcIceCandidate);
// }
// }
// var video = false;
MediaDevicesShim.getSharedScreen = function(constraints) {
if (window.location.protocol !== 'https:') {
return Promise.reject(new Error('Screen sharing only works on HTTPS, try the https:// version of this page'));
}
if ('chrome' == webrtc.browserDetails.browser) {
return this._getSharedScreenChrome(constraints);
} else if ('firefox' == webrtc.browserDetails.browser) {
return this._getSharedScreenFirefox(constraints);
}
};
module.exports = function (constraints, cb) {
var hasConstraints = arguments.length === 2;
var callback = hasConstraints ? cb : constraints;
var error;
if ('getDisplayMedia' in window.navigator.mediaDevices) { // prefer spec getDisplayMedia
window.navigator.mediaDevices.getDisplayMedia(constraints)
.then(function (stream) {
callback(null, stream);
}).catch(function (err) {
callback(err);
});
} else if (adapter.browserDetails.browser === 'chrome') {
if (sessionStorage.getScreenMediaJSExtensionId) {
// check that the extension is installed by looking for a
// sessionStorage variable that contains the extension id
// this has to be set after installation unless the content
// script does that
chrome.runtime.sendMessage(sessionStorage.getScreenMediaJSExtensionId,
{type:'getScreen', id: 1}, null,
function (data) {
if (!data || data.sourceId === '') { // user canceled
var error = new Error('NavigatorUserMediaError');
error.name = 'NotAllowedError';
callback(error);
} else {
constraints = (hasConstraints && constraints) || {audio: false, video: {
mandatory: {
chromeMediaSource: 'desktop',
MediaDevicesShim._getSharedScreenChrome = function(constraints) {
var chromever = webrtc.browserDetails.version;
var maxver = 33;
if (window.navigator.userAgent.match('Linux')) {
maxver = 35;
} // "known" crash in chrome 34 and 35 on linux
if (chromever >= 26 && chromever <= maxver) {
// Chrome 26->33 requires some awkward chrome://flags manipulation
constraints = Helpers.extend({}, constraints, {
video: {
mandatory: {
googLeakyBucket: true,
maxWidth: window.screen.width,
maxHeight: window.screen.height,
maxFrameRate: 3,
chromeMediaSource: 'screen'
}
}
}
}};
constraints.video.mandatory.chromeMediaSourceId = sourceId;
window.navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
callback(null, stream);
}).catch(function (err) {
callback(err);
});
}
});
} else {
error = new Error('Screensharing is not supported');
error.name = 'NotSupportedError';
callback(error);
}
} else if (adapter.browserDetails.browser === 'firefox' && adapter.browserDetails.version >= 33) {
constraints = (hasConstraints && constraints) || {
video: {
mediaSource: 'window'
}
};
window.navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
callback(null, stream);
}).catch(function (err) {
callback(err);
});
} else {
error = new Error('Screensharing is not supported');
error.name = 'NotSupportedError';
callback(error);
}
};
componentDidMount() {
if (adapter.browserDetails.browser === "firefox") {
adapter.browserShim.shimGetDisplayMedia(window, "screen");
}
}
const callbacks = cbs || {};
callbacks.success = (typeof cbs.success == 'function') ? cbs.success : this.client.noop;
callbacks.error = (typeof cbs.error == 'function') ? cbs.error : this.client.noop;
const pluginHandle = this.pluginHandles[handleId];
if (!pluginHandle || !pluginHandle.webrtcStuff) {
this.client.warn('Invalid handle');
callbacks.error('Invalid handle');
return;
}
const config = pluginHandle.webrtcStuff;
this.client.log('Creating answer (iceDone=' + config.iceDone + ')');
let mediaConstraints = null;
const browser = adapter.browserDetails.browser;
if (browser === 'firefox' || browser === 'edge') {
mediaConstraints = {
offerToReceiveAudio: this.isAudioRecvEnabled(media),
offerToReceiveVideo: this.isVideoRecvEnabled(media)
};
} else {
mediaConstraints = {
mandatory: {
OfferToReceiveAudio: this.isAudioRecvEnabled(media),
OfferToReceiveVideo: this.isVideoRecvEnabled(media)
}
};
}
this.client.debug(mediaConstraints);
config.pc.createAnswer(
(answer) => {
getScreenDeviceId(constraints).then(function (screenSharingConstraints) {
//copy constraints
for (var prop in screenSharingConstraints) {
if (screenSharingConstraints.hasOwnProperty(prop)) {
constraints.video[prop] = screenSharingConstraints[prop];
}
}
if (webrtcAdapter.browserDetails.browser == "chrome") {
delete constraints.video.frameRate;
delete constraints.video.height;
delete constraints.video.width;
}
getAccess(constraints);
}, reject);
} else {
if (opts.disableContextMenu) {
element.oncontextmenu = function (e) {
e.preventDefault();
};
}
if (opts.autoplay) element.autoplay = 'autoplay';
element.muted = !!opts.muted;
if (!opts.audio) {
['', 'moz', 'webkit', 'o', 'ms'].forEach(function (prefix) {
var styleName = prefix ? prefix + 'Transform' : 'transform';
element.style[styleName] = opts.mirror ? 'scaleX(-1)' : 'scaleX(1)';
});
}
if (adapter.browserDetails.browser === 'safari') {
element.setAttribute('playsinline', true);
}
element.srcObject = stream;
return element;
};