Path: blob/master/test/jdk/javax/sound/sampled/Clip/ClipCloseLoss.java
41153 views
/*1* Copyright (c) 2003, 2017, 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*/39public class ClipCloseLoss {40static int frameCount = 441000; // lets say 10 seconds41static AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);42static ByteArrayInputStream bais =43new ByteArrayInputStream(new byte[frameCount * format.getFrameSize()]);4445static int success = 0;46static boolean failed = false;4748public static void run(Mixer m, long sleep) {49Clip clip = null;50try {51if (m == null) {52out("Using default mixer");53clip = (Clip) AudioSystem.getClip();54} else {55out("Using mixer: "+m);56DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);57clip = (Clip) m.getLine(info);58}59out(" got clip: "+clip);60if (!clip.getClass().toString().contains("Direct")) {61out(" no direct audio clip -> do not test.");62return;63}6465out(" open");66bais.reset();67clip.open(new AudioInputStream(bais, format, frameCount));6869out(" clip.close()");70// emulates a different delay between open() and close()71Thread.sleep(sleep);72//long t = System.currentTimeMillis();73clip.close();74//if (System.currentTimeMillis() - t > 1950) {75// out(" clip.close needed more than 2 seconds! Causes failure of this test.");76// failed = true;77//}78out(" clip closed");79success++;80} catch (LineUnavailableException luae) {81// line not available, test not failed82System.err.println(luae);83} catch (IllegalArgumentException iae) {84// line not available, test not failed85System.err.println(iae);86} catch (Throwable t) {87t.printStackTrace();88}89}9091public static int getClipThreadCount() {92int ret = 0;93ThreadGroup tg = Thread.currentThread().getThreadGroup();94while (tg.getParent() != null) { tg = tg.getParent(); }95Thread[] threads = new Thread[500];96int count = tg.enumerate(threads, true);97for (int i = 0; i < count; i++) {98if (threads[i].getName().contains("Direct")99&& threads[i].getName().contains("Clip")) {100out("Found Direct Clip thread object: "+threads[i]);101ret++;102}103}104return ret;105}106107public static void main(String[] args) throws Exception {108if (isSoundcardInstalled()) {109bais.mark(0);110Mixer.Info[] infos = AudioSystem.getMixerInfo();111for (int sleep = 0; sleep < 100; ++sleep) {112run(null, sleep);113for (int i = 0; i < infos.length; i++) {114try {115Mixer m = AudioSystem.getMixer(infos[i]);116run(m, sleep);117} catch (Exception e) {118}119}120}121out("Waiting 1 second to dispose of all threads");122Thread.sleep(1000);123if (getClipThreadCount() > 0) {124out("Unused clip threads exist! Causes test failure");125failed = true;126}127if (failed) throw new Exception("Test FAILED!");128if (success > 0) {129out("Test passed.");130} else {131System.err.println("Test could not execute: please install an audio device");132}133}134}135136/**137* Returns true if at least one soundcard is correctly installed138* on the system.139*/140public static boolean isSoundcardInstalled() {141boolean result = false;142try {143Mixer.Info[] mixers = AudioSystem.getMixerInfo();144if (mixers.length > 0) {145result = AudioSystem.getSourceDataLine(null) != null;146}147} catch (Exception e) {148System.err.println("Exception occured: "+e);149}150if (!result) {151System.err.println("Soundcard does not exist or sound drivers not installed!");152System.err.println("This test requires sound drivers for execution.");153}154return result;155}156157public static void out(String s) {158/*long t = System.nanoTime() / 1000000l;159String ts = ""+(t % 1000);160while (ts.length() < 3) ts = "0"+ts;161System.out.println(""+(t/1000)+":"+ts+" "+s);162System.out.flush();*/163System.out.println(s);164}165}166167168