### mediasoup编译及注意事项
Mediasoup c++ demo编译总体流程为 webrtc -> mediasoup-demo -> Mediasoup-broadcaster-demo
整体流程参考文档: https://www.jianshu.com/p/00cc45ea4620
Webrtc
地址:https://webrtc.googlesource.com/src.git
参考文档: https://mediasoup.org/documentation/v3/libmediasoupclient/installation/
$ mkdir -p /data/webrtc
$ cd /data/webrtc
# 检出depot_tools工具
$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# 获取webrtc源码,同时会拉取依赖
$ fetch --nohooks webrtc
$ cd src
# 切换到m94分支
$ git checkout -b m94 refs/remotes/branch-heads/4606
# 获取m94分支最近一次的提交记录时间点
$ COMMIT_DATE=$(git log -n 1 --pretty=format:%ci)
# 更改depot_tools工具到和webrtc分支m94比较靠近的时间点
$ cd ../depot_tools
# Check out depot_tools revision from the same time:
$ git checkout $(git rev-list -n 1 --before="$COMMIT_DATE" main)
# 不更新depot_tools版本
$ export DEPOT_TOOLS_UPDATE=0
# 切换到webrtc编译目录
$ cd ../src
# 这里主要是runhook,视网络情况耗时可能比较长
$ gclient sync
# 编译参数设定, 输出在out/m94目录
$ gn gen out/m94 --args='is_debug=false is_component_build=false is_clang=false rtc_include_tests=false rtc_use_h264=true use_rtti=true use_custom_libcxx=false treat_warnings_as_errors=false use_ozone=true'
# 开始编译
$ ninja -C out/m94
关于头文件提取:
native-api的说明: https://webrtc.googlesource.com/src/+/HEAD/native-api.md
注意:需要在gclient sync同步完成后执行脚本, 因为有些依赖不在webrtc仓库中,而在第三方依赖中。
创建一个空目录, 将下面脚本复制并执行, 注意可能需要修改webrtc_path为实际的webrtc的目录
#!/bin/bash
#
webrtc_path="/data/webrtc/src"
cp -a ${webrtc_path}/api .
find api -type f -name "*.cc" -delete
find api -type f -name "*.gn" -delete
base_dir_lists=(
"common_audio/include"
"media/base"
"media/engine"
"modules/audio_coding/include"
"modules/audio_device/include"
"modules/audio_processing/include"
"modules/congestion_controller/include"
"modules/include"
"modules/rtp_rtcp/include"
"modules/rtp_rtcp/source"
"modules/utility/include"
"modules/video_coding/codecs/h264/include"
"modules/video_coding/codecs/vp8/include"
"modules/video_coding/codecs/vp9/include"
"modules/video_coding/include"
"pc"
"rtc_base"
"system_wrappers/include"
)
for i in ${base_dir_lists[@]}
do
mkdir -p ${i}
cp -f ${webrtc_path}/${i}/*.h ${i}/
done
for r in $(seq 10)
do
echo "round ========= ${r} =========="
for i in $(grep -r '^#include' |awk '{print $2}' |grep -v '<' |tr -d '"' |grep -v '^api/' |grep -v '^\.\.' |sort |uniq)
do
#echo "file: ${i}, dir:$(dirname ${i}), name: $(basename ${i})"
if [[ -e ${i} || $(dirname ${i}) == '.' ]]; then
continue
fi
echo ${i}
for j in $(find ${webrtc_path} -name "$(basename ${i})")
do
if echo ${j} |grep -q "${i}"; then
mkdir -p $(dirname ${i})
cp ${j} $(dirname ${i})/
fi
done
done
done
可选操作步骤(和上面编译操作互斥, 只需要选一个执行):
不使用fetch命令下载源码, 通过手动下载并设定指定分支, 然后通过gclient sync进行依赖同步
mkdir -p /data/webrtc
cd /data/webrtc
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
git clone https://webrtc.googlesource.com/src.git
export PATH=/data/webrtc/depot_tools:$PATH
gclient config --name src --unmanaged https://webrtc.googlesource.com/src.git
cd /data/webrtc/src
git fetch origin +refs/branch-heads/*:refs/remotes/branch-heads/*
git checkout -b m94 refs/remotes/branch-heads/4606
COMMIT_DATE=$(git log -n 1 --pretty=format:%ci)
cd /data/webrtc/depot_tools
git checkout $(git rev-list -n 1 --before="$COMMIT_DATE" main)
export DEPOT_TOOLS_UPDATE=0
cd /data/webrtc/src
gclient sync --no-history
gn gen out/m94 --args='is_debug=false is_component_build=false is_clang=false rtc_include_tests=false rtc_use_h264=true use_rtti=true use_custom_libcxx=false treat_warnings_as_errors=false use_ozone=true'
ninja -C out/m94
libmediasoupclient (可选)
下面步骤的Mediasoup-broadcaster-demo自带编译了libmediasoupclient依赖, 所以这项不是必须, 仅用作检验Webrtc编译是否正常
$ cd /data/webrtc
$ git clone https://github.com/versatica/libmediasoupclient.git
# 具体是否切换分支视情况而定, 一般情况就是v3, 不需要切换
$ cd libmediasoupclient
$ cmake . -Bbuild -DLIBWEBRTC_INCLUDE_PATH:PATH=/data/webrtc/src -DLIBWEBRTC_BINARY_PATH:PATH=/data/webrtc/src/out/m94/obj
$ make -C build/
mediasoup-demo
mediasoup-demo是基于nodejs的, 依赖nodejs 版本 >= v16.0.0。
下一步操作的Mediasoup-broadcaster-demo需要依赖这里的服务端;
同时mediasoup-demo的客户端打开web浏览器页面可以提供一个roomid给下一步mediasoup-broadcaster-demo使用
具体流程可以参考官方github文档: https://github.com/versatica/mediasoup-demo
$ cd /data/webrtc
$ git clone https://github.com/versatica/mediasoup-demo.git
$ cd mediasoup-demo
$ git checkout v3
# 服务端编译:
$ cd /data/webrtc/mediasoup-demo/server
$ npm --registry=https://registry.npmmirror.com install
# 客户端编译:
$ cd /data/webrtc/mediasoup-demo/app
# For node 16
$ npm --registry=https://registry.npmmirror.com install
# For node 18, use legacy peer dependencies
$ npm --registry=https://registry.npmmirror.com install --legacy-peer-deps
# 服务端本地运行:
$ cd /data/webrtc/mediasoup-demo/server
$ cp config.example.js config.js
# 运行服务端需要提供一个证书密钥对:fullchain.pem 和 privkey.pem
# 运行前需要确保webRtcServerOptions和webRtcTransportOptions地址正确,否则可能出现无画面情况
$ npm start
# 客户端本地运行(新开一个窗口):
$ cd /data/webrtc/mediasoup-demo/app
$ npm start