aboutsummaryrefslogtreecommitdiffstats
path: root/deckit
diff options
context:
space:
mode:
Diffstat (limited to 'deckit')
-rw-r--r--deckit/modules/obs.py15
-rw-r--r--deckit/obsclient.py17
2 files changed, 29 insertions, 3 deletions
diff --git a/deckit/modules/obs.py b/deckit/modules/obs.py
index 96217c4..04c49ef 100644
--- a/deckit/modules/obs.py
+++ b/deckit/modules/obs.py
@@ -1,10 +1,22 @@
"""OBS stream-deck: recording, scenes, audio channels, transitions and sources."""
+import json
import threading
+from pathlib import Path
from ..module import Module, ModuleError
from ..obsclient import ObsClient
+OBS_CONFIG = Path.home() / ".config" / "obs-studio" / "plugin_config" / "obs-websocket" / "config.json"
+
+
+def _obs_password(path=OBS_CONFIG) -> str:
+ """OBS stores its websocket password in plain text here."""
+ try:
+ return json.loads(Path(path).read_text()).get("server_password") or ""
+ except (OSError, ValueError):
+ return ""
+
AUDIO_KINDS = {
"pulse_input_capture", "pulse_output_capture", "alsa_input_capture", "alsa_output_capture",
"wasapi_input_capture", "wasapi_output_capture", "coreaudio_input_capture", "coreaudio_output_capture",
@@ -15,7 +27,8 @@ AUDIO_KINDS = {
def make(cfg: dict) -> Module:
o = cfg.get("obs", {})
client = cfg.get("client") or ObsClient(
- host=o.get("host", "127.0.0.1"), port=o.get("port", 4455), password=o.get("password", ""))
+ host=o.get("host", "127.0.0.1"), port=o.get("port", 4455),
+ password=o.get("password") or _obs_password())
lock = threading.Lock()
stop = threading.Event()
last_error = {"msg": None}
diff --git a/deckit/obsclient.py b/deckit/obsclient.py
index 181225a..87eefff 100644
--- a/deckit/obsclient.py
+++ b/deckit/obsclient.py
@@ -30,18 +30,31 @@ class ObsClient:
self._stop = threading.Event()
self._thread = None
+ def _recv_json(self) -> dict:
+ raw = self.ws.recv()
+ if not raw:
+ raise ModuleError("OBS closed the connection")
+ try:
+ return json.loads(raw)
+ except ValueError:
+ raise ModuleError("OBS sent a non-JSON reply")
+
def connect(self):
import websocket # only needed when actually talking to OBS
self.ws = websocket.create_connection(f"ws://{self.host}:{self.port}",
timeout=5, suppress_origin=True)
- hello = json.loads(self.ws.recv())
+ hello = self._recv_json()
auth = {}
if hello.get("d", {}).get("authentication"):
a = hello["d"]["authentication"]
auth["authentication"] = auth_string(self.password, a["salt"], a["challenge"])
self.ws.send(json.dumps({"op": 1, "d": {"rpcVersion": 1, **auth, "eventSubscriptions": 0xFFFFFFFF}}))
- if json.loads(self.ws.recv()).get("op") != 2:
+ try:
+ identified = self._recv_json()
+ except ModuleError:
+ raise ModuleError("OBS rejected the password" if auth else "OBS did not identify")
+ if identified.get("op") != 2:
raise ModuleError("OBS did not identify")
self.connected = True
self._stop.clear()