1. 程式人生 > 其它 >Windows上nginx-openresty新增rtmp模組的方法

Windows上nginx-openresty新增rtmp模組的方法

技術標籤:音視訊nginxrtmp

網上查詢nginx-openresty新增rtmp模組的方法基本都是在Linux上的。但由於專案需要在Windows上使用nginx,無奈只好自己去找資料,在Windows上編譯nginx-openresty同時加入rtmp模組。
一個基本的思路就是在Windows使用shell工具進行configure生成makefile然後使用mingw或cl進行編譯生成。
其實openresty官方已經給出了在Windows編譯生成的方法,我們只需要參考在Linux新增rtmp模組的方式,準備好nginx-rtmp-module原始碼,在configure時加入新增rtmp模組的語句即可。

一、準備相關檔案
1、下載openresty的最新原始碼
在openresty官網就可以下載最新的原始碼。網址是https://openresty.org/en/download.html
2、下載nginx-rtmp-module原始碼
到github上找到nginx-rtmp-module並下載。網址是https://github.com/arut/nginx-rtmp-module
3、下載msys2
到msys2官網下載msys2的安裝包。網址是https://www.msys2.org
4、下載依賴庫
(1)開啟openresty原始碼的目錄,有個util資料夾,其下面的build-win32.sh,使用記事本開啟就可以看到:
PCRE=pcre-8.44
ZLIB=zlib-1.2.11
OPENSSL=openssl-1.1.1g
(2)有3個依賴庫。接著下面還列出了依賴庫的下載地址:
#wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
#wget http://zlib.net/zlib-1.2.11.tar.gz
#wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
(3)所以目前的版本需要:
①openssl-1.1.1g.tar.gz
②zlib-1.2.11.tar.gz
③pcre/pcre-8.44.tar.gz
5、下載依賴工具
下載strawberryperl安裝包。網址是https://strawberryperl.com/
二、搭建編譯環境
1、安裝msys2
直接開啟msys2安裝包安裝msys2。
2、設定包管理國內映象
這一步不是必須的,不設定映象只是後續操作下載速度會很慢。
(1)找到msys2的安裝目錄,繼續轉到etc\pacman.d目錄下,用記事本開啟下列檔案:
①編輯mirrorlist.mingw32檔案
首行新增:Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/
②編輯mirrorlist.mingw64檔案
首行新增:Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64/
③編輯mirrorlist.msys檔案
首行新增:Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch/
(2)回到msys2的安裝目錄開啟msys2.exe執行 pacman -Sy 重新整理軟體包資料。
3、安裝mingw
在msys2的安裝目錄開啟msys2.exe執行如下語句:
(1)編譯64位安裝
pacman -S mingw-w64-x86_64-toolchain
(2)編譯32位安裝
pacman -S mingw-w64-i686-toolchain
出現提示直接回車完整安裝即可。
4、安裝必要的工具
在msys2的安裝目錄開啟msys2.exe執行如下語句:
(1)pacman -S make
(2)pacman -S patch
三、開始編譯
1、放置原始碼
(1)將openresty原始碼解壓放至msys2的安裝目錄下。
(2)將openssl-1.1.1g.tar.gz、zlib-1.2.11.tar.gz、pcre/pcre-8.44.tar.gz放至msys2的安裝目錄下。
(3)將nginx-rtmp-module原始碼解壓放至msys2的安裝目錄下,資料夾名稱為nginx-rtmp-module。
2、configure加入新增rtmp模組語句
使用記事本開啟openresty原始碼目錄下的utl資料夾中的build-win32.sh檔案,在.configure的末尾加入–add-module=…/nginx-rtmp-module\。如下倒數第二行所示:

./configure \
    --with-cc=gcc \
    --with-ipv6 \
    --prefix= \
    --with-cc-opt='-DFD_SETSIZE=1024' \
    --sbin-path=nginx.exe \
    --with-pcre-jit \
    --without-http_rds_json_module \
    --without-http_rds_csv_module \
    --without-lua_rds_parser \
    --with-ipv6 \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-http_v2_module \
    --without-mail_pop3_module \
    --without-mail_imap_module \
    --without-mail_smtp_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_secure_link_module \
    --with-http_random_index_module \
    --with-http_gzip_static_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-select_module \
    --with-luajit-xcflags="-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT" \
    --with-pcre=objs/lib/$PCRE \
    --with-zlib=objs/lib/$ZLIB \
    --with-openssl=objs/lib/$OPENSSL \
    --add-module=../nginx-rtmp-module\
    -j$JOBS || exit 1

3、使用mingw編譯
(1)在msys2的安裝目錄開啟mingw64.exe或mingw32exe(需要編譯64位開啟前者,32位開啟後者),cd到openresty原始碼目錄,執行:
util/build-win32.sh
(2)等待生成成功
(3)編譯失敗
①編譯nginx-rtmp-module時報錯重定義int8_t
找到nginx-rtmp-module下的ngx_rtmp.h註釋掉:typedef __int8 int8_t;回到(1)
四、打包
1、安裝依賴工具
(1)安裝下載好的strawberryperl安裝包
(2)在msys2的安裝目錄開啟mingw64.exe或mingw32exe(與三3(1)相同)執行:
①pacman -S dos2unix
②pacman -S zip
2、執行打包shell
在msys2的安裝目錄開啟mingw64.exe或mingw32exe(與三3(1)相同),cd到openresty原始碼目錄,執行:
util/package-win32.sh
執行完成後可以看到openresty原始碼目錄下openrestyxxx-win64.zip或openrestyxxx-win32.zip檔案,這就是最終打包的檔案。
五、驗證
1、新增rtmp配置
在生成的conf目錄中開啟nginx.conf,在底部新增:
rtmp {
server {
listen 1935;
application live{
live on;
}
}
}
2、推拉流測試
(1)使用ffmpeg.exe 輸入命令:
ffmpeg -f gdigrab -i desktop -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp:127.0.0.1/live/123
(2)使用ffplay.exe輸入命令
ffplay rtmp:127.0.0.1/live/123 -x 640 -y 360
六、附錄
1、openresty在Windows編譯生成的官方說明
https://github.com/openresty/openresty/blob/master/doc/README-windows.md
2、新增rtmp模組的方法
https://github.com/arut/nginx-rtmp-module