Compile and run ZLMediaKit on Ubuntu

Summary

This article describes how to build the ZLMediaKIt project source code on Ubuntu , and how to experience its WebRTC streaming and playback functions.

lab environment

操作系统版本:Ubuntu 22.04.3 LTS
gcc版本:11.4.0
g++版本:11.4.0

Dependent library installation

#让ZLMediaKit媒体服务器具备WebRTC流转发功能的必备依赖包
sudo apt install cmake pkg-config gcc g++ libssl-dev libsrtp2-dev libusrsctp-dev
#其它可选依赖包,请根据实际需要决定是否安装。
sudo apt install libsdl-dev libavcodec-dev libavutil-dev ffmpeg

#检查已安装的必备依赖库的版本
#示例版本为3.0.2
pkg-config --modversion libcrypto libssl
#示例版本为2.4.2
pkg-config --modversion libsrtp2
#示例版本为0.9.5.0
pkg-config --modversion usrsctp

Get ZLMediaKit source code

ZLMediaKit officially recommends using git to clone the ZLMediaKit code. The example is as follows:

#国内用户推荐从同步镜像网站gitee下载 
git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit
cd ZLMediaKit
#千万不要忘记执行这句命令
git submodule update --init

Generate ZLMediaKit build script

cd ZLMediaKit
mkdir -p build
cd build

#采用默认条件编译参数,生成Makefile文件
cmake -S .. -B .

During the above build script generation process, cmake will output a series of prompt messages. Please make sure you can see the following information:

Pay attention to the information pointed by the red arrow. If you don't see it, the WebRTC function must not work properly.

Build ZLMediaKit

#用cmake命令并行编译
cmake --build . -j 16
#或者用make命令并行编译
make -j16
#耐心等待编译进度走到[100%]。

Modify MediaServer configuration

In the new version of ZLMediaKit, officials require modification of the secret key configuration.

Use a text editor to open the MediaServer configuration file ZLMediaKit/release/linux/Debug/config.ini , search for the secret configuration item, and modify the default key.

Open ZLMediaKit/release/linux/Debug/www/webrtc/index.html with a text editor , search for the secret keyword, and ensure that the key parameters of the url are consistent with those in config.ini, otherwise the live stream list will always be encountered in subsequent experiments. empty question.

run MediaServer

#cd到ZLMediaKit的构建输出目录
cd release/linux/Debug

#通过-h可以了解启动参数
./MediaServer -h

#在调试时,不带参数启动,方便观察控制台输出,退出请按Ctrl+C。
./MediaServer

#正式部署时,官方建议以守护进程模式启动
./MediaServer -d &

#杀死MediaServer守护进程的方法
killall -2 MediaServer

WebRTC streaming experiment

On the host where MediaServer is running, open a browser instance (or tab) and enter: in the address bar:

#示例为了避免端口冲突,将config.ini中的[http]节区的port修改为8080
http://localhost:8080/webrtc/

#如果未修改过默认的http服务端口,则应输入如下地址:
http://localhost/webrtc/

In the browser's WebRTC test webpage, the camera resolution can be set.

As a WebRTC push end, select push as the method option, check datachannel, and click the start button.

Not surprisingly, you can see the following screen:

 When you see the picture in the picture above, it means that streaming has started and you can start the WebRTC playback experiment.

WebRTC playback experiment

Open another browser instance (or tab) and enter the same address as the WebRTC streaming experiment in the address bar.

As a WebRTC player, select play for the method option, check datachannel, and click the start button.

Not surprisingly, you can see the following screen:

 Seeing the picture above indicates that the WebRTC streaming and playback experiment based on the ZLMediaKit media server has been successful. Congratulations, you have entered the threshold of WebRTC application development.

RSTP streaming and playback experiment

This section describes how to push a local mp4 file to the ZLMediaKit media server through the RTSP protocol, and allow users to watch it through the WebRTC player in the browser.

Modify server configuration:

Open the media server configuration file config.ini and change the value of the directProxy configuration item to 0. The example is as follows:

[rtsp]
#此配置项必须设置为0,否则WebRTC播放会失败。
directProxy=0

Streaming with ffmpeg:

#参数“-re”表示模拟实际播放的速度进行推流。
#参数“-stream_loop -l”表示循环播放test.mp4。
#参数“-bf 0”表示视频编码时,BFrame数量为0,即禁止使用B帧,避免WebRTC播放时卡顿。
#参数“-b:v 1500000”表示视频编码码率为1.5Mbps,码率越高,视频画面越清晰,对网络带宽要求越高,可省略此参数,采用默认编码码率。
#参数“-b:a 96000”表示音频编码码率为96Kbps,可省略此参数,采用默认编码码率。
ffmpeg -re -stream_loop -1 -i "test.mp4" -bf 0 -vcodec h264 -acodec libopus -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test

To watch live streams with VLC:

在MediaServer中观察流地址(比如:.../rtp/36731AF0)。
打开VLC播放器,在【媒体】菜单中选择【打开网络串流】,输入如下网络 URL:
rtsp://127.0.0.1/live/test
点击【播放】按钮。

Watch the live stream with a browser:

The playback method is the same as [WebRTC playback experiment]. Enter the following URL in the browser:

http://127.0.0.1/webrtc

Click on the live stream list on the left side of the WebRTC player page to start watching.

Summarize

ZLMediaKit has complete functions, excellent performance, and strong cross-platform capabilities. It is a commercial-level streaming media application solution. It is also an excellent example for learning streaming media application development and C++11 programming. It is worthy of in-depth study and understanding.

Guess you like

Origin blog.csdn.net/bigwave2000/article/details/132268733
Recommended