xiaosongshu / rtmp_server
A RTMP live streaming server written in PHP
Requires
- php: >=8.1
- ext-zlib: *
- apix/log: ^1.2
- evenement/evenement: ^3.0
- react/promise: ^2.9
- xiaosongshu/flv2mp4: ^1.2
This package is auto-updated.
Last update: 2026-06-17 15:20:56 UTC
README
π¨π³ δΈζ β’ π¬π§ English
A lightweight RTMP live streaming service written purely in PHP, no third-party streaming media dependencies, enabling rapid deployment of private live streaming platforms. Linux environment automatically enables epoll event-driven architecture, a single process easily handles 20,000+ concurrent connections; Windows falls back to select mode for compatibility.
ποΈ System Architecture
γPusherγOBS/FFmpeg
β
RTMP Push(1935) / HTTP-FLV/WS-FLV Push(8501)
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β RTMP Origin Server (Core) β
β β
β π₯ Ingest RTMP / HTTP-FLV / WebSocket-FLV three-protocol ingestion, connection authentication β
β π Transmuxing RTMP / HTTP-FLV / WS-FLV β HTTP-FLV / WebSocket-FLV / HLS / fMP4 / MP4 β
β πΎ Recording ββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ β
β β FLV Record β fMP4 Segmentβ HLS Segment β Three independent parallel tasks β
β β (raw stream)β (live chunk)β (live chunk)β β
β ββββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ β
β π€ Output HTTP-FLV(8501) / WebSocket-FLV / HLS live stream / fMP4 live stream β
β π¦ VOD Output fMP4 segments generated live β automatically merged into complete MP4 after stream ends β
β π Static Serve Origin built-in HTTP server (port 80), can directly serve static files (for low concurrency scenarios) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
HTTP-FLV(8501) HLS(TS/m3u8) fMP4(segments)
live stream static files static files
β β β
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββββββββ
β FLV Gateway β β Static File Gateway Cluster (fileGateway) β
β Cluster β β π― Hosts: HLS / fMP4 / MP4 / FLV / Web pages β
β β β β
β βββββββββββββ β β βββββββββββββ βββββββββββββ βββββββββββββ β
β β Level-1 β β β β Gateway 1 β β Gateway 2 β β Gateway 3 β β
β β Gateway β β β β (8100) β β (8101) β β (8102) β β
β β (8080) β β β βββββββ¬ββββββ βββββββ¬ββββββ βββββββ¬ββββββ β
β βββββββ¬ββββββ β β β β β β
β β β β βΌ βΌ βΌ β
β βββββββ΄ββββββ β β ββββββββββββββββββββββββββββββββββββββββ β
β βΌ βΌ βΌ β β β Client β β
β βββββ βββββ ββββββ β β HLS player / MSE player / VOD request / ffplay β
β βSubβ βSubβ βSubββ β ββββββββββββββββββββββββββββββββββββββββ β
β βGW1β βGW2β βGW3ββ β β
β βββ¬ββ βββ¬ββ βββ¬βββ βββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββββ β
β β Client β β
β β FLV Player / ffplay β
β ββββββββββββββββ β
βββββββββββββββββββ
Architecture Explanation
-
Origin Server: The only stream production node. Supports RTMP, HTTP-FLV, WebSocket-FLV three-protocol ingestion, responsible for ingest, pull, and multi-protocol remuxing. FLV recording, fMP4 segmentation, and HLS segmentation are three completely independent parallel tasks, non-blocking.
-
Origin Static Capability: The origin includes a built-in HTTP server (default port 80) that can directly serve static files. For low concurrency scenarios, no extra gateway is needed; works out-of-the-box.
-
Real-time Recording Mechanism:
- FLV Recording: Saves the raw stream in real time, producing a complete FLV file after the stream ends.
- fMP4 Segmentation: Generates audio/video fMP4 segments in real time (supports both mixed and separate formats), automatically merges into a complete MP4 after the stream ends.
- HLS Segmentation: Generates TS segments + m3u8 index in real time (mobile compatible).
- Independent Switches: Users can enable/disable each recording task separately in
server.php.
-
FLV Live Gateway Cluster: Pure traffic forwarding service. Pulls HTTP-FLV streams upstream, caches GOP keyframes for instant playback, and distributes to end clients or downstream gateways.
- Supports unlimited cascading levels: Level-1 gateway β Level-2 gateway β Level-3 gateway β ... β clients.
- Supports horizontal scaling: Deploy multiple gateway instances at the same level, distribute traffic via load balancing.
- Linux epoll high performance: Single process can handle 20,000+ concurrent connections; Windows fallback to select model.
-
Static File Gateway Cluster (Recommended): Lightweight HTTP static file server, unified hosting for all static assets.
- Supported protocols: HLS (.m3u8/.ts), fMP4 (.m4s/.mp4), MP4 VOD files, FLV recorded files, web player pages.
- Supports horizontal scaling: Deploy multiple instances at the same level, linearly increase concurrency.
- Supports vertical scaling: Can be layered with reverse proxies like Nginx for multi-level traffic distribution.
- Linux epoll high performance: Single process can handle 20,000+ concurrent connections; Windows fallback to select.
- Best practice: Point HLS/fMP4/MP4 playback paths to this gateway cluster to achieve read-write separation for static assets.
-
Deployment Recommendations:
- Low concurrency (< 500 concurrent): Use origin built-in HTTP server directly, no extra gateways needed.
- Medium concurrency (500 β 5,000 concurrent):
- Origin + single-layer gateway cluster (FLV gateway or static file gateway)
- A single gateway process is usually sufficient; no need for multiple instances.
- High concurrency (> 5,000 concurrent):
- Origin focuses on
ingest, remuxing, real-time recording. - Multi-level FLV gateway cluster: Level-1 β Level-2 β clients.
- Multi-level static file gateway cluster: Level-1 β Level-2 β clients.
- Each layer can be horizontally scaled to linearly increase concurrency.
- Origin focuses on
β¨ Features
- π₯ Three-Protocol Ingest: Full implementation of standard RTMP push/pull + HTTP-FLV push/pull + WebSocket-FLV push, compatible with mainstream push tools.
- π‘ HTTP-FLV / WebSocket-FLV: Low-latency browser live streaming solution, supports ffplay and other players for direct pull.
- π Built-in Web Push Test Page: Test WebSocket push directly in the browser without installing any push tool.
- π§© Automatic HLS Segmentation: Real-time generation of m3u8 + TS, fully compatible with mobile platforms.
- π¦ fMP4 Real-time Segmentation + Auto Merge: Generates fMP4 segments live, automatically merges into a complete MP4 after stream ends.
- π¬ Dual fMP4 Format Support: Supports both mixed (audio+video) and separate audio/video segment formats.
- πΎ Independent FLV Recording: Saves raw FLV stream in real time, decoupled from fMP4/MP4.
- ποΈ Independent Task Switches: FLV recording, fMP4 segmentation, and HLS segmentation can be independently enabled/disabled.
- π₯οΈ Built-in Multiple Web Players: Ready to use, supports FLV/HLS/MP4/mixed fMP4/separate fMP4 playback.
- π Cascadable FLV Streaming Gateway: Unlimited layered distribution, GOP cache for instant playback, automatic reconnection on stream break, supporting high-concurrency live scenarios.
- π Static File Gateway: Unified hosting for HLS/fMP4/MP4 recorded assets and web pages, supporting high-concurrency VOD scenarios.
- ποΈ Cross-Platform Playback Compatibility: Supports ffplay, VLC, browsers, mobile players, etc.
- π³ Docker One-Click Deployment: Quickly spin up a test environment.
- β‘ Native Pure PHP Implementation: No dependency on any third-party streaming software.
π Requirements
- PHP >= 8.1 (CLI mode only)
- Required extension:
sockets - Recommended extension:
event(greatly improves concurrency performance on Linux, automatically enables epoll)
π Quick Start
1. Install the project
composer create-project xiaosongshu/rtmp_server
2. Configure recording switches (server.php)
// Three independent recording task switches, can be enabled/disabled as needed define('FLV_TO_RECORD', true); // Whether to record raw FLV file in real time define('FLV_TO_MP4', true); // Whether to generate fMP4 segments in real time and merge to MP4 define('FLV_TO_HLS', true); // Whether to generate HLS (TS) segments in real time
3. Start the origin server
php server.php
4. Access playback (directly from origin in low concurrency)
# Playback page access (origin built-in HTTP service) http://127.0.0.1/index.html # FLV live page http://127.0.0.1/play.html # HLS live page http://127.0.0.1/mp4.html # MP4 VOD page http://127.0.0.1/play_merge.html # fMP4 segment VOD page (supports both mixed/separate formats)
5. Medium/High concurrency: Deploy static file gateway cluster (recommended)
Use case: High-concurrency access to HLS (.ts/.m3u8), fMP4 (.m4s/.mp4), MP4 VOD files, and web pages. On Linux, a single gateway process can handle 20,000+ connections; for higher loads, scale horizontally with multiple instances.
# Start a single instance (under epoll, directly handles high concurrency) php fileGateway.php 0.0.0.0 8100 # γHorizontal scalingγMultiple instances for ultra-high concurrency or multi-server php fileGateway.php 0.0.0.0 8100 php fileGateway.php 0.0.0.0 8101 php fileGateway.php 0.0.0.0 8102 # Linux/macOS run in background php fileGateway.php 0.0.0.0 8100 > /dev/null 2>&1 & # Vertical scaling: multi-level distribution via Nginx reverse proxy # Level-1 Nginx -> Level-2 fileGateway (8100/8101/8102) -> Level-3 fileGateway ...
Access examples (via static file gateway):
http://127.0.0.1:8100/play.html # HLS player page through gateway
http://127.0.0.1:8100/hls/live/stream/index.m3u8 # HLS stream through gateway
6. Medium/High concurrency: Deploy FLV live gateway cluster
On Linux, a single FLV gateway process can stably handle nearly 20,000 concurrent playback sessions.
# Level-1 gateway: pull stream from origin php flvGateway.php 8080 http://origin_IP:8501 # Horizontal scaling: multiple instances at same level php flvGateway.php 8081 http://origin_IP:8501 php flvGateway.php 8082 http://origin_IP:8501 # Vertical scaling: multi-level cascading php flvGateway.php 8080 http://origin_IP:8501 # Level-1 gateway php flvGateway.php 8081 http://127.0.0.1:8080 # Level-2 gateway (pulls from Level-1) php flvGateway.php 8082 http://127.0.0.1:8081 # Level-3 gateway (pulls from Level-2)
7. Stop services
| OS | Stop command |
|---|---|
| Windows | Ctrl + C |
| Linux/macOS | kill -9 PID |
π§ Port Configuration (modify in server.php)
| Port | Protocol | Purpose |
|---|---|---|
| 1935 | RTMP | RTMP push, RTMP pull playback |
| 8501 | HTTP/WebSocket | HTTP-FLV push/pull / WS-FLV push/pull / WS-FLV live playback |
| 80 | HTTP | Static file service + web player pages + web push test page |
π Web Push Test Page
The project includes a built-in WebSocket push test page, allowing you to test push functionality directly in your browser without installing any push software.
Access URL
http://127.0.0.1/push.html
Instructions
- Start origin server: Ensure
php server.phpis running. - Open push page: Visit
http://127.0.0.1/push.htmlin your browser. - Configure push parameters:
- Push URL:
ws://127.0.0.1:8501/live/stream(pre-filled by default) - App name:
live - Stream name:
stream
- Push URL:
- Choose push method:
- Camera push: Click
Get Camera, authorize, and start pushing. - Screen share push: Click
Share Screen, select a window or tab to start. - Local file push: Select a local video file (MP4/FLV supported), click push.
- Camera push: Click
- Start pushing: Click
Start Push, the page will send audio/video data to the server in real time. - Verify live stream:
- Open player page
http://127.0.0.1/index.htmlto watch FLV live. - Or use ffplay:
ffplay ws://127.0.0.1:8501/live/stream.flv
- Open player page
Push URL Format
# WebSocket-FLV push address
ws://127.0.0.1:8501/{app_name}/{stream_name}
# Example
ws://127.0.0.1:8501/live/stream
ws://127.0.0.1:8501/live/test
Note: Web push is based on WebSocket protocol and only supports H.264 video encoding and AAC/MP3 audio encoding. Before pushing, ensure your browser supports the required codecs.
π FLV Streaming Gateway (High-Concurrency Live Distribution)
Gateway Introduction
A lightweight traffic distribution component supporting unlimited cascading levels. Pulls HTTP-FLV from upstream origin/gateway, caches stream headers and GOP keyframes for instant playback on new client connections, and replicates stream data to downstream clients or sub-gateways. Designed for medium to high-concurrency live scenarios, supports both horizontal and vertical scaling.
Gateway Core Capabilities
- π‘ Single instance forwards multiple concurrent streams, handling different channels simultaneously.
- π Unlimited cascading: Level-1 β Level-2 β Level-3 chain expansion.
- β‘ GOP pre-caching: new connections don't wait for keyframes, enabling instant playback.
- π Automatic reconnection on upstream stream disconnection, transparent to end users.
- π Built-in runtime statistics: outputs online count, upload/download traffic every 10 seconds.
- π Horizontal scaling: add more gateway processes/instances at same level, linearly increase concurrency.
- π Vertical scaling: multi-level cascading, distribute pressure across layers.
- π§ Adaptive I/O: Linux automatically uses epoll, single process 20,000+ concurrent; Windows falls back to select for compatibility.
FLV Gateway Start Commands
# γHorizontal scalingγMultiple instances at same level php flvGateway.php 8080 http://origin_IP:8501 php flvGateway.php 8081 http://origin_IP:8501 php flvGateway.php 8082 http://origin_IP:8501 # γVertical scalingγMulti-level cascading php flvGateway.php 8080 http://origin_IP:8501 # Level-1 gateway php flvGateway.php 8081 http://127.0.0.1:8080 # Level-2 gateway php flvGateway.php 8082 http://127.0.0.1:8081 # Level-3 gateway # γCombined scalingγMulti-level + multiple instances per level # Level-1 gateway cluster php flvGateway.php 8080 http://origin_IP:8501 php flvGateway.php 8081 http://origin_IP:8501 # Level-2 gateway cluster (pulling from Level-1) php flvGateway.php 8180 http://127.0.0.1:8080 php flvGateway.php 8181 http://127.0.0.1:8081
Gateway Playback URL Specification
http://gateway_IP:port/{app_name}/{stream_name}.flv
Examples:
# Level-1 gateway
http://127.0.0.1:8080/live/stream.flv
# Level-2 gateway
http://127.0.0.1:8081/live/stream.flv
Debug Logging
Add $gateway->debug = true; in the gateway startup script to enable full detailed runtime logs.
π Static File Gateway fileGateway.php (High-Concurrency VOD Asset Hosting)
Gateway Introduction
A lightweight HTTP static file server, unified hosting for all static assets. For file-based protocols like HLS, fMP4, MP4, this is the recommended playback method. Supports horizontal and vertical scaling, can handle massive VOD concurrency.
Core Capabilities
- π Unified hosting for all static assets (recorded files + player pages).
- π Horizontal scaling: multiple instances, load balancing.
- π Vertical scaling: multi-level cascading (e.g., Nginx + fileGateway + backend storage).
- π Built-in access logs for statistics.
- π Pure PHP implementation, lightweight with no dependencies.
- π§ Adaptive I/O: Linux epoll single process 20,000+ concurrent, Windows select compatible.
- π‘ Best practice: Point HLS/fMP4/MP4 playback paths to this gateway cluster; origin only writes files, achieving read-write separation.
Start Commands (multiple processes / instances for distribution)
# Basic start (serves current directory, port 8100) php fileGateway.php 0.0.0.0 8100 # γHorizontal scalingγMultiple instances php fileGateway.php 0.0.0.0 8100 php fileGateway.php 0.0.0.0 8101 php fileGateway.php 0.0.0.0 8102 # Linux/macOS run multiple instances in background php fileGateway.php 0.0.0.0 8100 > /dev/null 2>&1 & php fileGateway.php 0.0.0.0 8101 > /dev/null 2>&1 & php fileGateway.php 0.0.0.0 8102 > /dev/null 2>&1 &
Nginx Reverse Proxy Configuration Example
upstream filegateway_cluster { # Horizontal scaling: multiple fileGateway instances 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; } }
Access URL Specification
http://gateway_IP:port/{relative_file_path}
Examples:
# Web player pages (via static gateway)
http://127.0.0.1:8100/index.html # FLV live page
http://127.0.0.1:8100/play.html # HLS live page
http://127.0.0.1:8100/mp4.html # MP4 VOD page
http://127.0.0.1:8100/video.html # FLV VOD page
http://127.0.0.1:8100/play_merge.html # fMP4 segment VOD page
# Recorded asset access
http://127.0.0.1:8100/hls/live/stream/index.m3u8
http://127.0.0.1:8100/mp4/live/stream/output_merge/init.mp4
http://127.0.0.1:8100/mp4/live/stream/output_merge/stream_full.mp4
http://127.0.0.1:8100/flv/live/stream/index.flv
π‘ Ingest Tutorial
The project supports RTMP, HTTP-FLV, WebSocket-FLV three mainstream ingestion protocols, compatible with OBS, FFmpeg, and browser-based Web push.
1. RTMP Push
URL Format
rtmp://127.0.0.1:1935/{app_name}/{stream_name}
app_name: e.g.,livestream_name: e.g.,stream- Only alphanumeric characters allowed.
Push Examples
-
OBS Studio Push
- Download and install OBS Studio
- Settings β Stream β Server:
rtmp://127.0.0.1:1935/live - Stream Key:
stream - Start streaming
-
FFmpeg Loop Push
ffmpeg -re -stream_loop -1 -i "video.mp4" -vcodec h264 -acodec aac -f flv rtmp://127.0.0.1:1935/live/stream
2. HTTP-FLV Push
URL Format
http://127.0.0.1:8501/{app_name}/{stream_name}
- Same naming rules as RTMP, alphanumeric only.
Push Example (FFmpeg)
# Push local FLV file ffmpeg -re -i test.flv -c:v libx264 -c:a aac -f flv http://127.0.0.1:8501/live/stream # Loop push local MP4 file ffmpeg -re -stream_loop -1 -i video.mp4 -c:v libx264 -c:a aac -f flv http://127.0.0.1:8501/live/stream
3. WebSocket-FLV Push (New)
URL Format
ws://127.0.0.1:8501/{app_name}/{stream_name}
- Same naming rules, alphanumeric only.
Push Methods
1. Built-in Web Push Test Page (Recommended)
http://127.0.0.1/push.html
Configure push URL ws://127.0.0.1:8501/live/stream on the page, choose camera, screen share, or local file to start pushing.
2. FFmpeg Push
# Push local file via WebSocket (requires conversion tool) ffmpeg -re -i video.mp4 -c:v libx264 -c:a aac -f flv - | websocat -b ws://127.0.0.1:8501/live/stream
3. Custom WebSocket Client
// JavaScript WebSocket push example const ws = new WebSocket('ws://127.0.0.1:8501/live/stream'); const mediaStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }); const recorder = new MediaRecorder(mediaStream, { mimeType: 'video/webm' }); recorder.ondataavailable = (event) => { if (event.data.size > 0 && ws.readyState === WebSocket.OPEN) { ws.send(event.data); } }; recorder.start(100); // send data every 100ms
4. Client Push Tool
The project includes a client push tool supporting FLV/MP4 static file push:
# HTTP-FLV push php pusher.php test.flv http://127.0.0.1:8501/live/stream 1.0 --no-reconnect php pusher.php test.mp4 http://127.0.0.1:8501/live/stream 1.0 --no-reconnect # WebSocket-FLV push php pusher.php test.flv ws://127.0.0.1:8501/live/stream 1.0 --no-reconnect php pusher.php test.mp4 ws://127.0.0.1:8501/live/stream 1.0 --no-reconnect
πΊ Playback URL Summary & Tools
Live Stream URLs
| Protocol | Access URL | Description | Distribution Suggestion |
|---|---|---|---|
| RTMP | rtmp://127.0.0.1:1935/live/stream |
Native RTMP player, ffplay available | Origin direct serve |
| HTTP-FLV | http://127.0.0.1:8501/live/stream.flv |
Low-latency browser playback, ffplay available | Via FLV gateway cluster |
| WebSocket-FLV | ws://127.0.0.1:8501/live/stream.flv |
WebSocket streaming (native browser support) | Via FLV gateway cluster |
| HLS | http://{fileGateway_IP}:8100/hls/live/stream/index.m3u8 |
Android/iOS mobile preferred | Must use fileGateway |
VOD Playback URLs (after recording completes)
| File Type | Access URL (must use fileGateway) | Description |
|---|---|---|
| Merged MP4 VOD | http://{fileGateway_IP}:8100/mp4/live/stream/output_merge/stream_full.mp4 |
|
| Mixed fMP4 segments (MSE) | http://{fileGateway_IP}:8100/mp4/live/stream/output_merge/init.mp4 |
|
| Separate audio/video fMP4 VOD | http://{fileGateway_IP}:8100/mp4/live/stream/output_separate/audio_init.mp4 (and video counterpart) |
|
| Raw FLV VOD | http://{fileGateway_IP}:8100/flv/live/stream/index.flv |
Under high concurrency: must use the static file gateway cluster (e.g.,
127.0.0.1:8100/8101/8102) with load balancing to achieve read-write separation for static assets.
Web Player Pages
| Page Purpose | Access URL (recommended via fileGateway) | Description |
|---|---|---|
| FLV live playback | http://{fileGateway_IP}:8100/index.html |
HTTP-FLV low-latency live |
| HLS live playback | http://{fileGateway_IP}:8100/play.html |
HLS mobile-compatible live |
| Merged MP4 VOD | http://{fileGateway_IP}:8100/mp4.html |
Complete MP4 file VOD |
| Raw FLV VOD | http://{fileGateway_IP}:8100/video.html |
FLV native file VOD |
| fMP4 segment VOD | http://{fileGateway_IP}:8100/play_merge.html |
Supports both mixed and separate fMP4 playback |
Command-line Playback Tools (ffplay)
The project is fully compatible with ffplay; you can directly pull any stream URL for playback:
# Play RTMP stream ffplay rtmp://127.0.0.1:1935/live/stream # Play origin HTTP-FLV stream ffplay http://127.0.0.1:8501/live/stream.flv # Play origin WebSocket-FLV stream ffplay ws://127.0.0.1:8501/live/stream.flv # Play FLV gateway forwarded stream ffplay http://127.0.0.1:8080/live/stream.flv # Play HLS stream ffplay http://127.0.0.1:8100/hls/live/stream/index.m3u8 # Play VOD FLV/MP4 file ffplay http://127.0.0.1:8100/flv/live/stream/index.flv ffplay http://127.0.0.1:8100/mp4/live/stream/output_merge/stream_full.mp4
πΎ Real-time Recording Explanation
Recording Mechanism (Three Independent Parallel Tasks)
Once ingestion starts, the origin simultaneously launches three independent parallel recording tasks, non-blocking:
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β RTMP / HTTP-FLV / WS-FLV three-protocol ingestβ
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββΌββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β FLV Record β β fMP4 Segmentβ β HLS Segment β
β (raw stream) β β (live chunk) β β (live chunk) β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ βββββββββ¬ββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β Complete FLV β β fMP4 segmentsβ β TS segments β
β (after streamβ β (during live)β β + m3u8 index β
β ends) β β β β β
βββββββββββββββββ βββββββββ¬ββββββββ βββββββββββββββββ
β
β automatically merged after stream ends
βΌ
βββββββββββββββββ
β Complete MP4 β
β (VOD replay) β
βββββββββββββββββ
Task Independence Explanation
| Recording Task | Real-time | Output | Use Case | Independent Switch |
|---|---|---|---|---|
| FLV Record | Yes | Complete FLV file | Raw format backup, VLC/ffplay playback | FLV_TO_RECORD |
| fMP4 Segment | Yes | fMP4 segments β merged to MP4 | Browser MSE playback, VOD replay | FLV_TO_MP4 |
| HLS Segment | Yes | TS segments + m3u8 | Mobile compatibility, HLS live | FLV_TO_HLS |
π Project Directory Structure
rtmp_server/
βββ flv/ # Raw FLV recorded files (FLV_TO_RECORD)
βββ mp4/ # MP4/fMP4 transcoding output (FLV_TO_MP4)
βββ hls/ # HLS TS segments + m3u8 index (FLV_TO_HLS)
βββ MediaServer/ # RTMP core protocol, push/pull session logic
βββ Root/ # Low-level async I/O, socket event driving (incl. epoll adaptive)
βββ SabreAMF/ # AMF0/AMF3 encoding/decoding
βββ server.php # Origin server entry point
βββ fileGateway.php # Static file gateway (supports epoll, 20k+ concurrency)
βββ flvGateway.php # FLV live gateway (supports epoll, 20k+ concurrency)
βββ pusher.php # FLV/MP4 static file push client
βββ push.html # WebSocket-FLV push test page (browser push)
βββ *.html # Web player pages
βββ README.md
π Concurrency Performance Test
The following tests were performed under the same environment: Docker container, ulimit -n 65535, using the same stress test script with 20,000 concurrent clients, each pulling stream for 5 seconds.
Origin Server (RTMP origin)
Current container pids.max: unknown
Start batch: 1000 clients (20 batches total)
All clients started, waiting for completion...
===== Results =====
Success: 17,330
Failure: 2,670
FLV Live Gateway
Current container pids.max: unknown
Start batch: 1000 clients (20 batches total)
All clients started, waiting for completion...
===== Results =====
Success: 19,923
Failure: 77
Static File Gateway
Concurrency: 20,000
Duration per client: 5s
Batch size: 1000
===== Results =====
Success: 20,000
Failure: 0
Explanation:
- The origin server, due to carrying RTMP/HTTP-FLV/WS-FLV ingestion, multi-protocol remuxing, and other business logic, still stably supports 17,330 successful concurrent connections; the few failures are due to port collisions during test.
- FLV gateway focuses purely on traffic forwarding, achieving 99.6% success rate (19,923/20,000), approaching the single-machine TCP port pool limit.
- Static file gateway is extremely lightweight: 20,000 concurrent all successful, zero failures.
- All components adapt to the OS: Linux automatically enables epoll, breaking the traditional select 1024 limit.
β FAQ
1. How can a single process support 20,000+ concurrent connections?
- Linux: If the
eventextension is detected, the server automatically enables epoll event-driven model, no longer limited by the traditionalselect1024 file descriptor limit; a single process easily handles 20,000+ connections. - Windows: Since the
eventextension is missing, it falls back to theselectmodel, limited to ~256 connections per process; it's recommended to deploy multiple instances. - Performance tests: In a Docker container (ulimit -n 65535), the static file gateway achieved 20,000 concurrent zero failures, and the FLV gateway achieved 99.6% success.
2. How to support even higher concurrency with gateways?
| Scaling Method | Explanation | Example |
|---|---|---|
| Single-process | Linux epoll mode: a single process can handle 20k+ | One fileGateway process handles 20,000 static requests |
| Horizontal | Multiple instances at the same level, load balancing | 3 fileGateway instances β 60,000+ concurrency |
| Vertical | Multi-level cascading | Level-1 β Level-2 β Level-3 gateways... |
| Combined | Horizontal + vertical together | 3 instances per level Γ 3 levels = theoretical 180,000+ concurrency |
3. When is a gateway needed?
| Concurrency Scenario | Deployment Plan |
|---|---|
| Low (< 500) | Origin only; origin built-in HTTP server serves directly |
| Medium (500 β 5,000) | Origin + single-layer gateway (1β2 instances sufficient) |
| High (> 5,000) | Origin + multi-level gateway cluster (each layer can be horizontally scaled) |
4. What is the difference between FLV gateway and static file gateway?
| Gateway Type | Use Case | Resource Types Handled | Scaling Methods |
|---|---|---|---|
| FLV live gateway | Live stream distribution | HTTP-FLV real-time streams | Horizontal + vertical, cascading |
| Static file gateway | Unified static asset hosting | HLS/fMP4/MP4/FLV static files + web player pages | Horizontal + vertical, can combine with Nginx |
5. What are the advantages of WebSocket-FLV push?
- Native browser support: No plugins or push software needed; push from a web page.
- Lower barrier: Non-technical users can quickly test live streaming via built-in test page.
- Mobile friendly: Supports camera push from mobile browsers (HTTPS environment required).
- Coexists with HTTP-FLV: Both protocols can be used simultaneously without interference.
6. How to verify gateway concurrency capability?
# Use built-in stress test script (20000 concurrency) sh play.sh # Or use ab (Apache Bench) to test static file gateway ab -n 10000 -c 500 http://127.0.0.1:8100/index.html # Use wrk to test FLV gateway wrk -t4 -c1000 -d30s http://127.0.0.1:8080/live/stream.flv
π¦ Toolkit
The protocol conversion part of this project has been extracted into an independent toolkit xiaosongshu/flv2mp4. Repository: https://github.com/2723659854/flv2mp4. It supports flv, mp4, hls transcoding, flv and file gateways, and an flv static file push client.
π License
This project is for learning and technical research only; commercial use risks are borne by the user.
β οΈ Disclaimer
- Some open source code is taken from the open source community; if copyright is involved, the author can be contacted for removal.
- The project is completely open source and free, only for technical exchange.
- The author assumes no responsibility for any legal consequences arising from commercial or illegal use by users.
π§ Contact
- π¬ Email: 2723659854@qq.com
- π GitHub: 2723659854