1 /*
   2  * Copyright (c) 1999, 2014, 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     // signed8 (use signed 8-bit values) is true for everything we support except for
  68     // the solaris sbpro card.
  69     // we'll leave it here as a variable; in the future we may need this in java.
  70     // wait, is that true?  i'm not sure.  i think solaris takes unsigned data?
  71     // $$kk: 03.11.99: i think solaris takes unsigned 8-bit or signed 16-bit data....
  72     private static boolean signed8;
  73 
  74     // intel is little-endian.  sparc is big-endian.
  75     private static boolean bigEndian;
  76 
  77     static {
  78         if(Printer.trace)Printer.trace(">> Platform.java: static");
  79 
  80         loadLibraries();
  81         readProperties();
  82     }
  83 
  84 
  85     /**
  86      * Private constructor.
  87      */
  88     private Platform() {
  89     }
  90 
  91 
  92     // METHODS FOR INTERNAL IMPLEMENTATION USE
  93 
  94 
  95     /**
  96      * Dummy method for forcing initialization.
  97      */
  98     static void initialize() {
  99 
 100         if(Printer.trace)Printer.trace("Platform: initialize()");
 101     }
 102 
 103 
 104     /**
 105      * Determine whether the system is big-endian.
 106      */
 107     static boolean isBigEndian() {
 108 
 109         return bigEndian;
 110     }
 111 
 112 
 113     /**
 114      * Determine whether the system takes signed 8-bit data.
 115      */
 116     static boolean isSigned8() {
 117 
 118         return signed8;
 119     }
 120 
 121     // PRIVATE METHODS
 122 
 123     /**
 124      * Load the native library or libraries.
 125      */
 126     private static void loadLibraries() {
 127         if(Printer.trace)Printer.trace(">>Platform.loadLibraries");
 128 
 129         // load the main library
 130         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 131             System.loadLibrary(libNameMain);
 132             return null;
 133         });
 134         // just for the heck of it...
 135         loadedLibs |= LIB_MAIN;
 136 
 137         // now try to load extra libs. They are defined at compile time in the Makefile
 138         // with the define EXTRA_SOUND_JNI_LIBS
 139         String extraLibs = nGetExtraLibraries();
 140         // the string is the libraries, separated by white space
 141         StringTokenizer st = new StringTokenizer(extraLibs);
 142         while (st.hasMoreTokens()) {
 143             final String lib = st.nextToken();
 144             try {
 145                 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 146                     System.loadLibrary(lib);
 147                     return null;
 148                 });
 149 
 150                 if (lib.equals(libNameALSA)) {
 151                     loadedLibs |= LIB_ALSA;
 152                     if (Printer.debug) Printer.debug("Loaded ALSA lib successfully.");
 153                 } else if (lib.equals(libNameDSound)) {
 154                     loadedLibs |= LIB_DSOUND;
 155                     if (Printer.debug) Printer.debug("Loaded DirectSound lib successfully.");
 156                 } else {
 157                     if (Printer.err) Printer.err("Loaded unknown lib '"+lib+"' successfully.");
 158                 }
 159             } catch (Throwable t) {
 160                 if (Printer.err) Printer.err("Couldn't load library "+lib+": "+t.toString());
 161             }
 162         }
 163     }
 164 
 165 
 166     static boolean isMidiIOEnabled() {
 167         return isFeatureLibLoaded(FEATURE_MIDIIO);
 168     }
 169 
 170     static boolean isPortsEnabled() {
 171         return isFeatureLibLoaded(FEATURE_PORTS);
 172     }
 173 
 174     static boolean isDirectAudioEnabled() {
 175         return isFeatureLibLoaded(FEATURE_DIRECT_AUDIO);
 176     }
 177 
 178     private static boolean isFeatureLibLoaded(int feature) {
 179         if (Printer.debug) Printer.debug("Platform: Checking for feature "+feature+"...");
 180         int requiredLib = nGetLibraryForFeature(feature);
 181         boolean isLoaded = (requiredLib != 0) && ((loadedLibs & requiredLib) == requiredLib);
 182         if (Printer.debug) Printer.debug("          ...needs library "+requiredLib+". Result is loaded="+isLoaded);
 183         return isLoaded;
 184     }
 185 
 186     // the following native methods are implemented in Platform.c
 187     private native static boolean nIsBigEndian();
 188     private native static boolean nIsSigned8();
 189     private native static String nGetExtraLibraries();
 190     private native static int nGetLibraryForFeature(int feature);
 191 
 192 
 193     /**
 194      * Read the required system properties.
 195      */
 196     private static void readProperties() {
 197         // $$fb 2002-03-06: implement check for endianness in native. Facilitates porting !
 198         bigEndian = nIsBigEndian();
 199         signed8 = nIsSigned8(); // Solaris on Sparc: signed, all others unsigned
 200     }
 201 }