Path: blob/master/test/jdk/javax/sound/sampled/LinuxBlock/PlaySine.java
41155 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.File;24import java.io.IOException;2526import javax.sound.sampled.AudioFormat;27import javax.sound.sampled.AudioInputStream;28import javax.sound.sampled.AudioSystem;29import javax.sound.sampled.DataLine;30import javax.sound.sampled.LineUnavailableException;31import javax.sound.sampled.Mixer;32import javax.sound.sampled.SourceDataLine;3334/**35* @test36* @bug 483446137* @summary Applet hang when you load it during sound card is in use38* @run main/manual PlaySine39*/40public class PlaySine {4142static int sampleRate = 8000;43static double frequency = 2000.0;44static double RAD = 2.0 * Math.PI;4546static byte[] audioData = new byte[sampleRate/2];47static SourceDataLine source;48static Mixer mixer = null;4950static AudioInputStream ais = null;51static AudioFormat audioFormat;52static String filename;5354public static void constructAIS() {55try {56ais = AudioSystem.getAudioInputStream(new File(filename));57} catch (Exception e) {58println("ERROR: could not open "+filename+": "+e.getMessage());59}60}6162public static void print(String s) {63System.out.print(s);64}65public static void println(String s) {66System.out.println(s);67}6869public static void key() {70println("");71print("Press ENTER to continue...");72try {73System.in.read();74} catch (IOException ioe) {75}76}7778static int audioLen = -1;79static int audioOffset = -1;8081public static void writeData() {82if (audioLen == -1) {83audioLen = audioData.length;84}85if (audioOffset < 0) {86audioOffset = audioLen;87}88try {89if (audioOffset >= audioLen) {90audioOffset = 0;91if (ais!=null) {92do {93audioLen = ais.read(audioData, 0, audioData.length);94if (audioLen < 0) {95constructAIS();96}97} while (audioLen < 0);98}99}100int toWrite = audioLen - audioOffset;101int written = source.write(audioData, audioOffset, toWrite);102audioOffset+=written;103} catch (Exception e) {104e.printStackTrace();105}106}107108109public static int play(boolean shouldPlay) {110int res = 0;111DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);112try {113println("Getting line from mixer...");114source = (SourceDataLine) mixer.getLine(info);115println("Opening line...");116println(" -- if the program is hanging here, kill the process that has blocks the audio device now.");117source.open(audioFormat);118println("Starting line...");119source.start();120println("Writing audio data for 1 second...");121long startTime = System.currentTimeMillis();122while (System.currentTimeMillis() - startTime < 1000) {123writeData();124Thread.sleep(100);125}126res = 1;127} catch (IllegalArgumentException iae) {128println("IllegalArgumentException: "+iae.getMessage());129println("Sound device cannot handle this audio format.");130println("ERROR: Test environment not correctly set up.");131if (source!=null) {132source.close();133}134return 3;135} catch (LineUnavailableException lue) {136println("LineUnavailableException: "+lue.getMessage());137if (shouldPlay) {138println("ERROR: the line should be available now!.");139println(" Verify that you killed the other audio process.");140} else {141println("Correct behavior! the bug is fixed.");142}143res = 2;144} catch (Exception e) {145println("Unexpected Exception: "+e.toString());146}147if (source != null) {148println("Draining...");149try {150source.drain();151} catch (NullPointerException npe) {152println("(NullPointerException: bug fixed in J2SE 1.4.2");153}154println("Stopping...");155source.stop();156println("Closing...");157source.close();158source = null;159}160return res;161}162163public static void main(String[] args) throws Exception {164println("This is an interactive test. You can run it with a filename as argument");165println("It is only meant to be run on linux, with the (old) OSS kernel drivers (/dev/dsp)");166println("This test should not be run on systems with ALSA installed, or kernel 2.6 or higher.");167println("");168println("The test verifies that Java Sound fails correctly if another process is blocking");169println("the audio device.");170println("");171println("Checking sanity...");172Mixer.Info[] mixers=null;173174mixers = AudioSystem.getMixerInfo();175for (int i=0; i<mixers.length; i++) {176try {177Mixer thisMixer = AudioSystem.getMixer(mixers[i]);178String mixerName = thisMixer.getMixerInfo().getName();179if (mixerName.indexOf("Java Sound")>=0180&& mixerName.indexOf("Engine")>=0) {181mixer = thisMixer;182break;183}184} catch (Exception e) {185e.printStackTrace();186}187}188if (mixer == null) {189if (mixers.length==0) {190System.out.println("ERROR: No mixers available!");191} else {192println("ERROR: Java Sound Engine could not be found.");193}194println("Cannot run this test.");195return;196}197println(" ...using mixer "+mixer.getMixerInfo());198199String osname = System.getProperty("os.name");200if ((osname == null) || (osname.toLowerCase().indexOf("linux")<0)) {201println("ERROR: not running on linux (you are running on "+osname+")");202return;203}204println(" ...running on "+osname);205println(" ...sanity test OK.");206207filename = null;208if (args.length>0) {209File f = new File(args[0]);210if (f.exists()) {211filename = args[0];212println("Opening "+filename);213constructAIS();214if (ais!=null) {215audioFormat = ais.getFormat();216}217}218}219if (ais == null) {220println("Using self-generated sine wave for playback");221audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);222for (int i=0; i<audioData.length; i++) {223audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);224}225}226227println("");228println("Now, on a second console, run the following command:");229println(" cat - < /dev/zero > /dev/dsp");230key();231println("After you press ENTER now, the mixer will be opened.");232println("There are 3 possible cases that can occur:");233println("1) you'll hear a sine wave");234println(" -> you are running with mixing OSS drivers. ");235println(" Some soundcards only provide mixing OSS drivers.");236println(" Test environment not valid. ");237println(" Repeat on another machine where you can reproduce the bug first.");238println("2) this program stops doing anything after 'Opening line...'");239println(" -> this is the bug.");240println(" Kill the command on the other console with Ctrl-C, this program");241println(" should continue working then.");242println("3) this program reports a LineUnavailableException");243println(" -> bug is fixed.");244println(" OR you run with non-blocking OSS drivers.");245println(" make sure that you can reproduce this bug first with e.g. J2SE 1.4.1");246key();247int playedFirst = play(false);248int playedSecond = 0;249250if (playedFirst == 2) {251println("");252println("Now kill the other process with Ctrl-C.");253println("After that, this program should be able to play ");254println("the sine wave without problems.");255key();256playedSecond = play(true);257}258println("");259if (playedFirst == 1) {260println("Test FAILED.");261}262else if (playedFirst == 2 && playedSecond == 1) {263println("Test SUCCESSFUL");264} else {265println("Test not failed (but not successful either...).");266}267}268}269270271