当前位置:   article > 正文

编译适配纯鸿蒙系统的ijkplayer中的ffmpeg库_ijkplayer 鸿蒙系统

ijkplayer 鸿蒙系统

    目前bilibili官方的ijkplayer播放器,是只适配Android和IOS系统的。而华为接下来即将发布纯harmony系统,是否有基于harmony系统的ijkplayer可以使用呢?

    鸿蒙版ijkplayer播放器是哪个,如何使用,这个问题,大家可以参考我另一个技术号的文章先行了解一下:鸿蒙版ijkplayer播放器示例工程适配运行

    不过上述提到的这个被鸿蒙化的ijkplayer播放器,里面的ffmpeg库是以已经编译好的ffmpeg的动态库集成进去的。对于需要修改ffmpeg代码的人来说,还需要知道如何编译适配harmony系统的ffmpeg库,从而来替代这个鸿蒙版ijkplayer中的ffmpeg库。

    因此本文想要讲解的重点是,如何从之前的安卓版的ijkplayer仓库中,将ffmpeg编译出适配harmony系统的动态库。

一、基于openHarmony环境编译开源库

    在本文主题正式开始之前,大家也可以通过我另一个技术号的这篇文章,需要先行了解一下如何基于openHarmony环境编译适配harmony系统的动态库:

    基于openHarmony环境编译开源库

二、编译前准备

    编译之前,需要搭建的编译环境,需要下载的工具等。可以先大致参考之前安卓版本的编译环境,可从这篇文章中先行了解一下编译环境即可:ijkplayer编译流程详解

    PS:其中与安卓相关的配置可以不用做。因为对应的是需要下载鸿蒙的编译工具链和文件系统等。

三、修改编译脚本

    我们从上述二中的安卓版的ijkplayer仓库中,对主要编译脚本进行修改,来进行适配。

    主要需要修改的就是android\contrib\tools下的三个脚本:do-compile-openssl.sh、do-compile-ffmpeg.sh和do-detect-env.sh。

    在修改这3个脚本之前,根据“一”中文章所述,先提前下载好鸿蒙的交叉编译工具和文件系统,并在这3个脚本的编译前置脚本中(compile-openssl.sh和compile-ffmpeg.sh),将交叉工具链和文件系统的路径export出来,或者在全局环境变量/etc/profile配置文件export出来路径,如下:

  1. export OHOS_SYSROOT=/home/weekend/openharmony/ohos-sdk/linux/native/sysroot
  2. export OHOS_LLVM=/home/weekend/openharmony/ohos-sdk/linux/native/llvm

    OHOS_SYSROOT和OHOS_LLVM变量在后续的脚本中都是需要的。

1、修改do-detect-env.sh

    do-detect-env.sh脚本主要用来检测编译环境的,在安卓版本中,会检查安卓sdk版本,ndk之类的。在鸿蒙版本中这些就都不需要了。我将这些都去掉。

    然后增加鸿蒙的编译工具链和文件系统的检查,如下:

  1. if [ -z "$OHOS_LLVM" ]; then
  2. echo "You must define OHOS_LLVM before starting."
  3. echo "sysroot and llvm is necessary.\n"
  4. exit 1
  5. fi
  6. if [ -z "$OHOS_SYSROOT" ]; then
  7. echo "You must define OHOS_SYSROOT before starting."
  8. echo "sysroot and llvm is necessary.\n"
  9. exit 1
  10. fi

    实际上,我在3个脚本的开头处,都会对这个进行检查。

    附上完整的修改后的 do-detect-env.sh:

  1. #! /usr/bin/env bash
  2. #
  3. #--------------------
  4. set -e
  5. UNAME_S=$(uname -s)
  6. UNAME_SM=$(uname -sm)
  7. echo "build on $UNAME_SM"
  8. echo "OHOS_SYSROOT=$OHOS_SYSROOT"
  9. echo "OHOS_LLVM=$OHOS_LLVM"
  10. if [ -z "$OHOS_LLVM" ]; then
  11. echo "You must define OHOS_LLVM before starting."
  12. echo "sysroot and llvm is necessary.\n"
  13. exit 1
  14. fi
  15. if [ -z "$OHOS_SYSROOT" ]; then
  16. echo "You must define OHOS_SYSROOT before starting."
  17. echo "sysroot and llvm is necessary.\n"
  18. exit 1
  19. fi
  20. # try to detect NDK version
  21. export IJK_GCC_VER=4.9
  22. export IJK_GCC_64_VER=4.9
  23. export IJK_MAKE_TOOLCHAIN_FLAGS=
  24. export IJK_MAKE_FLAG=
  25. case "$UNAME_S" in
  26. Darwin)
  27. export IJK_MAKE_FLAG=-j`sysctl -n machdep.cpu.thread_count`
  28. ;;
  29. CYGWIN_NT-*)
  30. IJK_WIN_TEMP="$(cygpath -am /tmp)"
  31. export TEMPDIR=$IJK_WIN_TEMP/
  32. echo "Cygwin temp prefix=$IJK_WIN_TEMP/"
  33. ;;
  34. esac

2、修改do-compile-openssl.sh

    do-compile-openssl.sh根据“二”中描述,主要是用于编译openssl的。根据上述“一”中的文章所述,主要是需要修改交叉编译工具链,和文件系统。

    将安卓NDK和交叉编译工具链相关的去掉,FF_CROSS_PREFIX=aarch64-linux-android这个是安卓的交叉工具链头,用的gcc。修改为export CC 后面跟着鸿蒙的交叉工具链是llvm目录下的clang,然后target=aarch64-linux-ohos,指明是64位的。

  1. CLANG=$OHOS_LLVM/bin/clang
  2. export CC="$CLANG $OHOS_CFLAGS"
  3. export CFLAGS="$OHOS_CFLAGS --sysroot=${OHOS_SYSROOT}"

    安卓的脚本中有FF_TOOLCHAIN_TOUCH,主要是安卓的ndk中提供的用于交叉编译的一些工具包。在鸿蒙的编译中,这一块就不需要了,直接去掉。就是下面这段是不需要了的:

  1. FF_MAKE_TOOLCHAIN_FLAGS=$IJK_MAKE_TOOLCHAIN_FLAGS
  2. FF_MAKE_FLAGS=$IJK_MAKE_FLAG
  3. FF_MAKE_TOOLCHAIN_FLAGS="$FF_MAKE_TOOLCHAIN_FLAGS --install-dir=$FF_TOOLCHAIN_PATH"
  4. FF_TOOLCHAIN_TOUCH="$FF_TOOLCHAIN_PATH/touch"
  5. if [ ! -f "$FF_TOOLCHAIN_TOUCH" ]; then
  6. $ANDROID_NDK/build/tools/make-standalone-toolchain.sh \
  7. $FF_MAKE_TOOLCHAIN_FLAGS \
  8. --platform=$FF_ANDROID_PLATFORM \
  9. --toolchain=$FF_TOOLCHAIN_NAME
  10. touch $FF_TOOLCHAIN_TOUCH;
  11. fi

    只需要在后面编译的时候,指明CFLAGS=--sysroot=xxx,跟着鸿蒙的文件系统目录。

export CFLAGS="$OHOS_CFLAGS --sysroot=${OHOS_SYSROOT}"

    为了确保编译,可以将这些关键的编译相关的都export出来,如下:

  1. export CC="$CLANG $OHOS_CFLAGS"
  2. export CXX="$CLANGXX $OHOS_CFLAGS"
  3. export AR=${OHOS_AR}
  4. export AS=${OHOS_AS}
  5. export NM=${OHOS_NM}
  6. export STRIP=${OHOS_STRIP}
  7. export RANLIB=${OHOS_RANLIB}
  8. export LD=${OHOS_LD}
  9. export LDFLAGS="--rtlib=compiler-rt -fuse-ld=lld"
  10. export CFLAGS="$OHOS_CFLAGS --sysroot=${OHOS_SYSROOT}"

    附上完整的do-compile-openssl.sh供参考,我暂时只保留是arm的32位,64位和x86的64位这三个常用的cpu架构编译。具体如下:

  1. #! /usr/bin/env bash
  2. #--------------------
  3. set -e
  4. if [ -z "$OHOS_LLVM" ]; then
  5. echo "You must define OHOS_LLVM before starting."
  6. echo "sysroot and llvm is necessary.\n"
  7. exit 1
  8. fi
  9. if [ -z "$OHOS_SYSROOT" ]; then
  10. echo "You must define OHOS_SYSROOT before starting."
  11. echo "sysroot and llvm is necessary.\n"
  12. exit 1
  13. fi
  14. #--------------------
  15. # common defines
  16. FF_ARCH=$1
  17. if [ -z "$FF_ARCH" ]; then
  18. echo "You must specific an architecture 'arm, armv7a, x86, ...'.\n"
  19. exit 1
  20. fi
  21. FF_BUILD_ROOT=`pwd`
  22. FF_BUILD_NAME=
  23. FF_SOURCE=
  24. FF_CROSS_PREFIX=
  25. FF_CFG_FLAGS=
  26. FF_PLATFORM_CFG_FLAGS=
  27. FF_EXTRA_CFLAGS=
  28. FF_EXTRA_LDFLAGS=
  29. # compile
  30. CLANG=$OHOS_LLVM/bin/clang
  31. CLANGXX=$OHOS_LLVM/bin/clang++
  32. OHOS_AR=$OHOS_LLVM/bin/llvm-ar
  33. OHOS_AS=$OHOS_LLVM/bin/llvm-as
  34. OHOS_NM=$OHOS_LLVM/bin/llvm-nm
  35. OHOS_RANLIB=$OHOS_LLVM/bin/llvm-ranlib
  36. OHOS_STRIP=$OHOS_LLVM/bin/llvm-strip
  37. OBJDUMP=$OHOS_LLVM/bin/llvm-objdump
  38. OHOS_LD=$OHOS_LLVM/bin/ld.lld
  39. OHOS_LIBS="-L${OHOS_LLVM}/lib"
  40. OHOS_INCS="-I${OHOS_LLVM}/include"
  41. OHOS_CFLAGS=
  42. #--------------------
  43. echo ""
  44. echo "--------------------"
  45. echo "[*] do detect env"
  46. echo "--------------------"
  47. . ./tools/do-detect-env.sh
  48. FF_MAKE_FLAGS=$IJK_MAKE_FLAG
  49. #----- armv7a begin -----
  50. if [ "$FF_ARCH" = "armv7a" ]; then
  51. FF_BUILD_NAME=openssl-armv7a
  52. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  53. FF_PLATFORM_CFG_FLAGS="linux-armv4"
  54. OHOS_CFLAGS="--target=arm-linux-ohos -march=armv7-a"
  55. elif [ "$FF_ARCH" = "arm64" ]; then
  56. FF_BUILD_NAME=openssl-arm64
  57. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  58. FF_PLATFORM_CFG_FLAGS="linux-aarch64"
  59. OHOS_CFLAGS="--target=aarch64-linux-ohos"
  60. elif [ "$FF_ARCH" = "x86_64" ]; then
  61. FF_BUILD_NAME=openssl-x86_64
  62. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  63. FF_PLATFORM_CFG_FLAGS="linux-x86_64"
  64. OHOS_CFLAGS="--target=x86_64-linux-ohos"
  65. else
  66. echo "unknown architecture $FF_ARCH";
  67. exit 1
  68. fi
  69. FF_SYSROOT=$OHOS_SYSROOT
  70. FF_PREFIX=$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output
  71. mkdir -p $FF_PREFIX
  72. # mkdir -p $FF_SYSROOT
  73. #--------------------
  74. echo ""
  75. echo "--------------------"
  76. echo "[*] check openssl env"
  77. echo "--------------------"
  78. export CC="$CLANG $OHOS_CFLAGS"
  79. export CXX="$CLANGXX $OHOS_CFLAGS"
  80. export AR=${OHOS_AR}
  81. export AS=${OHOS_AS}
  82. export NM=${OHOS_NM}
  83. export STRIP=${OHOS_STRIP}
  84. export RANLIB=${OHOS_RANLIB}
  85. export LD=${OHOS_LD}
  86. export LDFLAGS="--rtlib=compiler-rt -fuse-ld=lld"
  87. export CFLAGS="$OHOS_CFLAGS --sysroot=${OHOS_SYSROOT}"
  88. export COMMON_FF_CFG_FLAGS=
  89. FF_CFG_FLAGS="$FF_CFG_FLAGS $COMMON_FF_CFG_FLAGS"
  90. #--------------------
  91. # Standard options:
  92. FF_CFG_FLAGS="$FF_CFG_FLAGS zlib-dynamic"
  93. FF_CFG_FLAGS="$FF_CFG_FLAGS no-asm"
  94. FF_CFG_FLAGS="$FF_CFG_FLAGS no-shared"
  95. FF_CFG_FLAGS="$FF_CFG_FLAGS -fPIC"
  96. FF_CFG_FLAGS="$FF_CFG_FLAGS --openssldir=$FF_PREFIX"
  97. FF_CFG_FLAGS="$FF_CFG_FLAGS $FF_PLATFORM_CFG_FLAGS"
  98. #--------------------
  99. echo ""
  100. echo "--------------------"
  101. echo "[*] configurate openssl"
  102. echo "--------------------"
  103. cd $FF_SOURCE
  104. #if [ -f "./Makefile" ]; then
  105. # echo 'reuse configure'
  106. #else
  107. echo "./Configure $FF_CFG_FLAGS"
  108. ./Configure $FF_CFG_FLAGS
  109. #fi
  110. #--------------------
  111. echo ""
  112. echo "--------------------"
  113. echo "[*] compile openssl"
  114. echo "--------------------"
  115. make depend
  116. make $FF_MAKE_FLAGS
  117. make install_sw
  118. #--------------------
  119. echo ""
  120. echo "--------------------"
  121. echo "[*] link openssl"
  122. echo "--------------------"

3、修改do-compile-ffmpeg.sh

    这个脚本就是真正编译ffmpeg的了,与“2”中的openssl类似,主要是需要修改交叉编译工具链,和文件系统。

    同样的与安卓相关的FF_TOOLCHAIN_TOUCH这一段可以去掉的:

  1. FF_TOOLCHAIN_TOUCH="$FF_TOOLCHAIN_PATH/touch"
  2. if [ ! -f "$FF_TOOLCHAIN_TOUCH" ]; then
  3. $ANDROID_NDK/build/tools/make-standalone-toolchain.sh \
  4. $FF_MAKE_TOOLCHAIN_FLAGS \
  5. --platform=$FF_ANDROID_PLATFORM \
  6. --toolchain=$FF_TOOLCHAIN_NAME
  7. touch $FF_TOOLCHAIN_TOUCH;
  8. fi

    将交叉编译工具链中提供的工具等,都export出来,这些可能有些不一定用到,但是export出来有利无害:   

  1. CLANG=$OHOS_LLVM/bin/clang
  2. CLANGXX=$OHOS_LLVM/bin/clang++
  3. OHOS_AR=$OHOS_LLVM/bin/llvm-ar
  4. OHOS_AS=$OHOS_LLVM/bin/llvm-as
  5. OHOS_NM=$OHOS_LLVM/bin/llvm-nm
  6. OHOS_RANLIB=$OHOS_LLVM/bin/llvm-ranlib
  7. OHOS_STRIP=$OHOS_LLVM/bin/llvm-strip
  8. OBJDUMP=$OHOS_LLVM/bin/llvm-objdump
  9. OHOS_LD=$OHOS_LLVM/bin/ld.lld
  10. OHOS_LIBS="-L${OHOS_LLVM}/lib"
  11. OHOS_INCS="-I${OHOS_LLVM}/include"

    同样的将安卓NDK和交叉编译工具链相关的去掉,以arm的64位为例,FF_CROSS_PREFIX=aarch64-linux-android这个是安卓的交叉工具链头,修改为export CC 后面跟着鸿蒙的交叉工具链是llvm目录下的clang,然后FF_EXTRA_CFLAGS和FF_EXTRA_LDFLAGS需要target=aarch64-linux-ohos,指明是64位的,如下:

  1. FF_EXTRA_CFLAGS="$FF_EXTRA_CFLAGS --target=x86_64-linux-ohos"
  2. FF_EXTRA_LDFLAGS="$FF_EXTRA_LDFLAGS --target=x86_64-linux-ohos"

    还需要将相关的系统所需头文件,lib库等指明,用于后续编译链接使用:

  1. OHOS_LIBS="$OHOS_LIBS -L${OHOS_SYSROOT}/usr/lib/aarch64-linux-ohos"
  2. OHOS_INCS="$OHOS_INCS -I${OHOS_SYSROOT}/usr/include/aarch64-linux-ohos"

    后续会用到CC、LD、AR、STRIP这些,需要都指明一下:

  1. export CC=${CLANG}
  2. export LD=${OHOS_LD}
  3. export AR=${OHOS_AR}
  4. export STRIP=${OHOS_STRIP}

    在ffmpeg的configure中,需要加上--cc,--ld,--strip,如下:

  1. if [ -f "./config.h" ]; then
  2. echo 'reuse configure'
  3. else
  4. which $CC
  5. ./configure $FF_CFG_FLAGS \
  6. --cc=${CLANG} \
  7. --ld=${CLANG} \
  8. --strip=${STRIP} \
  9. --extra-cflags="$FF_CFLAGS $FF_EXTRA_CFLAGS" \
  10. --extra-ldflags="$FF_DEP_LIBS $FF_EXTRA_LDFLAGS"
  11. make clean
  12. fi

    然后在最后的CC编译链接成动态库中,需要将刚才的OHOS_LIBS和OHOS_INCS都加上,不然编译链接会报找不到一些系统的基本库之类。同时为了保持跟鸿蒙版ijkplayer中的ffmpeg库名字一致,将库名改为.z.so结尾。(这个貌似是无所谓的)。如下:

  1. $CC -lm -lz -shared --sysroot=$FF_SYSROOT -Wl,--no-undefined -Wl,-z,noexecstack $FF_EXTRA_LDFLAGS \
  2. -Wl,-soname,libqfffmpeg.z.so \
  3. $FF_C_OBJ_FILES \
  4. $FF_ASM_OBJ_FILES \
  5. $OHOS_LIBS \
  6. $OHOS_INCS \
  7. $FF_DEP_LIBS \
  8. -o $FF_PREFIX/libqfffmpeg.z.so

    另外在编译过程中发现--enable-asm和--enable-inline-asm打开的话,会编译不过,所以这2个改为disable的:

  1. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-asm"
  2. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-inline-asm"

    附上完整的do-compile-ffmpeg.sh供参考,我暂时只保留是arm的32位,64位和x86的64位这三个常用的cpu架构编译。具体如下:

  1. #! /usr/bin/env bash
  2. #
  3. #--------------------
  4. echo "===================="
  5. echo "[*] build ffmpeg and check env $1"
  6. echo "===================="
  7. set -e
  8. #--------------------
  9. # common defines
  10. FF_ARCH=$1
  11. FF_BUILD_OPT=$2
  12. echo "FF_ARCH=$FF_ARCH"
  13. echo "FF_BUILD_OPT=$FF_BUILD_OPT"
  14. if [ -z "$FF_ARCH" ]; then
  15. echo "You must specific an architecture 'arm, armv7a, x86, ...'."
  16. echo ""
  17. exit 1
  18. fi
  19. if [ -z "$OHOS_LLVM" ]; then
  20. echo "You must define OHOS_LLVM before starting."
  21. echo "sysroot and llvm is necessary.\n"
  22. exit 1
  23. fi
  24. if [ -z "$OHOS_SYSROOT" ]; then
  25. echo "You must define OHOS_SYSROOT before starting."
  26. echo "sysroot and llvm is necessary.\n"
  27. exit 1
  28. fi
  29. FF_BUILD_ROOT=`pwd`
  30. FF_BUILD_NAME=
  31. FF_SOURCE=
  32. FF_CROSS_PREFIX=
  33. FF_DEP_OPENSSL_INC=
  34. FF_DEP_OPENSSL_LIB=
  35. FF_DEP_LIBSOXR_INC=
  36. FF_DEP_LIBSOXR_LIB=
  37. FF_CFG_FLAGS=
  38. FF_EXTRA_CFLAGS=
  39. FF_EXTRA_LDFLAGS=
  40. FF_DEP_LIBS=
  41. FF_MODULE_DIRS="compat libavcodec libavfilter libavformat libavutil libswresample libswscale"
  42. FF_ASSEMBLER_SUB_DIRS=
  43. #--------------------
  44. echo ""
  45. echo "--------------------"
  46. echo "[*] do detect env"
  47. echo "--------------------"
  48. . ./tools/do-detect-env.sh
  49. FF_MAKE_FLAGS=$IJK_MAKE_FLAG
  50. # compile
  51. CLANG=$OHOS_LLVM/bin/clang
  52. CLANGXX=$OHOS_LLVM/bin/clang++
  53. OHOS_AR=$OHOS_LLVM/bin/llvm-ar
  54. OHOS_AS=$OHOS_LLVM/bin/llvm-as
  55. OHOS_NM=$OHOS_LLVM/bin/llvm-nm
  56. OHOS_RANLIB=$OHOS_LLVM/bin/llvm-ranlib
  57. OHOS_STRIP=$OHOS_LLVM/bin/llvm-strip
  58. OBJDUMP=$OHOS_LLVM/bin/llvm-objdump
  59. OHOS_LD=$OHOS_LLVM/bin/ld.lld
  60. OHOS_LIBS="-L${OHOS_LLVM}/lib"
  61. OHOS_INCS="-I${OHOS_LLVM}/include"
  62. #----- armv7a begin -----
  63. if [ "$FF_ARCH" = "armv7a" ]; then
  64. FF_BUILD_NAME=ffmpeg-armv7a
  65. FF_BUILD_NAME_OPENSSL=openssl-armv7a
  66. FF_BUILD_NAME_LIBSOXR=libsoxr-armv7a
  67. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  68. FF_CFG_FLAGS="$FF_CFG_FLAGS --arch=arm --cpu=cortex-a8"
  69. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-neon"
  70. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-thumb"
  71. OHOS_LIBS="$OHOS_LIBS -L${OHOS_SYSROOT}/usr/lib/arm-linux-ohos"
  72. OHOS_INCS="$OHOS_INCS -I${OHOS_SYSROOT}/usr/include/arm-linux-ohos"
  73. FF_EXTRA_CFLAGS="$FF_EXTRA_CFLAGS -march=armv7-a -mcpu=cortex-a8 -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb --target=arm-linux-ohos"
  74. FF_EXTRA_LDFLAGS="$FF_EXTRA_LDFLAGS -Wl,--fix-cortex-a8 --target=arm-linux-ohos"
  75. FF_ASSEMBLER_SUB_DIRS="arm"
  76. elif [ "$FF_ARCH" = "arm64" ]; then
  77. FF_BUILD_NAME=ffmpeg-arm64
  78. FF_BUILD_NAME_OPENSSL=openssl-arm64
  79. FF_BUILD_NAME_LIBSOXR=libsoxr-arm64
  80. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  81. FF_CFG_FLAGS="$FF_CFG_FLAGS --arch=aarch64 --enable-yasm"
  82. OHOS_LIBS="$OHOS_LIBS -L${OHOS_SYSROOT}/usr/lib/aarch64-linux-ohos"
  83. OHOS_INCS="$OHOS_INCS -I${OHOS_SYSROOT}/usr/include/aarch64-linux-ohos"
  84. FF_EXTRA_CFLAGS="$FF_EXTRA_CFLAGS --target=aarch64-linux-ohos"
  85. FF_EXTRA_LDFLAGS="$FF_EXTRA_LDFLAGS --target=aarch64-linux-ohos"
  86. FF_ASSEMBLER_SUB_DIRS="aarch64 neon"
  87. elif [ "$FF_ARCH" = "x86_64" ]; then
  88. FF_BUILD_NAME=ffmpeg-x86_64
  89. FF_BUILD_NAME_OPENSSL=openssl-x86_64
  90. FF_BUILD_NAME_LIBSOXR=libsoxr-x86_64
  91. FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
  92. FF_CFG_FLAGS="$FF_CFG_FLAGS --arch=x86_64 --enable-yasm"
  93. OHOS_LIBS="$OHOS_LIBS -L${OHOS_SYSROOT}/usr/lib/x86_64-linux-ohos"
  94. OHOS_INCS="$OHOS_INCS -I${OHOS_SYSROOT}/usr/include/x86_64-linux-ohos"
  95. FF_EXTRA_CFLAGS="$FF_EXTRA_CFLAGS --target=x86_64-linux-ohos"
  96. FF_EXTRA_LDFLAGS="$FF_EXTRA_LDFLAGS --target=x86_64-linux-ohos"
  97. FF_ASSEMBLER_SUB_DIRS="x86"
  98. else
  99. echo "unknown architecture $FF_ARCH";
  100. exit 1
  101. fi
  102. if [ ! -d $FF_SOURCE ]; then
  103. echo ""
  104. echo "!! ERROR"
  105. echo "!! Can not find FFmpeg directory for $FF_BUILD_NAME"
  106. echo "!! Run 'sh init-android.sh' first"
  107. echo ""
  108. exit 1
  109. fi
  110. FF_SYSROOT=$OHOS_SYSROOT
  111. FF_PREFIX=$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output
  112. FF_DEP_OPENSSL_INC=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/include
  113. FF_DEP_OPENSSL_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/lib
  114. FF_DEP_LIBSOXR_INC=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_LIBSOXR/output/include
  115. FF_DEP_LIBSOXR_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_LIBSOXR/output/lib
  116. case "$UNAME_S" in
  117. CYGWIN_NT-*)
  118. FF_SYSROOT="$(cygpath -am $FF_SYSROOT)"
  119. FF_PREFIX="$(cygpath -am $FF_PREFIX)"
  120. ;;
  121. esac
  122. mkdir -p $FF_PREFIX
  123. # mkdir -p $FF_SYSROOT
  124. #--------------------
  125. echo ""
  126. echo "--------------------"
  127. echo "[*] check ffmpeg env"
  128. echo "--------------------"
  129. export CC=${CLANG}
  130. export LD=${OHOS_LD}
  131. export AR=${OHOS_AR}
  132. export STRIP=${OHOS_STRIP}
  133. FF_CFLAGS="-O3 -Wall -pipe \
  134. -std=c99 \
  135. -ffast-math \
  136. -fstrict-aliasing -Werror=strict-aliasing \
  137. -Wno-psabi -Wa,--noexecstack \
  138. -DANDROID -DNDEBUG"
  139. # cause av_strlcpy crash with gcc4.7, gcc4.8
  140. # -fmodulo-sched -fmodulo-sched-allow-regmoves
  141. # --enable-thumb is OK
  142. #FF_CFLAGS="$FF_CFLAGS -mthumb"
  143. # not necessary
  144. #FF_CFLAGS="$FF_CFLAGS -finline-limit=300"
  145. export COMMON_FF_CFG_FLAGS=
  146. . $FF_BUILD_ROOT/config/module.sh
  147. #--------------------
  148. # with openssl
  149. if [ -f "${FF_DEP_OPENSSL_LIB}/libssl.a" ]; then
  150. echo "OpenSSL detected"
  151. # FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-nonfree"
  152. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-openssl"
  153. FF_CFLAGS="$FF_CFLAGS -I${FF_DEP_OPENSSL_INC}"
  154. FF_DEP_LIBS="$FF_DEP_LIBS -L${FF_DEP_OPENSSL_LIB} -lssl -lcrypto"
  155. fi
  156. FF_CFG_FLAGS="$FF_CFG_FLAGS $COMMON_FF_CFG_FLAGS"
  157. #--------------------
  158. # Standard options:
  159. FF_CFG_FLAGS="$FF_CFG_FLAGS --prefix=$FF_PREFIX"
  160. # Advanced options (experts only):
  161. FF_CFG_FLAGS="$FF_CFG_FLAGS "
  162. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-cross-compile"
  163. FF_CFG_FLAGS="$FF_CFG_FLAGS --target-os=linux"
  164. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-pic"
  165. # FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-symver"
  166. if [ "$FF_ARCH" = "x86" ]; then
  167. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-asm"
  168. else
  169. # Optimization options (experts only):
  170. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-asm"
  171. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-inline-asm"
  172. fi
  173. case "$FF_BUILD_OPT" in
  174. debug)
  175. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-optimizations"
  176. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-debug"
  177. FF_CFG_FLAGS="$FF_CFG_FLAGS --disable-small"
  178. ;;
  179. *)
  180. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-optimizations"
  181. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-debug"
  182. FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-small"
  183. ;;
  184. esac
  185. #--------------------
  186. echo ""
  187. echo "--------------------"
  188. echo "[*] configurate ffmpeg"
  189. echo "--------------------"
  190. cd $FF_SOURCE
  191. if [ -f "./config.h" ]; then
  192. echo 'reuse configure'
  193. else
  194. which $CC
  195. ./configure $FF_CFG_FLAGS \
  196. --cc=${CLANG} \
  197. --ld=${CLANG} \
  198. --strip=${STRIP} \
  199. --extra-cflags="$FF_CFLAGS $FF_EXTRA_CFLAGS" \
  200. --extra-ldflags="$FF_DEP_LIBS $FF_EXTRA_LDFLAGS"
  201. make clean
  202. fi
  203. #--------------------
  204. echo ""
  205. echo "--------------------"
  206. echo "[*] compile ffmpeg"
  207. echo "--------------------"
  208. cp config.* $FF_PREFIX
  209. make $FF_MAKE_FLAGS > /dev/null
  210. make install
  211. mkdir -p $FF_PREFIX/include/libffmpeg
  212. cp -f config.h $FF_PREFIX/include/libffmpeg/config.h
  213. #--------------------
  214. echo ""
  215. echo "--------------------"
  216. echo "[*] link ffmpeg"
  217. echo "--------------------"
  218. echo $FF_EXTRA_LDFLAGS
  219. FF_C_OBJ_FILES=
  220. FF_ASM_OBJ_FILES=
  221. for MODULE_DIR in $FF_MODULE_DIRS
  222. do
  223. C_OBJ_FILES="$MODULE_DIR/*.o"
  224. if ls $C_OBJ_FILES 1> /dev/null 2>&1; then
  225. echo "link $MODULE_DIR/*.o"
  226. FF_C_OBJ_FILES="$FF_C_OBJ_FILES $C_OBJ_FILES"
  227. fi
  228. for ASM_SUB_DIR in $FF_ASSEMBLER_SUB_DIRS
  229. do
  230. ASM_OBJ_FILES="$MODULE_DIR/$ASM_SUB_DIR/*.o"
  231. if ls $ASM_OBJ_FILES 1> /dev/null 2>&1; then
  232. echo "link $MODULE_DIR/$ASM_SUB_DIR/*.o"
  233. FF_ASM_OBJ_FILES="$FF_ASM_OBJ_FILES $ASM_OBJ_FILES"
  234. fi
  235. done
  236. done
  237. $CC -lm -lz -shared --sysroot=$FF_SYSROOT -Wl,--no-undefined -Wl,-z,noexecstack $FF_EXTRA_LDFLAGS \
  238. -Wl,-soname,libqfffmpeg.z.so \
  239. $FF_C_OBJ_FILES \
  240. $FF_ASM_OBJ_FILES \
  241. $OHOS_LIBS \
  242. $OHOS_INCS \
  243. $FF_DEP_LIBS \
  244. -o $FF_PREFIX/libqfffmpeg.z.so
  245. mysedi() {
  246. f=$1
  247. exp=$2
  248. n=`basename $f`
  249. cp $f /tmp/$n
  250. sed $exp /tmp/$n > $f
  251. rm /tmp/$n
  252. }
  253. echo ""
  254. echo "--------------------"
  255. echo "[*] create files for shared ffmpeg"
  256. echo "--------------------"
  257. rm -rf $FF_PREFIX/shared
  258. mkdir -p $FF_PREFIX/shared/lib/pkgconfig
  259. ln -s $FF_PREFIX/include $FF_PREFIX/shared/include
  260. ln -s $FF_PREFIX/libqfffmpeg.z.so $FF_PREFIX/shared/lib/libqfffmpeg.z.so
  261. cp $FF_PREFIX/lib/pkgconfig/*.pc $FF_PREFIX/shared/lib/pkgconfig
  262. for f in $FF_PREFIX/lib/pkgconfig/*.pc; do
  263. # in case empty dir
  264. if [ ! -f $f ]; then
  265. continue
  266. fi
  267. cp $f $FF_PREFIX/shared/lib/pkgconfig
  268. f=$FF_PREFIX/shared/lib/pkgconfig/`basename $f`
  269. # OSX sed doesn't have in-place(-i)
  270. mysedi $f 's/\/output/\/output\/shared/g'
  271. mysedi $f 's/-lavcodec/-lijkffmpeg/g'
  272. mysedi $f 's/-lavfilter/-lijkffmpeg/g'
  273. mysedi $f 's/-lavformat/-lijkffmpeg/g'
  274. mysedi $f 's/-lavutil/-lijkffmpeg/g'
  275. mysedi $f 's/-lswresample/-lijkffmpeg/g'
  276. mysedi $f 's/-lswscale/-lijkffmpeg/g'
  277. done

    以上便是最关键的3个脚本的修改,按照上述修改后,便可编译适配纯harmony系统的ffmpeg库。大家可以将我上述附上的3个完整的脚本复制出来跟原本安卓版的脚本进行对比,可以更清晰地知道修改什么地方。

四、编译

    修改脚本之后,便可跟安卓一样运行脚本编译。

    先编译openssl:

  1. ./compile-openssl.sh clean
  2. ./compile-openssl.sh all

    然后编译ffmpeg:

  1. ./compile-ffmpeg.sh clean
  2. ./compile-ffmpeg.sh all

    编译完成后,在contrib/build下会有对应cpu架构的ffmpeg和openssl产物文件夹。例如我编译arm64的:

    在ffmpeg-arm64的output目录下,就有最终编译好的libqfffmpeg.z.so动态库:

    亲测替换到鸿蒙版的ijkplayer中,可以正常运行。至此文章结束。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号