# Python WebRTC

**Chrome’s WebRTC engine, with the browser API, in Python.**

Python WebRTC binds libwebrtc M152, the engine inside Chrome, and exposes it through the W3C APIs you already know
from the browser: peer connections, data channels, tracks, WebCodecs frames, encoded transforms and stats, in
asyncio. Talk to browsers, servers, SFUs and AI agents peer-to-peer, with no vendor service in between.

```bash
pip install wrtc
```

#### TIP
Coming from aiortc? Install `wrtc[aiortc]`, change `from aiortc` to `from webrtc.aiortc`, and your code runs on Chrome’s
engine. See [Migrating from aiortc](guides/aiortc.md).

## If you know it in JavaScript, you know it in Python

### Python

```python
pc = webrtc.RTCPeerConnection()
channel = pc.create_data_channel('chat')
channel.on('open', lambda e: channel.send('Hello!'))
channel.on('message', lambda e: print(e.data))
pc.on('icecandidate', lambda e: signaling.send(e.candidate))

offer = await pc.create_offer()
await pc.set_local_description(offer)
signaling.send(pc.local_description)
```

### Browser

```js
const pc = new RTCPeerConnection();
const channel = pc.createDataChannel('chat');
channel.onopen = () => channel.send('Hello!');
channel.onmessage = (e) => console.log(e.data);
pc.onicecandidate = (e) => signaling.send(e.candidate);

const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
signaling.send(pc.localDescription);
```

Names follow the specifications in snake_case (`create_offer`), and the camelCase originals (`createOffer`) work
too. Enums compare equal to the strings of the specification, and dictionaries are typed dataclasses.

#### NOTE
Pre-1.0: until the 1.0.0 release, compatibility between versions isn’t guaranteed.

## Why Python WebRTC

**The real engine**

ICE, DTLS, SCTP, congestion control, jitter buffers and codecs are Chrome’s own, so interop with browsers is the
browsers’ interop.

**The browser API**

Written against the W3C specifications and tested with the web-platform-tests the browsers use.

**Native media path**

Packets, decoding and encoding never touch Python. You see frames, not packets.

**Every modern codec**

VP8, VP9, AV1, H.264 and Opus. Hardware H.264 through VideoToolbox on macOS. See [H.264](guides/h264.md).

**Fast connection setup**

[WARP](guides/field-trials.md#warp): DTLS 1.3, SNAP and SPED cut round trips off every new connection.

**pip install and go**

Prebuilt wheels for Linux, macOS and Windows, including free-threaded Python. Nothing else to install.

## How it compares

|                                                     | **Python WebRTC**    | aiortc         | LiveKit SDK              | GStreamer webrtcbin   |
|-----------------------------------------------------|----------------------|----------------|--------------------------|-----------------------|
| Engine                                              | libwebrtc (Chrome’s) | own, in Python | libwebrtc                | own, in C             |
| Connects peer-to-peer to any WebRTC endpoint        | ✅                    | ✅              | ❌ needs a LiveKit server | ✅                     |
| Browser API (`RTCPeerConnection`, `createOffer`, …) | ✅                    | similar        | ❌ rooms                  | ❌ GObject signals     |
| Media path                                          | native               | Python         | native                   | native                |
| VP9 and AV1                                         | ✅                    | ❌              | ✅                        | ✅                     |
| `pip install` on Linux, macOS and Windows           | ✅                    | ✅              | ✅                        | ❌ no Linux wheels     |

Against aiortc, it opens connections 7.3–8.3x faster, moves 7.7–14.6x more data through a data channel and spends
less CPU on audio. See the [benchmarks](benchmarks.md), which also measure Pion, libdatachannel and webrtc-rs.

The full comparison, and when another library is the better choice: [Choosing a Python WebRTC library](guides/comparison.md).

## Where to start

[**Quickstart**](quickstart.md)

Install it and connect two peers with a data channel. Five minutes.

[**Guides**](guides.md)

Connections, data channels, events and media, organised by what you are trying to do.

[**Examples**](examples.md)

Every runnable example in the repository, inlined and ready to copy.

[**Media**](guides/media.md)

Read the frames of a track, write your own, transform them on the way.

[**API reference**](api.md)

Every interface, dictionary, enum and exception of the package.

[**Changelog**](change_log.md)

What changed in each release.

## What it covers

- **WebRTC**: [`RTCPeerConnection`](api/interfaces/rtc_peer_connection.md#webrtc.interfaces.rtc_peer_connection.RTCPeerConnection), data channels, transceivers, senders and receivers, DTLS, ICE and
  SCTP transports, DTMF, certificates and the stats of WebRTC Statistics.
- **Media Capture and Streams**: tracks and streams, with a synthetic camera and microphone.
- **Media processing**: [`MediaStreamTrackProcessor`](api/interfaces/media_stream_track_processor.md#webrtc.interfaces.media_stream_track_processor.MediaStreamTrackProcessor), [`VideoTrackGenerator`](api/interfaces/track_generator.md#webrtc.interfaces.track_generator.VideoTrackGenerator) and
  [`MediaStreamTrackGenerator`](api/interfaces/track_generator.md#webrtc.interfaces.track_generator.MediaStreamTrackGenerator), with the [`VideoFrame`](api/models/video_frame.md#webrtc.models.video_frame.VideoFrame) and [`AudioData`](api/models/audio_data.md#webrtc.models.audio_data.AudioData) of
  WebCodecs and WHATWG Streams.
- **WebRTC Encoded Transform**: [`RTCRtpScriptTransform`](api/interfaces/rtc_rtp_script_transform.md#webrtc.interfaces.rtc_rtp_script_transform.RTCRtpScriptTransform) and end-to-end encryption with SFrame.

## Supported platforms

Pre-built wheels for CPython 3.9 to 3.15, and free-threaded 3.14t and 3.15t, on:

| Linux                                       | macOS                        | Windows   |
|---------------------------------------------|------------------------------|-----------|
| x86_64 and aarch64 (glibc 2.27+, manylinux) | 13+, Intel and Apple Silicon | x64       |

Elsewhere, pip builds it from sources. See [Installation](quickstart.md#installation).
