Path: blob/master/test/jdk/javax/sound/sampled/LinuxCrash/SDLLinuxCrash.java
41153 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.DataLine;26import javax.sound.sampled.LineEvent;27import javax.sound.sampled.LineListener;28import javax.sound.sampled.LineUnavailableException;29import javax.sound.sampled.Mixer;30import javax.sound.sampled.SourceDataLine;3132/**33* @test34* @bug 449884835* @summary Sound causes crashes on Linux (part 2)36*/37public class SDLLinuxCrash implements Runnable {38SourceDataLine sdl;39int size;4041SDLLinuxCrash(SourceDataLine sdl, int size) {42this.sdl = sdl;43this.size = size - (size % 4);44}4546public void run() {47int written=0;48//byte[] buffer = new byte[4096];49byte[] buffer = data;50out(" starting data line feed thread.");51try {52while (written<size) {53int toWrite = buffer.length;54if (toWrite+written > size) {55toWrite = size-written;56}57toWrite -= (toWrite % 4);58//out(" writing "+toWrite+" bytes.");59int thisWritten = sdl.write(buffer, 0, toWrite);60if (thisWritten<toWrite) {61out(" only wrote "+thisWritten+" bytes instead of "+toWrite);62}63if (thisWritten<=0) {64break;65}66written += thisWritten;67}68} catch (Throwable t) {69t.printStackTrace();70}71out(" leaving data line feed thread.");72}7374public static long bytes2Ms(long bytes, AudioFormat format) {75return (long) (bytes/format.getFrameRate()*1000/format.getFrameSize());76}7778static int staticLen=1000;79static boolean addLen=true;8081public static SourceDataLine start() throws Exception {82AudioFormat format = new AudioFormat(44100, 16, 2, true, false);83if (addLen) {84staticLen+=(int) (staticLen/5)+1000;85} else {86staticLen-=(int) (staticLen/5)+1000;87}88if (staticLen>8*44100*4) {89staticLen = 8*44100*4;90addLen=!addLen;91}92if (staticLen<1000) {93staticLen = 1000;94addLen=!addLen;95}96int len = staticLen;97len -= (len % 4);98out(" preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio...");99100DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);101SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);102sdl.addLineListener(new LineListener() {103public void update(LineEvent e) {104if (e.getType() == LineEvent.Type.STOP) {105out(" calling close() from event dispatcher thread");106((SourceDataLine) e.getSource()).close();107}108else if (e.getType() == LineEvent.Type.CLOSE) {109}110}111});112113out(" opening...");114sdl.open();115out(" starting...");116sdl.start();117(new Thread(new SDLLinuxCrash(sdl, len))).start();118return sdl;119}120121public static void main(String[] args) throws Exception {122if (!isSoundcardInstalled()) {123return;124}125126try {127int COUNT=10;128out();129out("4498848 Sound causes crashes on Linux (testing with SourceDataLine)");130if (args.length>0) {131COUNT=Integer.parseInt(args[0]);132}133for (int i=0; i<COUNT; i++) {134out(" trial "+(i+1)+"/"+COUNT);135SourceDataLine sdl = start();136int waitTime = 500+(1000*(i % 2)); // every 2nd time wait 1500, rather than 500ms.137out(" waiting for "+waitTime+" ms for audio playback to stop...");138Thread.sleep(waitTime);139out(" calling close() from main thread");140sdl.close();141// let the subsystem enough time to actually close the soundcard142out(" waiting for 2 seconds...");143Thread.sleep(2000);144out();145}146out(" waiting for 1 second...");147Thread.sleep(1000);148} catch (Exception e) {149e.printStackTrace();150out(" waiting for 1 second");151try {152Thread.sleep(1000);153} catch (InterruptedException ie) {}154// do not fail if no audio device installed - bug 4742021155if (!(e instanceof LineUnavailableException)) {156throw e;157}158}159out("Test passed");160}161162static void out() {163out("");164}165166static void out(String s) {167System.out.println(s); System.out.flush();168}169170/**171* Returns true if at least one soundcard is correctly installed172* on the system.173*/174public static boolean isSoundcardInstalled() {175boolean result = false;176try {177Mixer.Info[] mixers = AudioSystem.getMixerInfo();178if (mixers.length > 0) {179result = AudioSystem.getSourceDataLine(null) != null;180}181} catch (Exception e) {182System.err.println("Exception occured: "+e);183}184if (!result) {185System.err.println("Soundcard does not exist or sound drivers not installed!");186System.err.println("This test requires sound drivers for execution.");187}188return result;189}190191192193static final byte[] data = new byte[] {194123, 110, 100, 60, 11, 10, 10, 10, 9, 9,1959, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,1967, 7, 7, 7, 8, 8, 8, 8, 9, 9,1979, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,198123, 110, 100, 60, 11, 10, 10, 10, 9, 9,1999, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2007, 7, 7, 7, 8, 8, 8, 8, 9, 9,2019, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,202123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2039, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2047, 7, 7, 7, 8, 8, 8, 8, 9, 9,2059, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,206123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2079, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2087, 7, 7, 7, 8, 8, 8, 8, 9, 9,2099, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,210123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2119, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2127, 7, 7, 7, 8, 8, 8, 8, 9, 9,2139, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,214123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2159, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2167, 7, 7, 7, 8, 8, 8, 8, 9, 9,2179, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,218123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2199, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2207, 7, 7, 7, 8, 8, 8, 8, 9, 9,2219, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,222123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2239, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2247, 7, 7, 7, 8, 8, 8, 8, 9, 9,2259, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,226123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2279, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2287, 7, 7, 7, 8, 8, 8, 8, 9, 9,2299, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,230123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2319, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2327, 7, 7, 7, 8, 8, 8, 8, 9, 9,2339, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,234123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2359, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2367, 7, 7, 7, 8, 8, 8, 8, 9, 9,2379, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,238123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2399, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2407, 7, 7, 7, 8, 8, 8, 8, 9, 9,2419, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,242123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2439, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2447, 7, 7, 7, 8, 8, 8, 8, 9, 9,2459, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,246123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2479, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2487, 7, 7, 7, 8, 8, 8, 8, 9, 9,2499, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,250123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2519, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2527, 7, 7, 7, 8, 8, 8, 8, 9, 9,2539, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,254123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2559, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2567, 7, 7, 7, 8, 8, 8, 8, 9, 9,2579, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,258123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2599, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2607, 7, 7, 7, 8, 8, 8, 8, 9, 9,2619, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,262123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2639, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2647, 7, 7, 7, 8, 8, 8, 8, 9, 9,2659, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,266123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2679, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2687, 7, 7, 7, 8, 8, 8, 8, 9, 9,2699, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,270123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2719, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2727, 7, 7, 7, 8, 8, 8, 8, 9, 9,2739, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,274123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2759, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2767, 7, 7, 7, 8, 8, 8, 8, 9, 9,2779, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,278123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2799, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2807, 7, 7, 7, 8, 8, 8, 8, 9, 9,2819, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,282123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2839, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2847, 7, 7, 7, 8, 8, 8, 8, 9, 9,2859, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,286123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2879, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2887, 7, 7, 7, 8, 8, 8, 8, 9, 9,2899, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,290123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2919, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2927, 7, 7, 7, 8, 8, 8, 8, 9, 9,2939, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,294123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2959, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,2967, 7, 7, 7, 8, 8, 8, 8, 9, 9,2979, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,298123, 110, 100, 60, 11, 10, 10, 10, 9, 9,2999, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,3007, 7, 7, 7, 8, 8, 8, 8, 9, 9,3019, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,302123, 110, 100, 60, 11, 10, 10, 10, 9, 9,3039, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,3047, 7, 7, 7, 8, 8, 8, 8, 9, 9,3059, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,306123, 110, 100, 60, 11, 10, 10, 10, 9, 9,3079, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,3087, 7, 7, 7, 8, 8, 8, 8, 9, 9,3099, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120,310123, 110, 100, 60, 11, 10, 10, 10, 9, 9,3119, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6,3127, 7, 7, 7, 8, 8, 8, 8, 9, 9,3139, 9, 10, 10, 10, 11, 11, 60, 100, 110, 120, 122, 122314};315316}317318319