From 654259824c714f906824fb8b43416ea4d4d15038 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 20 Feb 2023 22:01:24 +0100 Subject: [PATCH] feat(playout): use liquidsoap interactive variables --- .../liquidsoap/1.4/ls_lib.liq | 42 ++++++++++++------- .../liquidsoap/1.4/ls_script.liq | 33 ++++----------- .../liquidsoap/client/_client.py | 25 +++++++---- .../liquidsoap/entrypoint.liq.j2 | 8 ++-- .../__snapshots__/entrypoint_test.ambr | 8 ++-- 5 files changed, 58 insertions(+), 58 deletions(-) diff --git a/playout/libretime_playout/liquidsoap/1.4/ls_lib.liq b/playout/libretime_playout/liquidsoap/1.4/ls_lib.liq index c71654b4a..cab734db6 100644 --- a/playout/libretime_playout/liquidsoap/1.4/ls_lib.liq +++ b/playout/libretime_playout/liquidsoap/1.4/ls_lib.liq @@ -27,17 +27,17 @@ end # A function applied to each metadata chunk def append_title(m) = - log("Using stream_format #{!stream_metadata_type}") + log("Using message format #{message_format()}") if list.mem_assoc("mapped", m) then #protection against applying this function twice. It shouldn't be happening #and bug file with Liquidsoap. m else - if !stream_metadata_type == 1 then - [("title", "#{!show_name} - #{m['artist']} - #{m['title']}"), ("mapped", "true")] - elsif !stream_metadata_type == 2 then - [("title", "#{!station_name} - #{!show_name}"), ("mapped", "true")] + if message_format() == "1" then + [("title", "#{show_name()} - #{m['artist']} - #{m['title']}"), ("mapped", "true")] + elsif message_format() == "2" then + [("title", "#{station_name()} - #{show_name()}"), ("mapped", "true")] else if "#{m['artist']}" == "" then [("title", "#{m['title']}"), ("mapped", "true")] @@ -50,10 +50,16 @@ end def transition(a,b) = log("transition called...") - add(normalize=false, - [ sequence([ blank(duration=0.01), - fade.initial(duration=!default_dj_fade, b) ]), - fade.final(duration=!default_dj_fade, a) ]) + add( + normalize=false, + [ + sequence([ + blank(duration=0.01), + fade.initial(duration=input_fade_transition(), b) + ]), + fade.final(duration=input_fade_transition(), a) + ] + ) end # we need this function for special transition case(from default to queue) @@ -63,10 +69,16 @@ def transition_default(a,b) = log("transition called...") if !just_switched then just_switched := false - add(normalize=false, - [ sequence([ blank(duration=0.01), - fade.initial(duration=!default_dj_fade, b) ]), - fade.final(duration=!default_dj_fade, a) ]) + add( + normalize=false, + [ + sequence([ + blank(duration=0.01), + fade.initial(duration=input_fade_transition(), b) + ]), + fade.final(duration=input_fade_transition(), a) + ] + ) else just_switched := false b @@ -90,12 +102,12 @@ def output_to(output_type, type, bitrate, host, port, pass, mount_point, url, de source = ref s def on_error(msg) connected := "false" - gateway("stream '#{stream}' '#{!time}' --error='#{msg}'") + gateway("stream '#{stream}' '#{boot_timestamp()}' --error='#{msg}'") 5. end def on_connect() connected := "true" - gateway("stream '#{stream}' '#{!time}'") + gateway("stream '#{stream}' '#{boot_timestamp()}'") end stereo = (channels == "stereo") diff --git a/playout/libretime_playout/liquidsoap/1.4/ls_script.liq b/playout/libretime_playout/liquidsoap/1.4/ls_script.liq index 684364ed1..c49527678 100644 --- a/playout/libretime_playout/liquidsoap/1.4/ls_script.liq +++ b/playout/libretime_playout/liquidsoap/1.4/ls_script.liq @@ -1,4 +1,4 @@ -time = ref string_of(gettimeofday()) +boot_timestamp = interactive.string("boot_timestamp", string_of(gettimeofday())) #Dynamic source list #dyn_sources = ref [] @@ -6,7 +6,7 @@ webstream_enabled = ref false current_dyn_id = ref '-1' -show_name = ref '' +show_name = interactive.string("show_name", "") dynamic_metadata_callback = ref fun (~new_track=false, s) -> begin () end @@ -76,27 +76,9 @@ stream_queue = map_metadata(update=false, append_title, stream_queue) ignore(output.dummy(stream_queue, fallible=true)) -server.register(namespace="vars", - "stream_metadata_type", - fun (s) -> begin log("vars.stream_metadata_type") stream_metadata_type := int_of_string(s) s end) -server.register(namespace="vars", - "show_name", - fun (s) -> begin log("vars.show_name") show_name := s s end) -server.register(namespace="vars", - "station_name", - fun (s) -> begin log("vars.station_name") station_name := s s end) -server.register(namespace="vars", - "off_air_meta", - fun (s) -> begin log("vars.off_air_meta") off_air_meta := s s end) -server.register(namespace="vars", - "bootup_time", - fun (s) -> begin log("vars.bootup_time") time := s s end) server.register(namespace="streams", "connection_status", fun (s) -> begin log("streams.connection_status") "1:#{!s1_connected},2:#{!s2_connected},3:#{!s3_connected},4:#{!s4_connected}" end) -server.register(namespace="vars", - "default_dj_fade", - fun (s) -> begin log("vars.default_dj_fade") default_dj_fade := float_of_string(s) s end) server.register(namespace="dynamic_source", description="Enable webstream output", @@ -141,13 +123,12 @@ server.register(namespace="dynamic_source", # fun (s) -> begin log("dynamic_source.read_stop") destroy_dynamic_source_all() end) default = amplify(id="silence_src", 0.00001, noise()) -if !off_air_meta == "" then - off_air_meta := "LibreTime - offline" + +def map_message_offline(m) = + [("title", message_offline())] end -def map_off_air_meta(m) = - [("title", !off_air_meta)] -end -default = map_metadata(map_off_air_meta, default) + +default = map_metadata(map_message_offline, default) ignore(output.dummy(default, fallible=true)) master_dj_enabled = ref false diff --git a/playout/libretime_playout/liquidsoap/client/_client.py b/playout/libretime_playout/liquidsoap/client/_client.py index 054ac9cfd..456ba271a 100644 --- a/playout/libretime_playout/liquidsoap/client/_client.py +++ b/playout/libretime_playout/liquidsoap/client/_client.py @@ -5,6 +5,8 @@ from typing import Any, Literal, Optional, Tuple from loguru import logger +from ..models import MessageFormatKind +from ..utils import quote from ..version import parse_liquidsoap_version from ._connection import LiquidsoapConnection @@ -36,9 +38,14 @@ class LiquidsoapClient: timeout=timeout, ) + def _quote(self, value: Any): + return quote(value, double=True) + def _set_var(self, name: str, value: Any): - self.conn.write(f"vars.{name} {value}") - self.conn.read() + self.conn.write(f"var.set {name} = {value}") + result = self.conn.read() + if f"Variable {name} set" not in result: + logger.error(result) def version(self) -> Tuple[int, int, int]: with self.conn: @@ -71,7 +78,7 @@ class LiquidsoapClient: def queue_push(self, queue_id: int, entry: str, show_name: str) -> None: with self.conn: self.conn.write(f"{queue_id}.push {entry}") - self._set_var("show_name", show_name) + self._set_var("show_name", self._quote(show_name)) def web_stream_get_id(self) -> str: with self.conn: @@ -118,23 +125,23 @@ class LiquidsoapClient: self, *, station_name: Optional[str] = None, - message_format: Optional[int] = None, + message_format: Optional[MessageFormatKind] = None, message_offline: Optional[str] = None, input_fade_transition: Optional[float] = None, ): with self.conn: if station_name is not None: - self._set_var("station_name", station_name) + self._set_var("station_name", self._quote(station_name)) if message_format is not None: - self._set_var("stream_metadata_type", message_format) + self._set_var("message_format", self._quote(message_format.value)) if message_offline is not None: - self._set_var("off_air_meta", message_offline) + self._set_var("message_offline", self._quote(message_offline)) if input_fade_transition is not None: - self._set_var("default_dj_fade", input_fade_transition) + self._set_var("input_fade_transition", input_fade_transition) def boot_timestamp_update(self, timestamp: float): with self.conn: - self._set_var("bootup_time", str(timestamp)) + self._set_var("boot_timestamp", self._quote(timestamp)) def restart(self): logger.warning("restarting Liquidsoap") diff --git a/playout/libretime_playout/liquidsoap/entrypoint.liq.j2 b/playout/libretime_playout/liquidsoap/entrypoint.liq.j2 index 389fe7519..9167f71ee 100644 --- a/playout/libretime_playout/liquidsoap/entrypoint.liq.j2 +++ b/playout/libretime_playout/liquidsoap/entrypoint.liq.j2 @@ -49,10 +49,10 @@ set("server.telnet.port", {{ config.liquidsoap.server_listen_port }}) set("harbor.bind_addrs", ["{{ config.liquidsoap.harbor_listen_address | join('", "') }}"]) -station_name = ref {{ info.station_name | quote }} +station_name = interactive.string("station_name", {{ info.station_name | quote }}) -off_air_meta = ref {{ preferences.message_offline | quote }} -stream_metadata_type = ref {{ preferences.message_format.value }} -default_dj_fade = ref {{ preferences.input_fade_transition }} +message_offline = interactive.string("message_offline", {{ preferences.message_offline | quote }}) +message_format = interactive.string("message_format", {{ preferences.message_format.value | quote }}) +input_fade_transition = interactive.float("input_fade_transition", {{ preferences.input_fade_transition }}) %include "{{ paths.lib_filepath }}" diff --git a/playout/tests/liquidsoap/__snapshots__/entrypoint_test.ambr b/playout/tests/liquidsoap/__snapshots__/entrypoint_test.ambr index aa887ba3c..f7f5d8702 100644 --- a/playout/tests/liquidsoap/__snapshots__/entrypoint_test.ambr +++ b/playout/tests/liquidsoap/__snapshots__/entrypoint_test.ambr @@ -100,11 +100,11 @@ set("harbor.bind_addrs", ["0.0.0.0"]) - station_name = ref "LibreTime" + station_name = interactive.string("station_name", "LibreTime") - off_air_meta = ref "LibreTime - offline" - stream_metadata_type = ref 0 - default_dj_fade = ref 0.0 + message_offline = interactive.string("message_offline", "LibreTime - offline") + message_format = interactive.string("message_format", "0") + input_fade_transition = interactive.float("input_fade_transition", 0.0) %include "/fake/1.4/ls_script.liq"