# ossrs/srs architecture

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

- Interactive diagram: https://gitdiagram.com/ossrs/srs
- Repository: https://github.com/ossrs/srs

## Overview

1. **What this repo is**
   This repository is the SRS media server codebase, centered on a high-performance streaming engine plus an optional Go-based proxy. The main product is the C++ server under `trunk/`; the Go proxy lives at the repo root in `cmd/proxy` and `internal/`.

2. **Top-level split of responsibilities**
   `trunk/` contains the actual realtime media server: protocol handling, media parsing/muxing, delivery, clustering, and the built-in HTTP/API surface.  
   `cmd/proxy` and `internal/` contain a separate proxy service that routes streams to origins and coordinates cluster state, including Redis-backed balancing.

3. **Ingress protocol layer**
   The server accepts multiple live input protocols: RTMP, WebRTC, SRT, RTSP, GB28181, and HTTP-based ingest. In the C++ tree these map to protocol modules like `srs_app_rtmp_conn`, `srs_app_rtc_conn`, `srs_app_srt_conn`, `srs_app_rtsp_conn`, and `srs_app_gb28181`. Each protocol has its own handshake/session logic but converges onto a shared stream model.

4. **Shared stream and session model**
   The core architectural boundary is between protocol-specific connection objects and shared stream/source objects. Files such as `srs_app_*_source`, `srs_app_stream_bridge`, `srs_app_stream_token`, and kernel stream/packet helpers indicate that once media is accepted, it is normalized into internal stream state that can be republished to other protocols.

5. **Media parsing and container/codec kernel**
   `trunk/src/kernel` holds the media primitives: FLV, TS, MP4, AAC, MP3, PS, RTP/RTCP, and codec helpers. This layer is where raw protocol payloads are interpreted into audio/video frames and remuxed into delivery formats. It is the main “media processing boundary” between network protocols and outputs.

6. **Egress and delivery subsystems**
   SRS can serve the same live source out through HLS, HTTP-FLV, MPEG-DASH, RTMP, RTC/WHEP, and MPEG-TS over UDP. Those are implemented in `trunk/src/app/srs_app_hls.cpp`, `srs_app_hds.cpp`, `srs_app_dash.cpp`, `srs_app_http_stream.cpp`, `srs_app_rtc_server.cpp`, and related modules. DVR, forwarding, transcoding, ingest, and edge/origin replication are additional delivery/processing paths layered on top.

7. **HTTP control plane and static serving**
   The server includes an HTTP plane separate from media transport. It provides API endpoints, hooks/callbacks, static content, stream inspection, and heartbeat/reload/security functionality. The relevant modules are `srs_app_http_api`, `srs_app_http_hooks`, `srs_app_http_static`, `srs_app_http_stream`, `srs_app_heartbeat`, `srs_app_reload`, and `srs_app_security`.

8. **Runtime and concurrency model**
   SRS uses State Threads-style cooperative networking rather than a generic request/response web framework. That shows up in `src/app/srs_app_st.*` and `src/protocol/srs_protocol_st.*`, plus coroutine-oriented support code across the app/kernel layers. The architecture is optimized for many concurrent long-lived streaming connections with low overhead.

9. **Observability and diagnostics**
   Logging is a first-class subsystem (`srs_app_log`, `srs_kernel_log`, `internal/logger` in the Go proxy). There is also built-in pprof support in the Go proxy, versioning helpers, statistics modules, and performance-oriented utilities such as pithy print and kbps tracking in the C++ core. The repo also ships extensive test and blackbox tooling for protocol and cluster validation.

10. **Go proxy architecture**
    The proxy binary (`cmd/proxy/main.go`) is structured around `internal/bootstrap`, `internal/proxy`, and `internal/lb`. The proxy has protocol-specific frontends for HTTP, RTMP, RTC, and SRT, then routes traffic through a load-balancing layer that can use in-memory state or Redis. This makes it an edge/origin coordination layer rather than the media engine itself.

11. **Cluster and balancing flow**
    For clustered deployments, the proxy and SRS origin/edge modules form a routing chain: client traffic lands on the proxy, the proxy selects an origin via local memory or Redis, and SRS then publishes or serves the stream from the chosen node. The repository includes configs and docs for origin cluster, edge mode, forward replication, and Redis-backed balancing.

12. **External dependencies that materially shape the design**
    The C++ server vendors patched third-party media/network libraries under `trunk/3rdparty`, including FFmpeg, OpenSSL, libsrtp, libsrt, gperftools, gtest, and State Threads. These are not incidental: they define codec support, crypto/TLS/SRTP, SRT transport, allocation/profiling, and the coroutine runtime.

13. **Build, packaging, and deployment boundaries**
    The repo supports both CMake and Makefile-based builds, plus Dockerfiles, systemd service files, and many runtime configs under `trunk/conf`. The `.run` and `skills/` scripts indicate repeatable e2e and protocol tests for RTMP, RTC, SRT, GB28181, cluster, and transmux workflows.

14. **How to think about the architecture diagram**
    Draw the C++ SRS server as the central media hub with protocol-specific ingress on one side, shared stream/media kernel in the middle, and multiple delivery/protocol outputs on the other. Draw the Go proxy as a separate control/routing component in front of or beside the SRS cluster, with Redis as the shared coordination backend for multi-node deployments.

Main entry points: [`trunk/src/app`](https://github.com/ossrs/srs/tree/develop/trunk/src/app) (App layer), [`trunk/src/main/srs_main_server.cpp`](https://github.com/ossrs/srs/blob/develop/trunk/src/main/srs_main_server.cpp) (Server core), [`trunk/src/protocol`](https://github.com/ossrs/srs/tree/develop/trunk/src/protocol) (Protocols), [`internal/proxy`](https://github.com/ossrs/srs/tree/develop/internal/proxy) (Proxy logic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_cpp["SRS server"]
  node_cpp_main["Server core<br/>C++ runtime"]
  node_cpp_app["App layer<br/>C++ services"]
  node_cpp_protocol["Protocols<br/>C++ ingress"]
  node_cpp_kernel["Media kernel<br/>C++ media primitives"]
  node_cpp_core["Core utils<br/>C++ core"]
end

subgraph group_grp_go["Go proxy"]
  node_go_main["Proxy binary<br/>Go entrypoint<br/>[main.go]"]
  node_go_bootstrap["Bootstrap<br/>Go startup<br/>[bootstrap.go]"]
  node_go_proxy["Proxy logic<br/>Go routing"]
  node_go_lb["Load balancer<br/>Go balance"]
  node_go_redis["Redis client<br/>Go client<br/>[redisclient.go]"]
end

subgraph group_grp_deps["Vendored deps"]
  node_dep_st["State Threads<br/>coroutine runtime"]
  node_dep_ffmpeg["FFmpeg fit<br/>media codec libs"]
  node_dep_openssl["OpenSSL fit<br/>crypto/TLS libs<br/>[openssl-1.1-fit]"]
  node_dep_srt["SRT fit<br/>transport libs"]
  node_dep_srtp["SRTP fit<br/>crypto/media libs"]
end

subgraph group_grp_tools["Tooling"]
  node_bench["Bench tests<br/>Go/C++ validation"]
  node_conf["Configs<br/>deployment config"]
  node_docs["Docs"]
end

node_repo["srs<br/>monorepo"]

node_repo -->|"hosts"| node_cpp_main
node_repo -->|"ships"| node_go_main
node_cpp_main -->|"starts"| node_cpp_app
node_cpp_main -->|"uses"| node_cpp_protocol
node_cpp_main -->|"uses"| node_cpp_kernel
node_cpp_app -->|"accepts"| node_cpp_protocol
node_cpp_app -->|"normalizes"| node_cpp_kernel
node_cpp_app -->|"configured by"| node_conf
node_cpp_app -->|"documented by"| node_docs
node_cpp_protocol -->|"runs on"| node_dep_st
node_cpp_protocol -->|"secures with"| node_dep_openssl
node_cpp_protocol -->|"transports with"| node_dep_srt
node_cpp_kernel -->|"parses/remuxes with"| node_dep_ffmpeg
node_cpp_kernel -->|"protects with"| node_dep_srtp
node_go_main -->|"initializes"| node_go_bootstrap
node_go_bootstrap -->|"wires"| node_go_proxy
node_go_proxy -->|"routes via"| node_go_lb
node_go_lb -->|"may use"| node_go_redis
node_go_proxy -->|"forwards to"| node_cpp_main
node_go_proxy -->|"validated by"| node_bench
node_cpp_app -->|"validated by"| node_bench
node_conf -->|"supports"| node_go_proxy
node_docs -->|"explains"| node_go_lb

click node_cpp_main "https://github.com/ossrs/srs/blob/develop/trunk/src/main/srs_main_server.cpp"
click node_cpp_app "https://github.com/ossrs/srs/tree/develop/trunk/src/app"
click node_cpp_protocol "https://github.com/ossrs/srs/tree/develop/trunk/src/protocol"
click node_cpp_kernel "https://github.com/ossrs/srs/tree/develop/trunk/src/kernel"
click node_cpp_core "https://github.com/ossrs/srs/tree/develop/trunk/src/core"
click node_go_main "https://github.com/ossrs/srs/blob/develop/cmd/proxy/main.go"
click node_go_bootstrap "https://github.com/ossrs/srs/blob/develop/internal/bootstrap/bootstrap.go"
click node_go_proxy "https://github.com/ossrs/srs/tree/develop/internal/proxy"
click node_go_lb "https://github.com/ossrs/srs/tree/develop/internal/lb"
click node_go_redis "https://github.com/ossrs/srs/blob/develop/internal/redisclient/redisclient.go"
click node_dep_st "https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/st-srs"
click node_dep_ffmpeg "https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/ffmpeg-4-fit"
click node_dep_openssl "https://github.com/ossrs/srs/blob/develop/trunk/3rdparty/openssl-1.1-fit"
click node_dep_srt "https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/srt-1-fit"
click node_dep_srtp "https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/libsrtp-2-fit"
click node_bench "https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/srs-bench"
click node_conf "https://github.com/ossrs/srs/tree/develop/trunk/conf"
click node_docs "https://github.com/ossrs/srs/tree/develop/docs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cpp_main,node_cpp_app,node_cpp_protocol,node_cpp_kernel,node_cpp_core toneBlue
class node_go_main,node_go_bootstrap,node_go_proxy,node_go_lb,node_go_redis toneAmber
class node_dep_st,node_dep_ffmpeg,node_dep_openssl,node_dep_srt,node_dep_srtp toneMint
class node_bench,node_conf,node_docs toneRose
class node_repo toneNeutral
```

## Components

### SRS server

C++ media server under trunk/ with protocol, kernel, and app layers

- [Server core](https://github.com/ossrs/srs/blob/develop/trunk/src/main/srs_main_server.cpp): C++ runtime · Primary media-server executable and core app orchestration · `trunk/src/main/srs_main_server.cpp`
- [App layer](https://github.com/ossrs/srs/tree/develop/trunk/src/app): C++ services · Connection handling, delivery, HTTP control plane, clustering, and lifecycle services · `trunk/src/app`
- [Protocols](https://github.com/ossrs/srs/tree/develop/trunk/src/protocol): C++ ingress · Protocol stacks and wire formats for RTMP, RTC, RTSP, SRT, HTTP, and related sessions · `trunk/src/protocol`
- [Media kernel](https://github.com/ossrs/srs/tree/develop/trunk/src/kernel): C++ media primitives · Codecs, containers, packetization, stream helpers, and timing/bandwidth utilities · `trunk/src/kernel`
- [Core utils](https://github.com/ossrs/srs/tree/develop/trunk/src/core): C++ core · Versioning, platform, time, and shared core helpers · `trunk/src/core`

### Go proxy

Separate proxy service at repo root for routing and cluster coordination

- [Proxy binary](https://github.com/ossrs/srs/blob/develop/cmd/proxy/main.go): Go entrypoint · Proxy service entrypoint · `cmd/proxy/main.go`
- [Bootstrap](https://github.com/ossrs/srs/blob/develop/internal/bootstrap/bootstrap.go): Go startup · Wires environment, config, and process lifecycle for the proxy · `internal/bootstrap/bootstrap.go`
- [Proxy logic](https://github.com/ossrs/srs/tree/develop/internal/proxy): Go routing · Protocol frontends and request routing for HTTP, RTMP, RTC, and SRT · `internal/proxy`
- [Load balancer](https://github.com/ossrs/srs/tree/develop/internal/lb): Go balance · Origin selection using in-memory or Redis-backed state · `internal/lb`
- [Redis client](https://github.com/ossrs/srs/blob/develop/internal/redisclient/redisclient.go): Go client · Redis connectivity used by the proxy balance layer · `internal/redisclient/redisclient.go`

### Vendored deps

Patched third-party libraries that shape transport, crypto, and runtime

- [State Threads](https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/st-srs): coroutine runtime · Cooperative networking/runtime foundation for the C++ server · `trunk/3rdparty/st-srs`
- [FFmpeg fit](https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/ffmpeg-4-fit): media codec libs · Vendored media format, codec, resampling, and mux/demux stack · `trunk/3rdparty/ffmpeg-4-fit`
- [OpenSSL fit](https://github.com/ossrs/srs/blob/develop/trunk/3rdparty/openssl-1.1-fit): crypto/TLS libs · TLS, crypto, and DTLS/SRTP-adjacent primitives · `trunk/3rdparty/openssl-1.1-fit`
- [SRT fit](https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/srt-1-fit): transport libs · SRT transport implementation and crypto helpers · `trunk/3rdparty/srt-1-fit`
- [SRTP fit](https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/libsrtp-2-fit): crypto/media libs · SRTP packet protection and replay/crypto primitives · `trunk/3rdparty/libsrtp-2-fit`

### Tooling

Build, configs, docs, tests, and packaging around the runtimes

- [Bench tests](https://github.com/ossrs/srs/tree/develop/trunk/3rdparty/srs-bench): Go/C++ validation · Blackbox and protocol validation tooling for server and proxy workflows · `trunk/3rdparty/srs-bench`
- [Configs](https://github.com/ossrs/srs/tree/develop/trunk/conf): deployment config · Reference configs for ingest, delivery, HTTP API, clustering, and proxy modes · `trunk/conf`
- [Docs](https://github.com/ossrs/srs/tree/develop/docs): architecture docs · Repository documentation for server, proxy, and deployment patterns · `docs`

### Other components

- **srs**: monorepo · C++ SRS media server plus optional Go proxy and tooling

## Connections

- srs → Server core: hosts · Top-level server executable lives in trunk/
- srs → Proxy binary: ships · Repository also builds a separate proxy binary
- Server core → App layer: starts · Main program boots the service layer
- Server core → Protocols: uses · Service delegates wire handling to protocol stacks
- Server core → Media kernel: uses · Service relies on media primitives and stream helpers
- App layer → Protocols: accepts · Protocol connections converge into app-level sessions
- App layer → Media kernel: normalizes · Accepted media is converted into shared stream state
- App layer → Configs: configured by · Runtime behavior is driven by trunk/conf profiles
- App layer → Docs: documented by · Architecture and feature docs describe the service layer
- Protocols → State Threads: runs on · Protocol networking uses State Threads-style concurrency
- Protocols → OpenSSL fit: secures with · TLS and DTLS/SRTP paths depend on OpenSSL
- Protocols → SRT fit: transports with · SRT protocol support depends on vendored SRT
- Media kernel → FFmpeg fit: parses/remuxes with · Media formats and codec support rely on FFmpeg-fit
- Media kernel → SRTP fit: protects with · RTC media security and replay protection use SRTP
- Proxy binary → Bootstrap: initializes · Main delegates startup wiring to bootstrap
- Bootstrap → Proxy logic: wires · Bootstrap constructs proxy services and handlers
- Proxy logic → Load balancer: routes via · Proxy forwards requests through the balancing layer
- Load balancer → Redis client: may use · Cluster selection can be backed by Redis state
- Proxy logic → Server core: forwards to · Proxy is an edge/origin coordinator in front of SRS nodes
- Proxy logic → Bench tests: validated by · Proxy workflows are exercised by e2e and protocol tests
- App layer → Bench tests: validated by · Server workflows are exercised by blackbox tests
- Configs → Proxy logic: supports · Proxy deployment has dedicated config examples
- Docs → Load balancer: explains · Proxy design docs cover load balancing and origin selection
