Install GStreamer 1.18 on Raspberry Pi 4 - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
Install GStreamer on Raspberry Pi 4

Install GStreamer 1.18 on Raspberry Pi 4.

Introduction.

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.
Version 1.18.
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.

Preparation.

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

Installation.

Next, install the core GStreamer libraries.
Core
# 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       \
        --wrap-mode=nofallback \
        -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.

Meson_Rdy_1

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
Meson_Test_1

OS check.
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.

Version_32_64

Plugins-base.

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.
Plugins Base
$ cd ~
# download and unpack the plug-ins base
$ wget https://gstreamer.freedesktop.org/src/gst-plugins-base/gst-plugins-base-1.18.4.tar.xz
$ sudo tar -xf gst-plugins-base-1.18.4.tar.xz
# make an installation folder
$ cd gst-plugins-base-1.18.4
$ mkdir build
$ cd build
# run meson
$ meson --prefix=/usr \
-D gl_winsys=wayland \
-D buildtype=release \
-D package-origin=https://gstreamer.freedesktop.org/src/gstreamer/ ..
$ ninja -j4
# optional
$ ninja test
# install the libraries
$ sudo ninja install
$ sudo ldconfig
Plugins Base
$ cd ~
# download and unpack the plug-ins base
$ wget https://gstreamer.freedesktop.org/src/gst-plugins-base/gst-plugins-base-1.18.4.tar.xz
$ sudo tar -xf gst-plugins-base-1.18.4.tar.xz
# make an installation folder
$ cd gst-plugins-base-1.18.4
$ mkdir build
$ cd build
# run meson
$ meson --prefix=/usr \
-D buildtype=release \
-D package-origin=https://gstreamer.freedesktop.org/src/gstreamer/ ..
$ ninja -j4
# optional
$ ninja test
# install the libraries
$ sudo ninja install
$ sudo ldconfig
Not all tests will pass on a 64-bit system. This is normal due to unsupported features in the Wayland protocol. The screen dump below shows the result of the 32-bit version.

Meson_Test_1

Plugins-good.

The last step is the installation of the plugins-good package.
Plugins Good
$ cd ~
# download and unpack the plug-ins base
$ wget https://gstreamer.freedesktop.org/src/gst-plugins-good/gst-plugins-good-1.18.4.tar.xz
$ sudo tar -xf gst-plugins-good-1.18.4.tar.xz
# make an installation folder
$ cd gst-plugins-good-1.18.4
$ mkdir build
$ cd build
# run meson
$ meson --prefix=/usr       \
       -D buildtype=release \
       -D package-origin=https://gstreamer.freedesktop.org/src/gstreamer/ \
       -D package-name="GStreamer 1.18.4 BLFS" ..
$ ninja -j4
# optional
$ ninja test
# install the libraries
$ sudo ninja install
$ sudo ldconfig

Plugins-bad.

There are two additional packages that you should be aware of. The plugins-bad does not meet the same high quality standard as the other pacakges. They may not have been thoroughly tested, or some documentation is missing or in progress. Commonly used format conversion tools, such as h264parse or matroskamux, can be found in this plugin. Keep in mind that the Raspberry Pi 4 still has modest computing power, so converting high frame rates live from MPEG to Matroska will give disappointing results.
Plugins-ugly.
The other package is the plugins ugly. The code is good but may have problems distributing. There may also be patent issues with the libraries on where the plugin depends. See this GStreamer site for more information.

Both packages can be installed on the Raspberry Pi 4 without any problem. The procedure is identical to the plugins-good. You only need to replace the name good to bad or ugly in the command lines.

Over time, a needed module may not be present in the current installation. If all four plugins are installed, chances are the underlying library isn't installed on your Raspberry Pi. You need to install the required library first and then rebuild the plugin. As an example, the missing x264enc module, found in the ugly package, is installed below. Note that there is no need to recompile OpenCV in this case.
# test if the module exists (for instance x264enc)
$ gst-inspect-1.0 x264enc
# if not, make sure you have the libraries installed
# stackoverflow is your friend here
$ sudo apt-get install libx264-dev
# check which the GStreamer site which plugin holds the module
# rebuild the module (in this case the ugly)
$ cd gst-plugins-ugly-1.18.4
# remove the previous build
$ rm-rf build
# make a new build folder
$ mkdir build && cd build
$ meson --prefix=/usr       \
      -D buildtype=release \
      -D package-origin=https://gstreamer.freedesktop.org/src/gstreamer/ \
      -D package-name="GStreamer 1.18.4 BLFS" ..
$ ninja -j4
$ sudo ninja install
$ sudo ldconfig

Testing.

With the three packages installed, you can test GStreamer 1.18.4 on your Raspberry Pi 4. The screen dump below shows the installed version and a test pipeline $ gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink to see if everything works.

Test_32

Unfortunately, the rpicamsrc module only works with a 32-bit operating system because the userland GPU interface does not fully support the new 64-bit operating system yet. Several modules have already been ported to 64 bits. However, the mmal core, which GStreamer needs, is still in the 32-bit version. We can still use GStreamer with all its features, but as source we need to use the old fashion /dev/video0 declaration.
32-bit OS testing.
The next test is to see the rpicamsrc working. If the $ gst-inspect-1.0 rpicamsrc command can be executed, it will certainly work correctly.

InspectRPiCam

Last test is the Raspicam projecting a preview on the screen by executing the following command.
$ gst-launch-1.0 -v rpicamsrc preview=true ! fakesink
Basically, the rpicamsrc has almost the same properties as the raspivid application. More information about the rpicamsrc can be found on the GStreamer site.
64-bit OS testing.
The 64-bit version uses the v4l2src module. First, see if the module is correctly installed with the command $ gst-inspect-1.0 v4l2src.

InspectV4L2Cam

If the Raspicam is connected and enabled in the Raspberry Pi Configurations, you will get the following screen when probing /dev/video0, indicating the camera is working.

InspectDevCam

The last test is, of course, a preview on the screen. It's done with the following rather large pipeline.
Raspicam 32 and 64-bit OS
$ gst-launch-1.0 v4l2src device=/dev/video0 ! \
                 video/x-raw, width=1280, height=720, framerate=30/1 ! \
                 videoconvert ! videoscale ! \
                 video/x-raw, width=640, height=360 ! \
                 autovideosink

Cleaning.

Once you are certain GStreamer is working well, you can remove all the code and zip files.
# remove the core code and zip files
$ rm gstreamer-1.18.4.tar.xz
$ rm -rf gstreamer-1.18.4
# remove the plugins
$ rm -rf gst-plugins*

OpenCV.

For OpenCV to work with GStreamer 1.18.4, it must be recompiled, even if OpenCV was built with GStreamer 1.14. An OpenCV with version 1.14 will not work with the newly installed 1.18 version.
Please follow the steps given in our tutorials. The only difference is setting the -D WITH_GSTREAMER=ON argument in the build.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
   ..................        

        -D WITH_GSTREAMER=ON \

        ..................        

        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D BUILD_EXAMPLES=OFF ..
OpenCV_GStreamer

If the generated build information shows the detected GStreamer version, you can be confident OpenCV will support the new version once build and install. On our GitHub page you can find a simple GStreamer example with the Raspicam for a Raspberry Pi 4 32 or 64-bit OS.

WebCam.

GStreamer can also work with a USB webcam. There are two points to keep in mind.

First, there is the address of the camera. The Raspicam will always occupy /dev/video0 once enabled. All other USB cameras use the following numbers. So the first USB camera has /dev/video1, the next one /dev/video2 and so on. If there is no Raspicam enabled, then, of course, the numbering starts at /dev/video0.

The second point is the available output formats the USB webcam supports. Below an example of a popular Logitech c720 webcam is shown.

Logitech_G1

Logitech_G2

Logitech_G3

The source format is declared in the pipeline. It is the first part of the command, starting with video/x-raw, width=1280, height=720, framerate=30/1. Choose only a format supported by your webcam. In the list of the Logitech c720, you will find formats such as 640x480@30 FPS or 1280x960@5 FPS. Other formats give an error. The same goes for the MJPG formats. We specified that the source is video/x-raw, not video/x-mpeg. Using the MPEG stream is more for advanced users. Especially on the limited resources of the Raspberry Pi 4.
GitHub examples
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content