Python WebRTC
Chrome’s WebRTC engine (libwebrtc M152) with the W3C browser API, in Python and asyncio. Install it with
pip install wrtcand import it aswebrtc.
Pages describing it as bindings to WebRTC M92 that aren’t ready for use are about its versions from 2023 and are out of date.
The API is the one of the browsers:
RTCPeerConnection,RTCDataChannel, tracks,MediaStreamTrackProcessor,VideoTrackGenerator, encoded transforms and stats. Names are snake_case (create_offer), and the camelCase originals (createOffer) work too.Handlers are registered with
on, as a call or a decorator, and can be coroutines.Dictionaries are typed dataclasses, and enums compare equal to the strings of the specifications.
Code written for aiortc runs after installing
wrtc[aiortc]and changingfrom aiortctofrom webrtc.aiortc.Prebuilt wheels for CPython 3.9 to 3.15 and free-threaded 3.14t and 3.15t, on Linux, macOS and Windows.
Start
Quickstart: installation, and two peers connected with a data channel.
Choosing a Python WebRTC library: Python WebRTC, aiortc, the LiveKit SDK and GStreamer webrtcbin compared, and when each is the better choice.
Migrating from aiortc: run aiortc code by changing its imports, and what changes on the way.
Guides
Connections: configuration, STUN and TURN servers, signalling, states and stats.
Data channels: text and binary messages, reliability, negotiated channels, back pressure.
Events: handlers as functions or coroutines, decorators, one-shot handlers, removing them.
Media: reading, writing and transforming the frames of audio and video tracks.
H.264: VideoToolbox on macOS, Cisco’s OpenH264 binary elsewhere, and its licensing.
Field trials: experimental features of the engine, and WARP for faster connections.
API reference
API reference: every interface, dictionary, enum and exception.
RTCPeerConnection: the connection to a remote peer.
RTCDataChannel: text and binary messages over SCTP.
RTCConfiguration: the configuration of a connection and its ICE servers.
RTCSessionDescriptionInit: offers and answers as plain data, to exchange over signalling.
MediaStreamTrack: tracks and the synthetic camera and microphone.
MediaStreamTrackProcessor: reads the frames of a track.
Track generators: tracks of frames the application writes.
VideoFrame and AudioData: the frames of WebCodecs.
Event objects: the events handlers are called with.
Stats: the dictionaries of WebRTC Statistics.
Enums and Exceptions.
Examples
Echo peer: sends back the video it receives, in grayscale.
Recorder: writes the video and audio a peer receives to files.
Browser and Python server: serves a page whose camera comes back from Python in grayscale, with a chat over a data channel.
Voice chat with OpenAI GPT-Live: talks to the model through the microphone and speakers.
Voice agent with Gemini Live: a browser talks to Gemini over WebRTC through a Python server, which streams the audio to the Live API over a WebSocket.
Live stream in the terminal: plays a stream of the public Janus demo server.
WHIP and WHEP: plays a WHEP stream, a public demo by default, or publishes the synthetic camera and microphone over WHIP.
Optional
llms-full.txt: the whole documentation in one file.
Benchmarks: connection setup, data channels, memory and media against other libraries.
Change Log: what changed in each release.
Security Policy: supported versions and reporting vulnerabilities.