Files
restream/reStream.sh
T

135 lines
3.4 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
2020-04-02 19:32:59 +02:00
ssh_host="root@10.11.99.1" # remarkable connected through USB
2020-04-02 14:14:36 +02:00
landscape=true # rotate 90 degrees to the right
2020-04-02 19:32:59 +02:00
output_path=- # display output through ffplay
format=- # automatic output format
2020-04-01 16:51:01 +02:00
2020-04-02 14:14:36 +02:00
# loop through arguments and process them
2020-04-02 16:17:21 +02:00
while [ $# -gt 0 ]; do
case "$1" in
2020-04-03 12:15:53 +02:00
-p | --portrait)
2020-04-02 14:14:36 +02:00
landscape=false
shift
;;
2020-04-07 11:21:10 +02:00
-s | --source)
2020-04-02 14:14:36 +02:00
ssh_host="$2"
shift
shift
;;
2020-04-07 11:21:10 +02:00
-o | --output)
2020-04-02 19:32:59 +02:00
output_path="$2"
shift
shift
;;
2020-04-07 11:21:10 +02:00
-f | --format)
2020-04-02 19:32:59 +02:00
format="$2"
shift
shift
;;
2020-04-07 11:48:46 +02:00
-h | --help | *)
2020-04-02 19:32:59 +02:00
echo "Usage: $0 [-p] [-s <source>] [-o <output>] [-f <format>]"
2020-04-07 11:48:46 +02:00
echo "Examples:"
echo " $0 # live view in landscape"
echo " $0 -p # live view in portrait"
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"
2020-04-02 14:14:36 +02:00
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
2020-04-07 11:17:45 +02:00
elif ! lz4 -V; 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
2020-04-07 11:17:45 +02:00
# list of ffmpeg filters to apply
video_filters=""
2020-04-02 19:32:59 +02:00
2020-04-07 11:17:45 +02:00
# store extra ffmpeg arguments in $@
set --
2020-04-02 19:32:59 +02:00
# calculate 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
$landscape && video_filters="$video_filters,transpose=1"
2020-03-12 00:17:13 +01:00
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)"
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-04-07 11:17:45 +02:00
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-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
2020-04-03 12:15:53 +02:00
# shellcheck disable=SC2086
ssh_cmd "$read_loop" \
| $decompress \
2020-04-07 10:55:20 +02:00
| "$output_cmd" \
2020-04-07 11:21:10 +02:00
-vcodec rawvideo \
-loglevel "$loglevel" \
-f rawvideo \
-pixel_format gray16le \
-video_size "$width,$height" \
-i - \
"$@"