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
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) {
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 //long t = System.currentTimeMillis();
72 clip.close();
73 //if (System.currentTimeMillis() - t > 1950) {
74 // out(" clip.close needed more than 2 seconds! Causes failure of this test.");
75 // failed = true;
76 //}
77 out(" clip closed");
78 success++;
79 } catch (LineUnavailableException luae) {
80 // line not available, test not failed
81 System.err.println(luae);
82 } catch (IllegalArgumentException iae) {
83 // line not available, test not failed
84 System.err.println(iae);
85 } catch (Throwable t) {
86 t.printStackTrace();
87 }
88 }
89
90 public static int getClipThreadCount() {
91 int ret = 0;
92 ThreadGroup tg = Thread.currentThread().getThreadGroup();
93 while (tg.getParent() != null) { tg = tg.getParent(); }
94 Thread[] threads = new Thread[500];
95 int count = tg.enumerate(threads, true);
96 for (int i = 0; i < count; i++) {
97 if (threads[i].getName().contains("Direct")
98 && threads[i].getName().contains("Clip")) {
99 out("Found Direct Clip thread object: "+threads[i]);
100 ret++;
101 }
102 }
103 return ret;
104 }
105
106 public static void main(String[] args) throws Exception {
107 if (isSoundcardInstalled()) {
108 bais.mark(0);
109 run(null);
110 Mixer.Info[] infos = AudioSystem.getMixerInfo();
111 for (int i = 0; i<infos.length; i++) {
112 try {
113 Mixer m = AudioSystem.getMixer(infos[i]);
114 run(m);
115 } catch (Exception e) {
116 }
117 }
118 out("Waiting 1 second to dispose of all threads");
119 Thread.sleep(1000);
120 if (getClipThreadCount() > 0) {
121 out("Unused clip threads exist! Causes test failure");
122 failed = true;
123 }
124 if (failed) throw new Exception("Test FAILED!");
125 if (success > 0) {
126 out("Test passed.");
127 } else {
128 System.err.println("Test could not execute: please install an audio device");
129 }
130 }
131 }
132
133 /**
134 * Returns true if at least one soundcard is correctly installed
135 * on the system.
136 */
137 public static boolean isSoundcardInstalled() {
138 boolean result = false;
139 try {
140 Mixer.Info[] mixers = AudioSystem.getMixerInfo();
141 if (mixers.length > 0) {
142 result = AudioSystem.getSourceDataLine(null) != null;
143 }
144 } catch (Exception e) {
145 System.err.println("Exception occured: "+e);
146 }
147 if (!result) {
148 System.err.println("Soundcard does not exist or sound drivers not installed!");
149 System.err.println("This test requires sound drivers for execution.");
150 }
151 return result;
152 }
153
154 public static void out(String s) {
155 /*long t = System.nanoTime() / 1000000l;
156 String ts = ""+(t % 1000);
157 while (ts.length() < 3) ts = "0"+ts;
158 System.out.println(""+(t/1000)+":"+ts+" "+s);
159 System.out.flush();*/
160 System.out.println(s);
161 }
162 }
--- EOF ---