Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert stack to allow for multiple libcamera devices #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions configs/libcamera.conf → configs/libcamera-default.conf
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
### Options for libcamera based cameras (PiCam, Arducam, ...)

# The port on which the webcam server for the camera should listen on. If you have only
# one camera, leave at 8080. If you have more, change to 8081, 8082, etc. The primary
# camera will be considered the one with 8080.
# The port on which the webcam server for the camera should listen on.
PORT=8080

# The path to the camera device.
DEVICE=/base/soc/i2c0mux/i2c@1/imx708@1a

# The resolution to request on the camera sensor. Defaults to 1280x720.
WIDTH=1920
HEIGHT=1080
Expand Down
163 changes: 163 additions & 0 deletions scripts/add-camera
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/bin/bash

if [ "$EUID" -ne 0 ]
then echo "Please run as root/with sudo"
exit 1
fi

if [[ "$1" != "libcamera" ]] && [[ "$1" != "usb" ]]
then echo "type must be libcamera or usb"
exit 1
fi

TYPE=$1
NAME=$2
PORT=$3
DEVICE=$4

CONFIG_DIR=/etc/camera-streamer.conf.d
CONTROL=/usr/bin/camera-streamer-control

## Name

if [ -z "$NAME" ]; then
echo "Usage: $0 <type> <name> [port] [device]"
exit 1
fi

# make sure the name is unique
if [ -e "$CONFIG_DIR/$TYPE-$NAME.conf" ]; then
echo "Camera $TYPE-$NAME already exists"
exit 1
fi

## Port

if [ -z "$PORT" ]; then
port=8080
while grep -qs "PORT=$port" $CONFIG_DIR/*.conf; do
port=$((port+1))
done
PORT=$port
echo "No port provided, automatically selected port $PORT"
echo
fi

# make sure the port is available
if grep -qs "PORT=$PORT" $CONFIG_DIR/*.conf; then
echo "Port $PORT is already used by another camera"
exit 1
fi

# Get a list all camera for a type
if [[ "$TYPE" == "usb" ]]
then
LIST=$(echo /dev/v4l/by-id/usb-*-video-index0)
else
LIST=$(/usr/bin/libcamera-hello --list-cameras | grep -o '/base/soc/[^)]*')
fi

## Device

if [ -z "$DEVICE" ]; then
echo "Please make sure your $TYPE camera is plugged in and select it below:"
echo
PS3="? "
options=($LIST)
select opt in "${options[@]}"; do
if [ -n "$opt" ]; then
DEVICE=$opt
break
fi
echo
done
echo
fi

# make sure the device exists
if [[ $TYPE == "usb" ]]; then
if [ ! -e "$DEVICE" ]; then
echo "Device $DEVICE does not exist"
exit 1
fi
else
found=0
for opt in $LIST; do
if [[ "$opt" == "$DEVICE" ]]; then
found=1
break
fi
done
if [ $found -eq 0 ]; then
echo "Device $DEVICE does not exist"
exit 1
fi
fi

if grep -qs "DEVICE=$DEVICE" $CONFIG_DIR/*.conf; then
echo "Device $DEVICE is already configured"
exit 1
fi

make_usb_conf() {
cat > $CONFIG_DIR/usb-$NAME.conf <<EOF
### Options for USB based cameras

# The port on which the webcam server for the camera should listen on.
PORT=$PORT

# The path to the camera device.
DEVICE=$DEVICE

# The image format of the camera.
FORMAT=YUYV

# The resolution to set on the camera. Defaults to 1280x720.
WIDTH=1280
HEIGHT=720

# The framerate to set on the camera. Defaults to 15fps.
FRAMERATE=15

# Additional options.
OPTIONS=
EOF
}

make_libcamera_conf() {
cat > $CONFIG_DIR/libcamera-$NAME.conf <<EOF
### Options for libcamera based cameras (PiCam, Arducam, ...)

# The port on which the webcam server for the camera should listen on.
PORT=$PORT

# The path to the camera device.
DEVICE=$DEVICE

# The resolution to request on the camera sensor. Defaults to 1280x720.
WIDTH=1920
HEIGHT=1080

# The height to use for the video stream. Defaults to 720.
VIDEO_HEIGHT=720

# The height to use for the snapshots. Defaults to 1080.
SNAPSHOT_HEIGHT=1080

# The framerate to set on the camera. Defaults to 15fps.
FRAMERATE=15

# Additional options. By default enables continuous auto focus (if possible).
OPTIONS='--camera-options="AfMode=2" --camera-options="AfRange=2"'
EOF
}

make_${TYPE}_conf

$CONTROL restart $NAME

echo "Camera $TYPE-$NAME added"
echo
echo "Port: $PORT"
echo "Device: $DEVICE"
echo "Configuration file: $CONFIG_DIR/$TYPE-$NAME.conf"
98 changes: 0 additions & 98 deletions scripts/add-usb-camera

This file was deleted.

36 changes: 36 additions & 0 deletions scripts/list-cameras
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

ALL=$1

CONFIG_DIR=/etc/camera-streamer.conf.d

echo "Available USB camera devices:"
for path in /dev/v4l/by-id/usb-*-video-index0; do
if [[ "$ALL" == "--all" ]]; then
echo
echo "=== $path ==="
echo
v4l2-ctl --device "$path" --all --list-formats --list-formats-ext
else
echo " None available"
fi
done


echo
echo "Available libcamera devices (* = in a config):"
if [ -e /usr/bin/libcamera-hello ]; then
IFS=$'\n'
LIST=$(/usr/bin/libcamera-hello --list-cameras | grep -v '^ ' | sed -e '1,2d')
for d in $LIST; do
inuse=' '
device=$(echo $d | grep -o '/base/soc/[^)]*')
if grep -qs "DEVICE=$device" $CONFIG_DIR/*.conf; then
inuse='*'
fi
echo " [$inuse] $d"
done
unset IFS
else
echo " None available"
fi
15 changes: 0 additions & 15 deletions scripts/list-usb-cameras

This file was deleted.

35 changes: 35 additions & 0 deletions scripts/remove-camera
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

if [ "$EUID" -ne 0 ]
then echo "Please run as root/with sudo"
exit 1
fi

if [[ "$1" != "libcamera" ]] && [[ "$1" != "usb" ]]
then echo "type must be libcamera or usb"
exit 1
fi

TYPE=$1
NAME=$2

CONFIG_DIR=/etc/camera-streamer.conf.d
CONTROL=/usr/bin/camera-streamer-control

if [ -z "$NAME" ]; then
echo "Usage: $0 <name>"
exit 1
fi

if [ ! -e "$CONFIG_DIR/$TYPE-$NAME.conf" ]; then
echo "Camera $TYPE-$NAME does not exist"
exit 1
fi

$CONTROL stop $NAME

rm $CONFIG_DIR/$TYPE-$NAME.conf
if [[ "$TYPE" == "usb" ]]; then
rm /run/systemd/system/camera-streamer-usb-$NAME.path || true
fi
systemctl daemon-reload
27 changes: 0 additions & 27 deletions scripts/remove-usb-camera

This file was deleted.

Loading