Skip to content

Commit e557ece

Browse files
committed
Switch to offline scene when stopping stream from OBS.
1 parent 2710855 commit e557ece

File tree

6 files changed

+115
-27
lines changed

6 files changed

+115
-27
lines changed

lib/components/Chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function () {
141141
var parse = parsed.message.slice(1).split(" ");
142142
var commandName = parse[0];
143143

144-
if (_config.default.twitchChat.adminUsers.includes(parsed.username) && this.rate != 20 || _config.default.twitchChat.enablePublicCommands && this.allowAllCommands.includes(commandName) && !this.wait && this.rate != 20 || _config.default.twitchChat.enableModCommands && parsed.tags.mod === "1" && this.allowModsCommands.includes(commandName) && this.rate != 20 || parsed.username === this.channel.substring(1) && this.rate != 20) {
144+
if (_config.default.twitchChat.adminUsers.includes(parsed.username) && this.rate != 20 || _config.default.twitchChat.enablePublicCommands && this.allowAllCommands.includes(commandName) && !this.wait && this.rate != 20 || _config.default.twitchChat.enableModCommands && parsed.tags.mod === "1" && this.allowModsCommands.includes(commandName) && this.rate != 20 || parsed.username === this.channel.substring(1) && this.rate != 20 || commandName == "noalbs") {
145145
if (this.commands.includes(commandName)) {
146146
this[commandName](parse[1]);
147147
log.success("".concat(parsed.username, " Executed ").concat(commandName, " command"));

lib/components/ObsSwitcher.js

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ function (_EventEmitter) {
122122
_asyncToGenerator(
123123
/*#__PURE__*/
124124
regeneratorRuntime.mark(function _callee() {
125-
var currentScene, bitrate, canSwitch;
125+
var bitrate, _ref2, currentScene, canSwitch;
126+
126127
return regeneratorRuntime.wrap(function _callee$(_context) {
127128
while (1) {
128129
switch (_context.prev = _context.next) {
@@ -136,17 +137,17 @@ function (_EventEmitter) {
136137

137138
case 2:
138139
_context.next = 4;
139-
return _this2.obs.GetCurrentScene();
140+
return _this2.getBitrate();
140141

141142
case 4:
142-
currentScene = _context.sent;
143+
bitrate = _context.sent;
143144
_context.next = 7;
144-
return _this2.getBitrate();
145+
return _this2.canSwitch();
145146

146147
case 7:
147-
bitrate = _context.sent;
148-
canSwitch = currentScene.name == _this2.lowBitrateScene || currentScene.name == _this2.normalScene || currentScene.name == _this2.offlineScene;
149-
_this2.currentScene = currentScene.name;
148+
_ref2 = _context.sent;
149+
currentScene = _ref2.currentScene;
150+
canSwitch = _ref2.canSwitch;
150151

151152
if (bitrate !== null) {
152153
_this2.isLive = true;
@@ -291,9 +292,44 @@ function (_EventEmitter) {
291292
}
292293
}, {
293294
key: "streamStopped",
294-
value: function streamStopped() {
295-
this.obsStreaming = false;
296-
}
295+
value: function () {
296+
var _streamStopped = _asyncToGenerator(
297+
/*#__PURE__*/
298+
regeneratorRuntime.mark(function _callee3() {
299+
var _ref3, canSwitch;
300+
301+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
302+
while (1) {
303+
switch (_context3.prev = _context3.next) {
304+
case 0:
305+
this.obsStreaming = false;
306+
_context3.next = 3;
307+
return this.canSwitch();
308+
309+
case 3:
310+
_ref3 = _context3.sent;
311+
canSwitch = _ref3.canSwitch;
312+
313+
if (canSwitch) {
314+
this.obs.setCurrentScene({
315+
"scene-name": this.offlineScene
316+
});
317+
}
318+
319+
case 6:
320+
case "end":
321+
return _context3.stop();
322+
}
323+
}
324+
}, _callee3, this);
325+
}));
326+
327+
function streamStopped() {
328+
return _streamStopped.apply(this, arguments);
329+
}
330+
331+
return streamStopped;
332+
}()
297333
}, {
298334
key: "streamStarted",
299335
value: function streamStarted() {
@@ -304,25 +340,25 @@ function (_EventEmitter) {
304340
value: function () {
305341
var _getSceneList = _asyncToGenerator(
306342
/*#__PURE__*/
307-
regeneratorRuntime.mark(function _callee3() {
343+
regeneratorRuntime.mark(function _callee4() {
308344
var list;
309-
return regeneratorRuntime.wrap(function _callee3$(_context3) {
345+
return regeneratorRuntime.wrap(function _callee4$(_context4) {
310346
while (1) {
311-
switch (_context3.prev = _context3.next) {
347+
switch (_context4.prev = _context4.next) {
312348
case 0:
313-
_context3.next = 2;
349+
_context4.next = 2;
314350
return this.obs.GetSceneList();
315351

316352
case 2:
317-
list = _context3.sent;
353+
list = _context4.sent;
318354
this.scenes = list.scenes;
319355

320356
case 4:
321357
case "end":
322-
return _context3.stop();
358+
return _context4.stop();
323359
}
324360
}
325-
}, _callee3, this);
361+
}, _callee4, this);
326362
}));
327363

328364
function getSceneList() {
@@ -342,6 +378,43 @@ function (_EventEmitter) {
342378
this.heartbeat = heartbeat;
343379
this.obsStreaming = heartbeat.streaming;
344380
}
381+
}, {
382+
key: "canSwitch",
383+
value: function () {
384+
var _canSwitch = _asyncToGenerator(
385+
/*#__PURE__*/
386+
regeneratorRuntime.mark(function _callee5() {
387+
var currentScene, canSwitch;
388+
return regeneratorRuntime.wrap(function _callee5$(_context5) {
389+
while (1) {
390+
switch (_context5.prev = _context5.next) {
391+
case 0:
392+
_context5.next = 2;
393+
return this.obs.GetCurrentScene();
394+
395+
case 2:
396+
currentScene = _context5.sent;
397+
canSwitch = currentScene.name == this.lowBitrateScene || currentScene.name == this.normalScene || currentScene.name == this.offlineScene;
398+
this.currentScene = currentScene.name;
399+
return _context5.abrupt("return", {
400+
currentScene: currentScene,
401+
canSwitch: canSwitch
402+
});
403+
404+
case 6:
405+
case "end":
406+
return _context5.stop();
407+
}
408+
}
409+
}, _callee5, this);
410+
}));
411+
412+
function canSwitch() {
413+
return _canSwitch.apply(this, arguments);
414+
}
415+
416+
return canSwitch;
417+
}()
345418
}]);
346419

347420
return ObsSwitcher;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nginx-obs-automatic-low-bitrate-switching",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "Simple app to automatically switch scenes based on the current bitrate on the nginx stats page.",
55
"main": "lib/app",
66
"scripts": {

src/components/Chat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ class Chat {
130130
parsed.tags.mod === "1" &&
131131
this.allowModsCommands.includes(commandName) &&
132132
this.rate != 20) ||
133-
(parsed.username === this.channel.substring(1) && this.rate != 20)
133+
(parsed.username === this.channel.substring(1) && this.rate != 20) ||
134+
commandName == "noalbs"
134135
) {
135136
if (this.commands.includes(commandName)) {
136137
this[commandName](parse[1]);

src/components/ObsSwitcher.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,8 @@ class ObsSwitcher extends EventEmitter {
6060
this.interval = setInterval(async () => {
6161
if (!this.obsStreaming) return;
6262

63-
const currentScene = await this.obs.GetCurrentScene();
6463
const bitrate = await this.getBitrate();
65-
const canSwitch =
66-
currentScene.name == this.lowBitrateScene || currentScene.name == this.normalScene || currentScene.name == this.offlineScene;
67-
68-
this.currentScene = currentScene.name;
64+
const { currentScene, canSwitch } = await this.canSwitch();
6965

7066
if (bitrate !== null) {
7167
this.isLive = true;
@@ -179,8 +175,16 @@ class ObsSwitcher extends EventEmitter {
179175
}, 5000);
180176
}
181177

182-
streamStopped() {
178+
async streamStopped() {
183179
this.obsStreaming = false;
180+
181+
const { canSwitch } = await this.canSwitch();
182+
183+
if (canSwitch) {
184+
this.obs.setCurrentScene({
185+
"scene-name": this.offlineScene
186+
});
187+
}
184188
}
185189

186190
streamStarted() {
@@ -200,6 +204,16 @@ class ObsSwitcher extends EventEmitter {
200204
this.heartbeat = heartbeat;
201205
this.obsStreaming = heartbeat.streaming;
202206
}
207+
208+
async canSwitch() {
209+
const currentScene = await this.obs.GetCurrentScene();
210+
const canSwitch =
211+
currentScene.name == this.lowBitrateScene || currentScene.name == this.normalScene || currentScene.name == this.offlineScene;
212+
213+
this.currentScene = currentScene.name;
214+
215+
return { currentScene, canSwitch };
216+
}
203217
}
204218

205219
export default ObsSwitcher;

0 commit comments

Comments
 (0)