Files
ts3java/android/audio/src/main/cpp/opus_jni.cpp
T
erikbrockoandClaude Opus 5.5 78fc45f9b2 Rename to TeamSpook TS3j, package eu.lixko.ts3j.client
Java/Kotlin sources move from com.ts3client to eu.lixko.ts3j.client, and
the Android application id becomes eu.lixko.ts3j.client. The product name
shows as "TeamSpook TS3j" in window titles, the tray, the launcher entries,
notifications and PipeWire streams.

The desktop profile directory, WM_CLASS and ts3j.* system properties keep
their names so existing settings and launchers carry on working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 14:43:34 +00:00

113 lines
4.3 KiB
C++

// 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_eu_lixko_ts3j_client_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_eu_lixko_ts3j_client_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_eu_lixko_ts3j_client_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_eu_lixko_ts3j_client_android_audio_NativeOpus_encoderDestroy(JNIEnv *, jclass, jlong handle) {
opus_encoder_destroy(encoder(handle));
}
JNIEXPORT jlong JNICALL
Java_eu_lixko_ts3j_client_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; with {@code fec},
* rebuilds the packet before {@code packet} from its error correction copy.
*/
JNIEXPORT jint JNICALL
Java_eu_lixko_ts3j_client_android_audio_NativeOpus_decode(JNIEnv *env, jclass, jlong handle, jbyteArray packet,
jfloatArray out, jint frameSize, jboolean fec) {
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, fec ? 1 : 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_eu_lixko_ts3j_client_android_audio_NativeOpus_decoderReset(JNIEnv *, jclass, jlong handle) {
opus_decoder_ctl(decoder(handle), OPUS_RESET_STATE);
}
JNIEXPORT void JNICALL
Java_eu_lixko_ts3j_client_android_audio_NativeOpus_decoderDestroy(JNIEnv *, jclass, jlong handle) {
opus_decoder_destroy(decoder(handle));
}
JNIEXPORT jstring JNICALL
Java_eu_lixko_ts3j_client_android_audio_NativeOpus_version0(JNIEnv *env, jclass) {
return env->NewStringUTF(opus_get_version_string());
}
} // extern "C"