Path: blob/master/test/jdk/javax/sound/sampled/Clip/SetPositionHang.java
66646 views
/*1* Copyright (c) 2021, 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 javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Clip;26import javax.sound.sampled.LineUnavailableException;2728/**29* @test30* @bug 826642131* @summary Tests that Clip.setFramePosition/setMicrosecondPosition do not hang.32*/33public final class SetPositionHang implements Runnable {3435private static volatile boolean testFramePosition;36private final Clip clip;37private final String thread;3839private SetPositionHang(String thread, Clip clip) {40this.thread = thread;41this.clip = clip;42}4344public static void main(String[] args) throws Exception {45testFramePosition = false;46test();47testFramePosition = true;48test();49}5051private static void test() throws InterruptedException {52try (Clip clip = AudioSystem.getClip()) {53// prepare audio data54int frameCount = 441000; // lets say 10 seconds55AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);56byte[] bytes = new byte[frameCount * format.getFrameSize()];5758clip.open(format, bytes, 0, frameCount);59Thread t1 = new Thread(new SetPositionHang("1", clip));60Thread t2 = new Thread(new SetPositionHang("2", clip));61Thread t3 = new Thread(new SetPositionHang("3", clip));62Thread t4 = new Thread(new SetPositionHang("4", clip));63Thread t5 = new Thread(new SetPositionHang("5", clip));64t1.start();65t2.start();66t3.start();67t4.start();68t5.start();69t1.join();70t2.join();71t3.join();72t4.join();73t5.join();74} catch (LineUnavailableException | IllegalArgumentException ignored) {75// the test is not applicable76}77}7879public void run() {80System.out.println("Thread " + thread + " Start");81for (int i = 0; i < 100; i++) {82// System.out.println("Thread " + thread + " Play " +83// System.currentTimeMillis() % 100000);84playSound();85try {86Thread.sleep(i);87} catch (InterruptedException e) {88throw new RuntimeException(e);89}90}91System.out.println("Thread " + thread + " Finish");92}9394private void playSound() {95if (clip.isRunning()) {96clip.stop();97}98if (testFramePosition) {99clip.setFramePosition(0);100} else {101clip.setMicrosecondPosition(0);102}103clip.start();104}105}106107108