-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontent.js
More file actions
104 lines (93 loc) · 4.32 KB
/
content.js
File metadata and controls
104 lines (93 loc) · 4.32 KB
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
99
100
101
102
103
104
/* behaviour is controlled via sessionStorage. Remember it stores JSON...
sessionStorage.__getUserMediaAudioError = "NotAllowedError";
sessionStorage.__getUserMediaVideoError = "NotFoundError";
sessionStorage.__filterAudioDevices = true;
sessionStorage.__filterVideoDevices = true;
sessionStorage.__filterDeviceLabels = true;
*/
// override getUserMedia to inject errors.
const origGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
navigator.mediaDevices.getUserMedia = async (constraints) => {
let err;
// for consistency with the device modifications reject with a NotFoundError.
if (constraints.audio && sessionStorage.__filterAudioDevices) {
err = new Error('getUserMedia error');
err.name = 'NotFoundError';
return Promise.reject(err);
}
var isScreenSharing = constraints.video && (constraints.video.mediaSource || constraints.video.mandatory && constraints.video.mandatory.chromeMediaSource);
if (constraints.video && sessionStorage.__filterVideoDevices && !isScreenSharing) {
err = new Error('getUserMedia error');
err.name = 'NotFoundError';
return Promise.reject(err);
}
// return errors
if (constraints.audio && sessionStorage.__getUserMediaAudioError) {
err = new Error('getUserMedia error');
err.name = sessionStorage.__getUserMediaAudioError;
return Promise.reject(err);
}
if (constraints.video && sessionStorage.__getUserMediaVideoError) {
err = new Error('getUserMedia error');
err.name = sessionStorage.__getUserMediaVideoError;
return Promise.reject(err);
}
if (constraints.video && constraints.video.deviceId &&
(constraints.video.deviceId.exact ? constraints.video.deviceId.exact.indexOf('dynamicGum:fake:') === 0 : constraints.video.deviceId.indexOf('dynamicGum:fake:') === 0)) {
const canvas = document.createElement('canvas');
canvas.width = 640; // TODO: actual width/height.
canvas.height = 480;
const ctx = canvas.getContext('2d', {alpha: false});
ctx.fillStyle = (constraints.video.deviceId.exact ? constraints.video.deviceId.exact : constraints.video.deviceId).substr(16);
ctx.fillRect(0,0,canvas.width, canvas.height);
const videoStream = canvas.captureStream();
const videoTrack = videoStream.getVideoTracks()[0];
delete constraints.video;
const stream = await origGetUserMedia(constraints);
stream.addTrack(videoTrack);
return stream;
}
return origGetUserMedia(constraints);
};
// override enumerateDevices to filter certain device kinds or return empty labels
// (which means no permission has been granted). Also returns empty labels
// and device ids when getUserMedia permission is denied via a session storage flag.
const origEnumerateDevices = navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices);
navigator.mediaDevices.enumerateDevices = async () => {
let devices = await origEnumerateDevices();
if (sessionStorage.__filterVideoDevices) {
devices = devices.filter((device) => device.kind !== 'videoinput');
}
if (sessionStorage.__filterAudioDevices) {
devices = devices.filter((device) => device.kind !== 'audioinput');
}
devices = devices.map((device) => {
const deviceWithoutLabelAndDeviceId = {
deviceId: '',
kind: device.kind,
label: '',
groupId: device.groupId,
};
if (device.kind === 'audioinput' && sessionStorage.__getUserMediaAudioError === 'NotAllowedError') {
return deviceWithoutLabelAndDeviceId;
}
if (device.kind === 'videoinput' && sessionStorage.__getUserMediaVideoError === 'NotAllowedError') {
return deviceWithoutLabelAndDeviceId;
}
if (sessionStorage.__filterDeviceLabels) {
return deviceWithoutLabelAndDeviceId;
}
return device;
});
if (sessionStorage.__fakeVideoDevices) {
JSON.parse(sessionStorage.__fakeVideoDevices).forEach(function(fakeDeviceSpec) {
devices.push({
deviceId: 'dynamicGum:fake:' + fakeDeviceSpec.color,
kind: 'videoinput',
label: fakeDeviceSpec.label,
groupId: 'fake devices',
});
});
}
return devices;
};