changeset 144:d040c92fd62b

2008-09-26 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java (flush): Implemented function. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java Fixed aSupportedFormat to have a proper frame rate. (testOpenWithFormat): New function. Tests that the TargetDataLine can open with another format than the default one. (testRead): New function. Reads from the TargetDataLine and checks that the buffer was written to. (testReadLessThanFrameSize): New function. Tests that not reading an integral number of frames throws an exception. (testFlush): Implemented function. Flushes a TargetDataLine. No longer an @Ignored test.
author Omair Majid <omajid@redhat.com>
date Fri, 26 Sep 2008 11:04:04 -0400
parents 9c11cbf114f3
children bfc67049ee9f
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java
diffstat 2 files changed, 94 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Thu Sep 25 13:54:01 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Fri Sep 26 11:04:04 2008 -0400
@@ -177,7 +177,13 @@
 			throw new IllegalStateException("Line must be open");
 		}
 
-		// FIXME how to flush a target data line
+		Operation operation;
+ 		synchronized (eventLoop.threadLock) {
+ 			operation = stream.flush();
+ 		}
+ 		operation.waitForCompletion();
+ 		operation.releaseReference();
+ 		
 	}
 
 	public int available() {
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Thu Sep 25 13:54:01 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLineTest.java	Fri Sep 26 11:04:04 2008 -0400
@@ -55,7 +55,6 @@
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class PulseAudioTargetDataLineTest {
@@ -67,7 +66,7 @@
 	int stopped = 0;
 
 	AudioFormat aSupportedFormat = new AudioFormat(
-			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true);
+			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
 	@Before
 	public void setUp() throws LineUnavailableException {
@@ -147,6 +146,74 @@
 	}
 
 	@Test
+	public void testOpenWithFormat() throws LineUnavailableException {
+		System.out.println("This test checks that read() sort of wroks");
+
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		targetDataLine.open(aSupportedFormat);
+
+	}
+
+	@Test
+	public void testRead() throws LineUnavailableException {
+		System.out.println("This test checks that read() sort of wroks");
+
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		targetDataLine.open(aSupportedFormat);
+
+		byte[] buffer = new byte[1000];
+		for (int i = 0; i < buffer.length; i++) {
+			buffer[i] = 0;
+		}
+		targetDataLine.start();
+
+		targetDataLine.read(buffer, 0, buffer.length);
+		Assert.assertTrue(buffer[999] != 0);
+
+		buffer = new byte[1000];
+		for (int i = 0; i < buffer.length; i++) {
+			buffer[i] = 0;
+		}
+
+		targetDataLine.read(buffer, 0, buffer.length - 2);
+		Assert.assertTrue(buffer[999] == 0);
+
+		targetDataLine.stop();
+		targetDataLine.close();
+
+	}
+
+	@Test(expected = IllegalArgumentException.class)
+	public void testReadLessThanFrameSize() throws LineUnavailableException {
+		System.out.println("This test checks that read() throws an exception "
+				+ "when not reading an integral number of frames");
+
+		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);
+
+		byte[] buffer = new byte[1000];
+		for (int i = 0; i < buffer.length; i++) {
+			buffer[i] = 0;
+		}
+		targetDataLine.start();
+
+		targetDataLine.read(buffer, 0, buffer.length - 1);
+
+		targetDataLine.stop();
+		targetDataLine.close();
+
+	}
+
+	@Test
 	public void testDrain() throws LineUnavailableException,
 			InterruptedException {
 		System.out
@@ -193,16 +260,31 @@
 
 	}
 
-	@Ignore
 	@Test
-	public void testFlush() {
+	public void testFlush() throws LineUnavailableException {
+		System.out.println("This test checks that flush() wroks");
+
+		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
+				TargetDataLine.class));
+		Assert.assertNotNull(targetDataLine);
+		targetDataLine.open();
 
+		byte[] buffer = new byte[1000];
+		for (int i = 0; i < buffer.length; i++) {
+			buffer[i] = 0;
+		}
+		targetDataLine.start();
+
+		targetDataLine.read(buffer, 0, buffer.length);
+		targetDataLine.stop();
+		targetDataLine.flush();
+		targetDataLine.close();
 	}
 
 	@Test(expected = IllegalStateException.class)
 	public void testFlushWithoutOpen() throws LineUnavailableException {
 		System.out
-				.println("This test checks that drain() fails on a line not opened");
+				.println("This test checks that flush() fails on a line not opened");
 
 		targetDataLine = (TargetDataLine) mixer.getLine(new Line.Info(
 				TargetDataLine.class));