From 1f369dae11fa179f675380de96da43c395d6ee78 Mon Sep 17 00:00:00 2001
From: smartfridge <37928912+smartfrigde@users.noreply.github.com>
Date: Tue, 13 Dec 2022 15:11:28 +0100
Subject: [PATCH] Add disable autogain
---
assets/lang/en-US.json | 2 +
src/content/js/disableAutogain.js | 94 +++++++++++++++++++++++++++++++
src/content/setup.html | 2 +
src/ipc.ts | 3 +
src/preload/preload.ts | 4 +-
src/settings/settings.html | 8 +++
src/utils.ts | 2 +
7 files changed, 114 insertions(+), 1 deletion(-)
create mode 100644 src/content/js/disableAutogain.js
diff --git a/assets/lang/en-US.json b/assets/lang/en-US.json
index 87931f4..d1ff9d5 100644
--- a/assets/lang/en-US.json
+++ b/assets/lang/en-US.json
@@ -47,6 +47,8 @@
"settings-prfmMode-desc": "Performance mode is an experimental function that may either increase responsiveness and performance of\n ArmCord or... decrease it. Please try every option and see which fits you the best.",
"settings-prfmMode-performance": "Performance",
"settings-prfmMode-battery": "Battery",
+ "settings-disableAutogain": "Disable autogain",
+ "settings-disableAutogain-desc": "Disables autogain.",
"settings-trayIcon": "Tray icon",
"settings-trayIcon-desc": "Set the icon which will appear in tray menu.",
"settings-trayIcon-dynamic": "Dynamic",
diff --git a/src/content/js/disableAutogain.js b/src/content/js/disableAutogain.js
new file mode 100644
index 0000000..186d56b
--- /dev/null
+++ b/src/content/js/disableAutogain.js
@@ -0,0 +1,94 @@
+// MIT License
+
+// Copyright (c) 2021 Joseph Watts
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+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);
diff --git a/src/content/setup.html b/src/content/setup.html
index bc945f3..65eac96 100644
--- a/src/content/setup.html
+++ b/src/content/setup.html
@@ -156,6 +156,7 @@
automaticPatches: false,
performanceMode: "none",
alternativePaste: false,
+ disableAutogain: false,
startMinimized: false,
trayIcon: "default",
mods: options.mod,
@@ -171,6 +172,7 @@
minimizeToTray: true,
automaticPatches: false,
mobileMode: false,
+ disableAutogain: false,
mods: "none",
startMinimized: false,
alternativePaste: false,
diff --git a/src/ipc.ts b/src/ipc.ts
index 9bc126b..be1ef00 100644
--- a/src/ipc.ts
+++ b/src/ipc.ts
@@ -121,6 +121,9 @@ export function registerIpc() {
ipcMain.on("trayIcon", async (event, arg) => {
event.returnValue = await getConfig("trayIcon");
});
+ ipcMain.on("disableAutogain", async (event, arg) => {
+ event.returnValue = await getConfig("disableAutogain");
+ });
ipcMain.on("titlebar", (event, arg) => {
event.returnValue = customTitlebar;
});
diff --git a/src/preload/preload.ts b/src/preload/preload.ts
index e987162..c874c62 100644
--- a/src/preload/preload.ts
+++ b/src/preload/preload.ts
@@ -57,7 +57,9 @@ if (window.location.href.indexOf("splash.html") > -1) {
});
})();
`);
-
+ if (ipcRenderer.sendSync("disableAutogain")) {
+ addScript(fs.readFileSync(path.join(__dirname, "../", "/content/js/disableAutogain.js"), "utf8"));
+ }
addScript(fs.readFileSync(path.join(__dirname, "../", "/content/js/rpc.js"), "utf8"));
const cssPath = path.join(__dirname, "../", "/content/css/discord.css");
addStyle(fs.readFileSync(cssPath, "utf8"));
diff --git a/src/settings/settings.html b/src/settings/settings.html
index 0791308..4225e62 100644
--- a/src/settings/settings.html
+++ b/src/settings/settings.html
@@ -96,6 +96,14 @@
+