Skip to content

Commit 61d83b3

Browse files
committed
This finally seems to work
1 parent c8191af commit 61d83b3

8 files changed

Lines changed: 78 additions & 49 deletions

scripts/10-install-camera-streamer

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,17 @@ sed -i 's/^camera_auto_detect=0/camera_auto_detect=1/' /boot/config.txt
2626
# install default configs
2727
mkdir -p /boot/camera-streamer
2828
cp /files/camera-streamer/libcamera.conf /boot/camera-streamer/libcamera.conf
29-
cp /files/camera-streamer/usb.conf /boot/camera-streamer/usb.conf
29+
cp /files/camera-streamer/usb-default.conf /boot/camera-streamer/usb-default.conf
3030

31-
# install libcamera service
32-
cp /files/camera-streamer/camera-streamer-libcamera.service /etc/systemd/system/camera-streamer-libcamera.service
33-
systemctl enable camera-streamer-libcamera.service
34-
35-
# install USB camera service
31+
# install services
3632
cp /files/camera-streamer/camera-streamer-control /root/bin/camera-streamer-control
3733
chmod +x /root/bin/camera-streamer-control
3834

39-
cp /files/camera-streamer/camera-streamer-usb@.service /etc/systemd/system/camera-streamer-usb@.service
40-
cp /files/camera-streamer/camera-streamer-usb@.path /etc/systemd/system/camera-streamer-usb@.path
4135
cp /files/camera-streamer/camera-streamer.service /etc/systemd/system/camera-streamer.service
36+
cp /files/camera-streamer/camera-streamer-libcamera.service /etc/systemd/system/camera-streamer-libcamera.service
37+
cp /files/camera-streamer/camera-streamer-usb@.service /etc/systemd/system/camera-streamer-usb@.service
4238

39+
# Only enable the camera-streamer service, it will start the others as configured
4340
systemctl enable camera-streamer.service
4441

4542
# install helper scripts
@@ -50,7 +47,48 @@ chmod +x /root/bin/add-usb-camera
5047
chmod +x /root/bin/remove-usb-camera
5148
chmod +x /root/bin/list-usb-cameras
5249

53-
# clean up the old crap
50+
# clean up the old stuff
51+
cat > /boot/octopi.txt <<EOF
52+
### IMPORTANT: Looking for the camera settings? Those are now located
53+
### in the camera-streamer directory! libcamera.conf to configure the Raspberry Pi
54+
### camera and usb-default.conf to configure USB cameras. Read more about it here:
55+
### https://faq.octoprint.org/camera-streamer-config
56+
57+
### Windows users: To edit this file use Notepad++, VSCode or SublimeText.
58+
### Do not use Notepad or WordPad.
59+
60+
### MacOSX users: If you use Textedit to edit this file make sure to use
61+
### "plain text format" and "disable smart quotes" in "Textedit > Preferences"
62+
63+
# Configuration of network monitoring
64+
#
65+
# This enables network monitoring for wifi connections with a simple ping test.
66+
# If connection terminates by variable reasons system tries to restart the wifi connection to reestablish a connection.
67+
# The connection test is done every minute.
68+
# By default it is disabled (0 = off / 1 = on)
69+
# destination_host can be an ip address or a hostname (for hostname ensure dns resosultion is working correctly)
70+
71+
enable_network_monitor=0
72+
73+
# Be sure to change this to match your network!
74+
destination_host=192.168.1.1
75+
76+
### EXPERIMENTAL
77+
# Support for different streamer types.
78+
#
79+
# Available options:
80+
# mjpeg [default] - camera-streamer
81+
# hls - FFMPEG HLS streamer
82+
#camera_streamer=mjpeg
83+
EOF
84+
85+
sed -i 's/webcamd\.service/camera-streamer.service/' /etc/systemd/system/streamer_select.service
86+
sed -i 's/webcamd\.service/camera-streamer.service/' /root/bin/streamer_select
87+
5488
systemctl disable webcamd
89+
90+
rm /etc/logrotate.d/webcamd
91+
rm /etc/systemd/system/webcamd.service
5592
rm /root/bin/webcamd
5693
rm -rf /opt/mjpg-streamer
94+
rm /home/pi/mjpg-streamer

scripts/files/camera-streamer/add-usb-camera

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if [ -z "$NAME" ]; then
1717
fi
1818

1919
# make sure the name is unique
20-
if [ -e "/boot/camera-streamer/$NAME.conf" ]; then
20+
if [ -e "/boot/camera-streamer/usb-$NAME.conf" ]; then
2121
echo "Camera $NAME already exists"
2222
exit 1
2323
fi
@@ -63,7 +63,7 @@ if [ ! -e "$DEVICE" ]; then
6363
exit 1
6464
fi
6565

66-
cat > /boot/camera-streamer/$NAME.conf <<EOF
66+
cat > /boot/camera-streamer/usb-$NAME.conf <<EOF
6767
### Options for USB based cameras
6868
6969
# The port on which the webcam server for the camera should listen on.
@@ -92,4 +92,4 @@ echo "Camera $NAME added"
9292
echo
9393
echo "Port: $PORT"
9494
echo "Device: $DEVICE"
95-
echo "Configuration file: /boot/camera-streamer/$NAME.conf"
95+
echo "Configuration file: /boot/camera-streamer/usb-$NAME.conf"
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,49 @@
11
#!/bin/bash
22

3+
if [ "$EUID" -ne 0 ]
4+
then echo "Please run as root/with sudo"
5+
exit 1
6+
fi
7+
38
action=$1
49
single=$2
510

611
[ -n "$single" ] || systemctl $action "camera-streamer-libcamera.service"
712

813
# iterate over all the camera confs in /boot/camera-streamer
9-
for conf in /boot/camera-streamer/*.conf; do
10-
# skip the libcamera.conf
11-
if [ "$conf" = "/boot/camera-streamer/libcamera.conf" ]; then
12-
continue
13-
fi
14-
14+
for conf in /boot/camera-streamer/usb-*.conf; do
1515
# get the name of the camera
1616
name=$(basename "$conf" .conf)
17+
name=${name#usb-}
1718

1819
if [[ "$single" != "" ]] && [[ "$single" != "$name" ]]; then
1920
# if we are only doing a single camera, skip the others
2021
continue
2122
fi
2223

2324
source $conf
25+
[ -n "$DEVICE" ] || continue
2426

2527
if [[ "$action" == "start" ]] || [[ "$action" == "restart" ]]; then
26-
# generate some additional files/config if we are starting this for the first time
27-
reload="no"
28-
2928
# Add runtime path unit file that starts the service when the device is plugged in
3029
path_dir="/run/systemd/system"
31-
if [[ ! -e "$path_dir/camera-streamer-usb-$name.path" ]] || [[ ! $(grep -q "PathExists=$DEVICE" "$path_dir/camera-streamer-usb-$name.path") ]]; then
30+
if ! grep -q "PathExistsGlob=$DEVICE" "$path_dir/camera-streamer-usb-$name.path"; then
31+
echo "Adding path unit for autolaunch of camera-streamer-usb@$name"
3232
cat > "$path_dir/camera-streamer-usb-$name.path" <<EOF
3333
[Unit]
3434
Description=camera-streamer $name autolaunch
3535
After=network.target
3636
3737
[Path]
38-
PathExists=$DEVICE
38+
PathExistsGlob=$DEVICE
3939
Unit=camera-streamer-usb@$name.service
4040
EOF
41-
reload="yes"
42-
fi
43-
44-
# Add path dependency to service file via runtime override
45-
override_dir="/run/systemd/system/camera-streamer-usb@$name.service.d"
46-
override_conf="$override_dir/override.conf"
47-
if [[ ! -e "$override_conf" ]] || [[! $(grep -q "ConditionPathExists=$DEVICE" "$override_conf") ]]; then
48-
mkdir -p "$override_dir"
49-
cat > "$override_conf" <<EOF
50-
[Unit]
51-
ConditionPathExists=$DEVICE
52-
EOF
53-
reload="yes"
41+
systemctl daemon-reload || true
5442
fi
55-
[[ "$reload" == "yes" ]] && systemctl daemon-reload || true
5643
fi
5744

5845
# do the action
46+
echo "Running $action for camera-streamer-usb-$name.path..."
5947
systemctl $action "camera-streamer-usb-$name.path"
60-
systemctl $action "camera-streamer-usb@$name.service"
48+
echo "... done."
6149
done

scripts/files/camera-streamer/camera-streamer-libcamera.service

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[Unit]
22
Description=camera-streamer libcamera
33
After=network.target
4+
ConditionPathExists=/etc/octopi_streamer/mjpeg
45
StartLimitIntervalSec=120
56
StartLimitBurst=10
67

78
[Service]
89
EnvironmentFile=/boot/camera-streamer/libcamera.conf
910
ExecCondition=/bin/sh -c "/usr/bin/libcamera-hello --list-cameras | grep -o '/base/soc/[^)]*'"
11+
ExecCondition=/bin/sh -c "! ss -ltn src :$PORT | grep -q LISTEN"
1012
ExecStart=/bin/sh -c "exec /usr/local/bin/camera-streamer \
1113
-http-port=${PORT} \
1214
-camera-type=libcamera \

scripts/files/camera-streamer/camera-streamer-usb@.service

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
Description=camera-streamer %i
33
After=network.target
44
After=camera-streamer-libcamera.service
5-
ConditionPathExists=/boot/camera-streamer/%i.conf
5+
ConditionPathExists=/boot/camera-streamer/usb-%i.conf
66
ConditionPathExistsGlob=/dev/v4l/by-id/usb-*-video-index0
7+
ConditionPathExists=/etc/octopi_streamer/mjpeg
78
StartLimitIntervalSec=120
89
StartLimitBurst=10
910

1011
[Service]
11-
EnvironmentFile=/boot/camera-streamer/%i.conf
12+
EnvironmentFile=/boot/camera-streamer/usb-%i.conf
13+
ExecCondition=/bin/sh -c "[ -e $DEVICE ]"
14+
ExecCondition=/bin/sh -c "! ss -ltn src :$PORT | grep -q LISTEN"
1215
ExecStart=/bin/sh -c "exec /usr/local/bin/camera-streamer \
1316
-http-port=${PORT} \
14-
-camera-path="${DEVICE}" \
1517
-camera-type=v4l2 \
18+
-camera-path=$(ls -b $DEVICE | head -n1) \
1619
-camera-format=${FORMAT} \
1720
-camera-width=${WIDTH} -camera-height=${HEIGHT} \
1821
-camera-fps=${FRAMERATE} \

scripts/files/camera-streamer/camera-streamer.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[Unit]
2-
Description=camera-streamer arm usb
2+
Description=camera-streamer
33
After=network.target
4+
ConditionPathExists=/etc/octopi_streamer/mjpeg
45

56
[Service]
67
Type=oneshot

scripts/files/camera-streamer/remove-usb-camera

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ if [ -z "$NAME" ]; then
1212
exit 1
1313
fi
1414

15-
if [ ! -e "/boot/camera-streamer/$NAME.conf" ]; then
15+
if [ ! -e "/boot/camera-streamer/usb-$NAME.conf" ]; then
1616
echo "Camera $NAME does not exist"
1717
exit 1
1818
fi
1919

2020
/root/bin/camera-streamer-control stop $NAME
2121

22-
rm /boot/camera-streamer/$NAME.conf
22+
rm /boot/camera-streamer/usb-$NAME.conf
2323
rm /run/systemd/system/camera-streamer-usb-$NAME.path || true
24-
rm -r /run/systemd/system/camera-streamer-usb@$NAME.service.d || true
2524
systemctl daemon-reload

scripts/files/camera-streamer/usb.conf renamed to scripts/files/camera-streamer/usb-default.conf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
# camera will be considered the one with 8080.
66
PORT=8080
77

8-
# The path to the camera device. If you have only one camera, leave at /dev/video0.
9-
# Otherwise you'll need to find the path to the camera device. You can do this by
10-
# running the command 'ls echo /dev/v4l/by-id/usb-*-video-index0'.
11-
DEVICE=/dev/video0
8+
# The path to the camera device. Leave as is if you are using no or only one USB camera.
9+
DEVICE=/dev/v4l/by-id/usb-*-video-index0
1210

1311
# The image format of the camera.
1412
FORMAT=YUYV

0 commit comments

Comments
 (0)