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