Compile WebRTC under Mac (Mac and iOS version)

created at 12-25-2021 views: 73

Preface

With the impact of the Covid-19, the demand for audio and video has exploded in the past two years. In the field of audio and video, WebRTC can be said to be an inevitable treasure house, including the entire process of audio and video acquisition, encoding and decoding, transmission, and rendering. This article mainly records the entire process of compiling WebRTC Mac and iOS versions on the Mac platform.

Install the depot_tools tool

git clone to get depot_tools

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

Configure the path of depot_tools to environment variables

export PATH=$PWD/depot_tools:$PATH

Download webrtc source code

mkdir webrtc
cd webrtc
fetch --nohooks webrtc_ios
gclient sync

The latest source code is downloaded by default. If you want to switch to the specified branch, you can use the following command:

# View available version branches
git branch -r
# Switch to m79 branch
git checkout branch-heads/m79
gclient sync
# Or forcibly switch to the specified commit (b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b is the last commit id submitted by m79)
gclient sync -r b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b --force

Compile

Mac version

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

After the compilation is successful,.

iOS version

# Compile version without certificate
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

# Get the certificate name
security find-identity -v -p codesigning

# Compile the certificate version
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="The string of numbers obtained by the above command" proprietary_codecs=false rtc_use_h264= false rtc_include_tests=false' --ide=xcode
ninja -C out/ios-release-sign

After successful compilation, the file all.xcworkspace will be generated under src\out\xxxx\. Open it to build and debug webrtc projects. Among them, APPRTCMobile is a sample demo provided by Google, which can be packaged and run on a real machine. The WebRTC.framework library file is also generated in src\out\xxxx\, and its audio and video capabilities can be used by quoting this library file in an external project.

The WebRTC.framework library file can also be generated separately by ninja command or python script.

# Generate WebRTC.framework library file separately by ninja command
ninja -C out/ios-release-sign framework_objc

# Generate WebRTC.framework library file through build_ios_libs.py script
python tools_webrtc/ios/build_ios_libs.py --bitcode

The library files generated by the python script (the earlier version of webrtc, the latest version of xcframework) are in the src/out_ios_lib directory. There will be 5 folders in this directory, among which WebRTC.framework is a dynamic library that supports the four architectures of `arm`, arm64, x64, and x86. In addition, arm_libs, arm64_libs, x64_libs, and x86_libs folders contain dynamic libraries that support these four architectures separately. You can use the lipo -info or file command to view the supported architectures.

Apple's new library type of xcframework later supports its unified multi-platform and multi-architecture. The earlier version of webrtc build_ios_libs.py does not support the generation of xcframework. For this reason, the following script can be used to convert `framework` to 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"

other

compilation errors

May encounter compilation errors

Fatal error:'libavutil/avconfig.h' file not found

solution

Create the avconfig.h file in src/third_party/ffmpeg/libavutil/ with the following content:

/* 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 */

compiling error of certificate version

Error encountered in compiling the version with certificate

Error: no mobile provisioning profile found for "com.google.AppRTCMobile".

solution

Open the all.xcworkspace project, modify the Signing in the project to be your own, and recompile the project.

another problem

There is an unresolved issue in the version I compiled

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.

Only set rtc_use_h264=false first,  not use h264.

created at:12-25-2021
edited at: 12-25-2021: