1 /* 2 * Copyright (c) 1999, 2015, 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 java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.StringTokenizer; 31 32 33 34 /** 35 * Audio configuration class for exposing attributes specific to the platform or system. 36 * 37 * @author Kara Kytle 38 * @author Florian Bomers 39 */ 40 final class Platform { 41 42 43 // STATIC FINAL CHARACTERISTICS 44 45 // native library we need to load 46 private static final String libNameMain = "jsound"; 47 private static final String libNameALSA = "jsoundalsa"; 48 private static final String libNameDSound = "jsoundds"; 49 50 // extra libs handling: bit flags for each different library 51 public static final int LIB_MAIN = 1; 52 public static final int LIB_ALSA = 2; 53 public static final int LIB_DSOUND = 4; 54 55 // bit field of the constants above. Willbe set in loadLibraries 56 private static int loadedLibs = 0; 57 58 // features: the main native library jsound reports which feature is 59 // contained in which lib 60 public static final int FEATURE_MIDIIO = 1; 61 public static final int FEATURE_PORTS = 2; 62 public static final int FEATURE_DIRECT_AUDIO = 3; 63 64 // SYSTEM CHARACTERISTICS 65 // vary according to hardware architecture 66 67 // intel is little-endian. sparc is big-endian. 68 private static boolean bigEndian; 69 70 static { 71 if(Printer.trace)Printer.trace(">> Platform.java: static"); 72 73 loadLibraries(); 74 readProperties(); 75 } 76 77 78 /** 79 * Private constructor. 80 */ 81 private Platform() { 82 } 83 84 85 // METHODS FOR INTERNAL IMPLEMENTATION USE 86 87 88 /** 89 * Dummy method for forcing initialization. 90 */ 91 static void initialize() { 92 93 if(Printer.trace)Printer.trace("Platform: initialize()"); 94 } 95 96 97 /** 98 * Determine whether the system is big-endian. 99 */ 100 static boolean isBigEndian() { 101 102 return bigEndian; 103 } 104 105 106 // PRIVATE METHODS 107 108 /** 109 * Load the native library or libraries. 110 */ 111 private static void loadLibraries() { 112 if(Printer.trace)Printer.trace(">>Platform.loadLibraries"); 113 114 // load the main library 115 AccessController.doPrivileged((PrivilegedAction<Void>) () -> { 116 System.loadLibrary(libNameMain); 117 return null; 118 }); 119 // just for the heck of it... 120 loadedLibs |= LIB_MAIN; 121 122 // now try to load extra libs. They are defined at compile time in the Makefile 123 // with the define EXTRA_SOUND_JNI_LIBS 124 String extraLibs = nGetExtraLibraries(); 125 // the string is the libraries, separated by white space 126 StringTokenizer st = new StringTokenizer(extraLibs); 127 while (st.hasMoreTokens()) { 128 final String lib = st.nextToken(); 129 try { 130 AccessController.doPrivileged((PrivilegedAction<Void>) () -> { 131 System.loadLibrary(lib); 132 return null; 133 }); 134 135 if (lib.equals(libNameALSA)) { 136 loadedLibs |= LIB_ALSA; 137 if (Printer.debug) Printer.debug("Loaded ALSA lib successfully."); 138 } else if (lib.equals(libNameDSound)) { 139 loadedLibs |= LIB_DSOUND; 140 if (Printer.debug) Printer.debug("Loaded DirectSound lib successfully."); 141 } else { 142 if (Printer.err) Printer.err("Loaded unknown lib '"+lib+"' successfully."); 143 } 144 } catch (Throwable t) { 145 if (Printer.err) Printer.err("Couldn't load library "+lib+": "+t.toString()); 146 } 147 } 148 } 149 150 151 static boolean isMidiIOEnabled() { 152 return isFeatureLibLoaded(FEATURE_MIDIIO); 153 } 154 155 static boolean isPortsEnabled() { 156 return isFeatureLibLoaded(FEATURE_PORTS); 157 } 158 159 static boolean isDirectAudioEnabled() { 160 return isFeatureLibLoaded(FEATURE_DIRECT_AUDIO); 161 } 162 163 private static boolean isFeatureLibLoaded(int feature) { 164 if (Printer.debug) Printer.debug("Platform: Checking for feature "+feature+"..."); 165 int requiredLib = nGetLibraryForFeature(feature); 166 boolean isLoaded = (requiredLib != 0) && ((loadedLibs & requiredLib) == requiredLib); 167 if (Printer.debug) Printer.debug(" ...needs library "+requiredLib+". Result is loaded="+isLoaded); 168 return isLoaded; 169 } 170 171 // the following native methods are implemented in Platform.c 172 private static native boolean nIsBigEndian(); 173 private static native String nGetExtraLibraries(); 174 private static native int nGetLibraryForFeature(int feature); 175 176 /** 177 * Read the required system properties. 178 */ 179 private static void readProperties() { 180 // $$fb 2002-03-06: implement check for endianness in native. Facilitates porting ! 181 bigEndian = nIsBigEndian(); 182 } 183 }