Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java
41161 views
/*1* Copyright (c) 2002, 2019, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import javax.sound.sampled.Mixer;28import javax.sound.sampled.spi.MixerProvider;2930/**31* DirectAudioDevice provider.32*33* @author Florian Bomers34*/35public final class DirectAudioDeviceProvider extends MixerProvider {3637/**38* Set of info objects for all port input devices on the system.39*/40private static DirectAudioDeviceInfo[] infos;4142/**43* Set of all port input devices on the system.44*/45private static DirectAudioDevice[] devices;4647static {48// initialize49Platform.initialize();50}5152/**53* Required public no-arg constructor.54*/55public DirectAudioDeviceProvider() {56synchronized (DirectAudioDeviceProvider.class) {57if (Platform.isDirectAudioEnabled()) {58init();59} else {60infos = new DirectAudioDeviceInfo[0];61devices = new DirectAudioDevice[0];62}63}64}6566private static void init() {67// get the number of input devices68int numDevices = nGetNumDevices();6970if (infos == null || infos.length != numDevices) {71// initialize the arrays72infos = new DirectAudioDeviceInfo[numDevices];73devices = new DirectAudioDevice[numDevices];7475// fill in the info objects now.76for (int i = 0; i < infos.length; i++) {77infos[i] = nNewDirectAudioDeviceInfo(i);78}79}80}8182@Override83public Mixer.Info[] getMixerInfo() {84synchronized (DirectAudioDeviceProvider.class) {85Mixer.Info[] localArray = new Mixer.Info[infos.length];86System.arraycopy(infos, 0, localArray, 0, infos.length);87return localArray;88}89}9091@Override92public Mixer getMixer(Mixer.Info info) {93synchronized (DirectAudioDeviceProvider.class) {94// if the default device is asked, we provide the mixer95// with SourceDataLine's96if (info == null) {97for (int i = 0; i < infos.length; i++) {98Mixer mixer = getDevice(infos[i]);99if (mixer.getSourceLineInfo().length > 0) {100return mixer;101}102}103}104// otherwise get the first mixer that matches105// the requested info object106for (int i = 0; i < infos.length; i++) {107if (infos[i].equals(info)) {108return getDevice(infos[i]);109}110}111}112throw new IllegalArgumentException(113String.format("Mixer %s not supported by this provider", info));114}115116private static Mixer getDevice(DirectAudioDeviceInfo info) {117int index = info.getIndex();118if (devices[index] == null) {119devices[index] = new DirectAudioDevice(info);120}121return devices[index];122}123124/**125* Info class for DirectAudioDevices. Adds an index value and a string for126* making native references to a particular device.127* This constructor is called from native.128*/129static final class DirectAudioDeviceInfo extends Mixer.Info {130private final int index;131private final int maxSimulLines;132133// For ALSA, the deviceID contains the encoded card index, device index, and sub-device-index134private final int deviceID;135136private DirectAudioDeviceInfo(int index, int deviceID, int maxSimulLines,137String name, String vendor,138String description, String version) {139super(name, vendor, "Direct Audio Device: "+description, version);140this.index = index;141this.maxSimulLines = maxSimulLines;142this.deviceID = deviceID;143}144145int getIndex() {146return index;147}148149int getMaxSimulLines() {150return maxSimulLines;151}152153int getDeviceID() {154return deviceID;155}156} // class DirectAudioDeviceInfo157158private static native int nGetNumDevices();159// index: [0..nGetNumDevices()-1]160private static native DirectAudioDeviceInfo nNewDirectAudioDeviceInfo(int deviceIndex);161}162163164