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:
@@ -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"
|
||||
Reference in New Issue
Block a user