diff --git a/.openclaw/memory/srs-codebase-map.md b/.openclaw/memory/srs-codebase-map.md index 3f100c956..b7799a8f0 100644 --- a/.openclaw/memory/srs-codebase-map.md +++ b/.openclaw/memory/srs-codebase-map.md @@ -215,7 +215,7 @@ The next-generation server (`cmd/` + `internal/`) is written in Go and maintaine `internal/bootstrap` — Server startup and lifecycle orchestration. Sets up logging context, signal handlers, loads environment, installs force-quit timer, optionally starts pprof, initializes the load balancer (memory or Redis based on `PROXY_LOAD_BALANCER_TYPE`), then starts all six servers sequentially (RTMP, WebRTC, HTTP API, SRT, System API, HTTP Stream) and blocks until context is cancelled. Deferred `Close()` on each server ensures graceful shutdown. -`internal/protocol` — Protocol proxy servers. Each server accepts client connections, parses just enough of the protocol to extract the stream URL, picks a backend via the load balancer, and proxies traffic bidirectionally. Contains five proxy servers: (1) **RTMP proxy** (`rtmp.go`) — TCP listener, simple handshake, parses connect/publish/play to get stream URL, bidirectional RTMP message copying, stateless. (2) **HTTP stream proxy** (`http.go`) — serves static files, proxies HTTP-FLV/TS via reverse-proxy, proxies HLS m3u8 with `spbhid` rewriting so TS segment requests route to the same backend. (3) **WebRTC proxy** (`rtc.go`) — two-phase: WHIP/WHEP signaling (SDP rewrite to replace backend UDP port with proxy's) + UDP media transport (identifies connections by STUN ufrag, supports address migration), stateful. (4) **SRT proxy** (`srt.go`) — intercepts SRT 4-step handshake locally, parses stream ID on handshake 2, replays full handshake with backend, then proxies UDP bidirectionally, stateful per-connection. (5) **HTTP API + System API** (`api.go`) — HTTP API delegates WHIP/WHEP to WebRTC server; System API provides `/api/v1/srs/register` where backend SRS C++ servers register themselves so the load balancer knows about them. +`internal/server` — Proxy server implementations. Each server accepts client connections, parses just enough of the protocol to extract the stream URL, picks a backend via the load balancer, and proxies traffic bidirectionally. Contains five proxy servers: (1) **RTMP proxy** (`rtmp.go`) — TCP listener, simple handshake, parses connect/publish/play to get stream URL, bidirectional RTMP message copying, stateless. (2) **HTTP stream proxy** (`http.go`) — serves static files, proxies HTTP-FLV/TS via reverse-proxy, proxies HLS m3u8 with `spbhid` rewriting so TS segment requests route to the same backend. (3) **WebRTC proxy** (`rtc.go`) — two-phase: WHIP/WHEP signaling (SDP rewrite to replace backend UDP port with proxy's) + UDP media transport (identifies connections by STUN ufrag, supports address migration), stateful. (4) **SRT proxy** (`srt.go`) — intercepts SRT 4-step handshake locally, parses stream ID on handshake 2, replays full handshake with backend, then proxies UDP bidirectionally, stateful per-connection. (5) **HTTP API + System API** (`api.go`) — HTTP API delegates WHIP/WHEP to WebRTC server; System API provides `/api/v1/srs/register` where backend SRS C++ servers register themselves so the load balancer knows about them. `internal/rtmp` — RTMP protocol implementation (parsing, not proxying). Full RTMP chunk stream and message protocol: simple handshake (C0/C1/C2), chunk stream reader/writer with all four format types, extended timestamp, message reassembly from chunks. Defines all RTMP message types, chunk stream IDs, and command names. Packet types include ConnectApp, CreateStream, Publish, Play, Call, SetChunkSize, WindowAcknowledgementSize, SetPeerBandwidth, UserControl. Uses Go generics (`ExpectPacket[T]`) to read until a specific packet type arrives. Also includes full AMF0 encoder/decoder supporting Number, Boolean, String, Object, Null, Undefined, EcmaArray, StrictArray, Date, LongString — with ordered key-value maps, auto-type-discovery, and safe type converters. diff --git a/internal/bootstrap/proxy.go b/internal/bootstrap/proxy.go index 5bd8fd7e1..11c52ff25 100644 --- a/internal/bootstrap/proxy.go +++ b/internal/bootstrap/proxy.go @@ -12,7 +12,7 @@ import ( "srsx/internal/errors" "srsx/internal/lb" "srsx/internal/logger" - "srsx/internal/protocol" + "srsx/internal/server" "srsx/internal/signal" "srsx/internal/version" ) @@ -99,42 +99,42 @@ func (b *proxyBootstrap) initializeLoadBalancer(ctx context.Context, environment // startServers initializes and starts all protocol servers. func (b *proxyBootstrap) startServers(ctx context.Context, environment env.ProxyEnvironment, gracefulQuitTimeout time.Duration) error { // Start the RTMP server. - srsRTMPServer := protocol.NewSRSRTMPServer(environment) + srsRTMPServer := server.NewSRSRTMPServer(environment) if err := srsRTMPServer.Run(ctx); err != nil { return errors.Wrapf(err, "rtmp server") } defer srsRTMPServer.Close() // Start the WebRTC server. - srsWebRTCServer := protocol.NewSRSWebRTCServer(environment) + srsWebRTCServer := server.NewSRSWebRTCServer(environment) if err := srsWebRTCServer.Run(ctx); err != nil { return errors.Wrapf(err, "rtc server") } defer srsWebRTCServer.Close() // Start the HTTP API server. - srsHTTPAPIServer := protocol.NewSRSHTTPAPIServer(environment, gracefulQuitTimeout, srsWebRTCServer) + srsHTTPAPIServer := server.NewSRSHTTPAPIServer(environment, gracefulQuitTimeout, srsWebRTCServer) if err := srsHTTPAPIServer.Run(ctx); err != nil { return errors.Wrapf(err, "http api server") } defer srsHTTPAPIServer.Close() // Start the SRT server. - srsSRTServer := protocol.NewSRSSRTServer(environment) + srsSRTServer := server.NewSRSSRTServer(environment) if err := srsSRTServer.Run(ctx); err != nil { return errors.Wrapf(err, "srt server") } defer srsSRTServer.Close() // Start the System API server. - systemAPI := protocol.NewSystemAPI(environment, gracefulQuitTimeout) + systemAPI := server.NewSystemAPI(environment, gracefulQuitTimeout) if err := systemAPI.Run(ctx); err != nil { return errors.Wrapf(err, "system api server") } defer systemAPI.Close() // Start the HTTP web server. - srsHTTPStreamServer := protocol.NewSRSHTTPStreamServer(environment, gracefulQuitTimeout) + srsHTTPStreamServer := server.NewSRSHTTPStreamServer(environment, gracefulQuitTimeout) if err := srsHTTPStreamServer.Run(ctx); err != nil { return errors.Wrapf(err, "http server") } diff --git a/internal/protocol/api.go b/internal/server/api.go similarity index 99% rename from internal/protocol/api.go rename to internal/server/api.go index d1f3ef26d..f2aa41330 100644 --- a/internal/protocol/api.go +++ b/internal/server/api.go @@ -1,7 +1,7 @@ // Copyright (c) 2026 Winlin // // SPDX-License-Identifier: MIT -package protocol +package server import ( "context" diff --git a/internal/protocol/http.go b/internal/server/http.go similarity index 99% rename from internal/protocol/http.go rename to internal/server/http.go index a145c551e..1b11f763a 100644 --- a/internal/protocol/http.go +++ b/internal/server/http.go @@ -1,7 +1,7 @@ // Copyright (c) 2026 Winlin // // SPDX-License-Identifier: MIT -package protocol +package server import ( "context" diff --git a/internal/protocol/rtc.go b/internal/server/rtc.go similarity index 99% rename from internal/protocol/rtc.go rename to internal/server/rtc.go index 51792f9ca..4e981d323 100644 --- a/internal/protocol/rtc.go +++ b/internal/server/rtc.go @@ -1,7 +1,7 @@ // Copyright (c) 2026 Winlin // // SPDX-License-Identifier: MIT -package protocol +package server import ( "context" diff --git a/internal/protocol/rtmp.go b/internal/server/rtmp.go similarity index 99% rename from internal/protocol/rtmp.go rename to internal/server/rtmp.go index d5c554b7f..80be13ba6 100644 --- a/internal/protocol/rtmp.go +++ b/internal/server/rtmp.go @@ -1,7 +1,7 @@ // Copyright (c) 2026 Winlin // // SPDX-License-Identifier: MIT -package protocol +package server import ( "context" diff --git a/internal/protocol/srt.go b/internal/server/srt.go similarity index 99% rename from internal/protocol/srt.go rename to internal/server/srt.go index cc9324f69..0da23f51f 100644 --- a/internal/protocol/srt.go +++ b/internal/server/srt.go @@ -1,7 +1,7 @@ // Copyright (c) 2026 Winlin // // SPDX-License-Identifier: MIT -package protocol +package server import ( "bytes"