2020-11-01 02:35:14 +00:00
|
|
|
"""
|
|
|
|
BAPSicle Server
|
|
|
|
Next-gen audio playout server for University Radio York playout,
|
|
|
|
based on WebStudio interface.
|
|
|
|
|
|
|
|
Audio Player
|
|
|
|
|
|
|
|
Authors:
|
|
|
|
Matthew Stratford
|
|
|
|
Michael Grace
|
|
|
|
|
|
|
|
Date:
|
|
|
|
October, November 2020
|
|
|
|
"""
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# This is the player. It does everything regarding playing sound.
|
|
|
|
# Reliability is critical here, so we're catching literally every exception possible and handling it.
|
2020-10-30 19:31:18 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# It is key that whenever the clients tells us to do something
|
|
|
|
# that we respond with something, FAIL or OKAY. They don't like to be kept waiting/ignored.
|
2020-10-30 19:31:18 +00:00
|
|
|
|
2021-04-08 20:15:15 +00:00
|
|
|
# Stop the Pygame Hello message.
|
|
|
|
import os
|
|
|
|
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
|
2021-10-12 19:45:29 +00:00
|
|
|
from helpers.os_environment import isLinux
|
|
|
|
# It's the only one we could get to work.
|
|
|
|
if isLinux():
|
|
|
|
os.putenv('SDL_AUDIODRIVER', 'pulseaudio')
|
2021-04-08 20:15:15 +00:00
|
|
|
|
2021-09-11 16:48:57 +00:00
|
|
|
from queue import Empty
|
|
|
|
import multiprocessing
|
|
|
|
import setproctitle
|
|
|
|
import copy
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
2021-12-23 19:31:36 +00:00
|
|
|
from pygame import mixer, error
|
2021-09-11 16:48:57 +00:00
|
|
|
from mutagen.mp3 import MP3
|
|
|
|
from syncer import sync
|
|
|
|
from threading import Timer
|
2021-09-21 21:49:05 +00:00
|
|
|
from datetime import datetime
|
2021-09-11 16:48:57 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
from helpers.normalisation import get_normalised_filename_if_available, get_original_filename_from_normalised
|
2021-09-11 16:48:57 +00:00
|
|
|
from helpers.myradio_api import MyRadioAPI
|
|
|
|
from helpers.state_manager import StateManager
|
|
|
|
from helpers.logging_manager import LoggingManager
|
|
|
|
from baps_types.plan import PlanItem
|
|
|
|
from baps_types.marker import Marker
|
|
|
|
import package
|
2020-10-23 20:10:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
# TODO ENUM
|
2021-04-05 23:32:58 +00:00
|
|
|
VALID_MESSAGE_SOURCES = ["WEBSOCKET", "UI", "CONTROLLER", "TEST", "ALL"]
|
2021-04-22 22:00:31 +00:00
|
|
|
TRACKLISTING_DELAYED_S = 20
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-04-08 21:32:16 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
class Player:
|
2021-04-04 21:34:46 +00:00
|
|
|
out_q: multiprocessing.Queue
|
|
|
|
last_msg: str
|
|
|
|
last_msg_source: str
|
2021-02-14 00:29:47 +00:00
|
|
|
last_time_update = None
|
2021-04-04 21:34:46 +00:00
|
|
|
|
|
|
|
state: StateManager
|
|
|
|
logger: LoggingManager
|
|
|
|
api: MyRadioAPI
|
|
|
|
|
|
|
|
running: bool = False
|
2021-04-07 19:14:12 +00:00
|
|
|
|
|
|
|
stopped_manually: bool = False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
tracklist_start_timer: Optional[Timer] = None
|
|
|
|
tracklist_end_timer: Optional[Timer] = None
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# The default state that should be set if there is no previous state info.
|
2020-10-24 20:31:52 +00:00
|
|
|
__default_state = {
|
2020-10-30 00:32:34 +00:00
|
|
|
"initialised": False,
|
2020-11-01 02:35:14 +00:00
|
|
|
"loaded_item": None,
|
2020-10-24 20:31:52 +00:00
|
|
|
"channel": -1,
|
|
|
|
"playing": False,
|
2020-10-30 19:31:18 +00:00
|
|
|
"paused": False,
|
2020-10-29 22:25:17 +00:00
|
|
|
"loaded": False,
|
2020-10-24 20:31:52 +00:00
|
|
|
"pos": 0,
|
2020-10-30 19:31:18 +00:00
|
|
|
"pos_offset": 0,
|
|
|
|
"pos_true": 0,
|
2020-10-24 20:31:52 +00:00
|
|
|
"remaining": 0,
|
|
|
|
"length": 0,
|
2020-11-03 01:07:25 +00:00
|
|
|
"auto_advance": True,
|
2021-02-14 17:53:28 +00:00
|
|
|
"repeat": "none", # none, one or all
|
2020-11-03 01:07:25 +00:00
|
|
|
"play_on_load": False,
|
2020-11-01 02:35:14 +00:00
|
|
|
"output": None,
|
2021-04-08 19:53:51 +00:00
|
|
|
"show_plan": [],
|
2021-06-20 23:22:29 +00:00
|
|
|
"live": True,
|
2021-04-22 22:00:31 +00:00
|
|
|
"tracklist_mode": "off",
|
|
|
|
"tracklist_id": None,
|
2020-10-24 20:31:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# These tell the StateManager which variables we don't care about really accurate history for.
|
|
|
|
# This means that the internal running state of the player will have quickly updating info (multiple times a sec)
|
|
|
|
# But we will rate limit (a few secs) saving updates to these variables to the state JSON file.
|
2021-04-08 19:53:51 +00:00
|
|
|
__rate_limited_params = ["pos", "pos_offset", "pos_true", "remaining"]
|
2020-11-04 01:19:56 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Checks if the mixer is init'd. It will throw an exception if not.
|
2020-10-29 22:25:17 +00:00
|
|
|
@property
|
2020-10-24 20:31:52 +00:00
|
|
|
def isInit(self):
|
|
|
|
try:
|
2020-10-25 01:23:24 +00:00
|
|
|
mixer.music.get_busy()
|
2021-04-08 21:32:16 +00:00
|
|
|
except Exception:
|
2020-10-24 20:31:52 +00:00
|
|
|
return False
|
2020-10-29 22:25:17 +00:00
|
|
|
|
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
@property
|
2021-04-12 21:59:51 +00:00
|
|
|
def isPlaying(self) -> bool:
|
2020-10-29 22:25:17 +00:00
|
|
|
if self.isInit:
|
2021-12-23 22:48:38 +00:00
|
|
|
return not self.isPaused and mixer.music.get_busy()
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 19:31:18 +00:00
|
|
|
@property
|
2020-12-19 14:57:37 +00:00
|
|
|
def isPaused(self) -> bool:
|
2021-04-18 19:27:54 +00:00
|
|
|
return self.state.get()["paused"]
|
2020-10-30 19:31:18 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
@property
|
|
|
|
def isLoaded(self):
|
2021-12-23 20:26:13 +00:00
|
|
|
return self.state.get()["loaded"]
|
2021-04-12 21:59:51 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Checks if a file has been loaded
|
|
|
|
# This should be run with a long test before client requests for status etc.
|
|
|
|
# Short tests are used in other logic in the player, without fussing over full playback tests.
|
2021-12-23 20:26:13 +00:00
|
|
|
def _checkIsLoaded(self, short_test: bool = False):
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-12-23 20:26:13 +00:00
|
|
|
loaded = True
|
|
|
|
|
|
|
|
if not self.state.get()["loaded_item"] or not self.isInit:
|
|
|
|
loaded = False
|
|
|
|
elif not self.isPlaying:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Because this function can be called very often, only some (less frequent) checks will initiate a full trial of loading success, for efficiency.
|
2021-12-23 20:26:13 +00:00
|
|
|
if not short_test:
|
|
|
|
# We're not playing now, so we can quickly test run
|
2021-12-23 22:48:38 +00:00
|
|
|
# If that works, we're truely loaded.
|
2021-12-23 20:26:13 +00:00
|
|
|
try:
|
|
|
|
mixer.music.set_volume(0)
|
|
|
|
mixer.music.play(0)
|
|
|
|
except Exception:
|
|
|
|
try:
|
|
|
|
mixer.music.set_volume(1)
|
|
|
|
except Exception:
|
|
|
|
self.logger.log.exception(
|
|
|
|
"Failed to reset volume after attempting loaded test."
|
|
|
|
)
|
|
|
|
pass
|
|
|
|
loaded = False
|
|
|
|
finally:
|
|
|
|
mixer.music.stop()
|
2021-04-12 21:59:51 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
mixer.music.set_volume(1)
|
2021-04-22 22:00:31 +00:00
|
|
|
|
2021-12-23 20:26:13 +00:00
|
|
|
self.state.update("loaded", loaded)
|
|
|
|
return loaded
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Is the player at a cue marker point?
|
2021-04-12 21:59:51 +00:00
|
|
|
@property
|
|
|
|
def isCued(self):
|
2021-12-23 20:26:13 +00:00
|
|
|
if not self.isLoaded:
|
2021-04-12 21:59:51 +00:00
|
|
|
return False
|
2021-09-11 15:49:08 +00:00
|
|
|
return (
|
|
|
|
self.state.get()["pos_true"] == self.state.get()["loaded_item"].cue
|
|
|
|
and not self.isPlaying
|
|
|
|
)
|
2021-04-12 21:59:51 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Returns the state of the player as a nice friendly JSON dump.
|
2020-10-30 00:32:34 +00:00
|
|
|
@property
|
|
|
|
def status(self):
|
2021-12-23 22:48:38 +00:00
|
|
|
# Get a copy of the server state.
|
2021-12-23 19:31:36 +00:00
|
|
|
state = self.state.state
|
2020-11-01 02:35:14 +00:00
|
|
|
|
|
|
|
# Not the biggest fan of this, but maybe I'll get a better solution for this later
|
2021-12-23 22:48:38 +00:00
|
|
|
# Convert objects to a nice JSON friendly dicts.
|
2021-04-08 19:53:51 +00:00
|
|
|
state["loaded_item"] = (
|
|
|
|
state["loaded_item"].__dict__ if state["loaded_item"] else None
|
|
|
|
)
|
2020-11-03 00:32:43 +00:00
|
|
|
state["show_plan"] = [repr.__dict__ for repr in state["show_plan"]]
|
2020-11-01 02:35:14 +00:00
|
|
|
|
|
|
|
res = json.dumps(state)
|
2020-10-30 00:32:34 +00:00
|
|
|
return res
|
2020-10-29 21:23:37 +00:00
|
|
|
|
2020-11-15 19:34:13 +00:00
|
|
|
# Audio Playout Related Methods
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2020-11-02 23:06:45 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Loads a plan item into the player, ready for playing.
|
|
|
|
# This includes some retry logic to try and double-down on ensuring it plays successfully.
|
2021-02-14 20:10:32 +00:00
|
|
|
def load(self, weight: int):
|
2020-10-29 22:25:17 +00:00
|
|
|
if not self.isPlaying:
|
2021-09-22 22:57:50 +00:00
|
|
|
# If we have something loaded already, unload it first.
|
2020-10-30 19:31:18 +00:00
|
|
|
self.unload()
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
loaded_state = self.state.get()
|
|
|
|
|
|
|
|
# Sometimes (at least on windows), the pygame player will lose output to the sound output after a while.
|
|
|
|
# It's odd, but essentially, to stop / recover from this, we de-init the pygame mixer and init it again.
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Resetting output (in case of sound output gone silent somehow) to "
|
|
|
|
+ str(loaded_state["output"])
|
|
|
|
)
|
2021-12-23 22:48:38 +00:00
|
|
|
self.set_output(loaded_state["output"])
|
2021-09-01 23:35:03 +00:00
|
|
|
|
|
|
|
showplan = loaded_state["show_plan"]
|
2020-12-08 19:41:11 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
loaded_item: Optional[PlanItem] = None
|
2020-12-08 19:41:11 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# Go find the show plan item of the weight we've been asked to load.
|
2020-12-08 19:41:11 +00:00
|
|
|
for i in range(len(showplan)):
|
2021-02-14 20:10:32 +00:00
|
|
|
if showplan[i].weight == weight:
|
2020-12-08 19:41:11 +00:00
|
|
|
loaded_item = showplan[i]
|
2020-11-01 02:35:14 +00:00
|
|
|
break
|
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# If we didn't find it, exit.
|
2021-04-08 21:05:25 +00:00
|
|
|
if loaded_item is None:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Failed to find weight: {}".format(weight))
|
2020-11-01 02:35:14 +00:00
|
|
|
return False
|
|
|
|
|
2021-09-25 18:28:13 +00:00
|
|
|
# This item exists, so we're comitting to load this item.
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# The file_manager helper may have pre-downloaded the file already, or we've played it before.
|
2021-04-07 21:52:33 +00:00
|
|
|
reload = False
|
2021-04-08 21:05:25 +00:00
|
|
|
if loaded_item.filename == "" or loaded_item.filename is None:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Filename is not specified, loading from API.")
|
2021-04-07 21:52:33 +00:00
|
|
|
reload = True
|
|
|
|
elif not os.path.exists(loaded_item.filename):
|
2021-04-08 19:53:51 +00:00
|
|
|
self.logger.log.warn(
|
|
|
|
"Filename given doesn't exist. Re-loading from API."
|
|
|
|
)
|
2021-04-07 21:52:33 +00:00
|
|
|
reload = True
|
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# Ask the API for the file if we need it.
|
2021-04-07 21:52:33 +00:00
|
|
|
if reload:
|
2021-09-22 22:57:50 +00:00
|
|
|
file = sync(self.api.get_filename(item=loaded_item))
|
|
|
|
loaded_item.filename = str(file) if file else None
|
2020-12-08 19:41:11 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# If the API still couldn't get the file, RIP.
|
2020-12-19 14:57:37 +00:00
|
|
|
if not loaded_item.filename:
|
|
|
|
return False
|
|
|
|
|
2021-08-16 22:43:09 +00:00
|
|
|
# Swap with a normalised version if it's ready, else returns original.
|
2021-09-11 15:49:08 +00:00
|
|
|
loaded_item.filename = get_normalised_filename_if_available(
|
|
|
|
loaded_item.filename
|
|
|
|
)
|
2021-08-16 22:43:09 +00:00
|
|
|
|
2021-09-25 18:28:13 +00:00
|
|
|
# Given we've just messed around with filenames etc, update the item again.
|
2020-12-08 19:41:11 +00:00
|
|
|
self.state.update("loaded_item", loaded_item)
|
|
|
|
for i in range(len(showplan)):
|
2021-02-14 20:10:32 +00:00
|
|
|
if showplan[i].weight == weight:
|
2020-12-08 19:41:11 +00:00
|
|
|
self.state.update("show_plan", index=i, value=loaded_item)
|
|
|
|
break
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-05-02 14:41:14 +00:00
|
|
|
load_attempt = 0
|
2021-07-16 22:56:58 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# Let's have 5 attempts at loading the item audio
|
2021-05-02 14:41:14 +00:00
|
|
|
while load_attempt < 5:
|
|
|
|
load_attempt += 1
|
2021-09-22 22:57:50 +00:00
|
|
|
|
|
|
|
original_file = None
|
|
|
|
if load_attempt == 3:
|
|
|
|
# Ok, we tried twice already to load the file.
|
|
|
|
# Let's see if we can recover from this.
|
|
|
|
# Try swapping the normalised version out for the original.
|
|
|
|
original_file = get_original_filename_from_normalised(
|
|
|
|
loaded_item.filename
|
|
|
|
)
|
|
|
|
self.logger.log.warning("3rd attempt. Trying the non-normalised file: {}".format(original_file))
|
|
|
|
|
|
|
|
if load_attempt == 4:
|
|
|
|
# well, we've got so far that the normalised and original files didn't load.
|
|
|
|
# Take a last ditch effort to download the original file again.
|
|
|
|
file = sync(self.api.get_filename(item=loaded_item, redownload=True))
|
|
|
|
if file:
|
|
|
|
original_file = str(file)
|
|
|
|
self.logger.log.warning("4rd attempt. Trying to redownload the file, got: {}".format(original_file))
|
|
|
|
|
|
|
|
if original_file:
|
|
|
|
loaded_item.filename = original_file
|
|
|
|
|
2021-05-02 14:41:14 +00:00
|
|
|
try:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info(
|
2021-09-22 22:57:50 +00:00
|
|
|
"Attempt {} Loading file: {}".format(load_attempt, loaded_item.filename))
|
2021-05-02 14:41:14 +00:00
|
|
|
mixer.music.load(loaded_item.filename)
|
|
|
|
except Exception:
|
|
|
|
# We couldn't load that file.
|
|
|
|
self.logger.log.exception(
|
|
|
|
"Couldn't load file: " + str(loaded_item.filename)
|
|
|
|
)
|
2021-09-11 15:49:08 +00:00
|
|
|
continue # Try loading again.
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-05-02 14:41:14 +00:00
|
|
|
try:
|
2021-07-16 22:56:58 +00:00
|
|
|
if loaded_item.filename.endswith(".mp3"):
|
2021-05-02 14:41:14 +00:00
|
|
|
song = MP3(loaded_item.filename)
|
|
|
|
self.state.update("length", song.info.length)
|
|
|
|
else:
|
2021-07-16 22:56:58 +00:00
|
|
|
# WARNING! Pygame / SDL can't seek .wav files :/
|
2021-05-02 14:41:14 +00:00
|
|
|
self.state.update(
|
2021-09-11 15:49:08 +00:00
|
|
|
"length",
|
2021-09-11 16:18:35 +00:00
|
|
|
mixer.Sound(
|
|
|
|
loaded_item.filename).get_length() / 1000,
|
2021-05-02 14:41:14 +00:00
|
|
|
)
|
|
|
|
except Exception:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.exception(
|
|
|
|
"Failed to update the length of item.")
|
2021-09-11 15:49:08 +00:00
|
|
|
continue # Try loading again.
|
2021-05-02 14:41:14 +00:00
|
|
|
|
|
|
|
# Everything worked, we made it!
|
2021-09-22 22:57:50 +00:00
|
|
|
# Write the loaded item again once more, to confirm the filename if we've reattempted.
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
2021-12-23 22:48:38 +00:00
|
|
|
|
|
|
|
# Now just double check that pygame could actually play it (silently)
|
2021-12-23 20:26:13 +00:00
|
|
|
self._checkIsLoaded()
|
2021-12-23 22:48:38 +00:00
|
|
|
if not self.isLoaded:
|
|
|
|
self.logger.log.error(
|
|
|
|
"Pygame loaded file without error, but never actually loaded."
|
|
|
|
)
|
|
|
|
continue # Try loading again.
|
2021-09-22 22:57:50 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# If the track has a cue point, let's jump to that, ready.
|
2021-05-02 14:41:14 +00:00
|
|
|
if loaded_item.cue > 0:
|
|
|
|
self.seek(loaded_item.cue)
|
2020-10-29 22:25:17 +00:00
|
|
|
else:
|
2021-05-02 14:41:14 +00:00
|
|
|
self.seek(0)
|
2021-02-14 13:23:51 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
if loaded_state["play_on_load"]:
|
2021-05-02 14:41:14 +00:00
|
|
|
self.unpause()
|
2021-04-12 21:59:51 +00:00
|
|
|
|
2021-05-02 14:41:14 +00:00
|
|
|
return True
|
2021-02-14 13:23:51 +00:00
|
|
|
|
2021-09-22 22:57:50 +00:00
|
|
|
# Even though we failed, make sure state is up to date with latest failure.
|
|
|
|
# We're comitting to load this item.
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
2021-12-23 20:26:13 +00:00
|
|
|
self._checkIsLoaded()
|
2021-05-02 14:41:14 +00:00
|
|
|
|
|
|
|
return False
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Remove the currently loaded item from the player.
|
|
|
|
# Not much reason to do this, but if it makes you happy.
|
2020-10-29 22:25:17 +00:00
|
|
|
def unload(self):
|
|
|
|
if not self.isPlaying:
|
|
|
|
try:
|
|
|
|
mixer.music.unload()
|
2020-10-30 19:31:18 +00:00
|
|
|
self.state.update("paused", False)
|
2020-11-01 02:35:14 +00:00
|
|
|
self.state.update("loaded_item", None)
|
2021-04-08 21:32:16 +00:00
|
|
|
except Exception:
|
2020-11-03 22:48:11 +00:00
|
|
|
self.logger.log.exception("Failed to unload channel.")
|
2020-10-29 22:25:17 +00:00
|
|
|
return False
|
2021-04-22 22:00:31 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
#self._potentially_end_tracklist()
|
2021-04-22 22:00:31 +00:00
|
|
|
# If we unloaded successfully, reset the tracklist_id, ready for the next item.
|
|
|
|
if not self.isLoaded:
|
|
|
|
self.state.update("tracklist_id", None)
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# If we successfully unloaded, this will return true, for success!
|
2020-10-29 22:25:17 +00:00
|
|
|
return not self.isLoaded
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
|
|
|
|
# Starts playing the loaded item, from a given position (secs)
|
|
|
|
def play(self, pos: float = 0):
|
|
|
|
self.logger.log.info("Playing from pos: " + str(pos))
|
|
|
|
if not self.isLoaded:
|
|
|
|
self.logger.log.warning("Player is not loaded.")
|
|
|
|
return False
|
2020-11-03 22:48:11 +00:00
|
|
|
try:
|
2021-12-23 22:48:38 +00:00
|
|
|
mixer.music.play(0, pos)
|
|
|
|
self.state.update("pos_offset", pos)
|
|
|
|
except Exception:
|
|
|
|
self.logger.log.exception("Failed to play at pos: " + str(pos))
|
|
|
|
return False
|
|
|
|
self.state.update("paused", False)
|
|
|
|
self._potentially_tracklist()
|
|
|
|
self.stopped_manually = False
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Pauses the player
|
|
|
|
def pause(self):
|
|
|
|
# Because the player's position is stored by a event from pygame while playing only,
|
|
|
|
# the current playback position state will remain, in case we unpause later.
|
|
|
|
try:
|
|
|
|
mixer.music.stop()
|
|
|
|
except Exception:
|
|
|
|
self.logger.log.exception("Failed to pause.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.stopped_manually = True
|
|
|
|
self.state.update("paused", True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Plays the player, from the playback position it was already at.
|
|
|
|
def unpause(self):
|
|
|
|
if not self.isPlaying:
|
|
|
|
state = self.state.get()
|
|
|
|
position: float = state["pos_true"]
|
|
|
|
if not self.play(position):
|
|
|
|
self.logger.log.exception(
|
|
|
|
"Failed to unpause from pos: " + str(position)
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
2020-11-03 22:48:11 +00:00
|
|
|
self.state.update("paused", False)
|
2021-12-23 22:48:38 +00:00
|
|
|
|
|
|
|
# Increment Played count
|
|
|
|
loaded_item = state["loaded_item"]
|
|
|
|
if loaded_item:
|
|
|
|
loaded_item.play_count_increment()
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
|
|
|
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Stop the player.
|
|
|
|
def stop(self, user_initiated: bool = False):
|
|
|
|
try:
|
|
|
|
mixer.music.stop()
|
2021-04-08 21:32:16 +00:00
|
|
|
except Exception:
|
2021-12-23 22:48:38 +00:00
|
|
|
self.logger.log.exception("Failed to stop playing.")
|
|
|
|
return False
|
|
|
|
self.state.update("paused", False)
|
|
|
|
|
|
|
|
# When it wasn't _ended() calling this, end the tracklist.
|
|
|
|
# _ended() already calls this, but user stops won't have.
|
|
|
|
if user_initiated:
|
|
|
|
self._potentially_end_tracklist()
|
|
|
|
self.stopped_manually = True
|
|
|
|
|
|
|
|
if not self.state.get()["loaded_item"]:
|
|
|
|
self.logger.log.warning("Tried to stop without a loaded item.")
|
|
|
|
return True
|
|
|
|
|
|
|
|
# This lets users toggle (using the stop button) between cue point and 0.
|
|
|
|
|
|
|
|
if user_initiated and not self.isCued:
|
|
|
|
# if there's a cue point ant we're not at it, go there.
|
|
|
|
self.seek(self.state.get()["loaded_item"].cue)
|
|
|
|
else:
|
|
|
|
# Otherwise, let's go to 0.
|
|
|
|
self.seek(0)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
# Move the audio position (secs) of the player
|
|
|
|
def seek(self, pos: float) -> bool:
|
|
|
|
self.logger.log.info("Seeking to pos:" + str(pos))
|
|
|
|
if self.isPlaying:
|
|
|
|
# If we're playing, just start playing directly from that position
|
|
|
|
try:
|
|
|
|
self.play(pos)
|
|
|
|
except Exception:
|
|
|
|
self.logger.log.exception("Failed to seek to pos: " + str(pos))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# If we're not actually playing at the moment, set the player to be paused at the new position
|
|
|
|
self.logger.log.debug(
|
|
|
|
"Not playing during seek, setting pos state for next play."
|
|
|
|
)
|
|
|
|
self.stopped_manually = True # Don't trigger _ended() on seeking.
|
|
|
|
if pos > 0:
|
|
|
|
self.state.update("paused", True)
|
|
|
|
self._updateState(pos=pos)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Set the output device name and initialise the pygame audio mixer.
|
|
|
|
def set_output(self, name: Optional[str] = None):
|
2021-09-22 22:57:50 +00:00
|
|
|
wasPlaying = self.isPlaying
|
|
|
|
|
|
|
|
state = self.state.get()
|
|
|
|
oldPos = state["pos_true"]
|
2021-04-07 19:14:39 +00:00
|
|
|
|
|
|
|
name = None if (not name or name.lower() == "none") else name
|
2020-11-05 18:59:28 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Stop the mixer if it's already init'd.
|
2020-10-24 20:31:52 +00:00
|
|
|
self.quit()
|
2020-10-30 00:32:34 +00:00
|
|
|
self.state.update("output", name)
|
2020-10-24 20:31:52 +00:00
|
|
|
try:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Setup the mixer.
|
|
|
|
# Sample rate of 44100Hz (44.1KHz) (matching the MP3's and typical CD/online source material)
|
|
|
|
# 16 bits per sample
|
|
|
|
# 2 channels (stereo)
|
|
|
|
# sample buffer of 1024 samples
|
2020-10-24 20:31:52 +00:00
|
|
|
if name:
|
2020-11-01 03:27:26 +00:00
|
|
|
mixer.init(44100, -16, 2, 1024, devicename=name)
|
2020-10-24 20:31:52 +00:00
|
|
|
else:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Use the default system output
|
2020-11-01 03:27:26 +00:00
|
|
|
mixer.init(44100, -16, 2, 1024)
|
2021-04-08 21:32:16 +00:00
|
|
|
except Exception:
|
2021-04-08 19:53:51 +00:00
|
|
|
self.logger.log.exception(
|
|
|
|
"Failed to init mixer with device name: " + str(name)
|
|
|
|
)
|
2020-10-30 00:32:34 +00:00
|
|
|
return False
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# If we had something loaded before, load it back in and play it.
|
2021-09-22 22:57:50 +00:00
|
|
|
loadedItem = state["loaded_item"]
|
2021-04-08 19:53:51 +00:00
|
|
|
if loadedItem:
|
2021-05-01 22:08:38 +00:00
|
|
|
self.logger.log.info("Reloading after output change.")
|
2021-02-14 20:10:32 +00:00
|
|
|
self.load(loadedItem.weight)
|
2020-11-01 03:19:21 +00:00
|
|
|
if wasPlaying:
|
2021-05-01 22:08:38 +00:00
|
|
|
self.logger.log.info("Resuming playback after output change.")
|
2021-04-24 21:03:54 +00:00
|
|
|
self.play(oldPos)
|
2020-11-01 03:19:21 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
return True
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# De-initialises the pygame mixer.
|
|
|
|
def quit(self):
|
|
|
|
try:
|
|
|
|
mixer.quit()
|
|
|
|
self.state.update("paused", False)
|
|
|
|
self.logger.log.info("Quit mixer.")
|
|
|
|
except Exception:
|
|
|
|
self.logger.log.exception("Failed to quit mixer.")
|
|
|
|
|
|
|
|
|
|
|
|
# Sets whether auto advance is on or off
|
|
|
|
# Auto advance is where the next item in the list is selected after the current item is finished playing.
|
|
|
|
def set_auto_advance(self, message: bool) -> bool:
|
|
|
|
self.state.update("auto_advance", message)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# As you'd expect, all rotates around all of the items in the channel plan, and loops to the first from the last.
|
|
|
|
# One plays the same item over and over again
|
|
|
|
def set_repeat(self, message: str) -> bool:
|
|
|
|
if message in ["all", "one", "none"]:
|
|
|
|
self.state.update("repeat", message)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Set whether the player should play the item as soon as it's been selected.
|
|
|
|
def set_play_on_load(self, message: bool) -> bool:
|
|
|
|
self.state.update("play_on_load", message)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# Show Plan Related Methods
|
|
|
|
|
|
|
|
def _check_ghosts(self, item: PlanItem):
|
|
|
|
# Webstudio returns intermediate "I" objects when dragging in from the media sidebar.
|
|
|
|
if isinstance(item.timeslotitemid, str) and item.timeslotitemid.startswith("I"):
|
|
|
|
# Kinda a bodge for the moment, each "Ghost" (item which is not saved in the database showplan yet)
|
|
|
|
# needs to have a unique temporary item.
|
|
|
|
# To do this, we'll start with the channel number the item was originally added to
|
|
|
|
# (to stop items somehow simultaneously added to different channels from having the same id)
|
|
|
|
# And chuck in the unix epoch in ns for good measure.
|
|
|
|
item.timeslotitemid = "GHOST-{}-{}".format(
|
|
|
|
self.state.get()["channel"], time.time_ns()
|
|
|
|
)
|
|
|
|
return item
|
|
|
|
|
|
|
|
# Pull in from the API the show plan items for this player channel.
|
|
|
|
def get_plan(self, show_plan_id: int):
|
|
|
|
# Call the API
|
|
|
|
# sync turns the asyncronous API into syncronous.
|
|
|
|
plan = sync(self.api.get_showplan(show_plan_id))
|
|
|
|
|
|
|
|
# Empty the channel plan so we can put the updated items in.
|
|
|
|
self.clear_channel_plan()
|
|
|
|
channel = self.state.get()["channel"]
|
|
|
|
self.logger.log.debug(plan)
|
|
|
|
# If there isn't a show plan for the required show, return failure without filling in the plan.
|
|
|
|
if not isinstance(plan, dict):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Add the items, if this channel has any.
|
|
|
|
if str(channel) in plan.keys():
|
|
|
|
plan_items = plan[str(channel)]
|
|
|
|
try:
|
|
|
|
self.add_to_plan(plan_items)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.log.error(
|
|
|
|
"Failed to add items to show plan: {}".format(e)
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Add a list of new show plan items to the channel.
|
|
|
|
# These will be in dict format, we'll validate them and turn them into proper plan objects.
|
|
|
|
# TODO Allow just moving an item inside the channel instead of removing and adding.
|
|
|
|
def add_to_plan(self, new_items: List[Dict[str, Any]]) -> bool:
|
|
|
|
plan_copy: List[PlanItem] = copy.copy(self.state.get()["show_plan"])
|
|
|
|
|
|
|
|
for new_item in new_items:
|
|
|
|
new_item_obj = PlanItem(new_item)
|
|
|
|
new_item_obj = self._check_ghosts(new_item_obj)
|
|
|
|
|
|
|
|
# Shift any plan items after the new position down one to make space.
|
|
|
|
for item in plan_copy:
|
|
|
|
if item.weight >= new_item_obj.weight:
|
|
|
|
item.weight += 1
|
|
|
|
|
|
|
|
plan_copy += [new_item_obj] # Add the new item.
|
|
|
|
|
|
|
|
loaded_item = self.state.get()["loaded_item"]
|
|
|
|
if loaded_item:
|
|
|
|
|
|
|
|
# Right. So this may be confusing.
|
|
|
|
# So... If the user has just moved the loaded item in the channel (by removing above and readding)
|
|
|
|
# Then we want to re-associate the loaded_item object reference with the new one.
|
|
|
|
# The loaded item object before this change is now an orphan, which was
|
|
|
|
# kept around while the loaded item was potentially moved to another
|
|
|
|
# channel.
|
|
|
|
if loaded_item.timeslotitemid == new_item_obj.timeslotitemid:
|
|
|
|
self.state.update("loaded_item", new_item_obj)
|
|
|
|
|
|
|
|
# NOPE NOPE NOPE
|
|
|
|
# THIS IS AN EXAMPLE OF WHAT NOT TO DO!
|
|
|
|
# ONCE AGAIN, THE LOADED ITEM IS THE SAME OBJECT INSTANCE AS THE ONE IN
|
|
|
|
# THE SHOW PLAN (AS LONG AS IT HASN'T BEEN RE/MOVED)
|
|
|
|
|
|
|
|
# loaded_item.weight = new_item_obj.weight
|
|
|
|
|
|
|
|
# Bump the loaded_item's weight if we just added a new item above it.
|
|
|
|
# elif loaded_item.weight >= new_item_obj.weight:
|
|
|
|
# loaded_item.weight += 1
|
|
|
|
|
|
|
|
# Else, new weight stays the same.
|
|
|
|
# else:
|
|
|
|
# return True
|
|
|
|
|
|
|
|
# self.state.update("loaded_item", loaded_item)
|
|
|
|
|
|
|
|
# Just in case somehow we've ended up with items with the same weights (or gaps)
|
|
|
|
# We'll correct them.
|
|
|
|
# This function also orders and saves the updated plan copy we've given it.
|
|
|
|
self._fix_and_update_weights(plan_copy)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Removes an item from the show plan with the given weight (index)
|
|
|
|
def remove_from_plan(self, weight: int) -> bool:
|
|
|
|
plan_copy: List[PlanItem] = copy.copy(self.state.get()["show_plan"])
|
|
|
|
found: Optional[PlanItem] = None
|
|
|
|
|
|
|
|
# Give some helpful debug
|
|
|
|
before = []
|
|
|
|
for item in plan_copy:
|
|
|
|
before += (item.weight, item.name)
|
|
|
|
|
|
|
|
self.logger.log.debug(
|
|
|
|
"Weights before removing weight {}:\n{}".format(weight, before)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Look for the item with the correct weight
|
|
|
|
for i in plan_copy:
|
|
|
|
if i.weight == weight:
|
|
|
|
found = i
|
|
|
|
plan_copy.remove(i)
|
|
|
|
|
|
|
|
if found:
|
|
|
|
self._fix_and_update_weights(plan_copy)
|
|
|
|
|
|
|
|
# If we removed the loaded item from this channel, update it's weight
|
|
|
|
# So we know how/not to autoadvance.
|
|
|
|
loaded_item = self.state.get()["loaded_item"]
|
|
|
|
if loaded_item == found:
|
|
|
|
# Loaded_item is actually the same PlanItem instance as in the show_plan.
|
|
|
|
# So if it's still in the show plan, we'll have corrected it's weight already.
|
|
|
|
# If it was removed above, fix_weights won't have done anything
|
|
|
|
# So we'll want to update the weight.
|
|
|
|
|
|
|
|
# We're removing the loaded item from the channel.
|
|
|
|
# if loaded_item.weight == weight:
|
|
|
|
loaded_item.weight = -1
|
|
|
|
|
|
|
|
# If loaded_item wasn't the same instance, we'd want to do the below.
|
|
|
|
|
|
|
|
# We removed an item above it. Shift it up.
|
|
|
|
# elif loaded_item.weight > weight:
|
|
|
|
# loaded_item.weight -= 1
|
|
|
|
# Else, new weight stays the same.
|
|
|
|
# else:
|
|
|
|
# return True
|
|
|
|
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Empties the channel's plan.
|
|
|
|
def clear_channel_plan(self) -> bool:
|
|
|
|
self.state.update("show_plan", [])
|
|
|
|
return True
|
|
|
|
|
|
|
|
# PlanItems can have markers. These are essentially bookmarked positions in the audio.
|
|
|
|
# Timeslotitemid can be a ghost (un-submitted item), so may be "IXXX", hence str.
|
2021-04-17 17:27:36 +00:00
|
|
|
def set_marker(self, timeslotitemid: str, marker_str: str):
|
2021-04-10 21:56:53 +00:00
|
|
|
set_loaded = False
|
|
|
|
success = True
|
|
|
|
try:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Take a string representation of the marker (from clients)
|
2021-04-10 21:56:53 +00:00
|
|
|
marker = Marker(marker_str)
|
|
|
|
except Exception as e:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Failed to create Marker instance with {} {}: {}".format(
|
|
|
|
timeslotitemid, marker_str, e
|
|
|
|
)
|
|
|
|
)
|
2021-04-10 21:56:53 +00:00
|
|
|
return False
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Allow setting a marker for the currently loaded item.
|
2021-04-17 17:27:36 +00:00
|
|
|
if timeslotitemid == "-1":
|
2021-04-10 21:56:53 +00:00
|
|
|
set_loaded = True
|
|
|
|
if not self.isLoaded:
|
|
|
|
return False
|
2021-04-18 19:27:54 +00:00
|
|
|
timeslotitemid = self.state.get()["loaded_item"].timeslotitemid
|
2021-09-11 15:49:08 +00:00
|
|
|
elif (
|
|
|
|
self.isLoaded
|
|
|
|
and self.state.get()["loaded_item"].timeslotitemid == timeslotitemid
|
|
|
|
):
|
2021-04-26 00:01:58 +00:00
|
|
|
set_loaded = True
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Loop over the show plan items. When you find the timeslotitemid the marker is for, update it.
|
|
|
|
# This is instead of weight, since the client asking doesn't know the weight of the item (or which channel it is)
|
|
|
|
# So all channels will look and update if necessary.
|
2021-04-18 19:27:54 +00:00
|
|
|
plan_copy: List[PlanItem] = copy.copy(self.state.get()["show_plan"])
|
|
|
|
for i in range(len(self.state.get()["show_plan"])):
|
2021-04-12 21:59:51 +00:00
|
|
|
|
|
|
|
item = plan_copy[i]
|
2021-04-10 21:56:53 +00:00
|
|
|
|
2021-04-17 17:27:36 +00:00
|
|
|
if str(item.timeslotitemid) == str(timeslotitemid):
|
2021-04-10 21:56:53 +00:00
|
|
|
try:
|
2021-04-12 21:59:51 +00:00
|
|
|
new_item = item.set_marker(marker)
|
|
|
|
self.state.update("show_plan", new_item, index=i)
|
2021-04-10 21:56:53 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.log.error(
|
2021-09-11 15:49:08 +00:00
|
|
|
"Failed to set marker on item {}: {} with marker \n{}".format(
|
|
|
|
timeslotitemid, e, marker
|
|
|
|
)
|
|
|
|
)
|
2021-04-10 21:56:53 +00:00
|
|
|
success = False
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# If the item to update was the loaded item, update it.
|
2021-04-10 21:56:53 +00:00
|
|
|
if set_loaded:
|
|
|
|
try:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.state.update(
|
2021-09-11 16:18:35 +00:00
|
|
|
"loaded_item", self.state.get(
|
|
|
|
)["loaded_item"].set_marker(marker)
|
2021-09-11 15:49:08 +00:00
|
|
|
)
|
2021-04-10 21:56:53 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.log.error(
|
2021-09-11 15:49:08 +00:00
|
|
|
"Failed to set marker on loaded_item {}: {} with marker \n{}".format(
|
|
|
|
timeslotitemid, e, marker
|
|
|
|
)
|
|
|
|
)
|
2021-04-10 21:56:53 +00:00
|
|
|
success = False
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# This marks an item as played, or not.
|
|
|
|
# A weight of -1 will affect all items in the channel
|
2021-09-03 20:49:16 +00:00
|
|
|
def set_played(self, weight: int, played: bool):
|
2021-05-02 01:18:00 +00:00
|
|
|
plan: List[PlanItem] = self.state.get()["show_plan"]
|
|
|
|
if weight == -1:
|
|
|
|
for item in plan:
|
2021-09-03 20:49:16 +00:00
|
|
|
item.play_count_increment() if played else item.play_count_reset()
|
2021-05-02 01:18:00 +00:00
|
|
|
self.state.update("show_plan", plan)
|
|
|
|
elif len(plan) > weight:
|
2021-09-11 15:49:08 +00:00
|
|
|
plan[weight].play_count_increment() if played else plan[
|
|
|
|
weight
|
|
|
|
].play_count_reset()
|
2021-05-02 01:18:00 +00:00
|
|
|
self.state.update("show_plan", plan[weight], weight)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-06-20 23:22:29 +00:00
|
|
|
# Tells the player that the fader is live on-air, so it can tell tracklisting from PFL
|
|
|
|
def set_live(self, live: bool):
|
2021-06-22 17:20:18 +00:00
|
|
|
|
2021-06-20 23:22:29 +00:00
|
|
|
self.state.update("live", live)
|
|
|
|
|
|
|
|
# If we're going to live (potentially from not live/PFL), potentially tracklist if it's playing.
|
2021-09-11 15:49:08 +00:00
|
|
|
if live:
|
2021-06-20 23:22:29 +00:00
|
|
|
self._potentially_tracklist()
|
2021-12-23 22:48:38 +00:00
|
|
|
# If the fader is now not live, don't bother stopping the tracklist, incase it's faded up again during the same playback.
|
2021-06-20 23:22:29 +00:00
|
|
|
return True
|
|
|
|
|
2021-04-10 21:56:53 +00:00
|
|
|
# Helper functions
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# This essentially allows the tracklist start API call to happen in a separate thread, to avoid hanging playout/loading.
|
2021-04-22 22:00:31 +00:00
|
|
|
def _potentially_tracklist(self):
|
2021-04-24 20:33:36 +00:00
|
|
|
mode = self.state.get()["tracklist_mode"]
|
2021-04-22 22:00:31 +00:00
|
|
|
|
|
|
|
time: int = -1
|
2021-12-23 22:48:38 +00:00
|
|
|
if mode == "on":
|
|
|
|
time = 0 # Let's do it pretty quickly.
|
|
|
|
if mode == "fader-live":
|
|
|
|
time = 4 # Give presenter a bit of a grace period in case they accidentally fade up the wrong one.
|
2021-04-22 22:00:31 +00:00
|
|
|
elif mode == "delayed":
|
|
|
|
# Let's do it in a bit, once we're sure it's been playing. (Useful if we've got no idea if it's live or cueing.)
|
|
|
|
time = TRACKLISTING_DELAYED_S
|
|
|
|
|
|
|
|
if time >= 0 and not self.tracklist_start_timer:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Setting timer for tracklisting in {} secs due to Mode: {}".format(
|
|
|
|
time, mode
|
|
|
|
)
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
self.tracklist_start_timer = Timer(time, self._tracklist_start)
|
|
|
|
self.tracklist_start_timer.start()
|
|
|
|
elif self.tracklist_start_timer:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Failed to potentially tracklist, timer already busy."
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
|
|
|
|
# This essentially allows the tracklist end API call to happen in a separate thread, to avoid hanging playout/loading.
|
|
|
|
def _potentially_end_tracklist(self):
|
|
|
|
|
2021-05-01 22:09:59 +00:00
|
|
|
if self.tracklist_start_timer:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"A tracklist start timer was running, cancelling.")
|
2021-05-01 22:09:59 +00:00
|
|
|
self.tracklist_start_timer.cancel()
|
|
|
|
self.tracklist_start_timer = None
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Decrement Played count on track we didn't play enough of to tracklist.
|
2021-05-02 01:18:00 +00:00
|
|
|
state = self.state.get()
|
|
|
|
loaded_item = state["loaded_item"]
|
|
|
|
if loaded_item and loaded_item.type == "central":
|
|
|
|
loaded_item.play_count_decrement()
|
|
|
|
self.state.update("loaded_item", loaded_item)
|
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
# Make a copy of the tracklist_id, it will get reset as we load the next item.
|
|
|
|
tracklist_id = self.state.get()["tracklist_id"]
|
2021-04-23 20:12:31 +00:00
|
|
|
if not tracklist_id:
|
|
|
|
self.logger.log.info("No tracklist to end.")
|
|
|
|
return
|
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
if tracklist_id:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Attempting to end tracklist_id '{}'".format(tracklist_id)
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
if self.tracklist_end_timer:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Failed to potentially end tracklist, timer already busy."
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
return
|
2021-09-01 22:23:50 +00:00
|
|
|
self.state.update("tracklist_id", None)
|
2021-04-22 22:00:31 +00:00
|
|
|
# This threads it, so it won't hang track loading if it fails.
|
2021-09-11 16:18:35 +00:00
|
|
|
self.tracklist_end_timer = Timer(
|
|
|
|
1, self._tracklist_end, [tracklist_id])
|
2021-04-22 22:00:31 +00:00
|
|
|
self.tracklist_end_timer.start()
|
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.warning(
|
|
|
|
"Failed to potentially end tracklist, no tracklist started."
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# The actual function that will register with the API an item being played.
|
2021-04-22 22:00:31 +00:00
|
|
|
def _tracklist_start(self):
|
2021-06-20 23:22:29 +00:00
|
|
|
state = self.state.get()
|
|
|
|
loaded_item = state["loaded_item"]
|
2021-04-22 22:00:31 +00:00
|
|
|
if not loaded_item:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Tried to call _tracklist_start() with no loaded item!"
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
|
2021-09-01 22:23:50 +00:00
|
|
|
elif not self.isPlaying:
|
2021-06-20 23:22:29 +00:00
|
|
|
self.logger.log.info("Not tracklisting since not playing.")
|
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
else:
|
|
|
|
|
2021-09-01 22:23:50 +00:00
|
|
|
tracklist_id = state["tracklist_id"]
|
2021-09-11 15:49:08 +00:00
|
|
|
if not tracklist_id:
|
|
|
|
if state["tracklist_mode"] == "fader-live" and not state["live"]:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Not tracklisting since fader is not live.")
|
2021-09-01 22:23:50 +00:00
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Tracklisting item: '{}'".format(loaded_item.name)
|
|
|
|
)
|
2021-09-01 22:23:50 +00:00
|
|
|
tracklist_id = self.api.post_tracklist_start(loaded_item)
|
|
|
|
if not tracklist_id:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.warning(
|
|
|
|
"Failed to tracklist '{}'".format(loaded_item.name)
|
|
|
|
)
|
2021-09-01 22:23:50 +00:00
|
|
|
else:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Tracklist id: '{}'".format(tracklist_id))
|
2021-09-01 22:23:50 +00:00
|
|
|
self.state.update("tracklist_id", tracklist_id)
|
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Not tracklisting item '{}', already got tracklistid: '{}'".format(
|
|
|
|
loaded_item.name, tracklist_id
|
|
|
|
)
|
|
|
|
)
|
2021-09-01 22:23:50 +00:00
|
|
|
|
|
|
|
# No matter what we end up doing, we need to kill this timer so future ones can run.
|
2021-04-22 22:00:31 +00:00
|
|
|
self.tracklist_start_timer = None
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# The actual function that will register with the API an item being finished playing.
|
2021-04-22 22:00:31 +00:00
|
|
|
def _tracklist_end(self, tracklist_id):
|
|
|
|
|
|
|
|
if tracklist_id:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Attempting to end tracklist_id '{}'".format(tracklist_id)
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
self.api.post_tracklist_end(tracklist_id)
|
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.error(
|
|
|
|
"Tracklist_id to _tracklist_end() missing. Failed to end tracklist."
|
|
|
|
)
|
2021-04-22 22:00:31 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# No matter what we end up doing, we need to kill this timer so future ones can run.
|
2021-04-22 22:00:31 +00:00
|
|
|
self.tracklist_end_timer = None
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# When an item has ended (the pygame mixer has told us that it has stopped playing)
|
2021-04-10 21:56:53 +00:00
|
|
|
def _ended(self):
|
2021-04-22 22:00:31 +00:00
|
|
|
self._potentially_end_tracklist()
|
|
|
|
|
2021-05-01 23:45:03 +00:00
|
|
|
state = self.state.get()
|
|
|
|
|
|
|
|
loaded_item = state["loaded_item"]
|
2021-02-14 00:29:47 +00:00
|
|
|
|
2021-04-12 21:59:51 +00:00
|
|
|
if not loaded_item:
|
|
|
|
return
|
|
|
|
|
2021-02-14 00:29:47 +00:00
|
|
|
# Track has ended
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Playback ended of {}, weight {}:".format(
|
|
|
|
loaded_item.name, loaded_item.weight
|
|
|
|
)
|
|
|
|
)
|
2021-09-22 22:57:50 +00:00
|
|
|
# Just make sure that if we stop and do nothing, we end up at 0.
|
2021-09-22 23:03:36 +00:00
|
|
|
self.state.update("pos", 0)
|
2021-02-14 00:29:47 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Repeat 1? Spin that record again!
|
2021-04-07 19:14:12 +00:00
|
|
|
# TODO ENUM
|
2021-05-01 23:45:03 +00:00
|
|
|
if state["repeat"] == "one":
|
2021-02-14 00:29:47 +00:00
|
|
|
self.play()
|
2021-04-07 19:14:12 +00:00
|
|
|
return
|
2021-02-14 00:29:47 +00:00
|
|
|
|
|
|
|
# Auto Advance
|
2021-05-01 23:45:03 +00:00
|
|
|
if state["auto_advance"]:
|
|
|
|
|
|
|
|
# Check for loaded item in show plan.
|
|
|
|
# If it's been removed, weight will be -1.
|
|
|
|
# Just stop in this case.
|
|
|
|
if loaded_item.weight < 0:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.debug(
|
|
|
|
"Loaded item is no longer in channel (weight {}), not auto advancing.".format(
|
|
|
|
loaded_item.weight
|
|
|
|
)
|
|
|
|
)
|
2021-05-01 23:45:03 +00:00
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.debug(
|
|
|
|
"Found current loaded item in this channel show plan. Auto Advancing."
|
|
|
|
)
|
2021-05-01 23:45:03 +00:00
|
|
|
|
|
|
|
# If there's another item after this one, load that.
|
2021-09-11 15:49:08 +00:00
|
|
|
if len(state["show_plan"]) > loaded_item.weight + 1:
|
|
|
|
self.load(loaded_item.weight + 1)
|
2021-05-01 23:45:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Repeat All (Jump to top again)
|
|
|
|
# TODO ENUM
|
|
|
|
elif state["repeat"] == "all":
|
2021-09-11 15:49:08 +00:00
|
|
|
self.load(0) # Jump to the top.
|
2021-05-01 23:45:03 +00:00
|
|
|
return
|
2021-02-14 13:23:51 +00:00
|
|
|
|
2021-04-07 19:14:12 +00:00
|
|
|
# No automations, just stop playing.
|
|
|
|
self.stop()
|
2021-05-01 23:45:03 +00:00
|
|
|
self._retAll("STOPPED") # Tell clients that we've stopped playing.
|
2021-02-14 00:29:47 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# This runs every main loop, to update anything that changes often / automatically.
|
2020-12-19 14:57:37 +00:00
|
|
|
def _updateState(self, pos: Optional[float] = None):
|
2021-12-23 22:48:38 +00:00
|
|
|
# Is pygame still happy?
|
|
|
|
isInit = self.isInit
|
|
|
|
self.state.update("initialised", isInit)
|
|
|
|
if isInit:
|
2021-04-24 18:32:43 +00:00
|
|
|
if pos is not None:
|
2021-04-24 20:31:35 +00:00
|
|
|
# Seeking sets the position like this when not playing.
|
2021-04-24 18:32:43 +00:00
|
|
|
self.state.update("pos", pos) # Reset back to 0 if stopped.
|
|
|
|
self.state.update("pos_offset", 0)
|
2020-11-04 01:19:56 +00:00
|
|
|
elif self.isPlaying:
|
2021-04-24 18:32:43 +00:00
|
|
|
# This is the bit that makes the time actually progress during playback.
|
2020-10-30 19:31:18 +00:00
|
|
|
# Get one last update in, incase we're about to pause/stop it.
|
2021-04-08 19:53:51 +00:00
|
|
|
self.state.update("pos", max(0, mixer.music.get_pos() / 1000))
|
2021-04-07 19:14:12 +00:00
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
# If the state is changing from playing to not playing, and the user didn't stop it, the item must have ended.
|
2021-04-08 19:53:51 +00:00
|
|
|
if (
|
2021-04-18 19:27:54 +00:00
|
|
|
self.state.get()["playing"]
|
2021-04-08 19:53:51 +00:00
|
|
|
and not self.isPlaying
|
|
|
|
and not self.stopped_manually
|
|
|
|
):
|
2021-04-10 21:56:53 +00:00
|
|
|
self._ended()
|
2021-04-07 19:14:12 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
self.state.update("playing", self.isPlaying)
|
2020-10-30 19:31:18 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
self.state.update(
|
|
|
|
"pos_true",
|
|
|
|
min(
|
2021-04-18 19:27:54 +00:00
|
|
|
self.state.get()["length"],
|
|
|
|
self.state.get()["pos"] + self.state.get()["pos_offset"],
|
2021-04-08 19:53:51 +00:00
|
|
|
),
|
|
|
|
)
|
2020-10-30 19:31:18 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
self.state.update(
|
|
|
|
"remaining",
|
2021-09-11 16:18:35 +00:00
|
|
|
max(0, (self.state.get()["length"] -
|
|
|
|
self.state.get()["pos_true"])),
|
2021-04-08 19:53:51 +00:00
|
|
|
)
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Sends the current playback position to clients, so they can update their UI frequently.
|
|
|
|
# Run on every main loop, but rate limited.
|
2021-02-14 00:29:47 +00:00
|
|
|
def _ping_times(self):
|
2021-03-21 13:06:09 +00:00
|
|
|
|
|
|
|
UPDATES_FREQ_SECS = 0.2
|
2021-04-08 19:53:51 +00:00
|
|
|
if (
|
2021-04-08 21:05:25 +00:00
|
|
|
self.last_time_update is None
|
2021-04-08 19:53:51 +00:00
|
|
|
or self.last_time_update + UPDATES_FREQ_SECS < time.time()
|
|
|
|
):
|
2021-02-14 00:29:47 +00:00
|
|
|
self.last_time_update = time.time()
|
2021-04-18 19:27:54 +00:00
|
|
|
self._retAll("POS:" + str(self.state.get()["pos_true"]))
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Broadcast a message to all other modules of the BAPSicle server.
|
2021-03-22 00:33:14 +00:00
|
|
|
def _retAll(self, msg):
|
2021-05-01 23:45:03 +00:00
|
|
|
if self.out_q:
|
2022-03-11 23:55:53 +00:00
|
|
|
self.out_q.put("{}:ALL:{}".format(self.state.get()["channel"], msg))
|
2020-11-03 01:07:25 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Send a response back to an incoming command, with the original content and a success or failure.
|
2021-04-08 19:53:51 +00:00
|
|
|
def _retMsg(
|
|
|
|
self, msg: Any, okay_str: bool = False, custom_prefix: Optional[str] = None
|
|
|
|
):
|
2021-09-24 20:11:39 +00:00
|
|
|
response = "{}:".format(self.state.get()["channel"])
|
2021-03-22 00:33:14 +00:00
|
|
|
# Make sure to add the message source back, so that it can be sent to the correct destination in the main server.
|
|
|
|
if custom_prefix:
|
2021-09-24 20:11:39 +00:00
|
|
|
response += custom_prefix
|
2021-03-22 00:33:14 +00:00
|
|
|
else:
|
2021-09-24 20:11:39 +00:00
|
|
|
response += "{}:{}:".format(self.last_msg_source, self.last_msg)
|
2021-04-08 21:05:25 +00:00
|
|
|
if msg is True:
|
2020-10-30 00:32:34 +00:00
|
|
|
response += "OKAY"
|
|
|
|
elif isinstance(msg, str):
|
|
|
|
if okay_str:
|
|
|
|
response += "OKAY:" + msg
|
|
|
|
else:
|
|
|
|
response += "FAIL:" + msg
|
2020-10-24 20:31:52 +00:00
|
|
|
else:
|
2020-10-30 00:32:34 +00:00
|
|
|
response += "FAIL"
|
2021-05-01 22:08:38 +00:00
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
if self.out_q:
|
2021-09-11 15:49:08 +00:00
|
|
|
if "STATUS:" not in response:
|
2021-05-01 22:08:38 +00:00
|
|
|
# Don't fill logs with status pushes, it's a mess.
|
|
|
|
self.logger.log.debug(("Sending: {}".format(response)))
|
2020-10-30 00:32:34 +00:00
|
|
|
self.out_q.put(response)
|
2021-05-01 22:08:38 +00:00
|
|
|
else:
|
2021-09-11 15:49:08 +00:00
|
|
|
self.logger.log.exception(
|
|
|
|
"Message return Queue is missing!!!! Can't send message."
|
|
|
|
)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Send the current status to all other modules/clients. Used for updating all client UIs when one of them causes a change etc.
|
2021-02-14 13:57:07 +00:00
|
|
|
def _send_status(self):
|
2021-09-11 16:18:35 +00:00
|
|
|
self._retMsg(str(self.status), okay_str=True,
|
|
|
|
custom_prefix="ALL:STATUS:")
|
2021-02-14 13:57:07 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Takes an input show plan, checks and corrects duplicate / gaps in weights, and stores it.
|
|
|
|
def _fix_and_update_weights(self, plan: List[PlanItem]):
|
2021-04-24 20:33:36 +00:00
|
|
|
def _sort_weight(e: PlanItem):
|
|
|
|
return e.weight
|
|
|
|
|
2021-05-05 22:06:29 +00:00
|
|
|
before = []
|
|
|
|
for item in plan:
|
|
|
|
before += (item.weight, item.name)
|
|
|
|
|
|
|
|
self.logger.log.debug("Weights before fixing:\n{}".format(before))
|
|
|
|
|
2021-04-24 20:33:36 +00:00
|
|
|
plan.sort(key=_sort_weight) # Sort into weighted order.
|
|
|
|
|
2021-05-05 22:06:29 +00:00
|
|
|
sorted = []
|
|
|
|
for item in plan:
|
|
|
|
sorted += (item.weight, item.name)
|
|
|
|
|
|
|
|
self.logger.log.debug("Weights after sorting:\n{}".format(sorted))
|
|
|
|
|
2021-04-24 20:33:36 +00:00
|
|
|
for i in range(len(plan)):
|
|
|
|
plan[i].weight = i # Recorrect the weights on the channel.
|
|
|
|
|
2021-05-05 22:06:29 +00:00
|
|
|
fixed = []
|
|
|
|
for item in plan:
|
|
|
|
fixed += (item.weight, item.name)
|
|
|
|
|
|
|
|
self.logger.log.debug("Weights after sorting:\n{}".format(fixed))
|
2021-05-19 23:31:44 +00:00
|
|
|
self.state.update("show_plan", plan)
|
2021-04-24 20:33:36 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Player start up. This is called from the BAPSicle server.py.
|
2021-04-08 19:53:51 +00:00
|
|
|
def __init__(
|
2021-09-11 15:49:08 +00:00
|
|
|
self,
|
|
|
|
channel: int,
|
|
|
|
in_q: multiprocessing.Queue,
|
|
|
|
out_q: multiprocessing.Queue,
|
|
|
|
server_state: StateManager,
|
2021-04-08 19:53:51 +00:00
|
|
|
):
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2022-03-12 15:01:51 +00:00
|
|
|
process_title = "BAPSicle - Player: Channel " + str(channel)
|
2020-10-30 23:14:29 +00:00
|
|
|
setproctitle.setproctitle(process_title)
|
|
|
|
multiprocessing.current_process().name = process_title
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-10-30 22:06:03 +00:00
|
|
|
self.running = True
|
|
|
|
self.out_q = out_q
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger = LoggingManager(
|
2021-09-21 21:49:05 +00:00
|
|
|
"Player" + str(channel), debug=package.BETA)
|
2020-10-30 22:06:03 +00:00
|
|
|
|
2021-04-22 22:00:31 +00:00
|
|
|
self.api = MyRadioAPI(self.logger, server_state)
|
2021-02-14 00:29:47 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
self.state = StateManager(
|
|
|
|
"Player" + str(channel),
|
|
|
|
self.logger,
|
|
|
|
self.__default_state,
|
|
|
|
self.__rate_limited_params,
|
|
|
|
)
|
2021-02-14 13:57:07 +00:00
|
|
|
|
2021-09-21 21:49:05 +00:00
|
|
|
self.state.update("start_time", datetime.now().timestamp())
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# When the state changes, use _send_status() to tell all clients.
|
2021-02-14 13:57:07 +00:00
|
|
|
self.state.add_callback(self._send_status)
|
|
|
|
|
2020-10-30 23:59:58 +00:00
|
|
|
self.state.update("channel", channel)
|
2021-12-23 22:48:38 +00:00
|
|
|
# tracklist mode is shared between all players, so grab that from the server config.
|
2021-09-11 16:18:35 +00:00
|
|
|
self.state.update("tracklist_mode", server_state.get()[
|
|
|
|
"tracklist_mode"])
|
2021-09-11 15:49:08 +00:00
|
|
|
self.state.update(
|
|
|
|
"live", True
|
2021-12-23 22:48:38 +00:00
|
|
|
) # Channel Fader is live until controller says it isn't.
|
2020-10-30 23:59:58 +00:00
|
|
|
|
2021-04-24 20:33:36 +00:00
|
|
|
# Just in case there's any weights somehow messed up, let's fix them.
|
|
|
|
plan_copy: List[PlanItem] = copy.copy(self.state.get()["show_plan"])
|
2021-05-19 23:31:44 +00:00
|
|
|
self._fix_and_update_weights(plan_copy)
|
2021-04-24 20:33:36 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
loaded_state = self.state.state
|
2020-10-24 20:31:52 +00:00
|
|
|
|
|
|
|
if loaded_state["output"]:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.info("Setting output to: " +
|
|
|
|
str(loaded_state["output"]))
|
2021-12-23 22:48:38 +00:00
|
|
|
self.set_output(loaded_state["output"])
|
2020-10-24 20:31:52 +00:00
|
|
|
else:
|
2020-10-30 22:06:03 +00:00
|
|
|
self.logger.log.info("Using default output device.")
|
2021-12-23 22:48:38 +00:00
|
|
|
self.set_output()
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2020-12-19 14:57:37 +00:00
|
|
|
loaded_item = loaded_state["loaded_item"]
|
|
|
|
if loaded_item:
|
2021-05-01 22:09:13 +00:00
|
|
|
# No need to load on init, the output switch does this, as it would for regular output switching.
|
2021-09-11 15:49:08 +00:00
|
|
|
# self.load(loaded_item.weight)
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-12 21:59:51 +00:00
|
|
|
# Load may jump to the cue point, as it would do on a regular load.
|
|
|
|
# If we were at a different state before, we have to override it now.
|
2020-10-30 19:31:18 +00:00
|
|
|
if loaded_state["pos_true"] != 0:
|
2021-04-08 19:53:51 +00:00
|
|
|
self.logger.log.info(
|
|
|
|
"Seeking to pos_true: " + str(loaded_state["pos_true"])
|
|
|
|
)
|
2021-12-23 19:31:36 +00:00
|
|
|
try:
|
|
|
|
self.seek(loaded_state["pos_true"])
|
|
|
|
except error:
|
|
|
|
self.logger.log.error("Failed to seek on player start. Continuing anyway.")
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-08 21:05:25 +00:00
|
|
|
if loaded_state["playing"] is True:
|
2021-05-01 22:08:38 +00:00
|
|
|
self.logger.log.info("Resuming playback on init.")
|
2021-09-11 16:18:35 +00:00
|
|
|
# Use un-pause as we don't want to jump to a new position.
|
2021-12-23 19:31:36 +00:00
|
|
|
try:
|
|
|
|
self.unpause()
|
|
|
|
except error:
|
|
|
|
self.logger.log.error("Failed to unpause on player start. Continuing anyway.")
|
2020-10-29 21:23:37 +00:00
|
|
|
else:
|
2021-05-01 22:08:38 +00:00
|
|
|
self.logger.log.info("No file was previously loaded to resume.")
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# The main loop. This keeps running till something tells it to stop.
|
2021-04-04 21:34:46 +00:00
|
|
|
try:
|
|
|
|
while self.running:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Update the state for playback position changes etc
|
2021-04-04 21:34:46 +00:00
|
|
|
self._updateState()
|
2021-12-23 22:48:38 +00:00
|
|
|
# If we need to, tell clients of the position updates
|
2021-04-04 21:34:46 +00:00
|
|
|
self._ping_times()
|
2020-10-29 21:23:37 +00:00
|
|
|
try:
|
2021-12-23 22:48:38 +00:00
|
|
|
# Try and get a new command message from clients
|
2021-03-22 00:33:14 +00:00
|
|
|
message = in_q.get_nowait()
|
|
|
|
source = message.split(":")[0]
|
|
|
|
if source not in VALID_MESSAGE_SOURCES:
|
|
|
|
self.last_msg_source = ""
|
|
|
|
self.last_msg = ""
|
2021-04-08 19:53:51 +00:00
|
|
|
self.logger.log.warn(
|
2021-09-11 16:18:35 +00:00
|
|
|
"Message from unknown sender source: {}".format(
|
|
|
|
source)
|
2021-04-08 19:53:51 +00:00
|
|
|
)
|
2021-03-22 00:33:14 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
self.last_msg_source = source
|
|
|
|
self.last_msg = message.split(":", 1)[1]
|
|
|
|
|
2021-04-17 17:27:36 +00:00
|
|
|
self.logger.log.debug(
|
2021-04-08 19:53:51 +00:00
|
|
|
"Recieved message from source {}: {}".format(
|
|
|
|
self.last_msg_source, self.last_msg
|
|
|
|
)
|
|
|
|
)
|
2020-10-29 21:23:37 +00:00
|
|
|
except Empty:
|
|
|
|
# The incomming message queue was empty,
|
|
|
|
# skip message processing
|
2021-12-23 20:26:13 +00:00
|
|
|
|
|
|
|
# If we're getting no messages, sleep.
|
|
|
|
# But if we do have messages, once we've done with one, we'll check for the next one more quickly.
|
|
|
|
time.sleep(0.05)
|
2020-10-29 21:23:37 +00:00
|
|
|
else:
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2020-10-29 21:23:37 +00:00
|
|
|
# We got a message.
|
2020-10-29 22:25:17 +00:00
|
|
|
|
2022-03-11 23:55:53 +00:00
|
|
|
# Check if we're successfully loaded
|
2021-12-23 20:26:13 +00:00
|
|
|
# This is here so that we can check often, but not every single loop
|
|
|
|
# Only when user gives input.
|
|
|
|
self._checkIsLoaded()
|
|
|
|
|
2020-10-30 00:32:34 +00:00
|
|
|
# Output re-inits the mixer, so we can do this any time.
|
2021-04-08 19:53:51 +00:00
|
|
|
if self.last_msg.startswith("OUTPUT"):
|
2020-10-30 00:32:34 +00:00
|
|
|
split = self.last_msg.split(":")
|
2021-12-23 22:48:38 +00:00
|
|
|
self._retMsg(self.set_output(split[1]))
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# Only process these commands if we're properly initialised.
|
2020-10-30 00:32:34 +00:00
|
|
|
elif self.isInit:
|
2021-04-08 19:53:51 +00:00
|
|
|
message_types: Dict[
|
|
|
|
str, Callable[..., Any]
|
|
|
|
] = { # TODO Check Types
|
2021-02-14 00:29:47 +00:00
|
|
|
"STATUS": lambda: self._retMsg(self.status, True),
|
2020-11-03 01:07:25 +00:00
|
|
|
# Audio Playout
|
2021-04-12 21:59:51 +00:00
|
|
|
# Unpause, so we don't jump to 0, we play from the current pos.
|
|
|
|
"PLAY": lambda: self._retMsg(self.unpause()),
|
2020-11-15 19:34:13 +00:00
|
|
|
"PAUSE": lambda: self._retMsg(self.pause()),
|
2021-09-11 15:49:08 +00:00
|
|
|
"PLAYPAUSE": lambda: self._retMsg(
|
|
|
|
self.unpause() if not self.isPlaying else self.pause()
|
|
|
|
), # For the hardware controller.
|
2020-11-15 19:34:13 +00:00
|
|
|
"UNPAUSE": lambda: self._retMsg(self.unpause()),
|
2021-09-11 15:49:08 +00:00
|
|
|
"STOP": lambda: self._retMsg(
|
|
|
|
self.stop(user_initiated=True)
|
|
|
|
),
|
2021-04-08 19:53:51 +00:00
|
|
|
"SEEK": lambda: self._retMsg(
|
|
|
|
self.seek(float(self.last_msg.split(":")[1]))
|
|
|
|
),
|
|
|
|
"AUTOADVANCE": lambda: self._retMsg(
|
|
|
|
self.set_auto_advance(
|
|
|
|
(self.last_msg.split(":")[1] == "True")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"REPEAT": lambda: self._retMsg(
|
|
|
|
self.set_repeat(self.last_msg.split(":")[1])
|
|
|
|
),
|
|
|
|
"PLAYONLOAD": lambda: self._retMsg(
|
|
|
|
self.set_play_on_load(
|
|
|
|
(self.last_msg.split(":")[1] == "True")
|
|
|
|
)
|
|
|
|
),
|
2020-11-03 01:07:25 +00:00
|
|
|
# Show Plan Items
|
2021-09-03 20:49:16 +00:00
|
|
|
"GETPLAN": lambda: self._retMsg(
|
2021-04-08 19:53:51 +00:00
|
|
|
self.get_plan(int(self.last_msg.split(":")[1]))
|
|
|
|
),
|
|
|
|
"LOAD": lambda: self._retMsg(
|
|
|
|
self.load(int(self.last_msg.split(":")[1]))
|
|
|
|
),
|
2020-11-15 19:34:13 +00:00
|
|
|
"LOADED?": lambda: self._retMsg(self.isLoaded),
|
|
|
|
"UNLOAD": lambda: self._retMsg(self.unload()),
|
2021-04-08 19:53:51 +00:00
|
|
|
"ADD": lambda: self._retMsg(
|
2022-03-12 14:55:33 +00:00
|
|
|
self.add_to_plan([
|
2021-09-11 16:18:35 +00:00
|
|
|
json.loads(
|
|
|
|
":".join(self.last_msg.split(":")[1:]))
|
2022-03-12 14:55:33 +00:00
|
|
|
])
|
2021-04-08 19:53:51 +00:00
|
|
|
),
|
|
|
|
"REMOVE": lambda: self._retMsg(
|
2021-09-11 16:18:35 +00:00
|
|
|
self.remove_from_plan(
|
|
|
|
int(self.last_msg.split(":")[1]))
|
2021-04-08 19:53:51 +00:00
|
|
|
),
|
|
|
|
"CLEAR": lambda: self._retMsg(self.clear_channel_plan()),
|
2021-09-11 15:49:08 +00:00
|
|
|
"SETMARKER": lambda: self._retMsg(
|
|
|
|
self.set_marker(
|
|
|
|
self.last_msg.split(":")[1],
|
|
|
|
self.last_msg.split(":", 2)[2],
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"RESETPLAYED": lambda: self._retMsg(
|
|
|
|
self.set_played(
|
|
|
|
weight=int(self.last_msg.split(":")[1]),
|
|
|
|
played=False,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"SETPLAYED": lambda: self._retMsg(
|
|
|
|
self.set_played(
|
|
|
|
weight=int(self.last_msg.split(":")[1]), played=True
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"SETLIVE": lambda: self._retMsg(
|
2021-09-11 16:18:35 +00:00
|
|
|
self.set_live(
|
|
|
|
self.last_msg.split(":")[1] == "True")
|
2021-09-11 15:49:08 +00:00
|
|
|
),
|
2020-11-01 02:37:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:32:43 +00:00
|
|
|
message_type: str = self.last_msg.split(":")[0]
|
|
|
|
|
2021-12-23 22:48:38 +00:00
|
|
|
# From the list above, work out which command type we have, and run it's handling function.
|
2020-11-03 00:32:43 +00:00
|
|
|
if message_type in message_types.keys():
|
|
|
|
message_types[message_type]()
|
2020-10-30 00:32:34 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
elif self.last_msg == "QUIT":
|
2021-04-05 23:32:58 +00:00
|
|
|
self._retMsg(True)
|
2020-10-29 21:23:37 +00:00
|
|
|
self.running = False
|
2020-10-30 00:32:34 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
self._retMsg("Unknown Command")
|
|
|
|
else:
|
2021-12-23 22:48:38 +00:00
|
|
|
# We're not initialised, return a failed status if they asked for one, or just say the command failed
|
2021-04-08 19:53:51 +00:00
|
|
|
if self.last_msg == "STATUS":
|
2020-10-30 00:32:34 +00:00
|
|
|
self._retMsg(self.status)
|
|
|
|
else:
|
|
|
|
self._retMsg(False)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
# Catch the player being killed externally.
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.logger.log.info("Received KeyboardInterupt")
|
|
|
|
except SystemExit:
|
|
|
|
self.logger.log.info("Received SystemExit")
|
|
|
|
except Exception as e:
|
2021-09-11 16:18:35 +00:00
|
|
|
self.logger.log.exception(
|
|
|
|
"Received unexpected Exception: {}".format(e))
|
2020-10-24 20:31:52 +00:00
|
|
|
|
2021-04-04 21:34:46 +00:00
|
|
|
self.logger.log.info("Quiting player " + str(channel))
|
2020-10-29 21:23:37 +00:00
|
|
|
self.quit()
|
2021-04-05 23:32:58 +00:00
|
|
|
self._retAll("QUIT")
|
2021-04-04 21:34:46 +00:00
|
|
|
del self.logger
|
|
|
|
os._exit(0)
|
2020-10-29 21:23:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-08 19:53:51 +00:00
|
|
|
raise Exception(
|
|
|
|
"This BAPSicle Player is a subcomponenet, it will not run individually."
|
|
|
|
)
|