今天我們搭建這個 rtsp 服務(wù)器的名稱叫做:ZLMediaKit,它是一個基于 C++11 的高性能運營級流媒體服務(wù)框架
功能圖
如果你想了解更多關(guān)于這個服務(wù)器的功能和代碼,可以去官網(wǎng)查看,這里我就不多說了,下面開始基于此搭建這個服務(wù)器;首先說明的是,我這里使用的環(huán)境是 ubuntu16.04,其實也可以用其它的環(huán)境搭建。
國內(nèi)?戶推薦從同步鏡像?站 gitee 下載:
git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit
root@txp-virtual-machine:/home/txp/share# mkdir rtsp
root@txp-virtual-machine:/home/txp/share# cd rtsp/
root@txp-virtual-machine:/home/txp/share/rtsp# ls
root@txp-virtual-machine:/home/txp/share/rtsp# git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit.git
Cloning into 'ZLMediaKit'...
remote: Enumerating objects: 548, done.
remote: Counting objects: 100% (548/548), done.
remote: Compressing objects: 100% (499/499), done.
remote: Total 548 (delta 60), reused 213 (delta 21), pack-reused 0
Receiving objects: 100% (548/548), 6.81 MiB | 132.00 KiB/s, done.
Resolving deltas: 100% (60/60), done.
Checking connectivity... done.
然后切換到 ZLMediaKit 目錄下,記得執(zhí)行下面這條語句:
root@txp-virtual-machine:/home/txp/share/rtsp# cd ZLMediaKit/
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit# ls
3rdpart AUTHORS build_for_linux.sh CMakeLists.txt Docker postman server www
Android benchmark.md build_for_mac.sh conf LICENSE README_en.md src
api build_docker_images.sh cmake _config.yml package README.md tests
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit# git submodule update --init
Submodule 'ZLToolKit' (https://gitee.com/xia-chu/ZLToolKit) registered for path '3rdpart/ZLToolKit'
Submodule '3rdpart/media-server' (https://gitee.com/xia-chu/media-server) registered for path '3rdpart/media-server'
Cloning into '3rdpart/ZLToolKit'...
remote: Enumerating objects: 1203, done.
remote: Counting objects: 100% (1203/1203), done.
remote: Compressing objects: 100% (1081/1081), done.
remote: Total 5951 (delta 884), reused 161 (delta 120), pack-reused 4748
Receiving objects: 100% (5951/5951), 31.25 MiB | 120.00 KiB/s, done.
Resolving deltas: 100% (4406/4406), done.
Checking connectivity... done.
Submodule path '3rdpart/ZLToolKit': checked out '1df5d6823fc48598c23fd425c5187b0c417a734f'
Cloning into '3rdpart/media-server'...
remote: Enumerating objects: 10370, done.
remote: Counting objects: 100% (10370/10370), done.
remote: Compressing objects: 100% (3708/3708), done.
remote: Total 10370 (delta 7635), reused 9114 (delta 6544), pack-reused 0
Receiving objects: 100% (10370/10370), 2.78 MiB | 144.00 KiB/s, done.
Resolving deltas: 100% (7635/7635), done.
Checking connectivity... done.
Submodule path '3rdpart/media-server': checked out 'ec171caa4b28db0f6ea47bca2c0c9530f345698a'
注意:在編譯這個代碼之前,你的安裝環(huán)境一定要安裝 cmake 和 gcc 版本在 4.8 以上,不然的話,可能會出現(xiàn)問題。
領(lǐng)取音視頻開發(fā)資料包:音視頻流媒體高級開發(fā)
FFmpegWebRTCRTMPRTSPHLSRTP播放器
企鵝君羊994289133領(lǐng)取資料
企鵝君羊994289133領(lǐng)取資料
2、安裝依賴庫:
ZLMediaKit 可選依賴一些第三方庫,這些庫都不是必選的;在構(gòu)建 ZLMediaKit 時,cmake 能查找系統(tǒng)路徑中的這些庫,并根據(jù)安裝情況選擇是否開啟相關(guān)特性,你可以選擇安裝這些依賴并啟用相關(guān)特性:
openssl
flash player 在播放 rtmp 時,采用的是復(fù)雜握手模式,如果不安裝該庫,flash player 將播放不了 zlmediakit 提供的 rtmp url.
同時 ZLMediaKit 的 https/rtsps 相關(guān)功能需要使用 openssl 才能開啟。
ffmpeg
zlmediakit 可以通過 fork ffmpeg 進程的方式實現(xiàn)多種協(xié)議的拉流,編譯時不需要安裝 FFmpeg。
sdl、avcodec、avutil
這 3 個庫供 ZLMediaKit 的 test_player 測試程序使用,你通常不需要安裝這 3 個庫。
#除了openssl,其他其實都可以不安裝
sudo apt-get install libssl-dev
sudo apt-get install libsdl-dev
sudo apt-get install libavcodec-dev
sudo apt-get install libavutil-dev
sudo apt-get install ffmpeg
3、開始編譯:
- 先建立一個 build 目錄,然后切換到該目錄下去:
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit# mkdir build
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit# cd build
- cmake
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Debug版本
-- found library:/usr/lib/x86_64-linux-gnu/libssl.so;/usr/lib/x86_64-linux-gnu/libcrypto.so,ENABLE_OPENSSL defined
-- Performing Test HAVE_MySQL_OPT_EMBEDDED_CONNECTION
-- Performing Test HAVE_MYSQL_OPT_EMBEDDED_CONNECTION - Failed
-- MySQL not found.
-- ENABLE_HLS defined
-- ENABLE_MP4 defined
- 開始正式編譯:make -j4:
編譯過程
4、開始運行:
ZLMediaKit 工程主要生成 3 種二進制目標文件,他們的生成的路徑在 release 目錄下,這些目標文件主要分為(這里我只介紹一種,另外一種感興趣的朋友可以查看官網(wǎng)介紹哈?。?/p>
MediaServer 進程:
這是 ZLMediaKit 作為服務(wù)器的主進程,該進程可以在免去開發(fā)的情況下直接作為測試流媒體服務(wù)器使用,如果你需要更復(fù)雜的業(yè)務(wù)邏輯,可以通過 Web HOOK 和 RESTful API 實現(xiàn),同時你可以通過配置文件控制其參數(shù)。
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit/release# cd linux/Debug/
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug# ls
api_tester_pusher libflv.a libmpeg.a tab test_httpApi test_rtcp test_wsClient
api_tester_server libjsoncpp.a libzlmediakit.a test_bench_proxy test_httpClient test_rtp test_wsServer
api_tester_websocket libmk_api.so libzltoolkit.a test_bench_pull test_pusher test_server
bom libmov.a MediaServer test_bench_push test_pusherMp4 test_sortor
我們可以通過./MediaServer -h 了解啟動參數(shù):
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug# ./MediaServer -h
-h --help 無參 默認:null 選填 打印此信息
-d --daemon 無參 默認:null 選填 是否以Daemon方式啟動
-l --level 有參 默認:0 選填 日志等級,LTrace~LError(0~4)
-m --max_day 有參 默認:7 選填 日志最多保存天數(shù)
-c --config 有參 默認:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug/config.ini 選填 配置文件路徑
-s --ssl 有參 默認:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug/ssl.p12 選填 ssl證書文件或文件夾,支持p12/pem類型
-t --threads 有參 默認:2 選填 啟動事件觸發(fā)線程數(shù)
-v --version 無參 默認:null 選填 顯示版本號
開始運行:
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug# ./MediaServer -d &
[1] 57310
root@txp-virtual-machine:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug# 2021-04-18 21:41:11.260 D MediaServer[57310-139825250957120] System.cpp:100 startDaemon | 啟動子進程:57313
2021-04-18 21:41:11.260 I MediaServer[57313-139825250957120] System.cpp:130 systemSetup | core文件大小設(shè)置為:18446744073709551615
2021-04-18 21:41:11.269 I MediaServer[57313-139825250957120] System.cpp:139 systemSetup | 文件最大描述符個數(shù)設(shè)置為:1048576
2021-04-18 21:41:11.270 I MediaServer[57313-139825250957120] config.cpp:34 loadIniConfig | dump ini file to:/home/txp/share/rtsp/ZLMediaKit/release/linux/Debug/config.ini
2021-04-18 21:41:11.438 W MediaServer[57313-139825250957120] SSLUtil.cpp:98 loadPublicKey | error:02001002:system library:fopen:No such file or directory
2021-04-18 21:41:11.438 W MediaServer[57313-139825250957120] SSLUtil.cpp:126 loadPrivateKey | error:2006D080:BIO routines:BIO_new_file:no such file
2021-04-18 21:41:11.440 D MediaServer[57313-139825143346944] util.cpp:340 operator() | Stamp thread started!
2021-04-18 21:41:11.442 I MediaServer[57313-139825250957120] EventPoller.cpp:467 EventPollerPool | 創(chuàng)建EventPoller個數(shù):2
2021-04-18 21:41:11.445 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:554
2021-04-18 21:41:11.456 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:332
2021-04-18 21:41:11.457 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:1935
2021-04-18 21:41:11.458 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:19350
2021-04-18 21:41:11.458 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:80
2021-04-18 21:41:11.459 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:443
2021-04-18 21:41:11.459 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:9000
2021-04-18 21:41:11.461 I MediaServer[57313-139825250957120] TcpServer.h:320 start_l | TCP Server listening on 0.0.0.0:10000
2021-04-18 21:41:11.462 I MediaServer[57313-139825250957120] main.cpp:339 start_main | 已啟動http api 接口
2021-04-18 21:41:11.463 I MediaServer[57313-139825250957120] main.cpp:341 start_main | 已啟動http hook 接口
注意:上面的兩條報錯,沒有關(guān)系,我們現(xiàn)在已經(jīng)真正開啟了 rtsp 服務(wù)器了。
企鵝君羊994289133領(lǐng)取資料
測試 rtsp 服務(wù)器:
我們現(xiàn)在來把本地媒體文件推流到這個服務(wù)器上,然后再進行拉流播放試試,你可以使用 obs 或者 ffmpeg 命令來進行,這里我使用了 ffmpeg 來進行推流,我電腦本地的媒體文件 test.mp4 路徑如下:
本地的 test.mp4 媒體文件
開始使用 ffmpeg 進行推流:
ffmpeg -re -i test.mp4 -vcodec h264 -acodec aac -f rtsp -rtsp_transport tcp rtsp://192.168.32.128/live/test
注意:后面的 ip 地址是你 ubuntu 搭建服務(wù)器的 ip 地址,這個千萬不要弄錯了,不然試驗不會成功的哈!
推流過程
現(xiàn)在我來開始拉流播放,這里使用 ffplay 進行拉流播放:
ffplay -rtsp_transport tcp rtsp://192.168.32.128/live/test
拉流播放成功
上面是基于 rtsp 協(xié)議進行拉流的,我們也可以用 rtmp 來進行拉流播放;
ffplay rtmp:://192.168.32.128/live/test
rtmp 協(xié)議拉流播放
接著我們使用 udp 的方式來進行拉流,你會發(fā)現(xiàn)速度非常快:
ffplay -rtsp_transport udp rtsp://192.168.32.128/live/test
udp 方式拉流