-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.html
212 lines (189 loc) · 6.93 KB
/
controller.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>OBS Replay Buffer Controller</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/obs-ws.min.js"></script>
<script>
// The obs machine ip and port
const obsHost = "127.0.0.1:4455";
// The obs-websocket password
const obsPassword = "YourPasswordHERE";
// Block button to avoid overlapping clips (false/true)
const avoidClipOverlays = false;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// register page load event listener to start work
window.addEventListener("load", init, false);
// some variables, don't change!
var interval = null;
var connectionIsOpen = false;
const obs = new OBSWebSocket();
var replayBufferActive = false;
var saveButton = null;
var lastSaveTrigger = 0;
var replayBufferTime = null;
// initialize
function init() {
attemptConnection();
saveButton = document.getElementById("saveButton");
saveButton.addEventListener("click", buttonWasClicked);
}
// Handle websocket connection stuff
async function attemptConnection() {
// Try to connect to obs
try {
const {
obsWebSocketVersion,
negotiatedRpcVersion
} = await obs.connect(`ws://${obsHost}`, obsPassword, {rpcVersion: 1});
console.log(`Connected to obs-websocket ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`);
toggleErrorDivVisibility(true);
} catch (error) {
console.error("Failed to connect", error.code, error.message);
if (error.message == "Cannot access 'obs' before initialization") {
toggleErrorDivVisibility(false, "Could not load websocket library. Please check your internet connection.");
} else {
toggleErrorDivVisibility(false, `The connection to obs failed with the following error: ${error.message || "No reason specified"}`);
}
}
}
// obs-websocket connection was closed for whatever reason
obs.on("ConnectionClosed", () => {
console.log("Connection to obs-websocket was closed");
connectionIsOpen = false;
if (!interval) {
interval = setInterval(attemptConnection, 5000);
}
});
// Identified is the new open
obs.on("Identified", () => {
clearInterval(interval);
connectionIsOpen = true;
interval = null;
getInitialState();
});
// request the initial filter status
async function getInitialState() {
try {
await getInitialState_ReplayBufferState();
} catch (error) {
console.error("There was an error getting the initial state", error);
}
}
// sync initial state
async function getInitialState_ReplayBufferState() {
const state = await obs.call("GetReplayBufferStatus");
ReplayBufferStateChanged(state);
const {parameterValue: replayBufferEnabled} = await obs.call("GetProfileParameter", {parameterCategory: "AdvOut", parameterName: "RecRB"});
if (replayBufferEnabled) {
const {parameterValue} = await obs.call("GetProfileParameter", {parameterCategory: "AdvOut", parameterName: "RecRBTime"});
replayBufferTime = parseInt(parameterValue)*1000;
}
}
// Handle replay buffer state changes (active/inactive)
obs.on("ReplayBufferStateChanged", ReplayBufferStateChanged);
async function ReplayBufferStateChanged(state) {
let { outputActive } = state;
replayBufferActive = outputActive;
if (replayBufferActive) {
saveButton.style.backgroundColor = "red";
saveButton.innerText = "save";
} else {
saveButton.style.backgroundColor = "";
saveButton.innerText = "start";
}
}
// Handle replay buffer save
obs.on("ReplayBufferSaved", ReplayBufferSaved);
async function ReplayBufferSaved(path) {
if (path.savedReplayPath) {
saveButton.innerText = "saved";
saveButton.style.backgroundColor = "green";
setTimeout(() => {
saveButton.style.backgroundColor = "red";
setTimeout(() => {
saveButton.style.backgroundColor = "green";
setTimeout(() => {
ReplayBufferStateChanged({outputActive: replayBufferActive});
}, 500)
}, 500)
}, 500)
}
}
// Handle button clicks
async function buttonWasClicked(event) {
if (!replayBufferActive) {
// start replay buffer
try {
await obs.call("StartReplayBuffer");
} catch (error) {
console.error("There was an error starting the replay buffer", error);
}
} else {
// save replay buffer if blocking wasn't triggered
if (avoidClipOverlays && replayBufferTime && Date.now() - lastSaveTrigger < replayBufferTime) {
saveButton.innerText = "blocked!";
setTimeout(async () => {
const state = await obs.call("GetReplayBufferStatus");
ReplayBufferStateChanged(state);
}, 1500)
return;
}
saveButton.innerText = "saving..";
try {
await obs.call("SaveReplayBuffer");
lastSaveTrigger = Date.now();
} catch (error) {
console.error("There was an error saving the replay buffer", error);
}
}
}
function toggleErrorDivVisibility(hidden = True, message = "") {
errorbox = document.getElementById("errorbox")
buttonbox = document.getElementById("buttonbox")
if (hidden) {
errorbox.style.display = "none";
buttonbox.style.display = "block";
} else {
errorbox.textContent = message;
errorbox.style.display = "block";
buttonbox.style.display = "none";
}
}
</script>
<style>
html {
height: 100%;
}
body {
background: #2b2e38;
color: white;
font-family: Arial;
overflow: hidden;
margin: 0px;
height: 100%;
}
.container {
display: none;
height: 100%;
}
#saveButton {
width: 100%;
height: 100%;
font-size: 3em;
text-transform: uppercase;
}
img {
margin-top: 5px;
}
.errorbox {
display: block
}
</style>
</head>
<body>
<div class="container" id="buttonbox"><button id="saveButton">start</button></div>
<div class="errorbox" id="errorbox"></div>
</body>
</html>