From 6d111eadfd9130b821170a32ad20596d9a02aa5a Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Fri, 24 Jun 2022 10:23:14 +0200
Subject: [PATCH] Refact: Liquidsoap 2.1 compatiblity

---
 src/library.liq              | 40 ++++++----------------
 src/outgoing_streams/mp3.liq | 66 ++++++++++++++++++++++++------------
 src/outgoing_streams/ogg.liq | 54 +++++++++++++++++++----------
 src/readini.liq              |  9 ++++-
 4 files changed, 99 insertions(+), 70 deletions(-)

diff --git a/src/library.liq b/src/library.liq
index e9a98aa..ea93e93 100644
--- a/src/library.liq
+++ b/src/library.liq
@@ -72,47 +72,29 @@ def stream_to_icecast(id, encoding, bitrate, host, port, pass, mount_point, url,
         log("Successfully connected to stream_#{streamnumber}")
     end
 
-    stereo = (int_of_string(channels) >= 2)
-
     user_ref = ref (user)
     if user == "" then
         user_ref := "source"
     end
 
-    # Liquidsoap cannot handle one output definition for mono and stereo
-    output_icecast_mono   = output.icecast(id = id, host = host, port = port, password = pass, mount = mount_point, fallible = true, url = url, description = description, name = name, genre = genre, user = !user_ref, on_error = on_error, on_connect = on_connect, icy_metadata = "true")
-    output_icecast_stereo = output.icecast(id = id, host = host, port = port, password = pass, mount = mount_point, fallible = true, url = url, description = description, name = name, genre = genre, user = !user_ref, on_error = on_error, on_connect = on_connect, icy_metadata = "true")
-
-#    %ifencoder %aac
-#    if encoding == "aac" then
-#        log("ENABLING AAC to ICECAST")
-#        %include "outgoing_streams/aac.liq"
-#    end
-#    %endif
-#
-#    %ifencoder %flac
-#    if encoding == "flac" then
-#        log("ENABLING FLAC to ICECAST")
-#        %include "outgoing_streams/flac.liq"
-#    end
-#    %endif
-
+    # TODO Refactor all outgoing stream formats this way
+    let stereo = (int_of_string(channels) >= 2)
+    let format = %mp3(bitrate = 128, stereo = true)
     if encoding == "mp3" then
-        log("ENABLING MP3 to ICECAST")
         %include "outgoing_streams/mp3.liq"
     end
-
     if encoding == "ogg" then
-        log("ENABLING OGG to ICECAST")
         %include "outgoing_streams/ogg.liq"
     end
 
-#    %ifencoder %opus
-#    if encoding == "opus" then
-#        log("ENABLING OPUS to ICECAST")
-#        %include "outgoing_streams/opus.liq"
-#    end
-#    %endif
+    log("Icecast output format: #{encoding} #{bitrate}")
+    # Liquidsoap cannot handle one output definition for mono and stereo
+    # FIXME should be working since Liquidsoap 2
+    output_icecast_mono   = output.icecast(id = id, host = host, port = port, password = pass, mount = mount_point, fallible = true, url = url, description = description, name = name, genre = genre, user = !user_ref, on_error = on_error, on_connect = on_connect, icy_metadata = "true", format, !source)
+    output_icecast_stereo = output.icecast(id = id, host = host, port = port, password = pass, mount = mount_point, fallible = true, url = url, description = description, name = name, genre = genre, user = !user_ref, on_error = on_error, on_connect = on_connect, icy_metadata = "true", format, !source)
+    ignore(output_icecast_mono)
+    ignore(output_icecast_stereo)
+
 end
 
 ###########
diff --git a/src/outgoing_streams/mp3.liq b/src/outgoing_streams/mp3.liq
index 70218fe..8f8ce70 100644
--- a/src/outgoing_streams/mp3.liq
+++ b/src/outgoing_streams/mp3.liq
@@ -19,69 +19,91 @@
 
 if bitrate == 24 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 24, stereo = true), !source))
+        format = %mp3(bitrate = 24, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 24, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 24, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 32 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 32, stereo = true), !source))
+        format = %mp3(bitrate = 32, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 32, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 32, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 48 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 48, stereo = true), !source))
+        format = %mp3(bitrate = 48, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 48, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 48, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 64 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 64, stereo = true), !source))
+        format = %mp3(bitrate = 64, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 64, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 64, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 96 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 96, stereo = true), !source))
+        format = %mp3(bitrate = 96, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 96, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 96, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 128 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 128, stereo = true), !source))
+        format = %mp3(bitrate = 128, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 128, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 128, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 160 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 160, stereo = true), !source))
+        format = %mp3(bitrate = 160, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 160, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 160, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 192 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 192, stereo = true), !source))
+        format = %mp3(bitrate = 192, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 192, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 192, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 224 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 224, stereo = true), !source))
+        format = %mp3(bitrate = 224, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 224, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 224, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 256 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 256, stereo = true), !source))
+        format = %mp3(bitrate = 256, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 256, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 256, stereo = false)
+        ignore(format)
     end
 elsif bitrate == 320 then
     if stereo then
-        ignore(output_icecast_stereo(%mp3(bitrate = 320, stereo = true), !source))
+        format = %mp3(bitrate = 320, stereo = true)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%mp3(bitrate = 320, stereo = false), mean(!source)))
+        format = %mp3(bitrate = 320, stereo = false)
+        ignore(format)
     end
 end
 
diff --git a/src/outgoing_streams/ogg.liq b/src/outgoing_streams/ogg.liq
index a8a6e62..7c790d1 100644
--- a/src/outgoing_streams/ogg.liq
+++ b/src/outgoing_streams/ogg.liq
@@ -23,57 +23,75 @@ end
 
 if bitrate == 24 or bitrate == 32 or bitrate == 48 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=-0.1, channels = 2), !source))
+        format = %vorbis(quality=-0.1, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=-0.1, channels = 1), mean(!source)))
+        format = %vorbis(quality=-0.1, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 64 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0, channels = 2), !source))
+        format = %vorbis(quality=0, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0, channels = 1), mean(!source)))
+        format = %vorbis(quality=0, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 96 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.2, channels = 2), !source))
+        format = %vorbis(quality=0.2, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.2, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.2, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 128 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.4, channels = 2), !source))
+        format = %vorbis(quality=0.4, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.4, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.4, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 160 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.5, channels = 2), !source))
+        format = %vorbis(quality=0.5, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.5, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.5, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 192 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.6, channels = 2), !source))
+        format = %vorbis(quality=0.6, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.6, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.6, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 224 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.7, channels = 2), !source))
+        format = %vorbis(quality=0.7, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.7, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.7, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 256 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.8, channels = 2), !source))
+        format = %vorbis(quality=0.8, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.8, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.8, channels = 1)
+        ignore(format)
     end
 elsif bitrate == 320 then
     if stereo then
-        ignore(output_icecast_stereo(%vorbis(quality=0.9, channels = 2), !source))
+        format = %vorbis(quality=0.9, channels = 2)
+        ignore(format)
     else
-        ignore(output_icecast_mono(%vorbis(quality=0.9, channels = 1), mean(!source)))
+        format = %vorbis(quality=0.9, channels = 1)
+        ignore(format)
     end
 end
 
diff --git a/src/readini.liq b/src/readini.liq
index 9671aa5..64a3b2d 100644
--- a/src/readini.liq
+++ b/src/readini.liq
@@ -28,7 +28,14 @@ def read_ini(file)
   # read ini file
   settings_file_content = process.read.lines("cat "^file )
   # one entry stored as ["setting_name", "setting"]
-  settings_map = list.map(string.split(separator="="), settings_file_content)
+  # settings_map = list.map(string.split(separator="="), settings_file_content)
+
+  def split_line(line)
+    string.split(line, separator="=")
+  end
+  settings_map = list.map(split_line, settings_file_content)
+
+  # settings_map = list.map(string.split("#{settings_file_content}", separator="="))
 
   def filter_pair(setting_pair) =
     if !debug then
-- 
GitLab