#106: tests: aircox_streamer (#110)

- Writes tests for aircox streamer application;
- Add test utilities in aircox

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#110
This commit is contained in:
Thomas Kairos
2023-06-18 17:00:08 +02:00
parent 73c7c471ea
commit b453c821c7
30 changed files with 2232 additions and 897 deletions

View File

@ -0,0 +1,25 @@
# TODO: for the moment, update in station and program names do not update the
# related fields.
from .metadata import Metadata, Request
from .streamer import Streamer
from .streamers import Streamers
from .sources import Source, PlaylistSource, QueueSource
from .monitor import Monitor
streamers = Streamers()
"""Default controller used by views and viewsets."""
__all__ = (
"Metadata",
"Request",
"Streamer",
"Streamers",
"Source",
"PlaylistSource",
"QueueSource",
"Monitor",
"streamers",
)

View File

@ -0,0 +1,95 @@
import logging
import tzlocal
from django.utils import timezone as tz
from django.utils.translation import gettext_lazy as _
__all__ = (
"Metadata",
"Request",
)
local_tz = tzlocal.get_localzone()
logger = logging.getLogger("aircox")
# FIXME liquidsoap does not manage timezones -- we have to convert
# 'on_air' metadata we get from it into utc one in order to work
# correctly.
class Metadata:
"""Base class for handling request metadata."""
controller = None
"""Controller."""
rid = None
"""Request id."""
uri = None
"""Request uri."""
status = None
"""Current playing status."""
request_status = None
"""Requests' status."""
air_time = None
"""Launch datetime."""
def __init__(self, controller=None, rid=None, data=None):
self.controller = controller
self.rid = rid
if data is not None:
self.validate(data)
@property
def is_playing(self):
"""True if the source is playing."""
# FIXME: validate on controller's current source?
return self.status == "playing"
@property
def status_verbose(self):
"""Verbose version of self's status (translated string)."""
status = self.validate_status(self.status)
return _(status) if status else ""
def fetch(self):
data = self.controller.send("request.metadata ", self.rid, parse=True)
if data:
self.validate(data)
def validate_status(self, status):
"""Return correct status for this metadata based on provided one and
controller.
:returns: status string
"""
on_air = self.controller.source
if "playing" and on_air and (on_air == self or on_air.rid == self.rid):
return "playing"
elif status in ("paused", "playing"):
return "paused"
else:
return "stopped"
def validate_air_time(self, air_time):
if air_time:
air_time = tz.datetime.strptime(air_time, "%Y/%m/%d %H:%M:%S")
return local_tz.localize(air_time)
def validate(self, data):
"""Validate provided data and set as attribute (must already be
declared)"""
for key, value in data.items():
if hasattr(self, key) and not callable(getattr(self, key)):
setattr(self, key, value)
self.uri = data.get("initial_uri")
self.air_time = self.validate_air_time(data.get("on_air"))
self.status = self.validate_status(data.get("status"))
self.request_status = data.get("status")
class Request(Metadata):
title = None
artist = None

View File

@ -0,0 +1,272 @@
# TODO:
# x controllers: remaining
# x diffusion conflicts
# x cancel
# x when liquidsoap fails to start/exists: exit
# - handle restart after failure
# - is stream restart after live ok?
from django.utils import timezone as tz
from aircox.models import Diffusion, Log, Sound, Track
class Monitor:
"""Log and launch diffusions for the given station.
Monitor should be able to be used after a crash a go back
where it was playing, so we heavily use logs to be able to
do that.
We keep trace of played items on the generated stream:
- sounds played on this stream;
- scheduled diffusions
- tracks for sounds of streamed programs
"""
streamer = None
"""Streamer controller."""
delay = None
""" Timedelta: minimal delay between two call of monitor. """
logs = None
"""Queryset to station's logs (ordered by -pk)"""
cancel_timeout = tz.timedelta(minutes=20)
"""Timeout in minutes before cancelling a diffusion."""
sync_timeout = tz.timedelta(minutes=5)
"""Timeout in minutes between two streamer's sync."""
sync_next = None
"""Datetime of the next sync."""
last_sound_logs = None
"""Last logged sounds, as ``{source_id: log}``."""
@property
def station(self):
return self.streamer.station
@property
def last_log(self):
"""Last log of monitored station."""
return self.logs.first()
@property
def last_diff_start(self):
"""Log of last triggered item (sound or diffusion)."""
return self.logs.start().with_diff().first()
def __init__(self, streamer, delay, **kwargs):
self.streamer = streamer
# adding time ensures all calculations have a margin
self.delay = delay + tz.timedelta(seconds=5)
self.__dict__.update(kwargs)
self.logs = self.get_logs_queryset()
self.init_last_sound_logs()
def get_logs_queryset(self):
"""Return queryset to assign as `self.logs`"""
return self.station.log_set.select_related(
"diffusion", "sound", "track"
).order_by("-pk")
def init_last_sound_logs(self):
"""Retrieve last logs and initialize `last_sound_logs`"""
logs = {}
for source in self.streamer.sources:
qs = self.logs.filter(source=source.id, sound__isnull=False)
logs[source.id] = qs.first()
self.last_sound_logs = logs
def monitor(self):
"""Run all monitoring functions once."""
if not self.streamer.is_ready:
return
self.streamer.fetch()
# Skip tracing - analyzis:
# Reason: multiple database request every x seconds, reducing it.
# We could skip this part when remaining time is higher than a minimal
# value (which should be derived from Command's delay). Problems:
# - How to trace tracks? (+ Source can change: caching log might sucks)
# - if liquidsoap's source/request changes: remaining time goes higher,
# thus avoiding fetch
#
# Approach: something like having a mean time, such as:
#
# ```
# source = stream.source
# mean_time = source.air_time
# + min(next_track.timestamp, source.remaining)
# - (command.delay + 1)
# trace_required = \/ source' != source
# \/ source.uri' != source.uri
# \/ now < mean_time
# ```
#
source = self.streamer.source
if source and source.uri:
log = self.trace_sound(source)
if log:
self.trace_tracks(log)
else:
print("no source or sound for stream; source = ", source)
self.handle_diffusions()
self.sync()
def trace_sound(self, source):
"""Return on air sound log (create if not present)."""
air_uri, air_time = source.uri, source.air_time
last_log = self.last_sound_logs.get(source.id)
if last_log and last_log.sound.file.path == source.uri:
return last_log
# FIXME: can be a sound played when no Sound instance? If not, remove
# comment.
# check if there is yet a log for this sound on the source
# log = self.logs.on_air().filter(
# Q(sound__file=air_uri) |
# # sound can be null when arbitrary sound file is played
# Q(sound__isnull=True, track__isnull=True, comment=air_uri),
# source=source.id,
# date__range=date_range(air_time, self.delay),
# ).first()
# if log:
# return log
# get sound
diff = None
sound = Sound.objects.path(air_uri).first()
if sound and sound.episode_id is not None:
diff = (
Diffusion.objects.episode(id=sound.episode_id)
.on_air()
.now(air_time)
.first()
)
# log sound on air
return self.log(
type=Log.TYPE_ON_AIR,
date=source.air_time,
source=source.id,
sound=sound,
diffusion=diff,
comment=air_uri,
)
def trace_tracks(self, log):
"""Log tracks for the given sound log (for streamed programs only)."""
if log.diffusion:
return
tracks = Track.objects.filter(
sound_id=log.sound_id, timestamp__isnull=False
).order_by("timestamp")
if not tracks.exists():
return
# exclude already logged tracks
tracks = tracks.exclude(log__station=self.station, log__pk__gt=log.pk)
now = tz.now()
for track in tracks:
pos = log.date + tz.timedelta(seconds=track.timestamp)
if pos <= now:
self.log(
type=Log.TYPE_ON_AIR,
date=pos,
source=log.source,
track=track,
comment=track,
)
def handle_diffusions(self):
"""Handle scheduled diffusion, trigger if needed, preload playlists and
so on."""
# TODO: program restart
# Diffusion conflicts are handled by the way a diffusion is defined
# as candidate for the next dealer's start.
#
# ```
# logged_diff: /\ \A diff in diffs: \E log: /\ log.type = START
# /\ log.diff = diff
# /\ log.date = diff.start
# queue_empty: /\ dealer.queue is empty
# /\ \/ ~dealer.on_air
# \/ dealer.remaining < delay
#
# start_allowed: /\ diff not in logged_diff
# /\ queue_empty
#
# start_canceled: /\ diff not in logged diff
# /\ ~queue_empty
# /\ diff.start < now + cancel_timeout
# ```
#
now = tz.now()
diff = (
Diffusion.objects.station(self.station)
.on_air()
.now(now)
.filter(episode__sound__type=Sound.TYPE_ARCHIVE)
.first()
)
# Can't use delay: diffusion may start later than its assigned start.
log = None if not diff else self.logs.start().filter(diffusion=diff)
if not diff or log:
return
dealer = self.streamer.dealer
# start
if (
not dealer.queue
and dealer.rid is None
or dealer.remaining < self.delay.total_seconds()
):
self.start_diff(dealer, diff)
# cancel
if diff.start < now - self.cancel_timeout:
self.cancel_diff(dealer, diff)
def log(self, source, **kwargs):
"""Create a log using **kwargs, and print info."""
kwargs.setdefault("station", self.station)
kwargs.setdefault("date", tz.now())
log = Log(source=source, **kwargs)
log.save()
log.print()
if log.sound:
self.last_sound_logs[source] = log
return log
def start_diff(self, source, diff):
playlist = Sound.objects.episode(id=diff.episode_id).playlist()
source.push(*playlist)
self.log(
type=Log.TYPE_START,
source=source.id,
diffusion=diff,
comment=str(diff),
)
def cancel_diff(self, source, diff):
diff.type = Diffusion.TYPE_CANCEL
diff.save()
self.log(
type=Log.TYPE_CANCEL,
source=source.id,
diffusion=diff,
comment=str(diff),
)
def sync(self):
"""Update sources' playlists."""
now = tz.now()
if self.sync_next is not None and now < self.sync_next:
return
self.sync_next = now + self.sync_timeout
for source in self.streamer.playlists:
source.sync()

View File

@ -0,0 +1,141 @@
import os
import tzlocal
from aircox.utils import to_seconds
from .metadata import Metadata, Request
__all__ = (
"Source",
"PlaylistSource",
"QueueSource",
)
local_tz = tzlocal.get_localzone()
class Source(Metadata):
controller = None
"""Parent controller."""
id = None
"""Source id."""
remaining = 0.0
"""Remaining time."""
status = "stopped"
@property
def station(self):
return self.controller.station
def __init__(self, controller=None, id=None, *args, **kwargs):
super().__init__(controller, *args, **kwargs)
self.id = id
def sync(self):
"""Synchronize what should be synchronized."""
def fetch(self):
try:
data = self.controller.send(self.id, ".remaining")
if data:
self.remaining = float(data)
except ValueError:
self.remaining = None
data = self.controller.send(self.id, ".get", parse=True)
if data:
self.validate(data if data and isinstance(data, dict) else {})
def skip(self):
"""Skip the current source sound."""
self.controller.send(self.id, ".skip")
def restart(self):
"""Restart current sound."""
# seek 10 hours back since there is not possibility to get current pos
self.seek(-216000 * 10)
def seek(self, n):
"""Seeks into the sound."""
self.controller.send(self.id, ".seek ", str(n))
class PlaylistSource(Source):
"""Source handling playlists (program streams)"""
path = None
"""Path to playlist."""
program = None
"""Related program."""
playlist = None
"""The playlist."""
def __init__(self, controller, id=None, program=None, **kwargs):
id = program.slug.replace("-", "_") if id is None else id
self.program = program
super().__init__(controller, id=id, **kwargs)
self.path = os.path.join(self.station.path, f"{self.id}.m3u")
def get_sound_queryset(self):
"""Get playlist's sounds queryset."""
return self.program.sound_set.archive()
def get_playlist(self):
"""Get playlist from db."""
return self.get_sound_queryset().playlist()
def write_playlist(self, playlist=[]):
"""Write playlist to file."""
os.makedirs(os.path.dirname(self.path), exist_ok=True)
with open(self.path, "w") as file:
file.write("\n".join(playlist or []))
def stream(self):
"""Return program's stream info if any (or None) as dict."""
# used in templates
# TODO: multiple streams
stream = self.program.stream_set.all().first()
if not stream or (not stream.begin and not stream.delay):
return
return {
"begin": stream.begin.strftime("%Hh%M") if stream.begin else None,
"end": stream.end.strftime("%Hh%M") if stream.end else None,
"delay": to_seconds(stream.delay) if stream.delay else 0,
}
def sync(self):
playlist = self.get_playlist()
self.write_playlist(playlist)
class QueueSource(Source):
queue = None
"""Source's queue (excluded on_air request)"""
@property
def requests(self):
"""Queue as requests metadata."""
requests = [Request(self.controller, rid) for rid in self.queue]
for request in requests:
request.fetch()
return requests
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def push(self, *paths):
"""Add the provided paths to source's play queue."""
for path in paths:
self.controller.send(f"{self.id}_queue.push {path}")
def fetch(self):
super().fetch()
queue = self.controller.send(f"{self.id}_queue.queue").strip()
if not queue:
self.queue = []
return
self.queue = queue.split(" ")

View File

@ -0,0 +1,193 @@
import atexit
import logging
import os
import re
import signal
import subprocess
import psutil
from django.template.loader import render_to_string
from aircox.conf import settings
from ..connector import Connector
from .sources import PlaylistSource, QueueSource
__all__ = ("Streamer",)
logger = logging.getLogger("aircox")
class Streamer:
connector = None
process = None
station = None
template_name = "aircox_streamer/scripts/station.liq"
path = None
"""Config path."""
sources = None
"""List of all monitored sources."""
source = None
"""Current source being played on air."""
# note: we disable on_air rids since we don't have use of it for the
# moment
# on_air = None
# """ On-air request ids (rid) """
inputs = None
"""Queryset to input ports."""
outputs = None
"""Queryset to output ports."""
def __init__(self, station, connector=None):
self.station = station
self.inputs = self.station.port_set.active().input()
self.outputs = self.station.port_set.active().output()
self.id = self.station.slug.replace("-", "_")
self.path = os.path.join(station.path, "station.liq")
self.connector = connector or Connector(
os.path.join(station.path, "station.sock")
)
self.init_sources()
@property
def socket_path(self):
"""Path to Unix socket file."""
return self.connector.address
@property
def is_ready(self):
"""If external program is ready to use, returns True."""
return self.send("list") != ""
@property
def is_running(self):
"""True if holds a running process."""
if self.process is None:
return False
returncode = self.process.poll()
if returncode is None:
return True
self.process = None
logger.debug("process died with return code %s" % returncode)
return False
@property
def playlists(self):
return (s for s in self.sources if isinstance(s, PlaylistSource))
@property
def queues(self):
return (s for s in self.sources if isinstance(s, QueueSource))
# Sources and config ###############################################
def send(self, *args, **kwargs):
return self.connector.send(*args, **kwargs) or ""
def init_sources(self):
streams = self.station.program_set.filter(stream__isnull=False)
self.dealer = QueueSource(self, "dealer")
self.sources = [self.dealer] + [
PlaylistSource(self, program=program) for program in streams
]
def make_config(self):
"""Make configuration files and directory (and sync sources)"""
data = render_to_string(
self.template_name,
{
"station": self.station,
"streamer": self,
"settings": settings,
},
)
data = re.sub("[\t ]+\n", "\n", data)
data = re.sub("\n{3,}", "\n\n", data)
os.makedirs(os.path.dirname(self.path), exist_ok=True)
with open(self.path, "w+") as file:
file.write(data)
self.sync()
def sync(self):
"""Sync all sources."""
for source in self.sources:
source.sync()
def fetch(self):
"""Fetch data from liquidsoap."""
for source in self.sources:
source.fetch()
# request.on_air is not ordered: we need to do it manually
self.source = next(
iter(
sorted(
(
source
for source in self.sources
if source.request_status == "playing"
and source.air_time
),
key=lambda o: o.air_time,
reverse=True,
)
),
None,
)
# Process ##########################################################
def get_process_args(self):
return ["liquidsoap", "-v", self.path]
def check_zombie_process(self):
if not os.path.exists(self.socket_path):
return
conns = [
conn
for conn in psutil.net_connections(kind="unix")
if conn.laddr == self.socket_path
]
for conn in conns:
if conn.pid is not None:
os.kill(conn.pid, signal.SIGKILL)
def run_process(self):
"""Execute the external application with corresponding informations.
This function must make sure that all needed files have been
generated.
"""
if self.process:
return
args = self.get_process_args()
if not args:
return
self.check_zombie_process()
self.process = subprocess.Popen(args, stderr=subprocess.STDOUT)
atexit.register(self.kill_process)
def kill_process(self):
if self.process:
logger.debug(
"kill process %s: %s",
self.process.pid,
" ".join(self.get_process_args()),
)
self.process.kill()
self.process = None
atexit.unregister(self.kill_process)
def wait_process(self):
"""Wait for the process to terminate if there is a process."""
if self.process:
self.process.wait()
self.process = None

View File

@ -0,0 +1,62 @@
from django.utils import timezone as tz
from aircox.models import Station
from .streamer import Streamer
__all__ = ("Streamers",)
class Streamers:
"""Keep multiple streamers in memory, allow fetching informations."""
streamers = None
"""Stations by station id."""
streamer_class = Streamer
timeout = None
"""Timedelta to next update."""
next_date = None
"""Next update datetime."""
def __init__(self, timeout=None, streamer_class=streamer_class):
self.timeout = timeout or tz.timedelta(seconds=2)
def reset(self, stations=Station.objects.active()):
# FIXME: cf. TODO in aircox.controllers about model updates
stations = stations.all()
self.streamers = {
station.pk: self.streamer_class(station) for station in stations
}
def fetch(self):
"""Call streamers fetch if timed-out."""
if self.streamers is None:
self.reset()
now = tz.now()
if self.next_date is not None and now < self.next_date:
return
for streamer in self.streamers.values():
streamer.fetch()
self.next_date = now + self.timeout
def get(self, key, default=None):
return self.streamers.get(key, default)
def values(self):
return self.streamers.values()
def __len__(self):
return self.streamers and len(self.streamers) or 0
def __getitem__(self, key):
return self.streamers[key]
def __contains__(self, key):
"""Key can be a Station or a Station id."""
if isinstance(key, Station):
return key.pk in self.streamers
return key in self.streamers
def __iter__(self):
return self.streamers.values() if self.streamers else iter(tuple())