---
title: "WebRTC (WHIP and WHEP)"
description: "The tatolab-webrtc module's two nodes, which send encoded video and audio to a WHIP endpoint or play them from a WHEP endpoint."
type: "docs-page"
url: "https://tatolab.com/docs/build/webrtc"
site: "Tatolab"
section: "Build in code"
---

# WebRTC (WHIP and WHEP)

> The tatolab-webrtc module's two nodes, which send encoded video and audio to a WHIP endpoint or play them from a WHEP endpoint.

The `tatolab-webrtc` module adds two nodes: `WhipPublisher`, which sends encoded video and audio to a [WHIP](https://www.rfc-editor.org/rfc/rfc9725) endpoint, and `WhepPlayer`, which plays them from a WHEP endpoint. Cloudflare Stream and most WebRTC media servers take both. It's an ordinary package with Rust inside, and its native code runs in those nodes' own processes.

Add the modules from **File › Marketplace…** in Tato Studio, or from a terminal:

```sh
uv add tatolab-webrtc tatolab-h264 tatolab-opus
```

Both nodes sit on the encoded side of the codecs: `H264Encoder` and `OpusEncoder` upstream of the publisher, `H264Decoder` and `OpusDecoder` downstream of the player. Each message is one bare Annex-B access unit or one Opus packet.

## Publish the camera and microphone

```txt
camera (CameraSource) ── video ──▶ H264Encoder ── encoded_video ──┐
                                                                  ├──▶ WhipPublisher (tracks)
microphone (MicrophoneSource) ── audio ──▶ OpusEncoder ── encoded_audio ──┘
```

`WhipPublisher` has one input, `tracks`, that takes several links: each link becomes one RTP track, video or audio by the codec its upstream port states. Its settings are `url`, the WHIP endpoint, and `bearer_token`, sent as `Authorization: Bearer` when the endpoint wants one.

> **Caution: The URL is a credential**
>
> A WHIP URL often carries its key, as Cloudflare Stream's does. Never write it into the graph document, which the project's history keeps. Read it from the runtime's environment instead, in a node of your own that subclasses `WhipPublisher`, as the `camera-webrtc-publish` example does:
>
> ```python title="nodes/whip_from_env.py"
> import os
>
> from tatolab.stream import node
> from tatolab.webrtc import WhipPublisher, WhipPublisherConfig
>
>
> @node.meta(description="Publishes to the WHIP endpoint the runtime's environment names.")
> @node.process(paced_by_real_time=True)
> class WhipFromEnvironment(WhipPublisher):
>     def __init__(self) -> None:
>         super().__init__(
>             WhipPublisherConfig(
>                 url=os.environ["STREAMLIB_WHIP_URL"],
>                 bearer_token=os.environ.get("STREAMLIB_WHIP_BEARER_TOKEN") or None,
>             )
>         )
> ```
>
> Each node's process inherits `tatolabd`'s environment, so set the variables where the runtime starts.

## How the session behaves

- **A session carries at most one video and one audio track.** A second link of either kind is refused.
- **Only H.264 video and Opus audio**, mono or stereo. A codec the session doesn't carry is refused, naming it.
- **The session opens with the first media frame**, not when the stream starts. If the endpoint is unreachable, the stream still plays, and the error is in the log.
- **Each track starts at a group start.** A link enters at a keyframe, or any Opus packet, and after a loss it discards up to the next one, so the far end never gets a picture it can't decode.

Video needs a hardware H.264 encoder; see [Encode and save](https://tatolab.com/docs/build/recording#what-the-hardware-has-to-have).

## Play a WHEP stream

`WhepPlayer` connects to a WHEP endpoint, with the same `url` and `bearer_token` settings, and writes what it receives on two outputs, `encoded_video` (H.264) and `encoded_audio` (Opus), in the same form the encoders write. It states each port's stream facts from the stream itself, so a decoder downstream knows the size and channel count before the first frame.

```txt
WhepPlayer ─┬─ encoded_video ──▶ H264Decoder ── video ──▶ (the Node visualizer, a model, a virtual camera)
            └─ encoded_audio ──▶ OpusDecoder ── audio ──▶ SpeakerSink
```

Decoded, its frames are ordinary frames: watch them in Tato Studio's Node visualizer, run a model on them, or put them on a [virtual camera](https://tatolab.com/docs/build/virtual-camera).

## Related

- The `camera-webrtc-publish` example, in the streamlib repository.
- [Encode and save](https://tatolab.com/docs/build/recording): the encoders and their settings.
