Skip to content

Commit

Permalink
Add support for OBS stream status and stream key update.
Browse files Browse the repository at this point in the history
  • Loading branch information
lamaral committed Sep 17, 2018
1 parent f99c88c commit 859b7b2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {/* ... */});
};
```
Expand Down Expand Up @@ -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)
#### <a name="streamStatus"></a> `> 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)
#### <a name="namespaces"></a> `> nodecg.Replicant('_obs:namespaces')`
A special replicant that is shared between all instances/namespaces. It always uses the name `_obs:namespaces`,
Expand Down Expand Up @@ -367,6 +375,48 @@ nodecg.sendMessage('obs:transition', 'Fade').then(() => {
});
```
#### <a name="message-transition"></a> `> 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);
});
```
#### <a name="message-transition"></a> `> 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);
});
```
#### <a name="message-transition"></a> `> 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
Expand Down
49 changes: 48 additions & 1 deletion packages/nodecg-utility-obs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -49,7 +50,8 @@ class OBSUtility extends OBSWebSocket {
previewScene,
sceneList,
transitioning,
studioMode
studioMode,
streamStatus
};
this.log = log;
this.hooks = opts.hooks || {};
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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.');
Expand Down
5 changes: 5 additions & 0 deletions packages/nodecg-utility-obs/schemas/streamStatus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "boolean",
"default": false
}

0 comments on commit 859b7b2

Please sign in to comment.