// libopus for NativeOpus: handles are the raw encoder/decoder pointers. #include #include #include #include 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(handle); } OpusDecoder *decoder(jlong handle) { return reinterpret_cast(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(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(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(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 data; if (packet != nullptr) { data.resize(static_cast(env->GetArrayLength(packet))); env->GetByteArrayRegion(packet, 0, static_cast(data.size()), reinterpret_cast(data.data())); } jfloat *samples = env->GetFloatArrayElements(out, nullptr); int decoded = opus_decode_float(decoder(handle), packet != nullptr ? data.data() : nullptr, static_cast(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"