Install GStreamer 1.18 on Raspberry Pi 4.
This page walks you through the installation of GStreamer 1.18 on a Raspberry Pi 4. GStreamer is a pipeline-based multimedia framework that links various media processes to a complex workflow. For example, with a single line of code, it can retrieve images from a camera, convert them to Mpeg, and send them as UDP packets over Ethernet to another computer. Obviously, GStreamer is complex software used by more advanced programmers. If you don't have a compelling reason to use it, it is better to use the simple OpenCV framework.
One of the main reasons for using GStreamer is the lack of latency. The OpenCV video capture module uses large video buffers that hold the frames. For example, if your camera has a frame rate of 30 FPS and your image processing algorithm can handle a maximum of 20 FPS, the synchronisation is lost very quickly due to the stacking in the video buffer. The absence of buffer flushing makes things even worse.
In this situation, GStreamer comes to the rescue. With the buffering of just one frame, you will always get an actual frame as output.
With version 1.18, GStreamer fully supports the Raspicam, thanks to the impressive work of Jan Schmidt (thaytan). Unfortunately, the Raspberry Pi repositories only support the older 1.14 version of GStreamer. Not only the Raspicam support but also the many bug fixes make version 1.18 superior to 1.14. It's best to install version 1.18.4 from scratch on your Raspberry Pi 4.
By the way, keep in mind that you will have to reinstall OpenCV once GStreamer 1.18 is installed.
You have to install at least three GStreamer packages, the core gstreamer, the plugins-base and the plugins-good. As mentioned, OpenCV has to be rebuilt also, after installing GStreamer. But first, remove the old GStreamer version, if available. Two versions on the same machine are the shortcut to disaster.
# remove the old version
$ sudo rm -rf /usr/bin/gst-*
$ sudo rm -rf /usr/include/gstreamer-1.0
# install a few dependencies
$ sudo apt-get install cmake meson
$ sudo apt-get install flex bison
Next, install the core GStreamer libraries.
# download and unpack the lib
$ wget https://gstreamer.freedesktop.org/src/gstreamer/gstreamer-1.18.4.tar.xz
$ sudo tar -xf gstreamer-1.18.4.tar.xz
# make an installation folder
$ cd gstreamer-1.18.4
$ mkdir build
$ cd build
# run meson (a kind of cmake)
$ meson --prefix=/usr \
-D buildtype=release \
-D gst_debug=false \
-D package-origin=https://gstreamer.freedesktop.org/src/gstreamer/ \
-D package-name="GStreamer 1.18.4 BLFS" ..
If everything went well, you end up with the screen below.
Now build and test GStreamer with the next commands.
# build the software
$ ninja -j4
# test the software (optional)
$ ninja test
# install the libraries
$ sudo ninja install
$ sudo ldconfig
Please check your operating system to be sure, before the next step. Run the command uname -a and verify your version with the screen dump below.
Once the core libraries are installed, the next step is to install the two additional plugins. Both follow the same procedure as above, so we'll give the installation without much explanation.