Path: blob/master/test/jdk/javax/sound/sampled/Lines/StopStart.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 java.io.File;24import java.util.Random;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 482855637* @summary stopping and starting sampled audio plays small chunk in infinite38* loop39*/40public class StopStart implements Runnable {4142static int sampleRate = 8000;43static double frequency = 2000.0;44static double RAD = 2.0 * Math.PI;45static Random random = new Random();4647static byte[] audioData = new byte[sampleRate/2];48static SourceDataLine source;4950static boolean terminated = false;5152static int buffersWritten = 0;53static long bytesWritten = 0;54static int buffersWrittenAfter5Seconds;5556static AudioInputStream ais = null;57static AudioFormat audioFormat;58static String filename;5960static int executedTests=0;61static int successfulTests = 0;6263public static void constructAIS() throws Exception {64ais = AudioSystem.getAudioInputStream(new File(filename));65}6667public static void doStartStopTest1() throws Exception {68System.out.println("TEST 1: play for 3 seconds, stop/start/stop/start/play for 3 seconds...");69source.start();70Thread.sleep(100);71bytesWritten = 0;72System.out.println("Waiting for 3 seconds...");73Thread.sleep(3000);74buffersWrittenAfter5Seconds = buffersWritten;75System.out.println("Buffers Written: "+buffersWritten);76System.out.println("stop()->start()->stop()->start()");77source.stop();78//System.out.println("start()");79source.start();80//System.out.println("stop()2 ----------------------------------------------------------");81source.stop();82//System.out.println("start()");83source.start();84System.out.println("Buffers Written: "+buffersWritten);85System.out.println("Waiting for 3 seconds...");86Thread.sleep(3000);87System.out.println("Buffers Written: "+buffersWritten);88if (buffersWritten >= ((buffersWrittenAfter5Seconds * 2) - ((buffersWrittenAfter5Seconds / 4)))) {89successfulTests++;90}91}9293private static int nextWaitTime() {94int waitTime = random.nextInt(25);95waitTime*=waitTime;96if (waitTime<20) waitTime = 0;97return waitTime;98}99100101public static void doStartStopTest2() throws Exception {102System.out.println("TEST 2: start and stop 100 times with random wait in between");103int max=100;104for (int i=0; i<max; i++) {105System.out.println("Round "+i);106System.out.println("Start....");107source.start();108int waitTime = nextWaitTime();109System.out.println("Waiting for "+waitTime+"ms...");110if (waitTime>0) {111Thread.sleep(waitTime);112}113System.out.println("stop()");114source.stop();115waitTime = nextWaitTime();116System.out.println("Waiting for "+waitTime+"ms...");117if (waitTime>0) {118Thread.sleep(waitTime);119}120}121}122123public static void doStartStopTest3() throws Exception {124System.out.println("TEST 3: start and stop 100 times with random wait only every 10 rounds ");125int max=100;126for (int i=0; i<max; i++) {127System.out.println("Round "+i);128System.out.println("Start....");129source.start();130if (i % 10 == 9) {131int waitTime = nextWaitTime();132System.out.println("Waiting for "+waitTime+"ms...");133if (waitTime>0) {134Thread.sleep(waitTime);135}136}137System.out.println("stop()");138source.stop();139if (i % 13 == 12) {140int waitTime = nextWaitTime();141System.out.println("Waiting for "+waitTime+"ms...");142if (waitTime>0) {143Thread.sleep(waitTime);144}145}146}147}148149public static void runTest(int testNum) {150terminated = false;151Thread thread = null;152buffersWrittenAfter5Seconds = 0;153// make the tests reproduceable by always seeding with same value154random.setSeed(1);155try {156executedTests++;157thread = new Thread(new StopStart());158thread.start();159switch (testNum) {160case 1: doStartStopTest1(); break;161case 2: doStartStopTest2(); break;162case 3: doStartStopTest3(); break;163}164} catch (Exception e) {165e.printStackTrace();166}167source.stop();168source.close();169if (thread!=null) {170terminated = true;171System.out.println("Waiting for thread to die...");172try {173thread.join();174} catch (InterruptedException ie) {175ie.printStackTrace();176}177}178}179180public static void main(String[] args) throws Exception {181filename = null;182if (args.length>0) {183File f = new File(args[0]);184if (f.exists()) {185filename = args[0];186System.out.println("Opening "+filename);187constructAIS();188audioFormat = ais.getFormat();189}190}191if (filename == null) {192audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);193for (int i=0; i<audioData.length; i++) {194audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);195}196}197long startTime = System.currentTimeMillis();198Mixer.Info[] mixers = AudioSystem.getMixerInfo();199for (int i=0; i<mixers.length; i++) {200try {201Mixer mixer = AudioSystem.getMixer(mixers[i]);202DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);203String mixerName = mixer.getMixerInfo().getName();204try {205source = (SourceDataLine) mixer.getLine(info);206source.open(audioFormat);207} catch (IllegalArgumentException iae) {208System.out.println("Mixer "+mixerName+" does not provide a SourceDataLine.");209continue;210} catch (LineUnavailableException lue) {211System.out.println("Mixer "+mixerName+": no lines available.");212continue;213}214System.out.println("***** Testing on Mixer "+mixerName+":");215//runTest(2);216//runTest(3);217runTest(1);218} catch (Exception e) {219e.printStackTrace();220}221}222if (mixers.length==0) {223System.out.println("No mixers available!");224} else {225long duration = System.currentTimeMillis() - startTime;226227System.out.println("Test took "+(duration/1000)+"s and "+(duration % 1000)+"ms.");228}229230System.out.println("Exiting main()");231if (executedTests>0) {232if (successfulTests == 0) {233if (args.length == 0) {234throw new Exception("Test FAILED");235}236System.out.println("test FAILED.");237} else {238System.out.println("test successful.");239}240} else {241System.out.println("Could not execute any tests - are soundcards correctly installed?");242System.out.println("Test NOT FAILED.");243}244}245246public void run() {247int len = audioData.length;248int offset = len;249System.out.println("Thread: started. Beginning audio i/o");250while (!terminated) {251try {252//if (!source.isActive()) {253// Thread.sleep(50);254//}255if (offset >= len) {256offset = 0;257if (ais!=null) {258do {259len = ais.read(audioData, 0, audioData.length);260if (len < 0) {261constructAIS();262}263} while (len < 0);264}265}266int toWrite = len - offset;267int written = source.write(audioData, offset, toWrite);268offset+=written;269bytesWritten += written;270buffersWritten = (int) (bytesWritten / audioData.length);271} catch (Exception e) {272e.printStackTrace();273terminated = true;274}275}276System.out.println("Thread: closing line");277source.stop();278source.close();279System.out.println("Thread finished");280}281}282283284