util class to check active mic

This commit is contained in:
cupcakearmy 2021-06-10 22:23:19 +02:00
parent 622bfc914e
commit 14ac96366e
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
2 changed files with 19 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import TrayUtility from './tray'
import Settings from './settings' import Settings from './settings'
import Banner from './banner' import Banner from './banner'
import Updater from './updater' import Updater from './updater'
import { InputDevicesStatus } from './utils'
export const DEV = !app.isPackaged export const DEV = !app.isPackaged
@ -27,6 +28,7 @@ app
Settings.init() Settings.init()
Banner.init() Banner.init()
Updater.init() Updater.init()
InputDevicesStatus.init()
logger.log('Done') logger.log('Done')
}) })
.catch((e) => { .catch((e) => {

View File

@ -1,28 +1,25 @@
import cp from 'child_process' import cp from 'child_process'
import { promisify } from 'util'
import Settings from './settings' import Settings from './settings'
const exec = promisify(cp.exec)
export async function isCameraActive(): Promise<boolean> { export async function isCameraActive(): Promise<boolean> {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
return new Promise((resolve) => { // Check number of processes using the camera
// Check number of processes using the camera const out = await exec(`lsof -n | grep "AppleCamera"`)
cp.exec(`lsof -n | grep "AppleCamera"`, (_, out) => { const processesUsingCamera = out.stdout.trim().split('\n').length
const processesUsingCamera = out.trim().split('\n').length return processesUsingCamera > 1 // One is the apple daemon that is always active
resolve(processesUsingCamera > 1) // One is the apple daemon that is always active
})
})
} }
return false return false
} }
export async function isMicrophoneActive(): Promise<boolean> { export async function isMicrophoneActive(): Promise<boolean> {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
return new Promise((resolve) => { const out = await exec(`ioreg -c AppleHDAEngineInput | grep "IOAudioEngineState"`)
cp.exec(`ioreg -c AppleHDAEngineInput | grep IOAudioEngineState`, (_, out) => { const parsed = parseInt(out.stdout.trim().replace(/[^\d]/gim, ''))
const parsed = parseInt(out.trim().replace(/[^\d]/gim, '')) return parsed > 0
resolve(parsed > 0)
})
})
} }
return false return false
} }
@ -33,11 +30,14 @@ export class InputDevicesStatus {
camera: false, 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() { static init() {
setInterval(() => { setInterval(this.update, 3000)
isMicrophoneActive().then((result) => (this.status.mic = result))
isCameraActive().then((result) => (this.status.camera = result))
}, 2000)
} }
static areCameraOrMicrophoneActive(): boolean { static areCameraOrMicrophoneActive(): boolean {