From 424e0a520e0ef605425525cccb40eff2db03c5a6 Mon Sep 17 00:00:00 2001 From: boyska Date: Mon, 28 Oct 2024 11:51:25 +0100 Subject: [PATCH] initial commit, experiments --- sapo.liq | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ server.liq | 8 ++++ 2 files changed, 130 insertions(+) create mode 100755 sapo.liq create mode 100755 server.liq diff --git a/sapo.liq b/sapo.liq new file mode 100755 index 0000000..b449754 --- /dev/null +++ b/sapo.liq @@ -0,0 +1,122 @@ +#!/usr/bin/liquidsoap + +upstream = input.http(id="http", "http://s.streampunk.cc/ondarossa.ogg") +playlist = playlist.list(["a.mp3", "b.mp3"], mode="normal", loop=false) + +audio = fallback([upstream, playlist], track_sensitive=false) + +icecast = output.icecast(%vorbis, +fallible=true, +host="localhost", +port=8081, +user="source", +password="hackme", +mount="/stream.ogg", +start=false, +audio) +# output(audio) # per debug: così sento dalle casse + +def icecast_change(~protocol, ~data, ~headers, uri) = + did_something = if string.contains(suffix="/start", uri) then + icecast.start() + true + elsif string.contains(suffix="/stop", uri) then + icecast.stop() + true + elsif string.contains(suffix="/toggle", uri) then + if icecast.is_started() then icecast.stop() else icecast.start() end + true + else + false + end + if did_something then + http.response(protocol=protocol, code=200, + data="started = " ^ string_of(icecast.is_started())) + else + http.response(protocol=protocol, code=400) + end +end + +def upstream_change(~protocol, ~data, ~headers, uri) = + args = snd(url.split(uri)) + if string.match(pattern="^http", args["url"]) then + if upstream.is_started() and upstream.url() == args["url"] then + http.response(protocol=protocol, code=200, data="No change") + else + log(args["url"]) + upstream.stop() + upstream.set_url(args["url"]) + upstream.start() + http.response(protocol=protocol, code=200, data="Updated") + end + else + http.response(protocol=protocol, code=400) + end +end + +def upstream_get(~protocol, ~data, ~headers, uri) = + ret = json() + ret.add("url", upstream.url()) + ret.add("status", upstream.status()) + ret.add("is_up", upstream.is_up()) + http.response(protocol=protocol, code=200, data=ret.stringify()) +end + +def upstream_clear(~protocol, ~data, ~headers, uri) = + upstream.stop() + upstream.set_url("") + http.response(protocol=protocol, code=200) +end + + +def playlist_change(~protocol, ~data, ~headers, uri) = + args = snd(url.split(uri)) + urls = list.filter(fun(key) -> fst(key) == "url", args) + log(string_of(urls)) + if list.is_empty(urls) then + http.response(protocol=protocol, code=400) + else + log(args["url"]) + playlist.set_queue([]) + playlist.skip() + req = request.create(args["url"]) + log("req = " ^ string_of(req)) + playlist.set_queue([req]) + http.response(protocol=protocol, code=200, data="Updated") + end +end + +def playlist_get(~protocol, ~data, ~headers, uri) = + ret = json() + ret.add("queue", list.map(fun(req) -> request.uri(req), playlist.queue())) + ret.add("remaining", playlist.remaining()) + ret.add("remaining_files", playlist.remaining_files()) + ret.add("is_active", playlist.is_active()) + metadata = playlist.last_metadata() ?? [] + ret.add("last_uri", metadata["initial_uri"]) + http.response(protocol=protocol, code=200, data=ret.stringify()) +end + +def playlist_clear(~protocol, ~data, ~headers, uri) = + playlist.set_queue([]) + playlist.skip() + http.response(protocol=protocol, code=200) +end + +harbor.http.register(port=8080, method="POST", "/api/icecast/.*", icecast_change) + +harbor.http.register(port=8080, method="POST", "/api/upstream", upstream_change) +harbor.http.register(port=8080, method="DELETE", "/api/upstream", upstream_clear) +harbor.http.register(port=8080, method="GET", "/api/upstream", upstream_get) +harbor.http.register(port=8080, method="POST", "/api/playlist", playlist_change) +harbor.http.register(port=8080, method="DELETE", "/api/playlist", playlist_clear) +harbor.http.register(port=8080, method="GET", "/api/playlist", playlist_get) + + +def startfunc() = + log("avvio...") + # icecast.stop() + () +end +startfunc() # potresti pensare che on_start(startfunc) fa la cosa giusta, invece no + diff --git a/server.liq b/server.liq new file mode 100755 index 0000000..830ce1f --- /dev/null +++ b/server.liq @@ -0,0 +1,8 @@ +#!/usr/bin/liquidsoap + +ext = input.harbor(port=8081, +"/stream.ogg" +) +backup = sine(amplitude=0.1) +audio = fallback([ext, backup], track_sensitive=false) +output(audio)