Skip to content

Commit f081f35

Browse files
committed
Don't send scene switched messages in chat when not streaming from OBS
1 parent e35f524 commit f081f35

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

lib/components/ObsSwitcher.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function (_EventEmitter) {
7272
_this.bitrate = null;
7373
_this.nginxVideoMeta = null;
7474
_this.streamStatus = null;
75+
_this.obsStreaming = false;
7576
_this.currentScene = null;
7677

7778
_this.obs.connect({
@@ -90,6 +91,10 @@ function (_EventEmitter) {
9091

9192
_this.obs.on("StreamStatus", _this.setStreamStatus.bind(_assertThisInitialized(_assertThisInitialized(_this))));
9293

94+
_this.obs.on("StreamStopped", _this.streamStopped.bind(_assertThisInitialized(_assertThisInitialized(_this))));
95+
96+
_this.obs.on("StreamStarted", _this.streamStarted.bind(_assertThisInitialized(_assertThisInitialized(_this))));
97+
9398
log.info("Connecting & authenticating");
9499
return _this;
95100
}
@@ -127,16 +132,16 @@ function (_EventEmitter) {
127132
_this2.isLive = true;
128133
_this2.isLive && canSwitch && (bitrate === 0 && currentScene.name !== _this2.lowBitrateScene && (_this2.obs.setCurrentScene({
129134
"scene-name": _this2.lowBitrateScene
130-
}), _config.default.twitchChat.enableAutoSwitchNotification && _this2.emit("live"), log.info("Stream went online switching to scene: \"".concat(_this2.lowBitrateScene, "\""))), bitrate <= _this2.lowBitrateTrigger && currentScene.name !== _this2.lowBitrateScene && bitrate !== 0 && (_this2.obs.setCurrentScene({
135+
}), _this2.switchSceneEmit("live"), log.info("Stream went online switching to scene: \"".concat(_this2.lowBitrateScene, "\""))), bitrate <= _this2.lowBitrateTrigger && currentScene.name !== _this2.lowBitrateScene && bitrate !== 0 && (_this2.obs.setCurrentScene({
131136
"scene-name": _this2.lowBitrateScene
132-
}), _config.default.twitchChat.enableAutoSwitchNotification && _this2.emit("lowBitrateScene"), log.info("Low bitrate detected switching to scene: \"".concat(_this2.lowBitrateScene, "\""))), bitrate > _this2.lowBitrateTrigger && currentScene.name !== _this2.normalScene && (_this2.obs.setCurrentScene({
137+
}), _this2.switchSceneEmit("lowBitrateScene"), log.info("Low bitrate detected switching to scene: \"".concat(_this2.lowBitrateScene, "\""))), bitrate > _this2.lowBitrateTrigger && currentScene.name !== _this2.normalScene && (_this2.obs.setCurrentScene({
133138
"scene-name": _this2.normalScene
134-
}), _config.default.twitchChat.enableAutoSwitchNotification && _this2.emit("normalScene"), log.info("Switching to normal scene: \"".concat(_this2.normalScene, "\""))));
139+
}), _this2.switchSceneEmit("normalScene"), log.info("Switching to normal scene: \"".concat(_this2.normalScene, "\""))));
135140
} else {
136141
_this2.isLive = false;
137142
canSwitch && currentScene.name !== _this2.offlineScene && (_this2.obs.setCurrentScene({
138143
"scene-name": _this2.offlineScene
139-
}), _config.default.twitchChat.enableAutoSwitchNotification && _this2.emit("offlineScene"), _this2.streamStatus = null, log.warn("Error receiving current bitrate or stream is offline. Switching to offline scene: \"".concat(_this2.offlineScene, "\"")));
144+
}), _this2.switchSceneEmit("offlineScene"), _this2.streamStatus = null, log.warn("Error receiving current bitrate or stream is offline. Switching to offline scene: \"".concat(_this2.offlineScene, "\"")));
140145
}
141146

142147
case 9:
@@ -147,6 +152,13 @@ function (_EventEmitter) {
147152
}, _callee, this);
148153
})), _config.default.obs.requestMs);
149154
}
155+
}, {
156+
key: "switchSceneEmit",
157+
value: function switchSceneEmit(sceneName) {
158+
if (_config.default.twitchChat.enableAutoSwitchNotification && this.obsStreaming) {
159+
this.emit(sceneName);
160+
}
161+
}
150162
}, {
151163
key: "getBitrate",
152164
value: function () {
@@ -250,6 +262,16 @@ function (_EventEmitter) {
250262
});
251263
}, 5000);
252264
}
265+
}, {
266+
key: "streamStopped",
267+
value: function streamStopped() {
268+
this.obsStreaming = false;
269+
}
270+
}, {
271+
key: "streamStarted",
272+
value: function streamStarted() {
273+
this.obsStreaming = true;
274+
}
253275
}]);
254276

255277
return ObsSwitcher;

src/components/ObsSwitcher.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ObsSwitcher extends EventEmitter {
2828
this.bitrate = null;
2929
this.nginxVideoMeta = null;
3030
this.streamStatus = null;
31+
this.obsStreaming = false;
3132
this.currentScene = null;
3233

3334
this.obs.connect({ address: this.address, password: this.password }).catch(e => {
@@ -39,6 +40,8 @@ class ObsSwitcher extends EventEmitter {
3940
this.obs.on("AuthenticationFailure", this.onAuthFail.bind(this));
4041
this.obs.on("error", this.error.bind(this));
4142
this.obs.on("StreamStatus", this.setStreamStatus.bind(this));
43+
this.obs.on("StreamStopped", this.streamStopped.bind(this));
44+
this.obs.on("StreamStarted", this.streamStarted.bind(this));
4245

4346
log.info("Connecting & authenticating");
4447
}
@@ -64,34 +67,40 @@ class ObsSwitcher extends EventEmitter {
6467
(this.obs.setCurrentScene({
6568
"scene-name": this.lowBitrateScene
6669
}),
67-
config.twitchChat.enableAutoSwitchNotification && this.emit("live"),
70+
this.switchSceneEmit("live"),
6871
log.info(`Stream went online switching to scene: "${this.lowBitrateScene}"`)),
6972
bitrate <= this.lowBitrateTrigger &&
7073
currentScene.name !== this.lowBitrateScene &&
7174
bitrate !== 0 &&
7275
(this.obs.setCurrentScene({
7376
"scene-name": this.lowBitrateScene
7477
}),
75-
config.twitchChat.enableAutoSwitchNotification && this.emit("lowBitrateScene"),
78+
this.switchSceneEmit("lowBitrateScene"),
7679
log.info(`Low bitrate detected switching to scene: "${this.lowBitrateScene}"`)),
7780
bitrate > this.lowBitrateTrigger &&
7881
currentScene.name !== this.normalScene &&
7982
(this.obs.setCurrentScene({ "scene-name": this.normalScene }),
80-
config.twitchChat.enableAutoSwitchNotification && this.emit("normalScene"),
83+
this.switchSceneEmit("normalScene"),
8184
log.info(`Switching to normal scene: "${this.normalScene}"`)));
8285
} else {
8386
this.isLive = false;
8487

8588
canSwitch &&
8689
currentScene.name !== this.offlineScene &&
8790
(this.obs.setCurrentScene({ "scene-name": this.offlineScene }),
88-
config.twitchChat.enableAutoSwitchNotification && this.emit("offlineScene"),
91+
this.switchSceneEmit("offlineScene"),
8992
(this.streamStatus = null),
9093
log.warn(`Error receiving current bitrate or stream is offline. Switching to offline scene: "${this.offlineScene}"`));
9194
}
9295
}, config.obs.requestMs);
9396
}
9497

98+
switchSceneEmit(sceneName) {
99+
if (config.twitchChat.enableAutoSwitchNotification && this.obsStreaming) {
100+
this.emit(sceneName);
101+
}
102+
}
103+
95104
async getBitrate(username = "live") {
96105
try {
97106
const response = await fetch(`http://${config.nginx.ip}/stat`);
@@ -145,6 +154,14 @@ class ObsSwitcher extends EventEmitter {
145154
});
146155
}, 5000);
147156
}
157+
158+
streamStopped() {
159+
this.obsStreaming = false;
160+
}
161+
162+
streamStarted() {
163+
this.obsStreaming = true;
164+
}
148165
}
149166

150167
export default ObsSwitcher;

0 commit comments

Comments
 (0)