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.Mixer;
  29 import javax.sound.sampled.SourceDataLine;
  30 import javax.sound.sampled.TargetDataLine;
  31 
  32 /**
  33  * @test
  34  * @bug 5053380
  35  * @summary Verify that getting a line initializes it with the format in
  36  *          DataLine.Info
  37  */
  38 public class LineDefFormat {
  39 
  40     final static int samplerate = 22050;
  41     static int passed = 0;
  42     static int failed = 0;
  43 
  44     private static void doLine1(DataLine line, AudioFormat format) {
  45         try {
  46             System.out.println("  - got line: "+line);
  47             System.out.println("  - line has format: "+line.getFormat());
  48             if (!line.getFormat().matches(format)) {
  49                 System.out.println("  ## Error: expected this format: "+format);
  50                 failed++;
  51             } else {
  52                 passed++;
  53             }
  54         } catch (Throwable t) {
  55             System.out.println("  - Caught exception. Not failed.");
  56             System.out.println("  - "+t.toString());
  57         }
  58     }
  59 
  60     private static void doLine2(DataLine line, AudioFormat format) {
  61         try {
  62             System.out.println("  - call to open()");
  63             line.open();
  64             try {
  65                 System.out.println("  - line has format: "+line.getFormat());
  66                 if (!line.getFormat().matches(format)) {
  67                     System.out.println("## Error: expected this format: "+format);
  68                     failed++;
  69                 } else {
  70                     passed++;
  71                 }
  72             } finally {
  73                 line.close();
  74                 System.out.println("  - closed");
  75             }
  76         } catch (Throwable t) {
  77             System.out.println("  - Caught exception. Not failed.");
  78             System.out.println("  - "+t.toString());
  79         }
  80     }
  81 
  82     private static void doMixerClip(Mixer mixer, AudioFormat format) {
  83         if (mixer==null) return;
  84         try {
  85             System.out.println("Clip from mixer "+mixer+":");
  86             System.out.println("   "+mixer.getMixerInfo());
  87                 DataLine.Info info = new DataLine.Info(
  88                                           Clip.class,
  89                                           format);
  90 
  91             if (mixer.isLineSupported(info)) {
  92                 Clip clip = (Clip) mixer.getLine(info);
  93                 doLine1(clip, format);
  94             } else {
  95                 System.out.println("  - Line not supported");
  96             }
  97         } catch (Throwable t) {
  98             System.out.println("  - Caught exception. Not failed.");
  99             System.out.println("  - "+t.toString());
 100         }
 101     }
 102 
 103     private static void doMixerSDL(Mixer mixer, AudioFormat format) {
 104         if (mixer==null) return;
 105         try {
 106             System.out.println("SDL from mixer "+mixer+":");
 107                 DataLine.Info info = new DataLine.Info(
 108                                           SourceDataLine.class,
 109                                           format);
 110 
 111             if (mixer.isLineSupported(info)) {
 112                 SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
 113                 doLine1(sdl, format);
 114                 doLine2(sdl, format);
 115             } else {
 116                 System.out.println("  - Line not supported");
 117             }
 118         } catch (Throwable t) {
 119             System.out.println("  - Caught exception. Not failed.");
 120             System.out.println("  - "+t.toString());
 121         }
 122     }
 123 
 124     private static void doMixerTDL(Mixer mixer, AudioFormat format) {
 125         if (mixer==null) return;
 126         try {
 127             System.out.println("TDL from mixer "+mixer+":");
 128                 DataLine.Info info = new DataLine.Info(
 129                                           TargetDataLine.class,
 130                                           format);
 131             if (mixer.isLineSupported(info)) {
 132                 TargetDataLine tdl = (TargetDataLine) mixer.getLine(info);
 133                 doLine1(tdl, format);
 134                 doLine2(tdl, format);
 135             } else {
 136                 System.out.println("  - Line not supported");
 137             }
 138         } catch (Throwable t) {
 139             System.out.println("  - Caught exception. Not failed.");
 140             System.out.println("  - "+t.toString());
 141         }
 142     }
 143 
 144     private static void doAll() throws Exception {
 145         Mixer.Info[] mixers = AudioSystem.getMixerInfo();
 146                AudioFormat pcm;
 147         for (int i=0; i<mixers.length; i++) {
 148             Mixer mixer = AudioSystem.getMixer(mixers[i]);
 149             pcm = new AudioFormat(samplerate, 16, 1, true, false);
 150             doMixerClip(mixer, pcm);
 151             pcm = new AudioFormat(samplerate, 8, 1, false, false);
 152             doMixerSDL(mixer, pcm);
 153             pcm = new AudioFormat(samplerate, 16, 2, true, true);
 154             doMixerTDL(mixer, pcm);
 155         }
 156         if (mixers.length==0) {
 157             System.out.println("No mixers available!");
 158         }
 159 
 160     }
 161 
 162     public static void main(String args[]) throws Exception{
 163         doAll();
 164         if (passed==0 && failed==0) {
 165             System.out.println("Could not execute any of the tests. Test NOT failed.");
 166         } else if (failed == 0) {
 167             System.out.println("Test PASSED.");
 168         } else {
 169             throw new Exception("Test FAILED!");
 170         }
 171     }
 172 }