/RaspberryPi_WebRTC

Native WebRTC uses v4l2 hardware h264 and software openh264 encoder for live streaming on Raspberry Pi.

Primary LanguageC++Apache License 2.0Apache-2.0

RaspberryPi_WebRTC

License

Pi 4b latency demo

Turn your Raspberry Pi into a low-latency home security camera using the v4l2 DMA hardware encoder and WebRTC. [demo video]

  • It's designed as a pure P2P-based camera that allows video playback and download without needing a media server.

  • Support multiple users to watch the live stream simultaneously.

  • Raspberry Pi 5 or other SBCs do not support v4l2 hardware encoding, please run this project in software encoding mode.

How to use

  • Download the latest binary file from Releases.
  • Install the Pi Camera app and follow the instructions.

Hardware Requirements

  • Raspberry Pi (Zero 2W or better).
  • CSI Camera Module.
  • At least 4GB micro sd card.
  • A USB disk and a Micro-USB Male to USB-A Female adaptor.

Environment Setup

  1. Use the Raspberry Pi Imager to write the Lite OS (Bookworm 64-bit) to the micro SD card.

  2. Install essential libs

    sudo apt install libmosquitto1 pulseaudio libavformat59 libswscale6
  3. Enable Raspberry Pi Hardware by adding below in /boot/firmware/config.txt

    camera_auto_detect=0
    start_x=1
    gpu_mem=16
    

    Set camera_auto_detect=0 in order to read camera by v4l2.

  4. Mount USB disk [ref]

    • Skip this step if you don't want to record videos. Don't set the record_path flag while running.
    • When the disk drive is detected, it will automatically mount to /mnt/ext_disk.
    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,umask=000 :/dev/sda1' | sudo tee -a /etc/auto.usb > /dev/null
    sudo systemctl restart autofs

Running the Application

MQTT is currently the only signaling mechanism used, so ensure that your MQTT server is ready before starting the application. If the application is only intended for use within a local area network (LAN), the MQTT server (such as Mosquitto) can be installed on the same Pi. However, if remote access is required, it is recommended to use a cloud-based MQTT server. Free plans include, but are not limited to, HiveMQ and EXMQ. Because If you choose to self-host and need to access the signaling server remotely via mobile data, you may need to set up DDNS and port forwarding if your ISP provides a dynamic IP.

Run

  • Running the binary file pi_webrtc with the -h flag will display all available options.

  • To start the application, use your settings and apply them to the following example command below. The SDP/ICE data will be transferred under the MQTT topic specified by your uid setting.

    # run the PulseAudio service if it is not started.
    pulseaudio --start
    
    # run main program with your settings
    /path/to/pi_webrtc --device=/dev/video0 --fps=30 --width=1280 --height=960 --v4l2_format=h264 --hw_accel --mqtt_host=example.s1.eu.hivemq.cloud --mqtt_port=8883 --mqtt_username=hakunamatata --mqtt_password=Wonderful --uid=home-pi-zero2w --record_path=/mnt/ext_disk/video/

    Hint 1: Since the Pi 5 does not support hardware encoding, please remove the --hw_accel flag and set --v4l2_format to mjpeg. Video encoding will be handled by OpenH264.

    Hint 2: I noticed that when I set 1920x1080, the hardware decoder firmware changes it to 1920x1088, but the isp/encoder does not adjust in the 6.6.31 kernel. This leads to memory being out of range. However, if I set 1920x1088, all works fine.

Run as Linux Service

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

  • create a file /etc/systemd/system/pulseaudio.service
    [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 cmd to add a autospawn = no in the client conf
    echo 'autospawn = no' | sudo tee -a /etc/pulse/client.conf > /dev/null
  • Add root to 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 with the following content:
    [Unit]
    Description= The p2p camera via webrtc.
    After=systemd-networkd.service
    
    [Service]
    Type=simple
    WorkingDirectory=/path/to
    ExecStart=/path/to/pi_webrtc --device=/dev/video0 --fps=30 --width=1280 --height=960 --v4l2_format=h264 --hw_accel --mqtt_host=example.s1.eu.hivemq.cloud --mqtt_port=8883 --mqtt_username=hakunamatata --mqtt_password=wonderful --record_path=/mnt/ext_disk/video/
    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

Advance

To enable two-way communication, a microphone and speaker need to be added to the Pi.

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.

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

Copyright 2022 Tzu Huan Tai (Author)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.