1/*
2 * Copyright (c) 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
24import java.util.ArrayList;
25import java.util.List;
26import java.util.concurrent.CountDownLatch;
27
28import javax.sound.sampled.AudioFormat;
29import javax.sound.sampled.AudioSystem;
30import javax.sound.sampled.Clip;
31import javax.sound.sampled.DataLine;
32import javax.sound.sampled.Line;
33import javax.sound.sampled.LineEvent;
34import javax.sound.sampled.LineUnavailableException;
35
36/**
37 * @test
38 * @bug 8156169
39 * @run main/othervm/timeout=300 IsRunningHang
40 */
41public final class IsRunningHang {
42
43    private static CountDownLatch go;
44
45    /**
46     * We will try to use all usually supported formats.
47     */
48    private static final List<AudioFormat> formats = new ArrayList<>();
49
50    private static final AudioFormat.Encoding[] encodings = {
51            AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
52            AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
53            AudioFormat.Encoding.PCM_FLOAT
54    };
55
56    private static final int[] sampleRates = {8000, 16000, 48000};
57
58    private static final int[] sampleBits = {8, 16, 24, 32, 64};
59
60    private static final int[] channels = {1, 2, 3, 5};
61
62    static {
63        for (final Boolean end : new boolean[]{false, true}) {
64            for (final int sampleSize : sampleBits) {
65                for (final int sampleRate : sampleRates) {
66                    for (final int channel : channels) {
67                        for (final AudioFormat.Encoding enc : encodings) {
68                            int s = ((sampleSize + 7) / 8) * channel;
69                            formats.add(new AudioFormat(enc, sampleRate,
70                                                        sampleSize, channel,
71                                                        s, sampleRate, end));
72                        }
73                    }
74                }
75            }
76        }
77    }
78
79    public static void main(final String[] args) throws Exception {
80        for (final AudioFormat format : formats) {
81            System.out.println("format = " + format);
82            // create a 0.5-second data
83            byte[] soundData = new byte[(int) (format.getFrameRate()
84                                                       * format.getFrameSize()
85                                                       / 2)];
86            try {
87                test(format, soundData);
88            } catch (LineUnavailableException | IllegalArgumentException ignored) {
89                // the test is not applicable
90            }
91        }
92    }
93
94    private static void test(final AudioFormat format, final byte[] data)
95            throws Exception {
96        final Line.Info info = new DataLine.Info(Clip.class, format);
97        final Clip clip = (Clip) AudioSystem.getLine(info);
98
99        go = new CountDownLatch(1);
100        clip.addLineListener(event -> {
101            if (event.getType().equals(LineEvent.Type.START)) {
102                go.countDown();
103            }
104        });
105
106        clip.open(format, data, 0, data.length);
107        clip.start();
108        go.await();
109        while (clip.isRunning()) {
110            // This loop should not hang
111        }
112        while (clip.isActive()) {
113            // This loop should not hang
114        }
115        clip.close();
116    }
117}
118