|
1 /* |
|
2 * Copyright (c) 2002, 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 import java.io.InputStream; |
|
26 |
|
27 import javax.sound.sampled.AudioFormat; |
|
28 import javax.sound.sampled.AudioInputStream; |
|
29 import javax.sound.sampled.AudioSystem; |
|
30 import javax.sound.sampled.Clip; |
|
31 import javax.sound.sampled.DataLine; |
|
32 import javax.sound.sampled.LineEvent; |
|
33 import javax.sound.sampled.LineListener; |
|
34 |
|
35 /** |
|
36 * @test |
|
37 * @bug 4498848 |
|
38 * @summary Sound causes crashes on Linux (part 1) |
|
39 */ |
|
40 public class ClipLinuxCrash { |
|
41 |
|
42 static Clip clip; |
|
43 |
|
44 public static long bytes2Ms(long bytes, AudioFormat format) { |
|
45 return (long) (bytes / format.getFrameRate() * 1000 |
|
46 / format.getFrameSize()); |
|
47 } |
|
48 |
|
49 static int staticLen = 1000; |
|
50 |
|
51 static boolean addLen = true; |
|
52 |
|
53 public static long start() throws Exception { |
|
54 AudioFormat fmt = new AudioFormat(44100, 16, 2, true, false); |
|
55 if (addLen) { |
|
56 staticLen += (int) (staticLen / 5) + 1000; |
|
57 } else { |
|
58 staticLen -= (int) (staticLen / 5) + 1000; |
|
59 } |
|
60 if (staticLen > 8 * 44100 * 4) { |
|
61 staticLen = 8 * 44100 * 4; |
|
62 addLen = !addLen; |
|
63 } |
|
64 if (staticLen < 1000) { |
|
65 staticLen = 1000; |
|
66 addLen = !addLen; |
|
67 } |
|
68 int len = staticLen; |
|
69 len -= (len % 4); |
|
70 byte[] fakedata = new byte[len]; |
|
71 InputStream is = new ByteArrayInputStream(fakedata); |
|
72 AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, |
|
73 44100, 16, 2, 4, 44100, false); |
|
74 AudioInputStream ais = new AudioInputStream(is, format, fakedata.length |
|
75 / format.getFrameSize()); |
|
76 |
|
77 out(" preparing to play back " + len + " bytes == " + bytes2Ms(len, |
|
78 format) |
|
79 + "ms audio..."); |
|
80 |
|
81 DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat()); |
|
82 clip = (Clip) AudioSystem.getLine(info); |
|
83 clip.addLineListener(new LineListener() { |
|
84 public void update(LineEvent e) { |
|
85 if (e.getType() == LineEvent.Type.STOP) { |
|
86 out(" calling close() from event dispatcher thread"); |
|
87 ((Clip) e.getSource()).close(); |
|
88 } else if (e.getType() == LineEvent.Type.CLOSE) { |
|
89 } |
|
90 } |
|
91 }); |
|
92 |
|
93 out(" opening..."); |
|
94 try { |
|
95 clip.open(ais); |
|
96 } catch (Throwable t) { |
|
97 t.printStackTrace(); |
|
98 clip.close(); |
|
99 clip = null; |
|
100 } |
|
101 ais.close(); |
|
102 if (clip != null) { |
|
103 out(" starting..."); |
|
104 clip.start(); |
|
105 } |
|
106 return bytes2Ms(fakedata.length, format); |
|
107 } |
|
108 |
|
109 public static void main(String[] args) throws Exception { |
|
110 if (AudioSystem.getMixerInfo().length == 0) { |
|
111 System.out.println("Cannot execute test: no mixers installed!"); |
|
112 System.out.println("Not Failed."); |
|
113 return; |
|
114 } |
|
115 try { |
|
116 int COUNT = 10; |
|
117 out(); |
|
118 out("4498848 Sound causes crashes on Linux (testing with Clip)"); |
|
119 if (args.length > 0) { |
|
120 COUNT = Integer.parseInt(args[0]); |
|
121 } |
|
122 for (int i = 0; i < COUNT; i++) { |
|
123 out(" trial " + (i + 1) + "/" + COUNT); |
|
124 start(); |
|
125 int waitTime = 500 + (1000 * (i |
|
126 % 2)); // every second |
|
127 // time wait 1500, rather than 500ms. |
|
128 out(" waiting for " + waitTime |
|
129 + " ms for audio playback to stop..."); |
|
130 Thread.sleep(waitTime); |
|
131 out(" calling close() from main thread"); |
|
132 if (clip != null) { |
|
133 clip.close(); |
|
134 } |
|
135 // let the subsystem enough time to actually close the soundcard |
|
136 out(" waiting for 2 seconds..."); |
|
137 Thread.sleep(2000); |
|
138 out(); |
|
139 } |
|
140 out(" waiting for 1 second..."); |
|
141 Thread.sleep(1000); |
|
142 } catch (Exception e) { |
|
143 e.printStackTrace(); |
|
144 out(" waiting for 1 second"); |
|
145 try { |
|
146 Thread.sleep(1000); |
|
147 } catch (InterruptedException ie) { |
|
148 } |
|
149 throw e; |
|
150 } |
|
151 out("Test passed"); |
|
152 } |
|
153 |
|
154 static void out() { |
|
155 out(""); |
|
156 } |
|
157 |
|
158 static void out(String s) { |
|
159 System.out.println(s); |
|
160 System.out.flush(); |
|
161 } |
|
162 |
|
163 } |