< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java

Print this page




  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 /**
  33  * DirectAudioDevice provider.
  34  *
  35  * @author Florian Bomers
  36  */
  37 public final class DirectAudioDeviceProvider extends MixerProvider {
  38 
  39     // STATIC VARIABLES
  40 
  41     /**
  42      * Set of info objects for all port input devices on the system.
  43      */
  44     private static DirectAudioDeviceInfo[] infos;
  45 
  46     /**
  47      * Set of all port input devices on the system.
  48      */
  49     private static DirectAudioDevice[] devices;
  50 
  51 
  52     // STATIC
  53 
  54     static {
  55         // initialize
  56         Platform.initialize();
  57     }
  58 
  59 
  60     // CONSTRUCTOR
  61 
  62 
  63     /**
  64      * Required public no-arg constructor.
  65      */
  66     public DirectAudioDeviceProvider() {
  67         synchronized (DirectAudioDeviceProvider.class) {
  68             if (Platform.isDirectAudioEnabled()) {
  69                 init();
  70             } else {
  71                 infos = new DirectAudioDeviceInfo[0];
  72                 devices = new DirectAudioDevice[0];
  73             }
  74         }
  75     }
  76 
  77     private static void init() {
  78         // get the number of input devices
  79         int numDevices = nGetNumDevices();
  80 
  81         if (infos == null || infos.length != numDevices) {
  82             if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init()");
  83             // initialize the arrays
  84             infos = new DirectAudioDeviceInfo[numDevices];
  85             devices = new DirectAudioDevice[numDevices];
  86 
  87             // fill in the info objects now.
  88             for (int i = 0; i < infos.length; i++) {
  89                 infos[i] = nNewDirectAudioDeviceInfo(i);
  90             }
  91             if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init(): found numDevices: " + numDevices);
  92         }
  93     }
  94 

  95     public Mixer.Info[] getMixerInfo() {
  96         synchronized (DirectAudioDeviceProvider.class) {
  97             Mixer.Info[] localArray = new Mixer.Info[infos.length];
  98             System.arraycopy(infos, 0, localArray, 0, infos.length);
  99             return localArray;
 100         }
 101     }
 102 
 103 
 104     public Mixer getMixer(Mixer.Info info) {
 105         synchronized (DirectAudioDeviceProvider.class) {
 106             // if the default device is asked, we provide the mixer
 107             // with SourceDataLine's
 108             if (info == null) {
 109                 for (int i = 0; i < infos.length; i++) {
 110                     Mixer mixer = getDevice(infos[i]);
 111                     if (mixer.getSourceLineInfo().length > 0) {
 112                         return mixer;
 113                     }
 114                 }
 115             }
 116             // otherwise get the first mixer that matches
 117             // the requested info object
 118             for (int i = 0; i < infos.length; i++) {
 119                 if (infos[i].equals(info)) {
 120                     return getDevice(infos[i]);
 121                 }
 122             }
 123         }
 124         throw new IllegalArgumentException(
 125                 String.format("Mixer %s not supported by this provider", info));
 126     }
 127 
 128 
 129     private static Mixer getDevice(DirectAudioDeviceInfo info) {
 130         int index = info.getIndex();
 131         if (devices[index] == null) {
 132             devices[index] = new DirectAudioDevice(info);
 133         }
 134         return devices[index];
 135     }
 136 
 137     // INNER CLASSES
 138 
 139 
 140     /**
 141      * Info class for DirectAudioDevices.  Adds an index value and a string for
 142      * making native references to a particular device.
 143      * This constructor is called from native.
 144      */
 145     static final class DirectAudioDeviceInfo extends Mixer.Info {
 146         private final int index;
 147         private final int maxSimulLines;
 148 
 149         // For ALSA, the deviceID contains the encoded card index, device index, and sub-device-index
 150         private final int deviceID;
 151 
 152         private DirectAudioDeviceInfo(int index, int deviceID, int maxSimulLines,
 153                                       String name, String vendor,
 154                                       String description, String version) {
 155             super(name, vendor, "Direct Audio Device: "+description, version);
 156             this.index = index;
 157             this.maxSimulLines = maxSimulLines;
 158             this.deviceID = deviceID;
 159         }
 160 
 161         int getIndex() {
 162             return index;
 163         }
 164 
 165         int getMaxSimulLines() {
 166             return maxSimulLines;
 167         }
 168 
 169         int getDeviceID() {
 170             return deviceID;
 171         }
 172     } // class DirectAudioDeviceInfo
 173 
 174     // NATIVE METHODS
 175     private static native int nGetNumDevices();
 176     // index: [0..nGetNumDevices()-1]
 177     private static native DirectAudioDeviceInfo nNewDirectAudioDeviceInfo(int deviceIndex);
 178 }


  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  * DirectAudioDevice provider.
  33  *
  34  * @author Florian Bomers
  35  */
  36 public final class DirectAudioDeviceProvider extends MixerProvider {
  37 


  38     /**
  39      * Set of info objects for all port input devices on the system.
  40      */
  41     private static DirectAudioDeviceInfo[] infos;
  42 
  43     /**
  44      * Set of all port input devices on the system.
  45      */
  46     private static DirectAudioDevice[] devices;
  47 



  48     static {
  49         // initialize
  50         Platform.initialize();
  51     }
  52 




  53     /**
  54      * Required public no-arg constructor.
  55      */
  56     public DirectAudioDeviceProvider() {
  57         synchronized (DirectAudioDeviceProvider.class) {
  58             if (Platform.isDirectAudioEnabled()) {
  59                 init();
  60             } else {
  61                 infos = new DirectAudioDeviceInfo[0];
  62                 devices = new DirectAudioDevice[0];
  63             }
  64         }
  65     }
  66 
  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             if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init()");
  73             // initialize the arrays
  74             infos = new DirectAudioDeviceInfo[numDevices];
  75             devices = new DirectAudioDevice[numDevices];
  76 
  77             // fill in the info objects now.
  78             for (int i = 0; i < infos.length; i++) {
  79                 infos[i] = nNewDirectAudioDeviceInfo(i);
  80             }
  81             if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init(): found numDevices: " + numDevices);
  82         }
  83     }
  84 
  85     @Override
  86     public Mixer.Info[] getMixerInfo() {
  87         synchronized (DirectAudioDeviceProvider.class) {
  88             Mixer.Info[] localArray = new Mixer.Info[infos.length];
  89             System.arraycopy(infos, 0, localArray, 0, infos.length);
  90             return localArray;
  91         }
  92     }
  93 
  94     @Override
  95     public Mixer getMixer(Mixer.Info info) {
  96         synchronized (DirectAudioDeviceProvider.class) {
  97             // if the default device is asked, we provide the mixer
  98             // with SourceDataLine's
  99             if (info == null) {
 100                 for (int i = 0; i < infos.length; i++) {
 101                     Mixer mixer = getDevice(infos[i]);
 102                     if (mixer.getSourceLineInfo().length > 0) {
 103                         return mixer;
 104                     }
 105                 }
 106             }
 107             // otherwise get the first mixer that matches
 108             // the requested info object
 109             for (int i = 0; i < infos.length; i++) {
 110                 if (infos[i].equals(info)) {
 111                     return getDevice(infos[i]);
 112                 }
 113             }
 114         }
 115         throw new IllegalArgumentException(
 116                 String.format("Mixer %s not supported by this provider", info));
 117     }
 118 

 119     private static Mixer getDevice(DirectAudioDeviceInfo info) {
 120         int index = info.getIndex();
 121         if (devices[index] == null) {
 122             devices[index] = new DirectAudioDevice(info);
 123         }
 124         return devices[index];
 125     }
 126 



 127     /**
 128      * Info class for DirectAudioDevices.  Adds an index value and a string for
 129      * making native references to a particular device.
 130      * This constructor is called from native.
 131      */
 132     static final class DirectAudioDeviceInfo extends Mixer.Info {
 133         private final int index;
 134         private final int maxSimulLines;
 135 
 136         // For ALSA, the deviceID contains the encoded card index, device index, and sub-device-index
 137         private final int deviceID;
 138 
 139         private DirectAudioDeviceInfo(int index, int deviceID, int maxSimulLines,
 140                                       String name, String vendor,
 141                                       String description, String version) {
 142             super(name, vendor, "Direct Audio Device: "+description, version);
 143             this.index = index;
 144             this.maxSimulLines = maxSimulLines;
 145             this.deviceID = deviceID;
 146         }
 147 
 148         int getIndex() {
 149             return index;
 150         }
 151 
 152         int getMaxSimulLines() {
 153             return maxSimulLines;
 154         }
 155 
 156         int getDeviceID() {
 157             return deviceID;
 158         }
 159     } // class DirectAudioDeviceInfo
 160 

 161     private static native int nGetNumDevices();
 162     // index: [0..nGetNumDevices()-1]
 163     private static native DirectAudioDeviceInfo nNewDirectAudioDeviceInfo(int deviceIndex);
 164 }
< prev index next >