-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathradio.liq
More file actions
executable file
·333 lines (286 loc) · 11.1 KB
/
Copy pathradio.liq
File metadata and controls
executable file
·333 lines (286 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/local/bin/liquidsoap
# %include "tunein.liq"
#set("sandbox.tool","disabled")
set("log.file",true)
set("log.file.path","/var/log/liquidsoap.log")
set("log.stdout",true)
set("log.level",3)
set("server.telnet",true)
set("server.socket",true)
set("server.socket.path","/home/liquidsoap/tracks/liquidsoap.sock")
set("harbor.bind_addrs",["0.0.0.0"])
# needs these env variables
radio_name = getenv("RADIO_NAME")
icecast_host = "icecast"
icecast_port = "8000"
crossfade_enabled = getenv("CROSSFADE")
# tunein_partner_id = getenv("TUNEIN_PARTNER_ID")
# tunein_partner_key = getenv("TUNEIN_PARTNER_KEY")
# tunein_station_id = getenv("TUNEIN_STATION_ID")
# tunein_metadata_updates_enabled = getenv("TUNEIN_METADATA_UPDATES_ENABLED")
# liq_secret = getenv("LIQ_SECRET")
#
def icy_update(v) =
# Parse the argument
l = string.split(separator=",",v)
def split(l,v) =
v = string.split(separator="=",v)
if list.length(v) >= 2 then
list.append(l,[(list.nth(v,0,default=""),list.nth(v,1,default=""))])
else
l
end
end
meta = list.fold(split,[],l)
# Update metadata
icy.update_metadata(mount="#{radio_name}.ogg",password="hackme",
host=icecast_host,meta)
icy.update_metadata(mount="#{radio_name}.mp3",password="hackme",
host=icecast_host,meta)
"Done !"
end
# def clear_queue() =
# end
server.register("update",namespace="metadata",
description="Update metadata",
usage="update title=foo,album=bar,..",
icy_update)
# refs
title_prefix = ref("")
current_dj = ref("")
last_dj_disconnected = ref("")
current_dj_connected_at = ref(0.0)
last_dj_disconnected_at = ref(0.0)
current_dump_filename = ref("")
stop_dump_f = ref(fun () -> ())
live_dj = ref(empty())
live_video_audio = ref(empty())
live_video = ref(empty())
vj_video = ref(empty())
def dj_dummy_started() =
log("dj dummy on_start")
s = output.file(%mp3, "/home/liquidsoap/recordings/#{radio_name}-#{!current_dump_filename}.mp3", !live_dj, fallible=true, append=true)
# output.dummy(fallible=true, on_start=dummy_started, on_stop=dummy_stopped, s)
stop_dump_f := fun () -> source.shutdown(s)
end
def dj_dummy_stopped() =
log("dj dummy on_stop")
f = !stop_dump_f
f ()
end
def get_user(user,password) =
if user == "source" then
x = string.split(separator=';',password)
list.nth(x,0,default="")
else
user
end
end
def get_password(user,password) =
if user == "source" then
x = string.split(separator=';',password)
list.nth(x,1,default="")
else
password
end
end
#auth function
def dj_auth(args) =
log("dj auth")
u = get_user(args.user, args.password)
p = get_password(args.user, args.password)
ret = process.read.lines("./dj_auth.sh '#{u}' '#{p}' '#{radio_name}'")
#ret has now the value of the live client (dj1,dj2, or djx), or "ERROR"/"unknown"
ret = list.hd(default="",ret)
#return true to let the client transmit data, or false to tell harbor to decline
if ret == "true" then
title_prefix := "LIVE -- #{u}"
current_dj := "#{u}"
current_dj_connected_at := time()
log("dj auth succeeded")
log("#{current_dj}")
log("#{current_dj_connected_at}")
true
else
false
end
end
def my_request_function () =
lines = process.read.lines("./next_song.sh")
result = list.hd(default="", lines)
log("result: #{result}")
log("lines: #{lines}")
json = of_json(default=[("error","fail")], result)
log("json: #{json}")
track = json["track"]
cue_in = int_of_string(json["cue_in"])
cue_out = int_of_string(json["cue_out"])
log("cue_out: #{cue_out}")
fade_in = int_of_string(json["fade_in"])
fade_out = int_of_string(json["fade_out"])
if cue_out == 0 then
# don't set cue out if its 0
annotate_line = "annotate:liq_fade_in=#{fade_in},liq_fade_out=#{fade_out},liq_cue_in=#{cue_in}:#{track}"
log("annotate_line: #{annotate_line}")
request.create(annotate_line)
else
annotate_line = "annotate:liq_fade_in=#{fade_in},liq_fade_out=#{fade_out},liq_cue_in=#{cue_in},liq_cue_out=#{cue_out}:#{track}"
log("annotate_line: #{annotate_line}")
request.create(annotate_line)
end
end
def format_time(~sec,~min,~hour,~mday,~mon,~year,
~wday,~yday,~isdst) =
"#{1900+year}-#{mon+1}-#{mday}-#{hour}:#{min}:#{sec}"
end
def on_connect(headers) =
log("on_connect")
list.iter(fun(header)-> log("#{fst(header)}: #{snd(header)}"), headers)
# if they disconnected over 60 seconds ago, use new filename
log("current_dj_connected_at: #{!current_dj_connected_at}")
log("last_dj_disconnected_at: #{!last_dj_disconnected_at}")
if (!last_dj_disconnected_at == 0.0) then
log("*****************************************CREATING NEW DUMP, last_dj_disconnected_at is null***************************************************")
current_dump_filename := "#{!current_dj}-#{time.local(!current_dj_connected_at)}"
log("new dump filename (first dump):")
log(!current_dump_filename);
log("sending live notification")
ret = process.read.lines("./live_notification.sh 'LIVE - #{!current_dj}'")
ret = list.hd(default="",ret)
log(ret)
elsif (((!current_dj_connected_at - !last_dj_disconnected_at) > 60.0) or (!current_dj != !last_dj_disconnected)) then
log("*****************************************CREATING NEW DUMP***************************************************")
current_dump_filename := "#{!current_dj}-#{time.local(!current_dj_connected_at)}"
log("new dump filename:")
log(!current_dump_filename);
log("sending live notification")
ret = process.read.lines("./live_notification.sh 'LIVE - #{!current_dj}'")
ret = list.hd(default="",ret)
log(ret)
else
log("*****************************************APPENDING TO PREVIOUS DUMP***************************************************")
end
# else append to the old file (dont change the current_dump_filename)
end
def on_disconnect() =
log("disconnected")
last_dj_disconnected_at := gettimeofday()
# shutdown source
title_prefix := ""
last_dj_disconnected := !current_dj
current_dj := ""
end
backup_playlist = cue_cut(audio_to_stereo(request.dynamic(id="requests", timeout=60.0, my_request_function)))
if crossfade_enabled == "true" then
output.dummy(fallible=true,crossfade(backup_playlist))
else
output.dummy(fallible=true,backup_playlist)
end
live_dj := audio_to_stereo(input.harbor("#{radio_name}",id="live_dj",port=9000,auth=dj_auth,on_connect=on_connect,on_disconnect=on_disconnect,logfile="/tmp/liquidsoap_harbor.log",buffer=15.0,max=40.0))
def new_meta(m) =
if "#{title_prefix}" == "" then
title = m["title"]
[("title","#{title}")]
else
[("title","#{!title_prefix}")]
end
end
def pub_metadata(m) =
log("on_track");
title = m["title"]
ret = process.read.lines("./pub_metadata.sh '#{title}'")
ret = list.hd(default="",ret)
log(ret)
#url = "https://#{radio_name}.streampusher.com/publish_metadata.json"
#log("url: #{url}")
# headers = [("Content-Type","application/json"), ("Accept", "application/json"), ("liq-secret", liq_secret)]
# let (status, headers, data) = https.post(headers=headers,data="{\"metadata\": \"#{title}\"}", url)
# log("status: #{status}");
# log("headers: #{headers}");
# log("data: #{data}");
# let (protocol, code, desc) = status;
# log("protocol: #{protocol}");
# log("response code: #{code}");
# log("desc: #{desc}");
end
live_video := input.ffmpeg(id="main_video", fallible=true, buffer=60.0, "https://viz.streampusher.com:16666/hls/datafruits.m3u8")
vj_video := input.ffmpeg(id="vj_video", fallible=true, buffer=60.0, "https://viz.streampusher.com:16666/vj/datafruits.m3u8")
live_video_audio := audio_to_stereo(drop_video(!live_video))
live_dj := map_metadata(new_meta, !live_dj,update=false)
# live_audio = fallback(id="live_audio_fallback", track_sensitive=false, [!live_video_audio, !live_dj])
# live_audio = pipe(process='./stereo_tool_cmd_64 - - -s ./stereotool.sts', buffer=10.0, live_audio)
output.dummy(fallible=true, on_start=dj_dummy_started, on_stop=dj_dummy_stopped, !live_dj)
source = fallback(id="fallback",track_sensitive=false,
[!live_video_audio, !live_dj, mksafe(backup_playlist)])
single_video = noise()
video_source = fallback(id="video_fallback",track_sensitive=false,
[!live_video,
mux_audio(drop_audio(!vj_video), audio=source),
mksafe(mux_audio(single_video, audio=source))])
#source = if tunein_metadata_updates_enabled == "true" then on_track(tunein.submit(partnerid=tunein_partner_id,partnerkey=tunein_partner_key,stationid=tunein_station_id), source) else source end
source.on_track(pub_metadata)
source = server.insert_metadata(id="fallback", source)
clock.assign_new(id="vorbis_icecast", [output.icecast(%vorbis,id="icecast",
mount="#{radio_name}.ogg",
host=icecast_host, port=int_of_string(icecast_port), password="hackme",
icy_metadata="true",description="",
url="", encoding="UTF-8",
mksafe(buffer(source)))])
clock.assign_new(id="mp3_icecast", [output.icecast(%mp3,id="icecast",
mount="#{radio_name}.mp3",
host=icecast_host, port=int_of_string(icecast_port), password="hackme",
icy_metadata="true",description="",
url="", encoding="UTF-8",
mksafe(buffer(source)))])
aac_lofi = %ffmpeg(format="mpegts",
%audio(
codec="aac",
channels=2,
ar=44100
),
%video(
codec="libx264",
b="5M"
)
)
aac_midfi = %ffmpeg(format="mpegts",
%audio(
codec="aac",
channels=2,
ar=44100,
b="96k"
),
%video(
codec="libx264",
b="5M"
)
)
aac_hifi = %ffmpeg(format="mpegts",
%audio(
codec="aac",
channels=2,
ar=44100,
b="192k"
),
%video(
codec="libx264",
b="5M"
)
)
streams = [("aac_lofi",aac_lofi),
("aac_midfi", aac_midfi),
("aac_hifi", aac_hifi)]
def segment_name(~position,~extname,stream_name) =
timestamp = int_of_float(time())
duration = 2
"#{stream_name}_#{duration}_#{timestamp}_#{position}.#{extname}"
end
output.file.hls(playlist="#{radio_name}.m3u8",
segment_duration=2.0,
segments=5,
segments_overhead=5,
segment_name=segment_name,
persist_at="/home/liquidsoap/state.config",
"/home/liquidsoap/hls",
streams,
video_source)