From 93fd5a18991587a5f2a6415bc462a426f7a08fd7 Mon Sep 17 00:00:00 2001 From: deflax Date: Mon, 21 Apr 2025 21:59:16 +0300 Subject: [PATCH] rewrite the stream to autodetect the adb port --- .gitignore | 1 + README.md | 4 +-- android.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++--- config.dist | 2 +- stream.sh | 34 -------------------- 5 files changed, 93 insertions(+), 41 deletions(-) delete mode 100755 stream.sh diff --git a/.gitignore b/.gitignore index 04204c7..8ef4392 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config +rec/ diff --git a/README.md b/README.md index 5135542..d98a814 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ ### setup scrcpy `sudo apt install scrcpy` -`sudo apt install v4l2loopback-dkms` +`sudo apt install v4l2loopback-dkms v4l2loopback-utils` ### v4l2loopback.conf: -`options v4l2loopback video_nr=7 card_label="android video"` +`echo 'options v4l2loopback video_nr=5 card_label="android video"' > /etc/modprobe.d/v4l2loopback.conf` ### set wireless android camera pairing 1. enable wifi debugging diff --git a/android.sh b/android.sh index e2f241a..a7fbbc4 100755 --- a/android.sh +++ b/android.sh @@ -1,11 +1,96 @@ #!/bin/bash -# source config -adb connect ${android_camera}:$1 +SCAN_COUNT=0 +SCAN_ATTEMPTS=3 +FFMPEG_LOGLEVEL="repeat+level+info" -scrcpy \ +mkdir -p ./rec/ + +cleanup() +{ + adb_pid=$(ps aux | grep 'adb' | grep ${android} | awk '{print $2}'); + echo "] Killing adb services with pid: ${adb_pid}" + kill -9 ${adb_pid} + exit +} + +# Trap interrupts and exit instead of continuing the loop +trap cleanup SIGINT SIGTERM + +# check if the android host is even alive +if ping -c 1 ${android} &> /dev/null +then + echo "] ping success :)" +else + echo "] ping error :(" + exit 1 +fi + +is_correct_port () { + # check if port 5555 is open + PORT=$(nmap -sT -P0 ${android} -p 5555 | awk -F/ '/tcp open/{print $1}') + if [[ $PORT == 5555 ]]; then + echo "] port 5555 is open :)" + return 0 + else + return 1 + fi +} + +until is_correct_port || [[ $SCAN_COUNT -eq $SCAN_ATTEMPTS ]]; do + # scan and set the port to 5555 + if [ ! -n "$1" ]; then + RANGE="30000-49999" + else + RANGE="$1" + fi + echo "] Checking port(s) ${RANGE}" + + adb connect ${android}:$(nmap -sT -P0 ${android} -p${RANGE} | awk -F/ '/tcp open/{print $1}') + adb tcpip 5555 + adb disconnect + + echo -e "\n] $(( SCAN_COUNT++ ))..." + sleep 1 +done +[[ $SCAN_COUNT -eq $SCAN_ATTEMPTS ]] && echo "] could not set the tcpip port :(" && (exit 1) + + +### + +adb disconnect +adb connect ${android}:5555 + +scrcpy -n \ --video-source=camera --no-audio --no-video-playback \ --camera-id=2 --camera-size=1920x1080 --camera-fps=30 --orientation=90 \ - --v4l2-sink=/dev/video7 + --v4l2-sink=/dev/video5 & + +while : +do + CURRENTDATE=`date +"%Y_%m_%d_%T"` + echo "] ffmpeg start at ${CURRENTDATE}" + + #calf plugins + #input_audio_filter="-af lv2=p=http\\\\://calf.sourceforge.net/plugins/Gate:c=threshold=0.04010706|knee=8|range=0.03148599, lv2=http\\\\://calf.sourceforge.net/plugins/Compressor:c=knee=8|threshold=0.125, lv2=http\\\\://calf.sourceforge.net/plugins/Limiter:c=level_out=0.71697748|limit=0.62763602" + + #load normalization + #output_audio_filter="-filter:a loudnorm" + + ffmpeg -hide_banner -loglevel ${FFMPEG_LOGLEVEL} -threads:v 2 -threads:a 8 -filter_threads 2 \ + -thread_queue_size 512 -f v4l2 -framerate 30 -pix_fmt yuv420p -i /dev/video5 \ + -thread_queue_size 512 -f alsa -ac 2 -i plughw:CARD=USB ${input_audio_filter} \ + -bsf:a aac_adtstoasc -c:a aac -ac 2 -b:a 192k ${output_audio_filter} \ + -init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va \ + -vf 'format=nv12,hwupload' \ + -c:v h264_vaapi -b:v 6000k -minrate:v 6000k -maxrate:v 6000k -bufsize:v 6000k \ + -r:v 30 -g:v 120 -bf:v 3 -refs:v 16 \ + -f tee "[f=mpegts]${srt_output}|[f=matroska]local_recording_${CURRENTDATE}.mkv" + + #CURRENTDATE=`date +"%Y_%m_%d_%T"` + echo "] ffmpeg end at ${CURRENTDATE}" + sleep 5 +done + diff --git a/config.dist b/config.dist index 035e991..a1988b6 100644 --- a/config.dist +++ b/config.dist @@ -1,2 +1,2 @@ -android_camera=192.168.0.123 +android=192.168.0.123 srt_output="srt://CHANGEME:6000?mode=caller&transtype=live&pkt_size=1316&streamid=CHANGEME.stream,mode:publish,token:CHANGEME" diff --git a/stream.sh b/stream.sh deleted file mode 100755 index 5ced69a..0000000 --- a/stream.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -echo "Press CTRL+C to exit" - -source config -LOGLEVEL="debug" -LOGLEVEL="repeat+level+info" - -while : -do - CURRENTDATE=`date +"%Y-%m-%d %T"` - echo Start at ${CURRENTDATE} - - #calf plugins - #input_audio_filter="-af lv2=p=http\\\\://calf.sourceforge.net/plugins/Gate:c=threshold=0.04010706|knee=8|range=0.03148599, lv2=http\\\\://calf.sourceforge.net/plugins/Compressor:c=knee=8|threshold=0.125, lv2=http\\\\://calf.sourceforge.net/plugins/Limiter:c=level_out=0.71697748|limit=0.62763602" - - #load normalization - #output_audio_filter="-filter:a loudnorm" - - ffmpeg -hide_banner -loglevel ${LOGLEVEL} -threads:v 2 -threads:a 8 -filter_threads 2 \ - -thread_queue_size 512 -f v4l2 -framerate 30 -pix_fmt yuv420p -i /dev/video7 \ - -thread_queue_size 512 -f alsa -ac 2 -i plughw:CARD=USB ${input_audio_filter} \ - -bsf:a aac_adtstoasc -c:a aac -ac 2 -b:a 192k ${output_audio_filter} \ - -init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va \ - -vf 'format=nv12,hwupload' \ - -c:v h264_vaapi -b:v 6000k -minrate:v 6000k -maxrate:v 6000k -bufsize:v 6000k \ - -r:v 30 -g:v 120 -bf:v 3 -refs:v 16 \ - -f mpegts ${srt_output} - - CURRENTDATE=`date +"%Y-%m-%d %T"` - echo End at ${CURRENTDATE} - sleep 5 -done -