Commit 08d23ced authored by Daniel Sonck's avatar Daniel Sonck
Browse files

Merge pull request #22 in THFM/fm.touhou.touhoufm from bugfix/THFMA-15 to development

* commit 'dd368283':
  Fix memory leak in opusJni
parents 5ffdf2c9 dd368283
......@@ -24,8 +24,6 @@
#include "opusJni.hpp"
#include "opus.h"
JavaVM *gJavaVM = NULL;
jobject gJavaOpusDecoderClass;
const char* kJavaOpusDecoderClassPath = "fm/touhou/touhoufm/utils/OpusDecoder";
static JNINativeMethod methodTable[] = {
......@@ -44,7 +42,7 @@ int FRAME_SIZE;
extern "C" {
jint JNI_OnLoad(JavaVM *aVm, void *reserved) {
gJavaVM = aVm;
(void)reserved;
JNIEnv *env;
......@@ -59,7 +57,7 @@ jint JNI_OnLoad(JavaVM *aVm, void *reserved) {
return -1;
}
gJavaOpusDecoderClass = env->NewGlobalRef(opusDecoderClass);
env->NewGlobalRef(opusDecoderClass);
env->RegisterNatives(opusDecoderClass, methodTable, sizeof(methodTable) / sizeof(methodTable[0]));
......@@ -78,12 +76,9 @@ jint initDecoder(JNIEnv *env, jobject obj, jint sampleRate, jint channels, jint
SAMPLING_RATE = sampleRate;
CHANNELS = channels;
int size;
int error;
size = opus_decoder_get_size(channels);
gOpusDecoder = (OpusDecoder*)malloc(size);
error = opus_decoder_init(gOpusDecoder, SAMPLING_RATE, CHANNELS);
gOpusDecoder = opus_decoder_create(SAMPLING_RATE, channels, &error);
sprintf(logMsg, "Initialized Decoder with ErrorCode: %d", error);
__android_log_write(ANDROID_LOG_DEBUG, "libopus", logMsg);
......@@ -97,15 +92,15 @@ jint decodeBytes(JNIEnv *env, jobject obj, jbyteArray in, jshortArray out) {
jint inputArraySize = env->GetArrayLength(in);
jint outputArraySize = env->GetArrayLength(out);
jbyte *encodedData = env->GetByteArrayElements(in, 0);
jbyte *encodedData = env->GetByteArrayElements(in, nullptr);
opus_int16 *data = new opus_int16[outputArraySize];
int decodedDataArraySize = opus_decode(gOpusDecoder, (const unsigned char *) encodedData, inputArraySize, data, FRAME_SIZE, 0);
int decodedDataArraySize = opus_decode(gOpusDecoder, static_cast<const unsigned char *>( encodedData ), inputArraySize, data, FRAME_SIZE, 0);
if(decodedDataArraySize >= 0) {
if(decodedDataArraySize * 2 <= outputArraySize) {
if(decodedDataArraySize > 0) {
if(decodedDataArraySize * CHANNELS <= outputArraySize) {
env->SetShortArrayRegion(out, 0, decodedDataArraySize * CHANNELS, data);
} else {
sprintf(logMsg, "Output array of size: %d to small for storing encoded data.",
sprintf(logMsg, "Output array of size: %d to small for storing decoded data.",
outputArraySize);
__android_log_write(ANDROID_LOG_DEBUG, "Native Code:", logMsg);
......@@ -113,6 +108,8 @@ jint decodeBytes(JNIEnv *env, jobject obj, jbyteArray in, jshortArray out) {
}
}
delete[] data;
env->ReleaseByteArrayElements(in, encodedData, JNI_ABORT);
return decodedDataArraySize;
......@@ -124,6 +121,8 @@ jboolean releaseDecoder(JNIEnv *env, jobject obj) {
__android_log_write(ANDROID_LOG_DEBUG, "libopus", "Released Opus Decoder");
opus_decoder_destroy(gOpusDecoder);
free(gOpusDecoder);
return 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment