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/AudioSystem/DefaultMixers.java
38855 views
1
/*
2
* Copyright (c) 2003, 2020, 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.util.List;
25
26
import javax.sound.sampled.AudioFormat;
27
import javax.sound.sampled.AudioSystem;
28
import javax.sound.sampled.Clip;
29
import javax.sound.sampled.DataLine;
30
import javax.sound.sampled.Line;
31
import javax.sound.sampled.Mixer;
32
import javax.sound.sampled.Port;
33
import javax.sound.sampled.SourceDataLine;
34
import javax.sound.sampled.TargetDataLine;
35
import javax.sound.sampled.spi.MixerProvider;
36
37
import com.sun.media.sound.JDK13Services;
38
39
/**
40
* @test
41
* @bug 4776511
42
* @summary RFE: Setting the default MixerProvider. Test the retrieving of lines
43
* with defaut mixer properties.
44
*/
45
public class DefaultMixers {
46
47
private static final String ERROR_PROVIDER_CLASS_NAME = "abc";
48
private static final String ERROR_INSTANCE_NAME = "def";
49
50
private static final Class[] lineClasses = {
51
SourceDataLine.class,
52
TargetDataLine.class,
53
Clip.class,
54
Port.class,
55
};
56
57
public static void main(String[] args) throws Exception {
58
boolean allOk = true;
59
Mixer.Info[] infos;
60
61
out("Testing Mixers retrieved via AudioSystem");
62
infos = AudioSystem.getMixerInfo();
63
allOk &= testMixers(infos, null);
64
65
out("Testing MixerProviders");
66
List providers = JDK13Services.getProviders(MixerProvider.class);
67
for (int i = 0; i < providers.size(); i++) {
68
MixerProvider provider = (MixerProvider) providers.get(i);
69
infos = provider.getMixerInfo();
70
allOk &= testMixers(infos, provider.getClass().getName());
71
}
72
73
if (! allOk) {
74
throw new Exception("Test failed");
75
} else {
76
out("Test passed");
77
}
78
}
79
80
private static boolean testMixers(Mixer.Info[] infos,
81
String providerClassName) {
82
boolean allOk = true;
83
84
for (int i = 0; i < infos.length; i++) {
85
Mixer mixer = null;
86
try {
87
mixer = AudioSystem.getMixer(infos[i]);
88
} catch (NullPointerException e) {
89
out("Exception thrown; Test NOT failed.");
90
e.printStackTrace();
91
}
92
for (int j = 0; j < lineClasses.length; j++) {
93
if (mixer.isLineSupported(new Line.Info(lineClasses[j]))) {
94
allOk &= testMixer(mixer, lineClasses[j],
95
providerClassName);
96
}
97
}
98
}
99
return allOk;
100
}
101
102
private static boolean testMixer(Mixer mixer, Class lineType,
103
String providerClassName) {
104
boolean allOk = true;
105
String instanceName = mixer.getMixerInfo().getName();
106
107
// no error
108
allOk &= testMixer(mixer, lineType,
109
providerClassName, instanceName);
110
111
// erroneous provider class name, correct instance name
112
allOk &= testMixer(mixer, lineType,
113
ERROR_PROVIDER_CLASS_NAME, instanceName);
114
115
// erroneous provider class name, no instance name
116
allOk &= testMixer(mixer, lineType,
117
ERROR_PROVIDER_CLASS_NAME, "");
118
119
// erroneous provider class name, erroneous instance name
120
allOk &= testMixer(mixer, lineType,
121
ERROR_PROVIDER_CLASS_NAME, ERROR_INSTANCE_NAME);
122
123
return allOk;
124
}
125
126
private static boolean testMixer(Mixer mixer, Class lineType,
127
String providerClassName,
128
String instanceName) {
129
boolean allOk = true;
130
131
try {
132
String propertyValue = (providerClassName != null) ? providerClassName: "" ;
133
propertyValue += "#" + instanceName;
134
out("property value: " + propertyValue);
135
System.setProperty(lineType.getName(), propertyValue);
136
Line line = null;
137
Line.Info info = null;
138
Line.Info[] infos;
139
AudioFormat format = null;
140
if (lineType == SourceDataLine.class || lineType == Clip.class) {
141
infos = mixer.getSourceLineInfo();
142
format = getFirstLinearFormat(infos);
143
info = new DataLine.Info(lineType, format);
144
} else if (lineType == TargetDataLine.class) {
145
infos = mixer.getTargetLineInfo();
146
format = getFirstLinearFormat(infos);
147
info = new DataLine.Info(lineType, format);
148
} else if (lineType == Port.class) {
149
/* Actually, a Ports Mixer commonly has source infos
150
as well as target infos. We ignore this here, since we
151
just need a random one. */
152
infos = mixer.getSourceLineInfo();
153
for (int i = 0; i < infos.length; i++) {
154
if (infos[i] instanceof Port.Info) {
155
info = infos[i];
156
break;
157
}
158
}
159
}
160
out("Line.Info: " + info);
161
line = AudioSystem.getLine(info);
162
out("line: " + line);
163
if (! lineType.isInstance(line)) {
164
out("type " + lineType + " failed: class should be '" +
165
lineType + "' but is '" + line.getClass() + "'!");
166
allOk = false;
167
}
168
} catch (Exception e) {
169
out("Exception thrown; Test NOT failed.");
170
e.printStackTrace();
171
}
172
return allOk;
173
}
174
175
private static AudioFormat getFirstLinearFormat(Line.Info[] infos) {
176
for (int i = 0; i < infos.length; i++) {
177
if (infos[i] instanceof DataLine.Info) {
178
AudioFormat[] formats = ((DataLine.Info) infos[i]).getFormats();
179
for (int j = 0; j < formats.length; j++) {
180
AudioFormat.Encoding encoding = formats[j].getEncoding();
181
int sampleSizeInBits = formats[j].getSampleSizeInBits();
182
if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED) &&
183
sampleSizeInBits == 16 ||
184
encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) &&
185
sampleSizeInBits == 16) {
186
return formats[j];
187
}
188
}
189
}
190
}
191
return null;
192
}
193
194
private static void out(String message) {
195
System.out.println(message);
196
}
197
}
198
199