changeset 132:b7a5a39b31ab

2008-09-22 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java (ClipThread.run): Drain when done playing. (close): Interrupt the clipThread to stop it immediately. (drain): Interrupt and drain the buffer, dont block. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java (testStartedStopped): Calls drain to wait for the line to finish playing. (testDrainWithoutStart): New test.
author Omair Majid <omajid@redhat.com>
date Mon, 22 Sep 2008 14:44:23 -0400
parents 11a52266951f
children 4a1c8f3d1f62
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java
diffstat 2 files changed, 45 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java	Mon Sep 22 14:10:35 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java	Mon Sep 22 14:44:23 2008 -0400
@@ -73,6 +73,7 @@
 	private class ClipThread extends Thread {
 		@Override
 		public void run() {
+
 			clipThreadStarted = true;
 			while (loopsLeft >= 0) {
 				writeFrames(currentFrame, endFrame + 1);
@@ -101,9 +102,9 @@
 					break;
 				}
 
-				PulseAudioClip.this.drain();
+			}
 
-			}
+			PulseAudioClip.this.drain();
 		}
 	}
 
@@ -193,6 +194,8 @@
 			throw new IllegalStateException("line already closed");
 		}
 
+		clipThread.interrupt();
+
 		try {
 			clipThread.join();
 		} catch (InterruptedException e) {
@@ -212,6 +215,13 @@
 			throw new IllegalStateException("line not open");
 		}
 
+		if (clipThread != null) {
+			clipThread.interrupt();
+			try {
+				clipThread.join();
+			} catch (InterruptedException e) {
+			}
+		}
 		Operation operation;
 
 		synchronized (eventLoop.threadLock) {
@@ -447,6 +457,7 @@
 			clipThread = new ClipThread();
 			clipThread.start();
 		}
+
 	}
 
 	public void stop() {
@@ -472,6 +483,7 @@
 		}
 
 		super.stop();
+
 	}
 
 }
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java	Mon Sep 22 14:10:35 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java	Mon Sep 22 14:44:23 2008 -0400
@@ -64,6 +64,11 @@
 	AudioFormat aSupportedFormat = new AudioFormat(
 			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
+	int started = 0;
+	int stopped = 0;
+	int opened = 0;
+	int closed = 0;
+
 	@Before
 	public void setUp() throws LineUnavailableException {
 		Mixer.Info wantedMixerInfo = null;
@@ -77,6 +82,11 @@
 		assert (wantedMixerInfo != null);
 		mixer = AudioSystem.getMixer(wantedMixerInfo);
 		mixer.open();
+
+		started = 0;
+		stopped = 0;
+		opened = 0;
+		closed = 0;
 	}
 
 	@Test
@@ -158,8 +168,6 @@
 
 	}
 
-	int opened = 0;
-
 	@Test
 	public void testOpenEvent() throws LineUnavailableException,
 			UnsupportedAudioFileException, IOException {
@@ -191,8 +199,6 @@
 
 	}
 
-	int closed = 0;
-
 	@Test
 	public void testCloseEvent() throws LineUnavailableException,
 			UnsupportedAudioFileException, IOException {
@@ -228,12 +234,9 @@
 
 	}
 
-	int started = 0;
-	int stopped = 0;
-
 	@Test
 	public void testStartedStopped() throws LineUnavailableException,
-			UnsupportedAudioFileException, IOException {
+			UnsupportedAudioFileException, IOException, InterruptedException {
 
 		File soundFile = new File("testsounds/startup.wav");
 		AudioInputStream audioInputStream = AudioSystem
@@ -244,9 +247,6 @@
 		clip = (Clip) mixer.getLine(new DataLine.Info(Clip.class, audioFormat));
 		Assert.assertNotNull(clip);
 
-		started = 0;
-		stopped = 0;
-
 		clip.open(audioInputStream);
 
 		LineListener startStopListener = new LineListener() {
@@ -271,16 +271,31 @@
 		clip.addLineListener(startStopListener);
 
 		clip.start();
-		// clip.drain();
-
+		clip.drain();
 		clip.stop();
 		clip.close();
 
 		Assert.assertEquals(1, started);
 		Assert.assertEquals(1, stopped);
 
-		started = 0;
-		stopped = 0;
+	}
+
+	@Test
+	public void testDrainWithoutStart() throws UnsupportedAudioFileException,
+			IOException, LineUnavailableException {
+
+		File soundFile = new File("testsounds/startup.wav");
+		AudioInputStream audioInputStream = AudioSystem
+				.getAudioInputStream(soundFile);
+		AudioFormat audioFormat = audioInputStream.getFormat();
+
+		Clip clip;
+		clip = (Clip) mixer.getLine(new DataLine.Info(Clip.class, audioFormat));
+		Assert.assertNotNull(clip);
+
+		clip.open(audioInputStream);
+		clip.drain();
+		clip.close();
 
 	}
 
@@ -387,6 +402,7 @@
 	@After
 	public void tearDown() {
 		mixer.close();
+
 	}
 
 }