initial commit, experiments

This commit is contained in:
boyska 2024-10-28 11:51:25 +01:00
commit 424e0a520e
2 changed files with 130 additions and 0 deletions

122
sapo.liq Executable file
View file

@ -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

8
server.liq Executable file
View file

@ -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)