前言
本文的前提條件:
- 你需要編譯WebRTC Android版本源碼
- 你只有macOS伞访,沒有Linux環(huán)境
安裝Linux虛擬機(jī)
安裝vagrant和VitualBox
Vagrant是一款用于構(gòu)建及配置虛擬開發(fā)環(huán)境的軟件蔓榄,基于Ruby, 主要以命令行的方式運(yùn)行养筒。 主要配合使用Oracle的開源Virtual Box 虛擬化系統(tǒng)讲坎。
分別在官網(wǎng)下載并安裝這兩個軟件吴藻。
注意:安裝VirtualBox時若遇到:
The Installation Failed
需要在設(shè)置中允許安裝Oracle的軟件
詳細(xì)請參考:Fixing ‘The Installation Failed’ VirtualBox Error on Mac High Sierra
創(chuàng)建Linux虛擬機(jī)(Ubuntu16.04)
mkdir webrtc-builder
cd webrtc-builder
vagrant init ubuntu/xenial64
編輯配置文件(重要)
這一步需要將虛擬機(jī)的配置設(shè)定好侧啼,否則使用默認(rèn)配置后面編譯會遇到諸多問題芜飘。
首先我們在webrtc-builder目錄下打開配置文件Vagrantfile
vi Vagrantfile
這是我已經(jīng)配置好的配置文件(省略了注釋恍风,方便閱讀)
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.disksize.size = '45GB'
config.vm.provider "virtualbox" do |v|
v.memory = 4096
v.cpus = 2
end
config.vm.network "private_network", ip: "192.168.50.4"
config.vm.synced_folder "/Volumes/fss/webrtc_linux", "/home/vagrant/webrtc-android-linux", type: :nfs
end
以下是配置文件中需要注意的幾點(diǎn):
-
config.disksize.size
磁盤容量蹦狂,默認(rèn)是10GB,由于WebRTC Android的源碼有將近20G左右朋贬,后續(xù)編譯還會生成一些文件凯楔,建議將size設(shè)置為40G以上。
-
v.memory
內(nèi)存大小锦募,默認(rèn)是1024MB摆屯。建議將其設(shè)置為4096MB,否則編譯時可能會遇到ninja: build stopped: subcommand failed.問題(內(nèi)存不足)
-
config.vm.synced_folder
共享目錄糠亩,為了方便之后的訪問虐骑,這里需要將虛擬機(jī)中的目錄共享出來。
"/Volumes/fss/webrtc_linux":這里是mac上的地址(需要新建卷宗赎线,稍后我會講到)
"/home/vagrant/webrtc-android-linux":這是虛擬機(jī)中對應(yīng)的目錄
注意一:type為nfs需要設(shè)置config.vm.network "private_network", ip: "192.168.50.4"廷没,這里根據(jù)我的這個配置設(shè)定即可。
注意二:默認(rèn)macOS中的APFS文件系統(tǒng)是大小寫不敏感的垂寥,而WebRTC源碼中使用了大小寫來進(jìn)行區(qū)分颠黎,這樣的話后面在編譯時會出現(xiàn)一些問題,導(dǎo)致找不到路徑滞项。解決辦法是在磁盤工具(Disk Utinity)中新建一個卷宗(例如fss)狭归,選擇文件系統(tǒng)為APFS(Case-sensitive)(中文是大小寫敏感)。之后將掛載的目錄設(shè)置為此卷宗上的目錄即可文判。
配置文件編輯好后过椎,即可開機(jī)。
附上vagrant常用命令:
vagrant up #開機(jī)
vagrant halt #關(guān)機(jī)
vagrant reload #類似重啟(重新加載配置文件)
vagrant ssh #ssh登錄到虛擬機(jī)
在編譯之前戏仓,我們先ssh登錄到虛擬機(jī):
vagrant ssh
接下來的操作在虛擬機(jī)上進(jìn)行潭流。
WebRTC Android 源碼編譯
準(zhǔn)備工作
官方教程: Reference Link
Depot Tools
depot_tools是Chromium官方提供的工具包,主要用于Chromium開發(fā)柜去。
clone depot_tools repository:
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
將剛才clone下來的目錄添加到運(yùn)行環(huán)境中〔鹜穑可以在終端進(jìn)行單次export嗓奢,也可以通過配置文件設(shè)置全局生效(具體請Google)
這里的/path/to/depot_tools換成自己的對應(yīng)路徑
export PATH=/path/to/depot_tools:$PATH
下載源碼
mkdir webrtc-android-linux
cd webrtc-android-linux
fetch --nohooks webrtc_android
gclient sync
新建一個工作目錄
fetch Android版本的源碼
fetch完成后,再和遠(yuǎn)端代碼庫進(jìn)行同步浑厚,即完成了源碼的下載股耽。
注意:此過程需要保持網(wǎng)絡(luò)暢通(可以訪問google)根盒。下載下來的源碼大概有將近20G(一半是WebRTC源碼,另外一部分是Android的NDK和SDK等特殊部分)
安裝依賴
./src/build/install-build-deps.sh
./src/build/install-build-deps-android.sh
編譯
使用GN生成工程
cd src
gn gen out/Debug --args='target_os="android" target_cpu="arm"'
參數(shù)可以修改物蝙,詳細(xì)請參考官網(wǎng)炎滞。
編譯
ninja -C out/Debug
全量編譯,用時較長诬乞,與機(jī)器性能有關(guān)系册赛。
使用和測試
編譯完成后,在out/Debug下生成編譯好的工程震嫉。
可以在out/Debug/apks/目錄下找到AppRTCMobile.apk, 安裝到手機(jī)即可測試森瘪。
附問題總結(jié)
我遇到的兩個比較大的問題都發(fā)生在編譯階段。
-
編譯到7000/11838左右時卡住票堵,killed扼睬,報錯:ninja: build stopped: subcommand failed Android
vagrant@ubuntu-xenial:~/webrtc-checkout-android/src$ ninja -C out/Debug ninja: Entering directory `out/Debug' [6902/11838] CXX obj/pc/peerconnection/peer_connection.o FAILED: obj/pc/peerconnection/peer_connection.o ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/pc/peerconnection/peer_connection.o.d -D_GNU_SOURCE -DANDROID -DHAVE_SYS_UIO_H -DANDROID_NDK_VERSION_ROLL=r20_1 -DCR_CLANG_REVISION=\"llvmorg-12-init-1771-g1bd7046e-3\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -D_LIBCPP_DEBUG=0 -DCR_LIBCXX_REVISION=375504 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DWEBRTC_ENABLE_PROTOBUF=1 -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE -DRTC_ENABLE_VP9 -DHAVE_SCTP -DWEBRTC_ARCH_ARM -DWEBRTC_ARCH_ARM_V7 -DWEBRTC_HAS_NEON -DWEBRTC_LIBRARY_IMPL -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1 -DWEBRTC_POSIX -DWEBRTC_LINUX -DWEBRTC_ANDROID -DABSL_ALLOCATOR_NOTHROW=1 -DHAVE_SCTP -I../.. -Igen -I../../third_party/abseil-cpp -I../../third_party/libyuv/include -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers -ffunction-sections -fno-short-enums --target=arm-linux-androideabi16 -DHAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC=1 -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Xclang -fdebug-compilation-dir -Xclang . -no-canonical-prefixes -mfpu=neon -mthumb -Wall -Werror -Wextra -Wimplicit-fallthrough -Wunreachable-code -Wthread-safety -Wextra-semi -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-psabi -Wno-ignored-pragma-optimize -Wno-implicit-int-float-conversion -Wno-final-dtor-non-final-class -Wno-builtin-assume-aligned-alignment -Wno-deprecated-copy -Wno-non-c-typedef-for-linkage -Wmax-tokens -Oz -fno-ident -fdata-sections -ffunction-sections -fomit-frame-pointer -gdwarf-3 -g1 -fdebug-info-for-profiling -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -Wglobal-constructors -Wc++11-narrowing -Wimplicit-fallthrough -Wthread-safety -Winconsistent-missing-override -Wundef -Wunused-lambda-capture -Wno-undefined-bool-conversion -Wno-tautological-undefined-compare -std=c++14 -fno-trigraphs -Wno-trigraphs -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fvisibility-inlines-hidden -Wnon-virtual-dtor -Woverloaded-virtual -c ../../pc/peer_connection.cc -o obj/pc/peerconnection/peer_connection.o Killed [6904/11838] CXX obj/pc/peerconnection/rtc_stats_collector.o ninja: build stopped: subcommand failed.
查詢了一下,許多人在編譯Android源碼時遇到過這個問題悴势,說是因?yàn)橄到y(tǒng)同時打開的文件數(shù)有限窗宇。我根據(jù)這個鏈接修改了一下,將文件數(shù)擴(kuò)大到20480后依然出現(xiàn)問題特纤。
隨后我查看了虛擬機(jī)的配置军俊,發(fā)現(xiàn)它的運(yùn)存也很小,這個原因也可能導(dǎo)致killed叫潦。因此將運(yùn)存調(diào)整到4096MB蝇完。
-
Invalid argument問題
vagrant@ubuntu-xenial:~/webrtc-android-linux/src$ ninja -C out/Debug ninja: Entering directory `out/Debug' [6519/11838] ACTION //third_party/android_deps:b...der(//build/toolchain/android:android_clang_arm) FAILED: obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar python ../../build/android/gyp/ijar.py clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar Traceback (most recent call last): File "../../build/android/gyp/ijar.py", line 34, in <module> main() File "../../build/android/gyp/ijar.py", line 30, in main build_utils.CheckOutput([ijar_bin, in_jar, f.name]) File "/home/vagrant/webrtc-android-linux/src/build/android/gyp/util/build_utils.py", line 254, in CheckOutput raise CalledProcessError(cwd, args, stdout + stderr) util.build_utils.CalledProcessError: Command failed: ( cd /home/vagrant/webrtc-android-linux/src/out/Debug; clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar /home/vagrant/webrtc-android-linux/src/out/Debug/obj/third_party/android_deps/tmp15NzRwbackport_util_concurrent_backport_util_concurrent.ijar.jar ) mmap(): Invalid argument [6521/11838] CXX clang_x64/obj/tools/android/md5sum/md5sum_bin/md5sum.o ninja: build stopped: subcommand failed.
這個問題是設(shè)置了共享目錄之后出現(xiàn)的(當(dāng)時映射的mac本地目錄是APFS大小寫不敏感)。
查看這個報錯矗蕊,應(yīng)該是在out/Debug中查找不到路徑短蜕,那就是路徑有誤。
后續(xù)將掛載目錄改為APFS大小寫敏感即可傻咖。