Use new backend seperation system

This commit is contained in:
Core447 2024-01-14 11:50:34 +01:00
parent 6fd75d54a4
commit 0b719a4352
No known key found for this signature in database
GPG Key ID: 5B97C0BD62F98516
6 changed files with 192 additions and 169 deletions

View File

@ -66,7 +66,7 @@ class OBSActionBase(ActionBase):
print("reconnecing obs") 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) 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.set_label("Successfully connected to OBS")
self.status_label.remove_css_class("red") self.status_label.remove_css_class("red")
self.status_label.add_css_class("green") self.status_label.add_css_class("green")

View File

@ -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()

View File

@ -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)

View File

@ -184,7 +184,7 @@ class OBSController(obsws):
def register(self, *args, **kwargs): def register(self, *args, **kwargs):
""" """
Pass all event register calls to the event_obs. 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: try:
self.event_obs.register(*args, **kwargs) self.event_obs.register(*args, **kwargs)

52
backend/backend.py Normal file
View File

@ -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()

172
main.py
View File

@ -1,3 +1,4 @@
from plugins.dev_core447_OBSPlugin.backend.OBSController import OBSController
from src.backend.PluginManager.ActionBase import ActionBase from src.backend.PluginManager.ActionBase import ActionBase
from src.backend.PluginManager.PluginBase import PluginBase from src.backend.PluginManager.PluginBase import PluginBase
@ -17,174 +18,10 @@ from obswebsocket import events
# Add plugin to sys.paths # Add plugin to sys.paths
sys.path.append(os.path.dirname(__file__)) sys.path.append(os.path.dirname(__file__))
from OBSController import OBSController
from OBSActionBase import OBSActionBase from OBSActionBase import OBSActionBase
class ToggleRecord(OBSActionBase): from actions.ToggleRecord.ToggleRecord import ToggleRecord
ACTION_NAME = "Toggle Record" from actions.RecPlayPause.RecPlayPause import RecPlayPause
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()
class OBS(PluginBase): class OBS(PluginBase):
def __init__(self): def __init__(self):
@ -192,7 +29,8 @@ class OBS(PluginBase):
self.GITHUB_REPO = "https://github.com/your-github-repo" self.GITHUB_REPO = "https://github.com/your-github-repo"
super().__init__() 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(ToggleRecord)
self.add_action(RecPlayPause) self.add_action(RecPlayPause)