Files
restream/reStream.sh
T

114 lines
3.3 KiB
Bash
Raw Normal View History

2020-02-12 13:13:14 +01:00
#!/bin/sh
2020-04-02 14:14:36 +02:00
# default values for arguments
2023-11-14 07:14:09 -05:00
remarkable="${REMARKABLE_IP:-10.11.99.1}" # remarkable IP address
2025-01-25 21:23:52 -08:00
#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
2020-03-12 00:17:13 +01:00
2020-04-03 12:15:53 +02:00
ssh_cmd() {
echo "[SSH]" "$@" >&2
ssh -o ConnectTimeout=1 \
-o PasswordAuthentication=no \
-o PubkeyAcceptedKeyTypes=+ssh-rsa \
-o HostKeyAlgorithms=+ssh-rsa \
"root@$remarkable" "$@"
2020-04-03 12:15:53 +02:00
}
2021-06-30 22:42:51 +02:00
# 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
2020-04-03 12:15:53 +02:00
if ! ssh_cmd true; then
2021-01-28 23:11:07 +01:00
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."
2024-01-09 17:40:41 +01:00
trap - EXIT
2020-02-12 13:13:14 +01:00
exit 1
fi
2021-01-04 20:54:20 -05:00
if ssh_cmd "[ ! -f ~/restream ] && [ ! -f /opt/bin/restream ]"; then
2020-12-31 14:57:33 +01:00
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
2020-03-12 00:17:13 +01:00
fi
2020-04-07 11:17:45 +02:00
# store extra ffmpeg arguments in $@
set --
2025-01-25 21:23:52 -08:00
video_filters="colorlevels=rimin=0:rimax=29/255:gimin=0:gimax=29/255:bimin=0:bimax=29/255,transpose=3"
2020-03-12 00:17:13 +01:00
# rotate 90 degrees if landscape=true
2020-12-31 17:51:23 +01:00
$landscape && video_filters="$video_filters,transpose=1"
2020-03-12 00:17:13 +01:00
2020-04-14 10:46:07 +02:00
# 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
2020-12-31 14:58:32 +01:00
# for business). Send a PR if you can get a higher resolution working.
2020-04-14 10:46:07 +02:00
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"
2020-04-14 10:46:07 +02:00
fi
2020-04-07 11:17:45 +02:00
# set each frame presentation time to the time it is received
video_filters="$video_filters,setpts=(RTCTIME - RTCSTART) / (TB * 1000000)"
set -- "$@" -vf "${video_filters#,}"
2020-04-02 19:32:59 +02:00
if [ "$output_path" = - ]; then
2020-04-07 10:55:20 +02:00
output_cmd=ffplay
2020-11-20 12:23:15 +01:00
window_title_option="-window_title $window_title"
2020-04-02 19:32:59 +02:00
else
2020-04-07 10:55:20 +02:00
output_cmd=ffmpeg
2020-04-02 19:32:59 +02:00
if [ "$format" != - ]; then
2020-04-07 11:17:45 +02:00
set -- "$@" -f "$format"
2020-04-02 19:32:59 +02:00
fi
2020-04-07 11:17:45 +02:00
set -- "$@" "$output_path"
2020-04-02 19:32:59 +02:00
fi
2020-03-12 00:17:13 +01:00
set -e # stop if an error occurs
2020-02-12 13:13:14 +01:00
# shellcheck disable=SC2089
2025-01-25 21:23:52 -08:00
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
2025-01-25 21:24:13 -08:00
receive_cmd="nc $remarkable $listen_port"
2025-01-25 21:23:52 -08:00
# TODO: return this from stdout
width=1872
height=1404
pixel_format="gray8"
2021-01-09 00:04:22 +01:00
# shellcheck disable=SC2086,SC2090
2025-01-25 15:27:53 -08:00
$receive_cmd |
2025-01-25 21:23:52 -08:00
pv |
2025-01-25 15:27:53 -08:00
(
2021-06-30 11:08:05 +02:00
"$output_cmd" \
-vcodec rawvideo \
2025-01-25 21:23:52 -08:00
-loglevel "error" \
2021-06-30 11:08:05 +02:00
-f rawvideo \
-pixel_format "$pixel_format" \
-video_size "$width,$height" \
$window_title_option \
-i - \
"$@" \
;
kill $$
2021-01-08 11:05:06 +01:00
)