GStreamer on Raspberry Pi 4: A Guide to Seamless Video Transmission

Yusuke Enami(Kishishita)
4 min readOct 21, 2023

--

Introduction

While I often pen articles on Google Cloud, I also harbor a passion for edge computing, using devices like the Raspberry Pi, M5 Stack series, and Arduino. Today, I will delve into the wonders of GStreamer.

GStreamer is an open-source multimedia framework, making it an invaluable tool for creating video cameras for purposes such as surveillance or monitoring your pets.

Despite its capabilities, I found it challenging to locate a comprehensive webpage that neatly summarizes the essential commands for a quick start. Therefore, this article aims to share pivotal GStreamer commands for sending and receiving video on the Raspberry Pi 4.

Environment Setup

  • Streaming Device: Raspberry Pi 4
  • Receiving Device: Laptop
  • OS on Raspberry Pi 4: Ubuntu 20.04
  • OS on Laptop: Any
  • GStreamer version: v1.19.2

And we will build the plugins on Raspberry Pi below:

  • GStreamer plugin-base
  • GStreamer plugin-good
  • GStreamer plugin-bad
  • GStreamer plugin-ugly
  • GStreamer rtsp-server

To streamline the setup process, I’ve prepared a Dockerfile. These files are provided in the final section, ensuring a hassle-free reading experience.

Getting Started with GStreamer

For the purposes of this guide, I’m assuming that the environment has been successfully set up and built as detailed in the previous section.

Streaming on the RTSP Server

We will stream the video using test-launch file in built directory of gst-rtsp-server . Please move this directory.

We’ll begin by streaming video using the test-launch file located in the built directory of gst-rtsp-server. Navigate to this directory with the following command:

cd <Your Directory>/gst-rtsp-server/build/examples

Streaming by H264:

./test-launch '( v4l2src device=/dev/video0 ! video/x-h264, width=640, height=480, framerate=30/1 ! h264parse config-interval=1 ! rtph264pay name=pay0 pt=96 )'

Streaming by MJPG:

./test-launch '( v4l2src device=/dev/video0 ! image/jpeg, width=640, height=480, framerate=30/1 ! jpegparse ! rtpjpegpay name=pay0 pt=96 )'

Note: The device parameter should point to your video location. Use the lsusb command to locate your camera. Additionally, you can adjust the video's width, height, and framerate to match your camera's specifications.

Receiving Video from the RTSP Server

In this section, we will detail how to receive video streamed from the Raspberry Pi on a laptop, using the local network.

  1. Connect to the Same Network: Ensure that both the Raspberry Pi and the laptop are connected to the same local network.
  2. Access the Stream: Open an internet browser such as Chrome and enter the Raspberry Pi’s local IP address. This will allow you to view the video being streamed from the Raspberry Pi.

Note: Determine the Raspberry Pi’s local IP address by executing the ifconfig command in the terminal.

And if you encounter the following error:

Could not receive any UDP packets for 5.0000 seconds, maybe your firewall is blocking it. Retrying using a tcp connection.

Resolve it by setting do-rtcp=TRUE and updating the location to rtspt://~ in the video receiving command.

For further details or troubleshooting, refer to this issue thread on GitHub.

Saving Streamed Video in Various Formats

Once you’ve successfully received the video from the RTSP server, you might want to save it in different formats or even extract individual frames. Here’s how you can do that:

Saving H264 Stream as FLV:

gst-launch-1.0 -v rtspsrc do-rtcp=TRUE location=rtspt://<your location>:8554/test ntp-sync=true ! application/x-rtp, media=video, encoding-name=H264  ! queue ! rtph264depay ! h264parse ! flvmux ! filesink location=received_h264.flv

Saving MJPG Stream as AVI:

gst-launch-1.0 -v rtspsrc do-rtcp=TRUE location=rtspt://<your location>:8554/test ntp-sync=true ! application/x-rtp, media=video ! queue ! rtpjpegdepay ! jpegparse ! avimux ! filesink location=received_mjpg.avi

Saving H264 Stream as Individual Frame Images:

gst-launch-1.0 -v rtspsrc do-rtcp=TRUE location=rtspt://<your location>:8554/test ntp-sync=true ! application/x-rtp ! queue ! rtph264depay ! h264parse ! multifilesink location="<Your directory>/frame%08d.jpg"

Saving MJPG Stream as Individual Frame Images:

gst-launch-1.0 -v rtspsrc do-rtcp=TRUE location=rtspt://<your location>:8554/test ntp-sync=true ! application/x-rtp ! queue ! rtpjpegdepay ! jpegparse ! multifilesink location="<Your directory>/frame%08d.jpg"

Replace <your location> with the appropriate IP address or hostname of your RTSP server and <Your directory> with the directory path where you want to save the frames.

Summary

Throughout this article, I’ve provided a concise overview of the fundamental GStreamer commands to help you hit the ground running. Use these commands as a foundation for your projects and let your creativity flow. Dive into the world of video streaming with enthusiasm, and most importantly, have fun along the way!

Appendix: GStreamer Dockerfile

For those interested in a seamless setup, I’ve prepared a Dockerfile to help you quickly build GStreamer. You can find it in the provided Gist link:

GStreamer Dockerfile Gist

--

--

Yusuke Enami(Kishishita)

I'm now finding a DevOps job abroad! I love Google Cloud/Kubernetes/Machine Learning/Raspberry Pi and Workout🏋️‍♂️ https://bigface0202.github.io/portfolio/