From 0b719a4352937236c7c745d6cc3df04f754da046 Mon Sep 17 00:00:00 2001 From: Core447 Date: Sun, 14 Jan 2024 11:50:34 +0100 Subject: [PATCH] Use new backend seperation system --- OBSActionBase.py | 2 +- actions/RecPlayPause/RecPlayPause.py | 59 +++++++ actions/ToggleRecord/ToggleRecord.py | 74 ++++++++ OBSController.py => backend/OBSController.py | 2 +- backend/backend.py | 52 ++++++ main.py | 172 +------------------ 6 files changed, 192 insertions(+), 169 deletions(-) create mode 100644 actions/RecPlayPause/RecPlayPause.py create mode 100644 actions/ToggleRecord/ToggleRecord.py rename OBSController.py => backend/OBSController.py (99%) create mode 100644 backend/backend.py diff --git a/OBSActionBase.py b/OBSActionBase.py index db9746d..b836717 100644 --- a/OBSActionBase.py +++ b/OBSActionBase.py @@ -66,7 +66,7 @@ class OBSActionBase(ActionBase): print("reconnecing obs") self.PLUGIN_BASE.obs.connect_to(host=self.PLUGIN_BASE.get_settings()["ip"], port=self.PLUGIN_BASE.get_settings()["port"], password=self.PLUGIN_BASE.get_settings()["password"], timeout=3, legacy=False) - if self.PLUGIN_BASE.obs.connected: + if self.PLUGIN_BASE.backend.get_connected(): self.status_label.set_label("Successfully connected to OBS") self.status_label.remove_css_class("red") self.status_label.add_css_class("green") diff --git a/actions/RecPlayPause/RecPlayPause.py b/actions/RecPlayPause/RecPlayPause.py new file mode 100644 index 0000000..770b00c --- /dev/null +++ b/actions/RecPlayPause/RecPlayPause.py @@ -0,0 +1,59 @@ +from plugins.dev_core447_OBSPlugin.OBSActionBase import OBSActionBase +import os + +class RecPlayPause(OBSActionBase): + ACTION_NAME = "Recording Play/Pause" + CONTROLS_KEY_IMAGE = True + def __init__(self, deck_controller, page, coords): + super().__init__(deck_controller=deck_controller, page=page, coords=coords) + + def on_ready(self): + # Connect to obs if not connected + if not self.PLUGIN_BASE.backend.get_connected(): + # self.PLUGIN_BASE.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) + self.reconnect_obs() + + # Show current rec status + self.show_current_rec_status() + + def show_current_rec_status(self, new_paused = False): + if not self.PLUGIN_BASE.backend.get_connected(): + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + status = self.PLUGIN_BASE.backend.get_record_status() + if status is None: + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + if status["active"] and not status["paused"]: + self.show_for_state(1) + elif status["paused"]: + self.show_for_state(2) + else: + self.show_for_state(0) + + def show_for_state(self, state: int): + """ + 0: Not Recording + 1: Recording + 2: Paused + 3: Stopping in progress + """ + image = "record_inactive.png" + if state == 1: + self.set_bottom_label("Pause", font_size=16) + image = "record_pause.png" + if state == 2: + self.set_bottom_label("Resume", font_size=16) + image = "record_resume.png" + else: + self.set_bottom_label(None) + + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", image)) + + def on_key_down(self): + if not self.PLUGIN_BASE.backend.get_connected(): + return + self.PLUGIN_BASE.backend.toggle_record_pause() + + def on_tick(self): + self.show_current_rec_status() \ No newline at end of file diff --git a/actions/ToggleRecord/ToggleRecord.py b/actions/ToggleRecord/ToggleRecord.py new file mode 100644 index 0000000..0d9822d --- /dev/null +++ b/actions/ToggleRecord/ToggleRecord.py @@ -0,0 +1,74 @@ +# from ...OBSActionBase import OBSActionBase +from plugins.dev_core447_OBSPlugin.OBSActionBase import OBSActionBase + +import os +class ToggleRecord(OBSActionBase): + ACTION_NAME = "Toggle Record" + CONTROLS_KEY_IMAGE = True + def __init__(self, deck_controller, page, coords): + super().__init__(deck_controller=deck_controller, page=page, coords=coords) + + def on_ready(self): + # Connect to obs if not connected + if not self.PLUGIN_BASE.backend.get_connected(): # self.PLUGIN_BASE.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) + self.reconnect_obs() + + # Show current rec status + self.show_current_rec_status() + + def show_current_rec_status(self, new_paused = False): + if not self.PLUGIN_BASE.backend.get_connected(): + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + status = self.PLUGIN_BASE.backend.get_record_status() + if status is None: + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + if status["paused"]: + self.show_for_state(2) + elif status["active"]: + self.show_for_state(1) + else: + self.show_for_state(0) + + def show_for_state(self, state: int): + """ + 0: Not Recording + 1: Recording + 2: Paused + 3: Stopping in progress + """ + image = "record_inactive.png" + if state == 0: + self.set_bottom_label(None) + image = "record_inactive.png" + elif state == 1: + self.show_rec_time() + image = "record_active.png" + print("active") + elif state == 2: + self.show_rec_time() + image = "record_resume.png" + + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", image)) + + def on_key_down(self): + if not self.PLUGIN_BASE.backend.get_connected(): + return + self.PLUGIN_BASE.backend.toggle_record() + + def on_tick(self): + self.show_current_rec_status() + + def show_rec_time(self): + if not self.PLUGIN_BASE.backend.get_connected(): + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + status = self.PLUGIN_BASE.backend.get_record_status() + if status is None: + self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) + return + if not status["active"]: + self.set_bottom_label(None) + return + self.set_bottom_label(status["timecode"][:-4], font_size=16) \ No newline at end of file diff --git a/OBSController.py b/backend/OBSController.py similarity index 99% rename from OBSController.py rename to backend/OBSController.py index 22445ec..4436633 100644 --- a/OBSController.py +++ b/backend/OBSController.py @@ -184,7 +184,7 @@ class OBSController(obsws): def register(self, *args, **kwargs): """ Pass all event register calls to the event_obs. - This avoid crashes if a request is made in an event + This avoids crashes if a request is made in an event """ try: self.event_obs.register(*args, **kwargs) diff --git a/backend/backend.py b/backend/backend.py new file mode 100644 index 0000000..48559c4 --- /dev/null +++ b/backend/backend.py @@ -0,0 +1,52 @@ +from streamcontroller_plugin_tools import BackendBase + +from OBSController import OBSController +from obswebsocket import events +import os +import threading +import Pyro5.api + +@Pyro5.api.expose +class Backend(BackendBase): + def __init__(self): + super().__init__() + self.OBSController = OBSController() + self.OBSController.connect_to( + host=self.frontend.get_settings()["ip"], + port=self.frontend.get_settings()["port"], + password=self.frontend.get_settings()["password"] + ) + + """ + Wrapper methods around OBSController aiming to allow a communication + between the frontend and the backend in default python data types + """ + + def get_record_status(self) -> dict: + status = self.OBSController.get_record_status() + if status is None: + return + return { + "active": status.datain["outputActive"], + "paused": status.datain["outputPaused"], + "timecode": status.datain["outputTimecode"], + "duration": status.datain["outputDuration"], + "bytes": status.datain["outputBytes"] + } + + def get_connected(self) -> bool: + return self.OBSController.connected + + def toggle_record(self): + self.OBSController.toggle_record() + + def toggle_record_pause(self): + self.OBSController.toggle_record_pause() + + +backend = Backend() + + +for t in threading.enumerate(): + if t.daemon: + t.join() \ No newline at end of file diff --git a/main.py b/main.py index a291924..8957e45 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from plugins.dev_core447_OBSPlugin.backend.OBSController import OBSController from src.backend.PluginManager.ActionBase import ActionBase from src.backend.PluginManager.PluginBase import PluginBase @@ -17,174 +18,10 @@ from obswebsocket import events # Add plugin to sys.paths sys.path.append(os.path.dirname(__file__)) -from OBSController import OBSController from OBSActionBase import OBSActionBase -class ToggleRecord(OBSActionBase): - ACTION_NAME = "Toggle Record" - CONTROLS_KEY_IMAGE = True - def __init__(self, deck_controller, page, coords): - super().__init__(deck_controller=deck_controller, page=page, coords=coords) - - def on_ready(self): - # Connect to obs if not connected - if not self.PLUGIN_BASE.obs.connected: - # self.PLUGIN_BASE.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) - self.reconnect_obs() - - # Show current rec status - self.show_current_rec_status() - - # Register signal - self.PLUGIN_BASE.obs.register(self.on_state_change, events.RecordStateChanged) - - - def on_state_change(self, message): - state_string = message.datain["outputState"] - state = 0 - if state_string == "OBS_WEBSOCKET_OUTPUT_STOPPED": - state = 0 - elif state_string in ["OBS_WEBSOCKET_OUTPUT_STARTING", "OBS_WEBSOCKET_OUTPUT_STARTED", "OBS_WEBSOCKET_OUTPUT_RESUMED"]: - state = 1 - elif state_string == "OBS_WEBSOCKET_OUTPUT_PAUSED": - state = 2 - - self.show_for_state(state) - - def show_current_rec_status(self, new_paused = False): - if not self.PLUGIN_BASE.obs.connected: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - status = self.PLUGIN_BASE.obs.get_record_status() - if status is None: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - active = status.datain["outputActive"] - paused = status.datain["outputPaused"] - if paused: - self.show_for_state(2) - elif active: - self.show_for_state(1) - else: - self.show_for_state(0) - - def show_for_state(self, state: int): - """ - 0: Not Recording - 1: Recording - 2: Paused - 3: Stopping in progress - """ - image = "record_inactive.png" - if state == 0: - self.set_bottom_label(None) - image = "record_inactive.png" - elif state == 1: - self.show_rec_time() - image = "record_active.png" - print("active") - elif state == 2: - self.show_rec_time() - image = "record_resume.png" - - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", image)) - - def on_key_down(self): - if not self.PLUGIN_BASE.obs.connected: - return - self.PLUGIN_BASE.obs.toggle_record() - - def on_tick(self): - self.show_current_rec_status() - - def show_rec_time(self): - if not self.PLUGIN_BASE.obs.connected: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - status = self.PLUGIN_BASE.obs.get_record_status() - if status is None: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - active = status.datain["outputActive"] - if not active: - self.set_bottom_label(None) - return - self.set_bottom_label(status.datain["outputTimecode"][:-4], font_size=16) - -class RecPlayPause(OBSActionBase): - ACTION_NAME = "Recording Play/Pause" - CONTROLS_KEY_IMAGE = True - def __init__(self, deck_controller, page, coords): - super().__init__(deck_controller=deck_controller, page=page, coords=coords) - - def on_ready(self): - # Connect to obs if not connected - if not self.PLUGIN_BASE.obs.connected: - # self.PLUGIN_BASE.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) - self.reconnect_obs() - - # Show current rec status - self.show_current_rec_status() - - # Register signal - self.PLUGIN_BASE.obs.register(self.on_state_change, events.RecordStateChanged) - - - def on_state_change(self, message): - state_string = message.datain["outputState"] - state = 0 - if state_string in ["OBS_WEBSOCKET_OUTPUT_RESUMED", "OBS_WEBSOCKET_OUTPUT_STARTED"]: - state = 1 - if state_string == "OBS_WEBSOCKET_OUTPUT_PAUSED": - state = 2 - - print(state_string) - - self.show_for_state(state) - - def show_current_rec_status(self, new_paused = False): - if not self.PLUGIN_BASE.obs.connected: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - status = self.PLUGIN_BASE.obs.get_record_status() - if status is None: - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", "error.png")) - return - active = status.datain["outputActive"] - paused = status.datain["outputPaused"] - if active and not paused: - self.show_for_state(1) - elif paused: - self.show_for_state(2) - else: - self.show_for_state(0) - - def show_for_state(self, state: int): - """ - 0: Not Recording - 1: Recording - 2: Paused - 3: Stopping in progress - """ - image = "record_inactive.png" - if state == 1: - self.set_bottom_label("Pause", font_size=16) - image = "record_pause.png" - if state == 2: - self.set_bottom_label("Resume", font_size=16) - image = "record_resume.png" - else: - self.set_bottom_label(None) - - self.set_key(media_path=os.path.join(self.PLUGIN_BASE.PATH, "assets", image)) - - def on_key_down(self): - if not self.PLUGIN_BASE.obs.connected: - return - self.PLUGIN_BASE.obs.toggle_record_pause() - - def on_tick(self): - self.show_current_rec_status() +from actions.ToggleRecord.ToggleRecord import ToggleRecord +from actions.RecPlayPause.RecPlayPause import RecPlayPause class OBS(PluginBase): def __init__(self): @@ -192,7 +29,8 @@ class OBS(PluginBase): self.GITHUB_REPO = "https://github.com/your-github-repo" super().__init__() - self.obs = OBSController() + # Launch backend + self.launch_backend(os.path.join(self.PATH, "backend", "backend.py")) self.add_action(ToggleRecord) self.add_action(RecPlayPause)