1 /*
   2  * Copyright (c) 2004, 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.Clip;
  27 import javax.sound.sampled.DataLine;
  28 import javax.sound.sampled.LineUnavailableException;
  29 import javax.sound.sampled.Mixer;
  30 
  31 /**
  32  * @test
  33  * @bug 5021234
  34  * @summary Using -2 for buffer size will fail retrieval of lines
  35  */
  36 public class DataLineInfoNegBufferSize {
  37 
  38     /**
  39      * returns:
  40      * 0: OK
  41      * 1: IAE
  42      * 2: other exception
  43      * 3: line not available
  44      */
  45     public static int run(Mixer m, int bufferSize) {
  46         int res;
  47         int frameCount = 441000; // lets say 10 seconds
  48         AudioFormat f = new AudioFormat(44100.0f, 16, 2, true, false);
  49         Clip clip = null;
  50         try {
  51             System.out.println("Requesting clip from Mixer "
  52                                +(m==null?"default":m.toString())
  53                                +" with bufferSize"+bufferSize);
  54             DataLine.Info info = new DataLine.Info(Clip.class, f, bufferSize);
  55             if (m==null) {
  56                 clip = (Clip) AudioSystem.getLine(info);
  57             } else {
  58                 clip = (Clip) m.getLine(info);
  59             }
  60             System.out.println("Got clip: "+clip+" with Buffer size "+clip.getBufferSize());
  61 
  62             res = 0;
  63         } catch (LineUnavailableException luae) {
  64             System.out.println(luae);
  65             res = 3; // line not available
  66         } catch (IllegalArgumentException iae) {
  67             System.out.println(iae);
  68             res = 1;
  69         } catch (Throwable t) {
  70             System.out.println("->Exception:"+t);
  71             t.printStackTrace();
  72             res=2; // other exception
  73         }
  74         return res;
  75     }
  76 
  77     public static void main(String[] args) throws Exception     {
  78         if (isSoundcardInstalled()) {
  79             int res=0;
  80             int count = 0;
  81             Mixer.Info[] infos = AudioSystem.getMixerInfo();
  82             for (int i = -1; i<infos.length; i++) {
  83                 try {
  84                     Mixer m;
  85                     if (i == -1) {
  86                         m = null;
  87                     } else {
  88                         m = AudioSystem.getMixer(infos[i]);
  89                     }
  90                     int r = run(m, AudioSystem.NOT_SPECIFIED);
  91                     // only continue if successful
  92                     if (r == 0) {
  93                         count++;
  94                         r = run(m, -2);
  95                         if (r == 1) {
  96                             // only fail if IAE was thrown
  97                             System.out.println("#FAILED: using -2 for buffer size does not work!");
  98                             res = 1;
  99                         }
 100                     }
 101                 } catch (Exception e) {
 102                 }
 103             }
 104             if (res!=1) {
 105                 System.out.println("Test passed");
 106             } else {
 107                 if (count == 0) {
 108                     System.err.println("Test could not execute -- no suitable mixers installed. NOT failed");
 109                 }
 110                 throw new Exception("Test FAILED!");
 111             }
 112         }
 113     }
 114 
 115     /**
 116      * Returns true if at least one soundcard is correctly installed
 117      * on the system.
 118      */
 119     public static boolean isSoundcardInstalled() {
 120         boolean result = false;
 121         try {
 122             Mixer.Info[] mixers = AudioSystem.getMixerInfo();
 123             if (mixers.length > 0) {
 124                 result = AudioSystem.getSourceDataLine(null) != null;
 125             }
 126         } catch (Exception e) {
 127             System.err.println("Exception occured: "+e);
 128         }
 129         if (!result) {
 130             System.err.println("Soundcard does not exist or sound drivers not installed!");
 131             System.err.println("This test requires sound drivers for execution.");
 132         }
 133         return result;
 134     }
 135 }