Path: blob/master/test/jdk/javax/sound/sampled/AudioSystem/DefaultMixers.java
41155 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.List;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioSystem;27import javax.sound.sampled.Clip;28import javax.sound.sampled.DataLine;29import javax.sound.sampled.Line;30import javax.sound.sampled.Mixer;31import javax.sound.sampled.Port;32import javax.sound.sampled.SourceDataLine;33import javax.sound.sampled.TargetDataLine;34import javax.sound.sampled.spi.MixerProvider;3536import com.sun.media.sound.JDK13Services;3738/**39* @test40* @bug 477651141* @summary RFE: Setting the default MixerProvider. Test the retrieving of lines42* with defaut mixer properties.43* @modules java.desktop/com.sun.media.sound44*/45public class DefaultMixers {4647private static final String ERROR_PROVIDER_CLASS_NAME = "abc";48private static final String ERROR_INSTANCE_NAME = "def";4950private static final Class[] lineClasses = {51SourceDataLine.class,52TargetDataLine.class,53Clip.class,54Port.class,55};5657public static void main(String[] args) throws Exception {58boolean allOk = true;59Mixer.Info[] infos;6061out("Testing Mixers retrieved via AudioSystem");62infos = AudioSystem.getMixerInfo();63allOk &= testMixers(infos, null);6465out("Testing MixerProviders");66List providers = JDK13Services.getProviders(MixerProvider.class);67for (int i = 0; i < providers.size(); i++) {68MixerProvider provider = (MixerProvider) providers.get(i);69infos = provider.getMixerInfo();70allOk &= testMixers(infos, provider.getClass().getName());71}7273if (! allOk) {74throw new Exception("Test failed");75} else {76out("Test passed");77}78}7980private static boolean testMixers(Mixer.Info[] infos,81String providerClassName) {82boolean allOk = true;8384for (int i = 0; i < infos.length; i++) {85Mixer mixer = null;86try {87mixer = AudioSystem.getMixer(infos[i]);88} catch (NullPointerException e) {89out("Exception thrown; Test NOT failed.");90e.printStackTrace();91}92for (int j = 0; j < lineClasses.length; j++) {93if (mixer.isLineSupported(new Line.Info(lineClasses[j]))) {94allOk &= testMixer(mixer, lineClasses[j],95providerClassName);96}97}98}99return allOk;100}101102private static boolean testMixer(Mixer mixer, Class lineType,103String providerClassName) {104boolean allOk = true;105String instanceName = mixer.getMixerInfo().getName();106107// no error108allOk &= testMixer(mixer, lineType,109providerClassName, instanceName);110111// erroneous provider class name, correct instance name112allOk &= testMixer(mixer, lineType,113ERROR_PROVIDER_CLASS_NAME, instanceName);114115// erroneous provider class name, no instance name116allOk &= testMixer(mixer, lineType,117ERROR_PROVIDER_CLASS_NAME, "");118119// erroneous provider class name, erroneous instance name120allOk &= testMixer(mixer, lineType,121ERROR_PROVIDER_CLASS_NAME, ERROR_INSTANCE_NAME);122123return allOk;124}125126private static boolean testMixer(Mixer mixer, Class lineType,127String providerClassName,128String instanceName) {129boolean allOk = true;130131try {132String propertyValue = (providerClassName != null) ? providerClassName: "" ;133propertyValue += "#" + instanceName;134out("property value: " + propertyValue);135System.setProperty(lineType.getName(), propertyValue);136Line line = null;137Line.Info info = null;138Line.Info[] infos;139AudioFormat format = null;140if (lineType == SourceDataLine.class || lineType == Clip.class) {141infos = mixer.getSourceLineInfo();142format = getFirstLinearFormat(infos);143info = new DataLine.Info(lineType, format);144} else if (lineType == TargetDataLine.class) {145infos = mixer.getTargetLineInfo();146format = getFirstLinearFormat(infos);147info = new DataLine.Info(lineType, format);148} else if (lineType == Port.class) {149/* Actually, a Ports Mixer commonly has source infos150as well as target infos. We ignore this here, since we151just need a random one. */152infos = mixer.getSourceLineInfo();153for (int i = 0; i < infos.length; i++) {154if (infos[i] instanceof Port.Info) {155info = infos[i];156break;157}158}159}160out("Line.Info: " + info);161line = AudioSystem.getLine(info);162out("line: " + line);163if (! lineType.isInstance(line)) {164out("type " + lineType + " failed: class should be '" +165lineType + "' but is '" + line.getClass() + "'!");166allOk = false;167}168} catch (Exception e) {169out("Exception thrown; Test NOT failed.");170e.printStackTrace();171}172return allOk;173}174175private static AudioFormat getFirstLinearFormat(Line.Info[] infos) {176for (int i = 0; i < infos.length; i++) {177if (infos[i] instanceof DataLine.Info) {178AudioFormat[] formats = ((DataLine.Info) infos[i]).getFormats();179for (int j = 0; j < formats.length; j++) {180AudioFormat.Encoding encoding = formats[j].getEncoding();181int sampleSizeInBits = formats[j].getSampleSizeInBits();182if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED) &&183sampleSizeInBits == 16 ||184encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) &&185sampleSizeInBits == 16) {186return formats[j];187}188}189}190}191return null;192}193194private static void out(String message) {195System.out.println(message);196}197}198199200