WebRTC 编译实践

本文详述了在Linux和MacOS环境下编译WebRTC的过程及遇到的问题,包括使用fetch命令从源码仓库拉取代码,依赖第三方库的管理,以及如何利用GN和Ninja进行构建。同时,深入解析了WebRTC的工程结构和架构图,为开发者提供全面的参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 时会报错。

win_sdk_change

如果提示错误如下:

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 的选项关闭即可。

windows_python_extension

导入环境变量 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所有子模块
p2pPeer to Peer 相关,STUN/TURN
pcPeer Connection 相关的业务逻辑层
resources资源文件
rtc_base基础代码,如线程/锁 相关的统一接口
rtc_tools音视频分析相关的工具代码
sdk移动端 Android/IOS 的业务逻辑代码,如视频采集/视频渲染等
stats各种数据统计相关代码
style-guide代码风格说明
system_wrappers与操作系统相关的代码,如 CPU 特性/原子操作/时钟等
test测试相关
tools_webrtcWebRTC 测试相关的工具代码,如网络模拟器
video视频引擎

0x03 WebRTC 架构图

WebRTC Architecture

图片来源 https://webrtc.github.io/webrtc-org/architecture/


参考资料

  1. https://webrtc.org/
  2. https://webrtc.github.io/webrtc-org/native-code/development/
  3. depot_tools_tutorial(7) Manual Page
  4. https://dev.chromium.org/developers/how-tos/depottools
  5. WebRTC samples
  6. webrtc所有平台下载编译步骤详细说明
  7. WebRTC中的编译工具 gyp 、gn 与 ninja
  8. WebRTC 架构分析
  9. chromium开发工具–gclient
  10. https://github.com/aggresss/libwebrtc/blob/master/docs/native-code/development/index.md
  11. https://chromium.googlesource.com/chromium/src/+/master/buildtools/checkdeps/README.md
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值