Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java
38924 views
/*1* Copyright (c) 2002, 2013, 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;293031/**32* DirectAudioDevice provider.33*34* @author Florian Bomers35*/36public final class DirectAudioDeviceProvider extends MixerProvider {3738// STATIC VARIABLES3940/**41* Set of info objects for all port input devices on the system.42*/43private static DirectAudioDeviceInfo[] infos;4445/**46* Set of all port input devices on the system.47*/48private static DirectAudioDevice[] devices;495051// STATIC5253static {54// initialize55Platform.initialize();56}575859// CONSTRUCTOR606162/**63* Required public no-arg constructor.64*/65public DirectAudioDeviceProvider() {66synchronized (DirectAudioDeviceProvider.class) {67if (Platform.isDirectAudioEnabled()) {68init();69} else {70infos = new DirectAudioDeviceInfo[0];71devices = new DirectAudioDevice[0];72}73}74}7576private static void init() {77// get the number of input devices78int numDevices = nGetNumDevices();7980if (infos == null || infos.length != numDevices) {81if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init()");82// initialize the arrays83infos = new DirectAudioDeviceInfo[numDevices];84devices = new DirectAudioDevice[numDevices];8586// fill in the info objects now.87for (int i = 0; i < infos.length; i++) {88infos[i] = nNewDirectAudioDeviceInfo(i);89}90if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init(): found numDevices: " + numDevices);91}92}9394public Mixer.Info[] getMixerInfo() {95synchronized (DirectAudioDeviceProvider.class) {96Mixer.Info[] localArray = new Mixer.Info[infos.length];97System.arraycopy(infos, 0, localArray, 0, infos.length);98return localArray;99}100}101102103public Mixer getMixer(Mixer.Info info) {104synchronized (DirectAudioDeviceProvider.class) {105// if the default device is asked, we provide the mixer106// with SourceDataLine's107if (info == null) {108for (int i = 0; i < infos.length; i++) {109Mixer mixer = getDevice(infos[i]);110if (mixer.getSourceLineInfo().length > 0) {111return mixer;112}113}114}115// otherwise get the first mixer that matches116// the requested info object117for (int i = 0; i < infos.length; i++) {118if (infos[i].equals(info)) {119return getDevice(infos[i]);120}121}122}123throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");124}125126127private static Mixer getDevice(DirectAudioDeviceInfo info) {128int index = info.getIndex();129if (devices[index] == null) {130devices[index] = new DirectAudioDevice(info);131}132return devices[index];133}134135// INNER CLASSES136137138/**139* Info class for DirectAudioDevices. Adds an index value and a string for140* making native references to a particular device.141* This constructor is called from native.142*/143static final class DirectAudioDeviceInfo extends Mixer.Info {144private final int index;145private final int maxSimulLines;146147// For ALSA, the deviceID contains the encoded card index, device index, and sub-device-index148private final int deviceID;149150private DirectAudioDeviceInfo(int index, int deviceID, int maxSimulLines,151String name, String vendor,152String description, String version) {153super(name, vendor, "Direct Audio Device: "+description, version);154this.index = index;155this.maxSimulLines = maxSimulLines;156this.deviceID = deviceID;157}158159int getIndex() {160return index;161}162163int getMaxSimulLines() {164return maxSimulLines;165}166167int getDeviceID() {168return deviceID;169}170} // class DirectAudioDeviceInfo171172// NATIVE METHODS173private static native int nGetNumDevices();174// index: [0..nGetNumDevices()-1]175private static native DirectAudioDeviceInfo nNewDirectAudioDeviceInfo(int deviceIndex);176}177178179