obs-ss-plugin-webui/lib/obsClient.js
Decobus 1d4b1eefba
Some checks failed
Lint and Build / build (20) (push) Has been cancelled
Lint and Build / build (22) (push) Has been cancelled
Initial commit - OBS Source Switcher Plugin UI
Complete Next.js application for managing OBS Source Switcher
- Stream management with multiple screen layouts
- Team management CRUD operations
- SQLite database integration
- OBS WebSocket API integration
- Updated to latest versions (Next.js 15.4.1, React 19.1.0, Tailwind CSS 4.0.0)
- Enhanced .gitignore for privacy and development
2025-07-15 22:15:57 -04:00

125 lines
No EOL
3.8 KiB
JavaScript

// const config = require('../config');
const { OBSWebSocket } = require('obs-websocket-js');
let obs = null;
async function connectToOBS() {
if (!obs) {
obs = new OBSWebSocket();
}
try {
const OBS_HOST = process.env.OBS_WEBSOCKET_HOST || '127.0.0.1';
const OBS_PORT = process.env.OBS_WEBSOCKET_PORT || '4455';
const OBS_PASSWORD = process.env.OBS_WEBSOCKET_PASSWORD || '';
console.log('Connecting to OBS WebSocket...');
console.log('Host:', OBS_HOST);
console.log('Port:', OBS_PORT);
console.log('Password:', OBS_PASSWORD ? '***' : '(none)');
await obs.connect(`ws://${OBS_HOST}:${OBS_PORT}`, OBS_PASSWORD);
console.log('Connected to OBS WebSocket.');
} catch (err) {
console.error('Failed to connect to OBS WebSocket:', err.message);
throw err;
}
}
function getOBSClient() {
if (!obs) {
throw new Error('OBS WebSocket client is not initialized. Call connectToOBS() first.');
}
// console.log('client', obs)
return obs;
}
async function disconnectFromOBS() {
if (obs) {
await obs.disconnect();
console.log('Disconnected from OBS WebSocket.');
obs = null;
}
}
async function addSourceToSwitcher(inputName, newSources) {
if (!obs) {
obs = new OBSWebSocket();
}
try {
const OBS_HOST = process.env.OBS_WEBSOCKET_HOST || '127.0.0.1';
const OBS_PORT = process.env.OBS_WEBSOCKET_PORT || '4455';
const OBS_PASSWORD = process.env.OBS_WEBSOCKET_PASSWORD || '';
await obs.connect(`ws://${OBS_HOST}:${OBS_PORT}`, OBS_PASSWORD);
// Step 1: Get current input settings
const { inputSettings } = await obs.call('GetInputSettings', { inputName });
// console.log('Current Settings:', inputSettings);
// Step 2: Add new sources to the sources array
const updatedSources = [...inputSettings.sources, ...newSources];
// Step 3: Update the settings with the new sources array
await obs.call('SetInputSettings', {
inputName,
inputSettings: {
...inputSettings,
sources: updatedSources,
},
});
console.log('Updated settings successfully for', inputName);
obs.disconnect();
} catch (error) {
console.error('Error updating settings:', error.message);
}
}
// async function addSourceToGroup(obs, teamName, obs_source_name, url) {
// try {
// // Step 1: Check if the group exists
// const { scenes } = await obs.call('GetSceneList');
// const groupExists = scenes.some((scene) => scene.sceneName === teamName);
// // Step 2: Create the group if it doesn't exist
// if (!groupExists) {
// console.log(`Group "${teamName}" does not exist. Creating it.`);
// await obs.call('CreateScene', { sceneName: teamName });
// } else {
// console.log(`Group "${teamName}" already exists.`);
// }
// // Step 3: Add the source to the group
// console.log(`Adding source "${obs_source_name}" to group "${teamName}".`);
// await obs.call('CreateInput', {
// sceneName: teamName,
// inputName: obs_source_name,
// inputKind: 'browser_source',
// inputSettings: {
// width: 1600,
// height: 900,
// url,
// control_audio: true,
// },
// });
// // Step 4: Enable "Control audio via OBS"
// await obs.call('SetInputSettings', {
// inputName: obs_source_name,
// inputSettings: {
// control_audio: true, // Enable audio control
// },
// overlay: true, // Keep existing settings and apply changes
// });
// console.log(`Source "${obs_source_name}" successfully added to group "${teamName}".`);
// } catch (error) {
// console.error('Error adding source to group:', error.message);
// }
// }
// Export all functions
module.exports = { connectToOBS, getOBSClient, disconnectFromOBS, addSourceToSwitcher};