Skip to content

Commit

Permalink
fix #15: Add parameter to set max fps
Browse files Browse the repository at this point in the history
  • Loading branch information
mPyKen committed Apr 25, 2024
1 parent 94315f3 commit 7a3f4c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "screen-area-share",
"productName": "screen-area-share",
"version": "1.3.3",
"version": "1.4.0",
"description": "Share part of your screen on e.g. Teams",
"author": {
"name": "github.com/mPyKen"
Expand Down
49 changes: 24 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ ipcMain.on("set-ignore-mouse-events", (event, ...args) => {
BrowserWindow.fromWebContents(event.sender).setIgnoreMouseEvents(...args);
});

const maxFrameRate = app.commandLine.hasSwitch("maxfps")
? parseInt(app.commandLine.getSwitchValue("maxfps"))
: 60;
const freeze = app.commandLine.hasSwitch("freeze");
const initCapRect = {
x: app.commandLine.hasSwitch("cx")
Expand Down Expand Up @@ -64,7 +67,6 @@ function checkWindowBounds(win) {
}

const createWindows = () => {

//
// create and setup render window
//
Expand All @@ -85,10 +87,7 @@ const createWindows = () => {

// start by setting initial window location starting at (0,0) and change it
// later, when all other settings have been applied
mainWindow.setPosition(
0,
0,
);
mainWindow.setPosition(0, 0);

mainWindow.loadFile(path.join(__dirname, "render.html"));
mainWindow.on("closed", () => app.quit());
Expand All @@ -98,10 +97,10 @@ const createWindows = () => {
mainWindow.on("focus", (event) => mainWindow.send("window-focus"));
mainWindow.on("blur", (event) => mainWindow.send("window-blur"));
mainWindow.on("resize", (event) =>
mainWindow.send("window-resize", mainWindow.getBounds())
mainWindow.send("window-resize", mainWindow.getBounds()),
);
mainWindow.on("move", (event) =>
mainWindow.send("window-move", mainWindow.getBounds())
mainWindow.send("window-move", mainWindow.getBounds()),
);
mainWindow.send("window-resize", mainWindow.getBounds());
mainWindow.send("window-move", mainWindow.getBounds());
Expand All @@ -112,13 +111,12 @@ const createWindows = () => {
// aversity about negative window coordinates
mainWindow.setPosition(
initRenderRect.x ?? mainWindow.getPosition()[0],
initRenderRect.y ?? mainWindow.getPosition()[1]
initRenderRect.y ?? mainWindow.getPosition()[1],
);


//
// create and setup capture window
//
//

const captureWindow = new BrowserWindow({
webPreferences: {
Expand All @@ -135,10 +133,7 @@ const createWindows = () => {

// start by setting initial window location starting at (0,0) and change it
// later, when all other settings have been applied
captureWindow.setPosition(
0,
0,
);
captureWindow.setPosition(0, 0);

captureWindow.loadFile(path.join(__dirname, "capture.html"));
captureWindow.setContentProtection(true); // exclude from capture
Expand All @@ -153,7 +148,7 @@ const createWindows = () => {
// coordinates
captureWindow.setPosition(
initCapRect.x ?? captureWindow.getPosition()[0],
initCapRect.y ?? captureWindow.getPosition()[1]
initCapRect.y ?? captureWindow.getPosition()[1],
);

captureWindow.on("resized", (event) => {
Expand All @@ -166,7 +161,7 @@ const createWindows = () => {
determineScreenToCapture();
});
captureWindow.on("resize", (event) =>
updateMain(null, captureWindow.getSize())
updateMain(null, captureWindow.getSize()),
);
let moveTimer = undefined;
captureWindow.on("move", (event) => {
Expand All @@ -191,26 +186,29 @@ const createWindows = () => {
if (display?.toString() !== currentDisplay?.id.toString()) {
currentDisplay = display;
const sourceId = await getVideoSourceIdForDisplay(display);
mainWindow.send("update-screen-to-capture", { display, sourceId });
mainWindow.send("update-screen-to-capture", {
display,
sourceId,
maxFrameRate,
});
}
}


async function getVideoSourceIdForDisplay(display) {
const inputSources = await desktopCapturer.getSources({
types: ["screen"],
});

cons.log(
`find display ${display.id.toString()} @ ${JSON.stringify(
display.bounds
)}`
display.bounds,
)}`,
);
inputSources.map((is) =>
cons.log(` ${is.id}, ${is.name}: ${is.display_id}`)
cons.log(` ${is.id}, ${is.name}: ${is.display_id}`),
);
let source = inputSources.find(
(is) => is.display_id === display.id.toString()
(is) => is.display_id === display.id.toString(),
);
if (!source) {
// the display_id property doesn't match up - time for plan B
Expand All @@ -221,14 +219,15 @@ const createWindows = () => {
const sourceName = `Screen ${displayIndex + 1}`;
cons.log(
"display_id didn't match; Plan B: looking for input source named",
sourceName
sourceName,
);
source = inputSources.find((is) => is.name === sourceName);
}
if (!source) {
throw new Error(
`Cannot find source matching display ${display.id
}. Candidates: ${JSON.stringify(inputSources, null, 2)}`
`Cannot find source matching display ${
display.id
}. Candidates: ${JSON.stringify(inputSources, null, 2)}`,
);
}

Expand Down
9 changes: 5 additions & 4 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const maskElement = document.getElementsByClassName("mask")[0];

const { ipcRenderer, app } = require("electron");

async function selectSource(sourceId, display) {
async function selectSource(sourceId, display, maxFrameRate) {
const constraints = {
audio: false,
video: {
Expand All @@ -18,6 +18,7 @@ async function selectSource(sourceId, display) {
chromeMediaSourceId: sourceId,
minWidth: display.bounds.width,
minHeight: display.bounds.height,
maxFrameRate: maxFrameRate,
},
},
};
Expand All @@ -37,14 +38,14 @@ let dispx = 0;
let dispy = 0;
ipcRenderer.on(
"update-screen-to-capture",
async (event, { display, sourceId }) => {
async (event, { display, sourceId, maxFrameRate }) => {
cons.log(`>> update display: ${display.id}, ${sourceId}`);
const { x: x, y: y } = display.bounds;
dispx = x ?? dispx;
dispy = y ?? dispy;
await selectSource(sourceId, display);
await selectSource(sourceId, display, maxFrameRate);
ipcRenderer.send("update-main");
}
},
);
ipcRenderer.on("update-capture-area", (event, pos, dim) => {
//console.log(pos, dim)
Expand Down

0 comments on commit 7a3f4c6

Please sign in to comment.