Path: blob/master/test/jdk/javax/sound/sampled/Lines/SourceDataLineDefaultBufferSizeCrash.java
41153 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 javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Line;26import javax.sound.sampled.LineUnavailableException;27import javax.sound.sampled.Mixer;28import javax.sound.sampled.SourceDataLine;2930/**31* @test32* @bug 468138433* @summary SourceDataLine.write() causes Unexpected Signal 11 in native code34* outside the VM35*/36public class SourceDataLineDefaultBufferSizeCrash {3738static final int STATUS_PASSED = 0;39static final int STATUS_FAILED = 2;40static final int STATUS_TEMP = 95;4142public static void main(String argv[]) throws Exception {43int testExitStatus = run(argv, System.out) + STATUS_TEMP;44}4546public static int run(String argv[], java.io.PrintStream out) throws Exception {47int testResult = STATUS_PASSED;4849int framesNumberToExceed = 2;50if ( argv.length > 0 ) {51try {52framesNumberToExceed = Integer.parseInt(argv[0]);53}54catch (NumberFormatException e) {55}56}5758out.println59("\n==> Test for SourceDataLine.write() method:");6061Mixer.Info[] installedMixersInfo = AudioSystem.getMixerInfo();6263if ( installedMixersInfo == null ) {64out.println("## AudioSystem.getMixerInfo() returned unexpected result:");65out.println("# expected: an array of Mixer.Info objects (may be array of length 0);");66out.println("# produced: null;");67return STATUS_FAILED;68}6970if ( installedMixersInfo.length == 0 ) {71// there are no mixers installed on the system -72// so this testcase can not be tested73out.println("\n>>> There are no mixers installed on the system!");74return STATUS_PASSED;75}7677out.println("\n>>> Number of mixers installed on the system = "78+ installedMixersInfo.length);79Mixer installedMixer = null;80for (int i=0; i < installedMixersInfo.length; i++) {81try {82installedMixer = AudioSystem.getMixer(installedMixersInfo[i]);83} catch (SecurityException securityException) {84// installed Mixer is unavailable because of security restrictions85out.println("\n>>> installedMixer[" + i86+ "] is unavailable because of security restrictions");87continue;88} catch (Throwable thrown) {89out.println("\n## installedMixer[" + i + "] is unavailable because of");90out.println("# AudioSystem.getMixer() threw unexpected exception:");91thrown.printStackTrace(out);92testResult = STATUS_FAILED;93continue;94}9596out.println("\n>>> installedMixer["+i+"] = " + installedMixer);97try {98installedMixer.open();99} catch (LineUnavailableException lineUnavailableException) {100// installedMixer is not available due to resource restrictions101out.println(">> installedMixer[" + i102+ "] is not opened because of resource restrictions");103continue;104} catch (SecurityException securityException) {105// installedMixer is not available due to security restrictions106out.println(">> installedMixer[" + i107+ "] is not opened because of security restrictions");108continue;109} catch (Throwable thrown) {110out.println("## installedMixer.open() throws unexpected exception:");111thrown.printStackTrace(out);112testResult = STATUS_FAILED;113continue;114}115Line.Info supportedSourceLineInfo[] = null;116try {117supportedSourceLineInfo = installedMixer.getSourceLineInfo();118} catch (Throwable thrown) {119out.println("## installedMixer.getSourceLineInfo() throws "120+ "unexpected exception:");121thrown.printStackTrace(out);122testResult = STATUS_FAILED;123installedMixer.close();124continue;125}126if ( supportedSourceLineInfo == null ) {127out.println("## installedMixer.getSourceLineInfo() returned null array");128out.println("# Mixer = " + installedMixer);129testResult = STATUS_FAILED;130installedMixer.close();131continue;132}133out.println("\n>> Number of SourceLineInfo supported by installedMixer ="134+ supportedSourceLineInfo.length);135136for (int j=0; j < supportedSourceLineInfo.length; j++) {137Line.Info testSourceLineInfo = supportedSourceLineInfo[j];138139out.println("\n> testSourceLineInfo["+j+"] = " + testSourceLineInfo);140Line testSourceLine = null;141try {142testSourceLine = installedMixer.getLine(testSourceLineInfo);143} catch (LineUnavailableException lineUnavailableException) {144// line is not available due to resource restrictions145out.println("> Line for this SourceLine Info is not available "146+ "due to resource restrictions");147continue;148} catch (SecurityException securityException) {149// line is not available due to security restrictions150out.println("> Line for this SourceLine Info is not available "151+ "due to security restrictions");152continue;153} catch (Throwable thrown) {154out.println("## installedMixer.getLine(testSourceLineInfo) throws"155+ "unexpected Exception:");156thrown.printStackTrace(out);157testResult = STATUS_FAILED;158continue;159}160161out.println("> testedSourceLine = " + testSourceLine);162if ( ! (testSourceLine instanceof SourceDataLine) ) {163out.println("> testSourceLine is not SourceDataLine");164continue;165}166167SourceDataLine testedSourceLine = (SourceDataLine)testSourceLine;168AudioFormat lineAudioFormat = testedSourceLine.getFormat();169170out.println("\n> opening tested SourceLine:");171try {172//testedSourceLine.open(lineAudioFormat, 2048);173testedSourceLine.open(lineAudioFormat);174out.println("> OK - line is opened with "+testedSourceLine.getBufferSize()+" bytes buffer");175} catch (LineUnavailableException lineUnavailableException) {176out.println("> Line is not available due to resource restrictions:");177lineUnavailableException.printStackTrace(out);178continue;179} catch (SecurityException securityException) {180out.println("> Line is not available due to security restrictions:");181securityException.printStackTrace(out);182continue;183} catch (Throwable thrown) {184out.println("## SourceDataLine.open(AudioFormat format) failed:");185out.println("# Unexpected Exception is thrown");186out.println("# Mixer = " + installedMixer);187out.println("# SourceDataLine = " + testedSourceLine);188thrown.printStackTrace(out);189testResult = STATUS_FAILED;190continue;191}192193testedSourceLine.start();194195int frameSize = 1;196if ( lineAudioFormat.getFrameSize() != AudioSystem.NOT_SPECIFIED ) {197frameSize = lineAudioFormat.getFrameSize();198} else {199if ( lineAudioFormat.getSampleSizeInBits() != AudioSystem.NOT_SPECIFIED ) {200frameSize = lineAudioFormat.getSampleSizeInBits()/8;201if ( lineAudioFormat.getSampleSizeInBits()%8 != 0 ) {202frameSize++;203}204}205}206int bufferSizeToWrite = testedSourceLine.available()207+ (frameSize * framesNumberToExceed);208byte[] dataToWrite = new byte[bufferSizeToWrite];209for (int k=0; k < bufferSizeToWrite; k++) {210dataToWrite[k] = (byte)1;211}212int offsetToWrite = 0;213214out.println("\n> check SourceDataLine.write() to write more data "215+ "than can currently be written:");216217out.println("> testedSourceLine.available() = " + testedSourceLine.available());218out.println("> frame size = " + frameSize);219out.println("> number of bytes to write = " + bufferSizeToWrite);220int writtenBytes = -1;221try {222writtenBytes =223testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);224out.println("> OK - number of written bytes = " + writtenBytes);225} catch (Throwable thrown) {226out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");227out.println("# Unexpected Exception is thrown");228thrown.printStackTrace(out);229testResult = STATUS_FAILED;230}231232testedSourceLine.close();233234} // for (int j=0; j < supportedSourceLineInfo.length; j++)235installedMixer.close();236237} // for (int i=0; i < installedMixersInfo.length; i++)238239if ( testResult == STATUS_FAILED ) {240throw new Exception("Test FAILED!");241} else {242out.println("\n==> test PASSED!");243}244return testResult;245}246247}248249250