# HG changeset patch # User Omair Majid # Date 1222109063 14400 # Node ID b7a5a39b31ab02b70ed6045b67438eb3d9d94132 # Parent 11a52266951ff44c21fe13baeb0bbf137ec1acca 2008-09-22 Omair Majid * src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java (ClipThread.run): Drain when done playing. (close): Interrupt the clipThread to stop it immediately. (drain): Interrupt and drain the buffer, dont block. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java (testStartedStopped): Calls drain to wait for the line to finish playing. (testDrainWithoutStart): New test. diff -r 11a52266951f -r b7a5a39b31ab src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java --- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java Mon Sep 22 14:10:35 2008 -0400 +++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java Mon Sep 22 14:44:23 2008 -0400 @@ -73,6 +73,7 @@ private class ClipThread extends Thread { @Override public void run() { + clipThreadStarted = true; while (loopsLeft >= 0) { writeFrames(currentFrame, endFrame + 1); @@ -101,9 +102,9 @@ break; } - PulseAudioClip.this.drain(); + } - } + PulseAudioClip.this.drain(); } } @@ -193,6 +194,8 @@ throw new IllegalStateException("line already closed"); } + clipThread.interrupt(); + try { clipThread.join(); } catch (InterruptedException e) { @@ -212,6 +215,13 @@ throw new IllegalStateException("line not open"); } + if (clipThread != null) { + clipThread.interrupt(); + try { + clipThread.join(); + } catch (InterruptedException e) { + } + } Operation operation; synchronized (eventLoop.threadLock) { @@ -447,6 +457,7 @@ clipThread = new ClipThread(); clipThread.start(); } + } public void stop() { @@ -472,6 +483,7 @@ } super.stop(); + } } diff -r 11a52266951f -r b7a5a39b31ab unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java --- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java Mon Sep 22 14:10:35 2008 -0400 +++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java Mon Sep 22 14:44:23 2008 -0400 @@ -64,6 +64,11 @@ AudioFormat aSupportedFormat = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true); + int started = 0; + int stopped = 0; + int opened = 0; + int closed = 0; + @Before public void setUp() throws LineUnavailableException { Mixer.Info wantedMixerInfo = null; @@ -77,6 +82,11 @@ assert (wantedMixerInfo != null); mixer = AudioSystem.getMixer(wantedMixerInfo); mixer.open(); + + started = 0; + stopped = 0; + opened = 0; + closed = 0; } @Test @@ -158,8 +168,6 @@ } - int opened = 0; - @Test public void testOpenEvent() throws LineUnavailableException, UnsupportedAudioFileException, IOException { @@ -191,8 +199,6 @@ } - int closed = 0; - @Test public void testCloseEvent() throws LineUnavailableException, UnsupportedAudioFileException, IOException { @@ -228,12 +234,9 @@ } - int started = 0; - int stopped = 0; - @Test public void testStartedStopped() throws LineUnavailableException, - UnsupportedAudioFileException, IOException { + UnsupportedAudioFileException, IOException, InterruptedException { File soundFile = new File("testsounds/startup.wav"); AudioInputStream audioInputStream = AudioSystem @@ -244,9 +247,6 @@ clip = (Clip) mixer.getLine(new DataLine.Info(Clip.class, audioFormat)); Assert.assertNotNull(clip); - started = 0; - stopped = 0; - clip.open(audioInputStream); LineListener startStopListener = new LineListener() { @@ -271,16 +271,31 @@ clip.addLineListener(startStopListener); clip.start(); - // clip.drain(); - + clip.drain(); clip.stop(); clip.close(); Assert.assertEquals(1, started); Assert.assertEquals(1, stopped); - started = 0; - stopped = 0; + } + + @Test + public void testDrainWithoutStart() throws UnsupportedAudioFileException, + IOException, LineUnavailableException { + + File soundFile = new File("testsounds/startup.wav"); + AudioInputStream audioInputStream = AudioSystem + .getAudioInputStream(soundFile); + AudioFormat audioFormat = audioInputStream.getFormat(); + + Clip clip; + clip = (Clip) mixer.getLine(new DataLine.Info(Clip.class, audioFormat)); + Assert.assertNotNull(clip); + + clip.open(audioInputStream); + clip.drain(); + clip.close(); } @@ -387,6 +402,7 @@ @After public void tearDown() { mixer.close(); + } }