Major screen share refactor

This commit is contained in:
Oleh Polisan 2024-04-17 02:17:46 +03:00
parent 3fdc55a47e
commit 11f9970f0c
2 changed files with 39 additions and 68 deletions

View file

@ -53,6 +53,9 @@ interface Source {
export let currentSettings: StreamSettings | null = null; export let currentSettings: StreamSettings | null = null;
const logger = new Vencord.Util.Logger("VesktopScreenShare");
var isOverloaded = false;
addPatch({ addPatch({
patches: [ patches: [
{ {
@ -449,77 +452,44 @@ function ModalComponent({
disabled={!selected} disabled={!selected}
onClick={() => { onClick={() => {
currentSettings = settings; currentSettings = settings;
try {
var conn = [...MediaEngineStore.getMediaEngine().connections].find(
connection => connection.streamUserId === UserStore.getCurrentUser().id
);
} catch {
console.log("No current stream.");
}
try {
if (conn) {
const track = conn.input.stream.getVideoTracks()[0];
const frameRate = Number(settings.fps);
const height = Number(settings.resolution);
const width = Math.round(height * (16 / 9));
var constraints = track.getConstraints();
const newConstraints = {
...constraints,
frameRate,
width: { min: 640, ideal: width, max: 2560 },
height: { min: 480, ideal: height, max: 1440 },
advanced: [{ width: width, height: height }],
resizeMode: "none"
};
track.applyConstraints(newConstraints).then(() => {
console.log("Applied constraints from ScreenSharePicker successfully.");
console.log("New constraints:", track.getConstraints());
});
// changing stream quality description
conn.videoStreamParameters[0].maxFrameRate = Number(settings.fps);
conn.videoStreamParameters[0].maxResolution.height = Number(settings.resolution);
conn.videoStreamParameters[0].maxResolution.width = Math.round(height * (16 / 9));
}
} catch {
console.log("No current stream.");
}
try { try {
submit({ submit({
id: selected!, id: selected!,
...settings ...settings
}); }).then(() => {
// reapply contraints after some time to let discord resubmit stream
// i believe there MUST be way to do it cleaner..
if (conn) {
setTimeout(() => { setTimeout(() => {
const track = conn.input.stream.getVideoTracks()[0]; var conn = [...MediaEngineStore.getMediaEngine().connections].find(
const frameRate = Number(settings.fps); connection => connection.streamUserId === UserStore.getCurrentUser().id
const height = Number(settings.resolution); );
const width = Math.round(height * (16 / 9)); if (conn) {
var constraints = track.getConstraints(); const track = conn.input.stream.getVideoTracks()[0];
const newConstraints = { const frameRate = Number(settings.fps);
...constraints, const height = Number(settings.resolution);
frameRate, const width = Math.round(height * (16 / 9));
width: { min: 640, ideal: width, max: 2560 }, var constraints = track.getConstraints();
height: { min: 480, ideal: height, max: 1440 }, const newConstraints = {
advanced: [{ width: width, height: height }], ...constraints,
resizeMode: "none" frameRate,
}; width: { min: 640, ideal: width, max: width },
track.applyConstraints(newConstraints).then(() => { height: { min: 480, ideal: height, max: height },
console.log("Applied constraints from ScreenSharePicker successfully."); advanced: [{ width: width, height: height }],
console.log("New constraints:", track.getConstraints()); resizeMode: "none"
}); };
track.applyConstraints(newConstraints).then(() => {
logger.log(
"Applied constraints successfully. New constraints: ",
track.getConstraints()
);
});
// changing stream quality description conn.videoStreamParameters[0].maxFrameRate = frameRate;
conn.videoStreamParameters[0].maxFrameRate = Number(settings.fps); conn.videoStreamParameters[0].maxResolution.height = height;
conn.videoStreamParameters[0].maxResolution.height = Number(settings.resolution); conn.videoStreamParameters[0].maxResolution.width = width;
conn.videoStreamParameters[0].maxResolution.width = Math.round(height * (16 / 9)); }
}, 100); }, 100);
} });
} catch { } catch (error) {
console.log("Unable to start stream."); logger.error("Error while submitting stream.", error);
} }
close(); close();

View file

@ -7,6 +7,8 @@
import { currentSettings } from "renderer/components/ScreenSharePicker"; import { currentSettings } from "renderer/components/ScreenSharePicker";
import { isLinux } from "renderer/utils"; import { isLinux } from "renderer/utils";
const logger = new Vencord.Util.Logger("VesktopScreenFixes");
if (isLinux) { if (isLinux) {
const original = navigator.mediaDevices.getDisplayMedia; const original = navigator.mediaDevices.getDisplayMedia;
@ -34,15 +36,14 @@ if (isLinux) {
const newConstraints = { const newConstraints = {
...constraints, ...constraints,
frameRate, frameRate,
width: { min: 640, ideal: width, max: 2560 }, width: { min: 640, ideal: width, max: width },
height: { min: 480, ideal: height, max: 1440 }, height: { min: 480, ideal: height, max: height },
advanced: [{ width: width, height: height }], advanced: [{ width: width, height: height }],
resizeMode: "none" resizeMode: "none"
}; };
track.applyConstraints(newConstraints).then(() => { track.applyConstraints(newConstraints).then(() => {
console.log("Applied constraints from ScreenShareFixes successfully."); logger.log("Applied constraints successfully. New constraints: ", track.getConstraints());
console.log("New constraints:", track.getConstraints());
}); });
if (id) { if (id) {