diff --git a/libraries/decoder_flac/README.md b/libraries/decoder_flac/README.md index 3e9ea9ae2f..5bb24c8b11 100644 --- a/libraries/decoder_flac/README.md +++ b/libraries/decoder_flac/README.md @@ -17,7 +17,7 @@ To use the module you need to clone this GitHub project and depend on its modules locally. Instructions for doing this can be found in the [top level README][]. -In addition, it's necessary to build the module's native components as follows: +In addition, it's necessary to fetch libflac as follows: * Set the following environment variables: @@ -26,30 +26,24 @@ cd "" FLAC_MODULE_PATH="$(pwd)/libraries/decoder_flac/src/main" ``` -* Download the [Android NDK][] and set its location in an environment variable. - This build configuration has been tested on NDK r21. - -``` -NDK_PATH="" -``` - -* Download and extract flac-1.3.2 as "${FLAC_MODULE_PATH}/jni/flac" folder: +* Fetch libflac: ``` cd "${FLAC_MODULE_PATH}/jni" && \ -curl https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.3.2.tar.xz | tar xJ && \ -mv flac-1.3.2 flac +git clone https://github.com/xiph/flac.git libflac ``` -* Build the JNI native libraries from the command line: +* [Install CMake][] -``` -cd "${FLAC_MODULE_PATH}"/jni && \ -${NDK_PATH}/ndk-build APP_ABI=all -j4 -``` +Having followed these steps, gradle will build the module automatically when run +on the command line or via Android Studio, using [CMake][] and [Ninja][] to +configure and build libflac and the module's [JNI wrapper library][]. [top level README]: ../../README.md -[Android NDK]: https://developer.android.com/tools/sdk/ndk/index.html +[Install CMake]: https://developer.android.com/studio/projects/install-ndk +[CMake]: https://cmake.org/ +[Ninja]: https://ninja-build.org +[JNI wrapper library]: src/main/jni/flac_jni.cc ## Build instructions (Windows) diff --git a/libraries/decoder_flac/build.gradle b/libraries/decoder_flac/build.gradle index a7fa55ee40..2f8173a1bb 100644 --- a/libraries/decoder_flac/build.gradle +++ b/libraries/decoder_flac/build.gradle @@ -17,13 +17,19 @@ android { namespace 'androidx.media3.decoder.flac' sourceSets { - main { - jniLibs.srcDir 'src/main/libs' - jni.srcDirs = [] // Disable the automatic ndk-build call by Android Studio. - } androidTest.assets.srcDir '../test_data/src/test/assets' } + defaultConfig { + externalNativeBuild { + cmake { + arguments "-DWITH_OGG=OFF" + arguments "-DINSTALL_MANPAGES=OFF" + targets "flacJNI" + } + } + } + // TODO(Internal: b/372449691): Remove packagingOptions once AGP is updated // to version 8.5.1 or higher. packagingOptions { @@ -33,6 +39,22 @@ android { } } +// Configure the native build only if libflac is present to avoid gradle sync +// failures if libflac hasn't been built according to the README instructions. +if (project.file('src/main/jni/libflac').exists()) { + android.externalNativeBuild.cmake { + path = 'src/main/jni/CMakeLists.txt' + version = '3.21.0+' + if (project.hasProperty('externalNativeBuildDir')) { + if (!new File(externalNativeBuildDir).isAbsolute()) { + ext.externalNativeBuildDir = + new File(rootDir, it.externalNativeBuildDir) + } + buildStagingDirectory = "${externalNativeBuildDir}/${project.name}" + } + } +} + dependencies { implementation project(modulePrefix + 'lib-decoder') // TODO(b/203752526): Remove this dependency. diff --git a/libraries/decoder_flac/src/main/jni/Android.mk b/libraries/decoder_flac/src/main/jni/Android.mk deleted file mode 100644 index b19d169111..0000000000 --- a/libraries/decoder_flac/src/main/jni/Android.mk +++ /dev/null @@ -1,40 +0,0 @@ -# -# Copyright (C) 2016 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -WORKING_DIR := $(call my-dir) - -# build libflacJNI.so -include $(CLEAR_VARS) -include $(WORKING_DIR)/flac_sources.mk - -LOCAL_PATH := $(WORKING_DIR) -LOCAL_MODULE := libflacJNI -LOCAL_ARM_MODE := arm -LOCAL_CPP_EXTENSION := .cc - -LOCAL_C_INCLUDES := \ - $(LOCAL_PATH)/flac/include \ - $(LOCAL_PATH)/flac/src/libFLAC/include -LOCAL_SRC_FILES := $(FLAC_SOURCES) - -LOCAL_CFLAGS += '-DPACKAGE_VERSION="1.3.2"' -DFLAC__NO_MD5 -DFLAC__INTEGER_ONLY_LIBRARY -LOCAL_CFLAGS += -D_REENTRANT -DPIC -DU_COMMON_IMPLEMENTATION -fPIC -DHAVE_SYS_PARAM_H -LOCAL_CFLAGS += -O3 -funroll-loops -finline-functions -DFLAC__NO_ASM '-DFLAC__HAS_OGG=0' - -LOCAL_LDLIBS := -llog -lz -lm -# Enable 16 KB ELF alignment. -LOCAL_LDFLAGS += "-Wl,-z,max-page-size=16384" -include $(BUILD_SHARED_LIBRARY) diff --git a/libraries/decoder_flac/src/main/jni/Application.mk b/libraries/decoder_flac/src/main/jni/Application.mk deleted file mode 100644 index e33070e121..0000000000 --- a/libraries/decoder_flac/src/main/jni/Application.mk +++ /dev/null @@ -1,20 +0,0 @@ -# -# Copyright (C) 2016 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -APP_OPTIM := release -APP_STL := c++_static -APP_CPPFLAGS := -frtti -APP_PLATFORM := android-14 diff --git a/libraries/decoder_flac/src/main/jni/CMakeLists.txt b/libraries/decoder_flac/src/main/jni/CMakeLists.txt new file mode 100644 index 0000000000..6ce3af0952 --- /dev/null +++ b/libraries/decoder_flac/src/main/jni/CMakeLists.txt @@ -0,0 +1,51 @@ +# +# Copyright 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR) + +# Enable C++11 features. +set(CMAKE_CXX_STANDARD 11) + +# Define project name for your JNI module +project(libflacJNI C CXX) + +set(libflac_jni_root "${CMAKE_CURRENT_SOURCE_DIR}") + +# Build libflac. +add_subdirectory("${libflac_jni_root}/libflac" + EXCLUDE_FROM_ALL) + +# Build libflacJNI. +add_library(flacJNI + SHARED + flac_jni.cc + flac_parser.cc) + +# Add the include directory from libflac. +include_directories("${libflac_jni_root}/libflac/include") + +# Locate NDK log library. +find_library(android_log_lib log) + +# Link libflacJNI against used libraries. +target_link_libraries(flacJNI + PRIVATE android + PRIVATE FLAC + PRIVATE ${android_log_lib}) + +# Enable 16 KB ELF alignment. +target_link_options(flacJNI + PRIVATE "-Wl,-z,max-page-size=16384") diff --git a/libraries/decoder_flac/src/main/jni/flac_sources.mk b/libraries/decoder_flac/src/main/jni/flac_sources.mk deleted file mode 100644 index ade9daa359..0000000000 --- a/libraries/decoder_flac/src/main/jni/flac_sources.mk +++ /dev/null @@ -1,45 +0,0 @@ -# -# Copyright (C) 2016 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -FLAC_SOURCES = \ - flac_jni.cc \ - flac_parser.cc \ - flac/src/libFLAC/bitmath.c \ - flac/src/libFLAC/bitreader.c \ - flac/src/libFLAC/bitwriter.c \ - flac/src/libFLAC/cpu.c \ - flac/src/libFLAC/crc.c \ - flac/src/libFLAC/fixed.c \ - flac/src/libFLAC/fixed_intrin_sse2.c \ - flac/src/libFLAC/fixed_intrin_ssse3.c \ - flac/src/libFLAC/float.c \ - flac/src/libFLAC/format.c \ - flac/src/libFLAC/lpc.c \ - flac/src/libFLAC/lpc_intrin_avx2.c \ - flac/src/libFLAC/lpc_intrin_sse2.c \ - flac/src/libFLAC/lpc_intrin_sse41.c \ - flac/src/libFLAC/lpc_intrin_sse.c \ - flac/src/libFLAC/md5.c \ - flac/src/libFLAC/memory.c \ - flac/src/libFLAC/metadata_iterators.c \ - flac/src/libFLAC/metadata_object.c \ - flac/src/libFLAC/stream_decoder.c \ - flac/src/libFLAC/stream_encoder.c \ - flac/src/libFLAC/stream_encoder_framing.c \ - flac/src/libFLAC/stream_encoder_intrin_avx2.c \ - flac/src/libFLAC/stream_encoder_intrin_sse2.c \ - flac/src/libFLAC/stream_encoder_intrin_ssse3.c \ - flac/src/libFLAC/window.c diff --git a/libraries/decoder_flac/src/main/jni/include/flac_parser.h b/libraries/decoder_flac/src/main/jni/include/flac_parser.h index 44a0d08718..ed200aca6f 100644 --- a/libraries/decoder_flac/src/main/jni/include/flac_parser.h +++ b/libraries/decoder_flac/src/main/jni/include/flac_parser.h @@ -19,15 +19,15 @@ #include +// libFLAC parser +#include + #include #include #include #include -// libFLAC parser -#include "FLAC/stream_decoder.h" - -#include "include/data_source.h" +#include "../include/data_source.h" typedef int status_t; diff --git a/libraries/decoder_iamf/src/main/jni/CMakeLists.txt b/libraries/decoder_iamf/src/main/jni/CMakeLists.txt index 32a65ed1ac..78f8aef5df 100644 --- a/libraries/decoder_iamf/src/main/jni/CMakeLists.txt +++ b/libraries/decoder_iamf/src/main/jni/CMakeLists.txt @@ -39,7 +39,7 @@ add_library(iamfJNI # Locate NDK log library. find_library(android_log_lib log) -# Link libgav1JNI against used libraries. +# Link libiamfJNI against used libraries. target_link_libraries(iamfJNI PRIVATE android PRIVATE iamf diff --git a/libraries/decoder_opus/src/main/jni/CMakeLists.txt b/libraries/decoder_opus/src/main/jni/CMakeLists.txt index de5b62b830..f5e56b7b07 100644 --- a/libraries/decoder_opus/src/main/jni/CMakeLists.txt +++ b/libraries/decoder_opus/src/main/jni/CMakeLists.txt @@ -39,7 +39,7 @@ add_library(opusV2JNI # Locate NDK log library. find_library(android_log_lib log) -# Link libgav1JNI against used libraries. +# Link libopusJNI against used libraries. target_link_libraries(opusV2JNI PRIVATE android PRIVATE opus