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
|
2020-10-24 13:44:26 +00:00
|
|
|
import player
|
2020-12-08 19:21:38 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory, request, jsonify
|
2020-10-23 20:10:32 +00:00
|
|
|
import json
|
2020-10-25 01:23:24 +00:00
|
|
|
import setproctitle
|
2020-11-01 00:31:58 +00:00
|
|
|
import logging
|
2020-11-01 01:36:42 +00:00
|
|
|
from helpers.os_environment import isMacOS
|
2020-11-03 21:24:45 +00:00
|
|
|
from helpers.device_manager import DeviceManager
|
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
|
|
|
|
|
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-03 23:25:17 +00:00
|
|
|
|
2020-10-25 01:23:24 +00:00
|
|
|
setproctitle.setproctitle("BAPSicle - Server")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
default_state = {
|
|
|
|
"server_version": 0,
|
|
|
|
"server_name": "URY BAPSicle",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 13500,
|
|
|
|
"num_channels": 3
|
|
|
|
}
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
class BAPSicleServer():
|
2020-10-30 23:14:29 +00:00
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
def __init__(self):
|
2020-10-30 23:14:29 +00:00
|
|
|
|
|
|
|
process_title = "Server"
|
|
|
|
setproctitle.setproctitle(process_title)
|
|
|
|
multiprocessing.current_process().name = process_title
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
startServer()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
stopServer()
|
|
|
|
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
logger = LoggingManager("BAPSicleServer")
|
|
|
|
|
|
|
|
state = StateManager("BAPSicleServer", logger, default_state)
|
|
|
|
state.update("server_version", config.VERSION)
|
|
|
|
|
2020-10-24 02:13:02 +00:00
|
|
|
app = Flask(__name__, static_url_path='')
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
log = logging.getLogger('werkzeug')
|
|
|
|
log.disabled = True
|
|
|
|
app.logger.disabled = True
|
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
channel_to_q = []
|
|
|
|
channel_from_q = []
|
|
|
|
channel_p = []
|
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
stopping = False
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-01 01:36:42 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# General Endpoints
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 12:47:48 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'ui_page': "404",
|
|
|
|
"ui_title": "404"
|
2020-10-24 12:47:48 +00:00
|
|
|
}
|
|
|
|
return render_template('404.html', data=data), 404
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2020-10-24 02:13:02 +00:00
|
|
|
def ui_index():
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'ui_page': "index",
|
2020-11-10 18:49:26 +00:00
|
|
|
"ui_title": "",
|
|
|
|
"server_version": config.VERSION,
|
|
|
|
"server_name": state.state["server_name"]
|
2020-10-24 02:13:02 +00:00
|
|
|
}
|
|
|
|
return render_template('index.html', data=data)
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/config")
|
|
|
|
def ui_config():
|
2020-10-23 21:58:53 +00:00
|
|
|
channel_states = []
|
2020-11-10 19:40:42 +00:00
|
|
|
for i in range(state.state["num_channels"]):
|
2020-10-30 00:33:00 +00:00
|
|
|
channel_states.append(status(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-11-03 21:24:45 +00:00
|
|
|
outputs = DeviceManager.getOutputs()
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'channels': channel_states,
|
|
|
|
'outputs': outputs,
|
|
|
|
'ui_page': "config",
|
|
|
|
"ui_title": "Config"
|
|
|
|
}
|
|
|
|
return render_template('config.html', data=data)
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
|
|
|
@app.route("/status")
|
|
|
|
def ui_status():
|
|
|
|
channel_states = []
|
2020-11-09 00:17:48 +00:00
|
|
|
for i in range(state.state["num_channels"]):
|
2020-10-30 00:33:00 +00:00
|
|
|
channel_states.append(status(i))
|
2020-10-23 21:58:53 +00:00
|
|
|
|
|
|
|
data = {
|
2020-10-24 14:04:33 +00:00
|
|
|
'channels': channel_states,
|
|
|
|
'ui_page': "status",
|
|
|
|
"ui_title": "Status"
|
2020-10-23 21:58:53 +00:00
|
|
|
}
|
2020-10-24 02:13:02 +00:00
|
|
|
return render_template('status.html', data=data)
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-09 00:17:48 +00:00
|
|
|
|
|
|
|
@app.route("/status-json")
|
|
|
|
def json_status():
|
|
|
|
channel_states = []
|
|
|
|
for i in range(state.state["num_channels"]):
|
|
|
|
channel_states.append(status(i))
|
|
|
|
return {
|
|
|
|
"server": state.state,
|
|
|
|
"channels": channel_states
|
|
|
|
}
|
|
|
|
|
2020-11-10 19:40:42 +00:00
|
|
|
|
|
|
|
@app.route("/server")
|
|
|
|
def server_config():
|
|
|
|
data = {
|
|
|
|
"ui_page": "server",
|
|
|
|
"ui_title": "Server Config",
|
|
|
|
"state": state.state
|
|
|
|
}
|
|
|
|
return render_template("server.html", data=data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/restart", methods=["POST"])
|
|
|
|
def restart_server():
|
|
|
|
state.update("server_name", request.form["name"])
|
|
|
|
state.update("host", request.form["host"])
|
|
|
|
state.update("port", int(request.form["port"]))
|
|
|
|
state.update("num_channels", int(request.form["channels"]))
|
|
|
|
stopServer(restart=True)
|
|
|
|
startServer()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# Channel Audio Options
|
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/play")
|
|
|
|
def play(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("PLAY")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/pause")
|
|
|
|
def pause(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("PAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/unpause")
|
|
|
|
def unPause(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("UNPAUSE")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/stop")
|
|
|
|
def stop(channel):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("STOP")
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/seek/<int:pos>")
|
|
|
|
def seek(channel, pos):
|
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("SEEK:" + str(pos))
|
|
|
|
|
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-10-23 21:58:53 +00:00
|
|
|
@app.route("/player/<int:channel>/output/<name>")
|
|
|
|
def output(channel, name):
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q[channel].put("OUTPUT:" + name)
|
|
|
|
return ui_status()
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/autoadvance/<int:state>")
|
|
|
|
def autoadvance(channel: int, state: int):
|
|
|
|
channel_to_q[channel].put("AUTOADVANCE:" + str(state))
|
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/repeat/<state>")
|
2020-11-04 00:09:42 +00:00
|
|
|
def repeat(channel: int, state):
|
2020-11-03 01:07:25 +00:00
|
|
|
channel_to_q[channel].put("REPEAT:" + state.upper())
|
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-03 01:07:25 +00:00
|
|
|
@app.route("/player/<int:channel>/playonload/<int:state>")
|
2020-11-04 00:09:42 +00:00
|
|
|
def playonload(channel: int, state: int):
|
2020-11-03 01:07:25 +00:00
|
|
|
channel_to_q[channel].put("PLAYONLOAD:" + str(state))
|
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# Channel Items
|
2020-11-02 23:06:45 +00:00
|
|
|
|
2020-11-05 23:53:27 +00:00
|
|
|
@app.route("/player/<int:channel>/load/<int:timeslotItemId>")
|
|
|
|
def load(channel:int, timeslotItemId: int):
|
|
|
|
channel_to_q[channel].put("LOAD:" + str(timeslotItemId))
|
2020-11-01 02:35:14 +00:00
|
|
|
return ui_status()
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-10-30 00:33:00 +00:00
|
|
|
@app.route("/player/<int:channel>/unload")
|
|
|
|
def unload(channel):
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-30 00:33:00 +00:00
|
|
|
channel_to_q[channel].put("UNLOAD")
|
|
|
|
|
|
|
|
return ui_status()
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-01 02:35:14 +00:00
|
|
|
@app.route("/player/<int:channel>/add", methods=["POST"])
|
|
|
|
def add_to_plan(channel: int):
|
|
|
|
new_item: Dict[str, any] = {
|
2020-11-05 23:53:27 +00:00
|
|
|
"timeslotItemId": int(request.form["timeslotItemId"]),
|
2020-11-01 02:35:14 +00:00
|
|
|
"filename": request.form["filename"],
|
|
|
|
"title": request.form["title"],
|
|
|
|
"artist": request.form["artist"],
|
|
|
|
}
|
|
|
|
|
|
|
|
channel_to_q[channel].put("ADD:" + json.dumps(new_item))
|
|
|
|
|
|
|
|
return new_item
|
|
|
|
|
2020-11-05 23:53:27 +00:00
|
|
|
@app.route("/player/<int:channel>/move/<int:timeslotItemId>/<int:position>")
|
|
|
|
def move_plan(channel: int, timeslotItemId: int, position: int):
|
|
|
|
channel_to_q[channel].put("MOVE:" + json.dumps({"timeslotItemId": timeslotItemId, "position": position}))
|
2020-11-05 18:58:18 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# TODO Return
|
2020-11-01 02:35:14 +00:00
|
|
|
return True
|
|
|
|
|
2020-11-05 23:53:27 +00:00
|
|
|
@app.route("/player/<int:channel>/remove/<int:timeslotItemId>")
|
|
|
|
def remove_plan(channel: int, timeslotItemId: int):
|
|
|
|
channel_to_q[channel].put("REMOVE:" + timeslotItemId)
|
2020-11-01 02:35:14 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# TODO Return
|
2020-11-01 02:35:14 +00:00
|
|
|
return True
|
2020-10-30 00:33:00 +00:00
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
|
2020-11-02 23:06:45 +00:00
|
|
|
@app.route("/player/<int:channel>/clear")
|
|
|
|
def clear_channel_plan(channel: int):
|
|
|
|
channel_to_q[channel].put("CLEAR")
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# TODO Return
|
2020-11-02 23:06:45 +00:00
|
|
|
return True
|
|
|
|
|
2020-11-09 00:10:36 +00:00
|
|
|
# General Channel Endpoints
|
|
|
|
|
2020-10-30 00:33:00 +00:00
|
|
|
|
|
|
|
@app.route("/player/<int:channel>/status")
|
|
|
|
def status(channel):
|
|
|
|
|
|
|
|
channel_to_q[channel].put("STATUS")
|
2020-10-24 14:04:33 +00:00
|
|
|
while True:
|
|
|
|
response = channel_from_q[channel].get()
|
2020-10-30 00:33:00 +00:00
|
|
|
if response.startswith("STATUS:"):
|
|
|
|
response = response[7:]
|
|
|
|
response = response[response.index(":")+1:]
|
|
|
|
try:
|
2020-12-08 19:21:38 +00:00
|
|
|
response = jsonify(json.loads(response))
|
2020-10-30 00:33:00 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return response
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
@app.route("/quit")
|
|
|
|
def quit():
|
|
|
|
stopServer()
|
|
|
|
return "Shutting down..."
|
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
|
|
|
@app.route("/player/all/stop")
|
|
|
|
def all_stop():
|
2020-10-24 14:04:33 +00:00
|
|
|
for channel in channel_to_q:
|
|
|
|
channel.put("STOP")
|
2020-11-02 23:06:45 +00:00
|
|
|
return ui_status()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/player/all/clear")
|
|
|
|
def clear_all_channels():
|
|
|
|
for channel in channel_to_q:
|
2020-11-05 18:58:18 +00:00
|
|
|
channel.put("CLEAR")
|
2020-11-02 23:06:45 +00:00
|
|
|
return ui_status()
|
2020-10-24 02:13:02 +00:00
|
|
|
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2020-10-24 02:13:02 +00:00
|
|
|
@app.route('/static/<path:path>')
|
|
|
|
def send_static(path):
|
|
|
|
return send_from_directory('ui-static', path)
|
2020-10-23 21:58:53 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
|
2020-11-09 00:42:09 +00:00
|
|
|
@app.route("/logs")
|
|
|
|
def list_logs():
|
|
|
|
data = {
|
|
|
|
"ui_page": "loglist",
|
|
|
|
"ui_title": "Logs",
|
|
|
|
"logs": ["BAPSicleServer"] + ["channel{}".format(x) for x in range(state.state["num_channels"])]
|
|
|
|
}
|
|
|
|
return render_template("loglist.html", data=data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/logs/<path:path>")
|
|
|
|
def send_logs(path):
|
|
|
|
l = open("logs/{}.log".format(path))
|
|
|
|
data = {
|
|
|
|
"logs": l.read().splitlines(),
|
|
|
|
'ui_page': "log",
|
|
|
|
"ui_title": "Logs - {}".format(path)
|
|
|
|
}
|
|
|
|
l.close()
|
|
|
|
return render_template('log.html', data=data)
|
|
|
|
|
|
|
|
|
2020-10-24 20:31:52 +00:00
|
|
|
def startServer():
|
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-31 03:04:14 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
channel_to_q.append(multiprocessing.Queue())
|
|
|
|
channel_from_q.append(multiprocessing.Queue())
|
|
|
|
channel_p.append(
|
|
|
|
multiprocessing.Process(
|
|
|
|
target=player.Player,
|
2020-10-24 20:31:52 +00:00
|
|
|
args=(channel, channel_to_q[-1], channel_from_q[-1]),
|
|
|
|
daemon=True
|
2020-10-24 14:04:33 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
channel_p[channel].start()
|
|
|
|
|
2020-11-04 22:38:31 +00:00
|
|
|
if not isMacOS():
|
|
|
|
|
|
|
|
# Temporary RIP.
|
|
|
|
|
|
|
|
# Welcome Speech
|
|
|
|
|
|
|
|
text_to_speach = pyttsx3.init()
|
|
|
|
text_to_speach.save_to_file(
|
2020-11-09 00:10:36 +00:00
|
|
|
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
|
2020-11-10 21:38:02 +00:00
|
|
|
By default, this server is accepting connections on port 13500
|
2020-11-09 00:10:36 +00:00
|
|
|
The version of the server service is {}
|
2020-11-04 22:38:31 +00:00
|
|
|
Please refer to the documentation included with this application for further assistance.""".format(
|
2020-11-09 00:10:36 +00:00
|
|
|
config.VERSION
|
|
|
|
),
|
|
|
|
"dev/welcome.mp3"
|
2020-11-04 22:38:31 +00:00
|
|
|
)
|
|
|
|
text_to_speach.runAndWait()
|
2020-11-03 00:32:43 +00:00
|
|
|
|
2020-11-01 02:35:14 +00:00
|
|
|
new_item: Dict[str, any] = {
|
2020-11-05 23:53:27 +00:00
|
|
|
"timeslotItemId": 0,
|
2020-11-03 00:32:43 +00:00
|
|
|
"filename": "dev/welcome.mp3",
|
|
|
|
"title": "Welcome to BAPSicle",
|
|
|
|
"artist": "University Radio York",
|
2020-11-01 02:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
channel_to_q[0].put("ADD:" + json.dumps(new_item))
|
2020-11-09 00:10:36 +00:00
|
|
|
# channel_to_q[0].put("LOAD:0")
|
|
|
|
# channel_to_q[0].put("PLAY")
|
2020-11-01 02:35:14 +00:00
|
|
|
|
2020-10-24 14:04:33 +00:00
|
|
|
# Don't use reloader, it causes Nested Processes!
|
2020-11-09 00:10:36 +00:00
|
|
|
app.run(host=state.state["host"], port=state.state["port"], debug=True, use_reloader=False)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
2020-11-10 19:40:42 +00:00
|
|
|
def stopServer(restart=False):
|
|
|
|
global channel_p
|
|
|
|
global channel_from_q
|
|
|
|
global channel_to_q
|
2020-10-24 20:31:52 +00:00
|
|
|
print("Stopping server.py")
|
|
|
|
for q in channel_to_q:
|
|
|
|
q.put("QUIT")
|
|
|
|
for player in channel_p:
|
2020-11-01 00:31:58 +00:00
|
|
|
try:
|
|
|
|
player.join()
|
|
|
|
except:
|
|
|
|
pass
|
2020-11-10 19:40:42 +00:00
|
|
|
finally:
|
|
|
|
channel_p = []
|
|
|
|
channel_from_q = []
|
|
|
|
channel_to_q = []
|
2020-11-01 01:36:42 +00:00
|
|
|
print("Stopped all players.")
|
2020-11-01 00:31:58 +00:00
|
|
|
global stopping
|
|
|
|
if stopping == False:
|
|
|
|
stopping = True
|
|
|
|
shutdown = request.environ.get('werkzeug.server.shutdown')
|
|
|
|
if shutdown is None:
|
|
|
|
print("Shutting down Server.")
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("Shutting down Flask.")
|
2020-11-10 19:40:42 +00:00
|
|
|
if not restart:
|
|
|
|
shutdown()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("BAPSicle is a service. Please run it like one.")
|