From 8d18178d9aa4aede575be59e94fe85967c37f738 Mon Sep 17 00:00:00 2001 From: Luiz Amaral Date: Mon, 17 Sep 2018 09:05:46 -0300 Subject: [PATCH] Add support for OBS stream status and stream key update. --- README.md | 50 +++++++++++++++++++ packages/nodecg-utility-obs/index.js | 49 +++++++++++++++++- .../schemas/streamStatus.json | 5 ++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 packages/nodecg-utility-obs/schemas/streamStatus.json diff --git a/README.md b/README.md index 1852d57..3b7e3f9 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ module.exports = function (nodecg) { obs.replicants.sceneList.on('change', () => {/* ... */}); obs.replicants.transitioning.on('change', () => {/* ... */}); obs.replicants.studioMode.on('change', () => {/* ... */}); + obs.replicants.streamStatus.on('change', () => {/* ... */}); }; ``` @@ -279,6 +280,13 @@ A boolean that becomes `true` when OBS is in Studio Mode `false` when OBS isn't Relevant Schemas: - [`studioMode.json`](packages/nodecg-utility-obs/schemas/studioMode.json) +#### `> nodecg.Replicant('obs:streamStatus')` + +A boolean that becomes `true` when OBS is streaming `false` when OBS isn't streaming. + +Relevant Schemas: + - [`streamStatus.json`](packages/nodecg-utility-obs/schemas/streamStatus.json) + #### `> nodecg.Replicant('_obs:namespaces')` A special replicant that is shared between all instances/namespaces. It always uses the name `_obs:namespaces`, @@ -367,6 +375,48 @@ nodecg.sendMessage('obs:transition', 'Fade').then(() => { }); ``` +#### `> nodecg.sendMessage('obs:startStreaming', callback)` + +Start streaming with OBS. + +##### Example + +```js +nodecg.sendMessage('obs:startStreaming').then(() => { + console.log('successfully started streaming'); +}).catch(err => { + console.error('failed to start streaming:', err); +}); +``` + +#### `> nodecg.sendMessage('obs:stopStreaming', callback)` + +Stop streaming with OBS. + +##### Example + +```js +nodecg.sendMessage('obs:stopStreaming').then(() => { + console.log('successfully stopped streaming'); +}).catch(err => { + console.error('failed to stop streaming:', err); +}); +``` + +#### `> nodecg.sendMessage('obs:setStreamKey', key, callback)` + +Set the OBS stream key. + +##### Example + +```js +nodecg.sendMessage('obs:setStreamKey', 'a1b2c3d4-a1b2c3d4').then(() => { + console.log('successfully set stream key'); +}).catch(err => { + console.error('failed to set stream key:', err); +}); +``` + ### API `nodecg-obs` extends [`obs-websocket-js`](https://github.com/haganbmj/obs-websocket-js). Please diff --git a/packages/nodecg-utility-obs/index.js b/packages/nodecg-utility-obs/index.js index 1f08921..7f241e1 100644 --- a/packages/nodecg-utility-obs/index.js +++ b/packages/nodecg-utility-obs/index.js @@ -37,6 +37,7 @@ class OBSUtility extends OBSWebSocket { const sceneList = nodecg.Replicant(`${namespace}:sceneList`, {schemaPath: buildSchemaPath('sceneList')}); const transitioning = nodecg.Replicant(`${namespace}:transitioning`, {schemaPath: buildSchemaPath('transitioning')}); const studioMode = nodecg.Replicant(`${namespace}:studioMode`, {schemaPath: buildSchemaPath('studioMode')}); + const streamStatus = nodecg.Replicant(`${namespace}:streamStatus`, {schemaPath: buildSchemaPath('streamStatus')}); const log = new nodecg.Logger(`${nodecg.bundleName}:${namespace}`); // Expose convenient references to the Replicants. @@ -49,7 +50,8 @@ class OBSUtility extends OBSWebSocket { previewScene, sceneList, transitioning, - studioMode + studioMode, + streamStatus }; this.log = log; this.hooks = opts.hooks || {}; @@ -133,6 +135,39 @@ class OBSUtility extends OBSWebSocket { callback(); }); + nodecg.listenFor(`${namespace}:startStreaming`, (_data, callback = function () {}) => { + try { + this.StartStreaming(); + } catch (error) { + log.error('Error starting the streaming:', error); + callback(error); + return; + } + callback(); + }); + + nodecg.listenFor(`${namespace}:stopStreaming`, (_data, callback = function () {}) => { + try { + this.StopStreaming(); + } catch (error) { + log.error('Error stopping the streaming:', error); + callback(error); + return; + } + callback(); + }); + + nodecg.listenFor(`${namespace}:setStreamKey`, (key, callback = function () {}) => { + try { + this.SetStreamSettings({'settings': {'key': key}}); + } catch (error) { + log.error('Error setting the stream key:', error); + callback(error); + return; + } + callback(); + }); + this.on('ConnectionClosed', () => { this._reconnectToOBS(); }); @@ -169,6 +204,18 @@ class OBSUtility extends OBSWebSocket { studioMode.value = data.newState; }); + this.on('StreamStatus', data => { + streamStatus.value = data.streaming; + }); + + this.on('StreamStarted', () => { + streamStatus.value = true; + }); + + this.on('StreamStopped', () => { + streamStatus.value = false; + }); + setInterval(() => { if (websocketConfig.value && websocketConfig.value.status === 'connected' && !this._connected) { log.warn('Thought we were connected, but the automatic poll detected we were not. Correcting.'); diff --git a/packages/nodecg-utility-obs/schemas/streamStatus.json b/packages/nodecg-utility-obs/schemas/streamStatus.json new file mode 100644 index 0000000..73145ce --- /dev/null +++ b/packages/nodecg-utility-obs/schemas/streamStatus.json @@ -0,0 +1,5 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "boolean", + "default": false +}