dnd-music-console/croaker/controller.py

74 lines
2.2 KiB
Python
Raw Normal View History

2024-03-01 01:00:17 -08:00
import logging
import queue
2024-03-05 22:15:51 -08:00
import threading
from croaker.playlist import load_playlist
from croaker.streamer import AudioStreamer
2024-03-05 22:21:56 -08:00
logger = logging.getLogger('controller')
class Controller(threading.Thread):
2024-03-05 23:25:21 -08:00
"""
A background thread started by the CroakerServer instance that controls a
shoutcast source streamer. The primary purpose of this class is to allow
the command and control server to interrupt streaming operations to
skip to a new track or load a new playlist.
"""
def __init__(self, control_queue):
self._streamer_queue = None
self._control_queue = control_queue
self.skip_event = threading.Event()
self.stop_event = threading.Event()
self._streamer = None
super().__init__()
@property
def streamer(self):
if not self._streamer:
self._streamer_queue = queue.Queue()
self._streamer = AudioStreamer(self._streamer_queue, self.skip_event, self.stop_event)
return self._streamer
2024-03-05 23:25:21 -08:00
def stop(self):
if self._streamer:
logging.debug("Sending STOP signal to streamer...")
self.stop_event.set()
self.playlist = None
def load(self, playlist_name: str):
self.playlist = load_playlist(playlist_name)
logger.debug(f"Switching to {self.playlist = }")
for track in self.playlist.tracks:
self._streamer_queue.put(str(track).encode())
def run(self):
2024-03-05 22:21:56 -08:00
logger.debug("Starting AudioStreamer...")
self.streamer.start()
2024-03-05 22:15:51 -08:00
self.load("session_start")
while True:
data = self._control_queue.get()
2024-03-05 22:21:56 -08:00
logger.debug(f"{data = }")
self.process_request(data)
def process_request(self, data):
2024-03-05 22:15:51 -08:00
cmd, *args = data.split(" ")
cmd = cmd.strip()
if not cmd:
return
handler = getattr(self, f"handle_{cmd}", None)
if not handler:
2024-03-05 22:21:56 -08:00
logger.debug("Ignoring invalid command: {cmd} = }")
return
handler(args)
def handle_PLAY(self, args):
return self.load(args[0])
def handle_FFWD(self, args):
2024-03-05 22:21:56 -08:00
logger.debug("Sending SKIP signal to streamer...")
self.skip_event.set()
def handle_STOP(self):
return self.stop()