# kmansoft/srtc architecture

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

- Interactive diagram: https://gitdiagram.com/kmansoft/srtc
- Repository: https://github.com/kmansoft/srtc

## Overview

##### Main Components
- Peer Connection Module: Central component that handles the SDP offer/answer exchange, state management, and overall connection lifecycle.
- Media Packetization: Separate modules dealing with packetizing H264 video, Opus audio, and simulcast (e.g., “packetizer.cpp”, “packetizer_h264.cpp”, “packetizer_opus.cpp”).
- ICE/STUN Negotiation: Modules in the “stun” folder and the ICE agent (files like “ice_agent.cpp” and “stun/stun*.c/h”) that manage NAT traversal and connectivity checks.
- SRTP/SRTCP and Security: Components responsible for authentication and encryption (files like “srtp_connection.cpp”, “srtp_crypto.cpp”, “srtp_openssl.cpp”, “srtp_hmac_sha1.cpp”).
- SDP Encoding/Decoding: Components that prepare and interpret SDP offers and answers (sdp_offer.cpp, sdp_answer.cpp).
- Utility and Error Management: Helper modules for logging, buffering, random generation, and error handling (e.g., “logging.cpp”, “byte_buffer.cpp”, “error.cpp”, “random_generator.cpp”).

##### Relationships and Interactions
- Data Flow: Illustrate the flow from the application layer to the Peer Connection, which then triggers the underlying ICE/STUN procedures, negotiates DTLS, and subsequently establishes the SRTP connection.
- Packet Handling: Show how encoded media samples provided by the application are passed to the packetizer modules which then prepare them for transmission over the established connection.
- Control & Error Flow: Indicate that the Peer Connection monitors connectivity through periodic STUN probes and error logging, triggering re-connection attempts as needed.
- SDP Role: Mark a distinct section where the SDP offer/answer mechanism integrates with external HTTP libraries (as mentioned in the README) to communicate with a WHIP server.
- Simulcast Support: Indicate a branch where additional layers and codec-specific configurations are managed (e.g., “packetizer_h264.cpp” and related codec-specific methods).

##### Architectural Patterns and Design Principles
- Modularity: Each core function (networking, security, media processing) is encapsulated into separate modules and source files.
- Separation of Concerns: The design keeps media encoding out of scope while taking care of packetization, ensuring that each component does one task (e.g., ICE negotiation is separate from packetization).
- Lightweight Threading: Emphasize that a single worker thread is used per PeerConnection, which is critical for scaling to hundreds or thousands of sessions.
- Cross-Cutting Utilities: Utility modules (like logging and error handling) support and are integrated with multiple other components.

##### Relevant Technologies and Frameworks
- C++ 17 (or C++ 14 with minor modification) – conservative and portable code.
- OpenSSL/BoringSSL – for cryptographic operations in DTLS/SRTP.
- Networking protocols – ICE, STUN, DTLS, and SRTP, represented by dedicated modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global nodes
    App("Application"):::external

    PC("Peer Connection"):::connection

    subgraph "SDP & Signaling"
        SDP_offer("SDP Offer"):::signaling
        SDP_answer("SDP Answer"):::signaling
    end

    subgraph "Media Packetization"
        H264("H264 Packetizer"):::media
        Opus("Opus Packetizer"):::media
        Generic("Generic Packetizer"):::media
    end

    subgraph "Networking"
        ICE("ICE Agent"):::networking
        STUN("STUN Module"):::networking
    end

    subgraph "Security"
        SRTP_conn("SRTP Connection Manager"):::security
        SRTP_crypto("SRTP Crypto"):::security
        SRTP_openssl("SRTP OpenSSL Integration"):::security
        SRTP_hmac("SRTP HMAC-SHA1"):::security
        SRTP_util("SRTP Util"):::security
    end

    subgraph "Utilities"
        Log("Logging"):::utility
        Buffer("Byte Buffer"):::utility
        Error("Error Handling"):::utility
        Rand("Random Generation"):::utility
        Util("Additional Utilities"):::utility
    end

    WHIP("WHIP Server"):::external

    %% Connections - vertical layout
    App -->|"media_sample"| PC

    %% Peer Connection interactions
    PC -->|"initiates"| SDP_offer
    PC -->|"initiates"| SDP_answer
    PC -->|"uses"| H264
    PC -->|"uses"| Opus
    PC -->|"uses"| Generic
    PC -->|"negotiates"| ICE
    ICE -->|"performs"| STUN
    PC -->|"secures"| SRTP_conn

    %% Security dependencies
    SRTP_conn -->|"depends_on"| SRTP_crypto
    SRTP_conn -->|"depends_on"| SRTP_openssl
    SRTP_conn -->|"depends_on"| SRTP_hmac
    SRTP_conn -->|"depends_on"| SRTP_util

    %% SDP exchanges with external server
    SDP_offer -->|"exchanges"| WHIP
    SDP_answer -->|"exchanges"| WHIP

    %% Utilities integration (selected connections)
    PC -->|"logs"| Log
    H264 -->|"util_support"| Buffer
    Opus -->|"util_support"| Error
    Generic -->|"util_support"| Rand
    ICE -->|"logs"| Log
    SRTP_conn -->|"logs"| Log

    %% Styles
    classDef connection fill:#ffd700,stroke:#333,stroke-width:2px;
    classDef media fill:#adff2f,stroke:#333,stroke-width:2px;
    classDef networking fill:#87cefa,stroke:#333,stroke-width:2px;
    classDef signaling fill:#ffb6c1,stroke:#333,stroke-width:2px;
    classDef security fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffa07a,stroke:#333,stroke-width:2px;
    classDef external fill:#cfc,stroke:#333,stroke-width:2px;

    %% Click Events
    click PC "https://github.com/kmansoft/srtc/blob/main/peer_connection.cpp"
    click PC "https://github.com/kmansoft/srtc/blob/main/include/srtc/peer_connection.h"

    click H264 "https://github.com/kmansoft/srtc/blob/main/packetizer_h264.cpp"
    click H264 "https://github.com/kmansoft/srtc/blob/main/include/srtc/packetizer_h264.h"

    click Opus "https://github.com/kmansoft/srtc/blob/main/packetizer_opus.cpp"
    click Opus "https://github.com/kmansoft/srtc/blob/main/include/srtc/packetizer_opus.h"

    click Generic "https://github.com/kmansoft/srtc/blob/main/packetizer.cpp"
    click Generic "https://github.com/kmansoft/srtc/blob/main/include/srtc/packetizer.h"

    click ICE "https://github.com/kmansoft/srtc/blob/main/ice_agent.cpp"
    click ICE "https://github.com/kmansoft/srtc/blob/main/include/srtc/ice_agent.h"

    click STUN "https://github.com/kmansoft/srtc/tree/main/stun"

    click SRTP_conn "https://github.com/kmansoft/srtc/blob/main/srtp_connection.cpp"
    click SRTP_conn "https://github.com/kmansoft/srtc/blob/main/include/srtc/srtp_connection.h"

    click SRTP_crypto "https://github.com/kmansoft/srtc/blob/main/srtp_crypto.cpp"
    click SRTP_crypto "https://github.com/kmansoft/srtc/blob/main/include/srtc/srtp_crypto.h"

    click SRTP_openssl "https://github.com/kmansoft/srtc/blob/main/srtp_openssl.cpp"
    click SRTP_openssl "https://github.com/kmansoft/srtc/blob/main/include/srtc/srtp_openssl.h"

    click SRTP_hmac "https://github.com/kmansoft/srtc/blob/main/srtp_hmac_sha1.cpp"
    click SRTP_hmac "https://github.com/kmansoft/srtc/blob/main/include/srtc/srtp_hmac_sha1.h"

    click SRTP_util "https://github.com/kmansoft/srtc/blob/main/srtp_util.cpp"
    click SRTP_util "https://github.com/kmansoft/srtc/blob/main/include/srtc/srtp_util.h"

    click SDP_offer "https://github.com/kmansoft/srtc/blob/main/sdp_offer.cpp"
    click SDP_offer "https://github.com/kmansoft/srtc/blob/main/include/srtc/sdp_offer.h"

    click SDP_answer "https://github.com/kmansoft/srtc/blob/main/sdp_answer.cpp"
    click SDP_answer "https://github.com/kmansoft/srtc/blob/main/include/srtc/sdp_answer.h"

    click Log "https://github.com/kmansoft/srtc/blob/main/logging.cpp"
    click Log "https://github.com/kmansoft/srtc/blob/main/include/srtc/logging.h"

    click Buffer "https://github.com/kmansoft/srtc/blob/main/byte_buffer.cpp"
    click Buffer "https://github.com/kmansoft/srtc/blob/main/include/srtc/byte_buffer.h"

    click Error "https://github.com/kmansoft/srtc/blob/main/error.cpp"
    click Error "https://github.com/kmansoft/srtc/blob/main/include/srtc/error.h"

    click Rand "https://github.com/kmansoft/srtc/blob/main/random_generator.cpp"
    click Rand "https://github.com/kmansoft/srtc/blob/main/include/srtc/random_generator.h"

    click Util "https://github.com/kmansoft/srtc/blob/main/util.cpp"
    click Util "https://github.com/kmansoft/srtc/blob/main/include/srtc/util.h"
```
