1 /* 2 * Copyright (c) 2002, 2019, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import javax.sound.sampled.Mixer; 29 import javax.sound.sampled.spi.MixerProvider; 30 31 /** 32 * Port provider. 33 * 34 * @author Florian Bomers 35 */ 36 public final class PortMixerProvider extends MixerProvider { 37 38 /** 39 * Set of info objects for all port input devices on the system. 40 */ 41 private static PortMixerInfo[] infos; 42 43 /** 44 * Set of all port input devices on the system. 45 */ 46 private static PortMixer[] devices; 47 48 static { 49 // initialize Platform.initialize()50 Platform.initialize(); 51 } 52 53 /** 54 * Required public no-arg constructor. 55 */ PortMixerProvider()56 public PortMixerProvider() { 57 synchronized (PortMixerProvider.class) { 58 if (Platform.isPortsEnabled()) { 59 init(); 60 } else { 61 infos = new PortMixerInfo[0]; 62 devices = new PortMixer[0]; 63 } 64 } 65 } 66 init()67 private static void init() { 68 // get the number of input devices 69 int numDevices = nGetNumDevices(); 70 71 if (infos == null || infos.length != numDevices) { 72 // initialize the arrays 73 infos = new PortMixerInfo[numDevices]; 74 devices = new PortMixer[numDevices]; 75 76 // fill in the info objects now. 77 // we'll fill in the device objects as they're requested. 78 for (int i = 0; i < infos.length; i++) { 79 infos[i] = nNewPortMixerInfo(i); 80 } 81 } 82 } 83 84 @Override getMixerInfo()85 public Mixer.Info[] getMixerInfo() { 86 synchronized (PortMixerProvider.class) { 87 Mixer.Info[] localArray = new Mixer.Info[infos.length]; 88 System.arraycopy(infos, 0, localArray, 0, infos.length); 89 return localArray; 90 } 91 } 92 93 @Override getMixer(Mixer.Info info)94 public Mixer getMixer(Mixer.Info info) { 95 synchronized (PortMixerProvider.class) { 96 for (int i = 0; i < infos.length; i++) { 97 if (infos[i].equals(info)) { 98 return getDevice(infos[i]); 99 } 100 } 101 } 102 throw new IllegalArgumentException( 103 String.format("Mixer %s not supported by this provider", info)); 104 } 105 getDevice(PortMixerInfo info)106 private static Mixer getDevice(PortMixerInfo info) { 107 int index = info.getIndex(); 108 if (devices[index] == null) { 109 devices[index] = new PortMixer(info); 110 } 111 return devices[index]; 112 } 113 114 /** 115 * Info class for PortMixers. Adds an index value for 116 * making native references to a particular device. 117 * This constructor is called from native. 118 */ 119 static final class PortMixerInfo extends Mixer.Info { 120 private final int index; 121 PortMixerInfo(int index, String name, String vendor, String description, String version)122 private PortMixerInfo(int index, String name, String vendor, String description, String version) { 123 super("Port " + name, vendor, description, version); 124 this.index = index; 125 } 126 getIndex()127 int getIndex() { 128 return index; 129 } 130 131 } // class PortMixerInfo 132 nGetNumDevices()133 private static native int nGetNumDevices(); nNewPortMixerInfo(int mixerIndex)134 private static native PortMixerInfo nNewPortMixerInfo(int mixerIndex); 135 } 136