编译环境
bash
ubuntu20.04
安装编译依赖
bash
sudo apt-get update
sudo apt-get install -y git python2.7 python3 pkg-config g++ libnss3-dev libnspr4-dev libssl-dev libasound2-dev
depot_tools
Git 工具链封装器:统一管理源码拉取、分支切换、依赖同步。
包含核心命令:
fetch
:拉取项目(WebRTC、Chromium 等)gclient
:管理项目依赖(通过.gclient
文件)gn
:生成构建文件(构建系统生成器)ninja
:编译器(Google 优化版 Make 替代)- 还有:
git-cl
,git-cache
,roll-dep
,presubmit
, 等
bash
cd /home/webrtc/depot_tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# 配置depot_tools路径至环境变量
vim ~/.bashrc
# 最后一行添加,保存wq!
export PATH="$PATH:/home/webrtc/depot_tools/depot_tools"
# 执行
source ~/.bashrc
WebRTC源码下载
bash
cd /home/webrtc/
mkdir src
cd src
# 拉取代码
fetch --nohooks webrtc
# 同步依赖(包括第三方依赖、build 工具、工具链等)
gclient sync
配置构建选项
bash
cd src
./build/install-build-deps.sh
gn gen out/Default
源码编译
bash
ninja -C out/Default
编译成功后,进入out/Default目录,即可查看编译结果。

编译参数总结
参数名 | 说明 | 示例值 |
---|---|---|
is_debug |
是否为 Debug 构建 | true / false |
rtc_include_tests |
是否编译测试代码 | false (生产时建议关闭) |
rtc_use_h264 |
启用 H264 支持 | true (需额外引入 openh264/ffmpeg) |
rtc_enable_protobuf |
启用 protobuf 支持(部分 API 需要) | true |
is_component_build |
是否使用组件构建(生成多个动态库) | false (一般推荐 false ) |
use_rtti |
启用 RTTI(运行时类型识别) | true (如你需要 C++ RTTI) |
use_custom_libcxx |
使用自定义 libc++(通常用于 macOS/iOS) | false |
target_cpu |
架构目标 | "x64" 、"arm" 、"arm64" |
target_os |
目标操作系统 | "linux" 、"win" 、"android" 、"ios" |
rtc_include_builtin_audio_codecs |
是否包含内置音频编解码器 | true (如需 Opus) |
rtc_include_builtin_video_codecs |
是否包含内置视频编解码器 | true (如需 VP8/VP9) |
rtc_build_examples |
是否编译 examples(如 peerconnection_client) | false |
rtc_build_tools |
是否构建调试工具 | false |
rtc_build_libevent |
使用 libevent(少见) | false |
示例
bash
gn gen out/Default --args='
is_debug=false
rtc_include_tests=false
rtc_use_h264=true
rtc_include_builtin_audio_codecs=true
rtc_include_builtin_video_codecs=true
rtc_build_examples=false
target_cpu="x64"
target_os="linux"
use_rtti=true
'
WebRTC源码管理
选择特定版本(稳定/旧版本)
WebRTC 的源码不使用传统 tag,而是依赖 branch-heads
或 commit。
查看分支:
bash
git ls-remote origin refs/branch-heads/*
切换到指定分支(例如 M114):
bash
git checkout branch-heads/5912
gclient sync
同步代码(已有代码更新/拉取)
在 src/
目录执行:
bash
gclient sync
可加速或精细控制:
bash
gclient sync --nohooks --no-history --shallow
参数说明:
--nohooks
: 不执行钩子脚本(如下载工具链)--shallow
: 浅拉取,提高速度--no-history
: 不拉 commit 历史,节省空间
更新源码至最新版本
bash
cd src
git checkout main
git pull
gclient sync
如果使用的是某个分支(非 main
),先确认当前分支是否有更新。
重新运行 hooks(必要时)
如果 DEPS
文件有改动,或者构建工具(如 Clang)需要重新安装:
bash
gclient runhooks
.gclient 文件说明(位于 webrtc-checkout/
)
这是 depot_tools 管理的依赖列表,类似 mono-repo 的 manifest:
python
solutions = [
{
"managed": False,
"name": "src",
"url": "https://webrtc.googlesource.com/src.git",
"deps_file": "DEPS",
"custom_deps": {},
"custom_vars": {},
},
]
使用 Python3(推荐)
目前 WebRTC 的 gclient
支持 Python3:
bash
sudo apt install python3 python3-pip
export DEPOT_TOOLS_UPDATE=0
确保 depot_tools 使用的是系统的 Python3。
依赖 DEPS 管理机制简述
WebRTC 使用 DEPS
文件定义 Git 子模块依赖:
- 所有依赖以 Git commit pin 定
- 更新某个依赖需修改
DEPS
文件中的 hash - 可以本地替换模块(用于 patch)
例如:
python
vars = {
'chromium_git': 'https://chromium.googlesource.com',
}
deps = {
'src/third_party/libvpx': Var('chromium_git') + '/webm/libvpx.git@<commit_hash>',
}