Overview
Since WCS build 5.2.1193 it is possible to publish MPEG-TS RTP stream via UDP to WCS. The feature can be used to publish H264+AAC stream from software or hardware encoder supporting MPEG-TS.
Codecs supported
- H264
- AAC
Operation flowchart
1. Publisher sends REST API query /mpegts/startup
2. Publisher receives 200 OK with URI to publish
3. Stream is publishing to WCS using URI
4. Browser establishes Websocket connestion and sends play
command.
5. Browser receives WebRTC stream and plays it on web page.
Testing
1. For test we use:
- WCS server
- ffmpeg to publish MPEG-TS stream
- Player web application in Chrome browser to play the stream
2. Send /mpegts/startup
query with stream name test
curl -H "Content-Type: application/json" -X POST http://test1.flashphoner.com:8081/rest-api/mpegts/startup -d '{"localStreamName":"test"}'
Where test1.flashphoner.com
- WCS server address
3. Receive 200 OK
response
{ "localMediaSessionId": "32ec1a8e-7df4-4484-9a95-e7eddc45c508", "localStreamName": "test", "uri": "udp://test1.flashphoner.com:31006", "status": "CONNECTED", "hasAudio": true, "hasVideo": true, "record": false, "timeout": 90000, "maxTimestampDiff": 90000 }
4. Publish MPEG-TS stream using URI from the response
ffmpeg -re -i bunny360p.mp4 -c:v libx264 -c:a aac -b:a 160k -bsf:v h264_mp4toannexb -keyint_min 60 -profile:v baseline -preset veryfast -f mpegts "udp://test1.flashphoner.com:31006?pkt_size=1316"
5. Open Player web application. Set the stream name test
to "Stream name" field and click "Start" button. Stream playback will start
Configuration
Stop stream publishing if there are no media data
By default, MPEG-TS stream publishing will stop at server side if server doe not receive any media data from publisher in 90 seconds. The timeout is set in milliseconds by the following papameter
mpegts_stream_timeout=90000
Close subscribers sessions if publisher stops sending media data
If publisher stopped sending media data by some reason, then started again (for example, ffmpeg was restarted), the stream frame timestamps sequence is corrupting. Te stream cannot be played via WebRTC correctky in this case. As workaround, all the subscribers sessions will be closed if stream timestamps sequence corruption occurs, then all the si=ubscribers should connect to the stream again. A maximum timestamp difference is set in seconds by the following parameter
mpegts_max_pts_diff=1
REST API
A REST-query should be HTTP/HTTPS POST request as follows:
- HTTP: http://test.flashphoner.com:8081/rest-api/mpegts/startup
- HTTPS: https://test.flashphoner.com:8444/rest-api/mpegts/startup
Where:
- test.flashphoner.com - is the address of the WCS server
- 8081 - is the standard REST / HTTP port of the WCS server
- 8444 - is the standard HTTPS port
- rest-api - is the required part of the URL
- /mpegts/startup - REST mathod to use
REST methods and response states
REST method | REST query body example | REST response body example | Response states | Description |
---|---|---|---|---|
/mpegts/startup | { "localStreamName":"test" } | { "localMediaSessionId": "32ec1a8e-7df4-4484-9a95-e7eddc45c508", "localStreamName": "test", "uri": "udp://192.168.1.39:31006", "status": "CONNECTED", "hasAudio": true, "hasVideo": true, "record": false, "timeout": 90000, "maxTimestampDiff": 90000 } | 200 - OK 409 - Conflict 500 - Internal error | Start MPEG-TS publishing |
/mpegts/find | { "localStreamName":"test", "uri": "udp://192.168.1.39:31006" } | [ { "localMediaSessionId": "32ec1a8e-7df4-4484-9a95-e7eddc45c508", "localStreamName": "test", "uri": "udp://192.168.1.39:31006", "status": "CONNECTED", "hasAudio": true, "hasVideo": true, "record": false, "timeout": 90000, "maxTimestampDiff": 90000 } ] | 200 – streams found 404 – streams not found 500 - Internal error | Find the MPEG-TS stream by criteria |
/mpegts/find_all | [ { "localMediaSessionId": "32ec1a8e-7df4-4484-9a95-e7eddc45c508", "localStreamName": "test", "uri": "udp://192.168.1.39:31006", "status": "CONNECTED", "hasAudio": true, "hasVideo": true, "record": false, "timeout": 90000, "maxTimestampDiff": 90000 } ] | 200 – streams found 404 – streams not found 500 - Internal error | Find all MPEG-TS streams | |
/mpegts/terminate | { "localStreamName":"test" } | 200 - stream stopped 404 - stream not found 500 - Internal error | Stop MPEG-TS stream |
Parameters
Name | Description | Example |
---|---|---|
localStreamName | Name to set to the stream on server | test |
uri | Endpoint URI to publish the stream | udp://192.168.1.39:31006 |
localMediaSessionId | Stream media session Id | 32ec1a8e-7df4-4484-9a95-e7eddc45c508 |
status | Stream status | CONNECTED |
hasAudio | Stream has audio track | true |
hasVideo | Stream has video track | true |
record | Stream is recording | false |
timeout | Maximum media data receiving timeout, ms | 90000 |
maxTimestampDiff | Maximum stream timestamps difference, ms | 90000 |
Known issues
1. When MPEG-TS stream publishing via UDP is stopped at server side via REST API query /mpegts/terminate
, publishing encoder still sends media data
Symptoms: ffmpeg still sends data via UDP when MPEG-TS stream publishing is stopped on server
Solution: this is normal behaviour for UDP because the protocol itself provides no any methods to let publisher know the UDP port is already closed