# ionorg/ion-sfu architecture

> Architecture diagram of the GitHub repository ionorg/ion-sfu, 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/ionorg/ion-sfu
- Repository: https://github.com/ionorg/ion-sfu

## Overview

##### Main Components

a. Client Layer:
- WebRTC Clients (Browsers/Applications)
- Multiple client connections to represent the N:M relationship

b. Signaling Layer (show both options):
- JSON-RPC Signaling Server (Port 7000)
- gRPC Signaling Server (Port 50051)

c. Core SFU Components:
- Session Management
- Peer Management
- Router Component
- Publisher/Subscriber Management
- Media Processing Pipeline

d. Network/Protocol Layer:
- WebRTC Media Ports (UDP 5000-5200)
- TWCC (Transport Wide Congestion Control)
- RTCP Statistics

##### Component Relationships

- Client → Signaling Server (Control Plane)
- Client ←→ SFU (Media Plane)
- Internal Components:
- Session → Peers → Publishers/Subscribers
- Router → Tracks
- Buffer Management System

##### Specific Implementation Details

Key architectural elements:
**Media Pipeline:**
**[Publisher] → [Router] → [Buffer System] → [Subscribers]**

**Session Management:**
**[Session Controller] → [Peer Management] → [Track Management]**

**Signal Flow:**
**[Client] → [JSON-RPC/gRPC Server] → [SFU Core] → [WebRTC Stack]**

##### Important Subsystems to Highlight

From the pkg/ directory structure, emphasize:
- Buffer Management (/pkg/buffer)
- SFU Core Logic (/pkg/sfu)
- Data Channel Middleware (/pkg/middlewares/datachannel)
- Statistics Collection (/pkg/stats)
- TWCC Implementation (/pkg/twcc)

**[Clients Layer]**
- ↓
**[Signaling Layer]**
- ↓
**[SFU Core Layer]**
- ↓
**[Media Processing Layer]**
- ↓
**[Network Protocol Layer]**

##### Additional Notes

- Port numbers for all network interfaces
- Monitoring/statistics collection points
- Security boundaries

## Diagram (Mermaid)

```mermaid
graph TB
    %% Style Definitions
    classDef client fill:#3498db,stroke:#2980b9,color:white
    classDef signaling fill:#2ecc71,stroke:#27ae60,color:white
    classDef core fill:#e74c3c,stroke:#c0392b,color:white
    classDef media fill:#f1c40f,stroke:#f39c12,color:black
    classDef network fill:#95a5a6,stroke:#7f8c8d,color:white
    
    %% Client Layer
    subgraph Clients
        C1[Browser Client 1]
        C2[Browser Client 2]
        C3[Browser Client N]
    end
    C1:::client
    C2:::client
    C3:::client

    %% Signaling Layer
    subgraph SignalingServers
        JRPC["JSON-RPC Server<br/>(Port 7000)"]
        GRPC["gRPC Server<br/>(Port 50051)"]
    end
    JRPC:::signaling
    GRPC:::signaling
    
    %% Core SFU Layer
    subgraph CoreSFU
        SFU[SFU Core]
        SM[Session Manager]
        PM[Peer Manager]
        RC[Router Component]
        PubM[Publisher Manager]
        SubM[Subscriber Manager]
    end
    SFU:::core
    SM:::core
    PM:::core
    RC:::core
    PubM:::core
    SubM:::core

    %% Media Processing Layer
    subgraph MediaProcessing
        BS[Buffer System]
        MP[Media Pipeline]
        ALS[Audio Level System]
        TWCC[TWCC Handler]
    end
    BS:::media
    MP:::media
    ALS:::media
    TWCC:::media

    %% Network Layer
    subgraph NetworkProtocols
        WMP["WebRTC Media Ports<br/>(UDP 5000-5200)"]
        DCM[DataChannel Middleware]
        Stats[Statistics Collection]
    end
    WMP:::network
    DCM:::network
    Stats:::network

    %% Relationships
    C1 & C2 & C3 --> JRPC & GRPC
    JRPC & GRPC --> SFU
    SFU --> SM
    SM --> PM
    PM --> RC
    RC --> PubM & SubM
    PubM & SubM --> BS
    BS --> MP
    MP --> WMP
    RC --> TWCC
    RC --> ALS
    SFU --> DCM
    SFU --> Stats

    %% Click Events
    click JRPC "https://github.com/ionorg/ion-sfu/tree/master/cmd/signal/json-rpc/"
    click GRPC "https://github.com/ionorg/ion-sfu/tree/master/cmd/signal/grpc/"
    click SM "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/session.go"
    click PM "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/peer.go"
    click RC "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/router.go"
    click PubM "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/publisher.go"
    click SubM "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/subscriber.go"
    click BS "https://github.com/ionorg/ion-sfu/tree/master/pkg/buffer/"
    click MP "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/downtrack.go"
    click DCM "https://github.com/ionorg/ion-sfu/tree/master/pkg/middlewares/datachannel/"
    click Stats "https://github.com/ionorg/ion-sfu/tree/master/pkg/stats/"
    click TWCC "https://github.com/ionorg/ion-sfu/tree/master/pkg/twcc/"
    click ALS "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/audioobserver.go"
    click SFU "https://github.com/ionorg/ion-sfu/blob/master/pkg/sfu/sfu.go"

    %% Legend
    subgraph Legend
        L1[Client Components]:::client
        L2[Signaling Components]:::signaling
        L3[Core Components]:::core
        L4[Media Components]:::media
        L5[Network Components]:::network
    end
```
