Main Content

Stream Video over Network Using Raspberry Pi Video Stream Transmit Block

This example shows how to use the Video Stream Transmit block from the Simulink® Support Package for Raspberry Pi® Hardware to stream a live video over a network using real-time streaming protocol (RTSP).

In this example, you can,

  • Acquire live video stream from a USB web camera or a camera module connected to your Raspberry Pi hardware.

  • Display the edge emphasized object using the Sobel edge detection algorithm to identify the boundaries of the object.

  • Stream the live video over any device connected to the network.

Prerequisites

For more information on how to use the Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi hardware, see Get Started with Simulink Support Package for Raspberry Pi Hardware.

Required Hardware

  • Raspberry Pi hardware board (Model 4B or Compute Module 4 recommended)

  • USB web camera or Raspberry Pi camera module

  • Ethernet cable

  • A monitor connected to the Raspberry Pi hardware and an HDMI cable (optional)

Configure Simulink Model and Calibrate Parameters

This example uses a preconfigured Simulink model from the Simulink Support Package for Raspberry Pi Hardware that enables you to stream a live video over the network.

Open the raspberrypi_video_stream_send Simulink model.

Video Source

Use the V4L2 Video Capture to capture live images from the USB camera connected to your Raspberry Pi hardware. The block outputs Y, Cb, and Cr components of the image pixel as an 8-bit value.

Configure these parameters in the V4L2 Video Capture Block Parameters dialog box.

1. Specify the name of the USB camera in the Device name parameter. If you do not know the name of the device, open the Raspberry Pi terminal and use the ls command. This command lists all the devices connected to your Raspberry Pi hardware.

ls -al  /dev/video*

Copy the name of your device and paste in the Device name parameter.

2. Set Image size to 640x480.

3. Set Pixel format to YCbCr 4:2:2.

4. Set Sample time to 1/30 to capture 30 image frames per second.

Algorithm

The Edge Detection Sub System in the Algorithm area uses a Edge Detection (Computer Vision Toolbox) block that is configured to detect the edges in the live video stream using the Sobel edge detector method.

The output from the Edge Detection block is inverted using the Logical Operator logical operator.

The Rate Transition block transfers data from the output of the Edge Detection block operating at one rate to the input of the Video Stream Transmit block in the Visualization area that operates at a different rate.

Visualization

This area contains two Video Stream Transmit blocks, the first displaying the live original video stream output while the second displaying the grayscale edge detection output.

For the Video Stream Transmit block that displays the original video stream, configure these parameters in the Block Parameters dialog box.

  1. Set Input image format to YCbCr 4:2:2.

  2. Set Frame rate same as Sample time in the V4L2 Video Capture Block Parameters. Here, it is 30 image frames per second.

  3. Configure the Raspberry Pi hardware Port number on which to stream the live video. Here, it is configured to 8554.

  4. Configure the string to identify the Mount point for the video. Here, it is configured to original.

For the Video Stream Transmit block that displays the grayscale edge detection output of the video stream, configure these parameters in the Block Parameters dialog box.

  1. Set Input image format to Grayscale.

  2. Set Frame rate to 16 image frames per second.

  3. Configure the Raspberry Pi hardware Port number on which to stream the live video. Here, it is configured to 8555.

  4. Configure the string to identify the Mount point for the video. Here, it is configured to edge-detection.

Deploy Simulink Model on Raspberry Pi Hardware

On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.

You can view the live video stream output on the Raspberry Pi desktop using the VNC viewer.

Open two separate Raspberry Pi terminals. To view the live stream output, enter the command in this format on one of the Raspberry Pi terminal.

gst-launch-1.0 -v playbin uri=rtsp://raspberrypi_ip_address:port_number/mount_point

For example, to view the original live stream, run,

gst-launch-1.0 -v playbin uri=rtsp://192.168.0.100:8554/original

Open the other Raspberry Pi terminal. To view the edge detected output, command in this command on the Raspberry Pi terminal.

gst-launch-1.0 -v playbin uri=rtsp://192.168.0.100:8555/edge-detection

View Live Video on FFMPEG

To view the live video output on FFMPEG, you can download the FFMPEG software.

You can run this command on your host terminal irrespective of the operating system. For example, to view the live stream output, you can run the same command on Windows®, Linux®, or Apple iOS.

To view the live stream video output, run the command in this format on your host terminal.

ffplay -i rtsp://raspberrypi_ip_address:port_number/mount_point

For example, to view the original live stream, run,

ffplay -i rtsp://192.168.0.100:8554/original

Similarly, to view the edge detected output, run this command on your host terminal.

ffplay -i rtsp://192.168.0.100:8555/edge-detection

Follow these steps to view the live video streaming output on the VLC media player.

1. On the Media tab of the VLC media player, select Open Network Stream.

2. In the Open Media dialog box, enter the network URL in this format:

rtsp://raspberrypi_ip_address:port_number/mount_point

For example, to view the edge detection output, enter,

rtsp://192.168.0.100:8555/edge-detection

3. Click Play.

See Also