2020-11-01 02:35:14 +00:00
|
|
|
"""
|
|
|
|
BAPSicle Server
|
|
|
|
Next-gen audio playout server for University Radio York playout,
|
|
|
|
based on WebStudio interface.
|
|
|
|
|
|
|
|
Flask Server
|
|
|
|
|
|
|
|
Authors:
|
|
|
|
Matthew Stratford
|
|
|
|
Michael Grace
|
|
|
|
|
|
|
|
Date:
|
|
|
|
October, November 2020
|
|
|
|
"""
|
2020-10-23 20:10:32 +00:00
|
|
|
import multiprocessing
|
2021-04-17 20:28:57 +00:00
|
|
|
from multiprocessing.queues import Queue
|
2020-12-20 01:10:19 +00:00
|
|
|
import time
|
2021-04-17 20:28:57 +00:00
|
|
|
from typing import Any
|
2020-10-23 20:10:32 +00:00
|
|
|
import json
|
2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-11 18:02:19 +00:00
|
|
|
from helpers.os_environment import isBundelled, isMacOS
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2020-11-04 22:38:31 +00:00
|
|
|
if not isMacOS():
|
|
|
|
# Rip, this doesn't like threading on MacOS.
|
|
|
|
import pyttsx3
|
|
|
|
|
2021-04-11 19:27:46 +00:00
|
|
|
if isBundelled():
|
|
|
|
import build
|
|
|
|
|
2020-11-03 23:25:17 +00:00
|
|
|
import config
|
|
|
|
from typing import Dict, List
|
2020-11-09 00:10:36 +00:00
|
|
|
from helpers.state_manager import StateManager
|
|
|
|
from helpers.logging_manager import LoggingManager
|
2020-11-15 17:40:18 +00:00
|
|
|
from websocket_server import WebsocketServer
|
2021-04-17 20:28:57 +00:00
|
|
|
from web_server import WebServer
|
|
|
|
from player_handler import PlayerHandler
|
2021-04-17 21:51:43 +00:00
|
|
|
from api_handler import APIHandler
|
|
|
|
from controllers.mattchbox_usb import MattchBox
|
|
|
|
from helpers.the_terminator import Terminator
|
|
|
|
import player
|
2020-11-03 23:25:17 +00:00
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
setproctitle("server.py")
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
|
|
|
|
class BAPSicleServer:
|
2021-04-17 20:47:46 +00:00
|
|
|
|
|
|
|
default_state = {
|
|
|
|
"server_version": "",
|
|
|
|
"server_build": "",
|
|
|
|
"server_name": "URY BAPSicle",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 13500,
|
|
|
|
"ws_port": 13501,
|
|
|
|
"num_channels": 3,
|
|
|
|
"serial_port": None,
|
|
|
|
"ser_connected": False,
|
|
|
|
"myradio_api_key": None,
|
|
|
|
"myradio_base_url": "https://ury.org.uk/myradio",
|
|
|
|
"myradio_api_url": "https://ury.org.uk/api"
|
|
|
|
}
|
|
|
|
|
|
|
|
player_to_q: List[Queue] = []
|
|
|
|
player_from_q: List[Queue] = []
|
|
|
|
ui_to_q: List[Queue] = []
|
|
|
|
websocket_to_q: List[Queue] = []
|
|
|
|
controller_to_q: List[Queue] = []
|
|
|
|
api_from_q: Queue
|
|
|
|
api_to_q: Queue
|
|
|
|
|
|
|
|
player: List[multiprocessing.Process] = []
|
|
|
|
websockets_server: multiprocessing.Process
|
|
|
|
controller_handler: multiprocessing.Process
|
|
|
|
player_handler: multiprocessing.Process
|
|
|
|
webserver: multiprocessing.Process
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
def __init__(self):
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2021-04-17 20:47:46 +00:00
|
|
|
self.startServer()
|
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
terminator = Terminator()
|
|
|
|
while not terminator.terminate:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.stopServer()
|
|
|
|
|
2021-04-17 20:47:46 +00:00
|
|
|
def startServer(self):
|
|
|
|
if isMacOS():
|
|
|
|
multiprocessing.set_start_method("spawn", True)
|
|
|
|
|
|
|
|
process_title = "startServer"
|
|
|
|
setproctitle(process_title)
|
|
|
|
# multiprocessing.current_process().name = process_title
|
|
|
|
|
|
|
|
self.logger = LoggingManager("BAPSicleServer")
|
|
|
|
|
|
|
|
self.state = StateManager("BAPSicleServer", self.logger, self.default_state)
|
2021-04-17 21:51:43 +00:00
|
|
|
|
2021-04-17 20:47:46 +00:00
|
|
|
build_commit = "Dev"
|
|
|
|
if isBundelled():
|
|
|
|
build_commit = build.BUILD
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
print("Launching BAPSicle...")
|
|
|
|
|
|
|
|
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
|
|
|
|
self.state.update("server_version", config.VERSION)
|
2021-04-17 20:47:46 +00:00
|
|
|
self.state.update("server_build", build_commit)
|
|
|
|
|
|
|
|
for channel in range(self.state.state["num_channels"]):
|
|
|
|
|
|
|
|
self.player_to_q.append(multiprocessing.Queue())
|
|
|
|
self.player_from_q.append(multiprocessing.Queue())
|
|
|
|
self.ui_to_q.append(multiprocessing.Queue())
|
|
|
|
self.websocket_to_q.append(multiprocessing.Queue())
|
|
|
|
self.controller_to_q.append(multiprocessing.Queue())
|
|
|
|
|
|
|
|
# TODO Replace state with individual read-only StateManagers or something nicer?
|
|
|
|
|
|
|
|
self.player.append(
|
|
|
|
multiprocessing.Process(
|
|
|
|
target=player.Player,
|
|
|
|
args=(channel, self.player_to_q[-1], self.player_from_q[-1], self.state)
|
|
|
|
)
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
2021-04-17 20:47:46 +00:00
|
|
|
self.player[channel].start()
|
|
|
|
|
|
|
|
self.api_to_q = multiprocessing.Queue()
|
|
|
|
self.api_from_q = multiprocessing.Queue()
|
|
|
|
self.api_handler = multiprocessing.Process(
|
|
|
|
target=APIHandler, args=(self.api_to_q, self.api_from_q, self.state)
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
2021-04-17 20:47:46 +00:00
|
|
|
self.api_handler.start()
|
|
|
|
|
|
|
|
self.player_handler = multiprocessing.Process(
|
|
|
|
target=PlayerHandler,
|
|
|
|
args=(self.player_from_q, self.websocket_to_q, self.ui_to_q, self.controller_to_q),
|
|
|
|
)
|
|
|
|
self.player_handler.start()
|
|
|
|
|
|
|
|
# Note, state here will become a copy in the process.
|
|
|
|
# It will not update, and callbacks will not work :/
|
2021-04-17 21:51:43 +00:00
|
|
|
self.websockets_server = multiprocessing.Process(
|
2021-04-17 20:47:46 +00:00
|
|
|
target=WebsocketServer, args=(self.player_to_q, self.websocket_to_q, self.state)
|
|
|
|
)
|
2021-04-17 21:51:43 +00:00
|
|
|
self.websockets_server.start()
|
2021-04-17 20:47:46 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
self.controller_handler = multiprocessing.Process(
|
2021-04-17 20:47:46 +00:00
|
|
|
target=MattchBox, args=(self.player_to_q, self.controller_to_q, self.state)
|
|
|
|
)
|
2021-04-17 21:51:43 +00:00
|
|
|
self.controller_handler.start()
|
2021-04-17 20:47:46 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
self.webserver = multiprocessing.Process(
|
2021-04-17 20:47:46 +00:00
|
|
|
target=WebServer, args=(self.player_to_q, self.ui_to_q, self.api_to_q, self.api_from_q, self.state)
|
|
|
|
)
|
2021-04-17 21:51:43 +00:00
|
|
|
self.webserver.start()
|
|
|
|
|
|
|
|
print("Welcome to BAPSicle Server version: {}, build: {}.".format(config.VERSION, build_commit))
|
|
|
|
print("The Server UI is available at http://{}:{}".format(self.state.state["host"], self.state.state["port"]))
|
2021-04-17 20:47:46 +00:00
|
|
|
|
|
|
|
# TODO Move this to player or installer.
|
|
|
|
if False:
|
|
|
|
if not isMacOS():
|
|
|
|
|
|
|
|
# Temporary RIP.
|
|
|
|
|
|
|
|
# Welcome Speech
|
|
|
|
|
|
|
|
text_to_speach = pyttsx3.init()
|
|
|
|
text_to_speach.save_to_file(
|
|
|
|
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
|
|
|
|
By default, this server is accepting connections on port 13500
|
|
|
|
The version of the server service is {}
|
|
|
|
Please refer to the documentation included with this application for further assistance.""".format(
|
|
|
|
config.VERSION
|
|
|
|
),
|
|
|
|
"dev/welcome.mp3",
|
|
|
|
)
|
|
|
|
text_to_speach.runAndWait()
|
|
|
|
|
|
|
|
new_item: Dict[str, Any] = {
|
|
|
|
"channel_weight": 0,
|
|
|
|
"filename": "dev/welcome.mp3",
|
|
|
|
"title": "Welcome to BAPSicle",
|
|
|
|
"artist": "University Radio York",
|
|
|
|
}
|
|
|
|
|
|
|
|
self.player_to_q[0].put("ADD:" + json.dumps(new_item))
|
|
|
|
self.player_to_q[0].put("LOAD:0")
|
|
|
|
self.player_to_q[0].put("PLAY")
|
|
|
|
|
|
|
|
def stopServer(self):
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping BASPicle Server.")
|
2021-04-17 20:47:46 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping Websocket Server")
|
2021-04-17 20:47:46 +00:00
|
|
|
self.websocket_to_q[0].put("WEBSOCKET:QUIT")
|
|
|
|
if self.websockets_server:
|
|
|
|
self.websockets_server.join()
|
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping Players")
|
2021-04-17 20:47:46 +00:00
|
|
|
for q in self.player_to_q:
|
|
|
|
q.put("ALL:QUIT")
|
|
|
|
|
|
|
|
for player in self.player:
|
|
|
|
player.join()
|
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping Web Server")
|
|
|
|
if self.webserver:
|
|
|
|
self.webserver.terminate()
|
|
|
|
self.webserver.join()
|
2021-04-17 20:47:46 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping Player Handler")
|
2021-04-17 20:47:46 +00:00
|
|
|
if self.player_handler:
|
|
|
|
self.player_handler.terminate()
|
|
|
|
self.player_handler.join()
|
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping API Handler")
|
|
|
|
if self.api_handler:
|
|
|
|
self.api_handler.terminate()
|
|
|
|
self.api_handler.join()
|
2021-04-17 20:47:46 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
print("Stopping Controllers")
|
|
|
|
if self.controller_handler:
|
|
|
|
self.controller_handler.terminate()
|
|
|
|
self.controller_handler.join()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-05 21:13:53 +00:00
|
|
|
raise Exception("BAPSicle is a service. Please run it like one.")
|