Mercurial > hg > pulseaudio
changeset 51:5c09d21c2d70
Added a Operation object to wrap pa_operation_*. Using it to free memory whenever async Operations are performed. Also added a new method to convert a pointer to a java long
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Fri, 08 Aug 2008 17:28:30 -0400 |
parents | 61cf6f811619 |
children | ab743c774cb2 |
files | makefile src/jni-common.c src/jni-common.h src/org/classpath/icedtea/pulseaudio/Operation.java src/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java src/org_classpath_icedtea_pulseaudio_EventLoop.c src/org_classpath_icedtea_pulseaudio_Operation.c src/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c |
diffstat | 8 files changed, 151 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
--- a/makefile Fri Aug 08 16:24:38 2008 -0400 +++ b/makefile Fri Aug 08 17:28:30 2008 -0400 @@ -35,8 +35,8 @@ bin/org_classpath_icedtea_pulseaudio_EventLoop.o \ bin/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.o \ bin/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.o \ + bin/org_classpath_icedtea_pulseaudio_Operation.o \ bin/jni-common.o -# bin/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.o \ # bin/org_classpath_icedtea_pulseaudio_PulseAudioTargetDataLine.o \ gcc -g -shared -o $@ $^ /usr/lib/libpulse.so @@ -54,7 +54,7 @@ bin/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.o: src/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.c src/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.h gcc $(CFLAGS) $(PLATFORM_FLAGS) -c -o $@ $< -bin/org_classpath_icedtea_pulseaudio_StreamVolume.o: src/org_classpath_icedtea_pulseaudio_StreamVolume.c src/org_classpath_icedtea_pulseaudio_StreamVolume.h +bin/org_classpath_icedtea_pulseaudio_Operation.o: src/org_classpath_icedtea_pulseaudio_Operation.c src/org_classpath_icedtea_pulseaudio_Operation.h gcc $(CFLAGS) $(PLATFORM_FLAGS) -c -o $@ $< bin/jni-common.o: src/jni-common.c src/jni-common.h @@ -74,8 +74,8 @@ src/org_classpath_icedtea_pulseaudio_PulseAudioStreamVolumeControl.h: src/org/classpath/icedtea/pulseaudio/PulseAudioStreamVolumeControl.class javah -d src -classpath src org.classpath.icedtea.pulseaudio.PulseAudioStreamVolumeControl -src/org_classpath_icedtea_pulseaudio_StreamVolume.h: src/org/classpath/icedtea/pulseaudio/StreamVolume.class - javah -d src -classpath src org.classpath.icedtea.pulseaudio.StreamVolume +src/org_classpath_icedtea_pulseaudio_Operation.h: src/org/classpath/icedtea/pulseaudio/Operation.class + javah -d src -classpath src org.classpath.icedtea.pulseaudio.Operation # Compile Java # Actually, this is not the best thing to do; javac might do some crazy things
--- a/src/jni-common.c Fri Aug 08 16:24:38 2008 -0400 +++ b/src/jni-common.c Fri Aug 08 17:28:30 2008 -0400 @@ -85,6 +85,16 @@ (*env)->SetLongField(env, obj, fid, value); } + +/* + * + * + * The functions below are going to cause headaches when porting + * + * + */ + + void* getJavaPointer(JNIEnv* env, jobject obj, char* name) { /* @@ -155,10 +165,15 @@ } -void* convertFromJavaPointer(jlong pointer) { +void* convertJavaLongToPointer(jlong pointer) { /* this truncates the jlong */ jPointer p = pointer; return (void*) p; } + +jlong convertPointerToJavaLong(void* pointer) { + return (long) pointer; + +}
--- a/src/jni-common.h Fri Aug 08 16:24:38 2008 -0400 +++ b/src/jni-common.h Fri Aug 08 17:28:30 2008 -0400 @@ -72,8 +72,8 @@ void* getJavaPointer(JNIEnv* env, jobject obj, char* name); void setJavaPointer(JNIEnv* env, jobject obj, char*name, void* pointer_value); -void* convertFromJavaPointer(jlong pointer); - +void* convertJavaLongToPointer(jlong pointer); +jlong convertPointerToJavaLong(void* pointer); #endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/classpath/icedtea/pulseaudio/Operation.java Fri Aug 08 17:28:30 2008 -0400 @@ -0,0 +1,56 @@ +package org.classpath.icedtea.pulseaudio; + +/* + * Encapsulates a pa_operation object + * + * + * This is really needed only so that we can deallocate the reference counted object + * + * + */ + +public class Operation { + + long operationPointer; + + public enum State { + Running, Done, Cancelled, + } + + private native void native_ref(); + + private native void native_unref(); + + private native int native_get_state(); + + public Operation(long operationPointer) { + this.operationPointer = operationPointer; + } + + public void addReference() { + assert(operationPointer != 0); + native_ref(); + } + + public void releaseReference() { + assert(operationPointer!= 0); + native_unref(); + } + + public State getState() { + assert(operationPointer!= 0); + int state = native_get_state(); + switch (state) { + case 0: + return State.Running; + case 1: + return State.Done; + case 2: + return State.Cancelled; + default: + throw new IllegalStateException("Invalid operation State"); + } + + } + +}
--- a/src/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java Fri Aug 08 16:24:38 2008 -0400 +++ b/src/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java Fri Aug 08 17:28:30 2008 -0400 @@ -95,9 +95,7 @@ private native int native_get_writable_size(); - private native int native_getOperationState(int operationState); - - private native int native_flush(); + private native long native_flush(); private native void native_start(); @@ -105,7 +103,7 @@ private native void native_resume(); - private native int native_drain(); + private native long native_drain(); private native void native_close(); @@ -555,33 +553,39 @@ @Override public void drain() { - int operationPointer; - int operationState; + Operation operation; + Operation.State operationState; synchronized (eventLoop.threadLock) { - operationPointer = native_drain(); - operationState = native_getOperationState(operationPointer); + operation = new Operation(native_drain()); + operationState = operation.getState(); } - while (operationState != 1) { + + // FIXME need to find a way to do a wait than a busy loop + while (operationState != Operation.State.Done) { synchronized (eventLoop.threadLock) { - operationState = native_getOperationState(operationPointer); + operationState = operation.getState(); } } + operation.releaseReference(); } @Override public void flush() { - int operationPointer; - int operationState; + Operation operation; + Operation.State operationState; synchronized (eventLoop.threadLock) { - operationPointer = native_flush(); - operationState = native_getOperationState(operationPointer); + operation = new Operation(native_flush()); + operationState = operation.getState(); } - while (operationState != 1) { + // FIXME need to find a way to do a wait than a busy loop + while (operationState != Operation.State.Done) { synchronized (eventLoop.threadLock) { - operationState = native_getOperationState(operationPointer); + operationState = operation.getState(); } } + + operation.releaseReference(); }
--- a/src/org_classpath_icedtea_pulseaudio_EventLoop.c Fri Aug 08 16:24:38 2008 -0400 +++ b/src/org_classpath_icedtea_pulseaudio_EventLoop.c Fri Aug 08 17:28:30 2008 -0400 @@ -262,7 +262,7 @@ (JNIEnv* env, jobject obj, jlong streamPointer, jint volume) { int* new_volume = malloc(sizeof(int)); *new_volume = volume; - int stream_id = pa_stream_get_index((pa_stream*) convertFromJavaPointer(streamPointer)); + int stream_id = pa_stream_get_index((pa_stream*) convertJavaLongToPointer(streamPointer)); pa_context_get_sink_input_info((pa_context*) getJavaPointer(env, obj,"contextPointer") ,stream_id,sink_input_change_volume, new_volume); return; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org_classpath_icedtea_pulseaudio_Operation.c Fri Aug 08 17:28:30 2008 -0400 @@ -0,0 +1,43 @@ +#include "org_classpath_icedtea_pulseaudio_Operation.h" + +#include "jni-common.h" +#include <pulse/pulseaudio.h> + +/* + * Class: org_classpath_icedtea_pulseaudio_Operation + * Method: native_ref + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_classpath_icedtea_pulseaudio_Operation_native_1ref +(JNIEnv* env, jobject obj) { + + pa_operation* operation = (pa_operation*) getJavaPointer(env, obj, "operationPointer"); + pa_operation_ref(operation); + +} + +/* + * Class: org_classpath_icedtea_pulseaudio_Operation + * Method: native_unref + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_classpath_icedtea_pulseaudio_Operation_native_1unref +(JNIEnv* env, jobject obj) { + + pa_operation* operation = (pa_operation*) getJavaPointer(env, obj, "operationPointer"); + pa_operation_unref(operation); + +} + +/* + * Class: org_classpath_icedtea_pulseaudio_Operation + * Method: native_get_state + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_classpath_icedtea_pulseaudio_Operation_native_1get_1state +(JNIEnv* env, jobject obj) { + + pa_operation* operation = (pa_operation*) getJavaPointer(env, obj, "operationPointer"); + int state = pa_operation_get_state(operation); + return state; +}
--- a/src/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c Fri Aug 08 16:24:38 2008 -0400 +++ b/src/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c Fri Aug 08 17:28:30 2008 -0400 @@ -113,7 +113,7 @@ java_context->env = env; java_context->obj = (*env)->NewGlobalRef(env, obj); - pa_context* context = (pa_context*) convertFromJavaPointer(contextPointer); + pa_context* context = (pa_context*) convertJavaLongToPointer(contextPointer); assert(context != NULL); pa_sample_spec sample_spec; @@ -210,28 +210,17 @@ /* * Class: org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine * Method: native_flush - * Signature: ()I + * Signature: ()J */ -JNIEXPORT jint JNICALL Java_org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine_native_1flush -(JNIEnv* env, jobject obj) { - +JNIEXPORT jlong JNICALL Java_org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine_native_1flush +(JNIEnv* env, jobject obj) +{ pa_stream *stream = (pa_stream*) getJavaPointer(env, obj, "streamPointer"); pa_operation *o = pa_stream_flush(stream, NULL, NULL); - return (int) o; + return convertPointerToJavaLong(o); } -/* - * Class: org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine - * Method: native_getOperationState - * Signature: (I)I - */ -JNIEXPORT jint JNICALL Java_org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine_native_1getOperationState -(JNIEnv *env, jobject obj, jint operation) { - - return pa_operation_get_state((pa_operation *) operation); - -} /* * Class: org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine @@ -269,14 +258,14 @@ /* * Class: org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine * Method: native_drain - * Signature: ()I + * Signature: ()J */ -JNIEXPORT jint JNICALL Java_org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine_native_1drain +JNIEXPORT jlong JNICALL Java_org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine_native_1drain (JNIEnv* env, jobject obj) { pa_stream *stream = (pa_stream*) getJavaPointer(env, obj, "streamPointer"); assert(stream); pa_operation *o = pa_stream_drain(stream, NULL, NULL); - return (int) o; + return convertPointerToJavaLong(o); } /*