Location>code7788 >text

FFmpeg development notes (forty-six) using the SRT protocol to build a cell phone APP live Demo

Popularity:59 ℃/2024-08-11 12:04:19
Whether it is the traditional Internet or mobile Internet, real-time data transmission is just needed, such as QQ, WeChat as the representative of instant messaging tools, able to real-time transmission of text and images. One-to-one graphic communication is called private chat, and many-to-many graphic communication is called group chat.

In addition to the common graphic instant messaging, there are real-time audio and video communications, such as one-to-one audio calls, one-to-one video calls, etc., at this time, you can use WebRTC technology, WebRTC programming and development and its project combat see "Android Studio Development Practice: From Zero Basis to the App online (3rd Edition)," the chapter of 20 "20.2 Integrate WebRTC to the App".
But one-to-many online live broadcasting using WebRTC technology is not very suitable, because WebRTC only cares about punching holes to connect the two sides of the network, without considering the integrated load. Once more devices connected to WebRTC, the entire WebRTC network will be paralyzed. Then one-to-many online live broadcasting should consider deploying an independent streaming media server to handle the audio and video format conversion of the media source, as well as the data distribution work of the audio and video streams through a specialized streaming media transmission protocol. For more detailed knowledge of FFmpeg development, see the book FFmpeg Development in Action: From Zero Basics to Short Video Online.

I. Comparison of four common streaming media transmission protocols

Common streaming media transmission protocols are mainly the following four categories: RTSP protocol, RTMP protocol, SRT protocol and RIST protocol, on the detailed description of these four protocols see the previous article, "Using RTMP protocol to build a computer and cell phone live Demo", here will not repeat.
In general, although the SRT protocol appeared later, its live broadcast quality is much better than RTMP protocol, and it will become the mainstream streaming protocol in the field of domestic live broadcasting in the future. The following is an example of SRT protocol, introducing how to do SRT live streaming to the streaming media server through OBS Studio and SRT Streamer.

Second, the computer side through the OBS Studio for SRT live push streaming

First start the streaming media server MediaMTX on your computer, see "10.2 FFmpeg Push Streaming and Pull Streaming" in the book "FFmpeg Development in Action: From Zero Basis to Short Video on Line" for details of the operation steps, or refer to the previous article "Explaining Push and Pull Streaming of MediaMTX".
Then start the live recording software OBS Studio on your computer, see the previous article "Combining OBS and MediaMTX to realize SRT live streaming" for more details on the operation steps. After that, let OBS Studio to MediaMTX srt address "srt://127.0.0.1:8890?streamid=publish:live" to push the stream, push the streaming process of the OBS Studio recording interface is shown in the figure below.

Then start the streaming media player VLC media player on your computer, open the network stream "srt://127.0.0.1:8890?streamid=read:live", at this time the video playback interface of VLC media player is shown in the following figure.

Combining the live recording screen of OBS Studio and the live viewing interface of VLC media player, it can be seen that the live broadcasting function of SRT protocol is successfully realized by OBS Studio.

Third, the cell phone through the SRT Streamer SRT live push streaming

First start the streaming media server SRS or ZLMediaKit on the cloud service. Deploying and starting SRS or ZLMediaKit on the cloud server is more troublesome, which can't be explained in a few words. If you want to figure out how to operate SRS or ZLMediaKit on the cloud server, you can buy the book "FFmpeg Development in Action: From Zero Basics to Short Video Online" from Jingdong Self-Provisioning, and contact the author to inquire about the detailed operation process of these two streaming media servers on the cloud server.
Then start the live recording software SRT Streamer on your phone, the specific operation steps are detailed in the previous article "Use SRT Streamer to open the APP live streaming".After SRT Streamer is started, click on the "Start Live" button at the bottom of the screen to let SRT Streamer start live streaming to the ZLMediaKit srt address "srt://:9000?streamid=#! Streamer to ZLMediaKit srt address "srt://:9000?streamid=#! ::r=live/test,m=publish", the SRT Streamer recording interface is shown below.

Observing the ZLMediaKit logs on Huawei Cloud as follows, you can see that SRT Streamer is pushing live streams to the back-end streaming server:

[MediaServer] [685314-event poller 0] :103 onRecv | 1-11(223.104.51.155:11372)
[MediaServer] [685314-event poller 0] :166 operator() | test(223.104.51.155:11372) permissible srt push the current (e.g. in mechanics)
[MediaServer] [685314-event poller 0] :143 onTrack | Got track: H264
[MediaServer] [685314-event poller 0] :143 onTrack | Got track: mpeg4-generic
[MediaServer] [685314-event poller 0] :97 onStream | Add track finished
[MediaServer] [685314-event poller 0] :161 emitAllTrackReady | All track ready use 15ms
[MediaServer] [685314-event poller 0] :517 emitEvent | Media Registration:fmp4://__defaultVhost__/live/test
[MediaServer] [685314-event poller 0] :551 onAllTrackReady | stream: schema://__defaultVhost__/app/stream , codec info: mpeg4-generic[44100/2/16] H264[720/1280/0]
[MediaServer] [685314-event poller 0] :517 emitEvent | Media Registration:rtmp://__defaultVhost__/live/test
[MediaServer] [685314-event poller 0] :517 emitEvent | Media Registration:rtsp://__defaultVhost__/live/test
[MediaServer] [685314-event poller 0] :517 emitEvent | Media Registration:ts://__defaultVhost__/live/test
[MediaServer] [685314-event poller 0] :517 emitEvent | Media Registration:hls://__defaultVhost__/live/test

Then start the streaming media player VLC media player on your computer, open the network stream "srt://:9000?streamid=#! ::r=live/test,m=request", at this time the video playback interface of VLC media player is shown below.

Observing the ZLMediaKit logs on Huawei Cloud as follows, you can see that VLC media player is pulling live streams from the back-end streaming server:

[MediaServer] [576478-event poller 0] :103 onRecv | 2-16(112.5.138.145:57022)
[MediaServer] [576478-event poller 0] :731 onShutdown | peer close connection
[MediaServer] [576478-event poller 0] :118 onError | 2-16(112.5.138.145:57022) 6(peer close connection)
[MediaServer] [576478-event poller 0] :14 ~SrtTransportImp | test(112.5.138.145:57022) srt player(__defaultVhost__/live/test)turn off (electric switch),take a period of (x amount of time)(s):16

Combining the live recording screen of SRT Streamer and the live viewing interface of VLC media player, it can be seen that the live broadcasting function of SRT protocol is successfully realized by SRT Streamer.

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