webrtc编译


webrtc编译

最近研究 libmediasoupclient,而 libmediasoupclient 又依赖 libwebrtc,所以首先就想着先在windows上编译一个webrtc的库,先是在网上找了一大堆,发现都不全面,导致各种问题,这里做个归纳总结。

由于 webrtc 是google的产物,而且很多东西都是自成一家,所以前提是需要FQ的。准备一个本地的代理,例如127.0.0.1:10080

windows

挂代理

代理需要挂在git和cmd下,后面通过git拉取depot_tools和webrtc都要借助git的代理,而挂在cmd下是为了在depot_tools下载好之后安装一系列工具链时下载用。

git

通过git拉取代码有http、https、ssh三种方式,对于这三种方式要分两种方式挂代理。详细可参考Git设置代理。本文后续使用的是https方式,所以只需要配置http和https即可。

  • http和https
    • git命令方式
      这里只针对后续可能使用的url地址使用代理,如果不带特定的url的话,会作用于全局。
      git config --global http.https://*.googlesource.com.proxy http://127.0.0.1:10080
      git config --global https.https://*.googlesource.com.proxy http://127.0.0.1:10080
      git config --global http.https://*.appspot.com.proxy http://127.0.0.1:10080
      git config --global https.https://*.appspot.com.proxy http://127.0.0.1:10080
      
    • 修改git配置文件
      修改git的配置文件 .gitconfig
      [http "https://*.googlesource.com"]
          proxy = http://127.0.0.1:10080
      [https "https://*.googlesource.com"]
          proxy = http://127.0.0.1:10080
      [http "https://*.appspot.com"]
          proxy = http://127.0.0.1:10080
      [https "https://*.appspot.com"]
          proxy = http://127.0.0.1:10080
      
  • ssh
    修改 .ssh 文件夹下的 config 文件
    Host googlesource.com
    User git
    ProxyCommand connect -H 127.0.0.1:10080 %h %p
    
    如果是socks5代理的话,最后的选项 -H 改成 -S

cmd

在cmd中直接设置环境变量,这两条命令只在当前窗口有效!

set http_proxy=127.0.0.1:10080
set https_proxy=127.0.0.1:10080

安装depot_tools

按照webrtc源码编译要求的,编译webrtc之前的准备工作需要安装google自家的depot_tools工具链。这个工具链的安装部署直接关系到后面是否能顺利下载webrtc的源码。

按照depot_tools安装描述的过程,大致分为以下几步:

  1. 获取 depot_tools

    • 使用 git 工具直接从google源码库中拉取

      git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
      
    • 直接下载depot_tools包并解压(解压时要以防某些解压工具不将压缩包中的隐藏文件一并解压!) https://storage.googleapis.com/chrome-infra/depot_tools.zip

  2. 设置环境变量
    将depot_tools文件夹所在路径设置在系统环境变量 PATH 的最前面!

  3. 执行 gclient
    通过cmd运行gclient,注意在此之前设置了cmd的代理!这条命令执行完成后,depot_tools目录下会多出很多东西,像python、git等工具都会被下载并安装在depot_tools的子目录下,被用于后续的过程,所以即使本地没有安装这些工具也不要紧。
    这条指令执行完后,大致会下载700-800左右的内容,最后会提示错误,因为后续只需要用来同步webrtc源码以及构建编译脚本等,所以忽略这个报错即可。
    WARNING: Your metrics.cfg file was invalid or nonexistent. A new one will be created.
    Error: client not configured; see 'gclient config'

  4. 创建chrome/4147d的分支

    git checkout -b m84 origin/chrome/4147
    

    并再次执行 gclient sync

编译webrtc

下载源码

按照以下命令一步一步执行,第三步的fetch最花费时间,大概需要下载10G的内容,所以留意你代理的流量套餐!

mkdir webrtc
cd webrtc
fetch --nohooks webrtc
gclient sync

编译

我这里按照mediasoap要求的m84版本的webrtc编译为例

  1. 先创建m84版本(branch-heads/4147)的分支m84,然后执行 gclient sync

    git checkout -b m84 refs/remotes/branch-heads/4147
    
  2. 在环境变量中先设置 DEPOT_TOOLS_WIN_TOOLCHAIN 为0,否则在生成ninja工程时会报错

    set DEPOT_TOOLS_WIN_TOOLCHAIN=0
    
  3. 生成ninja工程

    gn gen out/m84 --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"
    

    如果本地已安装有python3,并且被首选为python3的执行路径,这里有可能会出现报错,如:ValueError: path is on mount 'C:', start on mount 'F:',此时可以将本地安装的python3卸载或者去掉其环境变量,让depot_tool使用自带的python组件即可。

  4. 编译

    ninja -C out/m84 >> compile.log
    
  5. 编译过程中出现错误终端,打开compile.log文件查看失败的地方:

    [1466/3605] CC obj/third_party/ffmpeg/ffmpeg_internal/pcm.obj
    FAILED: obj/third_party/ffmpeg/ffmpeg_internal/pcm.obj 
    ...
    ...
    ...
    ../../third_party/ffmpeg/libavcodec/pcm.c(623): error C2059: 语法错误:“字符串”
    

    这里我将源文件中对应报错的行注释掉。

  6. 继续执行第4步,然后又出现下列错误

    [1834/2133] CXX obj/modules/video_coding/webrtc_h264/h264.obj
    FAILED: obj/modules/video_coding/webrtc_h264/h264.obj 
    ...
    ...
    ...
    E:\google\webrtc\src\modules/video_coding/codecs/h264/h264_decoder_impl.h(21): fatal error C1189: #error:  "See: bugs.webrtc.org/9213#c13."
    

    仍然注释掉报错行之后继续第4步,发现后续还会出现类似的报错,所以这里一次性注释掉另外三个文件中的行:See: bugs.webrtc.org/9213#c13.。三个文件分别是:

    • webrtc\src\modules/video_coding/codecs/h264/h264_decoder_impl.h(21)
    • webrtc\src\modules/video_coding/codecs/h264/h264_encoder_impl.h(21)
    • webrtc\src\modules/video_coding/codecs/h264/h264_color_space.h(20)
  7. 最终生成结果显示一大堆链接错误

    [286/289] LINK peerconnection_client.exe peerconnection_client.exe.pdb
    FAILED: peerconnection_client.exe peerconnection_client.exe.pdb 
    ...
    error LNK2001: 无法解析的外部符号 avpriv_emms_asm
    ...
    ./peerconnection_client.exe : fatal error LNK1120: 1 个无法解析的外部命令
    [287/289] LINK(DLL) webrtc_unity_plugin.dll webrtc_unity_plugin.dll.lib webrtc_unity_plugin.dll.pdb
    FAILED: webrtc_unity_plugin.dll webrtc_unity_plugin.dll.lib webrtc_unity_plugin.dll.pdb 
    ...
    error LNK2001: 无法解析的外部符号 avpriv_emms_as
    ...
    ./webrtc_unity_plugin.dll : fatal error LNK1120: 1 个无法解析的外部命令
    

    忽略,反正在out/m84/obj目录下已经生成我要的webrtc.lib文件了。

Linux

安装depot_tools

  1. 拉取depot_tools

    先设置git的代理,然后拉取depot_tools的代码,之后设置depot_tools的路径到 PATH 环变量。

  2. gclient sync

    在depot_tools目录下执行 gclient sync 之前,先做两件事:

    • 修改 update_depot_tools 文件
      将depot_tools目录下的 update_depot_tools 文件中关于使用root用户时就退出的地方去掉,否则运行这条指令的时候会提示Running depot tools as root is sad.,然后就没了反应。
    • 设置 curl 或者整个linux环境代理
      这里直接设置整个linux环境的代理,否则执行这条指令之后会提示curl https://chrome-infra-packages.appspot.com/ 失败之类的提示。
      可以在 ~/.bashrc 文件中最后加入下列两条,之后每次进入终端后,输入命令setproxy就可以了。
      alias setproxy="export http_proxy=http://10.18.0.60:10080;export https_proxy=http://10.18.0.60:10080;echo 'HTTP Proxy on';"
      alias unsetproxy="unset http_proxy; unset https_proxy; echo 'HTTP Proxy off';"
      

编译webrtc

参考windows上编译webrtc的步骤。

ubuntu上gcc版本最好在6.3之上,并且pkg-config和python已经安装,若pkg-config未安装,则会在生成ninja工程时报错,若gcc版本过低,则在编译时会报错!

参考

WebRTC development
WebRTC Development
depot_tools_tutorial
Git设置代理
curl 设置代理
Windows配置代理下载depot_tools
在win10上编译webRTC(问题篇)
Windows上WebRTC的源码下载和编译
Windows下webrtc.lib 的编译及vs工程中使用