forked from openjdk/jdk
/
StopStart.java
283 lines (263 loc) · 10 KB
/
StopStart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.File;
import java.util.Random;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
/**
* @test
* @key sound
* @bug 4828556
* @summary stopping and starting sampled audio plays small chunk in infinite
* loop
*/
public class StopStart implements Runnable {
static int sampleRate = 8000;
static double frequency = 2000.0;
static double RAD = 2.0 * Math.PI;
static Random random = new Random();
static byte[] audioData = new byte[sampleRate/2];
static SourceDataLine source;
static boolean terminated = false;
static int buffersWritten = 0;
static long bytesWritten = 0;
static int buffersWrittenAfter5Seconds;
static AudioInputStream ais = null;
static AudioFormat audioFormat;
static String filename;
static int executedTests=0;
static int successfulTests = 0;
public static void constructAIS() throws Exception {
ais = AudioSystem.getAudioInputStream(new File(filename));
}
public static void doStartStopTest1() throws Exception {
System.out.println("TEST 1: play for 3 seconds, stop/start/stop/start/play for 3 seconds...");
source.start();
Thread.sleep(100);
bytesWritten = 0;
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
buffersWrittenAfter5Seconds = buffersWritten;
System.out.println("Buffers Written: "+buffersWritten);
System.out.println("stop()->start()->stop()->start()");
source.stop();
//System.out.println("start()");
source.start();
//System.out.println("stop()2 ----------------------------------------------------------");
source.stop();
//System.out.println("start()");
source.start();
System.out.println("Buffers Written: "+buffersWritten);
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
System.out.println("Buffers Written: "+buffersWritten);
if (buffersWritten >= ((buffersWrittenAfter5Seconds * 2) - ((buffersWrittenAfter5Seconds / 4)))) {
successfulTests++;
}
}
private static int nextWaitTime() {
int waitTime = random.nextInt(25);
waitTime*=waitTime;
if (waitTime<20) waitTime = 0;
return waitTime;
}
public static void doStartStopTest2() throws Exception {
System.out.println("TEST 2: start and stop 100 times with random wait in between");
int max=100;
for (int i=0; i<max; i++) {
System.out.println("Round "+i);
System.out.println("Start....");
source.start();
int waitTime = nextWaitTime();
System.out.println("Waiting for "+waitTime+"ms...");
if (waitTime>0) {
Thread.sleep(waitTime);
}
System.out.println("stop()");
source.stop();
waitTime = nextWaitTime();
System.out.println("Waiting for "+waitTime+"ms...");
if (waitTime>0) {
Thread.sleep(waitTime);
}
}
}
public static void doStartStopTest3() throws Exception {
System.out.println("TEST 3: start and stop 100 times with random wait only every 10 rounds ");
int max=100;
for (int i=0; i<max; i++) {
System.out.println("Round "+i);
System.out.println("Start....");
source.start();
if (i % 10 == 9) {
int waitTime = nextWaitTime();
System.out.println("Waiting for "+waitTime+"ms...");
if (waitTime>0) {
Thread.sleep(waitTime);
}
}
System.out.println("stop()");
source.stop();
if (i % 13 == 12) {
int waitTime = nextWaitTime();
System.out.println("Waiting for "+waitTime+"ms...");
if (waitTime>0) {
Thread.sleep(waitTime);
}
}
}
}
public static void runTest(int testNum) {
terminated = false;
Thread thread = null;
buffersWrittenAfter5Seconds = 0;
// make the tests reproduceable by always seeding with same value
random.setSeed(1);
try {
executedTests++;
thread = new Thread(new StopStart());
thread.start();
switch (testNum) {
case 1: doStartStopTest1(); break;
case 2: doStartStopTest2(); break;
case 3: doStartStopTest3(); break;
}
} catch (Exception e) {
e.printStackTrace();
}
source.stop();
source.close();
if (thread!=null) {
terminated = true;
System.out.println("Waiting for thread to die...");
try {
thread.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
filename = null;
if (args.length>0) {
File f = new File(args[0]);
if (f.exists()) {
filename = args[0];
System.out.println("Opening "+filename);
constructAIS();
audioFormat = ais.getFormat();
}
}
if (filename == null) {
audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
for (int i=0; i<audioData.length; i++) {
audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
}
}
long startTime = System.currentTimeMillis();
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (int i=0; i<mixers.length; i++) {
try {
Mixer mixer = AudioSystem.getMixer(mixers[i]);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
String mixerName = mixer.getMixerInfo().getName();
try {
source = (SourceDataLine) mixer.getLine(info);
source.open(audioFormat);
} catch (IllegalArgumentException iae) {
System.out.println("Mixer "+mixerName+" does not provide a SourceDataLine.");
continue;
} catch (LineUnavailableException lue) {
System.out.println("Mixer "+mixerName+": no lines available.");
continue;
}
System.out.println("***** Testing on Mixer "+mixerName+":");
//runTest(2);
//runTest(3);
runTest(1);
} catch (Exception e) {
e.printStackTrace();
}
}
if (mixers.length==0) {
System.out.println("No mixers available!");
} else {
long duration = System.currentTimeMillis() - startTime;
System.out.println("Test took "+(duration/1000)+"s and "+(duration % 1000)+"ms.");
}
System.out.println("Exiting main()");
if (executedTests>0) {
if (successfulTests == 0) {
if (args.length == 0) {
throw new Exception("Test FAILED");
}
System.out.println("test FAILED.");
} else {
System.out.println("test successful.");
}
} else {
System.out.println("Could not execute any tests - are soundcards correctly installed?");
System.out.println("Test NOT FAILED.");
}
}
public void run() {
int len = audioData.length;
int offset = len;
System.out.println("Thread: started. Beginning audio i/o");
while (!terminated) {
try {
//if (!source.isActive()) {
// Thread.sleep(50);
//}
if (offset >= len) {
offset = 0;
if (ais!=null) {
do {
len = ais.read(audioData, 0, audioData.length);
if (len < 0) {
constructAIS();
}
} while (len < 0);
}
}
int toWrite = len - offset;
int written = source.write(audioData, offset, toWrite);
offset+=written;
bytesWritten += written;
buffersWritten = (int) (bytesWritten / audioData.length);
} catch (Exception e) {
e.printStackTrace();
terminated = true;
}
}
System.out.println("Thread: closing line");
source.stop();
source.close();
System.out.println("Thread finished");
}
}