Stream a remote camera using Go2RTC and WireGuard
Project Overview
Introduction
The aim of this project is to enable real-time streaming of a remote outdoor camera, viewable on an iPhone within the Home app.
The camera is at another location (Site A) with no public access, and the Home app runs at home (Site B). The challenge is to bring the video stream home securely, over a slow connection, and to present it to HomeKit.
Approach
This setup involves two sites:
- Site A (Remote Location): Includes a Hikvision camera and a Raspberry Pi 3.
- Site B (Home): Consists of an Ubuntu 22.04 VM running Docker and a Home Assistant VM, both hosted on a Proxmox server.
The two sites are linked by a WireGuard VPN: the camera is never exposed to the Internet.
How it works
- On Site A, the Raspberry Pi runs a WireGuard server. Its router forwards one UDP port to it.
- On Site B, a
wireguardcontainer connects to it as a client. Any RTSP request it receives on port 554 is forwarded (DNAT) through the tunnel to the camera. - A
go2rtccontainer, on the same Docker network, reads the camera stream atrtsp://wireguard/...and re-publishes it on the local network. - Home Assistant reads that local stream and exposes it to HomeKit, so it shows up in the Home app.
- An
autohealcontainer restarts thewireguardcontainer when the camera stops answering through the tunnel.
System Architecture
Before you start
You need:
- Site A: a Raspberry Pi (Raspberry Pi OS) on the same network as the camera, and access to the router to forward a UDP port
- Site B: a Linux machine or VM with Docker and Docker Compose, and Home Assistant with add-on support (Home Assistant OS)
- the camera's RTSP URL and credentials. For Hikvision cameras, the main stream is
/Streaming/Channels/101
The IP addresses below are examples:
| Address | Role |
|---|---|
192.168.1.201 | the camera, on the Site A network |
10.83.153.1 | the Raspberry Pi, inside the tunnel |
10.83.153.3 | the wireguard container on Site B, inside the tunnel |
51879/udp | the WireGuard port, forwarded by the Site A router to the Pi |
Implementation Steps
Setting Up Site A
-
Install and Configure WireGuard on Raspberry Pi:
- Use PiVPN for easy setup.
- Follow the provided documentation for a smooth installation.
-
WireGuard Server Configuration:
- The configuration file (
/etc/wireguard/wg0.conf) should be set up as follows:
/etc/wireguard/wg0.conf[Interface] Address = 10.83.153.1/24 MTU = 1480 SaveConfig = true PostUp = iptables -A FORWARD -i %i -j ACCEPT PostUp = iptables -A FORWARD -o %i -j ACCEPT PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT PostDown = iptables -D FORWARD -o %i -j ACCEPT PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE ListenPort = 51879 PrivateKey = XXX [Peer] PublicKey = XXX AllowedIPs = 10.83.153.3/32 PersistentKeepalive = 25The
MASQUERADErule makes traffic from the tunnel look like it comes from the Pi itself, so the camera answers without needing a route back to10.83.153.0/24. - The configuration file (
-
Forward the WireGuard port:
- On the Site A router, forward UDP port
51879to the Raspberry Pi. - If the public IP of Site A changes, set up a dynamic DNS name for it.
- On the Site A router, forward UDP port
Warning
Before editing your WireGuard configuration, stop the interface with wg-quick down wg0: with SaveConfig = true, any change made while it is running is overwritten when it stops.
Setting Up Site B
-
Deploy Docker-Compose Stack:
- The stack includes WireGuard, Go2RTC, and Autoheal containers.
- The
docker-compose.ymlfile should be configured as shown:
docker-compose.ymlservices: wireguard: image: linuxserver/wireguard container_name: wireguard cap_add: - NET_ADMIN - SYS_MODULE sysctls: - net.ipv4.conf.all.src_valid_mark=1 environment: - PUID=1000 - PGID=1000 - TZ=Europe/Paris volumes: - /path/to/wireguard/config:/config restart: always healthcheck: test: ["CMD", "curl", "--fail", "http://192.168.1.201"] interval: 5s retries: 10 start_period: 30s timeout: 5s labels: - autoheal=true networks: - vpn go2rtc: image: alexxit/go2rtc restart: unless-stopped environment: - TZ=Europe/Paris ports: - "1984:1984" - "8554:8554" depends_on: - wireguard networks: - vpn volumes: - "/path/to/go2rtc/config:/config" autoheal: restart: always image: willfarrell/autoheal environment: - AUTOHEAL_CONTAINER_LABEL=all volumes: - /var/run/docker.sock:/var/run/docker.sock:ro networks: vpn: name: vpn driver: bridgeThe
healthcheckfetches the camera web page through the tunnel every 5 seconds. When it fails 10 times in a row, the container is marked unhealthy andautohealrestarts it, which rebuilds the tunnel. -
WireGuard Client Configuration:
- Configure the tunnel so that Go2RTC can reach the camera through WireGuard: RTSP traffic (port 554) sent to the
wireguardcontainer is forwarded to the camera. - With the
linuxserver/wireguardimage, put the file in/path/to/wireguard/config/wg_confs/wg0.conf. - Example
wg0.conf:
wg0.conf[Interface] PrivateKey = XXX Address = 10.83.153.3/24 MTU = 1480 PostUp = iptables -t nat -A PREROUTING -p tcp --dport 554 -j DNAT \ --to-destination 192.168.1.201:554 PostUp = iptables -t nat -A POSTROUTING -j MASQUERADE PostDown = iptables -t nat -D PREROUTING -p tcp --dport 554 -j DNAT \ --to-destination 192.168.1.201:554 PostDown = iptables -t nat -D POSTROUTING -j MASQUERADE [Peer] PublicKey = XXX Endpoint = IP:PORT or DNS:PORT AllowedIPs = 192.168.1.201/32, 10.83.153.1/32 PersistentKeepalive = 25Info
I've set the MTU to 1480 because the remote connection is slow (~8 Mbps).
- Configure the tunnel so that Go2RTC can reach the camera through WireGuard: RTSP traffic (port 554) sent to the
-
Deploy the Stack:
- Run
docker compose up -din the directory containingdocker-compose.yml.
- Run
-
Configure Go2RTC on the Ubuntu VM:
- Access the Go2RTC configuration page (
http://serverip:1984). - Click on the
Configtab and write something like this:
Go2RTC configurationlog: rtsp: info streams: mystream: rtsp://camera:password@wireguard/Streaming/Channels/101 rtsp: listen: ":8554" # RTSP Server TCP port, default - 8554 username: "admin" # optional, default - disabled password: "pass" # optional, default - disabled default_query: "mp4" # optional, default codecs filters - Access the Go2RTC configuration page (
-
Integrating with Home Assistant:
Check that it works
Test each link of the chain, from the tunnel to the Home app:
- The tunnel is up: on Site B, run
docker exec wireguard wg show.latest handshakeshould be a few seconds or minutes old. - The camera answers through the tunnel:
docker exec wireguard curl -sI http://192.168.1.201should return an HTTP response. - Go2RTC reads the stream: on
http://serverip:1984,mystreamis listed; open it to see the video in the browser. - The local RTSP stream works: open
rtsp://admin:pass@serverip:8554/mystreamin VLC. - HomeKit: the camera appears in the Home app.
Troubleshooting
- No handshake: check the port forwarding on Site A, the
Endpointof the client, and that the public keys match on both sides (sudo wg showon the Pi). - The handshake works but the camera does not answer: check
AllowedIPson the client (it must include the camera IP) and theMASQUERADErule on the Pi. - The stream freezes or stutters: lower the MTU on both sides (try 1420, then 1380), or use the camera's sub-stream (
/Streaming/Channels/102), which needs much less bandwidth. - The
wireguardcontainer restarts in a loop:docker logs wireguardand theHealthsection ofdocker inspect wireguardshow why the healthcheck fails.
Security
Replace the example credentials (admin / pass, the camera password) and the HomeKit PIN 12345678 with your own: HomeKit refuses trivial PINs, and anyone on your network can read an unprotected RTSP stream.
Conclusion
With these steps, you should be able to stream your remote camera in real-time and view it in your Home app.