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
|
|
|
|
"""
|
2021-03-21 13:05:33 +00:00
|
|
|
from api_handler import APIHandler
|
2021-02-14 23:58:32 +00:00
|
|
|
from controllers.mattchbox_usb import MattchBox
|
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
|
2020-10-24 13:44:26 +00:00
|
|
|
import player
|
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
|
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:
|
2020-10-24 20:31:52 +00:00
|
|
|
def __init__(self):
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
startServer()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
# def get_flask(self):
|
|
|
|
# return app
|
2020-10-26 21:25:02 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
default_state = {
|
2021-04-11 19:27:46 +00:00
|
|
|
"server_version": "",
|
|
|
|
"server_build": "",
|
2021-04-04 21:34:46 +00:00
|
|
|
"server_name": "URY BAPSicle",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 13500,
|
|
|
|
"ws_port": 13501,
|
2021-04-05 21:13:53 +00:00
|
|
|
"num_channels": 3,
|
2021-04-10 23:12:15 +00:00
|
|
|
"serial_port": None,
|
2021-04-05 21:13:53 +00:00
|
|
|
"ser_connected": False,
|
2021-04-11 18:02:19 +00:00
|
|
|
"myradio_api_key": None,
|
2021-04-10 22:59:49 +00:00
|
|
|
"myradio_base_url": "https://ury.org.uk/myradio",
|
|
|
|
"myradio_api_url": "https://ury.org.uk/api"
|
2021-04-04 21:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
channel_to_q: List[Queue] = []
|
|
|
|
channel_from_q: List[Queue] = []
|
|
|
|
ui_to_q: List[Queue] = []
|
|
|
|
websocket_to_q: List[Queue] = []
|
|
|
|
controller_to_q: List[Queue] = []
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
channel_p: List[multiprocessing.Process] = []
|
|
|
|
websockets_server: multiprocessing.Process
|
|
|
|
controller_handler: multiprocessing.Process
|
|
|
|
webserver: multiprocessing.Process
|
2020-11-01 01:36:42 +00:00
|
|
|
|
2021-04-16 19:30:50 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
def startServer():
|
2021-04-07 20:13:19 +00:00
|
|
|
process_title = "startServer"
|
2021-04-04 21:34:46 +00:00
|
|
|
setproctitle(process_title)
|
2021-04-08 19:53:51 +00:00
|
|
|
# multiprocessing.current_process().name = process_title
|
2021-04-07 20:13:19 +00:00
|
|
|
|
|
|
|
global logger
|
|
|
|
global state
|
|
|
|
logger = LoggingManager("BAPSicleServer")
|
|
|
|
|
|
|
|
state = StateManager("BAPSicleServer", logger, default_state)
|
2021-04-11 18:02:19 +00:00
|
|
|
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
|
2021-04-07 20:13:19 +00:00
|
|
|
state.update("server_version", config.VERSION)
|
2021-04-11 19:40:25 +00:00
|
|
|
build_commit = "Dev"
|
2021-04-11 18:02:19 +00:00
|
|
|
if isBundelled():
|
2021-04-11 19:40:25 +00:00
|
|
|
build_commit = build.BUILD
|
|
|
|
state.update("server_build", build_commit)
|
2020-12-20 18:52:27 +00:00
|
|
|
|
2020-11-01 01:36:42 +00:00
|
|
|
if isMacOS():
|
|
|
|
multiprocessing.set_start_method("spawn", True)
|
2020-11-09 00:10:36 +00:00
|
|
|
for channel in range(state.state["num_channels"]):
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
channel_to_q.append(multiprocessing.Queue())
|
|
|
|
channel_from_q.append(multiprocessing.Queue())
|
2020-12-20 01:10:19 +00:00
|
|
|
ui_to_q.append(multiprocessing.Queue())
|
2021-03-22 00:33:14 +00:00
|
|
|
websocket_to_q.append(multiprocessing.Queue())
|
|
|
|
controller_to_q.append(multiprocessing.Queue())
|
2021-04-10 22:59:49 +00:00
|
|
|
|
|
|
|
# TODO Replace state with individual read-only StateManagers or something nicer?
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_p.append(
|
|
|
|
multiprocessing.Process(
|
|
|
|
target=player.Player,
|
2021-04-10 22:59:49 +00:00
|
|
|
args=(channel, channel_to_q[-1], channel_from_q[-1], state)
|
2021-04-08 19:53:51 +00:00
|
|
|
# daemon=True
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
channel_p[channel].start()
|
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
global api_from_q, api_to_q, api_handler, player_handler, websockets_server, controller_handler # , webserver
|
2021-03-21 13:05:33 +00:00
|
|
|
api_to_q = multiprocessing.Queue()
|
|
|
|
api_from_q = multiprocessing.Queue()
|
2021-04-08 19:53:51 +00:00
|
|
|
api_handler = multiprocessing.Process(
|
2021-04-10 22:59:49 +00:00
|
|
|
target=APIHandler, args=(api_to_q, api_from_q, state)
|
2021-04-08 19:53:51 +00:00
|
|
|
)
|
2021-03-21 13:05:33 +00:00
|
|
|
api_handler.start()
|
2020-12-20 01:10:19 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
player_handler = multiprocessing.Process(
|
|
|
|
target=PlayerHandler,
|
|
|
|
args=(channel_from_q, websocket_to_q, ui_to_q, controller_to_q),
|
|
|
|
)
|
2020-12-20 01:10:19 +00:00
|
|
|
player_handler.start()
|
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
# Note, state here will become a copy in the process.
|
|
|
|
# It will not update, and callbacks will not work :/
|
2021-04-08 19:53:51 +00:00
|
|
|
websockets_server = multiprocessing.Process(
|
|
|
|
target=WebsocketServer, args=(channel_to_q, websocket_to_q, state)
|
|
|
|
)
|
2020-11-15 17:40:18 +00:00
|
|
|
websockets_server.start()
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
controller_handler = multiprocessing.Process(
|
|
|
|
target=MattchBox, args=(channel_to_q, controller_to_q, state)
|
|
|
|
)
|
2021-02-14 23:58:32 +00:00
|
|
|
controller_handler.start()
|
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
webserver = multiprocessing.Process(
|
|
|
|
target=WebServer, args=(channel_to_q, ui_to_q, api_to_q, api_from_q, state)
|
|
|
|
)
|
|
|
|
webserver.start()
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# TODO Move this to player or installer.
|
|
|
|
if False:
|
|
|
|
if not isMacOS():
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# Temporary RIP.
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# Welcome Speech
|
2020-11-04 22:38:31 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
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
|
|
|
|
),
|
2021-04-08 19:53:51 +00:00
|
|
|
"dev/welcome.mp3",
|
2021-03-22 00:33:14 +00:00
|
|
|
)
|
|
|
|
text_to_speach.runAndWait()
|
2020-11-03 00:32:43 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
new_item: Dict[str, Any] = {
|
2021-03-22 00:33:14 +00:00
|
|
|
"channel_weight": 0,
|
|
|
|
"filename": "dev/welcome.mp3",
|
2021-04-08 19:53:51 +00:00
|
|
|
"title": "Welcome to BAPSicle",
|
|
|
|
"artist": "University Radio York",
|
2021-03-22 00:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
channel_to_q[0].put("ADD:" + json.dumps(new_item))
|
|
|
|
channel_to_q[0].put("LOAD:0")
|
|
|
|
channel_to_q[0].put("PLAY")
|
2020-11-01 02:35:14 +00:00
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
while True:
|
|
|
|
time.sleep(10000)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stopServer():
|
2021-04-11 18:02:19 +00:00
|
|
|
global channel_p, channel_from_q, channel_to_q, websockets_server, controller_handler, webserver
|
2021-04-05 21:13:53 +00:00
|
|
|
print("Stopping Controllers")
|
2021-04-11 18:02:19 +00:00
|
|
|
if controller_handler:
|
|
|
|
controller_handler.terminate()
|
|
|
|
controller_handler.join()
|
2021-04-05 21:13:53 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
print("Stopping Websockets")
|
|
|
|
websocket_to_q[0].put("WEBSOCKET:QUIT")
|
2021-04-11 18:02:19 +00:00
|
|
|
if websockets_server:
|
|
|
|
websockets_server.join()
|
|
|
|
del websockets_server
|
2021-04-05 21:13:53 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
print("Stopping server.py")
|
|
|
|
for q in channel_to_q:
|
2021-04-11 18:02:19 +00:00
|
|
|
q.put("ALL:QUIT")
|
2021-04-08 21:05:25 +00:00
|
|
|
for channel in channel_p:
|
2020-11-01 00:31:58 +00:00
|
|
|
try:
|
2021-04-08 21:05:25 +00:00
|
|
|
channel.join()
|
2021-04-04 21:34:46 +00:00
|
|
|
except Exception as e:
|
2021-04-08 19:53:51 +00:00
|
|
|
print("*** Ignoring exception:", e)
|
2020-11-01 00:31:58 +00:00
|
|
|
pass
|
2020-11-10 19:40:42 +00:00
|
|
|
finally:
|
2021-04-08 21:05:25 +00:00
|
|
|
del channel
|
2021-04-04 21:34:46 +00:00
|
|
|
del channel_from_q
|
|
|
|
del channel_to_q
|
2020-11-01 01:36:42 +00:00
|
|
|
print("Stopped all players.")
|
2021-04-04 21:34:46 +00:00
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
print("Stopping webserver")
|
2021-04-11 18:02:19 +00:00
|
|
|
|
|
|
|
if webserver:
|
|
|
|
webserver.terminate()
|
|
|
|
webserver.join()
|
2021-04-04 21:34:46 +00:00
|
|
|
|
2021-04-05 21:13:53 +00:00
|
|
|
print("Stopped webserver")
|
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.")
|