Add the Android app: Gradle build, Oboe audio, connection screen

android/ builds core and ts3j from the same sources as the desktop's
Maven build, so the app shares all of the client's logic:

- audio: Oboe streams behind AudioCapture/AudioPlayback, with a lock-free
  ring between the real-time callback and the blocking Java side, and
  libopus 1.5.2 built with the NDK behind OpusCodec. Speakers are mixed
  onto one stream. Capture uses the VoiceRecognition preset, leaving
  noise suppression and gain to the client's own processing.
- app: a plain connection screen to try the port out - connect,
  microphone, mute, deafen, channel chat and a log - until the real UI.

Targets Android 13 and up (minSdk 33) on arm64 and x86_64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-25 08:24:21 +00:00
parent e211ffa7b6
commit 086b913c8e
26 changed files with 1555 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.22)
project(ts3audio LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
find_package(oboe REQUIRED CONFIG)
# libopus from its release tarball, built static into our one library.
include(FetchContent)
FetchContent_Declare(opus
URL https://github.com/xiph/opus/releases/download/v1.5.2/opus-1.5.2.tar.gz
URL_HASH SHA256=65c1d2f78b9f2fb20082c38cbe47c951ad5839345876e46941612ee87f9a7ce1)
set(OPUS_BUILD_PROGRAMS OFF CACHE BOOL "" FORCE)
set(OPUS_BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(OPUS_BUILD_SHARED_LIBRARY OFF CACHE BOOL "" FORCE)
set(OPUS_INSTALL_PKG_CONFIG_MODULE OFF CACHE BOOL "" FORCE)
set(OPUS_INSTALL_CMAKE_CONFIG_MODULE OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(opus)
add_library(ts3audio SHARED audio_line.cpp opus_jni.cpp)
target_link_libraries(ts3audio PRIVATE oboe::oboe opus log)
target_compile_options(ts3audio PRIVATE -Wall -Wextra)

View File

@@ -0,0 +1,241 @@
// An Oboe stream behind the blocking read/write interface of AudioCapture and AudioPlayback.
//
// The real-time callback never waits and never calls into Java: it only moves samples
// between the device and a lock-free ring, which the Java side fills or drains at its own
// pace, blocking while the ring is full (playback) or short (capture).
#include <jni.h>
#include <oboe/Oboe.h>
#include <android/log.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstring>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
namespace {
constexpr int32_t kSampleRate = 48000;
constexpr int32_t kFrameSize = 960; // 20 ms
// Capture keeps some slack for a Java thread that is briefly late; playback keeps little,
// since everything queued is latency.
constexpr int32_t kCaptureFrames = 8;
constexpr int32_t kPlaybackFrames = 3;
constexpr auto kPollInterval = std::chrono::milliseconds(1);
const char *kTag = "ts3audio";
/** Single-producer, single-consumer ring of 16-bit samples. */
class SampleRing {
public:
explicit SampleRing(size_t capacity) : buffer_(capacity) {}
size_t available() const {
return head_.load(std::memory_order_acquire) - tail_.load(std::memory_order_acquire);
}
size_t space() const { return buffer_.size() - available(); }
/** Producer side: stores as much of {@code data} as fits. */
size_t write(const int16_t *data, size_t count) {
size_t head = head_.load(std::memory_order_relaxed);
size_t tail = tail_.load(std::memory_order_acquire);
size_t n = std::min(count, buffer_.size() - (head - tail));
for (size_t i = 0; i < n; i++) buffer_[(head + i) % buffer_.size()] = data[i];
head_.store(head + n, std::memory_order_release);
return n;
}
/** Consumer side: takes up to {@code count} samples. */
size_t read(int16_t *data, size_t count) {
size_t tail = tail_.load(std::memory_order_relaxed);
size_t head = head_.load(std::memory_order_acquire);
size_t n = std::min(count, head - tail);
for (size_t i = 0; i < n; i++) data[i] = buffer_[(tail + i) % buffer_.size()];
tail_.store(tail + n, std::memory_order_release);
return n;
}
private:
std::vector<int16_t> buffer_;
std::atomic<size_t> head_{0};
std::atomic<size_t> tail_{0};
};
class AudioLine : public oboe::AudioStreamDataCallback, public oboe::AudioStreamErrorCallback {
public:
AudioLine(bool input, int32_t deviceId, int32_t channels)
: input_(input), deviceId_(deviceId), channels_(channels),
ring_(static_cast<size_t>(kFrameSize) * channels * (input ? kCaptureFrames : kPlaybackFrames)) {}
int32_t channels() const { return channels_; }
oboe::Result open() {
oboe::AudioStreamBuilder builder;
builder.setDirection(input_ ? oboe::Direction::Input : oboe::Direction::Output)
->setSharingMode(oboe::SharingMode::Shared)
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setFormat(oboe::AudioFormat::I16)
->setFormatConversionAllowed(true)
->setChannelCount(channels_)
->setChannelConversionAllowed(true)
->setSampleRate(kSampleRate)
->setSampleRateConversionQuality(oboe::SampleRateConversionQuality::Medium)
->setDataCallback(this)
->setErrorCallback(this);
if (deviceId_ > 0) builder.setDeviceId(deviceId_);
if (input_) {
// The platform's own voice processing stays off: the client runs TS3's.
builder.setInputPreset(oboe::InputPreset::VoiceRecognition);
} else {
builder.setUsage(oboe::Usage::Media)->setContentType(oboe::ContentType::Speech);
}
std::shared_ptr<oboe::AudioStream> stream;
oboe::Result result = builder.openStream(stream);
if (result != oboe::Result::OK) return result;
std::lock_guard<std::mutex> lock(mutex_);
if (closing_.load()) { // closed while a reopen was under way
stream->close();
return oboe::Result::ErrorClosed;
}
stream_ = stream;
return result;
}
oboe::Result start() {
std::lock_guard<std::mutex> lock(mutex_);
return stream_ ? stream_->requestStart() : oboe::Result::ErrorClosed;
}
/** Blocks until {@code count} samples are captured; fewer only once closing. */
size_t read(int16_t *data, size_t count) {
size_t done = 0;
while (done < count && !closing_.load()) {
done += ring_.read(data + done, count - done);
if (done < count) std::this_thread::sleep_for(kPollInterval);
}
return done;
}
/** Blocks until all samples are queued for playback, or the line closes. */
void write(const int16_t *data, size_t count) {
size_t done = 0;
while (done < count && !closing_.load()) {
done += ring_.write(data + done, count - done);
if (done < count) std::this_thread::sleep_for(kPollInterval);
}
}
void close() {
closing_.store(true);
std::lock_guard<std::mutex> lock(mutex_);
if (stream_) {
stream_->stop();
stream_->close();
stream_.reset();
}
}
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *, void *audioData, int32_t numFrames) override {
auto *samples = static_cast<int16_t *>(audioData);
size_t count = static_cast<size_t>(numFrames) * channels_;
if (input_) {
ring_.write(samples, count); // a reader that fell behind loses the newest audio
} else {
size_t played = ring_.read(samples, count);
if (played < count) std::memset(samples + played, 0, (count - played) * sizeof(int16_t));
}
return oboe::DataCallbackResult::Continue;
}
/** The device went away (headset unplugged, route changed): follow onto the new one. */
void onErrorAfterClose(oboe::AudioStream *, oboe::Result error) override {
if (closing_.load()) return;
__android_log_print(ANDROID_LOG_INFO, kTag, "stream closed (%s), reopening",
oboe::convertToText(error));
deviceId_ = 0;
if (open() == oboe::Result::OK) start();
}
private:
const bool input_;
int32_t deviceId_;
const int32_t channels_;
SampleRing ring_;
std::atomic<bool> closing_{false};
std::mutex mutex_;
std::shared_ptr<oboe::AudioStream> stream_;
};
AudioLine *line(jlong handle) {
return reinterpret_cast<AudioLine *>(handle);
}
thread_local std::vector<int16_t> scratch;
} // namespace
extern "C" {
JNIEXPORT jlong JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeOpen(JNIEnv *env, jclass, jboolean input, jint deviceId,
jint channels) {
auto *created = new AudioLine(input, deviceId, channels);
oboe::Result result = created->open();
if (result != oboe::Result::OK) {
delete created;
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"), oboe::convertToText(result));
return 0;
}
return reinterpret_cast<jlong>(created);
}
JNIEXPORT jint JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeChannels(JNIEnv *, jclass, jlong handle) {
return line(handle)->channels();
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeStart(JNIEnv *env, jclass, jlong handle) {
oboe::Result result = line(handle)->start();
if (result != oboe::Result::OK) {
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"), oboe::convertToText(result));
}
}
JNIEXPORT jint JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeRead(JNIEnv *env, jclass, jlong handle, jbyteArray buffer,
jint offset, jint length) {
size_t samples = static_cast<size_t>(length) / sizeof(int16_t);
scratch.resize(samples);
size_t read = line(handle)->read(scratch.data(), samples);
env->SetByteArrayRegion(buffer, offset, static_cast<jsize>(read * sizeof(int16_t)),
reinterpret_cast<const jbyte *>(scratch.data()));
return static_cast<jint>(read * sizeof(int16_t));
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeWrite(JNIEnv *env, jclass, jlong handle, jbyteArray buffer,
jint offset, jint length) {
size_t samples = static_cast<size_t>(length) / sizeof(int16_t);
scratch.resize(samples);
env->GetByteArrayRegion(buffer, offset, static_cast<jsize>(samples * sizeof(int16_t)),
reinterpret_cast<jbyte *>(scratch.data()));
line(handle)->write(scratch.data(), samples);
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeClose(JNIEnv *, jclass, jlong handle) {
line(handle)->close();
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_OboeLine_nativeDelete(JNIEnv *, jclass, jlong handle) {
delete line(handle);
}
} // extern "C"

View File

@@ -0,0 +1,109 @@
// libopus for NativeOpus: handles are the raw encoder/decoder pointers.
#include <jni.h>
#include <opus.h>
#include <string>
#include <vector>
namespace {
constexpr int kMaxPacketBytes = 4096;
void throwOpus(JNIEnv *env, const char *call, int error) {
std::string message = std::string(call) + " failed: " + opus_strerror(error);
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"), message.c_str());
}
OpusEncoder *encoder(jlong handle) { return reinterpret_cast<OpusEncoder *>(handle); }
OpusDecoder *decoder(jlong handle) { return reinterpret_cast<OpusDecoder *>(handle); }
} // namespace
extern "C" {
JNIEXPORT jlong JNICALL
Java_com_ts3client_android_audio_NativeOpus_encoderCreate(JNIEnv *env, jclass, jint sampleRate, jint channels,
jint application) {
int error = OPUS_OK;
OpusEncoder *created = opus_encoder_create(sampleRate, channels, application, &error);
if (error != OPUS_OK) {
throwOpus(env, "opus_encoder_create", error);
return 0;
}
return reinterpret_cast<jlong>(created);
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_NativeOpus_encoderCtl(JNIEnv *env, jclass, jlong handle, jint request,
jint value) {
int result = opus_encoder_ctl(encoder(handle), request, value);
if (result < 0) throwOpus(env, "opus_encoder_ctl", result);
}
JNIEXPORT jbyteArray JNICALL
Java_com_ts3client_android_audio_NativeOpus_encode(JNIEnv *env, jclass, jlong handle, jfloatArray pcm,
jint frameSize) {
unsigned char packet[kMaxPacketBytes];
jfloat *samples = env->GetFloatArrayElements(pcm, nullptr);
int length = opus_encode_float(encoder(handle), samples, frameSize, packet, kMaxPacketBytes);
env->ReleaseFloatArrayElements(pcm, samples, JNI_ABORT);
if (length < 0) {
throwOpus(env, "opus_encode_float", length);
return nullptr;
}
jbyteArray out = env->NewByteArray(length);
env->SetByteArrayRegion(out, 0, length, reinterpret_cast<const jbyte *>(packet));
return out;
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_NativeOpus_encoderDestroy(JNIEnv *, jclass, jlong handle) {
opus_encoder_destroy(encoder(handle));
}
JNIEXPORT jlong JNICALL
Java_com_ts3client_android_audio_NativeOpus_decoderCreate(JNIEnv *env, jclass, jint sampleRate, jint channels) {
int error = OPUS_OK;
OpusDecoder *created = opus_decoder_create(sampleRate, channels, &error);
if (error != OPUS_OK) {
throwOpus(env, "opus_decoder_create", error);
return 0;
}
return reinterpret_cast<jlong>(created);
}
/** Decodes {@code packet}, or conceals {@code frameSize} samples when it is null. */
JNIEXPORT jint JNICALL
Java_com_ts3client_android_audio_NativeOpus_decode(JNIEnv *env, jclass, jlong handle, jbyteArray packet,
jfloatArray out, jint frameSize) {
std::vector<unsigned char> data;
if (packet != nullptr) {
data.resize(static_cast<size_t>(env->GetArrayLength(packet)));
env->GetByteArrayRegion(packet, 0, static_cast<jsize>(data.size()), reinterpret_cast<jbyte *>(data.data()));
}
jfloat *samples = env->GetFloatArrayElements(out, nullptr);
int decoded = opus_decode_float(decoder(handle), packet != nullptr ? data.data() : nullptr,
static_cast<opus_int32>(data.size()), samples, frameSize, 0);
env->ReleaseFloatArrayElements(out, samples, decoded < 0 ? JNI_ABORT : 0);
if (decoded < 0) throwOpus(env, "opus_decode_float", decoded);
return decoded;
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_NativeOpus_decoderReset(JNIEnv *, jclass, jlong handle) {
opus_decoder_ctl(decoder(handle), OPUS_RESET_STATE);
}
JNIEXPORT void JNICALL
Java_com_ts3client_android_audio_NativeOpus_decoderDestroy(JNIEnv *, jclass, jlong handle) {
opus_decoder_destroy(decoder(handle));
}
JNIEXPORT jstring JNICALL
Java_com_ts3client_android_audio_NativeOpus_version0(JNIEnv *env, jclass) {
return env->NewStringUTF(opus_get_version_string());
}
} // extern "C"

View File

@@ -0,0 +1,40 @@
package com.ts3client.android.audio;
import android.content.Context;
import com.ts3client.audio.AudioBackend;
import com.ts3client.audio.AudioIo;
import com.ts3client.audio.StreamingVoiceOutput;
import com.ts3client.audio.opus.OpusCodec;
/** Android's audio stack: Oboe for the devices, libopus built with the NDK. */
public final class AndroidAudioBackend implements AudioBackend {
private final AudioIo io;
private final OpusCodec opus = new NativeOpus();
public AndroidAudioBackend(Context context) {
this.io = new AndroidAudioIo(context.getApplicationContext());
}
@Override
public AudioIo io() {
return io;
}
@Override
public OpusCodec opus() {
return opus;
}
/** One low-latency stream for everyone, as mobile audio wants. */
@Override
public StreamingVoiceOutput.Lines outputLines() {
return StreamingVoiceOutput.Lines.SHARED;
}
@Override
public String description() {
return "Opus " + opus.version() + ", Oboe";
}
}

View File

@@ -0,0 +1,86 @@
package com.ts3client.android.audio;
import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import com.ts3client.audio.AudioCapture;
import com.ts3client.audio.AudioDevice;
import com.ts3client.audio.AudioIo;
import com.ts3client.audio.AudioPlayback;
import java.util.ArrayList;
import java.util.List;
/** The device's microphones and speakers, opened through Oboe. Device ids are Android's. */
final class AndroidAudioIo implements AudioIo {
private final AudioManager audioManager;
AndroidAudioIo(Context context) {
this.audioManager = context.getSystemService(AudioManager.class);
}
@Override
public List<AudioDevice> inputDevices() {
return devices(AudioManager.GET_DEVICES_INPUTS);
}
@Override
public List<AudioDevice> outputDevices() {
return devices(AudioManager.GET_DEVICES_OUTPUTS);
}
private List<AudioDevice> devices(int flags) {
List<AudioDevice> out = new ArrayList<>();
out.add(AudioDevice.DEFAULT);
for (AudioDeviceInfo info : audioManager.getDevices(flags)) {
out.add(new AudioDevice(Integer.toString(info.getId()), info.getProductName() + " (" + typeName(info) + ")"));
}
return out;
}
@Override
public AudioCapture openCapture(String deviceId, int preferredChannels) {
return OboeLine.capture(parseId(deviceId), preferredChannels);
}
@Override
public AudioPlayback openPlayback(String deviceId, int preferredChannels) {
return OboeLine.playback(parseId(deviceId), preferredChannels);
}
/** 0 lets the system choose, as it does for the default device. */
private static int parseId(String deviceId) {
try {
return deviceId == null || deviceId.isEmpty() ? 0 : Integer.parseInt(deviceId);
} catch (NumberFormatException e) {
return 0;
}
}
private static String typeName(AudioDeviceInfo info) {
switch (info.getType()) {
case AudioDeviceInfo.TYPE_BUILTIN_MIC:
return "built-in microphone";
case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
return "speaker";
case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:
return "earpiece";
case AudioDeviceInfo.TYPE_WIRED_HEADSET:
return "wired headset";
case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:
return "wired headphones";
case AudioDeviceInfo.TYPE_USB_HEADSET:
case AudioDeviceInfo.TYPE_USB_DEVICE:
return "USB";
case AudioDeviceInfo.TYPE_BLUETOOTH_SCO:
case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP:
case AudioDeviceInfo.TYPE_BLE_HEADSET:
case AudioDeviceInfo.TYPE_BLE_SPEAKER:
return "Bluetooth";
default:
return "type " + info.getType();
}
}
}

View File

@@ -0,0 +1,114 @@
package com.ts3client.android.audio;
import com.ts3client.audio.opus.CtlOpusEncoder;
import com.ts3client.audio.opus.LibOpus;
import com.ts3client.audio.opus.OpusCodec;
import com.ts3client.audio.opus.OpusDecoder;
import com.ts3client.audio.opus.OpusEncoder;
/** libopus, built into the app with the NDK. */
public final class NativeOpus implements OpusCodec {
static {
System.loadLibrary("ts3audio");
}
@Override
public OpusEncoder createEncoder(int sampleRate, int frameSize, int channels, Application application) {
return new Encoder(encoderCreate(sampleRate, channels, LibOpus.application(application)), frameSize);
}
@Override
public OpusDecoder createDecoder(int sampleRate, int maxFrameSize, int channels) {
return new Decoder(decoderCreate(sampleRate, channels), maxFrameSize);
}
@Override
public String version() {
return version0();
}
/** Methods may come from different threads, one at a time, so they only share the handle. */
private static final class Encoder extends CtlOpusEncoder {
private final int frameSize;
private long handle;
Encoder(long handle, int frameSize) {
this.handle = handle;
this.frameSize = frameSize;
}
@Override
protected synchronized void ctl(int request, int value) {
if (handle != 0) encoderCtl(handle, request, value);
}
@Override
public synchronized byte[] encode(float[] pcm) {
if (handle == 0) throw new IllegalStateException("encoder closed");
return NativeOpus.encode(handle, pcm, frameSize);
}
@Override
public synchronized void close() {
if (handle == 0) return;
encoderDestroy(handle);
handle = 0;
}
}
private static final class Decoder implements OpusDecoder {
private final int maxFrameSize;
private long handle;
Decoder(long handle, int maxFrameSize) {
this.handle = handle;
this.maxFrameSize = maxFrameSize;
}
@Override
public int decode(byte[] packet, float[] out) {
return NativeOpus.decode(live(), packet, out, maxFrameSize);
}
@Override
public int conceal(float[] out, int frameSize) {
return NativeOpus.decode(live(), null, out, Math.min(frameSize, maxFrameSize));
}
@Override
public void reset() {
if (handle != 0) decoderReset(handle);
}
@Override
public void close() {
if (handle == 0) return;
decoderDestroy(handle);
handle = 0;
}
private long live() {
if (handle == 0) throw new IllegalStateException("decoder closed");
return handle;
}
}
private static native long encoderCreate(int sampleRate, int channels, int application);
private static native void encoderCtl(long handle, int request, int value);
private static native byte[] encode(long handle, float[] pcm, int frameSize);
private static native void encoderDestroy(long handle);
private static native long decoderCreate(int sampleRate, int channels);
private static native int decode(long handle, byte[] packet, float[] out, int frameSize);
private static native void decoderReset(long handle);
private static native void decoderDestroy(long handle);
private static native String version0();
}

View File

@@ -0,0 +1,91 @@
package com.ts3client.android.audio;
import com.ts3client.audio.AudioCapture;
import com.ts3client.audio.AudioPlayback;
/**
* An Oboe stream, as either an {@link AudioCapture} or an {@link AudioPlayback}. The native
* side keeps a ring between the real-time callback and these blocking calls.
*/
final class OboeLine implements AudioCapture, AudioPlayback {
static {
System.loadLibrary("ts3audio");
}
/** Held across native reads and writes, so {@link #close} never frees a line in use. */
private final Object io = new Object();
private final int channels;
private volatile long handle;
private OboeLine(boolean input, int deviceId, int channels) {
this.handle = nativeOpen(input, deviceId, channels);
this.channels = nativeChannels(handle);
}
static OboeLine capture(int deviceId, int channels) {
return new OboeLine(true, deviceId, channels);
}
static OboeLine playback(int deviceId, int channels) {
return new OboeLine(false, deviceId, channels);
}
@Override
public int channels() {
return channels;
}
@Override
public void start() {
long h = handle;
if (h != 0) nativeStart(h);
}
@Override
public int read(byte[] buffer, int offset, int length) {
synchronized (io) {
long h = handle;
return h == 0 ? 0 : nativeRead(h, buffer, offset, length);
}
}
@Override
public void write(byte[] buffer, int offset, int length) {
synchronized (io) {
long h = handle;
if (h != 0) nativeWrite(h, buffer, offset, length);
}
}
/** Oboe has no drain; what is queued is at most a few frames and simply plays out. */
@Override
public void drain() {
}
@Override
public void close() {
long h = handle;
if (h == 0) return;
nativeClose(h); // wakes a blocked read or write
synchronized (io) {
if (handle == 0) return;
handle = 0;
nativeDelete(h);
}
}
private static native long nativeOpen(boolean input, int deviceId, int channels);
private static native int nativeChannels(long handle);
private static native void nativeStart(long handle);
private static native int nativeRead(long handle, byte[] buffer, int offset, int length);
private static native void nativeWrite(long handle, byte[] buffer, int offset, int length);
private static native void nativeClose(long handle);
private static native void nativeDelete(long handle);
}