1 /*
   2  * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.sound.sampled.AudioFormat;
  25 import javax.sound.sampled.AudioSystem;
  26 import javax.sound.sampled.DataLine;
  27 import javax.sound.sampled.LineUnavailableException;
  28 import javax.sound.sampled.Mixer;
  29 import javax.sound.sampled.SourceDataLine;
  30 
  31 /**
  32  * @test
  33  * @bug 4661602
  34  * @summary Buffersize is checked when re-opening line
  35  */
  36 public class BufferSizeCheck {
  37 
  38     public static void main(String[] args) throws Exception {
  39         boolean realTest = false;
  40         if (!isSoundcardInstalled()) {
  41             return;
  42         }
  43 
  44         try {
  45             out("4661602: Buffersize is checked when re-opening line");
  46             AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
  47             DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  48             SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);
  49             out("Opening with buffersize 12000...");
  50             sdl.open(format, 12000);
  51             out("Opening with buffersize 11000...");
  52             realTest=true;
  53             sdl.open(format, 11000);
  54             try {
  55                 sdl.close();
  56             } catch(Throwable t) {}
  57         } catch (Exception e) {
  58             e.printStackTrace();
  59             // do not fail if no audio device installed - bug 4742021
  60             if (realTest || !(e instanceof LineUnavailableException)) {
  61                 throw e;
  62             }
  63         }
  64         out("Test passed");
  65     }
  66 
  67     static void out(String s) {
  68         System.out.println(s); System.out.flush();
  69     }
  70 
  71     /**
  72      * Returns true if at least one soundcard is correctly installed
  73      * on the system.
  74      */
  75     public static boolean isSoundcardInstalled() {
  76         boolean result = false;
  77         try {
  78             Mixer.Info[] mixers = AudioSystem.getMixerInfo();
  79             if (mixers.length > 0) {
  80                 result = AudioSystem.getSourceDataLine(null) != null;
  81             }
  82         } catch (Exception e) {
  83             System.err.println("Exception occured: "+e);
  84         }
  85         if (!result) {
  86             System.err.println("Soundcard does not exist or sound drivers not installed!");
  87             System.err.println("This test requires sound drivers for execution.");
  88         }
  89         return result;
  90     }
  91 }