changeset 125:3c5c586cf5f3

2008-09-16 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java (getLine): Throw the exception with a more meaningful description. * src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java (drain): Implemented drain. Blocks while the TargetDataLine remains started. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java (testIsActiveAndIsOpen): Fixed thinko. Now tests a TargetDataLine instead of a SourceDataLine. (testDrain): New function. Tests if drain() blocks between calls to start() and stop(). (testStartedStopped): Removed call to drain. Instead wait a bit. (tearDown): Clean up a bit more if possible.
author Omair Majid <omajid@redhat.com>
date Tue, 16 Sep 2008 14:05:45 -0400
parents 7ad349ee575d
children fe9c2599d07d
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java
diffstat 3 files changed, 60 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java	Mon Sep 15 16:52:31 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java	Tue Sep 16 14:05:45 2008 -0400
@@ -255,7 +255,7 @@
 			throws LineUnavailableException {
 
 		if (!isOpen) {
-			throw new LineUnavailableException();
+			throw new LineUnavailableException("The mixer isnt open");
 		}
 
 		if (!isLineSupported(info)) {
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Mon Sep 15 16:52:31 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Tue Sep 16 14:05:45 2008 -0400
@@ -141,7 +141,14 @@
 
 	@Override
 	public void drain() {
-		// FIXME how do we drain a target data line?
+		// blocks when there is data on the line
+		// http://www.jsresources.org/faq_audio.html#stop_drain_tdl
+		while (isStarted) {
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) { }
+		}
+
 	}
 
 	@Override
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Mon Sep 15 16:52:31 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Tue Sep 16 14:05:45 2008 -0400
@@ -49,7 +49,6 @@
 import javax.sound.sampled.LineListener;
 import javax.sound.sampled.LineUnavailableException;
 import javax.sound.sampled.Mixer;
-import javax.sound.sampled.SourceDataLine;
 import javax.sound.sampled.TargetDataLine;
 import javax.sound.sampled.UnsupportedAudioFileException;
 
@@ -101,8 +100,8 @@
 	@Test
 	public void testIsActiveAndIsOpen() throws LineUnavailableException {
 
-		SourceDataLine line = (SourceDataLine) mixer.getLine(new DataLine.Info(
-				SourceDataLine.class, aSupportedFormat, 1000));
+		TargetDataLine line = (TargetDataLine) mixer.getLine(new DataLine.Info(
+				TargetDataLine.class, aSupportedFormat, 1000));
 
 		Assert.assertFalse(line.isActive());
 		Assert.assertFalse(line.isOpen());
@@ -147,6 +146,40 @@
 	}
 
 	@Test
+	public void testDrain() throws LineUnavailableException,
+			InterruptedException {
+		System.out
+				.println("This test checks that drain() on a start()ed TargetDataLine hangs");
+
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+
+		targetDataLine.open();
+		targetDataLine.start();
+
+		Thread th = new Thread(new Runnable() {
+
+			@Override
+			public void run() {
+				targetDataLine.drain();
+			}
+
+		});
+
+		th.start();
+
+		th.join(5000);
+
+		if (!th.isAlive()) {
+			targetDataLine.stop();
+			th.join();
+			targetDataLine.close();
+			Assert.fail("drain() on a opened TargetDataLine should hang");
+		}
+	}
+
+	@Test
 	public void testCloseEvents() throws LineUnavailableException {
 		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
 				TargetDataLine.class));
@@ -176,7 +209,7 @@
 
 	@Test
 	public void testStartedStopped() throws LineUnavailableException,
-			UnsupportedAudioFileException, IOException {
+			UnsupportedAudioFileException, IOException, InterruptedException {
 
 		File soundFile = new File("testsounds/startup.wav");
 		AudioInputStream audioInputStream = AudioSystem
@@ -215,7 +248,7 @@
 
 		line.start();
 
-		line.drain();
+		Thread.sleep(100);
 
 		line.stop();
 		line.close();
@@ -261,7 +294,19 @@
 
 	@After
 	public void tearDown() {
-		mixer.close();
+		if (targetDataLine != null) {
+			if (targetDataLine.isActive()) {
+				targetDataLine.stop();
+			}
+
+			if (targetDataLine.isOpen()) {
+				targetDataLine.close();
+			}
+		}
+
+		if (mixer.isOpen()) {
+			mixer.close();
+		}
 	}
 
 }