changeset 129:3a9c7727be22

2008-09-19 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java (ClipThread.run): Drain the stream when done playing. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java (testOpenEvent): Added a println indicate the event. (testCloseEvent): Added a println to indicate the event. (testStartedStopped): Added println to indicate the events. Removed call to drain(). * unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java (ThreadWriter.run): Commented out debug output. (testStartAndStopEventsOnCork): Enabled this test. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java (ThreadWriter): New class. Writes to a SourceDataLine while running in a separate thread. (testStartedStopped): Added clearer debug output. (test2StartAndStopEvents): New function. Checks that two START and two STOP events are caused. (test3StartAndStopEvents): New function. Checks that three START and STOP events are produced.
author Omair Majid <omajid@redhat.com>
date Fri, 19 Sep 2008 13:42:34 -0400
parents fda25668e4d1
children 3fc512ee667f
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java
diffstat 4 files changed, 190 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java	Fri Sep 19 13:06:48 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java	Fri Sep 19 13:42:34 2008 -0400
@@ -79,7 +79,7 @@
 				if (Thread.interrupted()) {
 					// Thread.currentThread().interrupt();
 					clipThreadStarted = false;
-					return;
+					break;
 				}
 
 				try {
@@ -89,7 +89,7 @@
 					if (loopsLeft == 0) {
 						System.out.println("Reading to the end of the file");
 						writeFrames(endFrame, getFrameLength());
-						return;
+						break;
 					} else {
 						synchronized (clipLock) {
 							currentFrame = startFrame;
@@ -98,9 +98,11 @@
 					}
 					clipSemaphore.release();
 				} catch (InterruptedException e) {
-					return;
+					break;
 				}
 
+				PulseAudioClip.this.drain();
+
 			}
 		}
 	}
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java	Fri Sep 19 13:06:48 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java	Fri Sep 19 13:42:34 2008 -0400
@@ -175,6 +175,7 @@
 			@Override
 			public void update(LineEvent event) {
 				if (event.getType() == LineEvent.Type.OPEN) {
+					System.out.println("OPEN");
 					opened++;
 				}
 			}
@@ -210,6 +211,7 @@
 			@Override
 			public void update(LineEvent event) {
 				if (event.getType() == LineEvent.Type.CLOSE) {
+					System.out.println("CLOSE");
 					closed++;
 				}
 			}
@@ -252,12 +254,13 @@
 			@Override
 			public void update(LineEvent event) {
 				if (event.getType() == LineEvent.Type.START) {
+					System.out.println("START");
 					started++;
 					Assert.assertEquals(1, started);
 				}
 
 				if (event.getType() == LineEvent.Type.STOP) {
-					System.out.println("Stopped event");
+					System.out.println("STOP");
 					stopped++;
 					Assert.assertEquals(1, stopped);
 				}
@@ -268,7 +271,7 @@
 		clip.addLineListener(startStopListener);
 
 		clip.start();
-		clip.drain();
+		// clip.drain();
 
 		clip.stop();
 		clip.close();
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java	Fri Sep 19 13:06:48 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java	Fri Sep 19 13:42:34 2008 -0400
@@ -57,7 +57,6 @@
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class PulseAudioSourceDataLineRawTest {
@@ -102,7 +101,7 @@
 					bytesRead = stream.read(abData, 0, abData.length);
 					// System.out.println("read data");
 					if (bytesRead > 0) {
-						System.out.println("about to write data");
+						// System.out.println("about to write data");
 						line.write(abData, 0, bytesRead);
 						// System.out.println("wrote data");
 					}
@@ -135,7 +134,6 @@
 
 	}
 
-	@Ignore
 	@Test
 	public void testStartAndStopEventsOnCork()
 			throws UnsupportedAudioFileException, IOException,
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java	Fri Sep 19 13:06:48 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java	Fri Sep 19 13:42:34 2008 -0400
@@ -73,6 +73,56 @@
 	AudioFormat aSupportedFormat = new AudioFormat(
 			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true);
 
+	class ThreadWriter extends Thread {
+		SourceDataLine line;
+		AudioInputStream stream;
+
+		public ThreadWriter(AudioInputStream stream, SourceDataLine line)
+				throws LineUnavailableException {
+
+			this.line = line;
+			this.stream = stream;
+
+			if (line.isOpen()) {
+				line.close();
+			}
+
+		}
+
+		@Override
+		public void run() {
+			try {
+				AudioFormat audioFormat = stream.getFormat();
+
+				line.open(audioFormat);
+
+				byte[] abData = new byte[1000];
+				int bytesRead = 0;
+
+				line.start();
+
+				while (bytesRead >= 0) {
+					bytesRead = stream.read(abData, 0, abData.length);
+					// System.out.println("read data");
+					if (bytesRead > 0) {
+						// System.out.println("about to write data");
+						line.write(abData, 0, bytesRead);
+						// System.out.println("wrote data");
+					}
+				}
+
+				line.drain();
+				line.close();
+
+			} catch (LineUnavailableException e) {
+				e.printStackTrace();
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+	}
+
 	@Before
 	public void setUp() throws Exception {
 		Mixer.Info mixerInfos[] = AudioSystem.getMixerInfo();
@@ -194,12 +244,13 @@
 			@Override
 			public void update(LineEvent event) {
 				if (event.getType() == LineEvent.Type.START) {
+					System.out.println("START");
 					started++;
 					Assert.assertEquals(1, started);
 				}
 
 				if (event.getType() == LineEvent.Type.STOP) {
-					System.out.println("Stopped event");
+					System.out.println("STOP");
 					stopped++;
 					Assert.assertEquals(1, stopped);
 				}
@@ -229,6 +280,133 @@
 
 	}
 
+	@Test
+	public void test2StartAndStopEvents() throws UnsupportedAudioFileException,
+			IOException, LineUnavailableException, InterruptedException {
+
+		System.out
+				.println("This test checks if START and STOP notifications appear on corking");
+
+		File soundFile = new File("testsounds/startup.wav");
+		AudioInputStream audioInputStream = AudioSystem
+				.getAudioInputStream(soundFile);
+		AudioFormat audioFormat = audioInputStream.getFormat();
+
+		SourceDataLine line;
+		line = (SourceDataLine) mixer.getLine(new DataLine.Info(
+				SourceDataLine.class, audioFormat));
+		Assert.assertNotNull(line);
+
+		LineListener startStopListener = new LineListener() {
+
+			@Override
+			public void update(LineEvent event) {
+				if (event.getType() == LineEvent.Type.START) {
+					System.out.println("START");
+					started++;
+				}
+
+				if (event.getType() == LineEvent.Type.STOP) {
+					System.out.println("STOP");
+					stopped++;
+				}
+			}
+
+		};
+
+		line.addLineListener(startStopListener);
+		System.out.println("Launching threadWriter");
+		ThreadWriter writer = new ThreadWriter(audioInputStream, line);
+		writer.start();
+		// System.out.println("started");
+
+		Thread.sleep(1000);
+
+		line.stop();
+
+		System.out.println("corked");
+
+		Thread.sleep(1000);
+
+		// UNCORK
+		line.stop();
+
+		Thread.sleep(1000);
+
+		// System.out.println("waiting for thread to finish");
+		writer.join();
+
+		Assert.assertEquals(2, started);
+		Assert.assertEquals(2, stopped);
+
+	}
+
+	@Test
+	public void test3StartAndStopEvents() throws UnsupportedAudioFileException,
+			IOException, LineUnavailableException, InterruptedException {
+
+		System.out
+				.println("This test checks if START and STOP notifications appear on corking");
+
+		File soundFile = new File("testsounds/startup.wav");
+		AudioInputStream audioInputStream = AudioSystem
+				.getAudioInputStream(soundFile);
+		AudioFormat audioFormat = audioInputStream.getFormat();
+
+		SourceDataLine line;
+		line = (SourceDataLine) mixer.getLine(new DataLine.Info(
+				SourceDataLine.class, audioFormat));
+		Assert.assertNotNull(line);
+
+		LineListener startStopListener = new LineListener() {
+
+			@Override
+			public void update(LineEvent event) {
+				if (event.getType() == LineEvent.Type.START) {
+					System.out.println("START");
+					started++;
+				}
+
+				if (event.getType() == LineEvent.Type.STOP) {
+					System.out.println("STOP");
+					stopped++;
+				}
+			}
+
+		};
+
+		line.addLineListener(startStopListener);
+		System.out.println("Launching threadWriter");
+		ThreadWriter writer = new ThreadWriter(audioInputStream, line);
+		writer.start();
+		// System.out.println("started");
+
+		Thread.sleep(1000);
+
+		line.stop();
+
+		Thread.sleep(1000);
+
+		line.start();
+
+		Thread.sleep(1000);
+
+		line.stop();
+
+		Thread.sleep(1000);
+
+		line.start();
+
+		Thread.sleep(1000);
+
+		// System.out.println("waiting for thread to finish");
+		writer.join();
+
+		Assert.assertEquals(3, started);
+		Assert.assertEquals(3, stopped);
+
+	}
+
 	@Test(expected = IllegalStateException.class)
 	public void testStartOnClosedLine() throws LineUnavailableException {
 		SourceDataLine line;