Mercurial > hg > pulseaudio
view unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java @ 135:c46f6e0e7959
2008-09-23 Omair Majid <omajid@redhat.com>
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java
Removed clipThreadStarted. Use clipThread.isAlive() etc.
Removed unused clipSemaphore
(ClipThread.run): Fixed to run without clipSemaphore.
(close): Reset currentFrame and framesSinceOpen on close.
(getFramePosition): Synchronized on framesSinceOpen.
(loop): Replaced use of clipThreadStarted with clipThread.isAlive.
(open): Initialize currentFrame, framesSinceOpen and endFrame.
(stop): Remember which frame we stopped at.
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java
(open): Clean up properly on error.
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java
(write): On interrupt, return asap and mark the thread as being interrupted.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java
(testFramePosition): Renamed to testFramePositionBeforeClose.
(testFramePositionWithStartStop): New function. Checks that starting
and stopping doesnt change the frame counts.
(testFramePositionWithLoop): Checks that looping doesnt interfere with
the frame position.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java
(ThreadWriter.run): Removed useless TODO comment.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java
(ThreadWriter.run): Removed useless TODO.
(setUp): Initialize listeners.
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Tue, 23 Sep 2008 15:02:55 -0400 |
parents | 4a1c8f3d1f62 |
children | 2d384ad19c3e |
line wrap: on
line source
/* PulseSourceDataLineTest.java Copyright (C) 2008 Red Hat, Inc. This file is part of IcedTea. IcedTea is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. IcedTea is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with IcedTea; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend 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.assertNotNull; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.BooleanControl; import javax.sound.sampled.Control; import javax.sound.sampled.DataLine; import javax.sound.sampled.FloatControl; import javax.sound.sampled.Line; import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineListener; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.Mixer; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class PulseAudioSourceDataLineTest { Mixer mixer; SourceDataLine sourceDataLine; private int listenerCalled = 0; int started = 0; int stopped = 0; int opened = 0; int closed = 0; 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) { e.printStackTrace(); } } } @Before public void setUp() throws Exception { Mixer.Info mixerInfos[] = AudioSystem.getMixerInfo(); Mixer.Info selectedMixerInfo = null; // 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; } } assertNotNull(selectedMixerInfo); mixer = AudioSystem.getMixer(selectedMixerInfo); assertNotNull(mixer); if (mixer.isOpen()) { mixer.close(); } mixer.open(); sourceDataLine = null; started = 0; stopped = 0; opened = 0; closed = 0; listenerCalled = 0; } @Test public void testOpenAndClose() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(); Assert.assertTrue(sourceDataLine.isOpen()); sourceDataLine.close(); Assert.assertFalse(sourceDataLine.isOpen()); } @Test public void testIsActiveAndIsOpen() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); Assert.assertFalse(sourceDataLine.isActive()); Assert.assertFalse(sourceDataLine.isOpen()); sourceDataLine.open(); Assert.assertTrue(sourceDataLine.isOpen()); Assert.assertFalse(sourceDataLine.isActive()); sourceDataLine.start(); Assert.assertTrue(sourceDataLine.isOpen()); Assert.assertTrue(sourceDataLine.isActive()); sourceDataLine.stop(); Assert.assertTrue(sourceDataLine.isOpen()); Assert.assertFalse(sourceDataLine.isActive()); sourceDataLine.close(); Assert.assertFalse(sourceDataLine.isOpen()); Assert.assertFalse(sourceDataLine.isActive()); } @Test public void testPlay() throws LineUnavailableException, UnsupportedAudioFileException, IOException { System.out.println("This test plays a file"); File soundFile = new File("testsounds/startup.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(audioFormat); System.out.println("opened"); sourceDataLine.start(); System.out.println("started"); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } System.out.println("done"); sourceDataLine.drain(); System.out.println("drained"); sourceDataLine.stop(); sourceDataLine.close(); System.out.println("closed"); } @Test public void testStartedStopped() throws LineUnavailableException, UnsupportedAudioFileException, IOException { File soundFile = new File("testsounds/startup.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(audioFormat); LineListener startStopListener = new LineListener() { @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("STOP"); stopped++; Assert.assertEquals(1, stopped); } } }; sourceDataLine.addLineListener(startStopListener); byte[] abData = new byte[1000]; int bytesRead = 0; sourceDataLine.start(); while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.stop(); sourceDataLine.close(); Assert.assertEquals(1, started); Assert.assertEquals(1, stopped); } @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 = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); 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++; } } }; sourceDataLine.addLineListener(startStopListener); System.out.println("Launching threadWriter"); ThreadWriter writer = new ThreadWriter(audioInputStream, sourceDataLine); writer.start(); // System.out.println("started"); Thread.sleep(1000); sourceDataLine.stop(); System.out.println("corked"); Thread.sleep(1000); // UNCORK sourceDataLine.start(); 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 = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); 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++; } } }; sourceDataLine.addLineListener(startStopListener); System.out.println("Launching threadWriter"); ThreadWriter writer = new ThreadWriter(audioInputStream, sourceDataLine); writer.start(); // System.out.println("started"); Thread.sleep(1000); sourceDataLine.stop(); Thread.sleep(1000); sourceDataLine.start(); Thread.sleep(1000); sourceDataLine.stop(); Thread.sleep(1000); sourceDataLine.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 = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertNotNull(sourceDataLine); sourceDataLine.start(); } @Test(expected = IllegalStateException.class) public void testStopOnClosedLine() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertNotNull(sourceDataLine); sourceDataLine.stop(); } @Test(expected = IllegalArgumentException.class) public void testPlayLessThanFrameSize() throws LineUnavailableException, UnsupportedAudioFileException, IOException { File soundFile = new File("testsounds/startup.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); // the audio file must have an even number of channels Assert.assertTrue(audioFormat.getChannels() % 2 == 0); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); byte[] data = new byte[1]; data[0] = (byte) 'a'; sourceDataLine.open(); sourceDataLine.start(); try { sourceDataLine.write(data, 0, 1); } finally { sourceDataLine.stop(); sourceDataLine.close(); } } @Test public void testOpenFormat() throws LineUnavailableException, UnsupportedAudioFileException, IOException { /* * This test makes sure that the default format of a line using open() * is the same format that was passed to the mixer's getLine() function * to get the line in the first place */ File soundFile = new File("testsounds/startup.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(); Assert.assertTrue(sourceDataLine.getFormat().matches(audioFormat)); sourceDataLine.close(); } @Test public void testFindLineWithFormat() throws LineUnavailableException { System.out .println("This test tries to find a line with a valid format"); AudioFormat wantedFormat = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true); System.out.println(wantedFormat); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, wantedFormat)); sourceDataLine.open(); System.out.println(sourceDataLine.getFormat()); sourceDataLine.close(); } @Test(expected = IllegalArgumentException.class) public void testFindLineWithWrongFormat() throws LineUnavailableException { System.out .println("This test tries to acquire a line with incorrect format spec"); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED, 44100, 10000, 1, 13, 10, true))); sourceDataLine.open(); sourceDataLine.close(); } @Test public void testFindControl() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(); Control[] controls = sourceDataLine.getControls(); Assert.assertNotNull(controls); Assert.assertTrue(sourceDataLine.getControls().length > 0); for (Control control : controls) { Assert.assertNotNull(control); } sourceDataLine.close(); } @Test public void testSupportedControls() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(); Assert.assertTrue(sourceDataLine .isControlSupported(FloatControl.Type.VOLUME)); Assert.assertTrue(sourceDataLine .isControlSupported(BooleanControl.Type.MUTE)); sourceDataLine.close(); } @Test public void testVolumeAndMute() throws Exception { Mixer selectedMixer = mixer; sourceDataLine = (SourceDataLine) selectedMixer.getLine(new Line.Info( SourceDataLine.class)); File soundFile = new File(new java.io.File(".").getCanonicalPath() + "/testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine.open(audioFormat); sourceDataLine.start(); FloatControl volume = (FloatControl) sourceDataLine .getControl(FloatControl.Type.VOLUME); BooleanControl mute = (BooleanControl) sourceDataLine .getControl(BooleanControl.Type.MUTE); mute.setValue(true); volume.setValue(volume.getMaximum()); mute.setValue(false); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.close(); selectedMixer.close(); } @Test public void testVolumeChanging() throws LineUnavailableException, IOException, UnsupportedAudioFileException { Mixer selectedMixer = mixer; sourceDataLine = (SourceDataLine) selectedMixer.getLine(new Line.Info( SourceDataLine.class)); File soundFile = new File(new java.io.File(".").getCanonicalPath() + "/testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine.open(audioFormat); sourceDataLine.start(); FloatControl volume = (FloatControl) sourceDataLine .getControl(FloatControl.Type.VOLUME); volume.setValue(volume.getMinimum()); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); volume.setValue(volume.getValue() + 100); } } sourceDataLine.drain(); sourceDataLine.close(); selectedMixer.close(); } @Test public void testOpenEvent() throws LineUnavailableException { listenerCalled = 0; LineListener openListener = new LineListener() { public void update(LineEvent event) { Assert.assertTrue(event.getType() == LineEvent.Type.OPEN); PulseAudioSourceDataLineTest.this.listenerCalled++; } }; sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.addLineListener(openListener); sourceDataLine.open(); sourceDataLine.removeLineListener(openListener); sourceDataLine.close(); Assert.assertEquals(1, listenerCalled); listenerCalled = 0; } @Test public void testCloseEvent() throws LineUnavailableException { listenerCalled = 0; LineListener closeListener = new LineListener() { public void update(LineEvent event) { Assert.assertTrue(event.getType() == LineEvent.Type.CLOSE); PulseAudioSourceDataLineTest.this.listenerCalled++; } }; sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(); sourceDataLine.addLineListener(closeListener); sourceDataLine.close(); sourceDataLine.removeLineListener(closeListener); Assert.assertEquals(1, listenerCalled); listenerCalled = 0; } @Test public void testCloseEventWrongListener() throws LineUnavailableException { listenerCalled = 0; LineListener closeListener = new LineListener() { public void update(LineEvent event) { PulseAudioSourceDataLineTest.this.listenerCalled++; } }; sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(); sourceDataLine.addLineListener(closeListener); sourceDataLine.removeLineListener(closeListener); sourceDataLine.close(); Assert.assertEquals(0, listenerCalled); listenerCalled = 0; } @Test public void testFramePosition() throws UnsupportedAudioFileException, IOException, LineUnavailableException { File soundFile = new File("testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(audioFormat); sourceDataLine.start(); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.stop(); System.out.println("frame position: " + sourceDataLine.getFramePosition()); long expected = 136703; long granularity = 100; long pos = sourceDataLine.getFramePosition(); Assert.assertTrue(Math.abs(expected - pos) < granularity); sourceDataLine.close(); } @Test public void testFramePositionAfterPlayingTwice() throws UnsupportedAudioFileException, IOException, LineUnavailableException { File soundFile = new File("testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(audioFormat); sourceDataLine.start(); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.stop(); soundFile = new File("testsounds/logout.wav"); audioInputStream = AudioSystem.getAudioInputStream(soundFile); audioFormat = audioInputStream.getFormat(); sourceDataLine.start(); abData = new byte[1000]; bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.stop(); System.out.println("frame position: " + sourceDataLine.getFramePosition()); long expected = 136703 * 2; long granularity = 100; long pos = sourceDataLine.getFramePosition(); Assert.assertTrue(Math.abs(expected - pos) < granularity); sourceDataLine.close(); } @Test public void testMicroSecondPosition() throws UnsupportedAudioFileException, IOException, LineUnavailableException { File soundFile = new File("testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(audioFormat); sourceDataLine.start(); byte[] abData = new byte[1000]; int bytesRead = 0; while (bytesRead >= 0) { bytesRead = audioInputStream.read(abData, 0, abData.length); if (bytesRead > 0) { sourceDataLine.write(abData, 0, bytesRead); } } sourceDataLine.drain(); sourceDataLine.stop(); System.out.println("time position: " + sourceDataLine.getMicrosecondPosition()); long expected = 6200; long granularity = 100; long pos = sourceDataLine.getMicrosecondPosition(); Assert.assertTrue(Math.abs(expected - pos) < granularity); sourceDataLine.close(); } @Test public void testBufferSizes() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); sourceDataLine.open(aSupportedFormat, 10000); Assert.assertEquals(10000, sourceDataLine.getBufferSize()); sourceDataLine.close(); } @Test public void testHasADefaultFormat() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertNotNull(sourceDataLine.getFormat()); System.out.println(sourceDataLine.getFormat()); } @Test public void testDefaultFormatWithGetLine() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); Assert.assertEquals(aSupportedFormat, sourceDataLine.getFormat()); } @Test public void testDefaultBufferSize() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); Assert.assertEquals(StreamBufferAttributes.SANE_DEFAULT, sourceDataLine .getBufferSize()); } @Test public void testDrainTwice() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); sourceDataLine.open(); sourceDataLine.drain(); sourceDataLine.drain(); sourceDataLine.close(); } @Test public void testDrainWithoutStart() throws LineUnavailableException, UnsupportedAudioFileException, IOException, InterruptedException { File soundFile = new File("testsounds/logout.wav"); AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(soundFile); AudioFormat audioFormat = audioInputStream.getFormat(); sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, audioFormat, 1000)); Assert.assertNotNull(sourceDataLine); sourceDataLine.open(); int available = sourceDataLine.available(); Assert.assertTrue(available > 1000); byte[] abData = new byte[1000]; int bytesRead = 0; bytesRead = audioInputStream.read(abData, 0, abData.length); Assert.assertTrue(bytesRead > 0); sourceDataLine.write(abData, 0, bytesRead); Runnable blocker = new Runnable() { @Override public void run() { sourceDataLine.drain(); } }; Thread th = new Thread(blocker); th.start(); th.join(1000); if (th.isAlive()) { sourceDataLine.close(); th.join(1000); if (th.isAlive()) { Assert .fail("drain() does not return when the line has been closed"); } else { sourceDataLine.close(); } } else { Assert.fail("drain() does not block when there is data on the " + "source data line and it hasnt been started"); } } @Test(expected = IllegalStateException.class) public void testDrainWithoutOpen() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); sourceDataLine.drain(); } @Test public void testFlushTwice() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); sourceDataLine.open(); sourceDataLine.flush(); sourceDataLine.flush(); sourceDataLine.close(); } @Test(expected = IllegalStateException.class) public void testFlushWithoutOpen() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( SourceDataLine.class, aSupportedFormat, 1000)); sourceDataLine.flush(); } @Test public void testMixerKnowsAboutOpenLines() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertEquals(0, mixer.getSourceLines().length); sourceDataLine.open(); Assert.assertEquals(1, mixer.getSourceLines().length); Assert.assertTrue(sourceDataLine == mixer.getSourceLines()[0]); sourceDataLine.close(); Assert.assertEquals(0, mixer.getSourceLines().length); } @Test public void testMixerKnowsAboutOpen2Lines() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertEquals(0, mixer.getSourceLines().length); sourceDataLine.open(aSupportedFormat); Assert.assertEquals(1, mixer.getSourceLines().length); Assert.assertTrue(sourceDataLine == mixer.getSourceLines()[0]); sourceDataLine.close(); Assert.assertEquals(0, mixer.getSourceLines().length); } @Test public void testMixerKnowsAboutOpen3Lines() throws LineUnavailableException { sourceDataLine = (SourceDataLine) mixer.getLine(new Line.Info( SourceDataLine.class)); Assert.assertEquals(0, mixer.getSourceLines().length); sourceDataLine.open(aSupportedFormat, 10000); Assert.assertEquals(1, mixer.getSourceLines().length); Assert.assertTrue(sourceDataLine == mixer.getSourceLines()[0]); sourceDataLine.close(); Assert.assertEquals(0, mixer.getSourceLines().length); } @Test public void testAllSourceLinesClosed() { Assert.assertEquals(0, mixer.getSourceLines().length); } @After public void tearDown() throws Exception { started = 0; stopped = 0; if (sourceDataLine != null && sourceDataLine.isOpen()) { sourceDataLine.close(); } if (mixer.isOpen()) { mixer.close(); } } }