-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsclient.js
109 lines (94 loc) · 2.81 KB
/
obsclient.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
108
109
/**
* DEVELOPED AND PUBLISHED BY @Caisesiume on 2022-09-02
* GPL-3.0 LICENSE
*/
const OBSWebSocket = require('obs-websocket-js').default;
const auth = require('./auth.json');
const TIME = require('../utils/time.js');
const obs = new OBSWebSocket();
const ip = 'ws://' + auth.ip + ':' + auth.port;
const pass = auth.pass;
const MIC = 'Mic';
const CAM = 'Cam';
const TIMEOUT = 10*1000; // 10 sec
/**
* Runs in 3 steps
*
* 1. Connects to the remote OBS app via Websocket
* 2. Toggles the mute state of inputDevice MIC and CAM, and sets a timer of TIMEOUT seconds
* for both inputDevice's state to be reset to what they were before this function was executed.
* 3. Disconnects the client from the websocket.
*/
module.exports.runOBS = async function() {
await connectToWs();
await swapActiveMic();
setTimeout(closeConnection, TIMEOUT + 2000);
}
/**
* Connection through websocket to OBS
*/
obs.on('ConnectionOpened', () => {
console.log('Connected to OBS');
});
/**
* Connects to OBS server websocket
*/
async function connectToWs() {
try {
await obs.connect(ip,pass,auth.d)
} catch (error) {
console.error('Failed to connect to OBS', error.code, error.message);
}
}
/**
* Swaps what mic is active in the current OBS Scene
*/
async function swapActiveMic() {
//console.log(MICS.length);
let mic_status = await getMicStatus(MIC);
let cam_status = await getMicStatus(CAM);
if(!mic_status) {
console.log(`${TIME.getTime()} | Swapping mics...`);
await setMicToggle(MIC);
await setMicToggle(CAM);
}
}
/**
*
* @param {String} sourceName inputName of the source which muted status will be edited
*/
async function setMicToggle(sourceName) {
try {
await obs.call('ToggleInputMute',{inputName: sourceName})
setTimeout(async function unmute() {
console.log(TIME.getTime() + " | Set " + sourceName + " to default audio state");
await obs.call('ToggleInputMute',{inputName: sourceName})
}, TIMEOUT)
} catch (error) {
console.log("Failed to change the state of input devices! \n" + error);
}
}
/**
*
* @param {String} sourceName inputName of the source to request status from. If muted or not. True = muted
* @returns
*/
async function getMicStatus(sourceName) {
try {
let status = await obs.call('GetInputMute',{inputName: sourceName});
return status.inputMuted;
} catch (error) {
console.log('Could not retrive data from sourceName! \n' + error);
}
}
/**
* Disconnects the client from the OBS websocket
*/
async function closeConnection() {
try {
await obs.disconnect();
console.log("Disconnected from OBS \n");
} catch (error) {
console.log("Failed to disconnect from OBS! \n" + error);
}
}