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