How ossrs/srs works
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: app App layer · srs_main_server.cpp Server core · protocol Protocols · proxy Proxy logic
Components 19
SRS server
C++ media server under trunk/ with protocol, kernel, and app layers
- Server core · Primary media-server executable and core app orchestration
trunk/src/main/srs_main_server.cpp - App layer · Connection handling, delivery, HTTP control plane, clustering, and lifecycle services
trunk/src/app - Protocols · Protocol stacks and wire formats for RTMP, RTC, RTSP, SRT, HTTP, and related sessions
trunk/src/protocol - Media kernel · Codecs, containers, packetization, stream helpers, and timing/bandwidth utilities
trunk/src/kernel - Core utils · 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 · Proxy service entrypoint
cmd/proxy/main.go - Bootstrap · Wires environment, config, and process lifecycle for the proxy
internal/bootstrap/bootstrap.go - Proxy logic · Protocol frontends and request routing for HTTP, RTMP, RTC, and SRT
internal/proxy - Load balancer · Origin selection using in-memory or Redis-backed state
internal/lb - Redis 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 · Cooperative networking/runtime foundation for the C++ server
trunk/3rdparty/st-srs - FFmpeg fit · Vendored media format, codec, resampling, and mux/demux stack
trunk/3rdparty/ffmpeg-4-fit - OpenSSL fit · TLS, crypto, and DTLS/SRTP-adjacent primitives
trunk/3rdparty/openssl-1.1-fit - SRT fit · SRT transport implementation and crypto helpers
trunk/3rdparty/srt-1-fit - SRTP fit · SRTP packet protection and replay/crypto primitives
trunk/3rdparty/libsrtp-2-fit
Tooling
Build, configs, docs, tests, and packaging around the runtimes
- Bench tests · Blackbox and protocol validation tooling for server and proxy workflows
trunk/3rdparty/srs-bench - Configs · Reference configs for ingest, delivery, HTTP API, clustering, and proxy modes
trunk/conf - Docs · Repository documentation for server, proxy, and deployment patterns
docs
Other components
- srs · C++ SRS media server plus optional Go proxy and tooling
Connections 23
- 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