soapdish/scripts/streaming/main/main-stream_live_alsa

113 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# SHOW: STREAM LIVE
# Streams show live from input/ALSA
# Includes and Declarations (global variables, formatting, and functions)
source "${APPLICATION_ROOT}/scripts/system/system-functions"
source "${APPLICATION_ROOT}/scripts/system/system-declarations"
# Get active config name, construct path to streamable config file (for validation)
CURRENTLY_ACTIVE_CONFIG_NAME=$(<"$CONFIG_FILE_IDENTIFIER_PATH")
STREAMABLE_CONFIG_FILE_PATH="$CONFIG_DIRECTORY/config-$CURRENTLY_ACTIVE_CONFIG_NAME.json"
# Check if config file exists
if [[ -f "$STREAMABLE_CONFIG_FILE_PATH" ]];
# If exists
then
# Parse JSON config file for each crucial streaming variable
v1='main_host'
MAIN_HOST=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v1//p" $STREAMABLE_CONFIG_FILE_PATH))
v2='main_port'
MAIN_PORT=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v2//p" $STREAMABLE_CONFIG_FILE_PATH))
v3='main_user'
MAIN_USER=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v3//p" $STREAMABLE_CONFIG_FILE_PATH))
v4='main_password'
MAIN_PASSWORD=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v4//p" $STREAMABLE_CONFIG_FILE_PATH))
v5='main_mount'
MAIN_MOUNT=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v5//p" $STREAMABLE_CONFIG_FILE_PATH))
v6='playlist_url'
PLAYLIST_URL=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v6//p" $STREAMABLE_CONFIG_FILE_PATH))
v7='randomize_playlist_files'
RANDOMIZE_PLAYLIST_FILES=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v7//p" $STREAMABLE_CONFIG_FILE_PATH))
v8='hardware_audio_frame_size'
HARDWARE_AUDIO_FRAME_SIZE=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v8//p" $STREAMABLE_CONFIG_FILE_PATH))
# Parse JSON config file for each metadata streaming variable
v9='main_metadata_artist'
MAIN_METADATA_ARTIST=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$v9//p" $STREAMABLE_CONFIG_FILE_PATH))
# MAIN_METADATA_ARTIST="${MAIN_METADATA_ARTIST// /\+}"
vA='main_metadata_title'
MAIN_METADATA_TITLE=$(sed -e s/'[ \t]*\"\"\:\"'// -e s/'".*'// <<< $(sed -n "s/$vA//p" $STREAMABLE_CONFIG_FILE_PATH))
# MAIN_METADATA_TITLE="${MAIN_METADATA_TITLE// /\+}"
# Ensure all configuration files are not blank
if [ -z "${MAIN_HOST}" ] || [ -z "${MAIN_PORT}" ] || [ -z "${MAIN_USER}" ] || [ -z "${MAIN_PASSWORD}" ] || [ -z "${MAIN_MOUNT}" ] || [ -z "${PLAYLIST_URL}" ] || [ -z "${RANDOMIZE_PLAYLIST_FILES}" ] || [ -z "${HARDWARE_AUDIO_FRAME_SIZE}" ]
then
# Fault if config variable is blank
error_message "[L] Stream ALSA (to Main): Streaming failed. Please check your configuration settings.";
echo ""
else
# Notice of Execution
confirmation_message "${COLOR_GREEN}[L] Stream ALSA (to Main):${COLOR_DEFAULT}";
# Encode and Submit Metadata Change (Hack: two requests to get changes to take)
CONVERTED_MAIN_METADATA_TITLE=$(url_encode_string "$MAIN_METADATA_TITLE")
CONVERTED_MAIN_METADATA_ARTIST=$(url_encode_string "$MAIN_METADATA_ARTIST")
curl "http://$MAIN_USER:$MAIN_PASSWORD@$MAIN_HOST:$MAIN_PORT/admin/metadata?mode=updinfo&mount=/&title='$CONVERTED_MAIN_METADATA_TITLE'&artist='$CONVERTED_MAIN_METADATA_ARTIST'"
sleep 5
curl "http://$MAIN_USER:$MAIN_PASSWORD@$MAIN_HOST:$MAIN_PORT/admin/metadata?mode=updinfo&mount=/&title='$CONVERTED_MAIN_METADATA_TITLE'&artist='$CONVERTED_MAIN_METADATA_ARTIST'"
# Liquidsoap Log
echo -e "\n"
echo -e "\n"
echo -e "${COLOR_LIGHT_MAGENTA}${FONT_BOLD}[STARTING LIQUIDSOAP]${FONT_DEFAULT}${COLOR_DEFAULT}\n"
echo -e "${COLOR_MAGENTA}"
# Launch Liquidsoap using variables
liquidsoap '
set("frame.audio.size",'$HARDWARE_AUDIO_FRAME_SIZE');
stream_master_source = output.icecast(
%mp3(bitrate=192,samplerate=44100),
host="'$MAIN_HOST'",
port='$MAIN_PORT',
user="'$MAIN_USER'",
password="'$MAIN_PASSWORD'",
mount="'$MAIN_MOUNT'",
input.alsa(bufferize = true)
)
stream_master_source
'
printf ${COLOR_DEFAULT}
printf "${COLOR_LIGHT_MAGENTA}${FONT_BOLD}[EXITING LIQUIDSOAP]${FONT_DEFAULT}${COLOR_DEFAULT}\n"
printf "\n"
printf "\n"
printf "\n"
fi
# If no config file exists
else
error_message "[L] Stream ALSA (to Main): No config file specified. To stream, please assign a config."
echo ""
fi