Path: blob/master/test/jdk/javax/sound/sampled/Clip/ClipFlushCrash.java
41153 views
/*1* Copyright (c) 2003, 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;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioInputStream;27import javax.sound.sampled.AudioSystem;28import javax.sound.sampled.Clip;29import javax.sound.sampled.DataLine;30import javax.sound.sampled.LineUnavailableException;31import javax.sound.sampled.Mixer;3233/**34* @test35* @bug 494694536* @summary Crash in javasound while running TicTacToe demo applet tiger b2637*/38public class ClipFlushCrash {39static int frameCount = 441000; // lets say 10 seconds40static AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);41static ByteArrayInputStream bais =42new ByteArrayInputStream(new byte[frameCount * format.getFrameSize()]);4344static int success = 0;4546public static void run(Mixer m) {47Clip clip = null;48try {49if (m == null) {50out("Using default mixer");51clip = (Clip) AudioSystem.getClip();52} else {53out("Using mixer: "+m);54DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);55clip = (Clip) m.getLine(info);56}57out(" got clip: "+clip);58if (!clip.getClass().toString().contains("Direct")) {59out(" no direct audio clip -> do not test.");60return;61}6263out(" open");64bais.reset();65clip.open(new AudioInputStream(bais, format, frameCount));6667AT at1 = new AT(clip, "flush thread", 123) {68public void doAction() throws Exception {69log("flush");70clip.flush();71}72};73AT at2 = new AT(clip, "setFramePosition thread", 67) {74public void doAction() throws Exception {75int pos = (int) (Math.random() * clip.getFrameLength());76log("setPosition to frame "+pos);77clip.setFramePosition(pos);78}79};80AT at3 = new AT(clip, "start/stop thread", 300) {81public void doAction() throws Exception {82if (clip.isRunning()) {83log("stop");84clip.stop();85} else {86log("start");87clip.setFramePosition(0);88clip.start();89}90}91};92AT at4 = new AT(clip, "open/close thread", 600) {93public synchronized void doAction() throws Exception {94log("close");95clip.close();96wait(50);97if (!terminated) {98log("open");99bais.reset();100clip.open(new AudioInputStream(bais, format, frameCount));101}102}103};104105out(" clip.start");106clip.start();107out(" for 10 seconds, call start/stop, setFramePosition, and flush from other threads");108at1.start();109at2.start();110at3.start();111at4.start();112try {113Thread.sleep(10000);114} catch (InterruptedException ie) {}115out(" finished.");116at1.terminate();117at2.terminate();118at3.terminate();119at4.terminate();120out(" clip.close()");121clip.close();122success++;123} catch (LineUnavailableException luae) {124// line not available, test not failed125System.err.println(luae);126} catch (IllegalArgumentException iae) {127// line not available, test not failed128System.err.println(iae);129} catch (Throwable t) {130t.printStackTrace();131}132}133134public static void main(String[] args) throws Exception {135if (isSoundcardInstalled()) {136bais.mark(0);137run(null);138Mixer.Info[] infos = AudioSystem.getMixerInfo();139for (int i = 0; i<infos.length; i++) {140try {141Mixer m = AudioSystem.getMixer(infos[i]);142run(m);143} catch (Exception e) {144}145}146if (success > 0) {147out("No crash -> Test passed");148} else {149System.err.println("Test could not execute: please install an audio device");150}151}152}153154/**155* Returns true if at least one soundcard is correctly installed156* on the system.157*/158public static boolean isSoundcardInstalled() {159boolean result = false;160try {161Mixer.Info[] mixers = AudioSystem.getMixerInfo();162if (mixers.length > 0) {163result = AudioSystem.getSourceDataLine(null) != null;164}165} catch (Exception e) {166System.err.println("Exception occured: "+e);167}168if (!result) {169System.err.println("Soundcard does not exist or sound drivers not installed!");170System.err.println("This test requires sound drivers for execution.");171}172return result;173}174175public static void out(String s) {176/*long t = System.nanoTime() / 1000000l;177String ts = ""+(t % 1000);178while (ts.length() < 3) ts = "0"+ts;179System.out.println(""+(t/1000)+":"+ts+" "+s);180System.out.flush();*/181System.out.println(s);182}183184private abstract static class AT extends Thread {185protected boolean terminated = false;186protected Clip clip;187private int waitTime;188189public AT(Clip clip, String name, int waitTime) {190super(name);191this.clip = clip;192this.waitTime = waitTime;193}194195public abstract void doAction() throws Exception;196197public void run() {198log("start");199while (!terminated) {200try {201synchronized(this) {202wait(waitTime);203}204if (!terminated) {205doAction();206}207} catch(Exception e) {208log("exception: "+e);209}210}211log("exit");212}213214public synchronized void terminate() {215log("terminate");216terminated = true;217notifyAll();218}219220protected void log(String s) {221//out(" "+Thread.currentThread().getId()+" "+getName()+": "+s);222out(" "+getName()+": "+s);223}224}225}226227228