Skip to content

Instantly share code, notes, and snippets.

@andresilva
Last active December 7, 2023 15:06
Show Gist options
  • Save andresilva/b019cedbc1f78f34f3537c9d95869639 to your computer and use it in GitHub Desktop.
Save andresilva/b019cedbc1f78f34f3537c9d95869639 to your computer and use it in GitHub Desktop.
Test ipu6 webcam on NixOS
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p linuxPackages_latest.v4l2loopback gst_all_1.gstreamer
function usage() {
echo "Usage: $0 <platform name> [--webcam]"
echo "Supported platforms are: ipu6, ipu6ep, ipu6epmtl"
exit
}
PLATFORM="$1"
case "${PLATFORM}" in
ipu6 | ipu6ep | ipu6epmtl) ;;
*)
echo "Invalid platform provided"
usage
;;
esac
if [[ "$2" == "--webcam" ]]; then
WEBCAM_MODE=1
elif [[ -n "$2" ]]; then
usage
else
WEBCAM_MODE=0
fi
if [[ "$PLATFORM" == "ipu6" ]]; then
FORMAT="YUY2"
else
FORMAT="NV12"
fi
if [[ $WEBCAM_MODE == 0 ]]; then
case "${XDG_SESSION_TYPE}" in
wayland)
SINK=waylandsink
;;
x11)
SINK=ximagesink
;;
*)
echo "Couldn't detect session type"
echo "Please set XDG_SESSION_TYPE to x11 or wayland"
exit
;;
esac
else
DEVICE=$(sudo v4l2loopback-ctl add -x 1 -n 'Test webcam')
SINK="v4l2sink device=$DEVICE"
fi
export NIXPKGS_ALLOW_UNFREE=1
export GST_PLUGIN_PATH=$(nix eval --raw --impure --expr "let pkgs = import <nixpkgs> {}; in with pkgs; with gst_all_1; lib.makeSearchPathOutput \"lib\" \"lib/gstreamer-1.0\" [ gst-plugins-bad gst-plugins-base gst-plugins-good gstreamer.out icamerasrc-$PLATFORM ]")
sudo -E gst-launch-1.0 icamerasrc ! video/x-raw,format=$FORMAT,width=1280,height=720 ! videoconvert ! $SINK &>/dev/null
if [[ $WEBCAM_MODE == 1 ]]; then
sudo v4l2loopback-ctl delete "$DEVICE"
if [[ -c "$DEVICE" ]]; then
echo "Failed to delete the loopback camera (likely because it was still in use)"
echo "Run 'sudo v4l2loopback-ctl delete $DEVICE' after it is no longer in use (or just reboot)"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment