交叉编译opus音频解码库
交叉编译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.sh
和CMakeLists.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
会生成两个库分别是:opusfile
,opusurl
。我们只需要opusfile
,根据opusfile
的CMakeLists.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
还需要在opusfile
的CMakeLists.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
目录下生成ogg
、opus
、opusfile
库。
留下评论