Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested implementation for #12 #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const connect = async () => {
return obs.connect(address, settings.password ?? "", { eventSubscriptions: EventSubscription.All | EventSubscription.InputVolumeMeters })
};

const streamButton = new ButtonType("streamButton", {
name: "OBS: Stream Button",
});

const recordButton = new ButtonType("recordButton", {
name: "OBS: Record Button",
});

const registerListeners = () => {
obs.on("InputVolumeChanged", (data) => {
Expand Down Expand Up @@ -63,6 +70,14 @@ const registerListeners = () => {
});
});

obs.on("StreamStateChanged", (data) => {
streamButton.active = data.outputActive;
});

obs.on("RecordStateChanged", (data) => {
recordButton.active = data.outputActive;
});

obs.on("ExitStarted", () => {
disconnect();
init();
Expand Down Expand Up @@ -149,6 +164,33 @@ const mapScenes = async () => {
});
};

const mapButtons = async () => {
streamButton.on("pressed", async () => {
const status = await obs.call("GetStreamStatus");

if (status.outputActive) {
obs.call("StopStream");
}
else {
obs.call("StartStream");
}
});

recordButton.on("pressed", async () => {
const status = await obs.call("GetRecordStatus");

if (status.ouputPaused) {
obs.call("ResumeRecord");
}
else if (status.outputActive) {
obs.call("StopRecord");
}
else {
obs.call("StartRecord");
}
});
};

function disconnect() {
console.log("Disconnecting");
obs.disconnect();
Expand All @@ -175,15 +217,20 @@ const init = async () => {

await connect();
registerListeners();
await Promise.all([mapSources(), mapScenes()]);
await Promise.all([mapSources(), mapScenes(), mapButtons()]);

$MM.setSettingsStatus("status", "Connected");
wsConnected = true;
} catch (err: any) {
console.warn("OBS error:", err);
$MM.setSettingsStatus("status", err.description || err.message || err);
}
};

const delay = (delayMs: number) => {
return new Promise(resolve => setTimeout(resolve, delayMs));
}

$MM.onSettingsButtonPress("reconnect", init);

init();