forked from yemount/pose-animator
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.js
110 lines (93 loc) · 3.01 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { app, shell, BrowserWindow, ipcMain } = require('electron')
const virtualcam = require('node-virtualcam')
const ratio = 9/16;
const windowWidth = 960;
const windowHeight = ratio * windowWidth;
const fps = 30
const delay = 0
let lastReceivedFrame = null;
let isQuitting = false;
function createWindow () {
let mainWindow = new BrowserWindow({
width: windowWidth,
height: windowHeight,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
// TODO not working, see https://github.com/electron/electron/issues/9567
backgroundThrottling: false
}
})
// https://github.com/electron/electron/issues/1344#issuecomment-392844066
mainWindow.webContents.on('new-window', function(event, url){
if (url.endsWith('editor.html'))
return
event.preventDefault();
shell.openExternal(url);
});
mainWindow.loadFile('dist/camera.html')
let width = 0
let height = 0
let frameIdx = 0;
ipcMain.on('frame', (event, arg) => {
lastReceivedFrame = arg.data;
if (width === 0) {
width = arg.width
height = arg.height
virtualcam.start(width, height, fps, delay)
console.log(`virtual cam output started (${width}x${height} @ ${fps}fps)`);
var timerId = setInterval(() => {
if (isQuitting) {
clearInterval(timerId)
}
virtualcam.send(frameIdx, lastReceivedFrame);
frameIdx += 1;
}, 1/fps * 1000);
}
if (width != arg.width || height != arg.height) {
throw Error(`received frame with mismatching size: ${arg.width}x${arg.height}`)
}
})
ipcMain.on('openEditor', (event, arg) => {
const avatar = arg;
let editorWindow = new BrowserWindow({
width: windowWidth,
height: windowHeight,
x: mainWindow.getPosition()[0] + 50,
y: mainWindow.getPosition()[1] + 50,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true
}
})
editorWindow.webContents.on('did-finish-load', () => {
editorWindow.webContents.send('avatar', avatar);
});
editorWindow.loadFile('dist/editor.html')
})
// forward avatar from editor to main window
ipcMain.on('avatar', (event, arg) => {
mainWindow.webContents.send('avatar', arg)
})
}
app.whenReady().then(createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('will-quit', () => {
isQuitting = true
virtualcam.stop()
console.log('virtual cam output stopped')
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})