restream/reStream.sh
2025-01-25 21:24:13 -08:00

114 lines
3.3 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=- # display output through ffplay
format=- # automatic output format
webcam=false # 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"
# Scale and add padding if we are targeting a webcam because a lot of services
# expect a size of exactly 1280x720 (tested in Firefox, MS Teams, and Skype for
# for business). Send a PR if you can get a higher resolution working.
if $webcam; then
video_filters="$video_filters,format=pix_fmts=yuv420p"
video_filters="$video_filters,scale=-1:720"
video_filters="$video_filters,pad=1280:0:-1:0:#eeeeee"
# Some applications, eg Zoom and Discord, mirror by default the webcam video
# Restore the correct orientation
$hflip && video_filters="$video_filters,hflip"
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 --listen $listen_port" &
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="gray8"
# shellcheck disable=SC2086,SC2090
$receive_cmd |
pv |
(
"$output_cmd" \
-vcodec rawvideo \
-loglevel "error" \
-f rawvideo \
-pixel_format "$pixel_format" \
-video_size "$width,$height" \
$window_title_option \
-i - \
"$@" \
;
kill $$
)