xiaosongshu / rtmp_server
Native PHP RTMP live server with FLV/HLS, MP4 slicing, PHP client, relay & gateway.
Requires
- php: >=8.1
- ext-zlib: *
- apix/log: ^1.2
- evenement/evenement: ^3.0
- react/promise: ^2.9
- xiaosongshu/flv2mp4: ^1.3
This package is auto-updated.
Last update: 2026-07-13 03:34:41 UTC
README
π¨π³ δΈζζζ‘£ β’ π¬π§ English Docs
A pure PHP self-developed lightweight RTMP live streaming service, zero dependence on third-party streaming media tools such as FFmpeg and Nginx, enabling rapid setup of a private live streaming platform out of the box. On Linux, the
eventextension is automatically enabled for epoll event-driven I/O; on Windows, it automatically falls back to the select I/O model, ensuring full platform compatibility. Project positioning: underlying infrastructure β fully self-developed RTMP/HTTP-FLV/WS-FLV protocol stacks and asynchronous networking engine; upper-layer applications such as business management, permissions, and playback management need to be extended and developed by developers themselves.
Table of Contents (Revised)
- Environment Dependencies
- Quick Start
- Push/Pull Stream Address Specification
- Live & VoD Access URLs
- Page/Script Usage Instructions
- Project Directory Structure
- Overall System Architecture
- Port Constant Configuration
- Recording Task Switch Configuration
- MultiβProcess Worker Configuration (IPC Stream Sync Core)
- Push Stream Authentication Configuration
- FLV Live Distribution Gateway
- Static File HTTP Gateway
- Tutorials for Multiple Push/Pull Methods
- Live Stream Forwarding Tutorial
- Cluster Deployment Architecture for 100,000+ Concurrent Connections
- FAQ
- Open Source License
- Affiliated Toolkits
- Contact
Environment Dependencies
| Dependency | Mandatory Requirements |
|---|---|
| PHP | >= 8.1, CLI mode only, FPM not supported |
| sockets extension | Strictly required, foundation for TCP/WS/RTMP communication |
| event extension | Strongly recommended on Linux to enable epoll highβconcurrency event model; on Windows, if missing, it automatically falls back to select |
Quick environment setup: the project includes a
docker-compose.ymlfile. Rundocker-compose up -dto start a complete runtime environment with one command.
Quick Start
1. Project Installation
composer create-project xiaosongshu/rtmp_server
2. Start the Origin Main Service
php server.php
Successful startup output example:
[INFO] RTMP Server started on 0.0.0.0:1935
[INFO] HTTP-FLV/WS-FLV Server started on 0.0.0.0:8501
[INFO] HTTP Static Server started on 0.0.0.0:80
3. Quick Push Stream Test
Method 1: Browserβbased push without extra software
- Screen realβtime push:
http://127.0.0.1/push.html - Local MP4/FLV file loop push:
http://127.0.0.1/flv_push.html
Method 2: Standard FFmpeg push
ffmpeg -re -stream_loop -1 -i video.mp4 -c:v libx264 -c:a aac -f flv rtmp://127.0.0.1:1935/live/stream
Method 3: OBS Studio push
- Server:
rtmp://127.0.0.1:1935/live/ - Stream Key:
stream
Method 4: Builtβin PHP push client
php pusher.php test.mp4 http://127.0.0.1:8501/live/stream
4. Quick Live Viewing
Open in browser: http://127.0.0.1/index.html
Push/Pull Stream Address Specification
Push Addresses (OBS/FFmpeg/PHP/Web unified format)
| Protocol | Standard Format | Example Address |
|---|---|---|
| RTMP | rtmp://host:1935/{app}/{stream} |
rtmp://127.0.0.1:1935/live/stream |
| HTTP-FLV | http://host:8501/{app}/{stream} |
http://127.0.0.1:8501/live/stream |
| WebSocket-FLV | ws://host:8501/{app}/{stream} |
ws://127.0.0.1:8501/live/stream |
Field constraints:
{app}(application name) and{stream}(channel name) may only contain English letters, digits, and underscores; special characters and Chinese are prohibited.
Live & VoD Access URLs
Live Playback Addresses
| Protocol | Access URL | Use Case |
|---|---|---|
| RTMP | rtmp://127.0.0.1:1935/live/stream |
ffplay, desktop professional players |
| HTTP-FLV | http://127.0.0.1:8501/live/stream.flv |
Lowβlatency PC browser playback |
| WebSocket-FLV | ws://127.0.0.1:8501/live/stream.flv |
Browser native WebSocket MSE playback |
| HLS-TS | http://127.0.0.1:80/hls/live/stream/index.m3u8 |
Mobile devices, WeChat builtβin browser |
| HLS-FMP4 (muxed audio/video) | http://127.0.0.1:80/mp4/live/stream/output_merge/index.m3u8 |
Mainstream desktop browsers, mobile devices, WeChat builtβin browser, ffplay, VLC, etc. |
| HLS-FMP4 (demuxed audio/video) | http://127.0.0.1:80/mp4/live/stream/output_separate/index.m3u8 |
Mainstream desktop browsers, mobile devices, WeChat builtβin browser, ffplay, VLC, etc. |
Recorded VoD Playback Addresses
Recorded files are persistently stored in the project root. After the live stream ends, the complete file is automatically generated:
| File Type | Storage Path | Access Example |
|---|---|---|
| Standard transcoded MP4 | mp4/live/stream/index.mp4 |
http://127.0.0.1/mp4/live/stream/index.mp4 |
| Raw FLV recording | flv/live/stream/index.flv |
http://127.0.0.1/flv/live/stream/index.flv |
| HLS TS segments directory | hls/live/stream/index.m3u8 |
http://127.0.0.1:80/hls/live/stream/index.m3u8 |
| HLS-FMP4 muxed audio/video segments | mp4/live/stream/output_merge/index.m3u8 |
http://127.0.0.1:80/mp4/live/stream/output_merge/index.m3u8 |
| HLS-FMP4 demuxed audio/video segments | mp4/live/stream/output_separate/index.m3u8 |
http://127.0.0.1:80/mp4/live/stream/output_separate/index.m3u8 |
Note: Standard MP4 files are generated only when multiβprocess FLV recording is enabled and the FLV file is automatically transcoded to standard MP4 after recording. Alternatively, you can use the toolkit xiaosongshu/flv2mp4 to manually transcode FLV to MP4.
Page/Script Usage Instructions
Live/VoD Playback Pages
| Page File | Description | Access URL |
|---|---|---|
| index.html | HTTPβFLV lowβlatency live player | http://127.0.0.1/index.html |
| play.html | HLS mobileβoptimized player | http://127.0.0.1/play.html |
| mp4.html | MP4 VoD dedicated page | http://127.0.0.1/mp4.html |
| video.html | FLV VoD player | http://127.0.0.1/video.html |
| play_merge.html | fMP4 segmented live/VoD page (native JS) | http://127.0.0.1/play_merge.html |
| mse.html | fMP4 segmented live/VoD page (hls.js) | http://127.0.0.1/mse.html |
WebβBased Push Pages
| Page File | Description | Access URL |
|---|---|---|
| push.html | Browser screen capture push (WSβFLV) | http://127.0.0.1/push.html |
| flv_push.html | Local MP4/FLV file loop push | http://127.0.0.1/flv_push.html |
| push_merge.html | Multiβstream composition push | http://127.0.0.1/push_merge.html |
| push_transcode.html | Frontβend multiβbitrate transcoding push for weak networks | http://127.0.0.1/push_transcode.html |
PHP Builtβin Push/Pull Client Scripts
| Script | Function | Command Example |
|---|---|---|
| pusher.php | Commandβline file push client | php pusher.php video.mp4 http://127.0.0.1:8501/live/stream |
| puller.php | Commandβline pull and record client | php puller.php http://127.0.0.1:8501/live/stream.flv output.flv |
PHP Builtβin Relay Client Script
| Script | Function | Command Example |
|---|---|---|
| forward.php | Commandβline live stream relay client | php forward.php ws://127.0.0.1:8501/a/b.flv rtmp://127.0.0.1:1935/c/d |
PHP Builtβin Gateway Client Scripts
| Script | Function | Command Example |
|---|---|---|
| fileGateway.php | Commandβline file gateway client | php fileGateway.php 0.0.0.0 8100 |
| flvGateway.php | Commandβline FLV gateway client | php flvGateway.php 8080 http://127.0.0.1:8501 |
PHP Live Startup Script
| Script | Function | Command Example |
|---|---|---|
| server.php | Start live service from command line | php server.php |
Project Directory Structure
rtmp_server/
βββ config/ # Global configuration: ports, multiβprocess, recording, push authentication
βββ flv/ # Realβtime raw FLV recording storage
βββ mp4/ # fMP4 segments & complete MP4 merged after stream ends
βββ hls/ # HLS TS segments, m3u8 index files
βββ MediaServer/ # Core RTMP/FLV/WSβFLV protocol stacks, session management
βββ Root/ # Lowβlevel async I/O, socket eventβdriven engine
βββ record/ # Clientβside static page resources
βββ server.php # RTMP origin main service entry
βββ flvGateway.php # FLV live distribution gateway startup script
βββ fileGateway.php # HLS/MP4/static resource HTTP gateway
βββ forward.php # Live stream relay client
βββ pusher.php # PHP push client
βββ puller.php # PHP pull client
βββ auth_config.php # Push authentication standalone config
βββ *.html # All Web push/pull/playback pages
βββ docker-compose.yml # Docker oneβclick deployment config
βββ LICENSE # Apache 2.0 open source license
Overall System Architecture
γExternal PushersγOBS / FFmpeg / Web
β
RTMP(1935) / HTTPβFLV/WSβFLV(8501) push access
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β RTMP Origin Main Service (Stream Production Core) β
β β
β π₯ Push/Pull Access: RTMP / HTTPβFLV / WSβFLV tripleβprotocol compatible, with β
β builtβin push authentication β
β π Protocol Transmuxing: Raw stream output to HTTPβFLV / WSβFLV / HLS / fMP4 / MP4 β
β πΎ Parallel recording tasks (completely nonβblocking, individually toggleable) β
β ββββββββββββ¬βββββββββββ¬βββββββββββ β
β β FLV raw β fMP4 realββ HLS TS β β
β β recordingβ time β segmentsβ β
β β β segments β β β
β ββββββββββββ΄βββββββββββ΄βββββββββββ β
β π€ Realβtime stream output: distributes HTTPβFLV, WSβFLV, HLS live streams β
β π¦ VoD artifacts: fMP4 segment cache, autoβmerge to complete MP4 after stream endsβ
β π Builtβin static HTTP service (port 80): provides direct page and VoD file β
β access without additional gateway in lowβconcurrency scenarios β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββΌββββββββββββββββββββ
β β β
βΌ βΌ βΌ
HTTPβFLV realβtime HLS static fMP4 static
stream segment files segment files
β β β
βΌ βΌ βΌ
βββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββββ
β FLV Live β β Static File Gateway Cluster β
β Gateway β β (fileGateway) β
β Cluster β β Hosted resources: HLS/fMP4/MP4/FLV/ β
β β β web static assets β
β βββββββββββ β β β
β βPrimary β β β βββββββββ βββββββββ βββββββββ β
β βGateway β β β βGW 1 β βGW 2 β βGW 3 β β
β β(port8080)β β β β(8100) β β(8101) β β(8102) β β
β βββββ¬ββββββ β β ββββ¬βββββ ββββ¬βββββ ββββ¬βββββ β
β β β β β β β β
β βββββ΄ββββ β β βΌ βΌ βΌ β
β βΌ βΌ βΌ β β ββββββββββββββββββββββββββββββββββββ β
β βββ βββ βββ β β βEndβuser player clients β β
β βSβ βSβ βSβ β β βMSE/HLS players/ffplay/browsers β β
β βuβ βuβ βuβ β β ββββββββββββββββββββββββββββββββββββ β
β βbβ βbβ βbβ β β β
β ββ¬ββ ββ¬ββ ββ¬ββ β ββββββββββββββββββββββββββββββββββββββββββββ
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββ β
β βLive viewer β β
β βFLV players β β
β ββββββββββββββ β
βββββββββββββββββββ
Architecture Detailed Description
-
Origin Main Service (the sole stream producer) All external pushes are ingested by the origin, where protocol parsing, authentication, multiβformat transmuxing, and parallel recording are performed. The three recording tasks β FLV recording, fMP4 slicing, and HLS slicing β run in completely isolated threads without blocking each other. In lowβconcurrency scenarios, the builtβin portβ80 static service can be used directly without deploying extra gateways.
-
FLV Live Distribution Gateway No transcoding logic; it only forwards traffic and caches GOP key frames for instant player startβup. Supports horizontal scaling and multiβlevel cascading (production environment recommended max 2 levels, more levels increase latency). Linux uses epoll for high concurrency; Windows is for testing only. In highβconcurrency scenarios, all player pull requests should go through the gateway to relieve connection pressure on the origin main process.
-
Static File Gateway Cluster Dedicated to hosting HLS, MP4, FLV, frontβend pages and other static resources, achieving readβwrite separation. Must be deployed for largeβscale VoD scenarios to prevent the origin from being overwhelmed by file I/O requests.
-
Integrated Live Tooling This project provides pure PHP client push, pull, and live relay capabilities, as well as webβbased frontβend push, playback, transcoding, and stream mixing. It supports singleβprocess/multiβprocess switching and includes a personalized media toolkit
xiaosongshu/flv2mp4.
Deployment Recommendations by Concurrency Level
| Concurrency Level | Recommended Deployment |
|---|---|
| Low (online viewers < 1000) | Run only server.php (origin), using builtβin ports 80 and 8501; no gateway needed |
| Medium (1000 ~ 5000 online) | Origin + singleβlayer FLV gateway cluster + singleβlayer static file gateway cluster, with Nginx load balancing |
| High / Largeβevent live (>5000 online) | Origin + multiβlayer FLV and static gateway clusters, frontβend load balancing; for 10,000+ events, must incorporate commercial CDN edge distribution β do not rely on a single server for all traffic |
Port Constant Configuration
Modify config/app.php to adjust global service ports. Builtβin constant definitions:
/** HTTPβFLV / WebSocketβFLV main service port */ define('BASE_FLV_PORT', 8501); /** RTMP standard 1935 port */ define('BASE_RTMP_PORT', 1935); /** Builtβin static web page and VoD file HTTP port */ define('BASE_WEB_PORT', 80);
Recording Task Switch Configuration
In config/app.php, three recording tasks are independently controlled without interference:
define('FLV_TO_RECORD', true); // Enable realβtime raw FLV recording define('FLV_TO_MP4', true); // Enable fMP4 segmentation define('FLV_TO_HLS', true); // Enable HLS TS segment generation
Multi Process Worker Configuration Ipc Stream Sync Core
Principle
In the PHP CLI multiβprocess model, each worker process has isolated memory. When a single process receives a push stream, other workers cannot read that stream data. Therefore, IPC (InterβProcess Communication) is mandatory to synchronise live streams across processes. Instead of using traditional system IPC such as shared memory or pipes, this project implements a custom local TCP Socket IPC solution: it allocates a set of internal communication ports. The worker that receives the stream actively replicates the full stream data via a builtβin TCP client and forwards it to all other workers, achieving full stream data sharing among all processes.
Configuration in config/app.php
/** Master switch: enable multiβprocess worker mode */ define('ENABLE_MULTI_PROCESS', true); /** Number of worker processes, recommended not to exceed CPU physical cores */ define('WORKER_COUNT', 3); /** Starting port for interβprocess TCP communication, automatically assigns 8502, 8503... */ define('COPY_PORT_START', 8502);
When multiβprocess is disabled (
ENABLE_MULTI_PROCESS=false), the number of processes and internal communication ports are ignored, and the service runs in singleβprocess mode without IPC stream sync.
Load Balancing Rules for MultiβProcess Ports
- Linux: the system supports port reuse; multiple workers can simultaneously listen on the main FLV port 8501, and the kernel automatically distributes player connections evenly among workers.
- Windows: although
SO_REUSEADDRport reuse is supported, new TCP connections will only be assigned to the first process that bound port 8501, so native load balancing is not possible. You can use Nginx to reverseβproxy the internal communication ports (8502+) for traffic distribution. - Internal IPC ports are externally accessible for pull requests and can be used for manual load balancing on Windows.
Platform Performance Limitations
- Linux: epoll I/O model supports thousands of concurrent longβconnections per process; multiβprocess can fully utilise multiβcore CPUs β the preferred production environment.
- Windows: the underlying select model has a very low concurrency limit (~256 connections per process) β only for local development and debugging, never for production deployment.
Push Stream Authentication Configuration
Overview
Prevents unauthorised streams from overriding a live room. Only push requests carrying a valid stream key are allowed. Playback pull currently has no builtβin authentication; developers can implement referer/token validation at the gateway or reverseβproxy layer.
Configuration file config/auth.php
<?php return [ 'enabled' => false, // Master authentication switch 'publish' => [ 'require_auth' => true, // Enforce key validation for push 'stream_keys' => [ 'live_123456', 'stream_key_abc', ], ], 'global' => [ 'allowed_apps' => ['live'], // Allowed application names 'deny_apps' => [], ], ];
Authenticated Push Address Format
Pass the key via URL parameter key:
- RTMP
ffmpeg -re -i video.mp4 -f flv rtmp://127.0.0.1:1935/live/stream?key=live_123456
- OBS stream key:
stream?key=live_123456 - HTTPβFLV
ffmpeg -re -i video.mp4 -f flv http://127.0.0.1:8501/live/stream?key=live_123456
- WSβFLV PHP client
php pusher.php test.flv "ws://127.0.0.1:8501/live/stream?key=live_123456"
Security Best Practices
- Replace default keys with random strings of 32 characters or more.
- In public environments, enable HTTPS/WSS to avoid key interception in plaintext.
- Rotate stream keys periodically to reduce the risk of leakage.
- Authentication is disabled by default; enable it if needed.
Note: After modifying any of the above configurations, restart the service for changes to take effect.
FLV Live Distribution Gateway
Overview
A lightweight traffic forwarding service that pulls HTTPβFLV/WSβFLV streams from the upstream origin, caches GOP key frames for instant player startβup, supports horizontal scaling and multiβlevel cascading to share the originβs concurrent load. The gateway can pull from either HTTPβFLV or WSβFLV sources and provides both HTTPβFLV and WSβFLV playback addresses to clients.
Startup Commands
# Basic single instance php flvGateway.php 8080 http://127.0.0.1:8501 php flvGateway.php 8080 ws://127.0.0.1:8501 # Horizontal scaling with multiple instances php flvGateway.php 8080 http://127.0.0.1:8501 php flvGateway.php 8081 http://127.0.0.1:8501 php flvGateway.php 8082 ws://127.0.0.1:8501 # Multiβlevel cascading (not recommended beyond 2 levels) php flvGateway.php 8080 http://127.0.0.1:8501 # Levelβ1 gateway php flvGateway.php 8081 http://127.0.0.1:8080 # Levelβ2 gateway # Run in background on Linux php flvGateway.php 8080 http://127.0.0.1:8501 > /dev/null 2>&1 &
Gateway Playback Address Format
http://gateway_IP:port/{app}/{stream}.flv
ws://gateway_IP:port/{app}/{stream}.flv
Example: http://127.0.0.1:8080/live/stream.flv
Static File HTTP Gateway
Overview
An independent static resource HTTP service that hosts HLS, MP4, FLV, and frontβend pages, separating file I/O from live streaming traffic to improve stability under highβconcurrency VoD access.
Startup Commands
# Single instance php fileGateway.php 0.0.0.0 8100 # Multiple instances for horizontal scaling php fileGateway.php 0.0.0.0 8100 php fileGateway.php 0.0.0.0 8101 php fileGateway.php 0.0.0.0 8102 # Run in background on Linux php fileGateway.php 0.0.0.0 8100 > /dev/null 2>&1 &
Nginx Load Balancing Reverse Proxy Example
upstream filegateway_cluster { server 127.0.0.1:8100; server 127.0.0.1:8101; server 127.0.0.1:8102; } server { listen 80; server_name media.example.com; location ~* \.(m3u8|ts|mp4|m4s|flv|html|css|js)$ { proxy_pass http://filegateway_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
Resource Access Examples
http://127.0.0.1:8100/index.html
http://127.0.0.1:8100/hls/live/stream/index.m3u8
http://127.0.0.1:8100/mp4/live/stream/index.mp4
Tutorials for Multiple Push/Pull Methods
RTMP Push
OBS, FFmpeg, and the PHP client all support the standard RTMP protocol. Address format: rtmp://host:1935/{app}/{stream}
HTTPβFLV Push
Ideal for commandβline or automated programmatic pushing. Address: http://host:8501/{app}/{stream}
WebSocketβFLV Push
A browserβnative push solution with latency as low as 50ms. Use the builtβin push.html page.
PHP Pull Script
Used for serverβside pulling, backup, or crossβserver forwarding:
php puller.php http://127.0.0.1:8501/live/stream.flv output.flv php puller.php ws://127.0.0.1:8501/live/stream.flv output.flv
Live Stream Forwarding Tutorial
This project provides live stream forwarding capabilities β you can relay a live stream to multiple servers, supporting RTMP/WSβFLV/HTTPβFLV protocols for both pull and push. See forward.php for detailed usage. Example forwarding command:
php forward.php http://127.0.0.1:8501/a/b.flv "rtmp://127.0.0.1:1935/c/d,ws://127.0.0.1:8501/c/e,http://127.0.0.1:8501/c/f"
The above command pulls the stream from http://127.0.0.1:8501/a/b.flv and pushes it to rtmp://127.0.0.1:1935/c/d, ws://127.0.0.1:8501/c/e, and http://127.0.0.1:8501/c/f. You can also push to any other platform that supports RTMP, WSβFLV, or HTTPβFLV.
Engineering Recommendations
pusher.php / puller.php / forward.php can be integrated into custom scripts to automate pullβandβrelay, backup recording, etc., without thirdβparty tools, completing a full PHP live streaming workflow.
Cluster Deployment Architecture for 100,000+ Concurrent Connections
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β γLayer 1: MultiβBroadcaster Push Layerγ β
β β
β Broadcaster A (OBS/Web/FFmpeg) Broadcaster B (OBS/Web/FFmpeg) Broadcaster N (...) β
β β β β β
β βββββββΌββββββ βββββββΌββββββ βββββββΌββββββ β
β βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ β
β [Node1][Node2][Node3] [Node1][Node2][Node3] [Node1][Node2][Node3] β
β (push to multiple origin nodes simultaneously for pushβside disaster recovery) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β RTMP/HTTPβFLV/WSβFLV push access
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β γLayer 2: Origin Node Cluster (Stream Production Core)γ β
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Origin A β β Origin B β β Origin C β β Origin D β β
β β server.php β β server.php β β server.php β β server.php β β
β β (multiβproc)β β (multiβproc)β β (multiβproc)β β (multiβproc)β β
β β record/seg β β record/seg β β record/seg β β record/seg β β
β βββββββ¬ββββββββ βββββββ¬ββββββββ βββββββ¬ββββββββ βββββββ¬ββββββββ β
β β β β β β
β ββββββββββ¬βββββββββ΄ββββββββββββββββββ΄βββββββββ¬βββββββββ β
β β β β
β ββββββΌβββββ ββββββΌβββββ β
β β forward β β forward β β automatic stream sync β
β β sync β β sync β (pullβpush) β
β ββββββ¬βββββ ββββββ¬βββββ β
β ββββββββββββββββ¬βββββββββββββββββββββ β
β β β
β (all origin nodes back each other up; if any fails, others continue) β
ββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β forward pull (from origin, push to edge)
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β γLayer 3: Edge Node Cluster (Distribution & Cache)γ β
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Edge X β β Edge Y β β Edge Z β β Edge W β β
β β server.php β β server.php β β server.php β β server.php β β
β β (multiβproc)β β (multiβproc)β β (multiβproc)β β (multiβproc)β β
β β record/seg β β record/seg β β record/seg β β record/seg β β
β βββββββ¬ββββββββ βββββββ¬ββββββββ βββββββ¬ββββββββ βββββββ¬ββββββββ β
β β β β β β
β ββββββββββ¬βββββββββ΄ββββββββββββββββββ΄βββββββββ¬βββββββββ β
β β β β
β ββββββΌβββββ ββββββΌβββββ β
β β forward β β forward β β auto pull from origin,β
β β sync β β sync β cache β
β βββββββββββ βββββββββββ β
β β
β β
Dynamic role switching: any node can be upgraded to origin (accept push) or β
β downgraded to edge at any time β
β β
All nodes independently record, providing multiβcopy backup for high reliability β
ββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββ΄ββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββ
β γLayer 4: Gateway Layerγβ β γLayer 4: Gateway Layerγβ
β β β β
β flvGateway Cluster β β fileGateway Cluster β
β βββββββ βββββββ βββββββ β β βββββββ βββββββ βββββββ β
β βGW1 β βGW2 β βGW3 β β β βGW1 β βGW2 β βGW3 β β
β ββββ¬βββ ββββ¬βββ ββββ¬βββ β β ββββ¬βββ ββββ¬βββ ββββ¬βββ β
β β β β β β β β β β
β βββββββββΌββββββββ β β βββββββββΌββββββββ β
β β β β β β
β (HTTPβFLV/WSβFLV) β β (HLS/MP4/FLV VoD/static pages)β
βββββββββββββββΌββββββββββββββ βββββββββββββββΌββββββββββββββ
β β
βββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β γLayer 5: Viewers / End Usersγ β
β β
β PC browsers (MSE/FLV.js) Mobile (HLS) ffplay/pro players WebSocket players β
β β
β β
Viewers connect to the nearest edge gateway, automatically routed to optimal nodes β
β via load balancing (DNS or Nginx) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Core Architectural Explanations
1. PushβLayer High Availability (Disaster Recovery)
- Multiβdestination push: Broadcasters can push simultaneously to multiple origin nodes (e.g., A, B, C). If one node fails, others still hold the stream, and viewers experience no outage.
- Automatic failover on pusher side: OBS/FFmpeg can be configured with backup push addresses for primaryβbackup switching; Web clients can push to multiple destinations via JavaScript.
2. Origin Node Cluster (Stream Production Core)
- Activeβactive deployment: All origin nodes are active, can accept pushes, and synchronise stream data with each other via
forward.php, ensuring every origin node has a complete copy of the stream. - Automatic failover: If any origin node goes down, the others continue to serve, and forwarding sync links automatically reconnect with no service interruption.
- Parallel recording: Each origin node independently performs FLV/fMP4/HLS recording, creating multiple physical backups and preventing singleβpoint storage loss.
3. Edge Node Cluster (Stream Distribution & Cache)
- Proximity pull: Edge nodes pull live streams from origin nodes via
forward.php, caching GOP key frames to provide lowβlatency, instantβstart playback for viewers. - Elastic scaling: Add or remove edge nodes dynamically based on concurrency, supporting horizontal scaling (e.g., for traffic spikes).
- Flexible role switching: Origin and edge nodes use identical code; you can upgrade an edge node to origin (accept pushes) or downgrade an origin to edge (pullβonly) by configuration, enabling resource allocation on demand.
4. Gateway Distribution Layer
- flvGateway cluster: Designed specifically for HTTPβFLV/WSβFLV realβtime streams β no transcoding, pure forwarding, with GOP caching for instant startβup. Supports multiβlevel cascading and horizontal scaling to handle massive player connections.
- fileGateway cluster: Dedicated to hosting HLS segments, MP4 VoD files, static pages, and other resources, separating file I/O from dynamic stream services to prevent blocking.
5. Viewer Endpoints
- Multiβprotocol support: RTMP, HTTPβFLV, WSβFLV, HLS β compatible with PC, mobile, Web, and all platforms.
- Intelligent routing: Use DNS roundβrobin, Nginx reverse proxy, or GSLB to direct viewer requests to the nearest or leastβloaded edge node for optimal experience.
6. Data Flow
- Push: Broadcaster β (multiβdestination) β Origin cluster β
forwardsync to all origin nodes. - Pull (edge): Edge nodes β
forwardpull from any origin node β cache β serve local viewers. - Playback: Viewers β load balancer β flvGateway/fileGateway β edge (or origin) node β receive stream data.
- Recording: All nodes (origin/edge) record according to configuration, eventually merging to MP4 for VoD playback.
7. Disaster Recovery and Backup Mechanisms
- Nodeβlevel failover: If any single node (origin or edge) fails, forwarding clients automatically reconnect to other live nodes β stream data continues uninterrupted.
- Regionβlevel failover: If an entire data centre goes down, DNS can switch to a backup data centre (requires multiple clusters) for crossβregion high availability.
- Recording backup: Each node stores its recording files independently. For critical live events, multiple nodes can record simultaneously to ensure no data loss.
8. Scalability and Concurrency Capacity
- Horizontal scaling: All layers support horizontal scaling β add nodes to share load without restarting existing services.
- 100,000+ concurrency: Edge nodes and gateway layers can scale out massively; combined with CDN edge acceleration, they can support 100,000+ concurrent viewers (bandwidth and server resources permitting).
- Performance optimisation: On Linux, the event extension (epoll) drives each node to handle thousands of long connections (depending on server specs); multiple nodes linearly increase overall concurrency capacity.
9. Deployment Recommendations
- Stream synchronisation between nodes is accomplished by the builtβin
forward.phprelay client. This tool can pull RTMP/HTTPβFLV/WSβFLV streams from any source and simultaneously push to one or more target nodes, while also supporting authentication parameters (e.g., key) in the push. Developers can write scheduling scripts based on actual network topology and business needs (e.g., health checks, load balancing strategies, or business rules) to dynamically configure pull sources, target node lists, and forwarding parameters, thus automating stream synchronisation among nodes. Role switching between origin and edge nodes similarly relies on external scheduling logic: it is recommended to monitor node system status (CPU load, memory usage, active connections, number of pushed streams, etc.) or external traffic distribution policies, and trigger scripts to dynamically adjust node roles, enabling elastic scaling, failover, and disaster recovery. The entire scheduling system can be customised to fit realβworld scenarios, providing a highly flexible productionβgrade deployment solution.
FAQ
Q1: Missing event extension on Windows?
Windows does not have the event extension; the service automatically falls back to the select I/O model. Only the sockets extension is required and it will run normally β no extra steps needed.
Q2: How can I confirm the service started successfully?
Three listening logs in the terminal indicate success: RTMP on 1935, FLV on 8501, and static HTTP on port 80.
Q3: Push succeeds but playback stutters?
- The push bitrate or resolution is too high β try lowering bitrate/frame rate.
- Server CPU is saturated β enable multiβprocess to utilise multiple cores.
- High concurrency without FLV gateway β too many player connections consume origin resources.
- Insufficient server upload bandwidth β limit concurrent viewers.
Q4: How do I stop the service?
Press Ctrl + C in the terminal to send a termination signal, or simply close the terminal window.
Q5: Which thirdβparty push software is supported?
Fully compatible with standard RTMP clients: OBS Studio, FFmpeg, xSplit, mobile RTMP push SDKs.
Open Source License
This project is licensed under the Apache License 2.0.
The software is provided βas isβ, without warranty of any kind, express or implied. The developers are not liable for any direct, indirect, or consequential damages arising from the use of this software. The full terms are available in the LICENSE file in the project root.
Affiliated Toolkits
The underlying codec and transmuxing capabilities have been extracted into a separate toolkit: xiaosongshu/flv2mp4 It provides conversions among FLV/MP4/fMP4/HLS, standalone push/pull clients, and gateway components, and can be imported into other thirdβparty PHP projects.
Contact
- Email: 2723659854@qq.com
- GitHub: https://github.com/2723659854