Path: blob/master/test/jdk/javax/sound/sampled/LinuxCrash/ClipLinuxCrash2.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;33import javax.sound.sampled.LineUnavailableException;34import javax.sound.sampled.Mixer;3536/**37* @test38* @bug 449884839* @summary Sound causes crashes on Linux (part 3)40*/41public class ClipLinuxCrash2 implements LineListener{42Clip clip;43int stopOccured;44static final Object lock = new Object();4546public static long bytes2Ms(long bytes, AudioFormat format) {47return (long) (bytes/format.getFrameRate()*1000/format.getFrameSize());48}4950static int staticLen=1000;51static boolean addLen=true;5253ClipLinuxCrash2() {54}5556public void update(LineEvent e) {57if (e.getType() == LineEvent.Type.STOP) {58stopOccured++;59out(" Test program: receives STOP event for clip="+clip.toString()+" no."+stopOccured);60out(" Test program: Calling close() in event dispatcher thread");61clip.close();62synchronized (lock) {63lock.notifyAll();64}65}66else if (e.getType() == LineEvent.Type.CLOSE) {67out(" Test program: receives CLOSE event for "+clip.toString());68synchronized (lock) {69lock.notifyAll();70}71}72else if (e.getType() == LineEvent.Type.START) {73out(" Test program: receives START event for "+clip.toString());74}75else if (e.getType() == LineEvent.Type.OPEN) {76out(" Test program: receives OPEN event for "+clip.toString());77}78}7980public long start() throws Exception {81AudioFormat format = new AudioFormat(44100, 16, 2, true, false);8283if (addLen) {84staticLen+=(int) (staticLen/5)+1000;85} else {86staticLen-=(int) (staticLen/5)+1000;87}88if (staticLen>8*44100*4) {89staticLen = 8*44100*4;90addLen=!addLen;91}92if (staticLen<1000) {93staticLen = 1000;94addLen=!addLen;95}96int len = staticLen;97len -= (len % 4);98out(" Test program: preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio...");99100byte[] fakedata=new byte[len];101InputStream is = new ByteArrayInputStream(fakedata);102AudioInputStream ais = new AudioInputStream(is, format, fakedata.length/format.getFrameSize());103104DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());105clip = (Clip) AudioSystem.getLine(info);106clip.addLineListener(this);107108out(" Test program: opening clip="+((clip==null)?"null":clip.toString()));109clip.open(ais);110ais.close();111out(" Test program: starting clip="+((clip==null)?"null":clip.toString()));112clip.start();113return bytes2Ms(fakedata.length, format);114}115116public static void main(String[] args) throws Exception {117if (!isSoundcardInstalled()) {118return;119}120121try {122int COUNT=10;123out();124out("4498848 Sound causes crashes on Linux");125if (args.length>0) {126COUNT=Integer.parseInt(args[0]);127}128for (int i=0; i<COUNT; i++) {129out("trial "+(i+1)+"/"+COUNT);130ClipLinuxCrash2 t = new ClipLinuxCrash2();131t.start();132int waitTime = 300+(1300*(i % 2)); // every 2nd time wait 1600, rather than 300ms.133out(" Test program: waiting for "+waitTime+" ms for audio playback to stop...");134Thread.sleep(waitTime);135out(" Test program: calling close() from main thread");136t.clip.close();137// let the subsystem enough time to actually close the soundcard138//out(" Test program: waiting for 2 seconds...");139//Thread.sleep(2000);140//out();141}142out(" Test program: waiting for 1 second...");143Thread.sleep(1000);144} catch (Exception e) {145e.printStackTrace();146out(" Test program: waiting for 1 second");147try {148Thread.sleep(1000);149} catch (InterruptedException ie) {}150// do not fail if no audio device installed - bug 4742021151if (!(e instanceof LineUnavailableException)) {152throw e;153}154}155out("Test passed");156}157158static void out() {159out("");160}161162static void out(String s) {163System.out.println(s); System.out.flush();164}165166/**167* Returns true if at least one soundcard is correctly installed168* on the system.169*/170public static boolean isSoundcardInstalled() {171boolean result = false;172try {173Mixer.Info[] mixers = AudioSystem.getMixerInfo();174if (mixers.length > 0) {175result = AudioSystem.getSourceDataLine(null) != null;176}177} catch (Exception e) {178System.err.println("Exception occured: "+e);179}180if (!result) {181System.err.println("Soundcard does not exist or sound drivers not installed!");182System.err.println("This test requires sound drivers for execution.");183}184return result;185}186187}188189190