or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mddata-channels.mdindex.mdmedia-streaming.mdnetwork-transport.mdpeer-connection.mdrtp-transport.mdstatistics.md
tile.json

tessl/pypi-aiortc

Python implementation of WebRTC and ORTC for real-time peer-to-peer communication

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/aiortc@1.13.x

To install, run

npx @tessl/cli install tessl/pypi-aiortc@1.13.0

index.mddocs/

aiortc

A 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.

Package Information

  • Package Name: aiortc
  • Language: Python
  • Installation: pip install aiortc
  • Version: 1.13.0

Core Imports

import aiortc

Common imports for WebRTC applications:

from aiortc import (
    RTCPeerConnection,
    RTCConfiguration,
    RTCIceServer,
    RTCSessionDescription,
    AudioStreamTrack,
    VideoStreamTrack
)

Basic Usage

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())

Architecture

aiortc follows the WebRTC standard architecture with these key components:

  • RTCPeerConnection: Main entry point managing the complete peer-to-peer connection lifecycle
  • Transport Layer: ICE, DTLS, and SCTP transports handling connectivity, security, and data delivery
  • Media Layer: RTP senders/receivers and transceivers for audio/video streaming
  • Data Channels: SCTP-based reliable data transport for application messages
  • Media Streams: Abstract base classes for custom audio/video track implementations

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.

Capabilities

Peer Connection Management

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): ...

Peer Connection

Media Streaming

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): ...

Media Streaming

RTP Transport

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): ...

RTP Transport

Data Channels

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): ...

Data Channels

Network Transport

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): ...

Network Transport

Configuration and Parameters

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): ...

Configuration

Statistics and Monitoring

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: ...

Statistics

Exception Handling

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.

Types

# 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: ...