Monday, March 21, 2022

Setup a Raspberry Pi 4B to publish a webcam with hardware accelerated video encoding

The Raspberry Pi 4B has an integrated video processing unit, a so called GPU. I wanted to use the GPU to offload the video encoding processing from the CPU when I publish my webcam as a RTSP video stream from the Pi board running Ubuntu 20.04 64 bit and using ffmpeg

Download and install rtsp-simple-server

  1. Using a browser, download and extract the open source rtsp-simple-server from https://github.com/aler9/rtsp-simple-server into a folder, e.g. /path/to/rtsp/

    The files rtsp-simple-server and rtsp-simple-server.yml are extracted out.

  2. Open up a Terminal. At the prompt, change directory to the previously created folder /path/to/rtsp/.

    $ cd /path/to/rtsp/

  3. Move the binary to the folder /usr/local/bin/

    $ sudo mv rtsp-simple-server /usr/local/bin/

  4. Move the yml configuration file to the folder /usr/local/etc/

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

Configure rtsp-simple-server to publish the webcam from ffmpeg

  1.  Open up a Terminal. At the prompt, change directory to the directory /usr/local/etc/.

    $ cd /usr/local/etc

  2. Using a text editor, edit the configuration file rtsp-simple-server.yml. Create a new path name, e.g. cam under the paths key.

    paths:
      cam:
        runOnDemand: ffmpeg -hide_banner -s 1280x720 -r 25 -i /dev/video0 -b:v 2M -c:v h264_v4l2m2m -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH
        runOnDemandRestart: yes
    

    Notes:
    -s 1280x720 is the source video resolution size from the webcam
    -r 25 is the source sample rate
    -i /dev/video0 is the source webcam video
    -b:v 2M is the output video bit rate
    -c:v h264_v4l2m2m is the video encoder to use


  3. Save and close the configuration file.

 Create and start the rtsp-simple-server service

  1.  Open up a Terminal. Type in the following to create the systemd service.

    sudo tee /etc/systemd/system/rtsp-simple-server.service >/dev/null << EOF
    [Unit]
    After=network.target
    [Service]
    ExecStart=/usr/local/bin/rtsp-simple-server /usr/local/etc/rtsp-simple-server.yml
    [Install]
    WantedBy=multi-user.target
    EOF
    

  2. Enable and start the service by executing the following commands. Or reboot the system.

    $ sudo systemctl enable rtsp-simple-server
    $ sudo systemctl start rtsp-simple-server

Viewing the webcam stream using VLC

  1. If the configuration parameters are correct, then start up VLC on a PC on the network.

    VLC starts up.


  2. Select Media | Open Network Stream.

    The Open Media dialog box appears.


  3. In the URL field, type in the address of the Raspberry Pi e.g. rtsp://192.168.18.5:8554/cam. Then click Play.

    If all the parameters are correct, then VLC should show a stream from the webcam.

    VLC displaying a webcam stream.


    Note: if the video stream looks wrong i.e. greenish and weird as shown above, then the ffmpeg version installed on the Raspberry Pi is an older buggy version 4.2.4 and you need to replace it by downloading the latest ffmpeg and building from source.

Replacing ffmpeg and build from source

  1. Open up a Terminal and remove the system installed ffmpeg with the following command:

    $ sudo apt remove ffmpeg

  2. Using a browser and follow the instructions on https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu to install the latest ffmpeg.

  3. Compilation on the Raspberry Pi 4B may take a while, maybe an hour or two so be aware. After compiling successfully, you can try to use VLC and open up the webcam steam again.

    VLC displaying the webcam stream correctly.

No comments:

Post a Comment