diff --git a/packages/audiodocs/docs/other/prebuilt-libraries-support.mdx b/packages/audiodocs/docs/other/prebuilt-libraries-support.mdx index 3781514f25..af0550cd5e 100644 --- a/packages/audiodocs/docs/other/prebuilt-libraries-support.mdx +++ b/packages/audiodocs/docs/other/prebuilt-libraries-support.mdx @@ -12,7 +12,7 @@ import TabItem from '@theme/TabItem'; | Library set | Default | Option | What it adds | | :---- | :---: | :---: | :---- | | FFmpeg (`libavcodec`, `libavformat`, `libavutil`, `libswresample`) | **off** | `enableFFmpeg` | Remote URL streaming (HTTP byte ranges), HLS (`.m3u8`), remote URL metadata — see [Runtime flags](./runtime-flags#where-ffmpeg-is-used). | -| Static libs (`libopus`, `libopusfile`, `libogg`, `libvorbis`, `libvorbisenc`, `libvorbisfile`) | **on** | `disableStaticExternalLibs` | Decoding `ogg`, `opus`, `oga` files | +| Static libs (`libopus`, `libopusfile`, `libogg`, `libvorbis`, `libvorbisenc`, `libvorbisfile`) | **on** | `disableStaticExternalLibs` | Decoding `ogg`, `opus`, `oga` files (on Android, much faster than the system decoder) | The options are independent and can be combined. @@ -146,4 +146,4 @@ The app's `package.json`: -This removes the bundled Opus / Ogg / Vorbis static libraries. With this option enabled, attempts to decode `.ogg`, `.opus`, or `.oga` files through `AudioContext.decodeAudioData` (or any other decoding path) will return an error — those formats are handled inside miniaudio via the now-disabled custom backends. +This removes the bundled Opus / Ogg / Vorbis static libraries, which decode `.ogg`, `.opus` and `.oga` files inside miniaudio. Without them, decoding those files through `AudioContext.decodeAudioData` (or any other decoding path) falls back to the system decoder, which on Android is much slower. diff --git a/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt b/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt index f1709a4e00..796a35188b 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt +++ b/packages/react-native-audio-api/android/src/main/cpp/audioapi/CMakeLists.txt @@ -36,6 +36,16 @@ set_source_files_properties( COMPILE_FLAGS "-O3 -D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__ -Werror=unguarded-availability" ) +# Decoding is throughput-bound even in Debug builds, so the in-process MiniAudio path stays +# optimized like the MediaCodec one above. +set_source_files_properties( + "${COMMON_CPP_DIR}/audioapi/utils/MiniaudioImplementation.cpp" + "${COMMON_CPP_DIR}/audioapi/libs/miniaudio/decoders/libvorbis/miniaudio_libvorbis.c" + "${COMMON_CPP_DIR}/audioapi/libs/miniaudio/decoders/libopus/miniaudio_libopus.c" + PROPERTIES + COMPILE_FLAGS "-O3" +) + # APerformanceHint (API 33) is weak-linked and guarded the same way. set_source_files_properties( "${ANDROID_CPP_DIR}/audioapi/android/ThroughputHintSession.cpp" diff --git a/packages/react-native-audio-api/common/cpp/audioapi/decoding/DecoderFactory.cpp b/packages/react-native-audio-api/common/cpp/audioapi/decoding/DecoderFactory.cpp index 43f99543fd..a513544060 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/decoding/DecoderFactory.cpp +++ b/packages/react-native-audio-api/common/cpp/audioapi/decoding/DecoderFactory.cpp @@ -52,15 +52,25 @@ CreateDecoderResult createAndOpen(const Source &source) { template CreateDecoderResult tryOpenWithOsFallback(const Source &source, const char *errorMessage) { +#if defined(__ANDROID__) + // Android codecs are very slow compared to miniaudio, so place miniaudio first. + // MediaCodec stays the fallback for the rest (AAC/M4A). + if (auto result = createAndOpen(source); result.is_ok()) { + return result; + } + if (auto result = createAndOpen(source); result.is_ok()) { + return result; + } +#else #if RN_AUDIO_API_HAS_OS_DECODER if (auto result = createAndOpen(source); result.is_ok()) { return result; } #endif - if (auto result = createAndOpen(source); result.is_ok()) { return result; } +#endif return Err(errorMessage); } diff --git a/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.cpp b/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.cpp index 3488b44420..8f67c9ec82 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.cpp +++ b/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.cpp @@ -57,6 +57,12 @@ void MiniAudioDecoder::teardownDecoder() { resetOpenMetadata(); } +void MiniAudioDecoder::discardFailedDecoder() { + decoder_.reset(); + memoryCopy_.clear(); + resetOpenMetadata(); +} + void MiniAudioDecoder::close() { teardownDecoder(); } @@ -75,7 +81,7 @@ DecoderResult MiniAudioDecoder::open(const LocalFileSource &source) { decoder_ = std::make_unique(); const ma_result result = ma_decoder_init_file(source.path.c_str(), &config, decoder_.get()); if (result != MA_SUCCESS) { - teardownDecoder(); + discardFailedDecoder(); return Err("MiniAudioDecoder::open failed: " + parseMiniAudioError(result)); } @@ -104,7 +110,7 @@ DecoderResult MiniAudioDecoder::open(const EncodedMemorySource &source) { const ma_result result = ma_decoder_init_memory(memoryCopy_.data(), memoryCopy_.size(), &config, decoder_.get()); if (result != MA_SUCCESS) { - teardownDecoder(); + discardFailedDecoder(); return Err("MiniAudioDecoder::open failed: " + parseMiniAudioError(result)); } diff --git a/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.h b/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.h index 4db3041f18..5df9b0f685 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.h +++ b/packages/react-native-audio-api/common/cpp/audioapi/decoding/backends/MiniAudioDecoder.h @@ -32,6 +32,9 @@ class MiniAudioDecoder : public AudioDecoderBackend { private: void teardownDecoder(); + /// Drops a decoder whose ma_decoder_init_* failed. Init has already released what it + /// acquired, so uninit would close that file a second time. + void discardFailedDecoder(); std::unique_ptr decoder_; std::vector memoryCopy_; diff --git a/packages/react-native-audio-api/common/cpp/audioapi/utils/MiniaudioImplementation.cpp b/packages/react-native-audio-api/common/cpp/audioapi/utils/MiniaudioImplementation.cpp index 91e7e33c04..1f187d4f6c 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/utils/MiniaudioImplementation.cpp +++ b/packages/react-native-audio-api/common/cpp/audioapi/utils/MiniaudioImplementation.cpp @@ -5,10 +5,8 @@ #define MINIAUDIO_IMPLEMENTATION #define MA_DEBUG_OUTPUT -// MiniAudio only decodes, as the fallback behind the OS decoder: Ogg Vorbis and Opus through -// the bundled libvorbis/libopus backends, plus WAV. The OS decoder covers MP3 and FLAC, and -// every encoder is a system one. -#define MA_NO_MP3 +// MiniAudio only decodes: Ogg Vorbis and Opus through the bundled libvorbis/libopus backends, +// plus MP3 and WAV. Android tries it before MediaCodec; iOS keeps it behind the OS decoder. #define MA_NO_FLAC #define MA_NO_ENCODING #define MA_NO_DEVICE_IO diff --git a/packages/react-native-audio-api/common/cpp/test/src/decoding/DecoderFactoryTest.cpp b/packages/react-native-audio-api/common/cpp/test/src/decoding/DecoderFactoryTest.cpp index be6f9cc4fe..11085df381 100644 --- a/packages/react-native-audio-api/common/cpp/test/src/decoding/DecoderFactoryTest.cpp +++ b/packages/react-native-audio-api/common/cpp/test/src/decoding/DecoderFactoryTest.cpp @@ -99,6 +99,19 @@ TEST(DecoderFactoryTest, RejectsUnsupportedLocalFile) { EXPECT_EQ(result.unwrap_err(), "Cannot open local file: unsupported or invalid audio format"); } +TEST(DecoderFactoryTest, RejectsExistingNonAudioLocalFile) { + const std::string path = testFilePath("decoder-factory-not-audio.txt"); + { + std::ofstream output(path, std::ios::binary); + output << std::string(4096, 'x'); + } + + auto result = decoding::createDecoder(decoding::LocalFileSource{.path = path, .sampleRate = 0}); + EXPECT_TRUE(result.is_err()); + + removeFile(path); +} + TEST(DecoderFactoryTest, OpensRawPcmSource) { std::vector pcmBytes(2); pcmBytes[0] = 0x00;