Compare commits

..

9 Commits

Author SHA1 Message Date
Malte Jürgens
cd2aca55c7 add toggle to disable automatic gain 2023-05-28 10:33:38 +02:00
Malte Jürgens
8a6f49b949 Merge pull request #149 from Vitalya-code/master
Move two functions into separate file
2023-04-27 18:04:34 +00:00
Malte Jürgens
47fd620876 fix formatting 2023-04-27 20:04:04 +02:00
Vitalya
c15250498b Move server function to the bottom and change "utils" file name to "localserver" 2023-04-27 19:52:28 +03:00
Vitalya
6f0303206e Move two functions into separate file 2023-04-27 17:21:03 +03:00
Malte Jürgens
6e86647c95 Merge pull request #148 from Vitalya-code/master
Shows error message to user if discord is running
2023-04-25 16:33:21 +00:00
Vitalya
798fb3d5e4 Minor change in error message
Change "Discord" to "discord-screenaudio" in error message
2023-04-25 15:07:25 +03:00
Malte Jürgens
6c9b76ed90 format code 2023-04-24 22:41:36 +02:00
Vitalya
b582584c69 Shows error message to user if discord is running 2023-04-24 19:58:56 +03:00
7 changed files with 119 additions and 20 deletions

View File

@@ -45,6 +45,7 @@ set(discord-screenaudio_SRC
src/log.cpp
src/userscript.cpp
src/centralwidget.cpp
src/localserver.cpp
resources.qrc
)

View File

@@ -1,12 +1,14 @@
navigator.mediaDevices.chromiumGetDisplayMedia =
navigator.mediaDevices.getDisplayMedia;
navigator.mediaDevices.chromiumGetUserMedia =
navigator.mediaDevices.getUserMedia;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const getAudioDevice = async (nameOfAudioDevice) => {
await navigator.mediaDevices.getUserMedia({
await navigator.mediaDevices.chromiumGetUserMedia({
audio: true,
});
let audioDevice;
@@ -23,6 +25,16 @@ const getAudioDevice = async (nameOfAudioDevice) => {
return audioDevice;
};
function setGetUserMedia() {
const getUserMedia = async (constraints) => {
return await navigator.mediaDevices.chromiumGetUserMedia({
video: constraints?.video || false,
audio: { ...constraints?.audio, autoGainControl },
});
};
navigator.mediaDevices.getUserMedia = getUserMedia;
}
function setGetDisplayMedia(video = true, overrideArgs = undefined) {
const getDisplayMedia = async (...args) => {
var id;
@@ -34,26 +46,27 @@ function setGetDisplayMedia(video = true, overrideArgs = undefined) {
} catch (error) {
id = "default";
}
let captureSystemAudioStream = await navigator.mediaDevices.getUserMedia({
audio: {
// We add our audio constraints here, to get a list of supported constraints use navigator.mediaDevices.getSupportedConstraints();
// We must capture a microphone, we use default since its the only deviceId that is the same for every Chromium user
deviceId: {
exact: id,
let captureSystemAudioStream =
await navigator.mediaDevices.chromiumGetUserMedia({
audio: {
// We add our audio constraints here, to get a list of supported constraints use navigator.mediaDevices.getSupportedConstraints();
// We must capture a microphone, we use default since its the only deviceId that is the same for every Chromium user
deviceId: {
exact: id,
},
// We want auto gain control, noise cancellation and noise suppression disabled so that our stream won't sound bad
autoGainControl: false,
echoCancellation: false,
noiseSuppression: false,
// By default Chromium sets channel count for audio devices to 1, we want it to be stereo in case we find a way for Discord to accept stereo screenshare too
channelCount: 2,
// You can set more audio constraints here, bellow are some examples
//latency: 0,
//sampleRate: 48000,
//sampleSize: 16,
//volume: 1.0
},
// We want auto gain control, noise cancellation and noise suppression disabled so that our stream won't sound bad
autoGainControl: false,
echoCancellation: false,
noiseSuppression: false,
// By default Chromium sets channel count for audio devices to 1, we want it to be stereo in case we find a way for Discord to accept stereo screenshare too
channelCount: 2,
// You can set more audio constraints here, bellow are some examples
//latency: 0,
//sampleRate: 48000,
//sampleSize: 16,
//volume: 1.0
},
});
});
let [track] = captureSystemAudioStream.getAudioTracks();
const gdm = await navigator.mediaDevices.chromiumGetDisplayMedia(
...(overrideArgs
@@ -68,6 +81,7 @@ function setGetDisplayMedia(video = true, overrideArgs = undefined) {
}
setGetDisplayMedia();
setGetUserMedia();
let userscript;
let muteBtn;
@@ -76,6 +90,7 @@ let streamStartBtn;
let streamStartBtnInitialDisplay;
let streamStartBtnClone;
let resolutionString;
let autoGainControl = true;
const clonedElements = [];
const hiddenElements = [];
let wasStreamActive = false;
@@ -159,6 +174,10 @@ function main() {
streamStartBtnClone.remove();
});
userscript.getPref("disableAutomaticGain", false).then((disabled) => {
autoGainControl = !disabled;
});
function updateUserstyles() {
userscript.log("Loading userstyles...");
userscript.loadingMessage = "Loading userstyles...";
@@ -368,5 +387,29 @@ function main() {
}
}
}
for (const el of document.getElementsByClassName("sensitivity-3A7Gs9")) {
if (
el.getElementsByTagName("div").length > 0 &&
!document.getElementById("discord-screenaudio-gaintoggle")
) {
const toggle = createSwitch(
"Disable automatic gain",
await userscript.getPref("disableAutomaticGain", false),
async (disabled) => {
await userscript.setPref("disableAutomaticGain", disabled);
autoGainControl = !disabled;
setGetUserMedia();
if (disabled)
userscript.showInformation(
"discord-screenaudio",
"If you are currently in a call, this setting may only take effect after you rejoin the call or restart discord-screenaudio."
);
}
);
toggle.id = "discord-screenaudio-gaintoggle";
el.getElementsByTagName("div")[0].appendChild(toggle);
}
}
}, 500);
}

22
src/localserver.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include "localserver.h"
bool isProgramRunning(const QString &program_name) {
QLocalSocket socket;
socket.connectToServer(program_name);
if (socket.waitForConnected()) {
return true; // program is already running
}
return false;
}
void showErrorMessage(const char *text) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setWindowIcon(QIcon(":assets/de.shorsh.discord-screenaudio.png"));
msgBox.exec();
}

9
src/localserver.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include "mainwindow.h"
#include <QLocalServer>
#include <QLocalSocket>
#include <QMessageBox>
bool isProgramRunning(const QString &program_name);
void showErrorMessage(const char *text);

View File

@@ -1,3 +1,4 @@
#include "localserver.h"
#include "mainwindow.h"
#include "virtmic.h"
@@ -7,10 +8,14 @@
#include <QApplication>
#include <QCommandLineParser>
#include <QLocalServer>
#include <QLocalSocket>
#include <QLoggingCategory>
#include <QMessageBox>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QApplication::setApplicationName("discord-screenaudio");
QApplication::setWindowIcon(
QIcon(":assets/de.shorsh.discord-screenaudio.png"));
@@ -50,6 +55,20 @@ int main(int argc, char *argv[]) {
qgetenv("QTWEBENGINE_CHROMIUM_FLAGS"));
MainWindow w(parser.isSet(notifySendOption));
// Check if discord is already running
QString program_name = "discord-screenaudio";
if (isProgramRunning(program_name)) {
// if running show error message
showErrorMessage("discord-screenaudio is already running");
return 1;
}
// open server so we can check if discord is running
QLocalServer server;
server.listen(program_name);
QObject::connect(&server, &QLocalServer::newConnection, []() {});
w.show();
return app.exec();

View File

@@ -182,3 +182,7 @@ void UserScript::showThemeDialog() {
void UserScript::installUserStyles(QString url) {
emit shouldInstallUserStyles(url);
}
void UserScript::showInformation(QString title, QString message) {
QMessageBox::information(MainWindow::instance(), title, message);
}

View File

@@ -72,6 +72,7 @@ public Q_SLOTS:
void showShortcutsDialog();
void showHelpMenu();
void showStreamDialog();
void showInformation(QString title, QString message);
void stopVirtmic();
void startVirtmic(QString target);
void showThemeDialog();