changeset 150:f508adb1c908

2008-09-29 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java (close): Dont drain the line on close. Also mark the line as stopped. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java (ThreaderReader): New class. Reads a targetDataLine in a thread. (testReadAndClose): New function. Tests the effects of read() and close() on the same line. (testReadAndStop): New function. Tests the effects of read() and stop() on a TargetDataLine. (testReadAndDrain): New function. Tests the effects of read() and drain() on a TargetDataLine. (testReadAndFlush): New function. Tests the effects of read() and flush() on a TargetDataLine.
author Omair Majid <omajid@redhat.com>
date Mon, 29 Sep 2008 13:50:48 -0400
parents f29cbcfbc354
children 350bc72ba495
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java
diffstat 2 files changed, 147 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java	Fri Sep 26 13:27:33 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java	Mon Sep 29 13:50:48 2008 -0400
@@ -261,8 +261,6 @@
 					"Line must be open for close() to work");
 		}
 
-		drain();
-
 		synchronized (eventLoop.threadLock) {
 			stream.disconnect();
 		}
@@ -278,6 +276,7 @@
 		}
 
 		super.close();
+		isStarted = false;
 
 	}
 
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Fri Sep 26 13:27:33 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Mon Sep 29 13:50:48 2008 -0400
@@ -55,6 +55,7 @@
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class PulseAudioTargetDataLineTest {
@@ -68,6 +69,28 @@
 	AudioFormat aSupportedFormat = new AudioFormat(
 			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
+	class ThreadReader extends Thread {
+		TargetDataLine line;
+		byte[] buffer;
+
+		public ThreadReader(TargetDataLine line, byte[] buffer)
+				throws LineUnavailableException {
+
+			this.line = line;
+			this.buffer = buffer;
+
+		}
+
+		@Override
+		public void run() {
+			int bytesRead = 0;
+
+			bytesRead = line.read(buffer, 0, buffer.length);
+			// System.out.println("read data");
+
+		}
+	}
+
 	@Before
 	public void setUp() throws LineUnavailableException {
 		Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
@@ -214,6 +237,129 @@
 	}
 
 	@Test
+	public void testReadAndClose() throws LineUnavailableException,
+			InterruptedException {
+		System.out.println("This test tries to close a line while "
+				+ "read()ing to check that read() returns");
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		AudioFormat breakingFormat = new AudioFormat(
+				AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 2, 2, 44100f,
+				true);
+		targetDataLine.open(breakingFormat);
+		targetDataLine.start();
+		byte[] buffer = new byte[1000000];
+
+		ThreadReader reader = new ThreadReader(targetDataLine, buffer);
+		reader.start();
+
+		Thread.sleep(100);
+
+		Assert.assertTrue(reader.isAlive());
+
+		targetDataLine.close();
+
+		reader.join(500);
+
+		Assert.assertFalse(reader.isAlive());
+
+	}
+
+	@Test
+	public void testReadAndStop() throws LineUnavailableException,
+			InterruptedException {
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		AudioFormat breakingFormat = new AudioFormat(
+				AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 2, 2, 44100f,
+				true);
+		targetDataLine.open(breakingFormat);
+		targetDataLine.start();
+		byte[] buffer = new byte[10000000];
+
+		ThreadReader reader = new ThreadReader(targetDataLine, buffer);
+		reader.start();
+
+		Thread.sleep(100);
+
+		Assert.assertTrue(reader.isAlive());
+
+		targetDataLine.stop();
+
+		Thread.sleep(100);
+
+		Assert.assertFalse(reader.isAlive());
+
+		targetDataLine.close();
+
+	}
+
+	// this is kind of messed up
+	// drain should hang on a started data line
+	// but read should return
+	@Ignore
+	@Test
+	public void testReadAndDrain() throws LineUnavailableException,
+			InterruptedException {
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		AudioFormat breakingFormat = new AudioFormat(
+				AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 2, 2, 44100f,
+				true);
+		targetDataLine.open(breakingFormat);
+		targetDataLine.start();
+		byte[] buffer = new byte[10000000];
+
+		ThreadReader reader = new ThreadReader(targetDataLine, buffer);
+		reader.start();
+
+		Thread.sleep(100);
+
+		Assert.assertTrue(reader.isAlive());
+
+		targetDataLine.drain();
+
+		Thread.sleep(100);
+
+		Assert.assertFalse(reader.isAlive());
+		targetDataLine.stop();
+		targetDataLine.close();
+	}
+
+	@Test
+	public void testReadAndFlush() throws LineUnavailableException,
+			InterruptedException {
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		AudioFormat breakingFormat = new AudioFormat(
+				AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 2, 2, 44100f,
+				true);
+		targetDataLine.open(breakingFormat);
+		targetDataLine.start();
+		byte[] buffer = new byte[10000000];
+
+		ThreadReader reader = new ThreadReader(targetDataLine, buffer);
+		reader.start();
+
+		Thread.sleep(100);
+
+		Assert.assertTrue(reader.isAlive());
+
+		targetDataLine.flush();
+
+		Thread.sleep(100);
+
+		Assert.assertFalse(reader.isAlive());
+
+		targetDataLine.stop();
+		targetDataLine.close();
+	}
+
+	@Test
 	public void testDrain() throws LineUnavailableException,
 			InterruptedException {
 		System.out