Path: blob/master/test/jdk/javax/sound/sampled/LinuxCrash/ClipLinuxCrash.java
41153 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.InputStream;2526import javax.sound.sampled.AudioFormat;27import javax.sound.sampled.AudioInputStream;28import javax.sound.sampled.AudioSystem;29import javax.sound.sampled.Clip;30import javax.sound.sampled.DataLine;31import javax.sound.sampled.LineEvent;32import javax.sound.sampled.LineListener;3334/**35* @test36* @bug 449884837* @summary Sound causes crashes on Linux (part 1)38*/39public class ClipLinuxCrash {4041static Clip clip;4243public static long bytes2Ms(long bytes, AudioFormat format) {44return (long) (bytes / format.getFrameRate() * 100045/ format.getFrameSize());46}4748static int staticLen = 1000;4950static boolean addLen = true;5152public static long start() throws Exception {53AudioFormat fmt = new AudioFormat(44100, 16, 2, true, false);54if (addLen) {55staticLen += (int) (staticLen / 5) + 1000;56} else {57staticLen -= (int) (staticLen / 5) + 1000;58}59if (staticLen > 8 * 44100 * 4) {60staticLen = 8 * 44100 * 4;61addLen = !addLen;62}63if (staticLen < 1000) {64staticLen = 1000;65addLen = !addLen;66}67int len = staticLen;68len -= (len % 4);69byte[] fakedata = new byte[len];70InputStream is = new ByteArrayInputStream(fakedata);71AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,7244100, 16, 2, 4, 44100, false);73AudioInputStream ais = new AudioInputStream(is, format, fakedata.length74/ format.getFrameSize());7576out(" preparing to play back " + len + " bytes == " + bytes2Ms(len,77format)78+ "ms audio...");7980DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());81clip = (Clip) AudioSystem.getLine(info);82clip.addLineListener(new LineListener() {83public void update(LineEvent e) {84if (e.getType() == LineEvent.Type.STOP) {85out(" calling close() from event dispatcher thread");86((Clip) e.getSource()).close();87} else if (e.getType() == LineEvent.Type.CLOSE) {88}89}90});9192out(" opening...");93try {94clip.open(ais);95} catch (Throwable t) {96t.printStackTrace();97clip.close();98clip = null;99}100ais.close();101if (clip != null) {102out(" starting...");103clip.start();104}105return bytes2Ms(fakedata.length, format);106}107108public static void main(String[] args) throws Exception {109if (AudioSystem.getMixerInfo().length == 0) {110System.out.println("Cannot execute test: no mixers installed!");111System.out.println("Not Failed.");112return;113}114try {115int COUNT = 10;116out();117out("4498848 Sound causes crashes on Linux (testing with Clip)");118if (args.length > 0) {119COUNT = Integer.parseInt(args[0]);120}121for (int i = 0; i < COUNT; i++) {122out(" trial " + (i + 1) + "/" + COUNT);123start();124int waitTime = 500 + (1000 * (i125% 2)); // every second126// time wait 1500, rather than 500ms.127out(" waiting for " + waitTime128+ " ms for audio playback to stop...");129Thread.sleep(waitTime);130out(" calling close() from main thread");131if (clip != null) {132clip.close();133}134// let the subsystem enough time to actually close the soundcard135out(" waiting for 2 seconds...");136Thread.sleep(2000);137out();138}139out(" waiting for 1 second...");140Thread.sleep(1000);141} catch (Exception e) {142e.printStackTrace();143out(" waiting for 1 second");144try {145Thread.sleep(1000);146} catch (InterruptedException ie) {147}148throw e;149}150out("Test passed");151}152153static void out() {154out("");155}156157static void out(String s) {158System.out.println(s);159System.out.flush();160}161162}163164165