changeset 68:9a72b909f18a

2008-08-12 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/Operation.java: added a function isNull to check if the pa_operation pointer is NULL * src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java: added support for LineListeners to be notified on a CLOSE event * src/native/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c: removed debugging output * unittests/org/classpath/icedtea/pulseaudio/PulseSourceDataLineTest.java: added description to be println()ed for testPlay() test
author Omair Majid <omajid@redhat.com>
date Tue, 12 Aug 2008 16:55:28 -0400
parents 856e11044f24
children c054a0681a49
files src/java/org/classpath/icedtea/pulseaudio/Operation.java src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java src/native/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c unittests/org/classpath/icedtea/pulseaudio/PulseSourceDataLineTest.java
diffstat 4 files changed, 51 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/Operation.java	Tue Aug 12 15:17:14 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/Operation.java	Tue Aug 12 16:55:28 2008 -0400
@@ -94,6 +94,13 @@
 		}
 	}
 
+	public boolean isNull() {
+		if (operationPointer == 0) {
+			return true;
+		}
+		return false;
+	}
+	
 	public State getState() {
 		assert (operationPointer != 0); 
 		int state;
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Tue Aug 12 15:17:14 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Tue Aug 12 16:55:28 2008 -0400
@@ -364,7 +364,7 @@
 			}
 
 		};
-		
+
 		addStreamListener(openCloseListener);
 
 		final Semaphore semaphore = new Semaphore(0);
@@ -374,15 +374,12 @@
 			this.addStreamListener(new StreamListener() {
 				@Override
 				public void update(StreamEvent e) {
-					System.out.println(this.getClass().getName()
-							+ " waiting to stream to become ready");
 					if (e.getType() == StreamEvent.Type.READY) {
 						semaphore.release();
 					}
 				}
 			});
 
-			System.out.println("about to open stream");
 			native_start();
 		}
 
@@ -393,8 +390,6 @@
 			// stream");
 		}
 
-		System.out.println(this.getClass().getName() + "stream is ready");
-
 		controls = new Control[2];
 		volumeControl = new PulseAudioStreamVolumeControl(this);
 		controls[0] = volumeControl;
@@ -500,7 +495,15 @@
 	}
 
 	private void addStreamListener(StreamListener listener) {
-		this.streamListeners.add(listener);
+		synchronized (streamListeners) {
+			this.streamListeners.add(listener);
+		}
+	}
+
+	private void removeStreamListener(StreamListener listener) {
+		synchronized (streamListeners) {
+			this.streamListeners.remove(listener);
+		}
 	}
 
 	public boolean isOpen() {
@@ -516,10 +519,33 @@
 	public void close() {
 		assert (isOpen);
 
+		final Semaphore semaphore = new Semaphore(0);
+
+		StreamListener closeListener = new StreamListener() {
+			@Override
+			public void update(StreamEvent e) {
+				if (e.getType() == StreamEvent.Type.TERMINATED
+						|| e.getType() == StreamEvent.Type.FAILED) {
+					semaphore.release();
+				}
+			}
+		};
+
+		addStreamListener(closeListener);
+
 		synchronized (eventLoop.threadLock) {
 			native_close();
 		}
 
+		try {
+			semaphore.acquire();
+			System.out.println("stream closed");
+			removeStreamListener(closeListener);
+		} catch (InterruptedException e) {
+			// throw new LineUnavailableException("unable to prepare
+			// stream");
+		}
+
 	}
 
 	public int getBufferSize() {
@@ -660,6 +686,7 @@
 				fireStreamEvent(new StreamEvent(StreamEvent.Type.FAILED));
 				break;
 			case 4:
+				fireStreamEvent(new StreamEvent(StreamEvent.Type.TERMINATED));
 				break;
 			default:
 				assert ("not supposed to happen".indexOf("false") >= 0);
@@ -674,8 +701,10 @@
 	}
 
 	private void fireStreamEvent(StreamEvent e) {
-		for (StreamListener streamListener : streamListeners) {
-			streamListener.update(e);
+		synchronized (streamListeners) {
+			for (StreamListener streamListener : streamListeners) {
+				streamListener.update(e);
+			}
 		}
 	}
 
--- a/src/native/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c	Tue Aug 12 15:17:14 2008 -0400
+++ b/src/native/org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine.c	Tue Aug 12 16:55:28 2008 -0400
@@ -75,7 +75,7 @@
 
 	jobject obj = java_context->obj;
 
-	printf("----> Stream state changed to %d\n", pa_stream_get_state(stream));
+	// printf("stream state changed to %d\n", pa_stream_get_state(stream));
 
 	/* Call the 'update' method in java
 	 * to handle all java-side events
@@ -91,10 +91,10 @@
 		return;
 
 	}
-	//	printf("calling update on java\n");
+	//printf("calling update on java\n");
 	(*env)->CallVoidMethod(env, obj, mid, pa_stream_get_state(stream));
 
-	//	printf("returning form stream_state_change_callback\n");
+	//printf("returning form stream_state_change_callback\n");
 	return;
 
 }
@@ -144,15 +144,12 @@
 		return;
 	}
 
-	
 	sample_spec.rate = sampleRate;
 	sample_spec.channels = channels;
 
 	printf("sample_spec.rate = %d\n", sample_spec.rate);
 	printf("sample_spec.channels = %d\n", sample_spec.channels);
-	
-	
-	
+
 	if ( !pa_sample_spec_valid(&sample_spec)) {
 		printf("error: invalid format\n");
 		throwByName(env, "java/lang/IllegalArgumentException", "Invalid format");
@@ -228,7 +225,6 @@
 
 }
 
-
 /*
  * Class:     org_classpath_icedtea_pulseaudio_PulseAudioSourceDataLine
  * Method:    native_start
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseSourceDataLineTest.java	Tue Aug 12 15:17:14 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseSourceDataLineTest.java	Tue Aug 12 16:55:28 2008 -0400
@@ -81,6 +81,8 @@
 	@Test
 	public void testPlay() throws LineUnavailableException,
 			UnsupportedAudioFileException, IOException {
+		System.out.println("This test plays a file");
+		
 		File soundFile = new File("testsounds/startup.wav");
 		AudioInputStream audioInputStream = AudioSystem
 				.getAudioInputStream(soundFile);