ZLMediaKit 是一个基于 C++11 的高性能运营级流媒体服务框架,这个项目定位于商用级流媒体服务器,支持 linux、macos、windows、android、ios全平台,支持多种协议,如 RTSP、RTMP、HLS、HTTP-FLV、WebSocket-FLV、GB28181、HTTP-TS、WebSocket-TS、HTTP-fMP4、WebSocket-fMP4、MP4、WebRTC等,且支持协议互转,提供了完善的标准 C API、RESTful API和 web hook,支持丰富的业务逻辑,真正做到了拿来即用
下图是 ZLMediaKit 的功能清单,涵盖的内容非常广泛
C++音视频开发学习资料:点击莬费领取→音视频开发(资料文档+视频教程+面试题)(FFmpeg+WebRTC+RTMP+RTSP+HLS+RTP)
RTSP[S]
C++音视频开发学习资料:点击莬费领取→音视频开发(资料文档+视频教程+面试题)(FFmpeg+WebRTC+RTMP+RTSP+HLS+RTP)
源码编译之前,先安装编译器、cmake 构建工具及需要的第三方依赖库
# 安装编译器
sudo apt install build-essential cmake
# 其它依赖库
sudo apt-get install libssl-dev libsdl-dev libavcodec-dev libavutil-dev ffmpeg
完成后,就可以开始拉取源码并编译了,依次执行下面命令
git clone https://github.com/ZLMediaKit/ZLMediaKit.git
cd ZLMediaKit
# 更新子模块
git submodule update --init
mkdir build
cd build
cmake ..
make -j4
编译好后,生成的可执行文件存放在 ZLMediaKit/release/linux
来启动服务
cd ../release/linux
# 可以通过-h来查看命令支持的参数
sudo ./MediaServer -d
(base) xugaoxiang@1070Ti:~/workshop/github/ZLMediaKit/release/linux$ sudo ./MediaServer -d
2021-09-24 14:27:54.227 D MediaServer[1895-main thread] System.cpp:100 startDaemon | 启动子进程:1897
2021-09-24 14:27:54.227 I MediaServer[1897-main thread] System.cpp:130 systemSetup | core文件大小设置为:18446744073709551615
2021-09-24 14:27:54.227 I MediaServer[1897-main thread] System.cpp:139 systemSetup | 文件最大描述符个数设置为:1048576
2021-09-24 14:27:54.232 W MediaServer[1897-main thread] SSLUtil.cpp:98 loadPublicKey | error:02001002:system library:fopen:No such file or directory
2021-09-24 14:27:54.232 W MediaServer[1897-main thread] SSLUtil.cpp:126 loadPrivateKey | error:2006D080:BIO routines:BIO_new_file:no su ch file
2021-09-24 14:27:54.232 D MediaServer[1897-stamp thread] util.cpp:342 operator() | Stamp thread started!
2021-09-24 14:27:54.233 I MediaServer[1897-main thread] EventPoller.cpp:466 EventPollerPool | 创建EventPoller个数:4
2021-09-24 14:27:54.233 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:554
2021-09-24 14:27:54.234 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:332
2021-09-24 14:27:54.234 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:1935
2021-09-24 14:27:54.234 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:19350
2021-09-24 14:27:54.235 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:8080
2021-09-24 14:27:54.235 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:443
2021-09-24 14:27:54.235 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:9000
2021-09-24 14:27:54.236 I MediaServer[1897-main thread] TcpServer.cpp:188 start_l | TCP Server listening on 0.0.0.0:10000
2021-09-24 14:27:54.236 I MediaServer[1897-main thread] UdpServer.cpp:78 start_l | UDP Server bind to 0.0.0.0:10000
2021-09-24 14:27:54.237 I MediaServer[1897-main thread] main.cpp:368 start_main | 已启动http api 接口
2021-09-24 14:27:54.237 I MediaServer[1897-main thread] main.cpp:370 start_main | 已启动http hook 接口
服务启动好了以后,就可以在客户端推流,简单起见,就在同一台机器上推流,不同主机的话,修改下 ip 地址即可
C++音视频开发学习资料:点击莬费领取→音视频开发(资料文档+视频教程+面试题)(FFmpeg+WebRTC+RTMP+RTSP+HLS+RTP)
使用rtsp方式推流
# h264编码
ffmpeg -re -i test.mp4 -vcodec h264 -acodec aac -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test
# h265编码
ffmpeg -re -i test.mp4 -vcodec h265 -acodec aac -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test
ffmpeg 推流成功后,注意看下服务器端的输出信息
2021-09-24 16:47:42.327 D MediaServer[21725-event poller 0] RtspSession.cpp:53 RtspSession | 140068211663280(127.0.0.1:55128)
2021-09-24 16:47:42.329 I MediaServer[21725-event poller 0] MediaSource.cpp:414 emitEvent | 媒体注册:hls __defaultVhost__ live test
2021-09-24 16:47:42.329 D MediaServer[21725-event poller 0] MediaSink.cpp:141 emitAllTrackReady | all track ready use 0ms
2021-09-24 16:47:42.329 I MediaServer[21725-event poller 0] MultiMediaSourceMuxer.cpp:297 onAllTrackReady | stream: rtsp://127.0.0.1:554/live/test , codec info: mpeg4-generic[48000/2/16] H264[854/480/48]
2021-09-24 16:47:45.596 I MediaServer[21725-event poller 0] MediaSource.cpp:414 emitEvent | 媒体注册:rtsp __defaultVhost__ live test
2021-09-24 16:47:45.641 I MediaServer[21725-event poller 0] MediaSource.cpp:414 emitEvent | 媒体注册:rtmp __defaultVhost__ live test
2021-09-24 16:47:45.641 I MediaServer[21725-event poller 0] MediaSource.cpp:414 emitEvent | 媒体注册:ts __defaultVhost__ live test
2021-09-24 16:47:45.728 I MediaServer[21725-event poller 0] MediaSource.cpp:414 emitEvent | 媒体注册:fmp4 __defaultVhost__ live test
可以看到几个媒体注册的消息,同时支持 rtsp、rtmp、hls等协议,现在可以另一台机器上开个播放器播放了,像 vlc、ffplay 都可以,播放的 url 是
rtsp://192.168.1.140/live/test
rtmp://192.168.1.140/live/test
http://192.168.1.140/live/test/hls.m3u8
其中 192.168.1.140 是开启视频服务的机器 IP 地址
hls 默认端口是80,我这里由于冲突,已经将配置文件 config.ini 中的端口改成了8080,这个 ini 文件也是视频服务器的配置文件,可以根据自己的实际情况进行修改
使用rtmp方式推流
ffmpeg -re -i test.mp4 -vcodec h264 -acodec aac -f flv rtmp://127.0.0.1/live/test
# RTMP标准不支持H265,但是国内有自行扩展的,如果你想让ffmpeg支持RTMP-H265,需要重新编译ffmpeg,可以参考:https://github.com/ksvc/FFmpeg/wiki/hevcpush
ffmpeg 推流成功后,服务器端产生的 url 与 上面的rtsp 推流方式类似,这里不再赘述
使用rtp方式推流
ffmpeg -re -i test.mp4 -vcodec h264 -acodec aac -f rtp_mpegts rtp://127.0.0.1:10000
ffmpeg 推送后,服务器端输出了对应流的 ID
拿到了这个 ID 后,这时候就可以拼接 url 进行播放了,如
rtmp://192.168.1.140/rtp/C20C27F2
rtsp://192.168.1.140/rtp/C20C27F2
http://192.168.1.140:8080/rtp/C20C27F2/hls.m3u8
如果你对音视频开发感兴趣,或者对本文的一些阐述有自己的看法,可以在下方的留言框,一起探讨。
留言与评论(共有 0 条评论) “” |