-
Notifications
You must be signed in to change notification settings - Fork 21
/
disableAutogain.js
78 lines (76 loc) · 2.92 KB
/
disableAutogain.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
(function() {
function setLegacyChromeConstraint(constraint, name, value) {
if (constraint.mandatory && name in constraint.mandatory) {
constraint.mandatory[name] = value;
return;
}
if (constraint.optional) {
const element = constraint.optional.find(opt => name in opt);
if (element) {
element[name] = value;
return;
}
}
// `mandatory` options throw errors for unknown keys, so avoid that by
// setting it under optional.
if (!constraint.optional) {
constraint.optional = [];
}
constraint.optional.push({ [name]: value });
}
function setConstraint(constraint, name, value) {
if (constraint.advanced) {
const element = constraint.advanced.find(opt => name in opt);
if (element) {
element[name] = value;
return;
}
}
constraint[name] = value;
}
function disableAutogain(constraints) {
console.log("Automatically unsetting gain!", constraints);
if (constraints && constraints.audio) {
if (typeof constraints.audio !== "object") {
constraints.audio = {};
}
if (constraints.audio.optional || constraints.audio.mandatory) {
setLegacyChromeConstraint(constraints.audio, "googAutoGainControl", false);
setLegacyChromeConstraint(constraints.audio, "googAutoGainControl2", false);
} else {
setConstraint(constraints.audio, "autoGainControl", false);
}
}
}
function patchFunction(object, name, createNewFunction) {
if (name in object) {
var original = object[name];
object[name] = createNewFunction(original);
}
}
patchFunction(navigator.mediaDevices, "getUserMedia", function (original) {
return function getUserMedia(constraints) {
disableAutogain(constraints);
return original.call(this, constraints);
};
});
function patchDeprecatedGetUserMedia(original) {
return function getUserMedia(constraints, success, error) {
disableAutogain(constraints);
return original.call(this, constraints, success, error);
};
}
patchFunction(navigator, "getUserMedia", patchDeprecatedGetUserMedia);
patchFunction(navigator, "mozGetUserMedia", patchDeprecatedGetUserMedia);
patchFunction(navigator, "webkitGetUserMedia", patchDeprecatedGetUserMedia);
patchFunction(MediaStreamTrack.prototype, "applyConstraints", function (original) {
return function applyConstraints(constraints) {
disableAutogain(constraints);
return original.call(this, constraints);
};
});
console.log(
"Disable Autogain by Joey Watts!",
navigator.mediaDevices.getUserMedia
);
})();