Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/sampled/AudioSystem/DefaultMixers.java
38855 views
/*1* Copyright (c) 2003, 2020, 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*/44public class DefaultMixers {4546private static final String ERROR_PROVIDER_CLASS_NAME = "abc";47private static final String ERROR_INSTANCE_NAME = "def";4849private static final Class[] lineClasses = {50SourceDataLine.class,51TargetDataLine.class,52Clip.class,53Port.class,54};5556public static void main(String[] args) throws Exception {57boolean allOk = true;58Mixer.Info[] infos;5960out("Testing Mixers retrieved via AudioSystem");61infos = AudioSystem.getMixerInfo();62allOk &= testMixers(infos, null);6364out("Testing MixerProviders");65List providers = JDK13Services.getProviders(MixerProvider.class);66for (int i = 0; i < providers.size(); i++) {67MixerProvider provider = (MixerProvider) providers.get(i);68infos = provider.getMixerInfo();69allOk &= testMixers(infos, provider.getClass().getName());70}7172if (! allOk) {73throw new Exception("Test failed");74} else {75out("Test passed");76}77}7879private static boolean testMixers(Mixer.Info[] infos,80String providerClassName) {81boolean allOk = true;8283for (int i = 0; i < infos.length; i++) {84Mixer mixer = null;85try {86mixer = AudioSystem.getMixer(infos[i]);87} catch (NullPointerException e) {88out("Exception thrown; Test NOT failed.");89e.printStackTrace();90}91for (int j = 0; j < lineClasses.length; j++) {92if (mixer.isLineSupported(new Line.Info(lineClasses[j]))) {93allOk &= testMixer(mixer, lineClasses[j],94providerClassName);95}96}97}98return allOk;99}100101private static boolean testMixer(Mixer mixer, Class lineType,102String providerClassName) {103boolean allOk = true;104String instanceName = mixer.getMixerInfo().getName();105106// no error107allOk &= testMixer(mixer, lineType,108providerClassName, instanceName);109110// erroneous provider class name, correct instance name111allOk &= testMixer(mixer, lineType,112ERROR_PROVIDER_CLASS_NAME, instanceName);113114// erroneous provider class name, no instance name115allOk &= testMixer(mixer, lineType,116ERROR_PROVIDER_CLASS_NAME, "");117118// erroneous provider class name, erroneous instance name119allOk &= testMixer(mixer, lineType,120ERROR_PROVIDER_CLASS_NAME, ERROR_INSTANCE_NAME);121122return allOk;123}124125private static boolean testMixer(Mixer mixer, Class lineType,126String providerClassName,127String instanceName) {128boolean allOk = true;129130try {131String propertyValue = (providerClassName != null) ? providerClassName: "" ;132propertyValue += "#" + instanceName;133out("property value: " + propertyValue);134System.setProperty(lineType.getName(), propertyValue);135Line line = null;136Line.Info info = null;137Line.Info[] infos;138AudioFormat format = null;139if (lineType == SourceDataLine.class || lineType == Clip.class) {140infos = mixer.getSourceLineInfo();141format = getFirstLinearFormat(infos);142info = new DataLine.Info(lineType, format);143} else if (lineType == TargetDataLine.class) {144infos = mixer.getTargetLineInfo();145format = getFirstLinearFormat(infos);146info = new DataLine.Info(lineType, format);147} else if (lineType == Port.class) {148/* Actually, a Ports Mixer commonly has source infos149as well as target infos. We ignore this here, since we150just need a random one. */151infos = mixer.getSourceLineInfo();152for (int i = 0; i < infos.length; i++) {153if (infos[i] instanceof Port.Info) {154info = infos[i];155break;156}157}158}159out("Line.Info: " + info);160line = AudioSystem.getLine(info);161out("line: " + line);162if (! lineType.isInstance(line)) {163out("type " + lineType + " failed: class should be '" +164lineType + "' but is '" + line.getClass() + "'!");165allOk = false;166}167} catch (Exception e) {168out("Exception thrown; Test NOT failed.");169e.printStackTrace();170}171return allOk;172}173174private static AudioFormat getFirstLinearFormat(Line.Info[] infos) {175for (int i = 0; i < infos.length; i++) {176if (infos[i] instanceof DataLine.Info) {177AudioFormat[] formats = ((DataLine.Info) infos[i]).getFormats();178for (int j = 0; j < formats.length; j++) {179AudioFormat.Encoding encoding = formats[j].getEncoding();180int sampleSizeInBits = formats[j].getSampleSizeInBits();181if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED) &&182sampleSizeInBits == 16 ||183encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) &&184sampleSizeInBits == 16) {185return formats[j];186}187}188}189}190return null;191}192193private static void out(String message) {194System.out.println(message);195}196}197198199