How to use local port forwarding to get a remote RTSP video stream
Introduction
This wiki shows how to implement SSH local port forwarding, or SSH tunneling, to be able to stream a live video from a remote RTSP stream in your local host. Disclaimer: Always make sure you have permission to remotely stream the video.
What is SSH Port Forwarding?
SSH port forwarding is a technique that allows secure tunneling of network traffic through an SSH connection. It enables you to forward a local, remote, or dynamic port on your machine to another port on a remote system. This is useful for accessing services (like databases, web servers, or RTSP streams) running on remote servers that might not be directly accessible due to firewall rules or network restrictions. By forwarding ports, you can securely access these services as if they were running on your local machine, all while encrypting the traffic through the SSH tunnel.
Implementation
In this implementation, let's assume that there is a need to receive a live RTSP video stream on a local machine from a media server running on a remote device for testing or debugging purposes. Using SSH local port forwarding, the following steps can be followed:
1. On your local machine, open a new terminal and execute the following command to forward your local port over SSH:
![]() | Note: Replace <local_port> with the port on your local machine you want to forward, for example, 1232. Replace <remote_port> with the service port RTSP is using to stream in the remote server, for example, 1232. In this case, localhost is being used because we want the data in the remote server's 1232 port to be sent over to the local port 1232 in our local machine. Replace <user@remote_ip_address> with the remote server's user and IP address. |
ssh -X -L <local_port>:localhost:<remote_port> <user@remote_ip_address>
Options:
- L: SSH Port Forwarding option.
- X: Enables X11 Forwarding, which allows us to run graphical applications from the remote machine and display them on the local machine, given that we need to view a live video stream.
2. The port forwarding is now configured and running, so we should have the data from the remote server's port 1232 in our local machine's 1232 port. Let's assume the data is an H264 video stream, so on a new terminal the following GStreamer pipeline can be executed to visualize the stream:
![]() | Note: Replace <local_port> with the port on your local machine you forwarded. Replace <stream_name> with the name of the remote stream. |
GST_DEBUG=2 gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:<local_port>/<stream_name> latency=0 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! autovideosink sync=false
Troubleshooting
- If the port forwarding is failing, it could be because the remote server does not allow TCP port forwarding. In that case, assuming you have permissions to change it, you can access
/etc/ssh/sshd_config
the remote server and set the following line to yesAllowTcpForwarding yes
.
For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.
Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.