交叉编译opus音频解码库

6 分钟阅读

交叉编译opus文件解码器库(opusfile)到android平台上。

准备工作

opus库

git clone下载opus库,ogg库,opusfile库。(使用最新的稳定版本库2022-04-11)

opus主页 :切换到v1.3.5的git标签

ogg主页:切换到v1.3.1的git标签

opusfile主页master分支:commit cf218fb54929a1f54e30e2cb208a22d08b08c889

opusfile稳定版本无法使用cmake编译,master分支刚添加了cmake编译的支持,所以使用master分支。

ndk库

下载android-ndk,我们目前统一使用这个:

android-ndk-r21e-linux-x86_64下载

目录结构

都下载解压完后如下放置:

1
2
3
4
5
6
7
8
9
10
$ tree -L 1
.
├── android-ndk-r21e
├── build.sh
├── CMakeLists.txt
├── ogg
├── opus
└── opusfile

4 directories, 2 files

其中build.shCMakeLists.txt是后添加的,稍后介绍。

配置

为了把opusfile依赖的ogg库和opus库集成到一起,需要写CMakeLists.txt

CMakeLists.txt很简单:

1
2
3
4
5
6
7
cmake_minimum_required(VERSION 3.5)

project(opus_project)

add_subdirectory(ogg)
add_subdirectory(opus)
add_subdirectory(opusfile)

build.sh内容:

opusfile会生成两个库分别是:opusfileopusurl。我们只需要opusfile,根据opusfileCMakeLists.txt的代码需要给cmake传递-DOP_DISABLE_HTTP=ON参数,还有例子也不进行编译:-DOP_DISABLE_EXAMPLES=ON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

# Set work directory and ndk directory
WORKDIR=$(cd "$(dirname "$0")" || exit; pwd)
export ANDROID_NDK=$WORKDIR/android-ndk-r21e

rm -rf build
mkdir -p build && cd build || exit

#refer to: https://developer.android.com/ndk/guides/cmake?hl=zh-cn
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
	-DANDROID_ABI="armeabi-v7a" \
	-DANDROID_NDK=$ANDROID_NDK \
	-DANDROID_PLATFORM=android-21 \
	-DCMAKE_INSTALL_PREFIX="$WORKDIR"/install \
	-DOP_DISABLE_HTTP=ON \
	-DOP_DISABLE_EXAMPLES=ON \
	..

make opusfile -j8 && make install

还需要在opusfileCMakeLists.txt配置里面把ogg库和opus库的依赖来源改掉,不要从系统中搜索。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ee37291..9871206 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,8 +22,6 @@ option(OP_DISABLE_DOCS "Do not build API documentation" OFF)

 include(GNUInstallDirs)

-find_package(Ogg REQUIRED)
-find_package(Opus REQUIRED)

 include(CMakePushCheckState)
 include(CheckSymbolExists)
@@ -59,8 +57,8 @@ target_include_directories(opusfile
 )
 target_link_libraries(opusfile
   PUBLIC
-    Ogg::ogg
-    Opus::opus
+    ogg
+    opus
     $<$<BOOL:${OP_HAVE_LIBM}>:m>
 )
 target_compile_options(opusfile

执行build.sh编译即可在install目录下生成oggopusopusfile库。

留下评论