中文请参考 README_CN.md
A media server implementation supporting GB/T 28181, RTSP, RTMP, WebRTC and HTTP streaming protocols.
- Features
- Dependencies
- Build Instructions
- Docker Usage
- Configuration
- Usage
- Web Management Page
- API Usage
- GB/T 28181 Support: Implements GB/T 28181 standard for video surveillance networking systems.
- RTSP Server: Supports Real Time Streaming Protocol (RTSP) for media streaming.
- HTTP Server: Built-in HTTP server for management and signaling.
- HTTP Streaming: Support for media streaming over HTTP.
- RTMP Support: Support for RTMP publish.
- WebRTC Support: Support for WebRTC WHIP (publish) and WHEP (playback) protocols.
- ONVIF Support: Includes handling for ONVIF protocol.
- Device Management: Manages connected devices.
- Database Integration: Uses SQLite for data persistence.
- Extensible Architecture: Built with a modular design using a reactor pattern.
The project requires the following dependencies:
- C++ Compiler: Supports C++17 standard.
- CMake: Version 3.10 or higher.
- FFmpeg: Requires
libavcodec,libavformat,libavutil, andlibswresample. Recommended version 8.0 and above. - SQLite3: Requires
sqlite3library/headers installed on the system. - TinyXML2: Source included.
- OpenSSL (Optional): Required if HTTPS support is enabled.
- libdatachannel (Optional): Required if WebRTC support is enabled.
-
Create a build directory:
mkdir build cd build -
Configure with CMake:
cmake ..
To enable HTTPS support, use:
cmake -DENABLE_HTTPS=1 ..
To enable WebRTC support, use:
cmake -DENABLE_RTC=1 ..
-
Build the project:
cmake --build .
The executable media_server will be generated in the output directory.
You can also run the media server using Docker.
docker pull ghcr.io/greenjim301-ux/media-server:maindocker build -t media-server .docker run -d \
--network host \
-v $(pwd)/output/conf:/app/conf \
-v $(pwd)/output/log:/app/log \
-v $(pwd)/output/files:/app/files \
--name media-server \
media-serverYou can also specify the server IP using -bind_ip:
docker run -d \
--network host \
-v $(pwd)/output/conf:/app/conf \
-v $(pwd)/output/log:/app/log \
-v $(pwd)/output/files:/app/files \
--name media-server \
media-server -bind_ip x.x.x.xNote: Using --network host is recommended for handling RTP/UDP ports efficiently and avoiding NAT issues, especially for WebRTC and RTSP.
If you prefer bridge mode, you need to map all necessary ports:
docker run -d \
-p 26080:26080 \
-p 26090:26090/tcp \
-p 26090:26090/udp \
-p 5080:5080/tcp \
-p 5080:5080/udp \
-v $(pwd)/output/conf:/app/conf \
-v $(pwd)/output/log:/app/log \
-v $(pwd)/output/files:/app/files \
--name media-server \
media-serverConfiguration is loaded from a JSON file. The conf directory in the output folder typically contains config.json. The server sets up logging, and starts various service modules (GB, RTSP, HTTP) based on this configuration.
Note: You need to set `localBindIP` in `config.json` to the server's IP address before starting the service.
If HTTPS support is enabled (built with -DENABLE_HTTPS=1), you need to specify the SSL certificate and key paths in config.json:
{
"sslCertFile": "path/to/cert.pem",
"sslKeyFile": "path/to/key.pem"
}You can use the provided scripts to start and stop the service:
cd output
# Start the service
./start.sh
# Stop the service
./stop.shOr run the compiled executable directly:
cd output
./media_serverEnsure the configuration file and database are accessible as expected by the application.
The media server includes a web-based management interface located in the web directory. This page provides a user-friendly interface for managing devices, files, GB28181 domains, and WebRTC sessions.
Open http://<server_ip>:<httpPort> in your browser.
-
Device Management
- View all devices with status, codec, and resolution information
- Add new RTSP or ONVIF devices
- Preview live streams using mpegts.js
- Preview live streams with PTZ control
- Delete devices
-
File Management
- View uploaded media files with metadata (size, codec, duration, etc.)
- Upload new media files
- Preview files using HTTP-FLV streaming
- Delete files
-
GB28181 Management
- View GB domain list with device counts
- Sync device catalog from GB28181 platforms
- View GB server configuration
- Search and playback GB28181 recordings (with 1-hour time range limit)
-
WebRTC Sessions
- View active WebRTC sessions
- Preview streams using WHEP
The web page supports both English and Chinese languages. Click the EN or 中文 button in the sidebar to switch languages. Your preference is saved in the browser's local storage.
+------------------+----------------------------------------+
| Media Server | Device List [+] [↻] |
| [EN] [中文] |----------------------------------------|
| | Device ID | Name | Protocol | Status |
| ▶ Device | --------- | ---- | -------- | ------ |
| 📄 File | cam_001 | Cam1 | RTSP | ON |
| 🏢 GB Domain | cam_002 | Cam2 | GB28181 | ON |
| 📡 WebRTC | |
+------------------+----------------------------------------+
To retrieve the list of devices or a specific device, send a HTTP GET request to /device.
URL: http://<server_ip>:<httpPort>/device
Method: GET
Parameters:
deviceId(optional): The ID of the specific device to retrieve. If omitted, all devices are returned.
Example using curl:
Get all devices:
curl -X GET http://127.0.0.1:26080/deviceGet a specific device:
curl -X GET "http://127.0.0.1:26080/device?deviceId=rtsp_cam_01"Response Example:
{
"code": 0,
"msg": "success",
"result": [
{
"deviceId": "rtsp_cam_01",
"domainId": "domain_01",
"name": "Camera 01",
"type": "camera",
"parentId": ["parent_01"],
"status": "online",
"manufacturer": "Hikvision",
"model": "DS-2CD2T45",
"owner": "admin",
"civilCode": "110000",
"address": "Building A",
"ipAddr": "192.168.1.100",
"user": "admin",
"pass": "******",
"port": 554,
"longitude": "116.397128",
"latitude": "39.916527",
"ptzType": 1,
"url": "rtsp://192.168.1.100:554/stream1",
"protocol": 0,
"codec": "H.264",
"resolution": "1920x1080",
"bindIP": "0.0.0.0",
"remark": "Front entrance camera",
"onvifProfile": "Profile_1",
"onvifPtzUrl": "http://192.168.1.100/onvif/ptz"
}
]
}To add an RTSP device, send a HTTP POST request to /device with the following JSON body:
URL: http://<server_ip>:<httpPort>/device
Method: POST
Body:
{
"name": "My_RTSP_Camera",
"protocol": 2,
"url": "rtsp://admin:123456@192.168.1.100:554/ch1/main/av_stream"
}name: Display name for the device (string).protocol:2for RTSP device.url: The full RTSP stream URL.
Example using curl:
curl -X POST http://127.0.0.1:26080/device \
-H "Content-Type: application/json" \
-d '{
"name": "Door Camera",
"protocol": 2,
"url": "rtsp://192.168.1.50:554/live"
}'To add an ONVIF device, send a HTTP POST request to /device with the following JSON body. The media server will probe the ONVIF device, get the RTSP URL and PTZ control URL, and add this device as an RTSP device.
URL: http://<server_ip>:<httpPort>/device
Method: POST
Body:
{
"name": "My_ONVIF_Camera",
"protocol": 4,
"ipAddr": "192.168.1.100",
"user": "admin",
"pass": "123456"
}name: Display name for the device (string).protocol:4for ONVIF device.ipAddr: The IP address of the ONVIF device.user: ONVIF username.pass: ONVIF password.
Example using curl:
curl -X POST http://127.0.0.1:26080/device \
-H "Content-Type: application/json" \
-d '{
"name": "Office Camera",
"protocol": 4,
"ipAddr": "192.168.1.100",
"user": "admin",
"pass": "123456"
}'To retrieve the live streaming URLs (RTSP, HTTP-TS, HTTP-FLV) for a device.
URL: http://<server_ip>:<httpPort>/device/url
Method: GET or POST
Parameters:
deviceId(required): The ID of the device.netType(optional): Network type preference.
Example using curl:
curl -X POST http://127.0.0.1:26080/device/url \
-H "Content-Type: application/json" \
-d '{
"deviceId": "rtsp_cam_01"
}'Response:
{
"code": 0,
"msg": "success",
"result": {
"rtspUrl": "rtsp://192.168.1.100:554/live/rtsp_cam_01",
"httpTsUrl": "http://192.168.1.100:8080/live/rtsp_cam_01.ts",
"httpFlvUrl": "http://192.168.1.100:8080/live/rtsp_cam_01.flv",
"rtcUrl": "http://192.168.1.100:8080/rtc/whep/rtsp_cam_01"
}
}Note: You can use mpegts.js to play HTTP-FLV/TS streams in the browser.
Codec Support: Media-server only supports H.264, H.265, AAC, and Opus codecs. The rtcUrl is for WebRTC WHEP playback. For WHEP, only H.264, H.265, and Opus are supported. AAC audio will be transcoded to Opus automatically.
To control the PTZ (Pan-Tilt-Zoom) of a device. Supported for GB28181 and ONVIF devices. Note that some devices may not support all commands.
URL: http://<server_ip>:<httpPort>/device/ptz
Method: POST
Body:
{
"deviceId": "34020000001320000001",
"ptzCmd": 1,
"timeout": 500,
"presetID": "1"
}deviceId(required): The ID of the device.ptzCmd(required): The PTZ command integer.1: Move Left2: Move Right3: Move Up4: Move Down5: Zoom In6: Zoom Out7: Goto Preset8: Set Preset9: Delete Preset11: Move Left Up12: Move Right Up13: Move Left Down14: Move Right Down
timeout(optional): The duration of the movement in milliseconds. After this time, the movement stops (default: 500).presetID(optional): The preset ID, required for preset commands (7, 8, 9).
To query the presets of a device.
URL: http://<server_ip>:<httpPort>/device/preset
Method: GET or POST
GET Example:
curl -X GET "http://127.0.0.1:26080/device/preset?deviceId=34020000001320000001"POST Body:
{
"deviceId": "34020000001320000001"
}deviceId(required): The ID of the device.
Response Example:
{
"code": 0,
"msg": "ok",
"result": [
{"presetID": "1"},
{"presetID": "2"},
{"presetID": "3"}
]
}-
Upload File
Upload a video file to the server using HTTP multipart/form-data.
URL:
http://<server_ip>:<httpPort>/file/uploadMethod:POSTContent-Type:multipart/form-data -
Get File List
Retrieve the list of uploaded files or information about a specific file.
URL:
http://<server_ip>:<httpPort>/fileMethod:GETParameters:fileId(optional): The ID of the specific file to retrieve. If omitted, all files are returned.
Response Example:
{ "code": 0, "msg": "success", "result": [ { "fileId": 1, "fileName": "sample.mp4", "size": 10485760, "codec": "h264", "resolution": "1920x1080", "duration": 120.5, "frameRate": 25.0 } ] } -
Get File Playback URL
Get the playback URL for a specific file.
URL:
http://<server_ip>:<httpPort>/file/urlMethod:GETParameters:fileId(required): The ID of the file.netType(optional): Network type preference.
Response Example:
{ "code": 0, "msg": "success", "result": { "rtspUrl": "rtsp://192.168.1.100:554/vod/abc123xyz/sample.mp4", "httpTsUrl": "http://192.168.1.100:8080/vod/abc123xyz/ts/sample.mp4", "httpFlvUrl": "http://192.168.1.100:8080/vod/abc123xyz/flv/sample.mp4" } }
-
Get GB Server Information
Retrieve the server information required to configure your GB28181 device or platform.
URL:
http://<server_ip>:<httpPort>/gb/serverMethod:GETExample:
curl -X GET http://127.0.0.1:26080/gb/server
Response:
{ "code": 0, "msg": "success", "result": { "id": "34020000002000000001", "ip": "192.168.1.100", "port": 5080, "pass": "12345678", "rtpTransport": 2 } }Note on
rtpTransport: ThertpTransportfield indicates the transport method the GB server uses to receive streams from the device or platform:0: UDP1: TCP Active2: TCP Passive (Default)
You can change this value by modifying the
rtpTransportfield inconf/config.json. -
Configure GB Device/Platform
Use the information from the previous step (
id,ip,port,pass) to configure the "SIP Server" or "Platform Access" settings on your GB28181 device or platform. -
Verify Registration
After configuration, the device should register automatically. Check the registered domains/devices.
URL:
http://<server_ip>:<httpPort>/gb/domainMethod:GETExample:
curl -X GET http://127.0.0.1:26080/gb/domain
Response Example:
{ "code": 0, "msg": "success", "result": [ { "id": "34020000002000000002", "devNum": 5, "ip": "192.168.1.50", "port": 5060 } ] } -
Sync Device Channel List
The server usually syncs the device list automatically upon registration. If this does not happen, you can trigger it manually.
URL:
http://<server_ip>:<httpPort>/gb/catalogMethod:GETExample:
curl -X GET http://127.0.0.1:26080/gb/catalog
Once the device channels are synced, you can use the Get Device List and Get Device Preview URL APIs to access the video streams.
-
Query Record List
To query the recording files on a device, send a POST request to
/gb/record.URL:
http://<server_ip>:<httpPort>/gb/recordMethod:POSTBody:
{ "deviceId": "34020000001320000001", "startTime": "2023-10-27T10:00:00", "endTime": "2023-10-27T11:00:00", "type": "all" }deviceId: GB device ID (must be sub-device/channel ID)startTime: Start time (ISO 8601 format or similar string)endTime: End timetype: Record type ("all", etc.)
Response Example:
{ "code": 0, "msg": "success", "result": [ { "deviceId": "34020000001320000001", "name": "Recording 1", "startTime": "2023-10-27T10:00:00", "endTime": "2023-10-27T10:30:00", "type": "time" }, { "deviceId": "34020000001320000001", "name": "Recording 2", "startTime": "2023-10-27T10:30:00", "endTime": "2023-10-27T11:00:00", "type": "time" } ] } -
Get Playback URL
After identifying the recording you want to play, request the playback stream URL.
URL:
http://<server_ip>:<httpPort>/gb/record/urlMethod:POSTBody:
{ "deviceId": "34020000001320000001", "startTime": "2023-10-27T10:00:00", "endTime": "2023-10-27T11:00:00", "type": "time" }Response:
{ "code": 0, "msg": "success", "result": { "rtspUrl": "rtsp://192.168.1.100:554/gbvod/randStr/34020000001320000001-start-end", "httpTsUrl": "http://192.168.1.100:8080/gbvod/randStr/34020000001320000001-start-end.ts", "httpFlvUrl": "http://192.168.1.100:8080/gbvod/randStr/34020000001320000001-start-end.flv" } }You can use RTSP or HTTP-FLV/TS players (like mpegts.js) to play these playback streams.
-
Publish Stream (WHIP)
Publish a WebRTC stream to the media server using the WHIP protocol.
URL:
http://<server_ip>:<httpPort>/rtc/whipMethod:POSTBody: SDP OfferResponse: SDP Answer (201 Created)
Codec Support: For WHIP, only H.264, H.265, and Opus codecs are supported.
-
Get Live Sessions
Retrieve the list of active WebRTC sessions.
URL:
http://<server_ip>:<httpPort>/rtc/sessionMethod:GETResponse Example:
{ "code": 0, "msg": "ok", "result": [ { "sessionId": "abc123xyz456", "videoCodec": "H264", "audioCodec": "OPUS" } ] } -
Get Playback URL
Get the playback URL for a specific WebRTC session.
URL:
http://<server_ip>:<httpPort>/rtc/session/urlMethod:GETParameters:sessionId(required): The ID of the session.
Response Example:
{ "code": 0, "msg": "success", "result": { "httpFlvUrl": "http://192.168.1.100:8080/live/abc123.flv", "httpTsUrl": "http://192.168.1.100:8080/live/abc123.ts", "rtcUrl": "http://192.168.1.100:8080/rtc/whep/abc123", "rtspUrl": "rtsp://192.168.1.100:554/live/abc123" } }Note: Converting WebRTC streams to HTTP-FLV requires FFmpeg 8.0 or above.
WHEP (WebRTC-HTTP Egress Protocol) allows you to play live streams via WebRTC with ultra-low latency.
Supported Sources:
-
RTSP and GB28181 Live Cameras
For RTSP cameras and GB28181 devices, use the
rtcUrlreturned byGET /device/url:GET /device/url?deviceId=<deviceId>The response
rtcUrlfield (e.g.,http://192.168.1.100:8080/rtc/whep/rtsp_cam_01) can be used directly with a WHEP client. -
WHIP Streams
For WebRTC streams published via WHIP, use the
rtcUrlreturned byGET /rtc/session/url:GET /rtc/session/url?sessionId=<sessionId>The response
rtcUrlfield (e.g.,http://192.168.1.100:8080/rtc/whep/abc123) can be used directly with a WHEP client.
WHEP Playback:
URL: http://<server_ip>:<httpPort>/rtc/whep/<streamId>
Method: POST
Body: SDP Offer
Response: SDP Answer (201 Created)
Codec Support: For WHEP, only H.264, H.265, and Opus codecs are supported. AAC audio will be transcoded to Opus automatically.
You can publish a live stream using RTMP.
URL: rtmp://<server_ip>:<rtmpPort>/live/<streamId>
Example using FFmpeg:
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://127.0.0.1:1935/live/mystreamGet Playback URL:
-
Get Stream List:
URL:
http://<server_ip>:<httpPort>/rtmp/streamMethod:GETResponse will contain the list of active streams.
Response Example:
{ "code": 0, "message": "OK", "result": [ { "stream": "mystream", "videoCodec": "H.264", "audioCodec": "AAC" } ] } -
Get Playback URL for a Specific Stream:
URL:
http://<server_ip>:<httpPort>/rtmp/stream/url?stream=<streamId>Method:GETResponse Example:
{ "code": 0, "msg": "success", "result": { "httpFlvUrl": "http://192.168.1.100:8080/live/mystream.flv", "httpTsUrl": "http://192.168.1.100:8080/live/mystream.ts", "rtcUrl": "http://192.168.1.100:8080/rtc/whep/mystream", "rtspUrl": "rtsp://192.168.1.100:554/live/mystream" } }