Location>code7788 >text

FFmpeg development notes (forty-three) using SRS to open the SRT protocol live video service

Popularity:828 ℃/2024-08-03 12:03:48
The book "FFmpeg Development in Action: From Zero Basics to Short Video Online" introduces a lightweight streaming server MediaMTX in Chapter 10, through which you can test push and pull streams of streaming protocols such as RTSP/RTMP. However, MediaMTX is too simple to be applied to the production environment of real live broadcasts, and the real streaming media server that can be used in the production environment should also look at SRS or ZLMediaKit.

SRS is a domestic open source streaming media server, supports RTMP, SRT and other mainstream live protocols, its installation instructions see the previous article "Linux environment to install SRS to achieve video push streaming". Combined with SRS and ffmpeg to achieve RTMP protocol push stream function, has been in the "Linux environment to install SRS to achieve video push stream" article in detail, here alone explain how to SRS and ffmpeg to achieve SRT protocol push stream function.
SRS already supports SRT by default during compilation and startup. Check the configuration file dedicated to SRT service of SRS, and find the following configuration information in the srt section, which shows that SRS assigns port 10080 to SRT protocol by default.

srt_server {
    enabled on;
    listen 10080;
    maxbw 1000000000;
    connect_timeout 4000;
    peerlatency 0;
    recvlatency 0;
    latency 0;
    tsbpdmode off;
    tlpktdrop off;
    sendbuf 2000000;
    recvbuf 2000000;
}

In addition to this, add the following line inside the "vhost __defaultVhost__" node:

gop_cache   on;

The role of this line of configuration is to enable the caching of keyframes, the actual application remember to set to on, otherwise the client pulls the stream to find keyframes can not be rendered on the screen.
Then run the following command to start the SRS streaming server dedicated to the SRT protocol.

cd /usr/local/src/srs/trunk
./objs/srs -c conf/ &

After SRS starts, run the following ffmpeg commands to push stream video files to SRT address. Note, make sure FFmpeg on Linux server has integrated libsrt library, otherwise ffmpeg can't push stream to SRT address, for detailed integration steps, please refer to the previous article "Integrate libsrt and librist with FFmpeg in Linux environment".

ffmpeg -re -stream_loop -1 -i "/usr/local/src/test/" -c copy -f mpegts 'srt://127.0.0.1:10080?streamid=#!::r=live/test,m=publish'

Note that the second half of the srt address in the above command is "r=live/test,m=publish", where "r=live/test" means that the service name of SRT is called "live/test" and "m=publish" means that the address belongs to the publishing function, which is for the push streamer to use. live/test", and "m=publish" means that the address belongs to the publish function, which is for the push streamer to use.
SRS also has requirements on the encapsulation format of the video source file, not only the source file is required to be ts format, but also the push stream format is also required to be ts format, so ffmpeg added "-f mpegts" to the ffmpeg command to indicate that it is converted to mpeg ts stream format. If the source file is not in ts format, or not converted to mpegts format, subsequent playback of srt links via ffplay will report the following error.

non-existing PPS 0 referenced

After running the SRT Push Stream command of ffmpeg, SRS outputs the following log message, which shows that its SRT Push Stream function is running normally.

[x7gy1tv8] SRT client ip=127.0.0.1:58898, fd=237547294
[x7gy1tv8] @srt, streamid=#!::r=live/test,m=publish, stream_url=/live/test, vhost=__defaultVhost__, app=live, stream=test, param=
[x7gy1tv8] new srt source, stream_url=/live/test
[x7gy1tv8] new live source, stream_url=/live/test
[x7gy1tv8] ignore disabled exec for vhost=__defaultVhost__
[x7gy1tv8] http: mount flv stream for sid=/live/test, mount=/live/

Then follow the introduction of "FFmpeg Development in Action: From Zero Basics to Online Short Video" book "1.3 Windows System Installation of FFmpeg", install FFmpeg on your PC and open the command line of MSYS, run the following ffplay command, expecting to pull the streams from the SRT address. play. Note, make sure the FFmpeg on your computer has integrated libsrt library, otherwise ffplay will not be able to play srt links, see the previous article "Windows environment to FFmpeg integration libsrt" for detailed integration steps.

ffplay -i 'srt://:10080?streamid=#!::r=live/test,m=request'

The above SRT streaming address is similar to the previous streaming address, except that the IP address is changed to the IP address of the external network, that is, the "m=publish" at the end of the link is changed to "m=request", in which request indicates that the request is also used to pull the stream party.
ffplay runs and pops up the player window and plays the video screen and sound normally. Also observe the SRS service log as shown below:

[3p39n49z] SRT client ip=112.5.138.145:51436, fd=237547293
[3p39n49z] @srt, streamid=#!::r=live/test,m=request, stream_url=/live/test, vhost=__defaultVhost__, app=live, stream=test, param=
[3p39n49z] create ts consumer, no gop cache

As seen from the above logs, SRS has successfully implemented the SRT push-pull streaming feature for live video streaming via the SRT protocol.

For more details on FFmpeg development seeFFmpeg Development in Action: From Zero Basics to Short Video OnlineA book.