Skip to content

Commit 8aa9a40

Browse files
committed
don't leak state in JNI
1 parent 8e55282 commit 8aa9a40

2 files changed

Lines changed: 6 additions & 30 deletions

File tree

library/src/androidMain/kotlin/coredevices/speex/SpeexCodec.android.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ actual class SpeexCodec actual constructor(
77
private val bitRate: Int,
88
private val frameSize: Int
99
): AutoCloseable {
10-
private val gain = 1.0f
11-
enum class Preprocessor(val flagValue: Int) {
12-
DENOISE(1),
13-
AGC(2),
14-
VAD(4)
15-
}
16-
17-
init {
18-
initNative()
19-
}
2010
private val speexDecBits: Long = initSpeexBits()
2111
private val speexDecState: Long = initDecState(sampleRate, bitRate)
2212

@@ -28,7 +18,7 @@ actual class SpeexCodec actual constructor(
2818
*/
2919
actual fun decodeFrame(encodedFrame: ByteArray, decodedFrame: ByteArray, hasHeaderByte: Boolean): SpeexDecodeResult {
3020
val decodedFrameBuf = ByteBuffer.allocateDirect(decodedFrame.size)
31-
val result = SpeexDecodeResult.fromInt(decode(encodedFrame, decodedFrameBuf, hasHeaderByte))
21+
val result = SpeexDecodeResult.fromInt(decode(encodedFrame, decodedFrameBuf, this.speexDecBits, this.speexDecState, hasHeaderByte))
3222
if (result == SpeexDecodeResult.Success) {
3323
decodedFrameBuf.get(decodedFrame, 0, decodedFrame.size)
3424
}
@@ -39,9 +29,7 @@ actual class SpeexCodec actual constructor(
3929
destroySpeexBits(speexDecBits)
4030
destroyDecState(speexDecState)
4131
}
42-
43-
private external fun initNative()
44-
private external fun decode(encodedFrame: ByteArray, decodedFrame: ByteBuffer, hasHeaderByte: Boolean): Int
32+
private external fun decode(encodedFrame: ByteArray, decodedFrame: ByteBuffer, speexDecBits: Long, speexDecState: Long, hasHeaderByte: Boolean): Int
4533
private external fun initSpeexBits(): Long
4634
private external fun initDecState(sampleRate: Long, bitRate: Int): Long
4735
private external fun destroySpeexBits(speexBits: Long)

speex/speex_codec_jni.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
#include <speex/speex.h>
44
//#include <speex/speex_preprocess.h>
55

6-
static jfieldID speexDecBits;
7-
static jfieldID speexDecState;
8-
96
extern "C"
107
JNIEXPORT jint
118

129
JNICALL
1310
Java_coredevices_speex_SpeexCodec_decode(JNIEnv *env, jobject thiz,
14-
jbyteArray encoded_frame, jobject out_frame, jboolean has_header_byte) {
11+
jbyteArray encoded_frame, jobject out_frame, jlong speexDecBits, jlong speexDecState, jboolean has_header_byte) {
1512
jbyte *encoded_frame_data = env->GetByteArrayElements(encoded_frame, nullptr);
1613
jsize encoded_frame_length = env->GetArrayLength(encoded_frame);
1714
auto *out_frame_data = reinterpret_cast<spx_int16_t *>(env->GetDirectBufferAddress(out_frame));
18-
auto *bits = reinterpret_cast<SpeexBits *>(env->GetLongField(thiz, speexDecBits));
19-
auto *dec_state = reinterpret_cast<void *>(env->GetLongField(thiz, speexDecState));
15+
auto clazz = env->GetObjectClass(thiz);
16+
auto *bits = reinterpret_cast<SpeexBits *>(speexDecBits);
17+
auto *dec_state = reinterpret_cast<void *>(speexDecState);
2018
int offset = has_header_byte ? 1 : 0;
2119
speex_bits_read_from(bits, reinterpret_cast<char *>(encoded_frame_data) + offset, encoded_frame_length - offset);
2220
int result = speex_decode_int(dec_state, bits, out_frame_data);
@@ -64,14 +62,4 @@ Java_coredevices_speex_SpeexCodec_destroyDecState(
6462
) {
6563
auto *state = reinterpret_cast<void *>(dec_state);
6664
speex_decoder_destroy(state);
67-
}
68-
extern "C"
69-
JNIEXPORT void JNICALL
70-
Java_coredevices_speex_SpeexCodec_initNative(
71-
JNIEnv *env,
72-
jobject thiz
73-
) {
74-
jclass clazz = env->GetObjectClass(thiz);
75-
speexDecBits = env->GetFieldID(clazz, "speexDecBits", "J");
76-
speexDecState = env->GetFieldID(clazz, "speexDecState", "J");
7765
}

0 commit comments

Comments
 (0)