#!/bin/bash # $HeadURL$ $LastChangedRevision$ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff TIMEOUT=5 # Other globals main() { local MY_ARGS local PROGNAME # Defaults for options MODE=unset # Process options special_opts_handler() { case $1 in --server|--submit|--stop) MODE=${1#--} ;; *) return 1 ;; esac } miniade_process_options --help-handler=usage --special-opts-handler=special_opts_handler MY_ARGS "$@" && set -- "${MY_ARGS[@]}" # Process arguments # (delegated) # Sanity checks and derivations [ $MODE != unset ] || usage [ "X$LOGNAME" != X ] || miniade_error "LOGNAME: not set" miniade_get_progname PROGNAME LOCKFILE=/tmp/$PROGNAME-$LOGNAME.lock SOCK=/tmp/$PROGNAME-$LOGNAME.sock # Guts ${MODE//-/_} "$@" } usage() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] { --server | --submit | --stop }" exit 0 } server() { local PROG BBC_OPTS NEW_BBC_OPTS RADIO_FM_OPTS STANDARD_OPTS # Process arguments [ $# = 0 ] || usage # Sanity checks and derivations miniade_validate_command mpv socat # Guts miniade_debug 10 "server: locking ..." if ! miniade_lock $LOCKFILE; then miniade_debug 10 "server: server is already running; this is fine" return 0 fi miniade_debug 10 "server: launching mpv server ..." # # That didn't work but we'll keep the options as they may help with # future problems. # # Other suggestions at the same page were to add reconnect_at_eof, # but I think that should be reconnect_on_eof. Anyway, it didn't # work. Again, we'll keep it. STANDARD_OPTS=( --no-terminal --really-quiet --idle --input-ipc-server=$SOCK --audio-device=pulse # '--fs' is so rnd-xmas-film can use the mpv helper --fs ) # https://github.com/mpv-player/mpv/issues/5793#issuecomment-1902197279 # suggested the following options to prevent disconnect-without-recovery # issues for RĂ¡dio FM. They did not work so we still define them but we # don't use them in the mpv call itself. RADIO_FM_OPTS=( --stream-lavf-o-append=reconnect_on_http_error=4xx,5xx --stream-lavf-o-append=reconnect_delay_max=30 --stream-lavf-o-append=reconnect_streamed=yes --stream-lavf-o-append=reconnect_on_network_error=yes ) # https://github.com/mpv-player/mpv/issues/13428 suggested the following # options to prevent disconnect-without-recovery issues for BBC Radio 4. # They worked! BBC_OPTS=( --loop-playlist=force ) # Comments on https://github.com/mpv-player/mpv/issues/13428 suggest this # is enough, and it kept playing for two weeks, before I killed it. NEW_BBC_OPTS=( --demuxer-lavf-o=max_reload=1000 ) # Note that only NEW_BBC_OPTS is used currently. WRA_LAUNCHED_THIS=1 mpv "${STANDARD_OPTS[@]}" "${NEW_BBC_OPTS[@]}" & # Since *we* locked above, then *we* can rewrite the lock file :-) But # we need to do it atomically, which means we need to use mv. See # https://unix.stackexchange.com/questions/24395/. echo $! > $LOCKFILE.$$ mv $LOCKFILE.$$ $LOCKFILE # Let it settle. sleep 5 } submit() { local URL # Process arguments [ $# = 1 ] || usage URL=$1 # Sanity checks and derivations miniade_lock_is_fresh $LOCKFILE || miniade_error "mpv server seems not to be running" # Guts echo "{ \"command\": [ \"loadfile\", \"$URL\", \"replace\" ] }" | socat -u - $SOCK } stop() { # Process arguments [ $# = 0 ] || usage # Sanity checks and derivations # No need to send stop if it's not running miniade_lock_is_fresh $LOCKFILE || return 0 # Guts echo '{ "command": [ "stop" ] }' | socat -u - $SOCK } main "$@"