icecast improvements
[distro-setup] / ffs
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to change
7 # to a recommended GPL license.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 # ffs = ffmpeg stream
24
25 # potential improvement: it might be nice that we could have a tall terminal but only use
26 # the top half for a 1080p stream, this is how:
27 # https://superuser.com/questions/1106674/how-to-add-blank-lines-above-the-bottom-in-terminal
28
29
30 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
31 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
32 set -eE -o pipefail
33 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
34
35 usage() {
36 cat <<EOF
37 Usage: ${0##*/} [OPTIONS] [sysops|tech|staff|test]
38
39 arg is icecast mountpoint suffix, except staff removes suffix.
40
41 -d debug.
42 -r RESOLUTION_TYPE
43 full: full screen even high resolution.
44 tall (default): half screen.
45 quarter: self evident
46 -l loud/listen. Start unmuted. Usually for testing.
47 -u Undelayed. Removes 5 second video delay, and about 4 second audio delay.
48 -w do not launch watch of stream
49
50 note: args duplicated in ffp
51
52
53 -h|--help Print help and exit.
54
55 Note: Uses util-linux getopt option parsing: spaces between args and
56 options, short options can be combined, options before args.
57 EOF
58 exit $1
59 }
60
61 ##### begin command line parsing ########
62
63 # ensure we can handle args with spaces or empty.
64 ret=0; getopt -T || ret=$?
65 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
66
67 ffp_args=()
68 debug=false
69 delay=true
70 loglevel=fatal
71 watch=true
72 volume=0
73 fullscreen=false
74 tall=true
75 temp=$(getopt -l help hdlr:uw "$@") || usage 1
76 eval set -- "$temp"
77 while true; do
78 case $1 in
79 -d)
80 debug=true
81 loglevel=debug
82 loglevel=info
83 ffp_args+=(-d)
84 ;;
85 -l)
86 volume=1
87 ;;
88 -r)
89 case $2 in
90 tall)
91 fullscreen=false
92 tall=true
93 ;;
94 quarter)
95 fullscreen=false
96 tall=false
97 ;;
98 full)
99 fullscreen=true
100 tall=false
101 ;;
102 esac
103 shift
104 ;;
105 -w)
106 watch=false
107 ;;
108 -u)
109 delay=false
110 ;;
111 -h|--help) usage ;;
112 --) shift; break ;;
113 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
114 esac
115 shift
116 done
117
118 mount_suffix=-sysops
119 if [[ $1 ]]; then
120 case $1 in
121 sysops|tech)
122 mount_suffix=-$1
123 ;;&
124 tech)
125 delay=false
126 ;;
127 staff)
128 mount_suffix=
129 ;;
130 *)
131 echo "error: unexpected \$1: $1" >&2
132 exit 1
133 ;;
134 esac
135 ffp_args+=($1)
136 fi
137
138 if $delay; then
139 # 2500 gets us around a 4 second delay, up from 1.5s.
140 delay_arg=,tpad=start_duration=2500ms
141 fi
142
143
144 ##### end command line parsing ########
145
146 host=live.iankelling.org:8443
147 live_host=$(dig +timeout=1 +short @iankelling.org live.iankelling.org)
148 vps_host=$(dig +timeout=1 +short iankelling.org)
149 if [[ $live_host != "$vps_host" ]] && ip n show 10.2.0.1 | grep . &>/dev/null && \
150 [[ $(dig +timeout=1 +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]]; then
151 host=127.0.0.1:8000
152 else
153 find_prefix="ssh live.iankelling.org"
154 fi
155
156 if $find_prefix find /var/icecast -type f | grep .; then
157 echo "warning: suggest clearing /var/icecast with icrmr or moving files. sleeping for 4 seconds"
158 sleep 4
159 fi
160
161
162 pass=$(sed -n 's/ *<source-password>\([^<]*\).*/\1/p' /p/c/icecast.xml)
163
164
165 tmpf=$(mktemp)
166 xrandr >$tmpf
167
168 # example xrandr output: 1280x800+0+0
169 primary_res=$(awk '$2 == "connected" && $3 == "primary" { print $4 }' $tmpf | sed 's/+.*//')
170 tmp=$(awk '$2 == "connected" && $3 != "primary" { print $3 }' $tmpf | sed 's/+/ /g')
171 read -r secondary_res x_offset _ <<<"$tmp"
172
173
174 if [[ $secondary_res ]]; then
175 secondary_x=${secondary_res%%x*}
176 secondary_y=${secondary_res##*x}
177 if $fullscreen; then
178 stream_res=$secondary_res
179 elif $tall; then
180 stream_res=$(( secondary_x / 2 ))x$secondary_y
181 else
182 stream_res=$(( secondary_x / 2 ))x$(( secondary_y / 2))
183 fi
184 else
185 x_offset=0
186 stream_res=$primary_res
187 fi
188
189 # for 1080p, default 256k is poor quality. 500 is ok. 1500 is a bit
190 # better, so go with that. Also, that is about 2x what is recommended
191 # in https://livekit.io/webrtc/bitrate-guide (our framerate is lower).
192
193 bitrate=$(( ( ${stream_res/x/*} ) / 1380 ))
194
195 # 8 seems fine. be conservative by going a bit higher.
196 framerate=10
197 keyframe_interval=$((framerate * 2))
198
199 # Monitor of default sink.
200 # eg: alsa_output.usb-Audio-gd_Audio-gd-00.analog-stereo
201 pa_sink=$(pactl get-default-sink).monitor
202
203 # this is for ffmpeg warnings. doesnt seem to affect latency.
204 # 160 was too small. at 300, it occasionally complains,
205 # probably only when we are using delayed output
206 thread_queue_size_arg="-thread_queue_size 500"
207
208 opts=(
209 # global options
210 # be relatively quiet. switch to debug when testing.
211 -v $loglevel
212 -hide_banner
213 -nostats
214
215 # tested for decreasing latency: did not help.
216 # -probesize 32
217 # tested for warning "Queue input is backward in time". did not help.
218 #-rtbufsize 500M
219
220 # note: ordering of inputs also affects zmqsend commands.
221
222 ## audio input options
223
224 -f pulse
225 -name ffs
226 # note: duplicated
227 $thread_queue_size_arg
228 -fragment_size 512
229 -i default
230
231 -f pulse
232 $thread_queue_size_arg
233 # pulse knows this name somewhere
234 -name ffsdesktop
235 # This fixes latency. i haven't tried tuning it, but going too low creates
236 # choppy output.
237 -fragment_size 512
238 -i "$pa_sink"
239
240
241 ## video input options
242 -video_size $stream_res
243 $thread_queue_size_arg
244 -f x11grab
245 -framerate $framerate
246 -i :0.0+$x_offset.0
247
248 # Video + audio filter. Note: this has only the things we actually need in it.
249 #
250 # volume=precision=fixed fixes this error:
251 # The following filters could not choose their formats: Parsed_amerge_4.
252 #
253 # Default volume precision is float. Our input is fixed. maybe ffmpeg
254 # thinks the input could change and so can't commit to an output.
255 # The error suggests using aformat, which seems like it would probably
256 # also fix the error.
257 #
258 # man page say zmq url default includes "localhost", but specifying a
259 # localhost url caused an error for me.
260 -filter_complex "[0]azmq,volume=precision=fixed: volume=$volume [vol0];
261 [1]azmq='b=tcp\://127.0.0.1\:5556',volume=precision=fixed: volume=0 [vol1];
262 [vol0][vol1] amerge=inputs=2;
263 [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]"
264
265 # An online source says to match a 5 second vid delay, we can do an
266 # audio delay filter: "adelay=5000|5000". However, we already get
267 # a stream delay of about 2 seconds, and having the audio be about
268 # 2 seconds ahead is fine, they do that intentionally in soccer
269 # matches.
270
271 # Based on error message and poking around, it seems ffmpeg is not
272 # smart enough to see that [vol0] and [vol1] are inputs to the amerge
273 # filter, and thus we would not want them as final outputs. So, we
274 # have to identify the amerge output and pass it to -out. This
275 # identifier is called an "output pad" in man ffmpeg-filters, and a
276 # "link label" in man ffmpeg.
277 -map '[out]'
278
279 # video output options
280 -vcodec libvpx
281 -g $keyframe_interval
282 -quality realtime
283 -b:v ${bitrate}k
284 -threads 2
285 -error-resilient 1
286
287 ## audio output options
288 -c:a libvorbis
289 -b:a 128k
290 # afaik, this ensures that the amerge doesn't make 4 channel output if
291 # our output format supported it.
292 -ac 2
293
294 -content_type video/webm
295 -f webm
296 icecast://source:$pass@$host/fsf$mount_suffix.webm
297 )
298
299 rm -f /tmp/iank-ffmpeg-interlude-toggle
300
301 # system mute. disabled, just using application level mute atm.
302 #
303 # pactl set-source-mute @DEFAULT_SOURCE@ true
304
305 if pkill -f ^ffmpeg.\*icecast://source.\*/fsf; then
306 sleep 1
307 fi
308
309 #echo executing: ffmpeg ${opts[@]}
310
311 #{ sleep 1; ffp &>/dev/null & }
312
313 if $debug; then
314 ffmpeg "${opts[@]}"
315 exit 0
316 fi
317
318 ##### begin clipboard history checkup ####
319
320 # Avoid streaming with secrets in our clipboard history. We could just
321 # clear the history, but here I truncate it to a max and then show it,
322 # and then I can press super+y if I want to clear it, or close the
323 # window if I want to keep it.
324 copyqcount=$(copyq count)
325 regex='^[1-9][0-9]*$'
326 if [[ $copyqcount =~ $regex ]]; then
327 # i dont want to think about more than this
328 max_rows=40
329 if (( copyqcount >= max_rows )); then
330 rows_arg=()
331 for ((i=max_rows; i<copyqcount; i++)); do
332 rows_arg+=($i)
333 done
334 copyq remove "${rows_arg[@]}"
335 fi
336 copyq show
337 gone=false
338 for (( i=0; i<40; i++ )); do
339 if i3-msg -t get_tree | jq -e '.. | select(.class? == "copyq" and .instance? == "copyq")' &>/dev/null; then
340 sleep .5
341 else
342 gone=true
343 break
344 fi
345 done
346 if ! $gone; then
347 msg="ffs: copyq not gone. aborting. super+y = copyq-restart / clear"
348 if [[ -t 0 ]]; then
349 echo $msg
350 else
351 dunstify -u critical -h string:x-dunst-stack-tag:alert "$msg"
352 fi
353 exit 1
354 fi
355 fi
356 ##### end clipboard history checkup ####
357
358 if [[ $mount_suffix == -sysops ]]; then
359 touch $HOME/.iank-stream-on
360 fi
361
362 echo $volume >$HOME/.iank-stream-muted
363
364 ffmpeg "${opts[@]}" &
365 if $watch; then
366 # watch the stream and end the stream when we stop watching.
367 sleep 2
368 ffp -d "${ffp_args[@]}" ||:
369 kill %%
370 rm -f $HOME/.iank-stream-on
371 fi
372
373 ### begin background/development docs ###
374
375 # zmq vs stdin commands:
376 #
377 # * zmq allows targeting a specific filter when there are duplicates.
378 #
379 # * if you type stdin command too slow, ffmpeg will die because it stops
380 # doing normal work while it is waiting.
381 #
382 # * zmq returns a result to the calling process, rather than printing to
383 # stdout.
384 #
385 # * the only simple zmq tool I found, zmqsend, requires compiling. I
386 # used the latest ffmpeg 7.0.1. Build like so:
387 #
388 # p build-dep ffmpeg/aramo
389 # ./configure --enable-libzmq # i already had libzmq3-dev installed
390 # make alltools
391 # cp tools/zmqsend /a/opt/bin
392 #
393 # * ffmpeg debug output was useful in testing zmq commands.
394 #
395 # * Important documentation for stdin commands is only found by typing
396 # "c" into the stdin of ffmpeg and reading the output.
397 #
398 #
399 #
400 # stdin command docs, before I abandoned it for zmq:
401 # mkfifo -m 0600 /tmp/iank-ffmpeg
402 # # ffmpeg sits and waits until we do this. dunno why.
403 # echo >/tmp/iank-ffmpeg &
404 # ffmpeg ... </tmp/iank-ffmpeg |& while read -r line; do : check results; done
405 # # example of working commands:
406 # echo "cdrawbox -1 t 0" >/tmp/iank-ffmpeg
407 # echo "cdrawbox -1 t fill" >/tmp/iank-ffmpeg
408 # echo "cdrawtext -1 reinit text=''" >/tmp/iank-ffmpeg
409 # echo "cvolume -1 volume=1" >/tmp/iank-ffmpeg
410
411
412 # For testing: to show the number of audio channels in a resulting file
413 # https://stackoverflow.com/questions/47905083/how-to-check-number-of-channels-in-my-audio-wav-file-using-ffmpeg-command
414 #
415 # ffprobe -i /tmp/out.wav -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0
416
417 # for a right/left speaker test:
418 # https://askubuntu.com/questions/148363/which-linux-command-can-i-use-to-test-my-speakers-for-current-talk-radio-output
419 # p install alsa-utils
420 # speaker-test -t wav -c 2 -l 1
421
422 # There are 2 other options for audio, so I wanted to do a little
423 # performance measurement of this method.
424 # 1 is to combine the 2 audio sources in pulse,
425 # https://unix.stackexchange.com/questions/351764/create-combined-source-in-pulseaudio .
426 # 1 is to record mumble and combine in post processing.
427
428 ### benchmark / perf tests: these are pretty inaccurate.
429 # 29 seconds cpu use. video bitrate 1500k, 8 fps, 2x keyframe interval.
430 # * 64k vorbis: 69.7%
431 # * 128k vorbis: 70.1% (used in subsequent tests)
432 # * 1 audio input: 64.3%
433 # * 0 audio inputs: 59.2%
434
435 # how I did perf testing: add -to 00:00:30 to ffmpeg opts to
436 # conveniently exit after measurement. Then run:
437 #
438 # ffmpeg "${opts[@]}" &
439 # pid=$!
440 # sleep 29
441 # ps -p $pid -o %cpu
442 # kill %%
443
444 # filter for only 1 audio input:
445 #-filter_complex "[0]azmq,volume=precision=fixed;[1]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
446 # filter with 0 audio input:
447 # -filter_complex "[0]zmq='b=tcp\://127.0.0.1\:5557',drawbox=color=0x262626,drawtext=fontsize=90: fontcolor=beige: x=40: y=40: text=''"
448
449
450 # When things weren't working, I did some checking on newer ffmpeg to
451 # see if that helped. It never did. I compiled the latest ffmpeg release
452 # tarball, 7.0.1, and tried the version in debian bullseye by schrooting
453 # before running ffmpeg. Building was just configure; make, but then I
454 # found some flags that were needed. gpl flags r just because I noticed them.
455 # ./configure --enable-libzmq --enable-libpulse --enable-libvorbis --enable-gpl --enable-version3
456 #
457
458 ### end background/development docs ###