December 22, 2024

Easiest way to hardware accelerated RTSP streaming with Raspberry PI 4

Hello everyone and thank you for your visit. As I was pretty busy lately I still want to share an article about my experience with realtime streaming on Raspberry PI 4. As the holiday season approached, I wanted to have a webcam streaming live from my place in Switzerland, not just for the sake of it, but also to learn a bit of the inner workings of the device and about multimedia in general. While studying the offerings on the market related to video cameras, I noticed that every camera has a software of its own and you generally don’t have access to the video stream. Although most decent cameras come with RTSP protocol enabled, the camera that I bought didn’t have this option. So I had to rely on an USB videocam to implement the live streaming solution.

Update Feb 2023

If you want hardware acceleration with the latest RPI OS (64 bit), I wrote this article for you. Although the steps outlined here regarding configuration are still valid, the usage of the OMX codec is deprecated and I addressed this change in the new article.

What you will need

The list is not too long:

  1. A Raspberry PI model 4 with either 4GB or 8GB of RAM
  2. A USB camera or a camera that supports RTSP through the network
  3. A microSD card to install the right OS for Raspberry (at least 8GB in size)
  4. SSH access enabled after installing the RPI OS with SSH keys configured for connection (Please refer to this article to guide you through)
  5. A bit of understanding of this software Simple RTSP Server
  6. Either your own Linux server accessible over the internet or an account on a streaming platform like rtsp.me or streamingvideoprovider.com
  7. Clone this repo on your machine. In this repo there are 2 apps one for the Raspberry with the RTSP Server compiled for ARM and the optional server app

Install the RaspberryPI OS

Insert the microSD card into your personal computer and use this official software to install the legacy version of the RPI OS.

IMPORTANT: Installing the PI OS Lite Legacy as in the image above is crucial because in the newer version they simply deprecated the support for the OMX codec which we’ll use to encode the stream and get the benefit of hardware acceleration.

After the SD card is ready, please insert it into the Raspberry and start the system. You can either use an external screen and keyboard or check inside your router to find the Raspberry IP address. Make sure you are able to connect to it through SSH. This is important because the repo that I will give you needs it. Check the article here for more SSH info.

Setup PM2 and deploy the Raspberry app

Cool, now the fun part begins! The commands I’m running here work only for Linux machines with Powershell installed. Otherwise, you’ll need to manually run the scp commands.

Now to the business: in the folder where you cloned this repository open a terminal to app-raspi folder then run these commands:

$ ./setup.ps1 <IP_ADDRESS>
$ ./setup-pm2.ps1 <IP_ADDRESS>

Replace IP_ADDRESS with the IP address of your Raspberry. Now you should have a running app that is managed by the awesome PM2 and starts on reboot as well.

Configuration of the Raspberry app

There can be 2 configurations depending on the camera type: USB or RTSP on local network. The difference is not so big it’s just a matter of modifying the rtsp-simple-server.yml config file. We will use FFMPEG which is a versatile solution for encoding and streaming multimedia. The app is installed by the setup script. If you do this tutorial manually, make sure you install the ffmpeg package manually on Raspberry.

USB Camera

For an USB camera, plug it into the Raspberry then make sure you have something like this in the config file:

ffmpeg -f v4l2 -i /dev/video0 -c:v h264_omx -preset medium -tune zerolatency -b:v 500k -f rtsp rtsp://output-url

Replace output-url to match the internet server url to which to push the stream to.

RTSP local network camera

For a network RTSP camera, you need to set it up according to the user’s manual. Then make sure you have something like this in the config file:

ffmpeg -rtsp_transport tcp -i rtsp://input-url -c:v h264_omx -preset medium -tune zerolatency -b:v 800k -s 1280:720 -crf 18 -f rtsp rtsp://output-url

Replace input-url to be the RTSP url of the camera you set up and output-url to match the internet server url to which to push the stream to.

Once you have this config in place, you always need to redeploy in order for it to take effect:

$ ./deploy.ps1 <IP_ADDRESS>

How to watch the stream

When it comes to watching the stream, basically you can use just any app that supports RTSP cameras on your device. There you need to add your RTSP stream url and you’re ready to go. I personally use IPCams on my iOS device.

Sample of an RTSP stream from IPCams

Optional – stream from RPI to your own server

If you have your own server over the internet (I’m assuming Ubuntu or Debian based), then you can use the same scripts to deploy the app-server application. You can then configure it to allow publishing with a user/password and configure access credentials for reading the stream as well. I do encourage you to read through the Simple RTSP Server page.

Conclusion

It was pretty complicated to understand and basically grasp all the know-how I’ve gathered here. I tried lots of configurations, codecs and setups until I stabilized the whole thing. This mostly happens because when it comes to elegance, performance and stability, I make no compromise. This is how this solution is also working in a hardware accelerated way provided that you use the OMX codec when you call FFMPEG. I still think there is a lot to improve, hence feel free to leave a comment or contact me if you have any questions 😊

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

Since it’s the season to be jolly, I wish you Happy Holidays and have a great year ahead! 🎅🏻😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →