restream/reStream.sh
Mark cef1cd7495
Some checks failed
Check / linting (push) Failing after 6s
Check / formatting (push) Failing after 6s
Check / build (push) Failing after 3m37s
Minor cleanup
2025-01-26 10:33:13 -08:00

122 lines
3.4 KiB
Bash
Executable File

#!/bin/sh
# default values for arguments
remarkable="${REMARKABLE_IP:-10.11.99.1}" # remarkable IP address
#remarkable="${REMARKABLE_IP:-10.143.0.112}" # remarkable IP address
landscape=true # rotate 90 degrees to the right
output_path=/dev/video5 # display output through ffplay
format=- # automatic output format
webcam=true # not to a webcam
hflip=false # horizontal flip webcam
window_title=reStream # stream window title is reStream
ssh_cmd() {
echo "[SSH]" "$@" >&2
ssh -o ConnectTimeout=1 \
-o PasswordAuthentication=no \
-o PubkeyAcceptedKeyTypes=+ssh-rsa \
-o HostKeyAlgorithms=+ssh-rsa \
"root@$remarkable" "$@"
}
# kill reStream on remarkable at the end.
# shellcheck disable=SC2016
exit_rm() {
ssh_cmd 'kill $(pidof restream)'
}
trap exit_rm EXIT INT HUP
# check if we are able to reach the remarkable
if ! ssh_cmd true; then
echo "$remarkable unreachable or you have not set up an ssh key."
echo "If you see a 'Permission denied' error, please visit"
echo "https://github.com/rien/reStream/#installation for instructions."
trap - EXIT
exit 1
fi
if ssh_cmd "[ ! -f ~/restream ] && [ ! -f /opt/bin/restream ]"; then
echo "The restream binary is not installed on your reMarkable."
echo "Please install it using the instruction in the README:"
echo "https://github.com/rien/reStream/#installation"
exit 1
fi
# store extra ffmpeg arguments in $@
set --
video_filters="colorlevels=rimin=0:rimax=29/255:gimin=0:gimax=29/255:bimin=0:bimax=29/255,transpose=3"
# rotate 90 degrees if landscape=true
$landscape && video_filters="$video_filters,transpose=1"
if $webcam; then
video_filters="$video_filters,format=pix_fmts=yuv420p"
format="v4l2"
# check if there is a modprobed v4l2 loopback device
# use the first cam as default if there is no output_path already
cam_path=$(v4l2-ctl --list-devices |
sed -n '/^[^\s]\+platform:v4l2loopback/{n;s/\s*//g;p;q}')
# fail if there is no such device
if [ -e "$cam_path" ]; then
if [ "$output_path" = "-" ]; then
output_path="$cam_path"
fi
else
echo "Could not find a video loopback device, did you"
echo "sudo modprobe v4l2loopback"
exit 1
fi
fi
# set each frame presentation time to the time it is received
video_filters="$video_filters,setpts=(RTCTIME - RTCSTART) / (TB * 1000000)"
set -- "$@" -vf "${video_filters#,}"
if [ "$output_path" = - ]; then
output_cmd=ffplay
window_title_option="-window_title $window_title"
else
output_cmd=ffmpeg
if [ "$format" != - ]; then
set -- "$@" -f "$format"
fi
set -- "$@" "$output_path"
fi
set -e # stop if an error occurs
# shellcheck disable=SC2089
restream_rs="PATH=\"\$PATH:/opt/bin/:.\" restream"
listen_port=16789
ssh_cmd "$restream_rs --port $listen_port --format mono" &
sleep 1 # give some time to restream.rs to start listening
receive_cmd="nc $remarkable $listen_port"
# TODO: return this from stdout
width=1872
height=1404
pixel_format="monob"
# shellcheck disable=SC2086,SC2090
$receive_cmd |
(
"$output_cmd" \
-vcodec rawvideo \
-loglevel "error" \
-f rawvideo \
-pixel_format "$pixel_format" \
-video_size "$width,$height" \
$window_title_option \
-i - \
"$@" \
;
kill $$
)