changeset 164:e688b471726e

2008-10-07 Omair Majid <omajid@redhat.com> * src/native/jni-common.h Grouped related functions together. Added sections in file. Added two constants ILLEGAL_STATE_EXCEPTION and ILLEGAL_ARGUMENT_EXCEPTION to make it easiser to throw exceptions. * src/native/org_classpath_icedtea_pulseaudio_Stream.c (Java_org_classpath_icedtea_pulseaudio_Stream_native_1pa_1stream_1read): Removed commented function. (drain_callback): Throw an exception instaed of an assert if the operation failed. (cork_callback): Likewise. (flush_callback): Likewise. (trigger_callback): Likewise. (set_name_callback): Likewise. (set_buffer_attr_callback): Likewise. (update_sample_rate_callback): Likewise
author Omair Majid <omajid@redhat.com>
date Tue, 07 Oct 2008 11:31:39 -0400
parents 6695cbc7fe43
children d7c213ee6717
files src/native/jni-common.h src/native/org_classpath_icedtea_pulseaudio_Stream.c
diffstat 2 files changed, 62 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/native/jni-common.h	Mon Oct 06 17:03:22 2008 -0400
+++ b/src/native/jni-common.h	Tue Oct 07 11:31:39 2008 -0400
@@ -49,6 +49,21 @@
 	jobject obj;
 } java_context_t;
 
+/* Exception Handling */
+
+void throwByName(JNIEnv* const env, const char* const name,
+		const char* const msg);
+
+#define ILLEGAL_ARGUMENT_EXCEPTION "java/lang/IllegalArgumentException"
+#define ILLEGAL_STATE_EXCEPTION "java/lang/IllegalStateException"
+
+/* Threading and Synchronization */
+
+jobject getLockObject(JNIEnv* env);
+void notifyWaitingOperations(JNIEnv* env);
+
+/* Storing and Loading Values */
+
 jint getJavaIntField(JNIEnv* env, jobject obj, char* fieldName);
 void setJavaIntField(JNIEnv* env, jobject obj, char* fieldName, jint value);
 
@@ -59,13 +74,7 @@
 void setJavaByteArrayField(JNIEnv* env, jobject obj, char* name,
 		jbyteArray array);
 
-void callJavaVoidMethod(JNIEnv* env, jobject obj, const char* method_name);
-
-void throwByName(JNIEnv* const env, const char* const name,
-		const char* const msg);
-
-jobject getLockObject(JNIEnv* env);
-void notifyWaitingOperations(JNIEnv* env);
+/* Pointers and Java */
 
 void* getJavaPointer(JNIEnv* env, jobject obj, char* name);
 void setJavaPointer(JNIEnv* env, jobject obj, char*name, void* pointer_value);
@@ -73,5 +82,9 @@
 void* convertJavaPointerToNative(JNIEnv* env, jbyteArray pointer);
 jbyteArray convertNativePointerToJava(JNIEnv* env, void* pointer);
 
+/* Calling Java Functions */
+
+void callJavaVoidMethod(JNIEnv* env, jobject obj, const char* method_name);
+
 #endif
 
--- a/src/native/org_classpath_icedtea_pulseaudio_Stream.c	Mon Oct 06 17:03:22 2008 -0400
+++ b/src/native/org_classpath_icedtea_pulseaudio_Stream.c	Tue Oct 07 11:31:39 2008 -0400
@@ -401,16 +401,15 @@
 		jbyteArray volumePointer, jbyteArray sync_streamPointer) {
 
 	pa_stream *sync_stream;
-        if(sync_streamPointer != NULL) {
-        	sync_stream = convertJavaPointerToNative(env, sync_streamPointer);
-        	printf("Master stream is %p\n", sync_stream);
-        } else {
-        	sync_stream = NULL;
-        }
+	if(sync_streamPointer != NULL) {
+		sync_stream = convertJavaPointerToNative(env, sync_streamPointer);
+		printf("Master stream is %p\n", sync_stream);
+	} else {
+		sync_stream = NULL;
+	}
 
 	pa_stream* stream = (pa_stream*) getJavaPointer(env, obj, STREAM_POINTER);
 
-
 	pa_buffer_attr buffer_attr;
 
 	memset(&buffer_attr, 0, sizeof(buffer_attr));
@@ -436,8 +435,8 @@
 		}
 	}
 	/* Set flags to 0 to fix problem with draining before calling start, might need to
-	be changed back to PA_STREAM_START_CORKED in the future, if we'll be able to implement
-	synchronization*/ 
+	 be changed back to PA_STREAM_START_CORKED in the future, if we'll be able to implement
+	 synchronization*/
 	int value = pa_stream_connect_playback(stream, dev, &buffer_attr, 0, NULL, sync_stream);
 
 	if (dev != NULL) {
@@ -526,24 +525,6 @@
 }
 
 /*
-JNIEXPORT jint JNICALL Java_org_classpath_icedtea_pulseaudio_Stream_native_1pa_1stream_1read
-(JNIEnv *env, jobject obj, jbyteArray array, jint length, jint offset) {
-	pa_stream *stream = getJavaPointer(env, obj, STREAM_POINTER);
-	assert(stream);
-	const void *read_data = NULL;
-	size_t read_length = 0;
-	pa_stream_peek(stream, &read_data, &read_length);
-	if (length < read_length) {
-		read_length = length;
-	}
-
-	(*env)->SetByteArrayRegion(env, array, offset, read_length, read_data);
-	pa_stream_drop(stream);
-	return read_length;
-}
-*/
-
-/*
  * Class:     org_classpath_icedtea_pulseaudio_Stream
  * Method:    native_pa_stream_peek
  * Signature: ()[B
@@ -564,7 +545,7 @@
 	if (startLocation == NULL) {
 		return NULL;
 	}
-	
+
 	jsize length = count;
 	jbyteArray data = (*env)->NewByteArray(env, length);
 
@@ -616,11 +597,16 @@
 
 static void drain_callback(pa_stream* stream, int success, void* userdata) {
 
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
+
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGA_STATE_EXCEPTION, "drain failed");
+	}
+
 }
 
 /*
@@ -654,11 +640,14 @@
 	java_context* context = userdata;
 	assert(stream);
 	assert(context);
-	assert(success);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGAL_STATE_EXCEPTION, "cork failed");
+	}
+
 }
 
 /*
@@ -680,11 +669,15 @@
 }
 
 static void flush_callback(pa_stream* stream, int success, void* userdata) {
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGAL_STATE_EXCEPTION, "flush failed");
+	}
+
 }
 
 /*
@@ -702,11 +695,15 @@
 }
 
 static void trigger_callback(pa_stream* stream, int success, void* userdata) {
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGAL_STATE_EXCEPTION, "trigger failed");
+	}
+
 }
 
 /*
@@ -724,10 +721,13 @@
 }
 
 static void set_name_callback(pa_stream* stream, int success, void* userdata) {
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGAL_STATE_EXCEPTION, "set_name failed");
+	}
 }
 
 /*
@@ -856,14 +856,14 @@
 static void set_buffer_attr_callback(pa_stream* stream, int success,
 		void* userdata) {
 
-	const pa_buffer_attr* buffer = pa_stream_get_buffer_attr(stream);
-	assert(buffer);
-
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(env, ILLEGAL_STATE_EXCEPTION, "set_buffer_attr failed");
+	}
 }
 
 /*
@@ -918,11 +918,15 @@
 
 static void update_sample_rate_callback(pa_stream* stream, int success,
 		void* userdata) {
-	assert(success);
+	assert(stream);
 	JNIEnv* env = pulse_thread_env;
 	assert(env);
 	notifyWaitingOperations(env);
 
+	if (success == 0) {
+		throwByName(eng, ILLEGAL_STATE_EXCEPTION, "update_sampl_rate failed");
+	}
+
 }
 /*
  * Class:     org_classpath_icedtea_pulseaudio_Stream