restream/reStream.sh

94 lines
2.2 KiB
Bash
Raw Normal View History

2020-02-12 13:13:14 +01:00
#!/bin/sh
# default values for arguments
ssh_host="root@10.11.99.1" # remarkable connected trough USB
landscape=true # rotate 90 degrees to the right
2020-04-01 16:51:01 +02:00
# loop through arguments and process them
while [ $# -gt 0 ]; do
case "$1" in
2020-04-03 12:15:53 +02:00
-p | --portrait)
landscape=false
shift
;;
2020-04-03 12:15:53 +02:00
-d | --destination)
ssh_host="$2"
shift
shift
;;
2020-04-01 16:51:01 +02:00
*)
echo "Usage: $0 [-p] [-d <destination>]"
exit 1
2020-04-03 12:15:53 +02:00
;;
2020-04-01 16:51:01 +02:00
esac
done
2020-03-12 00:17:13 +01:00
# technical parameters
2020-02-12 13:13:14 +01:00
width=1408
height=1872
bytes_per_pixel=2
loop_wait="true"
loglevel="info"
2020-04-03 12:15:53 +02:00
ssh_cmd() {
ssh -o ConnectTimeout=1 "$ssh_host" "$@"
}
# check if we are able to reach the remarkable
2020-04-03 12:15:53 +02:00
if ! ssh_cmd true; then
2020-02-12 13:13:14 +01:00
echo "$ssh_host unreachable"
exit 1
fi
2020-03-12 00:17:13 +01:00
fallback_to_gzip() {
echo "Falling back to gzip, your experience may not be optimal."
echo "Go to https://github.com/rien/reStream/#sub-second-latency for a better experience."
compress="gzip"
decompress="gzip -d"
2020-03-12 00:17:13 +01:00
sleep 2
}
# check if lz4 is present on remarkable
2020-04-03 12:15:53 +02:00
if ssh_cmd "[ -f /opt/bin/lz4 ]"; then
compress="/opt/bin/lz4"
2020-04-03 12:15:53 +02:00
elif ssh_cmd "[ -f ~/lz4 ]"; then
compress="\$HOME/lz4"
fi
2020-02-12 13:13:14 +01:00
# gracefully degrade to gzip if is not present on remarkable or host
2020-03-12 00:17:13 +01:00
if [ -z "$compress" ]; then
echo "Your remarkable does not have lz4."
2020-03-12 00:17:13 +01:00
fallback_to_gzip
elif ! which lz4; then
echo "Your host does not have lz4."
2020-03-12 00:17:13 +01:00
fallback_to_gzip
else
decompress="lz4 -d"
2020-03-12 00:17:13 +01:00
fi
# calculte how much bytes the window is
2020-04-03 12:15:53 +02:00
window_bytes="$((width * height * bytes_per_pixel))"
2020-03-12 00:17:13 +01:00
# rotate 90 degrees if landscape=true
2020-02-24 09:30:12 +01:00
landscape_param="$($landscape && echo '-vf transpose=1')"
2020-03-12 00:17:13 +01:00
# read the first $window_bytes of the framebuffer
head_fb0="dd if=/dev/fb0 count=1 bs=$window_bytes 2>/dev/null"
2020-03-12 00:17:13 +01:00
# loop that keeps on reading and compressing, to be executed remotely
read_loop="while $head_fb0; do $loop_wait; done | $compress"
2020-03-12 00:17:13 +01:00
set -e # stop if an error occurs
2020-02-12 13:13:14 +01:00
2020-04-03 12:15:53 +02:00
# shellcheck disable=SC2086
ssh_cmd "$read_loop" \
| $decompress \
2020-04-03 12:15:53 +02:00
| ffplay \
-vcodec rawvideo \
-loglevel "$loglevel" \
-f rawvideo \
-pixel_format gray16le \
-video_size "$width,$height" \
$landscape_param \
-i -