Skip to content

Advanced Settings

Tzu Huan Tai edited this page Feb 3, 2025 · 14 revisions

Outline

Using the Legacy V4L2 Driver

  • For Libcamera users (e.g., Camera Module 3), skip this step and add --use_libcamera to your command.
  • For V4L2 users, modify /boot/firmware/config.txt to enable the legacy driver:
    # camera_auto_detect=1  # Default setting
    camera_auto_detect=0    # Read camera by v4l2
    start_x=1               # Enable hardware-accelerated
    gpu_mem=128             # Adjust based on resolution; use 256MB for 1080p and higher.

Tip

How do I know if I should choose V4L2 or Libcamera for my camera?
V4L2 is typically used with older cameras that don’t require specific drivers. Libcamera supports newer official Raspberry Pi Camera Modules, like Camera Module 3. If you are unsure, start with Libcamera.

Here is a example command for V4L2 camera:

./pi_webrtc --device=/dev/video0 --v4l2_format=h264 --fps=30 --width=1280 --height=960 --hw_accel --no_audio --mqtt_host=your.mqtt.cloud --mqtt_port=8883 --mqtt_username=hakunamatata --mqtt_password=Wonderful --uid=your-custom-uid

Caution

When setting 1920x1080 with the legacy V4L2 driver, the hardware decoder firmware may adjust it to 1920x1088, while the ISP/encoder remains at 1920x1080 on the 6.6.31 kernel. This may cause memory out-of-range issues. Setting 1920x1088 resolves this issue.

Run as Linux Service

1. Run pulseaudio as system-wide daemon [ref]:

  • Create a service file /etc/systemd/system/pulseaudio.service
    sudo nano /etc/systemd/system/pulseaudio.service
  • Copy the following content:
    [Unit]
    Description= Pulseaudio Daemon
    After=rtkit-daemon.service systemd-udevd.service dbus.service
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
  • Run the command to add a autospawn = no in the client config
    echo 'autospawn = no' | sudo tee -a /etc/pulse/client.conf > /dev/null
  • Add root to the pulse group
    sudo adduser root pulse-access
  • Enable and start the Service
    sudo systemctl daemon-reload
    sudo systemctl enable pulseaudio.service
    sudo systemctl start pulseaudio.service

2. In order to run pi_webrtc and ensure it starts automatically on reboot:

  • Create a service file /etc/systemd/system/pi-webrtc.service
    sudo nano /etc/systemd/system/pi-webrtc.service
  • Modify WorkingDirectory and ExecStart to your settings:
    [Unit]
    Description= The p2p camera via webrtc.
    After=network-online.target pulseaudio.service
    
    [Service]
    Type=simple
    WorkingDirectory=/path/to
    ExecStart=/path/to/pi_webrtc --use_libcamera --fps=30 --width=1280 --height=960 --uid=your_uid --hw_accel --mqtt_host=example.s1.eu.hivemq.cloud --mqtt_port=8883 --mqtt_username=hakunamatata --mqtt_password=wonderful
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
  • Enable and start the Service
    sudo systemctl daemon-reload
    sudo systemctl enable pi-webrtc.service
    sudo systemctl start pi-webrtc.service

Recording

  • Find out where is your usb drive path, it might be /dev/sda1:
    sudo fdisk -l
    
  • To mount the /dev/sda1 USB disk at /mnt/ext_disk automatically when detected [ref]:
    sudo apt-get install autofs
    echo '/- /etc/auto.usb --timeout=5' | sudo tee -a /etc/auto.master > /dev/null
    echo '/mnt/ext_disk -fstype=auto,nofail,nodev,nosuid,noatime,async,umask=000 :/dev/sda1' | sudo tee -a /etc/auto.usb > /dev/null
    sudo systemctl restart autofs
  • Add --record_path followed by the path to the disk, as shown in the command below.
    /path/to/pi_webrtc ... --record_path=/mnt/ext_disk/video

Two-way communication

a microphone and speaker need to be added to the Pi. It's easier to plug in a USB mic/speaker. If you want to use GPIO, please follow the link below.

Microphone

Please see this link for instructions on wiring and testing your Pi.

Speaker

You can use the link for instructions on setting up a speaker on your Pi.

Virtual Camera

Sometimes, we need to enhance images, perform AI recognition, or preprocess them, and then stream the processed video to the client. In such cases, virtual cameras come in handy!

The concept is to read the raw video source from /dev/video0 or your specific stream, process the images, and then output the processed stream to a V4L2 loopback device (e.g., /dev/videoX).

  • Install packages

    sudo apt install v4l2loopback-dkms libopencv-dev python3-opencv python3-picamera2 ffmpeg
  • Run a virtual camera

    Output a yuv420(i420) format stream from /dev/video0 to the virtial device. See the full example, which create /dev/video9.

  • Run pi-webrtc

    Read the virtual device /dev/video9 and formats.

    /path/to/pi_webrtc --device=/dev/video9 --fps=30 --width=1280 --height=720 --v4l2_format=i420 ...

WHEP with Nginx proxy

  • Browsers require WebRTC connections to be built only when the website is served over https, so pi_webrtc also needs to be served over https. Below is an nginx.conf example using DDNS and Let's Encrypt, assuming your pi_webrtc is running with the --http_port=8080 flag and the hostname is example.ddns.net.
http {
    gzip on;
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

    access_log /var/log/nginx/access.log;

    server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name example.ddns.net;

        ssl_certificate /etc/letsencrypt/live/example.ddns.net/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.ddns.net/privkey.pem;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}
  • Run program
pi@home-pi-5:~ $ /path/to/pi_webrtc --fps=30 --width=2560 --height=1440 --http_port=8080 --uid=home-pi-5 --use_libcamera --no_audio
  • Play the stream via url in whep player
https://example.ddns.net/

Use WebRTC Camera in Home Assistant

This guide will walk you through setting up WebRTC Camera in Home Assistant and streaming live video using pi_webrtc on a Raspberry Pi.

1. Prepare the Environment

2. Install HACS (Home Assistant Community Store)

  • HACS allows you to install community-developed integrations like WebRTC Camera.

  • Follow the official HACS installation guide: How to Install HACS

    Screenshot 2025-02-03 043025

3. Install WebRTC Camera via HACS

  • Go to Home AssistantHACSIntegrations → Search for WebRTC Camera.

  • Restart Home Assistant after installation.

    Screenshot 2025-02-03 043256

4. Integrate WebRTC Camera in Home Assistant

  • Go to SettingsDevices & Services → click Add Integration.

    Screenshot 2025-02-03 045243

5. Run pi_webrtc on Raspberry Pi

  • Use HTTP signaling pi_webrtc and execute the following command to start streaming video:
    pi@home-pi-4b:~ $ /path/to/pi_webrtc --fps=30 --width=1280 --height=720 --http_port=8080 --uid=home-pi-4b --use_libcamera
  • The output URL will be exposed on port 8080, e.g., http://192.156.4.35:8080.

6. Display WebRTC Video in Home Assistant Dashboard

  • Go to Dashboard → Click Edit DashboardAdd Card → Select WebRTC Camera

    Screenshot 2025-02-03 043403

  • Enter the URL in the configuration and save:

    type: custom:webrtc-camera
    url: webrtc:http://192.168.4.35:8080

    Screenshot 2025-02-03 043600