librtmp、rtmpdump、xh264和ffmpeg的编译
2024-08-07 本文已影响0人
orgcheng
NDK 提供对 ndk-build 和 CMake 的官方支持
一、Mac平台使用ndk-build的方式,编译针对Android平台的librtmp库
代码的下载地址:https://github.com/yixia/librtmp
仓库的README中描述的很清楚,它只是在rtmpdump中添加编译脚本
git://git.ffmpeg.org/rtmpdump mirror, only add build librtmp script for Android & iOS
这里的编译参考:https://developer.android.google.cn/ndk/guides/other_build_systems?hl=zh-cn
librtmp属于非 Autoconf make 项目,编译脚本如下:
#!/bin/bash
export NDK=/Users/chengzhen/Library/Android/sdk/ndk/25.1.8937393
# Only choose one of these, depending on your build machine...
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
#export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...
export TARGET=aarch64-linux-android
#export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
#export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=21
# Set output path
PREFIX=$(pwd)/$TARGET$API
# Build.
make \
CC="$TOOLCHAIN/bin/clang --target=$TARGET$API" \
AR=$TOOLCHAIN/bin/llvm-ar \
RANLIB=$TOOLCHAIN/bin/llvm-ranlib
# Template params
make install SYS=android prefix=$PREFIX CRYPTO= SHARED= XDEF=-DNO_SSL
二、使用CMake方式,在android studio中集成rtmpdum源码
rtmpdum类似于okhttp,它是rtmp协议的一个实现工具,下载地址:https://github.com/aajanki/rtmpdump?tab=readme-ov-file
下载完成后,把C代码拷贝到工程中,修改CMakeLists.txt文件即可
截屏2024-08-08 21.41.36.png
三、Mac平台编译针对Android平台的xh264库
上面的CMakeLists.txt文件已经集成了xh264,这里讲解下编译
下载地址:https://www.videolan.org/developers/x264.html
xh264属于Autoconf 项目,编译脚本如下:
#!/bin/bash
export NDK=/Users/chengzhen/Library/Android/sdk/ndk/25.1.8937393
TOOLCHAIN_ROOT_DIR=darwin-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$TOOLCHAIN_ROOT_DIR/
SYSROOT=$TOOLCHAIN/sysroot
API=21
export CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
export CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
LLVM_TOOLCHAIN=$TOOLCHAIN/bin
PKG_CONFIG=/usr/local/bin/pkg-config
export NM=$LLVM_TOOLCHAIN/llvm-nm
export STRIP=$LLVM_TOOLCHAIN/llvm-strip
CPU=armv8-a
PREFIX=$(pwd)/android/$CPU
export CFLAGS="-march=$CPU -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fno-limit-debug-info -fPIC"
function build_android {
./configure \
--enable-pic \
--enable-strip \
--enable-static \
--disable-asm \
--disable-cli \
--prefix=$PREFIX \
--cross-prefix=$CROSS_PREFIX \
--host=aarch64-linux-android \
--sysroot=$SYSROOT \
--extra-cflags="$CFLAGS"
make clean
make
make install
}
build_android
四、ffmpeg的编译
这里只是贴一下mac平台编译成功的脚本,本地验证都是通过的
#!/bin/bash
NDK=/Users/chengzhen/Library/Android/sdk/ndk/25.1.8937393
TOOLCHAIN_ROOT_DIR=darwin-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$TOOLCHAIN_ROOT_DIR/
API=21
#要编译的ffmpeg内容方法
function build_android {
echo "Compiling FFmpeg for $CPU"
./configure \
--prefix=$PREFIX \
--disable-hwaccels \
--disable-gpl \
--disable-postproc \
--disable-programs \
--disable-mediacodec \
--disable-decoder=h264_mediacodec \
--disable-static \
--disable-vulkan \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--disable-x86asm \
--disable-filters \
--enable-cross-compile \
--enable-jni \
--enable-shared \
--cross-prefix=$CROSS_PREFIX \
--nm=$NM \
--strip=$STRIP \
--pkg-config=$PKG_CONFIG \
--target-os=android \
--arch=$ARCH \
--cpu=$CPU \
--cc=$CC \
--cxx=$CXX \
--sysroot=$SYSROOT \
--extra-cflags="$CFLAGS"
make clean
make
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}
#接下来是根据需要来决定
#armv8-a
ARCH=arm64
CPU=armv8-a
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
LLVM_TOOLCHAIN=$TOOLCHAIN/bin
#PKG_CONFIG_DIR=/usr/local
PKG_CONFIG=/usr/local/bin/pkg-config
NM=$LLVM_TOOLCHAIN/llvm-nm
STRIP=$LLVM_TOOLCHAIN/llvm-strip
SYSROOT=$TOOLCHAIN/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
CFLAGS="-march=$CPU -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fno-limit-debug-info -fPIC"
build_android
#x86_64
function fun {
ARCH=x86_64
CPU=x86-64
CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
LLVM_TOOLCHAIN=$TOOLCHAIN/bin
PKG_CONFIG_DIR=/usr/local/Cellar/pkg-config/0.29.2_3
PKG_CONFIG=$PKG_CONFIG_DIR/bin/pkg-config
NM=$LLVM_TOOLCHAIN/llvm-nm
STRIP=$LLVM_TOOLCHAIN/llvm-strip
SYSROOT=$TOOLCHAIN/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android
}
五、ffmpeg+rtmp的编译
另外如果想要ffmpeg使用我们自己的rtmp库,需要修改configure文件,注释掉原来ffmpeg自己的rtmp
#enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket
上面的编译脚本做出相应的修改,添自己的rtmp的头文件和库路径,本地修改后的脚本如下:
#!/bin/bash
NDK=/Users/chengzhen/Library/Android/sdk/ndk/25.1.8937393
TOOLCHAIN_ROOT_DIR=darwin-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$TOOLCHAIN_ROOT_DIR/
API=21
RTMP=/Users/chengzhen/Documents/ffmpeg-7.0.1/librtmp_path
#要编译的ffmpeg内容方法
function build_android {
echo "Compiling FFmpeg for $CPU"
./configure \
--prefix=$PREFIX \
--disable-hwaccels \
--disable-gpl \
--disable-postproc \
--disable-programs \
--disable-mediacodec \
--disable-decoder=h264_mediacodec \
--disable-static \
--disable-vulkan \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--disable-x86asm \
--disable-filters \
--enable-cross-compile \
--enable-librtmp \
--enable-jni \
--enable-static \
--cross-prefix=$CROSS_PREFIX \
--nm=$NM \
--ar=$AR \
--ranlib=$RANLIB \
--strip=$STRIP \
--pkg-config=$PKG_CONFIG \
--target-os=android \
--arch=$ARCH \
--cpu=$CPU \
--cc=$CC \
--cxx=$CXX \
--sysroot=$SYSROOT \
--extra-cflags="$CFLAGS" \
--extra-ldflags="-L$RTMP/lib" \
--extra-libs="-lrtmp"
make clean
make
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}
#接下来是根据需要来决定
#armv8-a
ARCH=arm64
CPU=armv8-a
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
LLVM_TOOLCHAIN=$TOOLCHAIN/bin
#PKG_CONFIG_DIR=/usr/local
PKG_CONFIG=/usr/local/bin/pkg-config
NM=$LLVM_TOOLCHAIN/llvm-nm
RANLIB=$LLVM_TOOLCHAIN/llvm-ranlib
AR=$LLVM_TOOLCHAIN/llvm-ar
STRIP=$LLVM_TOOLCHAIN/llvm-strip
SYSROOT=$TOOLCHAIN/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
CFLAGS="-I$RTMP/include -march=$CPU -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fno-limit-debug-info -fPIC"
build_android