Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/audiodocs/docs/other/prebuilt-libraries-support.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -146,4 +146,4 @@ The app's `package.json`:
</TabItem>
</Tabs>

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.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ CreateDecoderResult createAndOpen(const Source &source) {

template <OsFallbackOpenableSource Source>
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<miniaudio::MiniAudioDecoder>(source); result.is_ok()) {
Comment thread
mdydek marked this conversation as resolved.
Resolved
return result;
}
if (auto result = createAndOpen<os_decoder::Decoder>(source); result.is_ok()) {
return result;
}
#else
#if RN_AUDIO_API_HAS_OS_DECODER
if (auto result = createAndOpen<os_decoder::Decoder>(source); result.is_ok()) {
return result;
}
#endif

if (auto result = createAndOpen<miniaudio::MiniAudioDecoder>(source); result.is_ok()) {
return result;
}
#endif
return Err(errorMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ void MiniAudioDecoder::teardownDecoder() {
resetOpenMetadata();
}

void MiniAudioDecoder::discardFailedDecoder() {
decoder_.reset();
memoryCopy_.clear();
resetOpenMetadata();
}

void MiniAudioDecoder::close() {
teardownDecoder();
}
Expand All @@ -75,7 +81,7 @@ DecoderResult MiniAudioDecoder::open(const LocalFileSource &source) {
decoder_ = std::make_unique<ma_decoder>();
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));
}

Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ma_decoder> decoder_;
std::vector<uint8_t> memoryCopy_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> pcmBytes(2);
pcmBytes[0] = 0x00;
Expand Down
Loading