Skip to content

Commit

Permalink
detect if obs open through ps list instead of sources
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Sep 24, 2023
1 parent c3c82d5 commit 3f08f5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"electron-updater": "6.1.1",
"lodash-es": "^4.17.21",
"obs-websocket-js": "^5.0.2",
"ps-list": "^8.1.1",
"quickjs-emscripten": "^0.23.0",
"sequelize": "^6.28.0",
"sqlite3": "^5.1.4",
Expand Down
21 changes: 18 additions & 3 deletions packages/main/src/workers/OBSWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { default: OBSWebSocketLib } = require('obs-websocket-js');
import { desktopCapturer } from 'electron';
import type OBSWebSocketType from 'obs-websocket-js';
import { log } from '/@/logger';
import { Setting } from '../database/models/Setting';
Expand All @@ -13,10 +12,16 @@ export const OBSWebSocket = new class {
private isDisconncting = false;
private isObsOpen = false;
private isConnected = false;
private isConnecting = false; // Triggered when waiting for the app process to start up
private settings?: Setting<ObsWebSocketSettings>;
private psList?: (() => Promise<{ name: string }[]>);

constructor() {
this.obs = new OBSWebSocketLib();

import('ps-list').then(({ default: psList }) => {
this.psList = psList;
});
}

async init() {
Expand Down Expand Up @@ -121,15 +126,25 @@ export const OBSWebSocket = new class {
return;
}

const res = await desktopCapturer.getSources({ types: ['window', 'screen'] });
const isOpen = res.find((item) => item.name.toLocaleLowerCase().includes('obs')) !== undefined;
if (!this.psList || this.isConnecting) {
return;
}

const processes = await this.psList();
const isOpen = processes.find((process) => process.name.toLocaleLowerCase().includes('obs')) !== undefined;

// If app was closed and now is open, retrying connection
if (!this.isObsOpen && isOpen && this.settings) {
this.isConnecting = true;

// When process is detected, give time to app (3seconds) to open etc.
await new Promise((resolve) => setTimeout(resolve, 3 * 1000));

this.connect();
}

this.isObsOpen = isOpen;
this.isConnecting = false;

tellRenderer({
subject: ObsWebSocketSubject.AppStatus,
Expand Down

0 comments on commit 3f08f5a

Please sign in to comment.