1 /* 2 * Copyright (c) 2003, 2017, 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 java.io.ByteArrayInputStream; 25 26 import javax.sound.sampled.AudioFormat; 27 import javax.sound.sampled.AudioInputStream; 28 import javax.sound.sampled.AudioSystem; 29 import javax.sound.sampled.Clip; 30 import javax.sound.sampled.DataLine; 31 import javax.sound.sampled.LineUnavailableException; 32 import javax.sound.sampled.Mixer; 33 34 /** 35 * @test 36 * @bug 4946913 8178403 37 * @summary DirectClip doesn't kill the thread correctly, sometimes 38 * @run main/othervm ClipCloseLoss 39 */ 40 public class ClipCloseLoss { 41 static int frameCount = 441000; // lets say 10 seconds 42 static AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false); 43 static ByteArrayInputStream bais = 44 new ByteArrayInputStream(new byte[frameCount * format.getFrameSize()]); 45 46 static int success = 0; 47 static boolean failed = false; 48 49 public static void run(Mixer m, long sleep) { 50 Clip clip = null; 51 try { 52 if (m == null) { 53 out("Using default mixer"); 54 clip = (Clip) AudioSystem.getClip(); 55 } else { 56 out("Using mixer: "+m); 57 DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED); 58 clip = (Clip) m.getLine(info); 59 } 60 out(" got clip: "+clip); 61 if (!clip.getClass().toString().contains("Direct")) { 62 out(" no direct audio clip -> do not test."); 63 return; 64 } 65 66 out(" open"); 67 bais.reset(); 68 clip.open(new AudioInputStream(bais, format, frameCount)); 69 70 out(" clip.close()"); 71 // emulates a different delay between open() and close() 72 Thread.sleep(sleep); 73 //long t = System.currentTimeMillis(); 74 clip.close(); 75 //if (System.currentTimeMillis() - t > 1950) { 76 // out(" clip.close needed more than 2 seconds! Causes failure of this test."); 77 // failed = true; 78 //} 79 out(" clip closed"); 80 success++; 81 } catch (LineUnavailableException luae) { 82 // line not available, test not failed 83 System.err.println(luae); 84 } catch (IllegalArgumentException iae) { 85 // line not available, test not failed 86 System.err.println(iae); 87 } catch (Throwable t) { 88 t.printStackTrace(); 89 } 90 } 91 92 public static int getClipThreadCount() { 93 int ret = 0; 94 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 95 while (tg.getParent() != null) { tg = tg.getParent(); } 96 Thread[] threads = new Thread[500]; 97 int count = tg.enumerate(threads, true); 98 for (int i = 0; i < count; i++) { 99 if (threads[i].getName().contains("Direct") 100 && threads[i].getName().contains("Clip")) { 101 out("Found Direct Clip thread object: "+threads[i]); 102 ret++; 103 } 104 } 105 return ret; 106 } 107 108 public static void main(String[] args) throws Exception { 109 if (isSoundcardInstalled()) { 110 bais.mark(0); 111 Mixer.Info[] infos = AudioSystem.getMixerInfo(); 112 for (int sleep = 0; sleep < 100; ++sleep) { 113 run(null, sleep); 114 for (int i = 0; i < infos.length; i++) { 115 try { 116 Mixer m = AudioSystem.getMixer(infos[i]); 117 run(m, sleep); 118 } catch (Exception e) { 119 } 120 } 121 } 122 out("Waiting 1 second to dispose of all threads"); 123 Thread.sleep(1000); 124 if (getClipThreadCount() > 0) { 125 out("Unused clip threads exist! Causes test failure"); 126 failed = true; 127 } 128 if (failed) throw new Exception("Test FAILED!"); 129 if (success > 0) { 130 out("Test passed."); 131 } else { 132 System.err.println("Test could not execute: please install an audio device"); 133 } 134 } 135 } 136 137 /** 138 * Returns true if at least one soundcard is correctly installed 139 * on the system. 140 */ 141 public static boolean isSoundcardInstalled() { 142 boolean result = false; 143 try { 144 Mixer.Info[] mixers = AudioSystem.getMixerInfo(); 145 if (mixers.length > 0) { 146 result = AudioSystem.getSourceDataLine(null) != null; 147 } 148 } catch (Exception e) { 149 System.err.println("Exception occured: "+e); 150 } 151 if (!result) { 152 System.err.println("Soundcard does not exist or sound drivers not installed!"); 153 System.err.println("This test requires sound drivers for execution."); 154 } 155 return result; 156 } 157 158 public static void out(String s) { 159 /*long t = System.nanoTime() / 1000000l; 160 String ts = ""+(t % 1000); 161 while (ts.length() < 3) ts = "0"+ts; 162 System.out.println(""+(t/1000)+":"+ts+" "+s); 163 System.out.flush();*/ 164 System.out.println(s); 165 } 166 }