#!/usr/bin/liquidsoap

#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



icecast_vorbis_metadata = false
inputs = ref []


# Load settings from ini file
%include "settings.liq"

# Include some functions
%include "library.liq"

#####################################
#              EVENTS               #
#####################################

# Called when some new metadata info is available
def on_metadata_notification(meta) =
    filename = meta["filename"]
    track_duration = request.duration(filename)
    json_data = json_of(meta, compact=true)
    json_data = url.encode(json_data)
    json_data = '{ "action": "on_metadata", "meta": "#{json_data}", "track_duration": "#{track_duration}" }'
    # There's currently an issue with Liquidsoap http.post requests (should be gone with Liquidsoap 2):
    #headers = [("Content-Type","application/json; charset=utf-8")]
    #ignore(http.post(headers=headers, data="#{json_data}", "localhost:1337"))
    ignore(system("curl -X POST -H 'Content-Type: application/json' --data '#{json_data}' #{engine_control}"))
end

#####################################
#              INPUTS               #
#####################################

# Enable queue sources
%include "in_queue.liq"

# Enable fallback sources
%include "in_fallback.liq"

# Enable stream overtakes
%include "in_stream.liq"

# Enabled line in from soundcard
%include "in_soundcard.liq"


#####################################
#             ROUTING               #
#####################################


mixer = mix(id="mixer",
        list.append(
            [
                input_filesystem_0,
                input_filesystem_1,
                input_http_0,
                input_http_1,
                input_https_0,
                input_https_1
            ],
            !inputs
        )
    )

stripped_stream = strip_blank(
        id="strip_blank",
        track_sensitive=false,
        max_blank=fallback_max_blank,
        min_noise=fallback_min_noise,
        threshold=fallback_threshold,
        mixer
    )

output_source = attach_fallback_source(stripped_stream)

#####################################
#             OUTPUTS               #
#####################################

# create soundcard output
%include "out_soundcard.liq"

# stream output
%include "out_stream.liq"

# enable socket functions
%include "serverfunctions.liq"