前言

跟着新冠疫情的影响,这两年音视频的需求呈爆发式增长。在音视频领域中,WebRTC能够说是一个绕不开宝库,包含了音视频收集、编解码、传输、烘托的全过程。本文首要记录下在Mac平台上编译WebRTC Mac和iOS版别的全过程。

设置署理

因为众所周知的原因,要下载WebRTC的源码是需求署理东西的。

export http_porxy="http://127.0.0.1:21087"
export https_porxy="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。