WebRTC gateway🔗

Warning

Certain features require premium support: see here for limits and pricing.

The TURN gateway is the WebRTC face of STUNner: browsers and WebRTC clients open a TURN allocation on the public address of a STUNner Gateway, and STUNner relays their media to the WebRTC media servers running in ordinary Kubernetes pods (e.g., LiveKit, Janus, mediasoup), or between the clients themselves. Below we review the most important concepts to understand in order to use STUNner with WebRTC clients and media servers effectively.

See the WebRTC tutorials for tutorials on deploying STUNner as a TURN server.

Table of Contents🔗

  1. Concepts
  2. TURN gateways
  3. Client configuration
  4. Testing
  5. Kernel offload

Concepts🔗

A TURN Gateway is a plain Gateway with TURN-UDP, TURN-TCP, TURN-TLS or TURN-DTLS listeners. Every listener is a TURN server over the respective transport, and the UDPRoutes attached to the listener name the peers its clients may reach: the TURN server grants a client a permission only for the pods of the backends of the routes (or the address ranges of a StaticService), every other peer is refused.

STUNner supports two deployment models: in the headless model it is a plain TURN server the clients use to reach each other, in the media-plane model it is the gateway that ingests the media of the clients into a media server pool deployed behind it. In the media-plane model the media servers need no public address: they run in the private pod network, and the ICE mode decides whether they use STUNner as their TURN server too.

TURN gateways🔗

The below Gateway exposes a TURN server over UDP port 3478 on a LoadBalancer Service, and the UDPRoute lets its clients reach the pods of the media-server Service in the media-plane namespace. The GatewayClass and the GatewayConfig of the installation set the credentials the clients authenticate with (see the authentication guide).

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: udp-gateway
  namespace: stunner
spec:
  gatewayClassName: stunner-gatewayclass
  listeners:
    - name: udp-listener
      port: 3478
      protocol: TURN-UDP
---
apiVersion: stunner.l7mp.io/v1
kind: UDPRoute
metadata:
  name: media-plane
  namespace: stunner
spec:
  parentRefs:
    - name: udp-gateway
  rules:
    - backendRefs:
        - name: media-server
          namespace: media-plane

Add a TURN-TLS listener on port 443 for clients behind firewalls that pass only TLS: see listener configuration for the certificate, and the Gateway reference and the route reference for every field. A TURNRoute relays the allocations through an upstream TURN server instead.

Client configuration🔗

The clients need the ICE server configuration of the Gateway: the TURN URIs of its listeners and valid credentials. The STUNner authentication service returns it in the format the RTCPeerConnection takes, always in sync with the running configuration:

curl "http://stunner-auth.stunner-system:8088/ice?service=turn"

Your application server queries the service and hands the result to the clients, which pass it to the RTCPeerConnection unchanged. No other client-side change is needed.

Testing🔗

stunnerctl icetest deploys a WebRTC echo server, exposes it through a UDP and a TCP TURN Gateway of its own, connects to it and reports the loss and the latency, the quickest test of a fresh installation. The UDP echo tutorial walks through a TURN Gateway step by step, and mediactl streams packets through a TURN allocation to a UDP echo peer and measures the loss and the round trip:

mediactl turn send -t 10s k8s://stunner/udp-gateway/udp-listener k8s://stunner/udp-echo:9001

mediactl finds the public address and the credentials of the listener and a pod of the echo Service through your kubeconfig; see the mediactl manual for plain addresses and more.

icetest and mediactl measure the gateway. To see a real call from the browser's side (which ICE candidate pair was selected, whether media actually went through the TURN relay, and loss and jitter as the client saw them), open chrome://webrtc-internals during the call, or record every call in production with rtcstats-js, an open-source client library.

Kernel offload🔗

STUNner can relay the UDP channels of a TURN/UDP listener in the Linux kernel instead of in user space: once a client binds a channel to a peer of a UDPRoute backend, the ChannelData from the client and the datagrams from the peer are relayed in the kernel, one offloaded flow per channel. The allocations, the permissions and the channel bindings stay in user space, and so does the data of a client that sends it in Send indications instead of a channel. TURN over TCP, TLS or DTLS, and relaying through an upstream TURN server of a TURNRoute, are not offloaded. See the kernel offload reference for the offload limit of your tier, the settings and the monitoring.