Path: blob/master/test/jdk/javax/sound/sampled/Clip/Drain/ClipDrain.java
41155 views
/*1* Copyright (c) 2002, 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 javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Clip;26import javax.sound.sampled.DataLine;27import javax.sound.sampled.Mixer;2829/**30* @test31* @bug 473221832* @summary Clip.drain does not actually block until all I/O is complete as33* documented.34*/35public class ClipDrain {36static int successfulTests = 0;37static AudioFormat format = new AudioFormat(8000, 16, 1, true, false);38// create a 10-second file39static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 10)];4041static int TOLERANCE_MS = 2500; // how many milliseconds too short is tolerated...4243private static void doMixerClip(Mixer mixer) throws Exception {44boolean waitedEnough=false;45try {46DataLine.Info info = new DataLine.Info(Clip.class, format);47Clip clip = (Clip) mixer.getLine(info);48clip.open(format, soundData, 0, soundData.length);4950// sanity51if (clip.getMicrosecondLength()/1000 < 9900) {52throw new Exception("clip's microsecond length should be at least 9900000, but it is "+clip.getMicrosecondLength());53}54long start = System.currentTimeMillis();5556System.out.println(" ---------- start --------");57clip.start();58// give time to actually start it. ALSA implementation needs that...59Thread.sleep(300);60System.out.println("drain ... ");61clip.drain();62long elapsedTime = System.currentTimeMillis() - start;63System.out.println("close ... ");64clip.close();65System.out.println("... done");66System.out.println("Playback duration: "+elapsedTime+" milliseconds.");67waitedEnough = elapsedTime >= ((clip.getMicrosecondLength() / 1000) - TOLERANCE_MS);68} catch (Throwable t) {69System.out.println(" - Caught exception. Not failed.");70System.out.println(" - "+t.toString());71return;72}73if (!waitedEnough) {74throw new Exception("Drain did not wait long enough to play entire clip.");75}76successfulTests++;77}787980private static void doAll() throws Exception {81Mixer.Info[] mixers = AudioSystem.getMixerInfo();82for (int i=0; i<mixers.length; i++) {83Mixer mixer = AudioSystem.getMixer(mixers[i]);84System.out.println("--------------");85System.out.println("Testing mixer: "+mixers[i]);86doMixerClip(mixer);87}88if (mixers.length==0) {89System.out.println("No mixers available!");90}91}9293public static void main(String[] args) throws Exception {94if (!isSoundcardInstalled()) {95return;96}97doAll();98if (successfulTests==0) {99System.out.println("Could not execute any of the tests. Test NOT failed.");100} else {101System.out.println("Test PASSED.");102}103}104105/**106* Returns true if at least one soundcard is correctly installed107* on the system.108*/109public static boolean isSoundcardInstalled() {110boolean result = false;111try {112Mixer.Info[] mixers = AudioSystem.getMixerInfo();113if (mixers.length > 0) {114result = AudioSystem.getSourceDataLine(null) != null;115}116} catch (Exception e) {117System.err.println("Exception occured: "+e);118}119if (!result) {120System.err.println("Soundcard does not exist or sound drivers not installed!");121System.err.println("This test requires sound drivers for execution.");122}123return result;124}125}126127128