Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ static int FileDesc_seek(void *datasource, ogg_int64_t offset, int whence)
wrapper->current = actual_offset;
}

static int FileDesc_close(void *datasource)
static int FileDesc_clear(void *datasource)
{
FileDescWrapper* wrapper = (FileDescWrapper*)datasource;

LOGI("FD close");

return close(wrapper->fd);
LOGI("Clear resources -- delegating closure to the Android ParcelFileDescriptor");

/* release the file descriptor wrapper buffer */
free(wrapper);

wrapper = NULL;

return 0;
}

static long FileDesc_tell(void *datasource)
Expand All @@ -139,7 +144,7 @@ static long FileDesc_tell(void *datasource)
static ov_callbacks FileDescCallbacks = {
FileDesc_read,
FileDesc_seek,
FileDesc_close,
FileDesc_clear,
FileDesc_tell
};

Expand All @@ -157,10 +162,10 @@ static jfieldID nvf_field_bitRate;
static jfieldID nvf_field_totalBytes;
static jfieldID nvf_field_duration;

JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_nativeInit
JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_preInit
(JNIEnv *env, jclass clazz)
{
LOGI("nativeInit");
LOGI("preInit");

nvf_field_ovf = (*env)->GetFieldID(env, clazz, "ovf", "Ljava/nio/ByteBuffer;");;
nvf_field_seekable = (*env)->GetFieldID(env, clazz, "seekable", "Z");
Expand All @@ -171,7 +176,7 @@ JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_nativeInit
nvf_field_duration = (*env)->GetFieldID(env, clazz, "duration", "F");
}

JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_open
JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_init
(JNIEnv *env, jobject nvf, jint fd, jlong off, jlong len)
{
LOGI("open: fd = %d, off = %lld, len = %lld", fd, off, len);
Expand Down Expand Up @@ -330,19 +335,19 @@ JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_readFully
}
}

JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_close
JNIEXPORT void JNICALL Java_com_jme3_audio_plugins_NativeVorbisFile_clearResources
(JNIEnv *env, jobject nvf)
{
LOGI("close");
LOGI("clearResources");

jobject ovfBuf = (*env)->GetObjectField(env, nvf, nvf_field_ovf);
OggVorbis_File* ovf = (OggVorbis_File*) (*env)->GetDirectBufferAddress(env, ovfBuf);
FileDescWrapper* wrapper = (FileDescWrapper*) ovf->datasource;
wrapper->env = env;

/* release the ovf resources */
ov_clear(ovf);

free(wrapper);
/* release the ovf buffer */
free(ovf);
ovf = NULL;
/* destroy the java reference object */
(*env)->SetObjectField(env, nvf, nvf_field_ovf, NULL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@ public class NativeVorbisFile {

static {
System.loadLibrary("decodejme");
nativeInit();
preInit();
}

public NativeVorbisFile(int fd, long off, long len) throws IOException {
open(fd, off, len);
/**
* Initializes an ogg vorbis native file from a file descriptor [fd].
*
* @param fd an integer representing the file descriptor
* @param offset an integer representing the start of the
* @param length an integer representing the length of the
* @throws IOException
*/
public NativeVorbisFile(int fd, long offset, long length) throws IOException {
init(fd, offset, length);
}

private native void open(int fd, long off, long len) throws IOException;
private native void init(int fd, long offset, long length) throws IOException;

public native void seekTime(double time) throws IOException;

public native int read(byte[] buf, int off, int len) throws IOException;

public native void readFully(ByteBuffer out) throws IOException;

public native void close();
/**
* Clears the native resources by calling free() destroying the structure defining this buffer.
*/
public native void clearResources();

public static native void nativeInit();
public static native void preInit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void setTime(float time) {

@Override
public void close() throws IOException {
file.close();
file.clearResources();
afd.close();
}
}
Expand All @@ -78,7 +78,7 @@ private static AudioBuffer loadBuffer(AssetInfo assetInfo) throws IOException {
return ab;
} finally {
if (file != null) {
file.close();
file.clearResources();
}
if (afd != null) {
afd.close();
Expand Down Expand Up @@ -107,7 +107,7 @@ private static AudioStream loadStream(AssetInfo assetInfo) throws IOException {
} finally {
if (!success) {
if (file != null) {
file.close();
file.clearResources();
}
if (afd != null) {
afd.close();
Expand Down