forked from Envek/obs-studio-node-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
107 lines (91 loc) · 3.37 KB
/
renderer.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 { ipcRenderer, shell, remote } = require('electron');
const path = require('path');
async function initOBS() {
const result = await ipcRenderer.invoke('recording-init');
console.debug("initOBS result:", result);
if (result) {
ipcRenderer.on("performanceStatistics", (_event, data) => onPerformanceStatistics(data));
}
}
async function startRecording() {
const result = await ipcRenderer.invoke('recording-start');
console.debug("startRecording result:", result);
return result;
}
async function stopRecording() {
const result = await ipcRenderer.invoke('recording-stop');
console.debug("stopRecording result:", result);
return result;
}
let recording = false;
let recordingStartedAt = null;
let timer = null;
async function switchRecording() {
if (recording) {
recording = (await stopRecording()).recording;
} else {
recording = (await startRecording()).recording;
}
updateUI();
}
function updateUI() {
const button = document.getElementById('rec-button');
button.disabled = false;
if (recording) {
button.innerText = '⏹️ Stop recording'
startTimer();
} else {
button.innerText = '⏺️ Start recording'
stopTimer();
}
}
function startTimer() {
recordingStartedAt = Date.now();
timer = setInterval(updateTimer, 100);
}
function stopTimer() {
clearInterval(timer);
}
function updateTimer() {
const diff = Date.now() - recordingStartedAt;
const timerElem = document.getElementById('rec-timer');
const decimals = `${Math.floor(diff % 1000 / 100)}`;
const seconds = `${Math.floor(diff % 60000 / 1000)}`.padStart(2, '0');
const minutes = `${Math.floor(diff % 3600000 / 60000)}`.padStart(2, '0');
const hours = `${Math.floor(diff / 3600000)}`.padStart(2, '0');
timerElem.innerText = `${hours}:${minutes}:${seconds}.${decimals}`;
}
function openFolder() {
shell.openPath(remote.app.getPath("videos"));
}
function onPerformanceStatistics(data) {
document.querySelector(".performanceStatistics #cpu").innerText = `${data.CPU} %`;
document.querySelector(".performanceStatistics #cpuMeter").value = data.CPU;
document.querySelector(".performanceStatistics #numberDroppedFrames").innerText = data.numberDroppedFrames;
document.querySelector(".performanceStatistics #percentageDroppedFrames").innerText = `${data.percentageDroppedFrames} %`;
document.querySelector(".performanceStatistics #bandwidth").innerText = data.bandwidth;
document.querySelector(".performanceStatistics #frameRate").innerText = `${Math.round(data.frameRate)} fps`;
}
const previewContainer = document.getElementById('preview');
async function setupPreview() {
const { width, height, x, y } = previewContainer.getBoundingClientRect();
const result = await ipcRenderer.invoke('preview-init', { width, height, x, y });
previewContainer.style = `height: ${result.height}px`;
}
async function resizePreview() {
const { width, height, x, y } = previewContainer.getBoundingClientRect();
const result = await ipcRenderer.invoke('preview-bounds', { width, height, x, y });
previewContainer.style = `height: ${result.height}px`;
}
const currentWindow = remote.getCurrentWindow();
currentWindow.on('resize', resizePreview);
document.addEventListener("scroll", resizePreview);
var ro = new ResizeObserver(resizePreview);
ro.observe(document.querySelector("#preview"));
try {
initOBS();
setupPreview();
updateUI();
} catch (err) {
console.log(err)
}