Skip to content

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

  1. On Site A, the Raspberry Pi runs a WireGuard server. Its router forwards one UDP port to it.
  2. On Site B, a wireguard container connects to it as a client. Any RTSP request it receives on port 554 is forwarded (DNAT) through the tunnel to the camera.
  3. A go2rtc container, on the same Docker network, reads the camera stream at rtsp://wireguard/... and re-publishes it on the local network.
  4. Home Assistant reads that local stream and exposes it to HomeKit, so it shows up in the Home app.
  5. An autoheal container restarts the wireguard container when the camera stops answering through the tunnel.

System Architecture

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

  1. Install and Configure WireGuard on Raspberry Pi:

    • Use PiVPN for easy setup.
    • Follow the provided documentation for a smooth installation.
  2. 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 = 25
    

    The MASQUERADE rule makes traffic from the tunnel look like it comes from the Pi itself, so the camera answers without needing a route back to 10.83.153.0/24.

  3. Forward the WireGuard port:

    • On the Site A router, forward UDP port 51879 to the Raspberry Pi.
    • If the public IP of Site A changes, set up a dynamic DNS name for it.

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

  1. Deploy Docker-Compose Stack:

    • The stack includes WireGuard, Go2RTC, and Autoheal containers.
    • The docker-compose.yml file should be configured as shown:
    docker-compose.yml
    services:
        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: bridge
    

    The healthcheck fetches the camera web page through the tunnel every 5 seconds. When it fails 10 times in a row, the container is marked unhealthy and autoheal restarts it, which rebuilds the tunnel.

  2. WireGuard Client Configuration:

    • Configure the tunnel so that Go2RTC can reach the camera through WireGuard: RTSP traffic (port 554) sent to the wireguard container is forwarded to the camera.
    • With the linuxserver/wireguard image, 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 = 25
    

    Info

    I've set the MTU to 1480 because the remote connection is slow (~8 Mbps).

  3. Deploy the Stack:

    • Run docker compose up -d in the directory containing docker-compose.yml.
  4. Configure Go2RTC on the Ubuntu VM:

    • Access the Go2RTC configuration page (http://serverip:1984).
    • Click on the Config tab and write something like this:
    Go2RTC configuration
    log:
      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
    
  5. Integrating with Home Assistant:

    • Ensure the go2rtc add-on is installed: Open add-on
    • Go to the go2rtc add-on config and write something like this (replace 192.168.0.203 with the IP of the Ubuntu VM):
    streams:
      mystream: rtsp://admin:pass@192.168.0.203:8554/mystream
    
    homekit:
      mystream:
        pin: 12345678
        name: MY CAMERA
        device_id: my_camera_1
        device_private: my_camera_1
    
    • Now add the camera to the HomeKit Bridge configuration: HomeKit Integration

Check that it works

Test each link of the chain, from the tunnel to the Home app:

  1. The tunnel is up: on Site B, run docker exec wireguard wg show. latest handshake should be a few seconds or minutes old.
  2. The camera answers through the tunnel: docker exec wireguard curl -sI http://192.168.1.201 should return an HTTP response.
  3. Go2RTC reads the stream: on http://serverip:1984, mystream is listed; open it to see the video in the browser.
  4. The local RTSP stream works: open rtsp://admin:pass@serverip:8554/mystream in VLC.
  5. HomeKit: the camera appears in the Home app.

Troubleshooting

  • No handshake: check the port forwarding on Site A, the Endpoint of the client, and that the public keys match on both sides (sudo wg show on the Pi).
  • The handshake works but the camera does not answer: check AllowedIPs on the client (it must include the camera IP) and the MASQUERADE rule 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 wireguard container restarts in a loop: docker logs wireguard and the Health section of docker inspect wireguard show 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.