Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/sampled/Clip/ClipCloseLoss.java
38855 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 4946913 817840336* @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, long sleep) {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// emulates a different delay between open() and close()72Thread.sleep(sleep);73//long t = System.currentTimeMillis();74clip.close();75//if (System.currentTimeMillis() - t > 1950) {76// out(" clip.close needed more than 2 seconds! Causes failure of this test.");77// failed = true;78//}79out(" clip closed");80success++;81} catch (LineUnavailableException luae) {82// line not available, test not failed83System.err.println(luae);84} catch (IllegalArgumentException iae) {85// line not available, test not failed86System.err.println(iae);87} catch (Throwable t) {88t.printStackTrace();89}90}9192public static int getClipThreadCount() {93int ret = 0;94ThreadGroup tg = Thread.currentThread().getThreadGroup();95while (tg.getParent() != null) { tg = tg.getParent(); }96Thread[] threads = new Thread[500];97int count = tg.enumerate(threads, true);98for (int i = 0; i < count; i++) {99if (threads[i].getName().contains("Direct")100&& threads[i].getName().contains("Clip")) {101out("Found Direct Clip thread object: "+threads[i]);102ret++;103}104}105return ret;106}107108public static void main(String[] args) throws Exception {109if (isSoundcardInstalled()) {110bais.mark(0);111Mixer.Info[] infos = AudioSystem.getMixerInfo();112for (int sleep = 0; sleep < 100; ++sleep) {113run(null, sleep);114for (int i = 0; i < infos.length; i++) {115try {116Mixer m = AudioSystem.getMixer(infos[i]);117run(m, sleep);118} catch (Exception e) {119}120}121}122out("Waiting 1 second to dispose of all threads");123Thread.sleep(1000);124if (getClipThreadCount() > 0) {125out("Unused clip threads exist! Causes test failure");126failed = true;127}128if (failed) throw new Exception("Test FAILED!");129if (success > 0) {130out("Test passed.");131} else {132System.err.println("Test could not execute: please install an audio device");133}134}135}136137/**138* Returns true if at least one soundcard is correctly installed139* on the system.140*/141public static boolean isSoundcardInstalled() {142boolean result = false;143try {144Mixer.Info[] mixers = AudioSystem.getMixerInfo();145if (mixers.length > 0) {146result = AudioSystem.getSourceDataLine(null) != null;147}148} catch (Exception e) {149System.err.println("Exception occured: "+e);150}151if (!result) {152System.err.println("Soundcard does not exist or sound drivers not installed!");153System.err.println("This test requires sound drivers for execution.");154}155return result;156}157158public static void out(String s) {159/*long t = System.nanoTime() / 1000000l;160String ts = ""+(t % 1000);161while (ts.length() < 3) ts = "0"+ts;162System.out.println(""+(t/1000)+":"+ts+" "+s);163System.out.flush();*/164System.out.println(s);165}166}167168169