Mac下编译WebRTC(Mac和iOS版本)

6,684 阅读3分钟

前言

随着新冠疫情的影响,这两年音视频的需求呈爆发式增长。在音视频领域中,WebRTC可以说是一个绕不开宝库,包括了音视频采集、编解码、传输、渲染的全过程。本文主要记录下在Mac平台上编译WebRTC Mac和iOS版本的全过程。

设置代理

因为众所周知的原因,要下载WebRTC的源码是需要代理工具的。

export http_proxy="http://127.0.0.1:21087"
export https_proxy="http://127.0.0.1:21087"

安装工具depot_tools

git clone获取depot_tools

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

将depot_tools的路径配置到环境变量中

export PATH=$PWD/depot_tools:$PATH

下载webrtc源码

mkdir webrtc
cd webrtc
fetch --nohooks webrtc_ios
gclient sync

默认下载的是最新的源码,如果想要切换到指定分支,可以使用以下命令:

# 查看可用版本分支
git branch -r
# 切换到m79分支
git checkout branch-heads/m79
gclient sync
# 或者强制切换到指定commit(b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b是m79最后一次提交的commit id)
gclient sync -r b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b --force

可以在从这里获取webrtc所有release版本的信息

编译

Mac版本:

gn gen out/mac-release --args='target_os="mac" target_cpu="x64" is_debug=false use_rtti=true is_component_build=false rtc_use_h264=false rtc_include_tests=false' --ide=xcode
ninja -C out/mac-release

编译成功后会在src\out\xxxx\下生成all.xcworkspace文件。打开就可以构建、调试webrtc的项目。其中APPRTCMobile是谷歌提供的示例demo,可以在Mac下直接编译运行。

iOS版本:

# 编译不带证书版本
gn gen out/ios-release --args='target_os="ios" target_cpu="arm64" is_debug=false use_rtti=true is_component_build=false ios_enable_code_signing=false proprietary_codecs=false rtc_use_h264=false rtc_include_tests=false' --ide=xcode
ninja -C out/ios-release

# 获取证书名
security find-identity -v -p codesigning

# 编译带证书版本
gn gen out/ios-release-sign --args='target_os="ios" target_cpu="arm64" is_debug=false use_rtti=true is_component_build=false ios_code_signing_identity="上面命令获取到的那串数字" proprietary_codecs=false rtc_use_h264=false rtc_include_tests=false' --ide=xcode
ninja -C out/ios-release-sign

编译成功后,会在src\out\xxxx\下生成all.xcworkspace文件。打开就可以构建、调试webrtc的项目。其中APPRTCMobile是谷歌提供的示例demo,可打包在真机上运行。在src\out\xxxx\也生成了WebRTC.framework库文件,在外部项目中引用该库文件就可以使用其音视频能力了。

WebRTC.framework库文件也可以通过ninja命令或者python脚本单独生成。

# 通过ninja命令单独生成WebRTC.framework库文件
ninja -C out/ios-release-sign framework_objc

# 通过build_ios_libs.py脚本生成WebRTC.framework库文件
python tools_webrtc/ios/build_ios_libs.py --bitcode

通过python脚本(较早版本的webrtc,最新版本的生成xcframework)生成的库文件在 src/out_ios_lib目录下。该目录下会有5个文件夹,其中WebRTC.framework是支持arm、arm64、x64、x86这四种架构的动态库。另外,arm_libs、arm64_libs、x64_libs、x86_libs文件夹里分别是单独支持这四种架构的动态库。可以通过lipo -info或者file命令来查看其支持的架构。

苹果后来新出的xcframework的库类型,为支持其大一统的多平台多架构。webrtc较早版本的build_ios_libs.py是不支持生成xcframework,为此可以通过以下脚本将framework转换为为xcframework。

#!/bin/bash

mkdir iphoneos iphonesimulator

cp -R WebRTC.framework  iphoneos
cp -R WebRTC.framework  iphonesimulator

lipo -remove i386 -remove x86_64 iphoneos/WebRTC.framework/WebRTC -o iphoneos/WebRTC.framework/WebRTC
lipo -remove armv7 -remove arm64 iphonesimulator/WebRTC.framework/WebRTC -o iphonesimulator/WebRTC.framework/WebRTC

xcodebuild -create-xcframework \
-framework iphoneos/WebRTC.framework \
-framework iphonesimulator/WebRTC.framework \
-output "WebRTC.xcframework"

其他

  • 可能碰到编译错误——fatal error: 'libavutil/avconfig.h' file not found。解决方案:在src/third_party/ffmpeg/libavutil/创建avconfig.h文件,内容如下:
/* Generated by ffconf */
\#ifndef AVUTIL_AVCONFIG_H
\#define AVUTIL_AVCONFIG_H
\#define AV_HAVE_BIGENDIAN 0
\#define AV_HAVE_FAST_UNALIGNED 0
\#endif /* AVUTIL_AVCONFIG_H */
  • 编译带证书版本中碰到错误——Error: no mobile provisioning profile found for "com.google.AppRTCMobile"。解决方案:打开all.xcworkspace工程,修改工程中的Signing为你自己的,重新编译工程即可。

  • 我编译的版本存在一个未解决问题—— fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file list file: obj/third_party/ffmpeg/libffmpeg_internal.a.rsp is empty。只能先设置rtc_use_h264=false,不使用h264。