diff --git a/index.js b/index.js index 62c470a..b5b9019 100755 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ import cli from "./utils/cli.js"; import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from "./utils/scene.js"; import { audioHelp, audioMute, audioUnmute, audioToggle, audioStatus } from "./utils/audio.js"; +import { streamHelp, streamStart, streamStop, streamStatus } from "./utils/stream.js"; import { QWebChannel } from "qwebchannel"; @@ -180,6 +181,66 @@ socket.onopen = function() { socket.close(); process.exit(0); } + + case "stream": + if (flags.help) { + console.log(streamHelp); + socket.close(); + process.exit(0); + } + + const streamCommand = input[1]; + switch (streamCommand) { + case "start": + channel = new QWebChannel(socket, function (channel) { + streamStart(channel) + .then((message) => { + console.log(message); + socket.close(); + process.exit(0); + }) + .catch((err) => { + console.error(`${err}`); + socket.close(); + process.exit(1); + }); + }); + break; + case "stop": + channel = new QWebChannel(socket, function (channel) { + streamStop(channel) + .then((message) => { + console.log(message); + socket.close(); + process.exit(0); + }) + .catch((err) => { + console.error(`${err}`); + socket.close(); + process.exit(1); + }); + }); + break; + case "status": + channel = new QWebChannel(socket, function (channel) { + streamStatus(channel) + .then((isStreaming) => { + console.log(`Streaming is currently ${isStreaming ? "active" : "inactive"}`); + socket.close(); + process.exit(0); + }) + .catch((err) => { + console.error(`Error fetching stream status: ${err}`); + socket.close(); + process.exit(1); + }); + }); + break; + default: + console.log(streamHelp); + socket.close(); + process.exit(0); + } } } catch (error) { console.error("Error handling CLI flags:", error); diff --git a/utils/cli.js b/utils/cli.js index 284d41f..c6025e5 100644 --- a/utils/cli.js +++ b/utils/cli.js @@ -8,6 +8,9 @@ const commands = { audio: { desc: "Manage audio settings", }, + stream: { + desc: "Manage streaming", + }, }; const flags = { diff --git a/utils/stream.js b/utils/stream.js new file mode 100644 index 0000000..5573adf --- /dev/null +++ b/utils/stream.js @@ -0,0 +1,83 @@ +import meowHelp from "cli-meow-help"; + +const commands = { + start: { + desc: "Start streaming", + }, + stop: { + desc: "Stop streaming", + }, + status: { + desc: "Show the current streaming status", + }, +}; + +const flags = { + help: { + type: "boolean", + shortFlag: "h", + desc: "Display help information", + }, +}; + +const streamHelp = meowHelp({ + name: "meld-cli stream", + flags, + commands, + description: "Manage streaming in Meld", + defaults: false, +}); + +function streamStart(channel) { + if (!channel.objects || !channel.objects.meld) { + return Promise.reject(new Error("Meld object not found in channel.")); + } + + const meld = channel.objects.meld; + if (meld.isStreaming) { + return Promise.reject(new Error("Streaming is already active.")); + } + + return new Promise((resolve, reject) => { + meld.toggleStream() + .then(() => { + resolve("Streaming started successfully."); + }) + .catch((err) => { + reject(err); + }); + }); +} + +function streamStop(channel) { + if (!channel.objects || !channel.objects.meld) { + return Promise.reject(new Error("Meld object not found in channel.")); + } + + const meld = channel.objects.meld; + if (!meld.isStreaming) { + return Promise.reject(new Error("Streaming is not currently active.")); + } + + return new Promise((resolve, reject) => { + meld.toggleStream() + .then(() => { + resolve("Streaming stopped successfully."); + }) + .catch((err) => { + reject(err); + }); + }); +} + + +function streamStatus(channel) { + if (!channel.objects || !channel.objects.meld) { + return Promise.reject(new Error("Meld object not found in channel.")); + } + + const meld = channel.objects.meld; + return Promise.resolve(meld.isStreaming); +} + +export { streamHelp, streamStart, streamStop, streamStatus }; \ No newline at end of file