fix(android): report a failed sample buffer allocation

The audio library builds without C++ exceptions since the size build,
so a std::vector that could not allocate ended the process, where it
used to fail the song with a Java exception.

The FLAC block, the exclusive output ring and the DSD read buffers use
PcmBuffer, which allocates without throwing and reports failure:
decoding fails with "Cannot allocate FLAC block" and the JNI calls
throw IllegalStateException as before.

With this and the speed level the arm64 library is 446.4 KB, against
441.6 KB after the size build and 490.1 KB before it.
This commit is contained in:
zarzet committed 2026-11-22 00:50:26 +07:00
1 parent bf9e7ee3e7
commit db59f84abb
5 files changed
+63 -7

No files matched your search

+4 -1
View File
@@ -54,7 +54,10 @@ FLAC__StreamDecoderWriteStatus FlacPcmSource::write(
source.failure = "FLAC format changed during decoding";
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
source.block.resize(static_cast<size_t>(header.blocksize) * source.channels);
if (!source.block.resize(static_cast<size_t>(header.blocksize) * source.channels)) {
source.failure = "Cannot allocate FLAC block";
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
source.offset = 0;
for (size_t i = 0; i < header.blocksize; ++i) {
for (int channel = 0; channel < source.channels; ++channel) {
+2 -2
View File
@@ -1,12 +1,12 @@
#pragma once
#include <FLAC/stream_decoder.h>
#include "pcm_buffer.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
// Owns its file descriptor, decodes one FLAC block at a time, and never passes
// samples through Android's extractor (which may silently emit 16-bit PCM).
@@ -15,7 +15,7 @@ class FlacPcmSource final {
void operator()(FLAC__StreamDecoder *decoder) const;
};
std::unique_ptr<FLAC__StreamDecoder, DeleteDecoder> decoder;
std::vector<int32_t> block;
PcmBuffer<int32_t> block;
size_t offset = 0;
uint64_t position = 0;
bool end = false;
+12 -4
View File
@@ -2,6 +2,7 @@
#include <oboe/Oboe.h>
#include <wavpack.h>
#include "flac_pcm_source.h"
#include "pcm_buffer.h"
#include <algorithm>
#include <atomic>
@@ -76,7 +77,7 @@ public:
class ExclusiveOutput final : public oboe::AudioStreamDataCallback {
std::shared_ptr<oboe::AudioStream> stream;
// Single producer (Kotlin worker), single consumer (audio callback).
std::vector<uint8_t> ring;
PcmBuffer<uint8_t> ring;
std::atomic<uint64_t> read{0}, written{0}, musicFrames{0};
std::atomic<int64_t> lastMusicEnd{0};
int64_t callbackFrames = 0;
@@ -130,7 +131,9 @@ public:
floatOutput = format == oboe::AudioFormat::Float;
bits = format == oboe::AudioFormat::I16 ? 16 : format == oboe::AudioFormat::I24 ? 24 : 32;
frameBytes = channels * (bits / 8);
ring.resize(static_cast<size_t>(rate / 4) * frameBytes);
if (!ring.resize(static_cast<size_t>(rate / 4) * frameBytes)) {
return fail(errorOut, "Cannot allocate output buffer");
}
return true;
}
appendFmt(attempts, " actual=%s/%dHz/%dch/%s, api=%s, device=%d",
@@ -353,7 +356,12 @@ JNIEXPORT jbyteArray JNICALL JNI_AUDIO(readWavPack)(JNIEnv *env, jobject, jlong
std::string error;
auto d = decoders.get(handle, &error);
if (!d) { report(env, error); return nullptr; }
std::vector<int32_t> data(static_cast<size_t>(frames) * d->channels);
PcmBuffer<int32_t> data;
PcmBuffer<jbyte> bytes;
if (!data.resize(static_cast<size_t>(frames) * d->channels) || !bytes.resize(data.size())) {
report(env, "Cannot allocate DSD buffer");
return nullptr;
}
auto count = WavpackUnpackSamples(d->context, data.data(), frames);
if (WavpackGetNumErrors(d->context) != 0) {
report(env, "Corrupt WavPack DSD data");
@@ -363,7 +371,7 @@ JNIEXPORT jbyteArray JNICALL JNI_AUDIO(readWavPack)(JNIEnv *env, jobject, jlong
report(env, "Truncated WavPack DSD data");
return nullptr;
}
std::vector<jbyte> bytes(static_cast<size_t>(count) * d->channels);
bytes.resize(static_cast<size_t>(count) * d->channels);
// OPEN_DSD_NATIVE returns interleaved, MSB-first DSD bytes regardless
// of the wrapper's original DSF bit-order/blocking flags.
for (size_t i = 0; i < bytes.size(); ++i) bytes[i] = static_cast<jbyte>(data[i]);
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <cstddef>
#include <memory>
#include <new>
#include <utility>
// Sample storage whose growth reports failure. This library builds without
// exceptions, where a std::vector that cannot allocate ends the process.
template <typename T>
class PcmBuffer final {
std::unique_ptr<T[]> values;
size_t capacity = 0;
size_t count = 0;
public:
// Growing drops the old contents. On false the buffer is unchanged.
bool resize(size_t size) {
if (size > capacity) {
std::unique_ptr<T[]> grown(new (std::nothrow) T[size]());
if (!grown) return false;
values = std::move(grown);
capacity = size;
}
count = size;
return true;
}
void clear() { count = 0; }
size_t size() const { return count; }
T *data() { return values.get(); }
const T *data() const { return values.get(); }
T &operator[](size_t index) { return values[index]; }
const T &operator[](size_t index) const { return values[index]; }
};
@@ -6,8 +6,10 @@
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <unistd.h>
#include <vector>
namespace {
void check(bool value, const char *message) {
@@ -126,6 +128,15 @@ void retainsDescriptor() {
check(source.seek(5001, &error), error.c_str());
compare(source, fixture, 5001, 24);
}
void reportsFailedAllocation() {
PcmBuffer<int32_t> buffer;
check(buffer.resize(8), "allocate buffer");
buffer[7] = 42;
const size_t impossible = std::numeric_limits<size_t>::max() / sizeof(int32_t);
check(!buffer.resize(impossible), "accepted an impossible size");
check(buffer.size() == 8 && buffer[7] == 42, "failed growth changed the buffer");
}
}
int main() {
@@ -137,6 +148,7 @@ int main() {
}
rejectsTruncatedData();
retainsDescriptor();
reportsFailedAllocation();
std::cout << "FLAC integer precision, padding, seek, EOF and truncation checks passed\n";
return EXIT_SUCCESS;
} catch (const std::exception &error) {