changeset 120:7ba42a599d59

2009-09-12 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java Changed sourceLineInfos and targetLineInfos to List<Line.Info> from Line.Info[]. (PulseAudioMixer): Fixed to work with the change in sourceLineInfos and targetLineInfos. (getSourceLineInfo): Likewise. (getSourceLineInfo): Likewise. (getTargetLineInfo): Likewise. (isLineSupported): Likewise. (open): Moved the initialization of Port infomration to the actual implementation in openRemote. (openRemote): Added the Port initialization info. Now it doesnt overwrite any Clip info. * unittests/org/classpath/icedtea/pulseaudio/OtherSourndProvidersAvailableTest.java (testOtherSoundProviders): Made the test more verbose and not fail if using the default Mixer. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerProviderTest.java Made the test more verbose and print out more information. Also tries to open a source line. * unittests/org/classpath/icedtea/pulseaudio/PulseAUdioMixerTest.java (testSourceLinesExist): Now tests if at least one SourceDataLine, Clip and Port are available.
author Omair Majid <omajid@redhat.com>
date Fri, 12 Sep 2008 11:19:27 -0400
parents 9eb090e7c22c
children b10eef873d2d
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java unittests/org/classpath/icedtea/pulseaudio/OtherSoundProvidersAvailableTest.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerProviderTest.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerTest.java
diffstat 4 files changed, 152 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java	Fri Sep 12 09:43:49 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioMixer.java	Fri Sep 12 11:19:27 2008 -0400
@@ -70,9 +70,8 @@
 	public EventLoop eventLoop;
 	public Thread eventLoopThread;
 
-	private Line.Info[] sourceLineInfos;
-	private Line.Info[] targetLineInfos;
-	private Line.Info[] clipInfos;
+	private List<Line.Info> sourceLineInfos = new ArrayList<Line.Info>();
+	private List<Line.Info> targetLineInfos = new ArrayList<Line.Info>();
 
 	private static PulseAudioMixer _instance = null;
 
@@ -88,21 +87,17 @@
 
 	private PulseAudioMixer() {
 		AudioFormat[] formats = getSupportedFormats();
-		List<Line.Info> sourceLineInfoList = new ArrayList<Line.Info>();
-		sourceLineInfoList.add(new DataLine.Info(SourceDataLine.class, formats,
+
+		sourceLineInfos.add(new DataLine.Info(SourceDataLine.class, formats,
 				StreamBufferAttributes.MIN_VALUE,
 				StreamBufferAttributes.MAX_VALUE));
-		sourceLineInfos = new Line.Info[] { new DataLine.Info(
-				SourceDataLine.class, formats,
+		sourceLineInfos.add(new DataLine.Info(Clip.class, formats,
 				StreamBufferAttributes.MIN_VALUE,
-				StreamBufferAttributes.MAX_VALUE) };
-		targetLineInfos = new Line.Info[] { new DataLine.Info(
-				TargetDataLine.class, formats,
+				StreamBufferAttributes.MAX_VALUE));
+
+		targetLineInfos.add(new DataLine.Info(TargetDataLine.class, formats,
 				StreamBufferAttributes.MIN_VALUE,
-				StreamBufferAttributes.MAX_VALUE) };
-		clipInfos = new Line.Info[] { new DataLine.Info(Clip.class, formats,
-				StreamBufferAttributes.MIN_VALUE,
-				StreamBufferAttributes.MAX_VALUE) };
+				StreamBufferAttributes.MAX_VALUE));
 
 	}
 
@@ -327,16 +322,13 @@
 	}
 
 	public javax.sound.sampled.Line.Info[] getSourceLineInfo() {
-		Line.Info[] localArray = new Line.Info[sourceLineInfos.length];
-		System.arraycopy(sourceLineInfos, 0, localArray, 0,
-				sourceLineInfos.length);
-		return localArray;
+		return sourceLineInfos.toArray(new Line.Info[0]);
 	}
 
 	@Override
 	public javax.sound.sampled.Line.Info[] getSourceLineInfo(
 			javax.sound.sampled.Line.Info info) {
-		ArrayList<javax.sound.sampled.Line.Info> infos = new ArrayList<javax.sound.sampled.Line.Info>();
+		ArrayList<Line.Info> infos = new ArrayList<Line.Info>();
 
 		for (Line.Info supportedInfo : sourceLineInfos) {
 			if (info.matches(supportedInfo)) {
@@ -354,10 +346,7 @@
 
 	@Override
 	public javax.sound.sampled.Line.Info[] getTargetLineInfo() {
-		Line.Info[] localArray = new Line.Info[targetLineInfos.length];
-		System.arraycopy(targetLineInfos, 0, localArray, 0,
-				targetLineInfos.length);
-		return localArray;
+		return targetLineInfos.toArray(new Line.Info[0]);
 	}
 
 	@Override
@@ -381,23 +370,18 @@
 	@Override
 	public boolean isLineSupported(javax.sound.sampled.Line.Info info) {
 		if (info != null) {
-			for (int i = 0; i < sourceLineInfos.length; i++) {
-				if (info.matches(sourceLineInfos[i])) {
+			for (Line.Info myInfo : sourceLineInfos) {
+				if (info.matches(myInfo)) {
 					return true;
 				}
 			}
 
-			for (int i = 0; i < targetLineInfos.length; i++) {
-				if (info.matches(targetLineInfos[i])) {
+			for (Line.Info myInfo : targetLineInfos) {
+				if (info.matches(myInfo)) {
 					return true;
 				}
 			}
 
-			for (Line.Info clipInfo : clipInfos) {
-				if (info.matches(clipInfo)) {
-					return true;
-				}
-			}
 		}
 		return false;
 
@@ -493,22 +477,6 @@
 	@Override
 	public void open() throws LineUnavailableException {
 		openLocal();
-		// the sourceLineInfo and targetLineInfo arrays need to be updated with
-		// port infos, which can only be obtained after EventLoop had started
-
-		ArrayList<Line.Info> sourceLineInfoList = new ArrayList<Line.Info>();
-		sourceLineInfoList.add(sourceLineInfos[0]);
-		for (String portName : eventLoop.updateSourcePortNameList()) {
-			sourceLineInfoList.add(new Port.Info(Port.class, portName, true));
-		}
-		sourceLineInfos = sourceLineInfoList.toArray(new Line.Info[0]);
-
-		ArrayList<Line.Info> targetLineInfoList = new ArrayList<Line.Info>();
-		targetLineInfoList.add(targetLineInfos[0]);
-		for (String portName : eventLoop.updateTargetPortNameList()) {
-			targetLineInfoList.add(new Port.Info(Port.class, portName, false));
-		}
-		targetLineInfos = targetLineInfoList.toArray(new Line.Info[0]);
 
 	}
 
@@ -621,6 +589,17 @@
 
 		this.isOpen = true;
 
+		// sourceLineInfo and targetLineInfo need to be updated with
+		// port infos, which can only be obtained after EventLoop had started
+
+		for (String portName : eventLoop.updateSourcePortNameList()) {
+			sourceLineInfos.add(new Port.Info(Port.class, portName, true));
+		}
+
+		for (String portName : eventLoop.updateTargetPortNameList()) {
+			targetLineInfos.add(new Port.Info(Port.class, portName, false));
+		}
+
 	}
 
 	@Override
--- a/unittests/org/classpath/icedtea/pulseaudio/OtherSoundProvidersAvailableTest.java	Fri Sep 12 09:43:49 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/OtherSoundProvidersAvailableTest.java	Fri Sep 12 11:19:27 2008 -0400
@@ -33,8 +33,7 @@
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
-*/
-
+ */
 
 package org.classpath.icedtea.pulseaudio;
 
@@ -48,58 +47,72 @@
 
 import org.junit.Test;
 
+public class OtherSoundProvidersAvailableTest {
 
-public class OtherSoundProvidersAvailableTest {
-	
-	public static junit.framework.Test suite() { 
-	    return new JUnit4TestAdapter(OtherSoundProvidersAvailableTest.class); 
+	public static junit.framework.Test suite() {
+		return new JUnit4TestAdapter(OtherSoundProvidersAvailableTest.class);
 	}
-	
+
 	@Test
 	public void testOtherSoundProviders() {
+		System.out.println("This tests if alsa mixers are still available");
 
-		Mixer.Info mixerInfos [] = AudioSystem.getMixerInfo();
+		Mixer.Info mixerInfos[] = AudioSystem.getMixerInfo();
 		Mixer.Info selectedMixerInfo = null;
 		Mixer selectedMixer;
-		
+
+		boolean selected = false;
 		int i = 0;
-		for ( Mixer.Info info: mixerInfos) {
-			System.out.println("Mixer Line " + i++ + ": " + info.getName() + " " + info.getDescription());
-			if ( info.getName().contains("0,4")) {
-				selectedMixerInfo =	info; 
+		System.out.println("Available Mixers:");
+		// use 0,0 or the default
+		for (Mixer.Info info : mixerInfos) {
+			System.out.println("Mixer Line " + i++ + ": " + info.getName()
+					+ " " + info.getDescription());
+			if (info.getName().contains("0,0") && !selected) {
+				System.out.println("^ selecting as the mixer to use");
+				selectedMixerInfo = info;
+				selected = true;
 			}
 		}
-		System.out.println(selectedMixerInfo.toString());
-		System.out.println("getting information from selected mixer:");
-		
-		// use 0,4 or the default
+
+		if (selectedMixerInfo != null) {
+			System.out.println(selectedMixerInfo.toString());
+		}
+
+		System.out.print("Selected mixer is of class: ");
+
 		selectedMixer = AudioSystem.getMixer(selectedMixerInfo);
-		System.out.println(selectedMixer.toString());
+		System.out.println(selectedMixer.getClass().toString());
 		try {
 			Line.Info sourceDataLineInfo = null;
-			
-			selectedMixer.open();	// initialize the mixer
-			
+
+			selectedMixer.open(); // initialize the mixer
+
 			Line.Info allLineInfo[] = selectedMixer.getSourceLineInfo();
+			System.out.println("Source lines supported by mixer: ");
 			int j = 0;
-			for ( Line.Info lineInfo : allLineInfo) {
-				System.out.println("Source Line " + j++ + ": " + lineInfo.getLineClass());
-				if ( lineInfo.getLineClass().toString().contains("SourceDataLine")) {
+			for (Line.Info lineInfo : allLineInfo) {
+				System.out.println("Source Line " + j++ + ": "
+						+ lineInfo.getLineClass());
+				if (lineInfo.toString().contains("SourceDataLine")) {
 					sourceDataLineInfo = lineInfo;
 				}
+
 			}
-			
-			SourceDataLine sourceDataLine = (SourceDataLine) selectedMixer.getLine(sourceDataLineInfo);
 
-			sourceDataLine.open();
-			//sourceDataLine.write('a',  0, 2);
-			sourceDataLine.close();
-			
-		} catch ( LineUnavailableException e ) {
+			if (sourceDataLineInfo == null) {
+				System.out.println("Mixer supports no SourceDataLines");
+			} else {
+				SourceDataLine sourceDataLine = (SourceDataLine) selectedMixer
+						.getLine(sourceDataLineInfo);
+
+				sourceDataLine.open();
+				// sourceDataLine.write('a', 0, 2);
+				sourceDataLine.close();
+			}
+		} catch (LineUnavailableException e) {
 			System.out.println("Line unavailable");
 		}
-		
-		
-		
+
 	}
 }
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerProviderTest.java	Fri Sep 12 09:43:49 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerProviderTest.java	Fri Sep 12 11:19:27 2008 -0400
@@ -33,61 +33,92 @@
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
-*/
+ */
 
 package org.classpath.icedtea.pulseaudio;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
 
+import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.Line;
 import javax.sound.sampled.LineUnavailableException;
 import javax.sound.sampled.Mixer;
+import javax.sound.sampled.SourceDataLine;
 
 import junit.framework.JUnit4TestAdapter;
 
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 
 public class PulseAudioMixerProviderTest {
 
-	private Mixer selectedMixer;
-	private Mixer.Info selectedMixerInfo;
-
-	public static junit.framework.Test suite() { 
-	    return new JUnit4TestAdapter(PulseAudioMixerProviderTest.class); 
+	public static junit.framework.Test suite() {
+		return new JUnit4TestAdapter(PulseAudioMixerProviderTest.class);
 	}
+	
+	AudioFormat aSupportedFormat = new AudioFormat(
+			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
-	@Before
-	public void setUp() throws Exception {
+	@Test
+	public void testMixerProvider() throws LineUnavailableException {
 
-		Mixer.Info mixerInfos [] = AudioSystem.getMixerInfo();
+		System.out
+				.println("This test checks that the PulseAudio mixer exists and is usable");
+
+		Mixer selectedMixer = null;
+		Mixer.Info selectedMixerInfo = null;
+
+		Mixer.Info mixerInfos[] = AudioSystem.getMixerInfo();
 
 		int i = 0;
-		for ( Mixer.Info info: mixerInfos) {
-			System.out.println("Mixer Line " + i++ + ": " + info.getName() + " " + info.getDescription());
-			if ( info.getName().contains("PulseAudio")) {
-				selectedMixerInfo =	info; 
+		for (Mixer.Info info : mixerInfos) {
+			System.out.println("Mixer Line " + i++ + ": " + info.getName()
+					+ " " + info.getDescription());
+			if (info.getName().contains("PulseAudio")) {
+				System.out.println("              ^ found PulseAudio Mixer!");
+				selectedMixerInfo = info;
 			}
 		}
+		
 		assertNotNull(selectedMixerInfo);
-	}
+
 
-	@Test 
-	public void testMixerProvider() throws LineUnavailableException {
-		System.out.println(selectedMixerInfo.toString());
-		System.out.println("getting information from selected mixer:");
-
+		System.out.println("Getting information from selected mixer:");
+		System.out.println("Name: "+ selectedMixerInfo.getName());
+		System.out.println("Version: " + selectedMixerInfo.getVersion());
+		
 		selectedMixer = AudioSystem.getMixer(selectedMixerInfo);
 		assertNotNull(selectedMixer);
-		System.out.println(selectedMixer.toString());
-	}
+		System.out.println("Implemented in class: " + selectedMixer.getClass().toString());
+
+		selectedMixer.open(); // initialize the mixer
+
+		Line.Info sourceDataLineInfo = null;
+		Line.Info allLineInfo[] = selectedMixer.getSourceLineInfo();
+		System.out.println("Source lines supported by mixer: ");
+		int j = 0;
+		for (Line.Info lineInfo : allLineInfo) {
+			System.out.println("Source Line " + j++ + ": "
+					+ lineInfo.getLineClass());
+			if (lineInfo.toString().contains("SourceDataLine")) {
+				sourceDataLineInfo = lineInfo;
+			}
 
+		}
 
-	@After
-	public void tearDown() throws Exception {
+		if (sourceDataLineInfo == null) {
+			System.out.println("Mixer supports no SourceDataLines");
+		} else {
+			SourceDataLine sourceDataLine = (SourceDataLine) selectedMixer
+					.getLine(sourceDataLineInfo);
+
+			sourceDataLine.open(aSupportedFormat);
+			// sourceDataLine.write('a', 0, 2);
+			sourceDataLine.close();
+		}
+		
+		selectedMixer.close();
 
 	}
 
-
 }
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerTest.java	Fri Sep 12 09:43:49 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerTest.java	Fri Sep 12 11:19:27 2008 -0400
@@ -64,7 +64,7 @@
 	PulseAudioMixer selectedMixer;
 
 	AudioFormat aSupportedFormat = new AudioFormat(
-			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true);
+			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
 	public static junit.framework.Test suite() {
 		return new JUnit4TestAdapter(PulseAudioMixerTest.class);
@@ -147,19 +147,38 @@
 
 	@Test
 	public void testSourceLinesExist() throws LineUnavailableException {
+		System.out.println("This tests that source lines exist");
 		selectedMixer.open();
 		Line.Info allLineInfo[] = selectedMixer.getSourceLineInfo();
 		Assert.assertNotNull(allLineInfo);
 		Assert.assertTrue(allLineInfo.length > 0);
+		
+		boolean foundSourceDataLine = false;
+		boolean foundClip = false;
+		boolean foundPort = false;
+		
 		int j = 0;
 		for (Line.Info lineInfo : allLineInfo) {
 			System.out.println("Source Line " + j++ + ": "
 					+ lineInfo.getLineClass());
+			if (lineInfo.getLineClass().toString().contains("SourceDataLine")) {
+				foundSourceDataLine = true;
+			} else if (lineInfo.getLineClass().toString().contains("Clip")) {
+				foundClip = true;
+			} else if (lineInfo.getLineClass().toString().contains("Port")) {
+				foundPort = true;
+			} else {
+				Assert.assertFalse("Found a new type of Line", true);
+			}
 			Line sourceLine = (Line) selectedMixer.getLine(lineInfo);
 			assertNotNull(sourceLine);
 
 		}
 
+		Assert.assertTrue("Couldnt find a SourceDataLine", foundSourceDataLine);
+		Assert.assertTrue("Couldnt find a Clip", foundClip);
+		Assert.assertTrue("Couldnt find a Port", foundPort);
+		
 	}
 
 	@Test