Module 6 · Lesson 58 of 108

GStreamer pipelines and tools

Build and run a GStreamer pipeline from the command line, inspect its elements, and verify the media format negotiated on each link.

  • Beginner to advanced
  • 7 min read
  • 11 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM, Bash, and the gstreamer1.0-tools and gstreamer1.0-plugins-base packages. No camera or graphical session is required.

Your place in the module
By the end you can 4 objectives
  • Identify the elements, pads, and bin in a simple pipeline.
  • Run a finite pipeline with gst-launch-1.0 and identify normal completion.
  • Use gst-inspect-1.0 to find element properties and pad capabilities.
  • Verify different pixel formats on either side of a video converter.
Video coming soonGStreamer pipelines and tools
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Capturing frames with V4L2
  • The format returned by VIDIOC_S_FMT describes the layout the driver accepted.
  • VIDIOC_QBUF gives the driver a buffer to fill, and VIDIOC_DQBUF returns a completed buffer to the application.
  • MMAP maps driver-allocated buffers, USERPTR supplies application memory, and DMABUF shares buffers through file descriptors.
01

Elements, pads, and pipelines

3 min read

Raw V4L2 capture is verbose; GStreamer composes media into pipelines, so this lesson introduces its elements and tools.

A raw frame needs a pixel format before a consumer can interpret its bytes. The Linux kernel documentation lists many formats precisely because the same image can be stored with different component orders and memory layouts. In GStreamer, the practical question is whether the producer and consumer agree on the format of the frames they exchange. You can investigate that question with generated video on Ubuntu, even over a terminal session with no camera attached.

An has a defined role. A source produces data. videotestsrc generates video frames containing a test pattern. A sink consumes data. fakesink accepts buffers and discards them, which lets you test whether data can reach the end of a pipeline. A transform consumes media and produces media after an operation. videoconvert can convert raw video between pixel formats. If both sides select the same format, it can pass the video through without converting it.

Elements connect through . An output is a source pad, usually labelled src in the elements used here. An input is a sink pad, labelled sink. A link connects a source pad to a sink pad. videotestsrc has a source pad, fakesink has a sink pad, and videoconvert has both. When you describe a linear pipeline, you can read each link in the direction that the frames travel.

Media links in a pipelinevideotestsrcSource padoutputsframesvideoconvertSink padin, sourcepad outfakesinkSink padreceivesframesvideotestsrcSource padoutputsframesvideoconvertSink padin, sourcepad outfakesinkSink padreceivesframes
The test source sends frames to the converter, which sends its output to the sink

A lets a group of connected elements be managed together. A bin can expose pads that give other elements access to endpoints inside the group. A coordinates state changes and provides a clock and a message bus. When the controlling program asks the pipeline to play, the pipeline manages the corresponding state changes of its children. The examples here use a linear graph. A pipeline can also contain branches and nested bins.

A carries the frame data through the linked pads in these examples. Buffers can also carry timing information such as a presentation timestamp and duration. The bus carries messages for the program controlling the pipeline. An element can post an error on the bus, and the pipeline can report that playback has reached the end of the stream. To find the pixel format of a frame, look at the caps on its link. To find out whether the pipeline reported an error, look at its messages.

02

Run a pipeline from Bash

3 min read

If the tools and base plugins are missing on Ubuntu 24.04, install them with:

bash
sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-base

Run a source followed by a sink:

bash
gst-launch-1.0 -m videotestsrc num-buffers=5 ! fakesink

gst-launch-1.0 creates and runs a pipeline from the description on the command line. Each element name selects the factory that creates the element. A property=value argument after an element name sets a property on that element. Here num-buffers=5 limits the source to five buffers. The standalone ! asks GStreamer to link the preceding element's output pad to the following element's input pad. The command runs as one program, and the links are GStreamer pad links inside it.

The -m option prints messages from the pipeline bus. You can see messages associated with state changes as the pipeline starts. Once the source has produced its five buffers, it sends an end-of-stream event downstream. After the sink receives the event and finishes processing the stream, the pipeline reports end of stream on the bus. gst-launch-1.0 then tears down the pipeline and returns control to the shell. Use end of stream, often abbreviated EOS, as the normal completion signal for this finite run. The sink discards the frames, so the command needs only a terminal.

A finite command-line runCreatepipelineBuildelementsand linkpadsProducefivebuffersSourcesends testframesReceive EOSeventSinkfinishesthe streamRead busmessageLauncherseespipelineEOSExit toshellCreatepipelineBuildelementsand linkpadsProducefivebuffersSourcesends testframesReceive EOSeventSinkfinishesthe streamRead busmessageLauncherseespipelineEOSExit toshell
From command-line description to end-of-stream notification

If you omit num-buffers=5, the test source keeps generating data until the pipeline is stopped. Press Ctrl+C to interrupt a running command. The buffer limit makes examples repeatable and lets you tell whether the pipeline completed on its own. It is a count of buffers produced by this source, so use it to specify how many frames the test should generate. The time spent running also depends on how the pipeline is configured and how quickly the elements process the data.

An error can occur before any frames are generated. For example, an element name that is unavailable on your installation prevents the command from constructing the requested pipeline. If the description links successfully, an error can still occur when the elements start and try to select a stream format. The stage at which the command fails tells you what to inspect. For an unavailable element, check whether its factory is installed. For a format problem, check the adjacent pads and any format restrictions between them.

03

Inspect elements and verify caps

3 min read

Use gst-inspect-1.0 to find out what factories and plugins your installation provides. With an element factory name, it describes that factory, including its pad templates and properties:

bash
gst-inspect-1.0 videotestsrc
gst-inspect-1.0 videoconvert
gst-inspect-1.0 fakesink

In videotestsrc, find the num-buffers property and the source pad template. In videoconvert, find both the sink and source pad templates. A template describes what kind of pad an element can provide and advertises capabilities for that pad. The property listing tells you what settings you can give an element, including their types and, where applicable, their allowed values. These details come from the installed element, so the same inspection commands are useful on an Ubuntu system and on the target image.

can express choices, such as a list of supported pixel formats, or a specific format selected for a stream. video/x-raw identifies uncompressed video. Fields such as format, width, height, and framerate describe how to interpret it. During caps negotiation, connected elements determine a format the upstream side can produce and the downstream side can accept. An element may advertise many possible formats in its template. To learn which one a running pipeline selected, inspect the negotiated caps.

The following command constrains the format on both sides of the converter. The source generates RGB frames and the converter produces I420, a planar YUV format:

bash
gst-launch-1.0 -v videotestsrc num-buffers=5 ! \
  'video/x-raw,format=RGB,width=320,height=240,framerate=30/1' ! \
  videoconvert ! 'video/x-raw,format=I420' ! fakesink

The caps expressions between ! tokens cause the launcher to insert caps filters. The quotes pass each expression to the command as one shell argument. The first filter requires raw RGB video at the stated size and frame rate. The second filter requires I420 and leaves size and frame rate unspecified. In this run videoconvert changes the pixel format while preserving the frame dimensions and frame rate. A caps filter constrains the format that can be selected through it. The converter performs the pixel operation.

Caps on each side of the converterRGB capsfilterRequiresRGB on theinput linkvideoconvertConvertsraw pixeldataI420 capsfilterRequiresI420 on theoutput linkRGB capsfilterRequiresRGB on theinput linkvideoconvertConvertsraw pixeldataI420 capsfilterRequiresI420 on theoutput link
The caps filters require RGB before videoconvert and I420 after it

The -v option enables verbose status output, including pad caps as they are set. Locate the caps reported for the converter's sink pad and then its source pad. The sink pad shows the format coming into the converter. The source pad shows the format going out. Compare the format fields on those two pads and confirm that width and height agree. Several pads may report caps, so use both the element name and the pad direction when reading the output.

If you remove videoconvert, one filter requires RGB and its neighbour requires I420 on the same connection. Those requirements have no common format. Written as explicit capsfilter elements, capsfilter caps=video/x-raw,format=RGB ! capsfilter caps=video/x-raw,format=I420, the launcher stops with erroneous pipeline: could not link capsfilter0 to capsfilter1 (GStreamer 1.24.2 on Ubuntu 24.04). Two bare caps strings in a row are rejected earlier, with no element "video", because the launch syntax expects an element between them. gst-inspect-1.0 gives you the supported capabilities to check before running. gst-launch-1.0 -v gives you the selected caps when negotiation succeeds.

In Building capture, display, and app pipelines, you will use the same way of reading elements, pad links, and formats to build pipelines that use a capture device and deliver frames to a display or an application.

Key takeaways

  • A pipeline is a top-level bin that manages a graph of linked elements.
  • Negotiated caps describe the media format on a link, and buffers carry the media data.
  • The pipeline bus delivers messages such as errors and end of stream to the controlling program.
  • gst-inspect-1.0 shows what an element supports, while gst-launch-1.0 -v can show the caps selected during a run.
Reflect
  1. Explain to a colleague, in two sentences: identify the elements, pads, and bin in a simple pipeline.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close