0x00 前言
WebRTC 的源码使用 googlesource 进行维护,并没有放到 Github 上面,由于其依赖的第三方库比较多,所以下载和编译比较耗时,遇到的坑也比较多,本文记录在 Linux 和 MacOS 环境下编译 WebRTC 的实践过程和一些见解。
0x01 WebRTC native code 下载和编译
下面这个脚本可以在 Ubuntu 18.04 环境中下载和编译 WebRTC native code
#!/usr/bin/env bash
# WebRTC Compile bundles
# 0x00 Function for envrionment operation
# add new element to environment variable append mode
# $1 enviroment variable
# $2 new element
function env_append()
{
eval local env_var=\$\{${1}\-\}
local new_element=${2%/}
if [ -d "$new_element" ] && ! echo $env_var | grep -E -q "(^|:)$new_element($|:)" ; then
eval export $1="\${$1-}\${$1:+\:}${new_element}"
fi
}
# 0x01 Install depot_tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
env_append PATH ${PWD}/depot_tools
# export DEPOT_TOOLS_UPDATE=0
# 0x02 Install GN
git clone https://gn.googlesource.com/gn.git
env_append PATH ${PWD}/gn/out
cd gn
python build/gen.py
ninja -C out
# 0x02 Checkout WebRTC
mkdir -p webrtc_checkout && cd webrtc_checkout
fetch webrtc
# 当过程中断时,我们可以使用下面的命令恢复并继续
# gclient sync --verbose
# 0x03 Compile WebRTC
cd src
sudo ./build/install-build-deps.sh
gn gen out/Default
ninja -C out/Default
说明:
-
fetch
命令是depot_tools
中的一个工具,其原理是自动拉取 Chrome 工程相关的工具,WebRTC 是 Chrome 工程的一部分,fetch
还可以拉取 Chrome 其他的组件,可以通过fetch --help
查看可拉取的组件,分析depot_tools
目录中的fetch.py
文件可以发现,fetch
支持的 config 选项是从fetch_configs
目录下的*.py
文件中分析出的,我们打开depot_tools/fetch_configs/webrtc.py
文件分析发现 WebRTC 的 Git 地址为https://webrtc.googlesource.com/src.git
-
WebRTC 的 Repository 大小并不是很庞大,大约在 200MB ~ 300MB,但其依赖的第三方库却比较庞大,所有第三方库 pull 下来大约会占用 11GB 的空间,编译完成后加入目标文件和临时文件,会占用约 20GB 的磁盘空间。WebRTC 所依赖的第三方库信息记录在 src 目录下
DEPS
文件中。 -
除了使用
fetch
来拉取 WebRTC 工程外,还可以通过生成.gclient
文件直接拉取,这种方法适合与对 WebRTC 魔改后替换为自己的 Repo 地址#!/usr/bin/env bash cat << END > ./.gclient solutions = [ { "name": "src", "url": "https://webrtc.googlesource.com/src.git", "deps_file": "DEPS", "managed": False, "custom_deps": {}, }, ] END gclient sync --verbose
-
GN 是一种元构建系统,是 GYP 的下一代产物,类似于 CMake、AutoTools;
-
ninja 是用来取代 GNU make 的,据谷歌官方的说法是速度有了好几倍的提升。
-
MacOS 环境需要安装 Xcode
.gclient file
It’s the primary file. It is, in fact, a python script. It specifies the following variables:
- solutions: an array of dictionaries specifying the projects that will be fetched.
- hooks: additional hooks to be run when this meta checkout is synced.
- target_os: an optional array of (target) operating systems to fetch OS-specific dependencies for.
- cache_dir: Primarily for bots, multiple working sets use a single git cache. See gclient.py --cache-dir.
Each project described in the solutions array can contain an optional DEPS file that will be processed. The .gclient file is generated with gclient config or by hand. Each solutions entry is a dictionary that can contain the following variables:
- name: really, the path of the checkout.
- url: the remote repository to fetch/clone.
- custom_deps: (optional) override the dependencies specified in the deps and deps_os variables in child DEPS files. Useful when you want to fetch a writable checkout instead of the default read-only checkout of a dependency, e.g. you work on native_client from a chromium checkout.
- custom_vars: (optional) override the variables defined in vars in child DEPS files. Example: override the WebKit version used for a chromium checkout.
- safesync_url: (optional) url to fetch to retrieve the revision to sync this checkout to.
Windows 下编译注意事项
Windows 下安装需要提前安装 Visual Studio 环境,Desktop development with C++
为必选项,将 Optional
中的 Windows 10 SDK
选上。
安装完成后还需要在 appwiz.cpl
中找到 Windows SDK 修改一下安装属性,将 Debugging Tools for Windows
选上,否则 gn 时会报错。
如果提示错误如下:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
在开始菜单中输入关键字 “Manage App Execution Aliases” 然后将 python.exe
的选项关闭即可。
导入环境变量 DEPOT_TOOLS_WIN_TOOLCHAIN
[Environment]::SetEnvironmentVariable("DEPOT_TOOLS_WIN_TOOLCHAIN", 0, "Process")
0x02 WebRTC 工程结构
目录 | 说明 |
---|---|
api | 对外访问接口 |
audio | 音频引擎 |
call | 数据流的管理层,Call 代表同一端点的所有数据的流入流出 |
common_audio | 音频算法相关 |
common_video | 视频算法相关 |
data | 音视频测试数据文件 |
docs | 文档相关 docs/native-code/rtp-hdrext 为RTP头部拓展定义 |
examples | 示例代码 |
logging | 日志相关 |
media | 多媒体相关逻辑处理,如编解码的逻辑处理 |
modules | 所有子模块 |
p2p | Peer to Peer 相关,STUN/TURN |
pc | Peer Connection 相关的业务逻辑层 |
resources | 资源文件 |
rtc_base | 基础代码,如线程/锁 相关的统一接口 |
rtc_tools | 音视频分析相关的工具代码 |
sdk | 移动端 Android/IOS 的业务逻辑代码,如视频采集/视频渲染等 |
stats | 各种数据统计相关代码 |
style-guide | 代码风格说明 |
system_wrappers | 与操作系统相关的代码,如 CPU 特性/原子操作/时钟等 |
test | 测试相关 |
tools_webrtc | WebRTC 测试相关的工具代码,如网络模拟器 |
video | 视频引擎 |
0x03 WebRTC 架构图
图片来源 https://webrtc.github.io/webrtc-org/architecture/
参考资料
- https://webrtc.org/
- https://webrtc.github.io/webrtc-org/native-code/development/
- depot_tools_tutorial(7) Manual Page
- https://dev.chromium.org/developers/how-tos/depottools
- WebRTC samples
- webrtc所有平台下载编译步骤详细说明
- WebRTC中的编译工具 gyp 、gn 与 ninja
- WebRTC 架构分析
- chromium开发工具–gclient
- https://github.com/aggresss/libwebrtc/blob/master/docs/native-code/development/index.md
- https://chromium.googlesource.com/chromium/src/+/master/buildtools/checkdeps/README.md