Skip to content

Instantly share code, notes, and snippets.

@julianoes
Forked from corenel/README.md
Last active February 29, 2024 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianoes/0555ee982991fe97e5a4c2b11316195f to your computer and use it in GitHub Desktop.
Save julianoes/0555ee982991fe97e5a4c2b11316195f to your computer and use it in GitHub Desktop.
RTSP Port Forwarding

RTSP forwarding using UDP transport

This is based on https://gist.github.com/corenel/db98113b346717e114864538996134a8, however, adapted to work over UDP transport, rather than TCP, which makes it a bit more complicated, and requires the client to fix the UDP ports used.

Assume we have the following network environments:

  • Forwarder device:
    • eth0 (192.168.144.25): for camera access
    • wlan0 (192.168.1.54): for PC access
  • IP Camera:192.168.144.25
  • PC:192.168.1.51

On forwarder device, we want to forward the 8554 port (used for RTSP) of the IP camera (192.168.144.25) to the same port in 192.168.1.54.

  1. Run rtsp-forwarding.sh

  2. You can use the following command to play forwarded RTSP stream on PC:

gst-launch-1.0 rtspsrc location=rtsp://192.168.1.54:8554/main.264 latency=0 protocols=udp port-range=25004-25005 ! rtph264depay ! avdec_h264 ! xvimagesink

The port range needs to be fixed, in order for UDP to be routed back to the client.

#!/usr/bin/env bash
#
# This script is based on:
# https://gist.github.com/corenel/db98113b346717e114864538996134a8
#
# The original script only supported forwarding using TCP transport but not
# UDP transport.
#
# The issue is that UDP traffic is set up from the client in the RTSP SETUP
# call. In there, it sets the UDP port on which it wants to receive the RTP
# traffic. Most RTSP clients choose this port randomly, and so the forwarder
# doesn't know which ports to forward from RTSP server to the client.
# We can work around this by fixing the UDP port on the client side, e.g.:
#
# gst-launch-1.0 rtspsrc location=rtsp://192.168.1.54:8554/main.264 latency=0 protocols=udp port-range=25004-25005 \
# ! rtph264depay ! avdec_h264 ! xvimagesink
set -e
enable_forwarding () {
ip=$1
port=$2
nic=$3
# Forward traffic from interface nic and port to the IP of the camera.
sudo iptables -t nat -A PREROUTING -p tcp -i ${nic} --dport ${port} -j DNAT --to-destination ${ip}:${port}
sudo iptables -t nat -A PREROUTING -p udp -i ${nic} --dport ${port} -j DNAT --to-destination ${ip}:${port}
# Accept traffic from camera.
sudo iptables -A FORWARD -p tcp -d ${ip} --dport ${port} -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A FORWARD -p udp -d ${ip} --dport ${port} -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
}
# Parameters
CAMERA_IP=192.168.144.25
CAMERA_NIC=eth0
CLIENT_IP=192.168.1.51
CLIENT_NIC=wlan0
# Enable ipv4 forwarding in system side
sudo sysctl net.ipv4.ip_forward=1
# Flush existed rules of iptables
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -X
# Enable port forwarding
enable_forwarding ${CAMERA_IP} 8554 ${CLIENT_NIC}
enable_forwarding ${CLIENT_IP} 25004 ${CAMERA_NIC}
enable_forwarding ${CLIENT_IP} 25005 ${CAMERA_NIC}
# Enable routing
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment