Python implementation of WebRTC and ORTC for real-time peer-to-peer communication
npx @tessl/cli install tessl/pypi-aiortc@1.13.0A comprehensive Python implementation of Web Real-Time Communication (WebRTC) and Object Real-Time Communication (ORTC) protocols built on asyncio. aiortc enables real-time peer-to-peer communication with support for audio, video, and data channels, offering features like SDP generation/parsing, Interactive Connectivity Establishment (ICE), DTLS encryption, SRTP/SRTCP for media streams, and a pure Python SCTP implementation for data channels.
pip install aiortcimport aiortcCommon imports for WebRTC applications:
from aiortc import (
RTCPeerConnection,
RTCConfiguration,
RTCIceServer,
RTCSessionDescription,
AudioStreamTrack,
VideoStreamTrack
)import aiortc
import asyncio
async def create_peer_connection():
# Create peer connection with STUN server
configuration = aiortc.RTCConfiguration(
iceServers=[aiortc.RTCIceServer("stun:stun.l.google.com:19302")]
)
pc = aiortc.RTCPeerConnection(configuration=configuration)
# Add media tracks
audio_track = aiortc.AudioStreamTrack() # Generates silence
video_track = aiortc.VideoStreamTrack() # Generates green frames
pc.addTrack(audio_track)
pc.addTrack(video_track)
# Create data channel
channel = pc.createDataChannel("chat")
# Create and set local description
offer = await pc.createOffer()
await pc.setLocalDescription(offer)
return pc
# Run async function
pc = asyncio.run(create_peer_connection())aiortc follows the WebRTC standard architecture with these key components:
The library provides a pure Python implementation that follows JavaScript WebRTC APIs while using Pythonic constructs like coroutines instead of promises and EventEmitter for events.
Core WebRTC peer connection functionality including connection establishment, signaling state management, ICE candidate handling, and session description processing.
class RTCPeerConnection:
def __init__(self, configuration=None): ...
async def createOffer(self): ...
async def createAnswer(self): ...
async def setLocalDescription(self, description): ...
async def setRemoteDescription(self, description): ...
async def addIceCandidate(self, candidate): ...
def addTrack(self, track, *streams): ...
def addTransceiver(self, trackOrKind, direction="sendrecv"): ...
def createDataChannel(self, label, **options): ...
async def close(self): ...Audio and video track management with base classes for custom media sources, support for multiple media formats, and integration with the RTP protocol stack.
class MediaStreamTrack:
async def recv(self): ...
def stop(self): ...
class AudioStreamTrack(MediaStreamTrack):
async def recv(self): ...
class VideoStreamTrack(MediaStreamTrack):
async def recv(self): ...
async def next_timestamp(self): ...Real-time Transport Protocol implementation with senders, receivers, transceivers, and complete parameter configuration for audio/video streaming.
class RTCRtpSender:
async def send(self, parameters): ...
async def replaceTrack(self, track): ...
async def getStats(self): ...
class RTCRtpReceiver:
async def receive(self, parameters): ...
async def getStats(self): ...
class RTCRtpTransceiver:
def setCodecPreferences(self, codecs): ...
def stop(self): ...SCTP-based data channels for reliable application data transport with support for ordered/unordered delivery, partial reliability, and binary/text messages.
class RTCDataChannel:
def send(self, data): ...
def close(self): ...
class RTCDataChannelParameters:
def __init__(self, label, **options): ...Low-level transport layer including ICE connectivity establishment, DTLS security, and SCTP association management for reliable data delivery.
class RTCIceTransport:
async def start(self, remoteParameters): ...
def addRemoteCandidate(self, candidate): ...
class RTCDtlsTransport:
async def start(self, remoteParameters): ...
def getLocalParameters(self): ...
class RTCSctpTransport:
async def start(self, remoteCaps, remotePort): ...WebRTC configuration objects, RTP parameters, codec capabilities, and session description handling for connection setup and media negotiation.
class RTCConfiguration:
def __init__(self, iceServers=None, bundlePolicy=None): ...
class RTCIceServer:
def __init__(self, urls, username=None, credential=None): ...
class RTCSessionDescription:
def __init__(self, sdp, type): ...Connection statistics, media quality metrics, and transport performance monitoring for debugging and quality assurance.
class RTCStatsReport:
def add(self, stats): ...
class RTCInboundRtpStreamStats: ...
class RTCOutboundRtpStreamStats: ...
class RTCTransportStats: ...aiortc defines specific exception types for different error conditions:
class InvalidAccessError(Exception): ...
class InvalidStateError(Exception): ...
class MediaStreamError(Exception): ...Common error scenarios include invalid state transitions, access violations, and media processing failures.
# Bundle policy enumeration
class RTCBundlePolicy:
BALANCED = "balanced"
MAX_COMPAT = "max-compat"
MAX_BUNDLE = "max-bundle"
# RTP capabilities and parameters
class RTCRtpCapabilities: ...
class RTCRtpParameters: ...
class RTCRtpCodecParameters: ...
# DTLS and ICE parameters
class RTCDtlsParameters: ...
class RTCIceParameters: ...
class RTCIceCandidate: ...
# SCTP capabilities
class RTCSctpCapabilities: ...