系统:macOS High Sierra 10.13.4
NDK:android-ndk-r11c
FFmpeg:ffmpeg-3.3.7
最近基于ffmpeg用c++写了一个流媒体播放器,支持播放本地/网络文件、rtmp/rtsp流,想将其移植到Android中,第一步要做的工作就是编译Android能用的ffmpeg,之前编译ffmpeg都是使用NDK直接编译,问题不大,而Android的NDK更新模式就如同Android的Framework更新一样,即新版系统中会不断调整(优化)文件位置,所以做过Android系统开发的朋友们应该时常遇到“No such file or directory”。废话不多说,开始。
1、为何不使用网友直接编译好的库?NO,因为我这个播放器是跨平台的,而ffmpeg中的AVMediaType与IOS中AVFoundation下的AVMediaType冲突了,为了兼容IOS,所以我将ffmpeg源码中的AVMediaType全局替换成了FFMAVMediaType然后再进行编译。
sed -i '' "s/AVMediaType/FFMAVMediaType/g" `grep -rl AVMediaType ./ffmpeg-*`
/*如果报错:sed: RE error: illegal byte sequence
缺少环境变量,在shell输入:
export LC_COLLATE='C'
export LC_CTYPE='C'*/
2、准备NDK和FFmpeg源码,也可以使用Android Studio的SDK Manager一键下载NDK,更加方便。
NDK下载地址:https://developer.android.google.cn/ndk/downloads/
FFmpeg下载地址:http://www.ffmpeg.org/download.html
3、下载完成后进入到ffmpeg根目录创建脚本build-android.sh
3.1 脚本一:一次性编译所有平台
#NDK位置
export NDK=/Users/luohan/Library/Android/my_ndk_bundle/android-ndk-r11c
#编译成功后的产出位置
PREFIX=android
NDK_HOST_PLATFORM=darwin-x86_64
#编译过程中的临时文件存放
export TMPDIR=/Users/luohan/ffmpegTemp
#各平台交叉编译链位置
ARM_LINUX_TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
ARM_LINUX_SYSROOT=$NDK/platforms/android-21/arch-arm
ARM_64_TOOLCHAIN=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64
ARM_64_SYSROOT=$NDK/platforms/android-21/arch-arm64
X86_TOOLCHAIN=$NDK/toolchains/x86-4.9/prebuilt/darwin-x86_64
X86_SYSROOT=$NDK/platforms/android-21/arch-x86
X86_64_TOOLCHAIN=$NDK/toolchains/x86_64-4.9/prebuilt/darwin-x86_64
X86_64_SYSROOT=$NDK/platforms/android-21/arch-x86_64
COMMON_OPTIONS="\
--prefix=$PREFIX/ \
--target-os=android \
--disable-static \
--enable-shared \
--enable-small \
--disable-programs \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--disable-symver \
--disable-asm \
--enable-cross-compile \
"
function build_android {
./configure \
--libdir=${PREFIX}/libs/armeabi-v7a \
--incdir=${PREFIX}/includes/armeabi-v7a \
--pkgconfigdir=${PREFIX}/pkgconfig/armeabi-v7a \
--arch=arm \
--cpu=armv7-a \
--cross-prefix="${ARM_LINUX_TOOLCHAIN}/bin/arm-linux-androideabi-" \
--sysroot="${ARM_LINUX_SYSROOT}/" \
--extra-cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -I $ARM_LINUX_SYSROOT/usr/include/" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
./configure \
--libdir=${PREFIX}/libs/arm64-v8a \
--incdir=${PREFIX}/includes/arm64-v8a \
--pkgconfigdir=${PREFIX}/pkgconfig/arm64-v8a \
--arch=aarch64 \
--cpu=armv8-a \
--cross-prefix="${ARM_64_TOOLCHAIN}/bin/aarch64-linux-android-" \
--sysroot="${ARM_64_SYSROOT}/" \
--extra-libs=-lgcc \
--extra-cflags="$ARM_64_SYSROOT/usr/include/" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
./configure \
--libdir=${PREFIX}/libs/x86 \
--incdir=${PREFIX}/includes/x86 \
--pkgconfigdir=${PREFIX}/pkgconfig/x86 \
--arch=x86 \
--cpu=i686 \
--cross-prefix="${X86_TOOLCHAIN}/bin/i686-linux-android-" \
--sysroot="${X86_SYSROOT}/" \
--extra-cflags="$X86_SYSROOT/usr/include/" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
./configure \
--libdir=${PREFIX}/libs/x86_64 \
--incdir=${PREFIX}/includes/x86_64 \
--pkgconfigdir=${PREFIX}/pkgconfig/x86_64 \
--arch=x86_64 \
--cpu=x86_64 \
--cross-prefix="${X86_64_TOOLCHAIN}/bin/x86_64-linux-android-" \
--sysroot="${X86_64_SYSROOT}/" \
--extra-cflags="$X86_64_SYSROOT/usr/include/" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
};
build_android
3.1 脚本二:只针对一种平台进行编译
#!/bin/sh
export NDK=/Users/luohan/Library/Android/my_ndk_bundle/android-ndk-r11c
SYSROOT=$NDK/platforms/android-21/arch-arm
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
PREFIX=android
ADDI_CFLAGS="-marm"
function build_one
{
./configure \
--prefix="${PREFIX}/" \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--disable-asm \
--cross-prefix="${TOOLCHAIN}/bin/arm-linux-androideabi-" \
--target-os=android \
--arch=arm \
--enable-cross-compile \
--sysroot="${SYSROOT}/" \
--extra-cflags="-mfpu=neon -mfloat-abi=softfp -I $NDK/platforms/android-21/arch-arm/usr/include/" \
--extra-ldflags="$ADDI_LDFLAGS"
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
说明下为什么这里我要使用旧版的NDK,因为我们在对ffmpeg进行交叉编译时需要引用到c库(--extra-cflags),此前,旧版NDK的c库目录位置在“NDK/platforms/android-21/arch-arm64/usr/include/”中,而新版NDK中所以Android版本下的usr文件夹中都移除了include文件夹,全局搜索了一下发现,它们被集合放在另一个目录“NDK/sysroot/usr/include/”,这算是一个优化吧,但是引入该目录后,编译仍然报找不到c文件,具体原因不明,最后我直接使用旧版NDK进行编译,平台版本选择android-21,因为android-21中全平台的c库都有,再次编译,成功。
编译过程中还遇到过其他问题,有时候命令行报错不清晰,不能说明问题,这时就需要打开config.log查看详细报错,能帮助我们迅速对症下药,以下我粗略地罗列了几个问题的解决方案:
/toolchains/aarch64-linux-android-4.9/prebuilt//bin/aarch64-linux-android-gcc is unable to create an executable file.
C compiler test failed.
/*1.gcc交叉编译文件位置不对(可通过查看config.log获知)。
2.没有制定临时编译文件存放目录,或目录没有提权。*/
WARNING: /Users/luohan/Library/Android/my_ndk_bundle/android-ndk-r16-beta1/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-pkg-config not found, library detection may fail.
/*警告,这种警告可直接无视*/
/bin/sh: ranlib/usr/local/lib/libavdevice.a: No such file or directory
make: *** [install-libavdevice-static] Error 127
/*未设置临时文件位置所致*/
CC libavcodec/aacdec.o
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s: Assembler messages:
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16330: Error: selected processor does not support ARM mode `ubfx r1,r2,#0,#4'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16331: Error: selected processor does not support ARM mode `ubfx r0,r2,#4,#4'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16491: Error: selected processor does not support ARM mode `ubfx r2,r1,#0,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16492: Error: selected processor does not support ARM mode `ubfx r7,r1,#2,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16494: Error: selected processor does not support ARM mode `ubfx r8,r1,#4,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16496: Error: selected processor does not support ARM mode `ubfx r10,r1,#6,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16501: Error: selected processor does not support ARM mode `rbit r1,r1'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16687: Error: selected processor does not support ARM mode `ubfx r2,r3,#0,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16688: Error: selected processor does not support ARM mode `ubfx r1,r3,#2,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16690: Error: selected processor does not support ARM mode `ubfx r0,r3,#4,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16692: Error: selected processor does not support ARM mode `ubfx lr,r3,#6,#2'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16842: Error: selected processor does not support ARM mode `ubfx r2,r3,#0,#4'
/var/folders/6c/zwgcjwzn7y3b5z8wzvd3b8h00000gn/T//ccjyLRuc.s:16843: Error: selected processor does not support ARM mode `ubfx r1,r3,#4,#4'
make: *** [libavcodec/aacdec.o] Error 1
/*.configure中加入–-disable-asm*/