# aiortc/aiortc architecture

> Architecture diagram of the GitHub repository aiortc/aiortc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aiortc/aiortc
- Repository: https://github.com/aiortc/aiortc

## Overview

##### Main Components

a. Core WebRTC Components:
- RTC Peer Connection (central component)
- ICE Transport Layer
- DTLS Transport Layer
- SCTP Transport Layer
- Media Subsystem
- Data Channel System

b. Media Processing Pipeline:
- Audio Codecs (Opus, G711)
- Video Codecs (VP8, H.264)
- Media Streams Handler
- Jitter Buffer
- RTP/RTCP Handler

c. Signaling and Control:
- SDP Processing
- Session Management
- Event System
- Statistics Collection

##### Component Relationships

**Application Layer**

- ▼
**RTC Peer Connection**

- ┬─────────────────┐
- ▼                    ▼                 ▼
**Media Pipeline     Data Channels    Session Control**

- ▼                    ▼                 ▼
**Codec Layer        SCTP Transport    SDP Processing**

- ┬────────┴─────────┬───────┘
- ▼                   ▼
- DTLS Transport     ICE Transport

- ┬────────┘
- ▼
- Network Layer

##### Implementation Details to Highlight

- Relationship between the following key components found in src/aiortc:
- rtcpeerconnection.py
- rtcdatachannel.py
- mediastreams.py
- codecs/ directory
- contrib/ directory

##### External Integrations

- Integration points with:
- Opus library
- LibVPX library
- Network interfaces
- Application-level APIs

##### Notes

- Keep the main WebRTC flow clear and central
- Both control and media paths
- Async boundaries
- Public APIs
- Error handling paths
- Version/compatibility indicators

Remember to include the examples/ directory use cases as optional overlays to show how the architecture supports different applications (datachannel-cli, videostream-cli, etc.).

This system design should emphasize aiortc's unique features as a Python WebRTC implementation while maintaining accuracy to the WebRTC specification.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Application Layer"
        APP[Application Interface]
    end

    subgraph "Core WebRTC"
        RTC[RTC Peer Connection]:::core
        click RTC "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtcpeerconnection.py"
        EVENT[Event System]:::core
        click EVENT "https://github.com/aiortc/aiortc/blob/main/src/aiortc/events.py"
        STATS[Statistics Collection]:::core
        click STATS "https://github.com/aiortc/aiortc/blob/main/src/aiortc/stats.py"
    end

    subgraph "Media Processing"
        MEDIA[Media Subsystem]:::media
        click MEDIA "https://github.com/aiortc/aiortc/blob/main/src/aiortc/mediastreams.py"
        JB[Jitter Buffer]:::media
        click JB "https://github.com/aiortc/aiortc/blob/main/src/aiortc/jitterbuffer.py"
        RTP[RTP/RTCP Handler]:::media
        click RTP "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtp.py"
        
        subgraph "Codecs"
            OPUS[Opus Codec]:::codec
            click OPUS "https://github.com/aiortc/aiortc/blob/main/src/aiortc/codecs/opus.py"
            G711[G711 Codec]:::codec
            click G711 "https://github.com/aiortc/aiortc/blob/main/src/aiortc/codecs/g711.py"
            VP8[VP8 Codec]:::codec
            click VP8 "https://github.com/aiortc/aiortc/blob/main/src/aiortc/codecs/vpx.py"
            H264[H264 Codec]:::codec
            click H264 "https://github.com/aiortc/aiortc/blob/main/src/aiortc/codecs/h264.py"
        end
    end

    subgraph "Data Channel"
        DC[Data Channel System]:::data
        click DC "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtcdatachannel.py"
    end

    subgraph "Session Control"
        SDP[SDP Processing]:::session
        click SDP "https://github.com/aiortc/aiortc/blob/main/src/aiortc/sdp.py"
        SIG[Signaling]:::session
        click SIG "https://github.com/aiortc/aiortc/blob/main/src/aiortc/contrib/signaling.py"
    end

    subgraph "Transport Layer"
        SCTP[SCTP Transport]:::transport
        click SCTP "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtcsctptransport.py"
        DTLS[DTLS Transport]:::transport
        click DTLS "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtcdtlstransport.py"
        ICE[ICE Transport]:::transport
        click ICE "https://github.com/aiortc/aiortc/blob/main/src/aiortc/rtcicetransport.py"
    end

    subgraph "Network Layer"
        NET[Network Interface]:::network
    end

    %% Connections
    APP --> RTC
    RTC --> EVENT
    RTC --> STATS
    RTC --> MEDIA
    RTC --> DC
    RTC --> SDP
    
    MEDIA --> JB
    MEDIA --> RTP
    MEDIA --> OPUS & G711 & VP8 & H264
    
    DC --> SCTP
    SDP --> SIG
    
    SCTP & DTLS --> ICE
    ICE --> NET

    %% Styles
    classDef core fill:#a8d1f0,stroke:#333
    classDef media fill:#90EE90,stroke:#333
    classDef codec fill:#98FB98,stroke:#333
    classDef data fill:#FFB6C1,stroke:#333
    classDef session fill:#DDA0DD,stroke:#333
    classDef transport fill:#F0E68C,stroke:#333
    classDef network fill:#D3D3D3,stroke:#333

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Media Processing]:::media
        L3[Codecs]:::codec
        L4[Data Channel]:::data
        L5[Session Control]:::session
        L6[Transport]:::transport
        L7[Network]:::network
    end
```
