webrtc 编译环境搭建

由于开发和研究需求,涉及到webrtc的native源码的编译和研究,就开启了自己的趟坑之路。但是webrtc的工程代码很庞大,涉及到的第三方库以及编译的工具都很多,光下载就要耗费很长时间,而且如果没有正确的网络下载起来还很麻烦。在拥有完整开发工程之后,编译也是个耗费精力的大工程。索性就记录下自己的趟坑过程。

下载

首先就是源码工程的下载。本次下载的是国内的镜像工程。目前自己找到的合适的镜像是声网和学而思的,而自己下载的是声网的镜像。

下载和编译的参考

声网镜像的参考地址
学而思的镜像参考地址(这个目前下载代理有问题)

自己的摘录
  • 同步工具depot_tools安装

webrtc工程的代码和编译工具都是通过gclient 来同步的,这是个对git 封装的工具。

##指定自己要同步的目录地址
export WORKSPACE=`pwd`
cd $WORKSPACE
rm -rf depot_tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
 
cd $WORKSPACE/depot_tools
git checkout gitlab
chmod +x $WORKSPACE/depot_tools/cipd
 
export PATH=$PATH:$WORKSPACE/depot_tools
  • 下载环境配置
git config --global user.email ""
git config --global user.name ""
 
git config --global url.http://120.92.49.206:3232/chromiumsrc/webrtc.git.insteadOf https://chromium.googlesource.com/external/webrtc.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/base.git.insteadOf https://chromium.googlesource.com/chromium/src/base
git config --global url.http://120.92.49.206:3232/chromiumsrc/build.git.insteadOf https://chromium.googlesource.com/chromium/src/build
git config --global url.http://120.92.49.206:3232/chromiumsrc/buildtools.git.insteadOf https://chromium.googlesource.com/chromium/src/buildtools
git config --global url.http://120.92.49.206:3232/chromiumsrc/gradle.git.insteadOf https://chromium.googlesource.com/external/github.com/gradle/gradle.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/ios.git.insteadOf https://chromium.googlesource.com/chromium/src/ios.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/testing.git.insteadOf https://chromium.googlesource.com/chromium/src/testing
git config --global url.http://120.92.49.206:3232/chromiumsrc/third_party.git.insteadOf https://chromium.googlesource.com/chromium/src/third_party
git config --global url.http://120.92.49.206:3232/chromiumsrc/clang-format.git.insteadOf https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libcxx.git.insteadOf https://chromium.googlesource.com/chromium/llvm-project/libcxx.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libcxxabi.git.insteadOf https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libunwind.git.insteadOf https://chromium.googlesource.com/external/llvm.org/libunwind.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/android_ndk.git.insteadOf https://chromium.googlesource.com/android_ndk.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/android_tools.git.insteadOf https://chromium.googlesource.com/android_tools.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/auto.git.insteadOf https://chromium.googlesource.com/external/github.com/google/auto.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/catapult.git.insteadOf https://chromium.googlesource.com/catapult.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/compact_enc_det.git.insteadOf https://chromium.googlesource.com/external/github.com/google/compact_enc_det.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/colorama.git.insteadOf https://chromium.googlesource.com/external/colorama.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/depot_tools.git.insteadOf https://chromium.googlesource.com/chromium/tools/depot_tools.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/errorprone.git.insteadOf https://chromium.googlesource.com/chromium/third_party/errorprone.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/ffmpeg.git.insteadOf https://chromium.googlesource.com/chromium/third_party/ffmpeg.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/findbugs.git.insteadOf https://chromium.googlesource.com/chromium/deps/findbugs.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/freetype2.git.insteadOf https://chromium.googlesource.com/chromium/src/third_party/freetype2.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/harfbuzz.git.insteadOf https://chromium.googlesource.com/external/github.com/harfbuzz/harfbuzz.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/gtest-parallel.git.insteadOf https://chromium.googlesource.com/external/github.com/google/gtest-parallel
git config --global url.http://120.92.49.206:3232/chromiumsrc/googletest.git.insteadOf https://chromium.googlesource.com/external/github.com/google/googletest.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/icu.git.insteadOf https://chromium.googlesource.com/chromium/deps/icu.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/jsr-305.git.insteadOf https://chromium.googlesource.com/external/jsr-305.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/jsoncpp.git.insteadOf https://chromium.googlesource.com/external/github.com/open-source-parsers/jsoncpp.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/junit.git.insteadOf https://chromium.googlesource.com/external/junit.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/fuzzer.git.insteadOf https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libjpeg_turbo.git.insteadOf https://chromium.googlesource.com/chromium/deps/libjpeg_turbo.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libsrtp.git.insteadOf https://chromium.googlesource.com/chromium/deps/libsrtp.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libvpx.git.insteadOf https://chromium.googlesource.com/webm/libvpx.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/libyuv.git.insteadOf https://chromium.googlesource.com/libyuv/libyuv.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/linux-syscall-support.git.insteadOf https://chromium.googlesource.com/linux-syscall-support.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/mockito.git.insteadOf https://chromium.googlesource.com/external/mockito/mockito.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/nasm.git.insteadOf https://chromium.googlesource.com/chromium/deps/nasm.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/openh264.git.insteadOf https://chromium.googlesource.com/external/github.com/cisco/openh264
git config --global url.http://120.92.49.206:3232/chromiumsrc/requests.git.insteadOf https://chromium.googlesource.com/external/github.com/kennethreitz/requests.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/robolectric.git.insteadOf https://chromium.googlesource.com/external/robolectric.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/ub-uiautomator.git.insteadOf https://chromium.googlesource.com/chromium/third_party/ub-uiautomator.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/usrsctp.git.insteadOf https://chromium.googlesource.com/external/github.com/sctplab/usrsctp
git config --global url.http://120.92.49.206:3232/chromiumsrc/binaries.git.insteadOf https://chromium.googlesource.com/chromium/deps/yasm/binaries.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/patched-yasm.git.insteadOf https://chromium.googlesource.com/chromium/deps/yasm/patched-yasm.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/tools.git.insteadOf https://chromium.googlesource.com/chromium/src/tools
git config --global url.http://120.92.49.206:3232/chromiumsrc/client-py.git.insteadOf https://chromium.googlesource.com/infra/luci/client-py.git
git config --global url.http://120.92.49.206:3232/chromiumsrc/boringssl.git.insteadOf https://boringssl.googlesource.com/boringssl.git
  • 开始下载
rm -rf $WORKSPACE/webrtc
mkdir $WORKSPACE/webrtc
cd $WORKSPACE/webrtc
gclient config --name src https://chromium.googlesource.com/external/webrtc.git@gitlab
 
export CDS_CLANG_BUCKET_OVERRIDE=http://120.92.49.206:3232/chromiumsrc/commondatastorage/raw/master/public/chromium-browser-clang
 
#cd ~/depot_tools; git fetch; git reset --hard origin/gitlab; chmod +x ~/depot_tools/cipd
#下载Android相关需要添加Android的源
cd $WORKSPACE/webrtc

echo "target_os = [ 'android' ]" >> .gclient

gclient sync --patch-ref=https://chromium.googlesource.com/chromium/src/build.git@gitlab


编译

Linux

Linux编译相对简单,同步完成之后安装依赖,再进行编译应该基本没什么大问题。

apt-get update
apt-get install -y g++

export PATH=$PATH:$WORKSPACE/depot_tools

cd $WORKSPACE/webrtc/src

./build/install-build-deps.sh

gn gen --ide=vs out/linux "--args=is_debug=false rtc_use_h264=true proprietary_codecs=true ffmpeg_branding=\"Chrome\" treat_warnings_as_errors=false rtc_include_tests=false is_clang=false use_custom_libcxx=false rtc_build_json=true"

ninja -C out/linux

gn 参数说明:
rtc_use_h264=true 打开264的编译宏支持
proprietary_codecs=true 支持264 codec
ffmpeg_branding=“Chrome” 编译带有 264 codec版本的ffmpeg,需配合上述2个参数来使能h264支持
is_clang=false配合use_custom_libcxx=false:使用gcc编译,而不是默认的clang
rtc_build_json=true: 编译webrtc 自带的jsoncpp,而不是使用系统安装的版本

Android

首先要安装java环境,最好是jdk8,不然又会有很多相关目录的兼容问题。这里建议一点是注意添加 jar的执行环境,因为webrtc工程中是通过jar 的运行环境来找到java的运行环境。

##查看候选
update-alternatives --config jar
##安装候选
##最后的是权重,如果存在多个jar环境,最好把提到所有候选的最高,这样就可以是默认的
update-alternatives --install /usr/bin/jar jar /usr/bin/jdk1.8.0_91/bin/jar 300
  • 最简单的编译
cd $WORKSPACE/webrtc/src

gn gen android/Release "--args=is_debug=false target_os=\"android\" target_cpu=\"arm64\""

ninja -C android/Release
  • mediasoup native的适配

上述的通用编译并不能适用于所有场合,比如视频会议mediasoup 工程需要不同的编译选项。

#mediasoup native 编译选项
gn gen out/android "--args=is_debug=false target_os=\"android\" target_cpu=\"arm64\" rtc_include_tests=false rtc_build_examples=false rtc_use_h264=true rtc_enable_protobuf=false use_rtti=true treat_warnings_as_errors=false use_ozone=true"

注意该配置开启了“use_rtti=true”,默认webrtc是不开启的,这样编译链接的时候不会报typeinfo 的错误,比如

undefined reference to 'typeinfo for rtc::MessageHandler'

当然也可以选择不开启rtti,然后在mediasoup的链接选项中添加 “-fno-rtti”。

  • 静态库的编译

获取libwertc.a 静态链接库,用于二次链接开发

ninja -C  out/android webrtc
  • 编译中遇到的坑
  1. webrtc对应的m73~74版本编译出来的静态库是有一些问题的,比如链接的时候会提示
../../modules/audio_processing/agc2/interpolated_gain_curve.cc:0:
error: undefined reference to 'std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > std::__1::operator+<char, std::__1::char_traits<char>,
std::__1::allocator<char> >(char const*, std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&)'

原因和解决方法可以参考这个讨论,也可以下载我的资源,然后替换src/build底下对应的目录文件。

  1. 遇到java代码中Android 的api level的解析不对
../SRC_ROOT1/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java:319 Unnecessary; SDK_INT is never < 16: ObsoleteSdkInt [warning]
        && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lint found 1 new issues.
 - For full explanation, please refer to out/android_32/gen/sdk/android/peerconnection_java__lint/result.xml
 - For more information about lint and how to fix lint issues, please refer to https://chromium.googlesource.com/chromium/src/+/master/build/android/docs/lint.md

目前不清楚是怎么解析失败,暂时把 “Build.VERSION_CODES.O_MR1” 改写成数字27(参考Android官方定义)。

windows

需要在cmd 环境下操作,可以windows + R 打开cmd。

  • 环境的准备
#用于下载windows的编译工具的
set CDS_CLANG_BUCKET_OVERRIDE=http://120.92.49.206:3232/chromiumsrc/commondatastorage/raw/master/public/chromium-browser-clang
#禁止depottool 更新
set DEPOT_TOOLS_UPDATE=0
#选择使用vs的编译工具进行编译
set DEPOT_TOOLS_WIN_TOOLCHAIN=0
set GYP_GENERATORS=msvs-ninja,ninja
#选择vs的版本
set GYP_MSVS_VERSION=2019
#设置windows sdk 位置
set WINDOWSSDKDIR=c:\Program Files (x86)\Windows Kits\10
set vs2019_install=c:\Program Files (x86)\Microsoft Visual Studio\2019\Community

另外注意还需要配置开头git 的代理
  • 接下来的执行步骤
#先执行gclient 来下载安装windows的依赖工具,例如git 和 python
gclient
cd path\to\webrtc
gclient sync --force --patch-ref=https://chromium.googlesource.com/chromium/src/build.git@gitlab

cd path\to\webrtc\src
gn gen --ide=vs2019 out/windows32 "--args=is_debug=false rtc_use_h264=true proprietary_codecs=true ffmpeg_branding=\"Chrome\" treat_warnings_as_errors=false rtc_include_tests=false is_clang=false use_custom_libcxx=false rtc_build_json=true target_cpu=\"x86\" use_lld=false enable_iterator_debugging=true"
ninja -C out\windows32

说明:
enable_iterator_debugging=true //是为了visual studio 编译的时候解决_ITERATOR_DEBUG_LEVEL 不匹配的问题

  • 需要修改的地方

如果上述gn gen 和ninja 编译出现错误可以参考如下修改

modules/video_coding/BUILD.gn 注释掉 assert(!(is_win && !is_clang) 这一行
third_party\ffmpeg\libavcodec\pcm.c  注释掉最后的PCM_VIDC 相关的信息

vs工程需要额外添加的项目

在vs工程新建应用工程时可能需要额外添加c/c++编译参数 /D_ITERATOR_DEBUG_LEVEL=0 
额外依赖的库:
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "secur32.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "dmoguids.lib")
#pragma comment(lib, "wmcodecdspuuid.lib")
#pragma comment(lib, "amstrmid.lib")
#pragma comment(lib, "msdmo.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
另外就是缺少相应的webrtc里面的目标文件,比如jsoncpp相关的目标文件,可以做相应拷贝
还需要添加宏 WEBRTC_WIN

这里目前只编译32位,因为64位存在ffmepg 符号问题,暂时待解决。。。

ffmpeg_internal.lib(pcm.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm
ffmpeg_internal.lib(h264_picture.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm
ffmpeg_internal.lib(autorename_libavcodec_utils.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm
ffmpeg_internal.lib(decode.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm
ffmpeg_internal.lib(h264dec.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm
ffmpeg_internal.lib(vp3.obj) : error LNK2001: unresolved external symbol avpriv_emms_asm

参考博客

https://blog.csdn.net/freeabc/article/details/109304937
https://www.jianshu.com/p/351b29e36243
https://www.dazhuanlan.com/2019/09/22/5d874337a4239/
https://blog.piasy.com/2017/09/03/Use-WebRTC-Static-Library/index.html
https://blog.csdn.net/danieljinbiao/article/details/109817514

你可能感兴趣的:(webrtc,c++,android,android,linux,webrtc,ndk,c++)