From 14ac96366eea1232956f4f6e584dbf42d158cd0e Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 10 Jun 2021 22:23:19 +0200 Subject: [PATCH] util class to check active mic --- src/back/index.ts | 2 ++ src/back/utils.ts | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/back/index.ts b/src/back/index.ts index 2fbf805..b9b562e 100644 --- a/src/back/index.ts +++ b/src/back/index.ts @@ -5,6 +5,7 @@ import TrayUtility from './tray' import Settings from './settings' import Banner from './banner' import Updater from './updater' +import { InputDevicesStatus } from './utils' export const DEV = !app.isPackaged @@ -27,6 +28,7 @@ app Settings.init() Banner.init() Updater.init() + InputDevicesStatus.init() logger.log('Done') }) .catch((e) => { diff --git a/src/back/utils.ts b/src/back/utils.ts index cbf4e91..7f09349 100644 --- a/src/back/utils.ts +++ b/src/back/utils.ts @@ -1,28 +1,25 @@ import cp from 'child_process' +import { promisify } from 'util' import Settings from './settings' +const exec = promisify(cp.exec) + export async function isCameraActive(): Promise { if (process.platform === 'darwin') { - return new Promise((resolve) => { - // Check number of processes using the camera - cp.exec(`lsof -n | grep "AppleCamera"`, (_, out) => { - const processesUsingCamera = out.trim().split('\n').length - resolve(processesUsingCamera > 1) // One is the apple daemon that is always active - }) - }) + // Check number of processes using the camera + const out = await exec(`lsof -n | grep "AppleCamera"`) + const processesUsingCamera = out.stdout.trim().split('\n').length + return processesUsingCamera > 1 // One is the apple daemon that is always active } return false } export async function isMicrophoneActive(): Promise { if (process.platform === 'darwin') { - return new Promise((resolve) => { - cp.exec(`ioreg -c AppleHDAEngineInput | grep IOAudioEngineState`, (_, out) => { - const parsed = parseInt(out.trim().replace(/[^\d]/gim, '')) - resolve(parsed > 0) - }) - }) + const out = await exec(`ioreg -c AppleHDAEngineInput | grep "IOAudioEngineState"`) + const parsed = parseInt(out.stdout.trim().replace(/[^\d]/gim, '')) + return parsed > 0 } return false } @@ -33,11 +30,14 @@ export class InputDevicesStatus { camera: false, } + static update() { + // TODO: Update electron version as soon as issue is resolved https://github.com/electron/electron/issues/26143 + isMicrophoneActive().then((result) => (this.status.mic = result)) + isCameraActive().then((result) => (this.status.camera = result)) + } + static init() { - setInterval(() => { - isMicrophoneActive().then((result) => (this.status.mic = result)) - isCameraActive().then((result) => (this.status.camera = result)) - }, 2000) + setInterval(this.update, 3000) } static areCameraOrMicrophoneActive(): boolean {