Skip to content

Commit

Permalink
Support for kernel module settings via the V4L2LOOPBACK_DC_PARAMS
Browse files Browse the repository at this point in the history
environment variable

The user is prompted for the desired video resolution on startup.
  • Loading branch information
popsUlfr committed Apr 28, 2022
1 parent 0db6984 commit f9a6052
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions linuxdeploy-plugin-droidcam.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ EOF
chmod +x "$appdir"/usr/bin/zenity-askpass
cat > "$appdir"/usr/bin/droidcam-module-load <<\EOF
#!/bin/sh
set -e
if [ "$(id -u)" -ne 0 ]
then
echo "Needs to be run as root." >&2
exit 1
fi
V4L2LOOPBACK_DC_PARAMS="${V4L2LOOPBACK_DC_PARAMS:-width=640 height=480}"
LOWER_DIR="$1"
if [ -n "$2" ]
then
V4L2LOOPBACK_DC_PARAMS="$2"
fi
if ! modinfo v4l2loopback-dc >/dev/null 2>&1
then
TMP_DIR="$(mktemp -d -p /tmp appimage-droidcam.XXXXXX)"
Expand All @@ -71,15 +77,68 @@ then
mount -t overlay -o lowerdir="$LOWER_DIR":"/usr/lib/modules/$(uname -r)",upperdir="$TMP_DIR/upper",workdir="$TMP_DIR/work" overlay "/usr/lib/modules/$(uname -r)"
depmod
fi
if lsmod | grep -q '^v4l2loopback_dc\b'
then
if ! modprobe -r v4l2loopback-dc
then
rmmod -f v4l2loopback-dc
fi
fi
modprobe videodev
modprobe -f v4l2loopback-dc width=640 height=480
modprobe -f v4l2loopback-dc $V4L2LOOPBACK_DC_PARAMS
modprobe snd-aloop
EOF
chmod +x "$appdir"/usr/bin/droidcam-module-load
mkdir -p "$appdir"/apprun-hooks
cat > "$appdir"/apprun-hooks/linuxdeploy-plugin-droidcam.sh <<\EOF
export PATH="$APPDIR/usr/bin:$PATH"
if ! lsmod | grep -q '^v4l2loopback_dc\b'
if command -v zenity >/dev/null
then
has_zenity=1
else
has_zenity=0
fi
if [ -z "$V4L2LOOPBACK_DC_PARAMS" ] && [ "$has_zenity" -eq 1 ]
then
curr_width="640"
curr_height="480"
if [ -f /sys/module/v4l2loopback_dc/parameters/width ]
then
curr_width="$(cat /sys/module/v4l2loopback_dc/parameters/width)"
fi
if [ -f /sys/module/v4l2loopback_dc/parameters/height ]
then
curr_height="$(cat /sys/module/v4l2loopback_dc/parameters/height)"
fi
if res="$(printf '640x480\n800x600\n1024x576\n1280x720\n1920x1080\n' | zenity \
--list \
--title="DroidCam Resolution" \
--width=300 \
--height=300 \
--text="Resolution to use for the webcam.\nCurrent size is ${curr_width}x${curr_height}.\nDefault is 640x480.\nV4L2LOOPBACK_DC_PARAMS environment variable can be set with the parameters to pass to the module.\ne.g.: V4L2LOOPBACK_DC_PARAMS=\"width=640 height=480 video_nr=(-1|0..)\"" \
--column=Resolution)" && [ -n "$res" ]
then
V4L2LOOPBACK_DC_PARAMS="width=$(echo "$res" | cut -dx -f1) height=$(echo "$res" | cut -dx -f2)"
export V4L2LOOPBACK_DC_PARAMS
fi
fi
reload_module=0
if [ -n "$V4L2LOOPBACK_DC_PARAMS" ]
then
width="$(echo "$V4L2LOOPBACK_DC_PARAMS" | sed -n 's/^.*\bwidth=\([0-9]\+\).*$/\1/p')"
height="$(echo "$V4L2LOOPBACK_DC_PARAMS" | sed -n 's/^.*\bheight=\([0-9]\+\).*$/\1/p')"
video_nr="$(echo "$V4L2LOOPBACK_DC_PARAMS" | sed -n 's/^.*\bvideo_nr=\(-\?[0-9]\+\).*$/\1/p')"
if { [ -n "$width" ] && [ -f /sys/module/v4l2loopback_dc/parameters/width ] && \
[ "$width" != "$(cat /sys/module/v4l2loopback_dc/parameters/width)" ] ;} || \
{ [ -n "$height" ] && [ -f /sys/module/v4l2loopback_dc/parameters/height ] && \
[ "$height" != "$(cat /sys/module/v4l2loopback_dc/parameters/height)" ] ;} || \
{ [ -n "$video_nr" ] && [ -f /sys/module/v4l2loopback_dc/parameters/video_nr ] && \
[ "$video_nr" != "$(cat /sys/module/v4l2loopback_dc/parameters/video_nr)" ] ;}
then
reload_module=1
fi
fi
if [ "$reload_module" -eq 1 ] || ! lsmod | grep -q '^v4l2loopback_dc\b'
then
if [ -z "$SUDO_ASKPASS" ]
then
Expand All @@ -96,12 +155,6 @@ then
fi
done
fi
if command -v zenity >/dev/null
then
has_zenity=1
else
has_zenity=0
fi
if [ ! -d "$APPDIR/usr/lib/modules/$(uname -r)" ]
then
if ! modinfo v4l2loopback_dc >/dev/null 2>&1
Expand All @@ -125,10 +178,12 @@ then
if [ "$has_zenity" -eq 1 ]
then
mkfifo -m 600 "$TMP_DIR/progress"
cat "$TMP_DIR/progress" | zenity --progress --pulsate --no-cancel --auto-close --title="Installing kernel module" --width=300 --text="Installing kernel module" &
cat "$TMP_DIR/progress" | zenity --progress --pulsate --no-cancel --auto-close --title="DroidCam - Installing kernel module" --width=300 --text="Installing kernel module" &
progress_pid="$!"
fi
if ! cat "$APPDIR/usr/bin/droidcam-module-load" | sudo -A sh -s "$TMP_DIR/lower" 2>&1 | tee -a "$LOG_FILE" >&2
sudo_status="$TMP_DIR/sudo_status"
if ! cat "$APPDIR/usr/bin/droidcam-module-load" | { sudo -A sh -s "$TMP_DIR/lower" "$V4L2LOOPBACK_DC_PARAMS" 2>&1 || echo "$?" > "$sudo_status" ;} | tee -a "$LOG_FILE" >&2 || \
{ [ -f "$sudo_status" ] && [ "$(cat "$sudo_status")" -ne 0 ] ;}
then
echo "ERROR! Failed to load the needed modules." 2>&1 | tee -a "$LOG_FILE" >&2
zenity --error --width=300 --title="ERROR! Failed to load the needed modules." --text="$(cat "$LOG_FILE")" || true
Expand Down

0 comments on commit f9a6052

Please sign in to comment.