Path: blob/master/test/jdk/javax/sound/sampled/Clip/IsRunningHang.java
41153 views
/*1* Copyright (c) 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.util.ArrayList;24import java.util.List;25import java.util.concurrent.CountDownLatch;2627import javax.sound.sampled.AudioFormat;28import javax.sound.sampled.AudioSystem;29import javax.sound.sampled.Clip;30import javax.sound.sampled.DataLine;31import javax.sound.sampled.Line;32import javax.sound.sampled.LineEvent;33import javax.sound.sampled.LineUnavailableException;3435/**36* @test37* @bug 815616938* @run main/othervm/timeout=300 IsRunningHang39*/40public final class IsRunningHang {4142private static CountDownLatch go;4344/**45* We will try to use all usually supported formats.46*/47private static final List<AudioFormat> formats = new ArrayList<>();4849private static final AudioFormat.Encoding[] encodings = {50AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,51AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,52AudioFormat.Encoding.PCM_FLOAT53};5455private static final int[] sampleRates = {8000, 16000, 48000};5657private static final int[] sampleBits = {8, 16, 24, 32, 64};5859private static final int[] channels = {1, 2, 3, 5};6061static {62for (final Boolean end : new boolean[]{false, true}) {63for (final int sampleSize : sampleBits) {64for (final int sampleRate : sampleRates) {65for (final int channel : channels) {66for (final AudioFormat.Encoding enc : encodings) {67int s = ((sampleSize + 7) / 8) * channel;68formats.add(new AudioFormat(enc, sampleRate,69sampleSize, channel,70s, sampleRate, end));71}72}73}74}75}76}7778public static void main(final String[] args) throws Exception {79for (final AudioFormat format : formats) {80System.out.println("format = " + format);81// create a 0.5-second data82byte[] soundData = new byte[(int) (format.getFrameRate()83* format.getFrameSize()84/ 2)];85try {86test(format, soundData);87} catch (LineUnavailableException | IllegalArgumentException ignored) {88// the test is not applicable89}90}91}9293private static void test(final AudioFormat format, final byte[] data)94throws Exception {95final Line.Info info = new DataLine.Info(Clip.class, format);96final Clip clip = (Clip) AudioSystem.getLine(info);9798go = new CountDownLatch(1);99clip.addLineListener(event -> {100if (event.getType().equals(LineEvent.Type.START)) {101go.countDown();102}103});104105clip.open(format, data, 0, data.length);106clip.start();107go.await();108while (clip.isRunning()) {109// This loop should not hang110}111while (clip.isActive()) {112// This loop should not hang113}114clip.close();115}116}117118119