2010-11-05 15:54:15 +01:00
|
|
|
"""
|
|
|
|
Python part of radio playout (pypo)
|
|
|
|
"""
|
2020-01-16 15:32:51 +01:00
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
import re
|
2018-12-23 11:01:25 +01:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import telnetlib
|
|
|
|
import time
|
2021-06-03 15:20:39 +02:00
|
|
|
from datetime import datetime
|
2022-01-13 16:11:37 +01:00
|
|
|
from pathlib import Path
|
|
|
|
from queue import Queue
|
|
|
|
from threading import Lock
|
|
|
|
from typing import Optional
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2022-01-13 16:11:37 +01:00
|
|
|
import click
|
2022-01-02 21:02:55 +01:00
|
|
|
from libretime_api_client.version1 import AirtimeApiClient as ApiClient
|
2022-01-18 20:59:11 +01:00
|
|
|
from libretime_shared.cli import cli_config_options, cli_logging_options
|
2022-01-13 16:11:37 +01:00
|
|
|
from libretime_shared.config import DEFAULT_ENV_PREFIX
|
|
|
|
from libretime_shared.logging import level_from_name, setup_logger
|
|
|
|
from loguru import logger
|
2012-03-16 04:14:19 +01:00
|
|
|
|
2021-06-03 15:20:39 +02:00
|
|
|
from . import pure
|
2022-01-18 20:59:11 +01:00
|
|
|
from .config import CACHE_DIR, RECORD_DIR, Config
|
2018-12-23 11:01:25 +01:00
|
|
|
from .listenerstat import ListenerStat
|
|
|
|
from .pypofetch import PypoFetch
|
|
|
|
from .pypofile import PypoFile
|
|
|
|
from .pypoliquidsoap import PypoLiquidsoap
|
|
|
|
from .pypomessagehandler import PypoMessageHandler
|
|
|
|
from .pypopush import PypoPush
|
|
|
|
from .recorder import Recorder
|
|
|
|
from .timeout import ls_timeout
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
LIQUIDSOAP_MIN_VERSION = "1.1.1"
|
|
|
|
|
2015-01-28 19:19:50 +01:00
|
|
|
|
2010-11-05 15:54:15 +01:00
|
|
|
class Global:
|
2013-05-06 17:42:39 +02:00
|
|
|
def __init__(self, api_client):
|
|
|
|
self.api_client = api_client
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-03-02 22:43:46 +01:00
|
|
|
def selfcheck(self):
|
2011-05-13 22:08:04 +02:00
|
|
|
return self.api_client.is_server_compatible()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-03-02 23:26:39 +01:00
|
|
|
def test_api(self):
|
|
|
|
self.api_client.test()
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2011-03-22 16:42:09 +01:00
|
|
|
def keyboardInterruptHandler(signum, frame):
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.info("\nKeyboard Interrupt\n")
|
2011-06-01 18:32:42 +02:00
|
|
|
sys.exit(0)
|
2011-03-02 23:26:39 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2022-01-13 16:11:37 +01:00
|
|
|
def liquidsoap_get_info(telnet_lock, host, port):
|
2012-06-28 22:15:39 +02:00
|
|
|
logger.debug("Checking to see if Liquidsoap is running")
|
|
|
|
try:
|
|
|
|
telnet_lock.acquire()
|
|
|
|
tn = telnetlib.Telnet(host, port)
|
|
|
|
msg = "version\n"
|
2020-01-23 11:37:49 +01:00
|
|
|
tn.write(msg.encode("utf-8"))
|
2022-01-25 23:45:00 +01:00
|
|
|
tn.write(b"exit\n")
|
2020-01-23 11:37:49 +01:00
|
|
|
response = tn.read_all().decode("utf-8")
|
2018-12-23 11:01:25 +01:00
|
|
|
except Exception as e:
|
2020-01-23 11:37:49 +01:00
|
|
|
logger.error(e)
|
2013-05-13 21:45:56 +02:00
|
|
|
return None
|
2012-06-28 22:15:39 +02:00
|
|
|
finally:
|
|
|
|
telnet_lock.release()
|
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
return get_liquidsoap_version(response)
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
def get_liquidsoap_version(version_string):
|
2013-05-17 18:14:33 +02:00
|
|
|
m = re.match(r"Liquidsoap (\d+.\d+.\d+)", version_string)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
def liquidsoap_startup_test(telnet_lock, liquidsoap_host, liquidsoap_port):
|
2013-05-13 21:45:56 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock,
|
|
|
|
liquidsoap_host,
|
|
|
|
liquidsoap_port,
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
while not liquidsoap_version_string:
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.warning(
|
|
|
|
"Liquidsoap doesn't appear to be running!, " + "Sleeping and trying again"
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
time.sleep(1)
|
2022-01-18 20:59:11 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock,
|
|
|
|
liquidsoap_host,
|
|
|
|
liquidsoap_port,
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
while pure.version_cmp(liquidsoap_version_string, LIQUIDSOAP_MIN_VERSION) < 0:
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.warning(
|
|
|
|
"Liquidsoap is running but in incorrect version! "
|
|
|
|
+ "Make sure you have at least Liquidsoap %s installed"
|
|
|
|
% LIQUIDSOAP_MIN_VERSION
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
time.sleep(1)
|
2022-01-18 20:59:11 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock,
|
|
|
|
liquidsoap_host,
|
|
|
|
liquidsoap_port,
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
logger.info("Liquidsoap version string found %s" % liquidsoap_version_string)
|
2012-06-28 22:15:39 +02:00
|
|
|
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2022-05-05 09:41:32 +02:00
|
|
|
@click.command(context_settings={"auto_envvar_prefix": DEFAULT_ENV_PREFIX})
|
2022-01-20 06:30:35 +01:00
|
|
|
@cli_logging_options()
|
2022-01-18 20:59:11 +01:00
|
|
|
@cli_config_options()
|
|
|
|
def cli(log_level: str, log_filepath: Optional[Path], config_filepath: Optional[Path]):
|
2022-01-13 16:11:37 +01:00
|
|
|
"""
|
|
|
|
Run playout.
|
|
|
|
"""
|
|
|
|
setup_logger(level_from_name(log_level), log_filepath)
|
2022-01-18 20:59:11 +01:00
|
|
|
config = Config(filepath=config_filepath)
|
2022-01-18 05:55:16 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
for dir_path in [CACHE_DIR, RECORD_DIR]:
|
|
|
|
dir_path.mkdir(exist_ok=True)
|
|
|
|
except OSError as exception:
|
|
|
|
logger.error(exception)
|
2022-01-13 16:11:37 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.info("###########################################")
|
|
|
|
logger.info("# *** pypo *** #")
|
|
|
|
logger.info("# Liquidsoap Scheduled Playout System #")
|
|
|
|
logger.info("###########################################")
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
# Although all of our calculations are in UTC, it is useful to know what timezone
|
|
|
|
# the local machine is, so that we have a reference for what time the actual
|
|
|
|
# log entries were made
|
2012-06-28 22:16:02 +02:00
|
|
|
logger.info("Timezone: %s" % str(time.tzname))
|
|
|
|
logger.info("UTC time: %s" % str(datetime.utcnow()))
|
2012-06-28 18:12:22 +02:00
|
|
|
|
2011-03-22 16:42:09 +01:00
|
|
|
signal.signal(signal.SIGINT, keyboardInterruptHandler)
|
2011-05-31 18:45:27 +02:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
api_client = ApiClient()
|
2013-05-06 17:42:39 +02:00
|
|
|
g = Global(api_client)
|
2011-05-13 22:08:04 +02:00
|
|
|
|
2012-06-28 22:15:39 +02:00
|
|
|
while not g.selfcheck():
|
|
|
|
time.sleep(5)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-05-06 17:42:39 +02:00
|
|
|
success = False
|
|
|
|
while not success:
|
|
|
|
try:
|
2018-12-23 11:01:25 +01:00
|
|
|
api_client.register_component("pypo")
|
2013-05-06 17:42:39 +02:00
|
|
|
success = True
|
2018-12-23 11:01:25 +01:00
|
|
|
except Exception as e:
|
2013-05-06 17:42:39 +02:00
|
|
|
logger.error(str(e))
|
|
|
|
time.sleep(10)
|
|
|
|
|
2012-06-28 22:15:39 +02:00
|
|
|
telnet_lock = Lock()
|
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
liquidsoap_host = config.playout.liquidsoap_host
|
|
|
|
liquidsoap_port = config.playout.liquidsoap_port
|
2013-05-13 21:45:56 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
liquidsoap_startup_test(telnet_lock, liquidsoap_host, liquidsoap_port)
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
pypoFetch_q = Queue()
|
2012-02-24 19:12:50 +01:00
|
|
|
recorder_q = Queue()
|
2012-02-28 20:44:39 +01:00
|
|
|
pypoPush_q = Queue()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
pypo_liquidsoap = PypoLiquidsoap(telnet_lock, liquidsoap_host, liquidsoap_port)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
"""
|
|
|
|
This queue is shared between pypo-fetch and pypo-file, where pypo-file
|
2013-04-26 04:11:26 +02:00
|
|
|
is the consumer. Pypo-fetch will send every schedule it gets to pypo-file
|
2012-03-01 23:58:44 +01:00
|
|
|
and pypo will parse this schedule to determine which file has the highest
|
2013-04-26 04:11:26 +02:00
|
|
|
priority, and retrieve it.
|
2012-03-01 23:58:44 +01:00
|
|
|
"""
|
|
|
|
media_q = Queue()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2014-12-17 01:42:07 +01:00
|
|
|
# Pass only the configuration sections needed; PypoMessageHandler only needs rabbitmq settings
|
2022-01-18 20:59:11 +01:00
|
|
|
pmh = PypoMessageHandler(pypoFetch_q, recorder_q, config.rabbitmq)
|
2012-02-28 20:44:39 +01:00
|
|
|
pmh.daemon = True
|
|
|
|
pmh.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
pfile = PypoFile(media_q)
|
2012-03-01 23:58:44 +01:00
|
|
|
pfile.daemon = True
|
|
|
|
pfile.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
pf = PypoFetch(
|
2022-01-18 20:59:11 +01:00
|
|
|
pypoFetch_q,
|
|
|
|
pypoPush_q,
|
|
|
|
media_q,
|
|
|
|
telnet_lock,
|
|
|
|
pypo_liquidsoap,
|
|
|
|
config,
|
2018-12-23 11:01:25 +01:00
|
|
|
)
|
2012-02-28 20:44:39 +01:00
|
|
|
pf.daemon = True
|
|
|
|
pf.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
pp = PypoPush(pypoPush_q, telnet_lock, pypo_liquidsoap, config)
|
2011-08-23 17:13:06 +02:00
|
|
|
pp.daemon = True
|
2011-03-21 00:34:43 +01:00
|
|
|
pp.start()
|
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
recorder = Recorder(recorder_q, config)
|
2012-02-24 19:12:50 +01:00
|
|
|
recorder.daemon = True
|
|
|
|
recorder.start()
|
|
|
|
|
2012-11-07 23:19:47 +01:00
|
|
|
stat = ListenerStat(config)
|
2012-11-02 22:50:43 +01:00
|
|
|
stat.daemon = True
|
|
|
|
stat.start()
|
|
|
|
|
2015-05-23 00:20:45 +02:00
|
|
|
# Just sleep the main thread, instead of blocking on pf.join().
|
|
|
|
# This allows CTRL-C to work!
|
|
|
|
while True:
|
|
|
|
time.sleep(1)
|