jdk/test/javax/sound/sampled/Clip/ClipCloseLoss.java
changeset 41905 e8e5df013c6e
child 44656 72decb1e4935
equal deleted inserted replaced
41904:524d908e49ea 41905:e8e5df013c6e
       
     1 /*
       
     2  * Copyright (c) 2003, 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 java.io.ByteArrayInputStream;
       
    25 
       
    26 import javax.sound.sampled.AudioFormat;
       
    27 import javax.sound.sampled.AudioInputStream;
       
    28 import javax.sound.sampled.AudioSystem;
       
    29 import javax.sound.sampled.Clip;
       
    30 import javax.sound.sampled.DataLine;
       
    31 import javax.sound.sampled.LineUnavailableException;
       
    32 import javax.sound.sampled.Mixer;
       
    33 
       
    34 /**
       
    35  * @test
       
    36  * @bug 4946913
       
    37  * @summary DirectClip doesn't kill the thread correctly, sometimes
       
    38  * @run main/othervm ClipCloseLoss
       
    39  * @key headful
       
    40  */
       
    41 public class ClipCloseLoss {
       
    42     static int frameCount = 441000; // lets say 10 seconds
       
    43     static AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);
       
    44     static ByteArrayInputStream bais =
       
    45     new ByteArrayInputStream(new byte[frameCount * format.getFrameSize()]);
       
    46 
       
    47     static int success = 0;
       
    48     static boolean failed = false;
       
    49 
       
    50     public static void run(Mixer m) {
       
    51         Clip clip = null;
       
    52         try {
       
    53             if (m == null) {
       
    54                 out("Using default mixer");
       
    55                 clip = (Clip) AudioSystem.getClip();
       
    56             } else {
       
    57                 out("Using mixer: "+m);
       
    58                 DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);
       
    59                 clip = (Clip) m.getLine(info);
       
    60             }
       
    61             out(" got clip: "+clip);
       
    62             if (!clip.getClass().toString().contains("Direct")) {
       
    63                 out(" no direct audio clip -> do not test.");
       
    64                 return;
       
    65             }
       
    66 
       
    67             out(" open");
       
    68             bais.reset();
       
    69             clip.open(new AudioInputStream(bais, format, frameCount));
       
    70 
       
    71             out(" clip.close()");
       
    72             //long t = System.currentTimeMillis();
       
    73             clip.close();
       
    74             //if (System.currentTimeMillis() - t > 1950) {
       
    75             //  out(" clip.close needed more than 2 seconds! Causes failure of this test.");
       
    76             //  failed = true;
       
    77             //}
       
    78             out(" clip closed");
       
    79             success++;
       
    80         } catch (LineUnavailableException luae) {
       
    81             // line not available, test not failed
       
    82             System.err.println(luae);
       
    83         } catch (IllegalArgumentException iae) {
       
    84             // line not available, test not failed
       
    85             System.err.println(iae);
       
    86         } catch (Throwable t) {
       
    87             t.printStackTrace();
       
    88         }
       
    89     }
       
    90 
       
    91     public static int getClipThreadCount() {
       
    92         int ret = 0;
       
    93         ThreadGroup tg = Thread.currentThread().getThreadGroup();
       
    94         while (tg.getParent() != null) { tg = tg.getParent(); }
       
    95         Thread[] threads = new Thread[500];
       
    96         int count = tg.enumerate(threads, true);
       
    97         for (int i = 0; i < count; i++) {
       
    98                 if (threads[i].getName().contains("Direct")
       
    99                     && threads[i].getName().contains("Clip")) {
       
   100                         out("Found Direct Clip thread object: "+threads[i]);
       
   101                         ret++;
       
   102                 }
       
   103         }
       
   104         return ret;
       
   105     }
       
   106 
       
   107     public static void main(String[] args) throws Exception    {
       
   108         if (isSoundcardInstalled()) {
       
   109             bais.mark(0);
       
   110             run(null);
       
   111             Mixer.Info[] infos = AudioSystem.getMixerInfo();
       
   112             for (int i = 0; i<infos.length; i++) {
       
   113                 try {
       
   114                     Mixer m = AudioSystem.getMixer(infos[i]);
       
   115                     run(m);
       
   116                 } catch (Exception e) {
       
   117                 }
       
   118             }
       
   119             out("Waiting 1 second to dispose of all threads");
       
   120             Thread.sleep(1000);
       
   121             if (getClipThreadCount() > 0) {
       
   122                 out("Unused clip threads exist! Causes test failure");
       
   123                 failed = true;
       
   124             }
       
   125             if (failed) throw new Exception("Test FAILED!");
       
   126             if (success > 0) {
       
   127                 out("Test passed.");
       
   128             } else {
       
   129                 System.err.println("Test could not execute: please install an audio device");
       
   130             }
       
   131         }
       
   132     }
       
   133 
       
   134     /**
       
   135     * Returns true if at least one soundcard is correctly installed
       
   136     * on the system.
       
   137     */
       
   138     public static boolean isSoundcardInstalled() {
       
   139         boolean result = false;
       
   140         try {
       
   141             Mixer.Info[] mixers = AudioSystem.getMixerInfo();
       
   142             if (mixers.length > 0) {
       
   143                 result = AudioSystem.getSourceDataLine(null) != null;
       
   144             }
       
   145         } catch (Exception e) {
       
   146             System.err.println("Exception occured: "+e);
       
   147         }
       
   148         if (!result) {
       
   149             System.err.println("Soundcard does not exist or sound drivers not installed!");
       
   150             System.err.println("This test requires sound drivers for execution.");
       
   151         }
       
   152         return result;
       
   153     }
       
   154 
       
   155     public static void out(String s) {
       
   156         /*long t = System.nanoTime() / 1000000l;
       
   157         String ts = ""+(t % 1000);
       
   158         while (ts.length() < 3) ts = "0"+ts;
       
   159         System.out.println(""+(t/1000)+":"+ts+" "+s);
       
   160         System.out.flush();*/
       
   161         System.out.println(s);
       
   162     }
       
   163 }