Trim restream.sh

This commit is contained in:
Mark 2025-01-25 21:23:52 -08:00
parent 66d13d6636
commit f0f4bec8db
Signed by: Mark
GPG Key ID: C6D63995FE72FD80

View File

@ -1,111 +1,14 @@
#!/bin/sh #!/bin/sh
# Current reStream.sh version
version="1.3.1"
# video_filters links:
# https://ffmpeg.org/doxygen/trunk/pixfmt_8h_source.html
# https://ffmpeg.org/ffmpeg-filters.html#eq
rm2_old_firmware_version="3.7.0.1930"
# default values for arguments # default values for arguments
remarkable="${REMARKABLE_IP:-10.11.99.1}" # remarkable IP address remarkable="${REMARKABLE_IP:-10.11.99.1}" # remarkable IP address
landscape=true # rotate 90 degrees to the right #remarkable="${REMARKABLE_IP:-10.143.0.112}" # remarkable IP address
cursor=false # show a cursor where the pen is hovering landscape=true # rotate 90 degrees to the right
output_path=- # display output through ffplay output_path=- # display output through ffplay
format=- # automatic output format format=- # automatic output format
webcam=false # not to a webcam webcam=false # not to a webcam
hflip=false # horizontal flip webcam hflip=false # horizontal flip webcam
measure_throughput=false # measure how fast data is being transferred window_title=reStream # stream window title is reStream
window_title=reStream # stream window title is reStream
video_filters="" # list of ffmpeg filters to apply
unsecure_connection=false # Establish a unsecure connection that is faster
# loop through arguments and process them
while [ $# -gt 0 ]; do
case "$1" in
-v | --version)
echo "reStream version: v$version"
exit
;;
-p | --portrait)
landscape=false
shift
;;
-c | --cursor)
cursor=true
shift
;;
-s | --source)
remarkable="$2"
shift
shift
;;
-o | --output)
output_path="$2"
shift
shift
;;
-f | --format)
format="$2"
shift
shift
;;
-m | --measure)
measure_throughput=true
shift
;;
-w | --webcam)
webcam=true
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
shift
;;
--mirror)
# do nothing if --webcam is not set
hflip=true
shift
;;
-t | --title)
window_title="$2"
shift
shift
;;
-u | --unsecure-connection)
unsecure_connection=true
shift
;;
-h | --help | *)
echo "Usage: $0 [-p] [-c] [-u] [-s <source>] [-o <output>] [-f <format>] [-t <title>] [-m] [-w] [--hflip]"
echo "Examples:"
echo " $0 # live view in landscape"
echo " $0 -p # live view in portrait"
echo " $0 -c # show a cursor where the pen is hovering (rM2 only)"
echo " $0 -s 192.168.0.10 # connect to different IP"
echo " $0 -o remarkable.mp4 # record to a file"
echo " $0 -o udp://dest:1234 -f mpegts # record to a stream"
echo " $0 -w --mirror # write to a webcam (yuv420p + resize + mirror)"
echo " $0 -u # establish a unsecure but faster connection"
exit 1
;;
esac
done
ssh_cmd() { ssh_cmd() {
echo "[SSH]" "$@" >&2 echo "[SSH]" "$@" >&2
@ -116,11 +19,6 @@ ssh_cmd() {
"root@$remarkable" "$@" "root@$remarkable" "$@"
} }
is_current_rm_firmware_version_ge() {
current_rm_firmware_version=$(ssh_cmd "grep 'REMARKABLE_RELEASE_VERSION=' /usr/share/remarkable/update.conf | cut -d '=' -f2")
test "$(printf '%s\n' "$current_rm_firmware_version" "$1" | sort -rV | head -n 1)" = "$current_rm_firmware_version"
}
# kill reStream on remarkable at the end. # kill reStream on remarkable at the end.
# shellcheck disable=SC2016 # shellcheck disable=SC2016
exit_rm() { exit_rm() {
@ -128,12 +26,6 @@ exit_rm() {
} }
trap exit_rm EXIT INT HUP trap exit_rm EXIT INT HUP
# SSH_CONNECTION is a variable on reMarkable => ssh '' instead of ssh ""
# shellcheck disable=SC2016
remarkable_ip() {
ssh_cmd 'echo $SSH_CONNECTION' | cut -d\ -f3 | cut -d":" -f4
}
# check if we are able to reach the remarkable # check if we are able to reach the remarkable
if ! ssh_cmd true; then if ! ssh_cmd true; then
echo "$remarkable unreachable or you have not set up an ssh key." echo "$remarkable unreachable or you have not set up an ssh key."
@ -143,54 +35,6 @@ if ! ssh_cmd true; then
exit 1 exit 1
fi fi
rm_version="$(ssh_cmd cat /sys/devices/soc0/machine)"
case "$rm_version" in
"reMarkable 1.0")
width=1408
height=1872
bytes_per_pixel=2
fb_file="/dev/fb0"
pixel_format="rgb565le"
;;
"reMarkable 2.0")
if ssh_cmd "[ -f /dev/shm/swtfb.01 ]"; then
width=1404
height=1872
bytes_per_pixel=2
fb_file="/dev/shm/swtfb.01"
pixel_format="rgb565le"
else
width=1872
height=1404
fb_file=":mem:"
# Use updated video settings?
if is_current_rm_firmware_version_ge $rm2_old_firmware_version; then
echo "Using the newer :mem: video settings."
bytes_per_pixel=2
pixel_format="gray16be"
video_filters="$video_filters,colorlevels=rimin=0:rimax=29/255:gimin=0:gimax=29/255:bimin=0:bimax=29/255,transpose=3"
# Use the previous video settings.
else
echo "Using the older :mem: video settings."
bytes_per_pixel=1
pixel_format="gray8"
video_filters="$video_filters,transpose=2"
fi
fi
;;
*)
echo "Unsupported reMarkable version: $rm_version."
echo "Please visit https://github.com/rien/reStream/ for updates."
exit 1
;;
esac
# technical parameters
loglevel="info"
# check if restream binay is present on remarkable
if ssh_cmd "[ ! -f ~/restream ] && [ ! -f /opt/bin/restream ]"; then if ssh_cmd "[ ! -f ~/restream ] && [ ! -f /opt/bin/restream ]"; then
echo "The restream binary is not installed on your reMarkable." echo "The restream binary is not installed on your reMarkable."
echo "Please install it using the instruction in the README:" echo "Please install it using the instruction in the README:"
@ -198,22 +42,10 @@ if ssh_cmd "[ ! -f ~/restream ] && [ ! -f /opt/bin/restream ]"; then
exit 1 exit 1
fi fi
# use pv to measure throughput if desired, else we just pipe through cat
if $measure_throughput; then
if ! pv --version >/dev/null; then
echo "You need to install pv to measure data throughput."
exit 1
else
loglevel="error" # verbose ffmpeg output interferes with pv
host_passthrough="pv"
fi
else
host_passthrough="cat"
fi
# store extra ffmpeg arguments in $@ # store extra ffmpeg arguments in $@
set -- 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 # rotate 90 degrees if landscape=true
$landscape && video_filters="$video_filters,transpose=1" $landscape && video_filters="$video_filters,transpose=1"
@ -251,30 +83,25 @@ fi
set -e # stop if an error occurs set -e # stop if an error occurs
restream_options="-h $height -w $width -b $bytes_per_pixel -f $fb_file"
if "$cursor"; then
restream_options="$restream_options -c"
fi
# shellcheck disable=SC2089 # shellcheck disable=SC2089
restream_rs="PATH=\"\$PATH:/opt/bin/:.\" restream $restream_options" restream_rs="PATH=\"\$PATH:/opt/bin/:.\" restream"
if $unsecure_connection; then listen_port=16789
listen_port=16789 ssh_cmd "$restream_rs --listen $listen_port" &
ssh_cmd "$restream_rs --listen $listen_port" & sleep 1 # give some time to restream.rs to start listening
sleep 1 # give some time to restream.rs to start listening receive_cmd="nc $(remarkable) $listen_port"
receive_cmd="nc $(remarkable_ip) $listen_port"
else # TODO: return this from stdout
receive_cmd="ssh_cmd $restream_rs" width=1872
fi height=1404
pixel_format="gray8"
# shellcheck disable=SC2086,SC2090 # shellcheck disable=SC2086,SC2090
$receive_cmd | $receive_cmd |
$host_passthrough | pv |
( (
"$output_cmd" \ "$output_cmd" \
-vcodec rawvideo \ -vcodec rawvideo \
-loglevel "$loglevel" \ -loglevel "error" \
-f rawvideo \ -f rawvideo \
-pixel_format "$pixel_format" \ -pixel_format "$pixel_format" \
-video_size "$width,$height" \ -video_size "$width,$height" \