7. Set up video streaming

Video Streaming Setup

I need an FPV from the drone to the ground station. It is what this setup can do:

  • Using Raspberry Pi Camera Module captures the live image;
  • Using GStreamer to set up the video streaming;
  • Sending the video streaming to the ground station through UDP;
  • Using mavsdk to monitoring the drone’s status and start the video streaming when the drone is armed;
  • Stop the video streaming when the drone is disarmed
  • Save the video to file;

My hardware
– Raspberry Pi Zero 2
– Raspberry Pi Camera Module 2

Connect the Raspberry Pi Camera Module

Connecting the Camera Module to Raspberry Pi is straightforward, the only thing that should note is using raspi-config to enable the camera.

Install gstreamer1.0

I use gstreamer to encode the video and send it to the grand station through UDP.

sudo apt-get update
sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-plugins-bad

Start video streaming on the Raspbarry Pi

The the gstreamer install success, you can test the video streaming by the command below. It will send the video to the ground station through the UDP on 5600 port.

raspivid -n -w 640 -h 360 -b 1000000 -fps 15 --flush --timeout 0 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=10 pt=96 ! udpsink host={ground-station_ip} port=5600

Replace the {ground-station_ip} to the IP of your ground station.

Receive the video

The setup above is the default setting of the QGroundControl. If you are using Windows and do the setup below, the video can be shown automatically.

Unfortunately, I can’t make it work on Mac with QGroundControl. I don’t know the reason, the workaround is to use the gst-launch to receive the video. It is the command that I used on Mac:

gst-launch-1.0 -v udpsrc port=5600 caps='application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264' ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=f
The command is running and waiting for the video stream.

When it receive the video, it will open a new window to display it.

Auto start and stop the video streaming

I want the video starts only when the drone is armed and it will stop the stream when disarmed. I used the mavsdk to listen the drone’s status.

Install the scripts

I write a python script to achieve it. Please read my github.
You can copy two files to your raspi:

  • stream.sh: It is the shell script to start the video streaming.
  • controller.py: is the python script that starts/stops the stream when the drone is armed/disarmed.
Remember to change the UDP_IP to your receiver IP.

I assume you save the files to /home/pi/video-stream/

Change the files to executable:

cd /home/pi/video-stream/
chmod u+x stream.sh
chmod u+x controller.py

Create a video folder to save videos. The video files will be saved to /home/pi/video-stream/videos/*

mkdir videos

Install required python modules:

sudo pip install mavsdk psutil

Run the script

sudo python controller.py
A demo of using video-stream/controller.py and receiving the video on Mac.

Start the controller.py when system boot

I need the controller.py auto start when the system boot. I added this line to the /ect/rc.local. It is my rc.local file.

# Strat video-control
/usr/bin/python /home/pi/video-stream/controller.py

If you found any mistake in the document or you have difficulty following the setups, please leave your comments to me. Your comments can help me to improve this article. Thank you!

RC Bellergy

One thought on “7. Set up video streaming”

Welcome your comments