ls_lib.liq 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. def notify(m)
  2. command = "timeout --signal=KILL 45 pyponotify --media-id=#{m['schedule_table_id']} &"
  3. log(command)
  4. system(command)
  5. end
  6. def notify_queue(m)
  7. f = !dynamic_metadata_callback
  8. ignore(f(m))
  9. notify(m)
  10. end
  11. def notify_stream(m)
  12. json_str = string.replace(pattern="\n",(fun (s) -> ""), json_of(m))
  13. #if a string has a single apostrophe in it, let's comment it out by ending the string before right before it
  14. #escaping the apostrophe, and then starting a new string right after it. This is why we use 3 apostrophes.
  15. json_str = string.replace(pattern="'",(fun (s) -> "'\''"), json_str)
  16. command = "timeout --signal=KILL 45 pyponotify --webstream='#{json_str}' --media-id=#{!current_dyn_id} &"
  17. if !current_dyn_id != "-1" then
  18. log(command)
  19. system(command)
  20. end
  21. end
  22. # A function applied to each metadata chunk
  23. def append_title(m) =
  24. log("Using stream_format #{!stream_metadata_type}")
  25. if list.mem_assoc("mapped", m) then
  26. #protection against applying this function twice. It shouldn't be happening
  27. #and bug file with Liquidsoap.
  28. m
  29. else
  30. if !stream_metadata_type == 1 then
  31. [("title", "#{!show_name} - #{m['artist']} - #{m['title']}"), ("mapped", "true")]
  32. elsif !stream_metadata_type == 2 then
  33. [("title", "#{!station_name} - #{!show_name}"), ("mapped", "true")]
  34. else
  35. [("title", "#{m['artist']} - #{m['title']}"), ("mapped", "true")]
  36. end
  37. end
  38. end
  39. def crossfade_airtime(s)
  40. #duration is automatically overwritten by metadata fields passed in
  41. #with audio
  42. s = fade.in(type="log", duration=0., s)
  43. s = fade.out(type="log", duration=0., s)
  44. fader = fun (a,b) -> add(normalize=false,[b,a])
  45. cross(fader,s)
  46. end
  47. def transition(a,b) =
  48. log("transition called...")
  49. add(normalize=false,
  50. [ sequence([ blank(duration=0.01),
  51. fade.initial(duration=!default_dj_fade, b) ]),
  52. fade.final(duration=!default_dj_fade, a) ])
  53. end
  54. # we need this function for special transition case(from default to queue)
  55. # we don't want the trasition fade to have effect on the first song that would
  56. # be played siwtching out of the default(silent) source
  57. def transition_default(a,b) =
  58. log("transition called...")
  59. if !just_switched then
  60. just_switched := false
  61. add(normalize=false,
  62. [ sequence([ blank(duration=0.01),
  63. fade.initial(duration=!default_dj_fade, b) ]),
  64. fade.final(duration=!default_dj_fade, a) ])
  65. else
  66. just_switched := false
  67. b
  68. end
  69. end
  70. # Define a transition that fades out the
  71. # old source, adds a single, and then
  72. # plays the new source
  73. def to_live(old,new) =
  74. # Fade out old source
  75. old = fade.final(old)
  76. # Compose this in sequence with
  77. # the new source
  78. sequence([old,new])
  79. end
  80. def output_to(output_type, type, bitrate, host, port, pass, mount_point, url, description, genre, user, s, stream, connected, name, channels) =
  81. source = ref s
  82. def on_error(msg)
  83. connected := "false"
  84. command = "timeout --signal=KILL 45 pyponotify --error='#{msg}' --stream-id=#{stream} --time=#{!time} &"
  85. system(command)
  86. log(command)
  87. 5.
  88. end
  89. def on_connect()
  90. connected := "true"
  91. command = "timeout --signal=KILL 45 pyponotify --connect --stream-id=#{stream} --time=#{!time} &"
  92. system(command)
  93. log(command)
  94. end
  95. stereo = (channels == "stereo")
  96. if output_type == "icecast" then
  97. user_ref = ref user
  98. if user == "" then
  99. user_ref := "source"
  100. end
  101. output_mono = output.icecast(host = host,
  102. port = port,
  103. password = pass,
  104. mount = mount_point,
  105. fallible = true,
  106. url = url,
  107. description = description,
  108. name = name,
  109. genre = genre,
  110. user = !user_ref,
  111. on_error = on_error,
  112. on_connect = on_connect)
  113. output_stereo = output.icecast(host = host,
  114. port = port,
  115. password = pass,
  116. mount = mount_point,
  117. fallible = true,
  118. url = url,
  119. description = description,
  120. name = name,
  121. genre = genre,
  122. user = !user_ref,
  123. on_error = on_error,
  124. on_connect = on_connect)
  125. if type == "mp3" then
  126. %include "mp3.liq"
  127. end
  128. if type == "ogg" then
  129. %include "ogg.liq"
  130. end
  131. %ifencoder %opus
  132. if type == "opus" then
  133. %include "opus.liq"
  134. end
  135. %endif
  136. %ifencoder %aac
  137. if type == "aac" then
  138. %include "aac.liq"
  139. end
  140. %endif
  141. %ifencoder %aacplus
  142. if type == "aacplus" then
  143. %include "aacplus.liq"
  144. end
  145. %endif
  146. %ifencoder %fdkaac
  147. if type == "fdkaac" then
  148. %include "fdkaac.liq"
  149. end
  150. %endif
  151. else
  152. user_ref = ref user
  153. if user == "" then
  154. user_ref := "source"
  155. end
  156. output_mono = output.shoutcast(id = "shoutcast_stream_#{stream}",
  157. host = host,
  158. port = port,
  159. password = pass,
  160. fallible = true,
  161. url = url,
  162. genre = genre,
  163. name = description,
  164. user = !user_ref,
  165. on_error = on_error,
  166. on_connect = on_connect)
  167. output_stereo = output.shoutcast(id = "shoutcast_stream_#{stream}",
  168. host = host,
  169. port = port,
  170. password = pass,
  171. fallible = true,
  172. url = url,
  173. genre = genre,
  174. name = description,
  175. user = !user_ref,
  176. on_error = on_error,
  177. on_connect = on_connect)
  178. if type == "mp3" then
  179. %include "mp3.liq"
  180. end
  181. %ifencoder %aac
  182. if type == "aac" then
  183. %include "aac.liq"
  184. end
  185. %endif
  186. %ifencoder %aacplus
  187. if type == "aacplus" then
  188. %include "aacplus.liq"
  189. end
  190. %endif
  191. end
  192. end
  193. # Add a skip function to a source
  194. # when it does not have one
  195. # by default
  196. #def add_skip_command(s)
  197. # # A command to skip
  198. # def skip(_)
  199. # # get playing (active) queue and flush it
  200. # l = list.hd(server.execute("queue.secondary_queue"))
  201. # l = string.split(separator=" ",l)
  202. # list.iter(fun (rid) -> ignore(server.execute("queue.remove #{rid}")), l)
  203. #
  204. # l = list.hd(server.execute("queue.primary_queue"))
  205. # l = string.split(separator=" ", l)
  206. # if list.length(l) > 0 then
  207. # source.skip(s)
  208. # "Skipped"
  209. # else
  210. # "Not skipped"
  211. # end
  212. # end
  213. # # Register the command:
  214. # server.register(namespace="source",
  215. # usage="skip",
  216. # description="Skip the current song.",
  217. # "skip",fun(s) -> begin log("source.skip") skip(s) end)
  218. #end
  219. def clear_queue(s)
  220. source.skip(s)
  221. end
  222. def set_dynamic_source_id(id) =
  223. current_dyn_id := id
  224. string_of(!current_dyn_id)
  225. end
  226. def get_dynamic_source_id() =
  227. string_of(!current_dyn_id)
  228. end
  229. #cc-4633
  230. # NOTE
  231. # A few values are hardcoded and may be dependent:
  232. # - the delay in gracetime is linked with the buffer duration of input.http
  233. # (delay should be a bit less than buffer)
  234. # - crossing duration should be less than buffer length
  235. # (at best, a higher duration will be ineffective)
  236. # HTTP input with "restart" command that waits for "stop" to be effected
  237. # before "start" command is issued. Optionally it takes a new URL to play,
  238. # which makes it a convenient replacement for "url".
  239. # In the future, this may become a core feature of the HTTP input.
  240. # TODO If we stop and restart quickly several times in a row,
  241. # the data bursts accumulate and create buffer overflow.
  242. # Flushing the buffer on restart could be a good idea, but
  243. # it would also create an interruptions while the buffer is
  244. # refilling... on the other hand, this would avoid having to
  245. # fade using both cross() and switch().
  246. def input.http_restart(~id,~initial_url="http://dummy/url")
  247. source = audio_to_stereo(input.http(buffer=5.,max=15.,id=id,autostart=false,initial_url))
  248. def stopped()
  249. "stopped" == list.hd(server.execute("#{id}.status"))
  250. end
  251. server.register(namespace=id,
  252. "restart",
  253. usage="restart [url]",
  254. fun (url) -> begin
  255. if url != "" then
  256. log(string_of(server.execute("#{id}.url #{url}")))
  257. end
  258. log(string_of(server.execute("#{id}.stop")))
  259. add_timeout(0.5,
  260. { if stopped() then
  261. log(string_of(server.execute("#{id}.start"))) ;
  262. (-1.)
  263. else 0.5 end})
  264. "OK"
  265. end)
  266. # Dummy output should be useless if HTTP stream is meant
  267. # to be listened to immediately. Otherwise, apply it.
  268. #
  269. # output.dummy(fallible=true,source)
  270. source
  271. end
  272. # Transitions between URL changes in HTTP streams.
  273. def cross_http(~debug=true,~http_input_id,source)
  274. id = http_input_id
  275. last_url = ref ""
  276. change = ref false
  277. def on_m(m)
  278. notify_stream(m)
  279. changed = m["source_url"] != !last_url
  280. log("URL now #{m['source_url']} (change: #{changed})")
  281. if changed then
  282. if !last_url != "" then change := true end
  283. last_url := m["source_url"]
  284. end
  285. end
  286. # We use both metadata and status to know about the current URL.
  287. # Using only metadata may be more precise is crazy corner cases,
  288. # but it's also asking too much: the metadata may not pass through
  289. # before the crosser is instantiated.
  290. # Using only status in crosser misses some info, eg. on first URL.
  291. source = on_metadata(on_m,source)
  292. cross_d = 3.
  293. def crosser(a,b)
  294. url = list.hd(server.execute('#{id}.url'))
  295. status = list.hd(server.execute('#{id}.status'))
  296. on_m([("source_url",url)])
  297. if debug then
  298. log("New track inside HTTP stream")
  299. log(" status: #{status}")
  300. log(" need to cross: #{!change}")
  301. log(" remaining #{source.remaining(a)} sec before, \
  302. #{source.remaining(b)} sec after")
  303. end
  304. if !change then
  305. change := false
  306. # In principle one should avoid crossing on a live stream
  307. # it'd be okay to do it here (eg. use add instead of sequence)
  308. # because it's only once per URL, but be cautious.
  309. sequence([fade.out(duration=cross_d,a),fade.in(b)])
  310. else
  311. # This is done on tracks inside a single stream.
  312. # Do NOT cross here or you'll gradually empty the buffer!
  313. sequence([a,b])
  314. end
  315. end
  316. # Setting conservative=true would mess with the delayed switch below
  317. cross(duration=cross_d,conservative=false,crosser,source)
  318. end
  319. # Custom fallback between http and default source with fading of
  320. # beginning and end of HTTP stream.
  321. # It does not take potential URL changes into account, as long as
  322. # they do not interrupt streaming (thanks to the HTTP buffer).
  323. def http_fallback(~http_input_id,~http,~default)
  324. id = http_input_id
  325. # We use a custom switching predicate to trigger switching (and thus,
  326. # transitions) before the end of a track (rather, end of HTTP stream).
  327. # It is complexified because we don't want to trigger switching when
  328. # HTTP disconnects for just an instant, when changing URL: for that
  329. # we use gracetime below.
  330. def gracetime(~delay=3.,f)
  331. last_true = ref 0.
  332. { if f() then
  333. last_true := gettimeofday()
  334. true
  335. else
  336. gettimeofday() < !last_true+delay
  337. end }
  338. end
  339. def connected()
  340. status = list.hd(server.execute("#{id}.status"))
  341. not(list.mem(status,["polling","stopped"]))
  342. end
  343. connected = gracetime(connected)
  344. def to_live(a,b) =
  345. log("TRANSITION to live")
  346. add(normalize=false,
  347. [fade.initial(b),fade.final(a)])
  348. end
  349. def to_static(a,b) =
  350. log("TRANSITION to static")
  351. sequence([fade.out(a),fade.initial(b)])
  352. end
  353. switch(
  354. track_sensitive=false,
  355. transitions=[to_live,to_static],
  356. [(# make sure it is connected, and not buffering
  357. {connected() and source.is_ready(http) and !webstream_enabled}, http),
  358. ({true},default)])
  359. end