1 /* 2 * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import javax.sound.sampled.AudioFormat; 25 import javax.sound.sampled.AudioSystem; 26 import javax.sound.sampled.Clip; 27 import javax.sound.sampled.DataLine; 28 import javax.sound.sampled.Mixer; 29 30 /** 31 * @test 32 * @bug 4732218 33 * @summary Clip.drain does not actually block until all I/O is complete as 34 * documented. 35 */ 36 public class ClipDrain { 37 static int successfulTests = 0; 38 static AudioFormat format = new AudioFormat(8000, 16, 1, true, false); 39 // create a 10-second file 40 static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 10)]; 41 42 static int TOLERANCE_MS = 2500; // how many milliseconds too short is tolerated... 43 44 private static void doMixerClip(Mixer mixer) throws Exception { 45 boolean waitedEnough=false; 46 try { 47 DataLine.Info info = new DataLine.Info(Clip.class, format); 48 Clip clip = (Clip) mixer.getLine(info); 49 clip.open(format, soundData, 0, soundData.length); 50 51 // sanity 52 if (clip.getMicrosecondLength()/1000 < 9900) { 53 throw new Exception("clip's microsecond length should be at least 9900000, but it is "+clip.getMicrosecondLength()); 54 } 55 long start = System.currentTimeMillis(); 56 57 System.out.println(" ---------- start --------"); 58 clip.start(); 59 // give time to actually start it. ALSA implementation needs that... 60 Thread.sleep(300); 61 System.out.println("drain ... "); 62 clip.drain(); 63 long elapsedTime = System.currentTimeMillis() - start; 64 System.out.println("close ... "); 65 clip.close(); 66 System.out.println("... done"); 67 System.out.println("Playback duration: "+elapsedTime+" milliseconds."); 68 waitedEnough = elapsedTime >= ((clip.getMicrosecondLength() / 1000) - TOLERANCE_MS); 69 } catch (Throwable t) { 70 System.out.println(" - Caught exception. Not failed."); 71 System.out.println(" - "+t.toString()); 72 return; 73 } 74 if (!waitedEnough) { 75 throw new Exception("Drain did not wait long enough to play entire clip."); 76 } 77 successfulTests++; 78 } 79 80 81 private static void doAll() throws Exception { 82 Mixer.Info[] mixers = AudioSystem.getMixerInfo(); 83 for (int i=0; i<mixers.length; i++) { 84 Mixer mixer = AudioSystem.getMixer(mixers[i]); 85 System.out.println("--------------"); 86 System.out.println("Testing mixer: "+mixers[i]); 87 doMixerClip(mixer); 88 } 89 if (mixers.length==0) { 90 System.out.println("No mixers available!"); 91 } 92 } 93 94 public static void main(String[] args) throws Exception { 95 if (!isSoundcardInstalled()) { 96 return; 97 } 98 doAll(); 99 if (successfulTests==0) { 100 System.out.println("Could not execute any of the tests. Test NOT failed."); 101 } else { 102 System.out.println("Test PASSED."); 103 } 104 } 105 106 /** 107 * Returns true if at least one soundcard is correctly installed 108 * on the system. 109 */ 110 public static boolean isSoundcardInstalled() { 111 boolean result = false; 112 try { 113 Mixer.Info[] mixers = AudioSystem.getMixerInfo(); 114 if (mixers.length > 0) { 115 result = AudioSystem.getSourceDataLine(null) != null; 116 } 117 } catch (Exception e) { 118 System.err.println("Exception occured: "+e); 119 } 120 if (!result) { 121 System.err.println("Soundcard does not exist or sound drivers not installed!"); 122 System.err.println("This test requires sound drivers for execution."); 123 } 124 return result; 125 } 126 }