distro specific fixes
[distro-setup] / ffs
diff --git a/ffs b/ffs
index d6dc01ff07018546a282f57a782bd52aa6168ccb..728b77d4e67288235f7ba96a3dc60f6245269f4c 100755 (executable)
--- a/ffs
+++ b/ffs
 
 # ffs = ffmpeg stream
 
-# todo: learn to start working in one corner of the screen.
-
-# potential improvement: it might be nice that we could have a tall terminal bug only use
+# potential improvement: it might be nice that we could have a tall terminal but only use
 # the top half for a 1080p stream, this is how:
 # https://superuser.com/questions/1106674/how-to-add-blank-lines-above-the-bottom-in-terminal
-#
 
 
 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
@@ -37,12 +34,16 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${P
 
 usage() {
   cat <<EOF
-Usage: ${0##*/} [OPTIONS] [sysops|tech|staff]
-3 mountpoints: fsf-sysops (default, public), fsf (all staff), fsf-tech (tech team)
+Usage: ${0##*/} [OPTIONS] [sysops|tech|staff|test]
+
+arg is icecast mountpoint suffix, except staff removes suffix.
 
 -d    debug.
--f    Stream full screen even high resolution.
--t    Stream tall half screen
+-r RESOLUTION_TYPE
+   full: full screen even high resolution.
+   tall (default): half screen.
+   quarter: self evident
+-l   loud/listen. Start unmuted. Usually for testing.
 -u    Undelayed. Removes 5 second video delay, and about 4 second audio delay.
 -w    do not launch watch of stream
 
@@ -68,9 +69,10 @@ debug=false
 delay=true
 loglevel=fatal
 watch=true
+volume=0
 fullscreen=false
-tall=false
-temp=$(getopt -l help hdftuw "$@") || usage 1
+tall=true
+temp=$(getopt -l help hdlr:uw "$@") || usage 1
 eval set -- "$temp"
 while true; do
   case $1 in
@@ -80,13 +82,25 @@ while true; do
       loglevel=info
       ffp_args+=(-d)
       ;;
-    -f)
-      fullscreen=true
-      tall=false
+    -l)
+      volume=1
       ;;
-    -t)
-      fullscreen=false
-      tall=true
+    -r)
+      case $2 in
+        tall)
+          fullscreen=false
+          tall=true
+          ;;
+        quarter)
+          fullscreen=false
+          tall=false
+          ;;
+        full)
+          fullscreen=true
+          tall=false
+          ;;
+      esac
+      shift
       ;;
     -w)
       watch=false
@@ -102,17 +116,24 @@ while true; do
 done
 
 mount_suffix=-sysops
-case $1 in
-  sysops|tech)
-    mount_suffix=-$1
-    ;;&
-  tech)
-    delay=false
-    ;;
-  staff)
-    mount_suffix=
-    ;;
-esac
+if [[ $1 ]]; then
+  case $1 in
+    sysops|tech)
+      mount_suffix=-$1
+      ;;&
+    tech)
+      delay=false
+      ;;
+    staff)
+      mount_suffix=
+      ;;
+    *)
+      echo "error: unexpected \$1: $1" >&2
+      exit 1
+      ;;
+  esac
+  ffp_args+=($1)
+fi
 
 if $delay; then
   # 2500 gets us around a 4 second delay, up from 1.5s.
@@ -122,12 +143,25 @@ fi
 
 ##### end command line parsing ########
 
-host=live.iankelling.org:8000
-if ip n show 10.2.0.1 | grep . &>/dev/null && \
-    [[ $(dig +timeout=1 +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]]; then
+host=live.iankelling.org:8443
+live_host=$(dig +timeout=1 +short @iankelling.org live.iankelling.org)
+vps_host=$(dig +timeout=1 +short iankelling.org)
+if [[ $live_host != "$vps_host" ]] &&  ip n show 10.2.0.1 | grep . &>/dev/null && \
+     [[ $(dig +timeout=1 +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]]; then
   host=127.0.0.1:8000
+  if ! pgrep '^icecast2$' >/dev/null; then
+    sudo systemctl start icecast2
+  fi
+else
+  find_prefix="ssh live.iankelling.org"
+fi
+
+if $find_prefix find /var/icecast -type f | grep .; then
+  echo "warning: suggest clearing /var/icecast with icrmr or moving files. sleeping for 4 seconds"
+  sleep 4
 fi
 
+
 pass=$(sed -n 's/ *<source-password>\([^<]*\).*/\1/p' /p/c/icecast.xml)
 
 
@@ -155,7 +189,47 @@ else
   stream_res=$primary_res
 fi
 
-framerate=8
+stream_x=${stream_res%x*}
+stream_y=${stream_res#*x}
+
+# leave out our i3 window borders
+stream_res=$(( stream_x - 4 ))x$(( stream_y - 4))
+
+
+# if hardware acceleration exists, use it to save power & cpu.
+if vainfo |& grep -i VAProfileVP9Profile &>/dev/null; then
+  # 1500 seems almost flawless
+  bitrate_1080=1500
+
+  encode_settings=(
+    -c:v vp9_vaapi
+    # these options increase compression based on random internet reference.
+    -bsf:v vp9_raw_reorder,vp9_superframe
+  )
+  # https://trac.ffmpeg.org/wiki/Hardware/VAAPI
+  global_extra_args=(
+    -vaapi_device /dev/dri/renderD128
+  )
+  extra_filter_arg=",format=nv12|vaapi,hwupload"
+else
+  # 1000 is a bit blury, 1500 is pretty clear, 2000 makes scrolling
+  # adjust much faster, 2500 has marginal improvement on that.
+  #
+  # note https://livekit.io/webrtc/bitrate-guide (our framerate is lower)
+  bitrate_1080=2000
+
+  encode_settings=(
+    -vcodec libvpx
+    -quality realtime
+    -error-resilient 1
+  )
+fi
+
+bitrate=$(( ( stream_x * stream_y ) / ( (1920*1080) / bitrate_1080 ) ))
+
+
+# 8 seems fine. be conservative by going a bit higher.
+framerate=10
 keyframe_interval=$((framerate * 2))
 
 # Monitor of default sink.
@@ -174,6 +248,8 @@ opts=(
   -hide_banner
   -nostats
 
+  ${global_extra_args[@]}
+
   # tested for decreasing latency: did not help.
   # -probesize 32
   # tested for warning "Queue input is backward in time". did not help.
@@ -219,13 +295,10 @@ opts=(
   #
   # man page say zmq url default includes "localhost", but specifying a
   # localhost url caused an error for me.
-  -filter_complex "[0]azmq,volume=precision=fixed: volume=0 [vol0];
+  -filter_complex "[0]azmq,volume=precision=fixed: volume=$volume [vol0];
 [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1];
 [vol0][vol1] amerge=inputs=2;
-[2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}[out]"
-
-#  [vol0][vol1] amerge=inputs=2,adelay=6000:all=1;
-
+[2]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''${delay_arg}${extra_filter_arg}[out]"
 
   # An online source says to match a 5 second vid delay, we can do an
   # audio delay filter: "adelay=5000|5000". However, we already get
@@ -242,13 +315,10 @@ opts=(
   -map '[out]'
 
   # video output options
-  -vcodec libvpx
   -g $keyframe_interval
-  -quality realtime
-  # for 1080p, default 256k is poor quality. 500 is ok. 1500 is a bit better.
-  -b:v 1500k
-  -threads 2
-  -error-resilient 1
+  ${encode_settings[@]}
+  -b:v ${bitrate}k
+
 
   ## audio output options
   -c:a libvorbis
@@ -264,14 +334,15 @@ opts=(
 
 rm -f /tmp/iank-ffmpeg-interlude-toggle
 
-# start muted
-pactl set-source-mute @DEFAULT_SOURCE@ true
+# system mute. disabled, just using application level mute atm.
+#
+# pactl set-source-mute @DEFAULT_SOURCE@ true
 
 if pkill -f ^ffmpeg.\*icecast://source.\*/fsf; then
   sleep 1
 fi
 
-#echo executing: ffmpeg ${opts[@]}
+echo executing: ffmpeg ${opts[@]}
 
 #{ sleep 1; ffp &>/dev/null & }
 
@@ -324,7 +395,7 @@ if [[ $mount_suffix == -sysops ]]; then
   touch $HOME/.iank-stream-on
 fi
 
-echo true >$HOME/.iank-stream-muted
+echo $volume >$HOME/.iank-stream-muted
 
 ffmpeg "${opts[@]}" &
 if $watch; then
@@ -335,8 +406,6 @@ if $watch; then
   rm -f $HOME/.iank-stream-on
 fi
 
-
-
 ### begin background/development docs ###
 
 # zmq vs stdin commands:
@@ -422,4 +491,9 @@ fi
 # ./configure --enable-libzmq --enable-libpulse --enable-libvorbis --enable-gpl --enable-version3
 #
 
+# note: when playing back, text is going to look aliased unless you
+# watch it in a window that is exactly as bit or bigger than the
+# recording: tabbed i3 window shrinks things. or, use: mpv
+# --video-unscaled
+
 ### end background/development docs ###