-
Notifications
You must be signed in to change notification settings - Fork 10
/
chrome.js
98 lines (83 loc) · 2.45 KB
/
chrome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var detect = require('rtc-core/detect');
var extend = require('cog/extend');
var OPT_DEFAULTS = {
target: 'rtc.io screenshare'
};
var REQUEST_OPTS = {
targets: ['screen', 'window']
};
var CHROME_VERSION = getChromeVersion();
// Chrome >= 50 allows for greater sharing options
if (CHROME_VERSION >= 50) {
REQUEST_OPTS.targets.push('tab');
REQUEST_OPTS.targets.push('audio');
}
/**
Returns true if we should use Chrome screensharing
**/
exports.supported = function() {
return detect.browser === 'chrome';
}
/**
Creates the share context.
**/
exports.share = function(opts) {
// if disableAudio, then hide the checkbox in the popup
if (!(opts || {}).disableAudio) {
var index = REQUEST_OPTS.targets.indexOf('audio');
if (index !== -1) REQUEST_OPTS.targets.splice(index, 1);
}
var extension = require('chromex/client')(extend({}, OPT_DEFAULTS, opts, {
target: (opts || {}).chromeExtension
}));
extension.type = 'google/chrome';
extension.available = function(callback) {
return extension.satisfies((opts || {}).version, callback);
};
// patch in our capture function
extension.request = function(callback) {
var requestOptions = extend({}, REQUEST_OPTS, (opts || {}).requests || {});
extension.sendCommand('share', requestOptions, function(err, sourceId) {
if (err) {
return callback(err);
}
if (! sourceId) {
return callback(new Error('user rejected screen share request'));
}
var audioConstraints = false;
// Support audio on Chrome 50+
if (CHROME_VERSION >= 50 && !(opts || {}).disableAudio) {
audioConstraints = {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sourceId
}
};
}
// pass the constraints through
return callback(null, extend({
audio: audioConstraints,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sourceId,
maxWidth: screen.width,
maxHeight: screen.height,
minFrameRate: 1,
maxFrameRate: 5
},
optional: []
}
}, opts.constraints));
});
};
extension.cancel = function() {
extension.sendCommand('cancel', function(err) {
});
};
return extension;
};
function getChromeVersion() {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : -1;
}