Kameraserver mit Raspberry Pi

Livestream als RTSP, RTMP, HLS und WebRTC. Codecs: H264, VP8, VP9, AV1, MPEG2, M-JPEG, MP3, MPEG4 Audio (AAC), Opus, G711, G722, LPCM und RTP-kompatible Codecs.

Hardware

Raspberry Pi® 2 Model B mit 1 GB RAM
8 GB microSD Karte
Raspberry Camera Module v1
Netzwerkkabel

Software

Betriebssystem

Raspberry Pi OS Lite mit Debian version 11 (bullseye)
Download: https://www.raspberrypi.com/software/operating-systems

Streaming Server

RtspSimpleServer https://github.com/aler9/rtsp-simple-server

Installationsanleitung

SD-Karte flashen https://www.raspberrypi.com/software/

Kamera testen

Netzwerk konfigurieren

SSH Server installieren

Hostnamen ändern

Updates einspielen

Neu starten

RtspSimpleServer installieren

Fertig!

Kamera testen

libcamera-still --list-cameras
Available cameras
-----------------
0 : ov5647 [2592x1944] (/base/soc/i2c0mux/i2c@1/ov5647@36)
    Modes: 'SGBRG10_CSI2P' : 640x480 [58.92 fps - (16, 0)/2560x1920 crop]
                             1296x972 [43.25 fps - (0, 0)/2592x1944 crop]
                             1920x1080 [30.62 fps - (348, 434)/1928x1080 crop]
                             2592x1944 [15.63 fps - (0, 0)/2592x1944 crop]

Netzwerk konfigurieren

Im DHCP-Server der MAC Adresse ether eine statische IP-Sdresse inet zuweisen

ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.11  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::aad6:c184:b6c:28d1  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:11:22:33  txqueuelen 1000  (Ethernet)
        RX packets 7514  bytes 874778 (854.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4199  bytes 425351 (415.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 10  bytes 1600 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 1600 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Im Nameserver der IP-Adresse einen Namen und Reverse-Adresse zuweisen

A-Record

vi /var/lib/bind/lan.hosts
cam1.lan.       IN      A       192.168.0.11

PTR-Record

vi /var/lib/bind/192.168.0.rev
11.0.168.192.in-addr.arpa.      IN      PTR     cam1.lan.

Zone anwenden

service bind9 restart

Der Kameraserver ist jetzt unter dem DNS-Namen cam1.lan erreichbar.

host cam1.lan
cam1.lan has address 192.168.0.11

SSH Server installieren

sudo raspi-config
3 Interface Options    Configure connections to peripherals

I2 SSH           Enable/disable remote command line access using SSH
netstat -nulat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0    172 192.168.0.11:22         192.168.0.4:48096       ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp        0      0 0.0.0.0:41142           0.0.0.0:*
udp        0      0 0.0.0.0:5353            0.0.0.0:*
udp6       0      0 :::46776                :::*
udp6       0      0 :::5353                 :::*

Hostnamen ändern

sudo hostnamectl set-hostname cam1
 sudo nano /etc/hosts
127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       cam1

Updates einspielen

sudo apt update && sudo apt upgrade -y

Neu starten

sudo reboot

Kernelversion testen

uname -r
5.15.84-v7+

RtspSimpleServer installieren

Download: https://github.com/aler9/rtsp-simple-server/releases

rtsp-simple-server_*_linux_armv7.tar.gz ist für Raspberry Pi 2 (32 bit)
rtsp-simple-server_*_linux_arm64v8.tar.gz ist für Raspberry Pi 3 und 4 (64 bit)

Entpacken

tar -xzvf rtsp-simple-server_v0.21.2_linux_armv7.tar.gz

Verschieben

sudo mv rtsp-simple-server /usr/local/sbin
sudo mv rtsp-simple-server.yml /etc

Kamera aktivieren

In der Datei /etc/rtsp-simple-server.yml nach der Zeile paths: den Pfad zur Kamera hinzufügen

sudo nano /etc/rtsp-simple-server.yml
# Path parameters

# These settings are path-dependent, and the map key is the name of the path.
# It's possible to use regular expressions by using a tilde as prefix.
# For example, "~^(test1|test2)$" will match both "test1" and "test2".
# For example, "~^prefix" will match all paths that start with "prefix".
# The settings under the path "all" are applied to all paths that do not match
# another entry.
paths:

  cam:
    source: rpiCamera

  all:
    # Source of the stream. This can be:

Als Dienst starten

Neue Datei anlegen

sudo nano /etc/systemd/system/rtsp-simple-server.service
[Unit]
Wants=network.target
[Service]
ExecStart=/usr/local/sbin/rtsp-simple-server /etc/rtsp-simple-server.yml
[Install]
WantedBy=multi-user.target

Dienst beim Booten automatisch starten

sudo systemctl daemon-reload
sudo systemctl enable rtsp-simple-server
Created symlink /etc/systemd/system/multi-user.target.wants/rtsp-simple-server.service → /etc/systemd/system/rtsp-simple-server.service.

Dienst starten

service rtsp-simple-server start
service rtsp-simple-server status
● rtsp-simple-server.service
     Loaded: loaded (/etc/systemd/system/rtsp-simple-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-01-30 02:57:49 GMT; 5min ago
   Main PID: 609 (rtsp-simple-ser)
      Tasks: 18 (limit: 1596)
        CPU: 2min 139ms
     CGroup: /system.slice/rtsp-simple-server.service
             ├─609 /usr/local/sbin/rtsp-simple-server /etc/rtsp-simple-server.yml
             └─622 /dev/shm/rtspss-embeddedexe-1675047469762345766

Jan 30 02:57:49 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:49 INF [path cam] [rpicamera source] started
Jan 30 02:57:49 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:49 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
Jan 30 02:57:49 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:49 INF [RTMP] listener opened on :1935
Jan 30 02:57:49 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:49 INF [HLS] listener opened on :8888
Jan 30 02:57:49 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:49 INF [WebRTC] listener opened on :8889 (HTTP)
Jan 30 02:57:49 cam1 rtsp-simple-server[622]: [0:03:28.870822028] [622]  INFO Camera camera_manager.cpp:299 libcamera v0.0.3+40-9b860a66
Jan 30 02:57:49 cam1 rtsp-simple-server[622]: [0:03:28.966620562] [623]  INFO RPI raspberrypi.cpp:1425 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
Jan 30 02:57:49 cam1 rtsp-simple-server[622]: [0:03:28.969219526] [622]  INFO Camera camera.cpp:1028 configuring streams: (0) 1920x1080-YUV420
Jan 30 02:57:49 cam1 rtsp-simple-server[622]: [0:03:28.970944634] [623]  INFO RPI raspberrypi.cpp:805 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 1920x1080-SGBRG10_1X10 - Selected unicam format: 1920x1080-pGAA
Jan 30 02:57:50 cam1 rtsp-simple-server[609]: 2023/01/30 02:57:50 INF [path cam] [rpicamera source] ready: 1 track (H264)

Ports benennen

Einträge in der Datei /etc/services hinzufügen

sudo nano /etc/services
rtmp            1935/tcp                        # rtsp-simple-server
rtp             8000/udp                        # rtsp-simple-server
rtcp            8001/udp                        # rtsp-simple-server
rtsp            8554/tcp                        # rtsp-simple-server
hls             8888/tcp                        # rtsp-simple-server
webrtc          8889/tcp                        # rtsp-simple-server

Lauschende Ports anzeigen

netstat -lautp|grep rtsp
tcp6       0      0 [::]:webrtc         [::]:*          LISTEN      977/rtsp-simple-ser
tcp6       0      0 [::]:hls            [::]:*          LISTEN      977/rtsp-simple-ser
tcp6       0      0 [::]:rtsp           [::]:*          LISTEN      977/rtsp-simple-ser
tcp6       0      0 [::]:rtmp           [::]:*          LISTEN      977/rtsp-simple-ser
udp6       0      0 [::]:rtp            [::]:*                      977/rtsp-simple-ser
udp6       0      0 [::]:rtcp           [::]:*                      977/rtsp-simple-ser

Fertig!

Der Livestream ist jetzt online erreichbar über

WebRTC
http://cam1.lan:8889/cam
HLS
http://cam1.lan:8888/cam/
RTSP
vlc rtsp://cam1.lan:8554/cam
RTMP
vlc rtmp://cam1.lan/cam