Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/javax/sound/sampled/Clip/ClipCloseLoss.java
83406 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 494691336* @summary DirectClip doesn't kill the thread correctly, sometimes37* @run main/othervm ClipCloseLoss38* @key headful39*/40public class ClipCloseLoss {41static int frameCount = 441000; // lets say 10 seconds42static AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);43static ByteArrayInputStream bais =44new ByteArrayInputStream(new byte[frameCount * format.getFrameSize()]);4546static int success = 0;47static boolean failed = false;4849public static void run(Mixer m) {50Clip clip = null;51try {52if (m == null) {53out("Using default mixer");54clip = (Clip) AudioSystem.getClip();55} else {56out("Using mixer: "+m);57DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);58clip = (Clip) m.getLine(info);59}60out(" got clip: "+clip);61if (!clip.getClass().toString().contains("Direct")) {62out(" no direct audio clip -> do not test.");63return;64}6566out(" open");67bais.reset();68clip.open(new AudioInputStream(bais, format, frameCount));6970out(" clip.close()");71//long t = System.currentTimeMillis();72clip.close();73//if (System.currentTimeMillis() - t > 1950) {74// out(" clip.close needed more than 2 seconds! Causes failure of this test.");75// failed = true;76//}77out(" clip closed");78success++;79} catch (LineUnavailableException luae) {80// line not available, test not failed81System.err.println(luae);82} catch (IllegalArgumentException iae) {83// line not available, test not failed84System.err.println(iae);85} catch (Throwable t) {86t.printStackTrace();87}88}8990public static int getClipThreadCount() {91int ret = 0;92ThreadGroup tg = Thread.currentThread().getThreadGroup();93while (tg.getParent() != null) { tg = tg.getParent(); }94Thread[] threads = new Thread[500];95int count = tg.enumerate(threads, true);96for (int i = 0; i < count; i++) {97if (threads[i].getName().contains("Direct")98&& threads[i].getName().contains("Clip")) {99out("Found Direct Clip thread object: "+threads[i]);100ret++;101}102}103return ret;104}105106public static void main(String[] args) throws Exception {107if (isSoundcardInstalled()) {108bais.mark(0);109run(null);110Mixer.Info[] infos = AudioSystem.getMixerInfo();111for (int i = 0; i<infos.length; i++) {112try {113Mixer m = AudioSystem.getMixer(infos[i]);114run(m);115} catch (Exception e) {116}117}118out("Waiting 1 second to dispose of all threads");119Thread.sleep(1000);120if (getClipThreadCount() > 0) {121out("Unused clip threads exist! Causes test failure");122failed = true;123}124if (failed) throw new Exception("Test FAILED!");125if (success > 0) {126out("Test passed.");127} else {128System.err.println("Test could not execute: please install an audio device");129}130}131}132133/**134* Returns true if at least one soundcard is correctly installed135* on the system.136*/137public static boolean isSoundcardInstalled() {138boolean result = false;139try {140Mixer.Info[] mixers = AudioSystem.getMixerInfo();141if (mixers.length > 0) {142result = AudioSystem.getSourceDataLine(null) != null;143}144} catch (Exception e) {145System.err.println("Exception occured: "+e);146}147if (!result) {148System.err.println("Soundcard does not exist or sound drivers not installed!");149System.err.println("This test requires sound drivers for execution.");150}151return result;152}153154public static void out(String s) {155/*long t = System.nanoTime() / 1000000l;156String ts = ""+(t % 1000);157while (ts.length() < 3) ts = "0"+ts;158System.out.println(""+(t/1000)+":"+ts+" "+s);159System.out.flush();*/160System.out.println(s);161}162}163164165